ComponentsTests.c 3.4 KB

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