ComponentsTests.c 3.2 KB

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