ComponentsTests.c 3.0 KB

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