ComponentsTests.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #define IMPORT_CORE
  2. #include "../Tests.h"
  3. #include "core/Components.h"
  4. LIST(int, Int)
  5. COMPONENTS(int, Int)
  6. static void testAddForEach() {
  7. Components c;
  8. initComponents(&c);
  9. int* i1 = getOrAddComponent(&c, 1);
  10. if(TEST_NOT_NULL(i1)) {
  11. *i1 = 10;
  12. }
  13. int* i2 = getOrAddComponent(&c, 1);
  14. if(TEST_NOT_NULL(i2)) {
  15. *i2 = 15;
  16. }
  17. int* i3 = getOrAddComponent(&c, 5);
  18. if(TEST_NOT_NULL(i3)) {
  19. *i3 = 20;
  20. }
  21. int* i4 = getOrAddComponent(&c, 10);
  22. if(TEST_NOT_NULL(i4)) {
  23. *i4 = 30;
  24. }
  25. TEST_TRUE(i1 == i2);
  26. ComponentIterator iter;
  27. initComponentIterator(&iter, &c);
  28. if(TEST_TRUE(hasNextComponentNode(&iter))) {
  29. ComponentNode* n = nextComponentNode(&iter);
  30. TEST_SIZE(1, n->entity);
  31. TEST_INT(15, *n->component);
  32. }
  33. if(TEST_TRUE(hasNextComponentNode(&iter))) {
  34. ComponentNode* n = nextComponentNode(&iter);
  35. TEST_SIZE(5, n->entity);
  36. TEST_INT(20, *n->component);
  37. }
  38. if(TEST_TRUE(hasNextComponentNode(&iter))) {
  39. ComponentNode* n = nextComponentNode(&iter);
  40. TEST_SIZE(10, n->entity);
  41. TEST_INT(30, *n->component);
  42. }
  43. TEST_FALSE(hasNextComponentNode(&iter));
  44. destroyComponents(&c);
  45. }
  46. static void testAddComponentForEach() {
  47. Components c;
  48. initComponents(&c);
  49. int* i1 = getOrAddComponent(&c, 1);
  50. if(TEST_NOT_NULL(i1)) {
  51. *i1 = 10;
  52. }
  53. int* i2 = getOrAddComponent(&c, 5);
  54. if(TEST_NOT_NULL(i2)) {
  55. *i2 = 20;
  56. }
  57. int* i3 = getOrAddComponent(&c, 10);
  58. if(TEST_NOT_NULL(i3)) {
  59. *i3 = 30;
  60. }
  61. int* iter = getComponentsStart(&c);
  62. int* end = getComponentsEnd(&c);
  63. if(TEST_TRUE(iter != end)) {
  64. TEST_INT(10, *(iter++));
  65. }
  66. if(TEST_TRUE(iter != end)) {
  67. TEST_INT(20, *(iter++));
  68. }
  69. if(TEST_TRUE(iter != end)) {
  70. TEST_INT(30, *(iter++));
  71. }
  72. TEST_TRUE(iter == end);
  73. destroyComponents(&c);
  74. }
  75. static void testRemove() {
  76. Components c;
  77. initComponents(&c);
  78. *getOrAddComponent(&c, 1) = 10;
  79. *getOrAddComponent(&c, 5) = 20;
  80. *getOrAddComponent(&c, 10) = 30;
  81. TEST_FALSE(removeComponent(&c, 20));
  82. TEST_TRUE(removeComponent(&c, 5));
  83. TEST_FALSE(removeComponent(&c, 30));
  84. *getOrAddComponent(&c, 20) = 40;
  85. TEST_TRUE(removeComponent(&c, 20));
  86. int* i1 = searchComponent(&c, 1);
  87. int* i3 = searchComponent(&c, 10);
  88. TEST_NULL(searchComponent(&c, 5));
  89. if(TEST_NOT_NULL(i1) && TEST_NOT_NULL(i3)) {
  90. TEST_INT(10, *i1);
  91. TEST_INT(30, *i3);
  92. }
  93. TEST_TRUE(removeComponent(&c, 10));
  94. i1 = searchComponent(&c, 1);
  95. TEST_NULL(searchComponent(&c, 5));
  96. TEST_NULL(searchComponent(&c, 10));
  97. if(TEST_NOT_NULL(i1)) {
  98. TEST_INT(10, *i1);
  99. }
  100. TEST_TRUE(removeComponent(&c, 1));
  101. TEST_NULL(searchComponent(&c, 1));
  102. TEST_NULL(searchComponent(&c, 5));
  103. TEST_NULL(searchComponent(&c, 10));
  104. destroyComponents(&c);
  105. }
  106. void testComponents() {
  107. testAddForEach();
  108. testAddComponentForEach();
  109. testRemove();
  110. }