component_pool.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <cstddef>
  2. #include <gtest/gtest.h>
  3. #include <component_pool.hpp>
  4. TEST(ComponentPool, Functionalities) {
  5. using pool_type = entt::ComponentPool<std::uint8_t, int, double>;
  6. pool_type pool{0};
  7. ASSERT_TRUE(pool.empty<int>());
  8. ASSERT_TRUE(pool.empty<double>());
  9. ASSERT_EQ(pool.capacity<int>(), pool_type::size_type{0});
  10. ASSERT_EQ(pool.capacity<double>(), pool_type::size_type{0});
  11. ASSERT_EQ(pool.size<int>(), pool_type::size_type{0});
  12. ASSERT_EQ(pool.size<double>(), pool_type::size_type{0});
  13. ASSERT_EQ(pool.begin<int>(), pool.end<int>());
  14. ASSERT_EQ(pool.begin<double>(), pool.end<double>());
  15. ASSERT_FALSE(pool.has<int>(0));
  16. ASSERT_FALSE(pool.has<double>(0));
  17. }
  18. TEST(ComponentPool, ConstructDestroy) {
  19. using pool_type = entt::ComponentPool<std::uint8_t, double, int>;
  20. pool_type pool{4};
  21. ASSERT_EQ(pool.construct<int>(0, 42), 42);
  22. ASSERT_FALSE(pool.empty<int>());
  23. ASSERT_TRUE(pool.empty<double>());
  24. ASSERT_EQ(pool.capacity<int>(), pool_type::size_type{4});
  25. ASSERT_EQ(pool.capacity<double>(), pool_type::size_type{4});
  26. ASSERT_EQ(pool.size<int>(), pool_type::size_type{1});
  27. ASSERT_EQ(pool.size<double>(), pool_type::size_type{0});
  28. ASSERT_TRUE(pool.has<int>(0));
  29. ASSERT_FALSE(pool.has<double>(0));
  30. ASSERT_FALSE(pool.has<int>(1));
  31. ASSERT_FALSE(pool.has<double>(1));
  32. ASSERT_EQ(pool.construct<int>(1), 0);
  33. ASSERT_FALSE(pool.empty<int>());
  34. ASSERT_TRUE(pool.empty<double>());
  35. ASSERT_EQ(pool.capacity<int>(), pool_type::size_type{4});
  36. ASSERT_EQ(pool.capacity<double>(), pool_type::size_type{4});
  37. ASSERT_EQ(pool.size<int>(), pool_type::size_type{2});
  38. ASSERT_EQ(pool.size<double>(), pool_type::size_type{0});
  39. ASSERT_TRUE(pool.has<int>(0));
  40. ASSERT_FALSE(pool.has<double>(0));
  41. ASSERT_TRUE(pool.has<int>(1));
  42. ASSERT_FALSE(pool.has<double>(1));
  43. ASSERT_NE(pool.get<int>(0), pool.get<int>(1));
  44. ASSERT_NE(&pool.get<int>(0), &pool.get<int>(1));
  45. ASSERT_NO_THROW(pool.destroy<int>(0));
  46. ASSERT_FALSE(pool.empty<int>());
  47. ASSERT_TRUE(pool.empty<double>());
  48. ASSERT_EQ(pool.capacity<int>(), pool_type::size_type{4});
  49. ASSERT_EQ(pool.capacity<double>(), pool_type::size_type{4});
  50. ASSERT_EQ(pool.size<int>(), pool_type::size_type{1});
  51. ASSERT_EQ(pool.size<double>(), pool_type::size_type{0});
  52. ASSERT_FALSE(pool.has<int>(0));
  53. ASSERT_FALSE(pool.has<double>(0));
  54. ASSERT_TRUE(pool.has<int>(1));
  55. ASSERT_FALSE(pool.has<double>(1));
  56. ASSERT_NO_THROW(pool.destroy<int>(1));
  57. ASSERT_TRUE(pool.empty<int>());
  58. ASSERT_TRUE(pool.empty<double>());
  59. ASSERT_EQ(pool.capacity<int>(), pool_type::size_type{4});
  60. ASSERT_EQ(pool.capacity<double>(), pool_type::size_type{4});
  61. ASSERT_EQ(pool.size<int>(), pool_type::size_type{0});
  62. ASSERT_EQ(pool.size<int>(), pool_type::size_type{0});
  63. ASSERT_FALSE(pool.has<int>(0));
  64. ASSERT_FALSE(pool.has<double>(0));
  65. ASSERT_FALSE(pool.has<int>(1));
  66. ASSERT_FALSE(pool.has<double>(1));
  67. int *comp[] = {
  68. &pool.construct<int>(0, 0),
  69. &pool.construct<int>(1, 1),
  70. nullptr,
  71. &pool.construct<int>(3, 3)
  72. };
  73. ASSERT_FALSE(pool.empty<int>());
  74. ASSERT_TRUE(pool.empty<double>());
  75. ASSERT_EQ(pool.capacity<int>(), pool_type::size_type{4});
  76. ASSERT_EQ(pool.capacity<double>(), pool_type::size_type{4});
  77. ASSERT_EQ(pool.size<int>(), pool_type::size_type{3});
  78. ASSERT_EQ(pool.size<double>(), pool_type::size_type{0});
  79. ASSERT_TRUE(pool.has<int>(0));
  80. ASSERT_FALSE(pool.has<double>(0));
  81. ASSERT_TRUE(pool.has<int>(1));
  82. ASSERT_FALSE(pool.has<double>(1));
  83. ASSERT_FALSE(pool.has<int>(2));
  84. ASSERT_FALSE(pool.has<double>(2));
  85. ASSERT_TRUE(pool.has<int>(3));
  86. ASSERT_FALSE(pool.has<double>(3));
  87. ASSERT_EQ(&pool.get<int>(0), comp[0]);
  88. ASSERT_EQ(&pool.get<int>(1), comp[1]);
  89. ASSERT_EQ(&pool.get<int>(3), comp[3]);
  90. ASSERT_EQ(pool.get<int>(0), 0);
  91. ASSERT_EQ(pool.get<int>(1), 1);
  92. ASSERT_EQ(pool.get<int>(3), 3);
  93. ASSERT_NO_THROW(pool.destroy<int>(0));
  94. ASSERT_NO_THROW(pool.destroy<int>(1));
  95. ASSERT_NO_THROW(pool.destroy<int>(3));
  96. }
  97. TEST(ComponentPool, HasGet) {
  98. using pool_type = entt::ComponentPool<std::uint8_t, int, char>;
  99. pool_type pool;
  100. const pool_type &cpool = pool;
  101. int &comp = pool.construct<int>(0, 42);
  102. ASSERT_EQ(pool.get<int>(0), comp);
  103. ASSERT_EQ(pool.get<int>(0), 42);
  104. ASSERT_TRUE(pool.has<int>(0));
  105. ASSERT_EQ(cpool.get<int>(0), comp);
  106. ASSERT_EQ(cpool.get<int>(0), 42);
  107. ASSERT_TRUE(cpool.has<int>(0));
  108. ASSERT_NO_THROW(pool.destroy<int>(0));
  109. }
  110. TEST(ComponentPool, BeginEndReset) {
  111. using pool_type = entt::ComponentPool<std::uint8_t, int, char>;
  112. pool_type pool{2};
  113. ASSERT_EQ(pool.construct<int>(0, 0), 0);
  114. ASSERT_EQ(pool.construct<int>(2, 2), 2);
  115. ASSERT_EQ(pool.construct<int>(3, 3), 3);
  116. ASSERT_EQ(pool.construct<int>(1, 1), 1);
  117. ASSERT_EQ(pool.size<int>(), decltype(pool.size<int>()){4});
  118. ASSERT_EQ(*(pool.begin<int>()+0), typename pool_type::entity_type{0});
  119. ASSERT_EQ(*(pool.begin<int>()+1), typename pool_type::entity_type{2});
  120. ASSERT_EQ(*(pool.begin<int>()+2), typename pool_type::entity_type{3});
  121. ASSERT_EQ(*(pool.begin<int>()+3), typename pool_type::entity_type{1});
  122. pool.destroy<int>(2);
  123. ASSERT_EQ(pool.size<int>(), decltype(pool.size<int>()){3});
  124. ASSERT_EQ(*(pool.begin<int>()+0), typename pool_type::entity_type{0});
  125. ASSERT_EQ(*(pool.begin<int>()+1), typename pool_type::entity_type{1});
  126. ASSERT_EQ(*(pool.begin<int>()+2), typename pool_type::entity_type{3});
  127. ASSERT_EQ(pool.construct<char>(0, 'c'), 'c');
  128. ASSERT_FALSE(pool.empty<int>());
  129. ASSERT_FALSE(pool.empty<char>());
  130. ASSERT_NO_THROW(pool.reset<char>());
  131. ASSERT_FALSE(pool.empty<int>());
  132. ASSERT_TRUE(pool.empty<char>());
  133. ASSERT_NO_THROW(pool.reset());
  134. ASSERT_TRUE(pool.empty<int>());
  135. ASSERT_TRUE(pool.empty<char>());
  136. }