ComponentsTests.cpp 3.2 KB

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