ComponentsTests.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "tests/ComponentsTests.h"
  2. #include "data/Components.h"
  3. #include "test/Test.h"
  4. using IntComponent = Core::Components<int>;
  5. static void testAddForEach() {
  6. IntComponent c;
  7. int* i1 = nullptr;
  8. int* i2 = nullptr;
  9. int* i3 = nullptr;
  10. CORE_TEST_ERROR(c.put(i1, 1, 10));
  11. CORE_TEST_ERROR(c.put(i2, 5, 20));
  12. CORE_TEST_ERROR(c.put(i3, 10, 30));
  13. CORE_TEST_NOT_NULL(i1);
  14. CORE_TEST_NOT_NULL(i2);
  15. CORE_TEST_NOT_NULL(i3);
  16. if(i1 != nullptr && i2 != nullptr && i3 != nullptr) {
  17. CORE_TEST_EQUAL(10, *i1);
  18. CORE_TEST_EQUAL(20, *i2);
  19. CORE_TEST_EQUAL(30, *i3);
  20. }
  21. auto iter = c.entities();
  22. auto pos = iter.begin();
  23. auto end = iter.end();
  24. CORE_TEST_EQUAL(1, (*pos).entity);
  25. CORE_TEST_EQUAL(10, (*pos).component);
  26. CORE_TEST_TRUE(pos != end);
  27. ++pos;
  28. CORE_TEST_EQUAL(5, (*pos).entity);
  29. CORE_TEST_EQUAL(20, (*pos).component);
  30. CORE_TEST_TRUE(pos != end);
  31. ++pos;
  32. CORE_TEST_EQUAL(10, (*pos).entity);
  33. CORE_TEST_EQUAL(30, (*pos).component);
  34. CORE_TEST_TRUE(pos != end);
  35. ++pos;
  36. CORE_TEST_FALSE(pos != end);
  37. }
  38. static void testAddComponentForEach() {
  39. IntComponent c;
  40. int* i1 = nullptr;
  41. int* i2 = nullptr;
  42. int* i3 = nullptr;
  43. CORE_TEST_ERROR(c.put(i1, 1, 10));
  44. CORE_TEST_ERROR(c.put(i2, 5, 20));
  45. CORE_TEST_ERROR(c.put(i3, 10, 30));
  46. CORE_TEST_NOT_NULL(i1);
  47. CORE_TEST_NOT_NULL(i2);
  48. CORE_TEST_NOT_NULL(i3);
  49. if(i1 != nullptr && i2 != nullptr && i3 != nullptr) {
  50. CORE_TEST_EQUAL(10, *i1);
  51. CORE_TEST_EQUAL(20, *i2);
  52. CORE_TEST_EQUAL(30, *i3);
  53. }
  54. auto iter = c.begin();
  55. CORE_TEST_EQUAL(10, *iter);
  56. CORE_TEST_TRUE(iter != c.end());
  57. ++iter;
  58. CORE_TEST_EQUAL(20, *iter);
  59. CORE_TEST_TRUE(iter != c.end());
  60. ++iter;
  61. CORE_TEST_EQUAL(30, *iter);
  62. CORE_TEST_TRUE(iter != c.end());
  63. ++iter;
  64. CORE_TEST_FALSE(iter != c.end());
  65. const IntComponent c2 = Core::move(c);
  66. auto iter2 = c2.begin();
  67. CORE_TEST_EQUAL(10, *iter2);
  68. CORE_TEST_TRUE(iter2 != c2.end());
  69. ++iter2;
  70. CORE_TEST_EQUAL(20, *iter2);
  71. CORE_TEST_TRUE(iter2 != c2.end());
  72. ++iter2;
  73. CORE_TEST_EQUAL(30, *iter2);
  74. CORE_TEST_TRUE(iter2 != c2.end());
  75. ++iter2;
  76. CORE_TEST_FALSE(iter2 != c2.end());
  77. }
  78. static void testRemove() {
  79. IntComponent c;
  80. CORE_TEST_ERROR(c.add(1, 10));
  81. CORE_TEST_ERROR(c.add(5, 20));
  82. CORE_TEST_ERROR(c.add(10, 30));
  83. CORE_TEST_EQUAL(Core::Error::NOT_FOUND, c.remove(20));
  84. CORE_TEST_ERROR(c.remove(5));
  85. CORE_TEST_EQUAL(Core::Error::NOT_FOUND, c.remove(30));
  86. CORE_TEST_ERROR(c.add(20, 40));
  87. CORE_TEST_ERROR(c.remove(20));
  88. int* i1 = c.search(1);
  89. int* i2 = c.search(5);
  90. int* i3 = c.search(10);
  91. CORE_TEST_NOT_NULL(i1);
  92. CORE_TEST_NULL(i2);
  93. CORE_TEST_NOT_NULL(i3);
  94. if(i1 != nullptr && i3 != nullptr) {
  95. CORE_TEST_EQUAL(10, *i1);
  96. CORE_TEST_EQUAL(30, *i3);
  97. }
  98. CORE_TEST_ERROR(c.remove(10));
  99. i1 = c.search(1);
  100. i2 = c.search(5);
  101. i3 = c.search(10);
  102. CORE_TEST_NOT_NULL(i1);
  103. CORE_TEST_NULL(i2);
  104. CORE_TEST_NULL(i3);
  105. if(i1 != nullptr) {
  106. CORE_TEST_EQUAL(10, *i1);
  107. }
  108. CORE_TEST_ERROR(c.remove(1));
  109. CORE_TEST_NULL(c.search(1));
  110. CORE_TEST_NULL(c.search(5));
  111. CORE_TEST_NULL(c.search(10));
  112. }
  113. void Core::ComponentsTests::test() {
  114. testAddForEach();
  115. testAddComponentForEach();
  116. testRemove();
  117. }