ComponentsTests.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "../../src/ErrorSimulator.hpp"
  2. #include "../Tests.hpp"
  3. #include "core/data/Components.hpp"
  4. template class Core::Components<int>;
  5. using IntComponent = Core::Components<int>;
  6. static void testAddForEach() {
  7. IntComponent c;
  8. int* i1 = nullptr;
  9. int* i2 = nullptr;
  10. int* i3 = nullptr;
  11. CORE_TEST_TRUE(c.put(i1, 1, 10));
  12. CORE_TEST_FALSE(c.put(i1, 1, 20));
  13. CORE_TEST_TRUE(c.put(i2, 5, 20));
  14. CORE_TEST_TRUE(c.put(i3, 10, 30));
  15. CORE_TEST_NOT_NULL(i1);
  16. CORE_TEST_NOT_NULL(i2);
  17. CORE_TEST_NOT_NULL(i3);
  18. if(i1 != nullptr && i2 != nullptr && i3 != nullptr) {
  19. CORE_TEST_EQUAL(10, *i1);
  20. CORE_TEST_EQUAL(20, *i2);
  21. CORE_TEST_EQUAL(30, *i3);
  22. }
  23. auto iter = c.entities();
  24. auto pos = iter.begin();
  25. auto end = iter.end();
  26. CORE_TEST_EQUAL(1, (*pos).entity);
  27. CORE_TEST_EQUAL(10, (*pos).component);
  28. CORE_TEST_TRUE(pos != end);
  29. ++pos;
  30. CORE_TEST_EQUAL(5, (*pos).entity);
  31. CORE_TEST_EQUAL(20, (*pos).component);
  32. CORE_TEST_TRUE(pos != end);
  33. ++pos;
  34. CORE_TEST_EQUAL(10, (*pos).entity);
  35. CORE_TEST_EQUAL(30, (*pos).component);
  36. CORE_TEST_TRUE(pos != end);
  37. ++pos;
  38. CORE_TEST_FALSE(pos != end);
  39. }
  40. static void testAddConstForEach() {
  41. IntComponent c;
  42. int* i1 = nullptr;
  43. int* i2 = nullptr;
  44. int* i3 = nullptr;
  45. CORE_TEST_TRUE(c.put(i1, 1, 10));
  46. CORE_TEST_TRUE(c.put(i2, 5, 20));
  47. CORE_TEST_TRUE(c.put(i3, 10, 30));
  48. auto iter = static_cast<const IntComponent&>(c).entities();
  49. auto pos = iter.begin();
  50. auto end = iter.end();
  51. CORE_TEST_EQUAL(1, (*pos).entity);
  52. CORE_TEST_EQUAL(10, (*pos).component);
  53. CORE_TEST_TRUE(pos != end);
  54. ++pos;
  55. CORE_TEST_EQUAL(5, (*pos).entity);
  56. CORE_TEST_EQUAL(20, (*pos).component);
  57. CORE_TEST_TRUE(pos != end);
  58. ++pos;
  59. CORE_TEST_EQUAL(10, (*pos).entity);
  60. CORE_TEST_EQUAL(30, (*pos).component);
  61. CORE_TEST_TRUE(pos != end);
  62. ++pos;
  63. CORE_TEST_FALSE(pos != end);
  64. }
  65. static void testAddComponentForEach() {
  66. IntComponent c;
  67. int* i1 = nullptr;
  68. int* i2 = nullptr;
  69. int* i3 = nullptr;
  70. CORE_TEST_TRUE(c.put(i1, 1, 10));
  71. CORE_TEST_TRUE(c.put(i2, 5, 20));
  72. CORE_TEST_TRUE(c.put(i3, 10, 30));
  73. CORE_TEST_NOT_NULL(i1);
  74. CORE_TEST_NOT_NULL(i2);
  75. CORE_TEST_NOT_NULL(i3);
  76. if(i1 != nullptr && i2 != nullptr && i3 != nullptr) {
  77. CORE_TEST_EQUAL(10, *i1);
  78. CORE_TEST_EQUAL(20, *i2);
  79. CORE_TEST_EQUAL(30, *i3);
  80. }
  81. auto iter = c.begin();
  82. CORE_TEST_EQUAL(10, *iter);
  83. CORE_TEST_TRUE(iter != c.end());
  84. ++iter;
  85. CORE_TEST_EQUAL(20, *iter);
  86. CORE_TEST_TRUE(iter != c.end());
  87. ++iter;
  88. CORE_TEST_EQUAL(30, *iter);
  89. CORE_TEST_TRUE(iter != c.end());
  90. ++iter;
  91. CORE_TEST_FALSE(iter != c.end());
  92. const IntComponent c2 = Core::move(c);
  93. auto iter2 = c2.begin();
  94. CORE_TEST_EQUAL(10, *iter2);
  95. CORE_TEST_TRUE(iter2 != c2.end());
  96. ++iter2;
  97. CORE_TEST_EQUAL(20, *iter2);
  98. CORE_TEST_TRUE(iter2 != c2.end());
  99. ++iter2;
  100. CORE_TEST_EQUAL(30, *iter2);
  101. CORE_TEST_TRUE(iter2 != c2.end());
  102. ++iter2;
  103. CORE_TEST_FALSE(iter2 != c2.end());
  104. }
  105. static void testRemove() {
  106. IntComponent c;
  107. CORE_TEST_TRUE(c.add(1, 10));
  108. CORE_TEST_TRUE(c.add(5, 20));
  109. CORE_TEST_TRUE(c.add(10, 30));
  110. CORE_TEST_FALSE(c.remove(20));
  111. CORE_TEST_TRUE(c.remove(5));
  112. CORE_TEST_FALSE(c.remove(30));
  113. CORE_TEST_TRUE(c.add(20, 40));
  114. CORE_TEST_TRUE(c.remove(20));
  115. int* i1 = c.search(1);
  116. int* i2 = c.search(5);
  117. int* i3 = c.search(10);
  118. CORE_TEST_NOT_NULL(i1);
  119. CORE_TEST_NULL(i2);
  120. CORE_TEST_NOT_NULL(i3);
  121. if(i1 != nullptr && i3 != nullptr) {
  122. CORE_TEST_EQUAL(10, *i1);
  123. CORE_TEST_EQUAL(30, *i3);
  124. }
  125. CORE_TEST_TRUE(c.remove(10));
  126. i1 = c.search(1);
  127. i2 = c.search(5);
  128. i3 = c.search(10);
  129. CORE_TEST_NOT_NULL(i1);
  130. CORE_TEST_NULL(i2);
  131. CORE_TEST_NULL(i3);
  132. if(i1 != nullptr) {
  133. CORE_TEST_EQUAL(10, *i1);
  134. }
  135. CORE_TEST_TRUE(c.remove(1));
  136. CORE_TEST_NULL(c.search(1));
  137. CORE_TEST_NULL(c.search(5));
  138. CORE_TEST_NULL(c.search(10));
  139. }
  140. static void testConstSearch() {
  141. IntComponent c;
  142. int* i = nullptr;
  143. CORE_TEST_TRUE(c.put(i, 1, 10));
  144. const IntComponent& cc = c;
  145. const int* component = cc.search(1);
  146. if(CORE_TEST_TRUE(component != nullptr)) {
  147. CORE_TEST_EQUAL(10, *component);
  148. }
  149. CORE_TEST_NULL(cc.search(2));
  150. }
  151. void Core::testComponents() {
  152. testAddForEach();
  153. testAddConstForEach();
  154. testAddComponentForEach();
  155. testRemove();
  156. testConstSearch();
  157. }