ComponentsTests.cpp 3.4 KB

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