ComponentsTests.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_ERROR(c.put(i1, 1, 10));
  12. CORE_TEST_ERROR(c.put(i2, 5, 20));
  13. CORE_TEST_ERROR(c.put(i3, 10, 30));
  14. CORE_TEST_NOT_NULL(i1);
  15. CORE_TEST_NOT_NULL(i2);
  16. CORE_TEST_NOT_NULL(i3);
  17. if(i1 != nullptr && i2 != nullptr && i3 != nullptr) {
  18. CORE_TEST_EQUAL(10, *i1);
  19. CORE_TEST_EQUAL(20, *i2);
  20. CORE_TEST_EQUAL(30, *i3);
  21. }
  22. auto iter = c.entities();
  23. auto pos = iter.begin();
  24. auto end = iter.end();
  25. CORE_TEST_EQUAL(1, (*pos).entity);
  26. CORE_TEST_EQUAL(10, (*pos).component);
  27. CORE_TEST_TRUE(pos != end);
  28. ++pos;
  29. CORE_TEST_EQUAL(5, (*pos).entity);
  30. CORE_TEST_EQUAL(20, (*pos).component);
  31. CORE_TEST_TRUE(pos != end);
  32. ++pos;
  33. CORE_TEST_EQUAL(10, (*pos).entity);
  34. CORE_TEST_EQUAL(30, (*pos).component);
  35. CORE_TEST_TRUE(pos != end);
  36. ++pos;
  37. CORE_TEST_FALSE(pos != end);
  38. }
  39. static void testAddConstForEach() {
  40. IntComponent c;
  41. int* i1 = nullptr;
  42. int* i2 = nullptr;
  43. int* i3 = nullptr;
  44. CORE_TEST_ERROR(c.put(i1, 1, 10));
  45. CORE_TEST_ERROR(c.put(i2, 5, 20));
  46. CORE_TEST_ERROR(c.put(i3, 10, 30));
  47. auto iter = static_cast<const IntComponent&>(c).entities();
  48. auto pos = iter.begin();
  49. auto end = iter.end();
  50. CORE_TEST_EQUAL(1, (*pos).entity);
  51. CORE_TEST_EQUAL(10, (*pos).component);
  52. CORE_TEST_TRUE(pos != end);
  53. ++pos;
  54. CORE_TEST_EQUAL(5, (*pos).entity);
  55. CORE_TEST_EQUAL(20, (*pos).component);
  56. CORE_TEST_TRUE(pos != end);
  57. ++pos;
  58. CORE_TEST_EQUAL(10, (*pos).entity);
  59. CORE_TEST_EQUAL(30, (*pos).component);
  60. CORE_TEST_TRUE(pos != end);
  61. ++pos;
  62. CORE_TEST_FALSE(pos != end);
  63. }
  64. static void testAddComponentForEach() {
  65. IntComponent c;
  66. int* i1 = nullptr;
  67. int* i2 = nullptr;
  68. int* i3 = nullptr;
  69. CORE_TEST_ERROR(c.put(i1, 1, 10));
  70. CORE_TEST_ERROR(c.put(i2, 5, 20));
  71. CORE_TEST_ERROR(c.put(i3, 10, 30));
  72. CORE_TEST_NOT_NULL(i1);
  73. CORE_TEST_NOT_NULL(i2);
  74. CORE_TEST_NOT_NULL(i3);
  75. if(i1 != nullptr && i2 != nullptr && i3 != nullptr) {
  76. CORE_TEST_EQUAL(10, *i1);
  77. CORE_TEST_EQUAL(20, *i2);
  78. CORE_TEST_EQUAL(30, *i3);
  79. }
  80. auto iter = c.begin();
  81. CORE_TEST_EQUAL(10, *iter);
  82. CORE_TEST_TRUE(iter != c.end());
  83. ++iter;
  84. CORE_TEST_EQUAL(20, *iter);
  85. CORE_TEST_TRUE(iter != c.end());
  86. ++iter;
  87. CORE_TEST_EQUAL(30, *iter);
  88. CORE_TEST_TRUE(iter != c.end());
  89. ++iter;
  90. CORE_TEST_FALSE(iter != c.end());
  91. const IntComponent c2 = Core::move(c);
  92. auto iter2 = c2.begin();
  93. CORE_TEST_EQUAL(10, *iter2);
  94. CORE_TEST_TRUE(iter2 != c2.end());
  95. ++iter2;
  96. CORE_TEST_EQUAL(20, *iter2);
  97. CORE_TEST_TRUE(iter2 != c2.end());
  98. ++iter2;
  99. CORE_TEST_EQUAL(30, *iter2);
  100. CORE_TEST_TRUE(iter2 != c2.end());
  101. ++iter2;
  102. CORE_TEST_FALSE(iter2 != c2.end());
  103. }
  104. static void testRemove() {
  105. IntComponent c;
  106. CORE_TEST_ERROR(c.add(1, 10));
  107. CORE_TEST_ERROR(c.add(5, 20));
  108. CORE_TEST_ERROR(c.add(10, 30));
  109. CORE_TEST_EQUAL(Core::Error::NOT_FOUND, c.remove(20));
  110. CORE_TEST_ERROR(c.remove(5));
  111. CORE_TEST_EQUAL(Core::Error::NOT_FOUND, c.remove(30));
  112. CORE_TEST_ERROR(c.add(20, 40));
  113. CORE_TEST_ERROR(c.remove(20));
  114. int* i1 = c.search(1);
  115. int* i2 = c.search(5);
  116. int* i3 = c.search(10);
  117. CORE_TEST_NOT_NULL(i1);
  118. CORE_TEST_NULL(i2);
  119. CORE_TEST_NOT_NULL(i3);
  120. if(i1 != nullptr && i3 != nullptr) {
  121. CORE_TEST_EQUAL(10, *i1);
  122. CORE_TEST_EQUAL(30, *i3);
  123. }
  124. CORE_TEST_ERROR(c.remove(10));
  125. i1 = c.search(1);
  126. i2 = c.search(5);
  127. i3 = c.search(10);
  128. CORE_TEST_NOT_NULL(i1);
  129. CORE_TEST_NULL(i2);
  130. CORE_TEST_NULL(i3);
  131. if(i1 != nullptr) {
  132. CORE_TEST_EQUAL(10, *i1);
  133. }
  134. CORE_TEST_ERROR(c.remove(1));
  135. CORE_TEST_NULL(c.search(1));
  136. CORE_TEST_NULL(c.search(5));
  137. CORE_TEST_NULL(c.search(10));
  138. }
  139. static void testOutOfMemory() {
  140. #ifdef ERROR_SIMULATOR
  141. IntComponent c;
  142. int* i1 = nullptr;
  143. int memFails = 0;
  144. for(int i = 0; i < 40; i++) {
  145. Core::Fail::leftAllocations = 2;
  146. Core::Error e = c.put(i1, 1, 10);
  147. if(e == Core::Error::OUT_OF_MEMORY) {
  148. memFails++;
  149. }
  150. }
  151. Core::Fail::leftAllocations = -1;
  152. CORE_TEST_TRUE(memFails != 0);
  153. #endif
  154. }
  155. static void testConstSearch() {
  156. IntComponent c;
  157. int* i = nullptr;
  158. CORE_TEST_ERROR(c.put(i, 1, 10));
  159. const IntComponent& cc = c;
  160. const int* component = cc.search(1);
  161. if(CORE_TEST_TRUE(component != nullptr)) {
  162. CORE_TEST_EQUAL(10, *component);
  163. }
  164. CORE_TEST_NULL(cc.search(2));
  165. }
  166. void Core::testComponents(bool outOfMemoryTest) {
  167. testAddForEach();
  168. testAddConstForEach();
  169. testAddComponentForEach();
  170. testRemove();
  171. if(outOfMemoryTest) {
  172. testOutOfMemory();
  173. }
  174. testConstSearch();
  175. }