ComponentsTests.c 3.0 KB

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