ComponentsTests.c 3.0 KB

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