1
0

component_pool.cpp 5.5 KB

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