ComponentsTests.cpp 3.2 KB

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