123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include "../Tests.h"
- #include "core/Components.h"
- static void testAddForEach() {
- CoreComponents c;
- coreInitComponents(&c, sizeof(int));
- int* i1 = coreComponentsGetOrAdd(&c, 1);
- if(CORE_TEST_NOT_NULL(i1)) {
- *i1 = 10;
- }
- int* i2 = coreComponentsGetOrAdd(&c, 1);
- if(CORE_TEST_NOT_NULL(i2)) {
- *i2 = 15;
- }
- int* i3 = coreComponentsGetOrAdd(&c, 5);
- if(CORE_TEST_NOT_NULL(i3)) {
- *i3 = 20;
- }
- int* i4 = coreComponentsGetOrAdd(&c, 10);
- if(CORE_TEST_NOT_NULL(i4)) {
- *i4 = 30;
- }
- CORE_TEST_TRUE(i1 == i2);
- CoreComponentIterator iter;
- coreInitComponentsIterator(&iter, &c);
- if(CORE_TEST_TRUE(coreComponentsHasNext(&iter))) {
- CoreComponentNode* n = coreComponentsNext(&iter);
- CORE_TEST_SIZE(1, n->entity);
- CORE_TEST_INT(15, *(int*)n->component);
- }
- if(CORE_TEST_TRUE(coreComponentsHasNext(&iter))) {
- CoreComponentNode* n = coreComponentsNext(&iter);
- CORE_TEST_SIZE(5, n->entity);
- CORE_TEST_INT(20, *(int*)n->component);
- }
- if(CORE_TEST_TRUE(coreComponentsHasNext(&iter))) {
- CoreComponentNode* n = coreComponentsNext(&iter);
- CORE_TEST_SIZE(10, n->entity);
- CORE_TEST_INT(30, *(int*)n->component);
- }
- CORE_TEST_FALSE(coreComponentsHasNext(&iter));
- coreDestroyComponents(&c);
- }
- static void testAddComponentForEach() {
- CoreComponents c;
- coreInitComponents(&c, sizeof(int));
- int* i1 = coreComponentsGetOrAdd(&c, 1);
- if(CORE_TEST_NOT_NULL(i1)) {
- *i1 = 10;
- }
- int* i2 = coreComponentsGetOrAdd(&c, 5);
- if(CORE_TEST_NOT_NULL(i2)) {
- *i2 = 20;
- }
- int* i3 = coreComponentsGetOrAdd(&c, 10);
- if(CORE_TEST_NOT_NULL(i3)) {
- *i3 = 30;
- }
- int* iter = coreComponentsBegin(&c);
- int* end = coreComponentsEnd(&c);
- if(CORE_TEST_TRUE(iter != end)) {
- CORE_TEST_INT(10, *(iter++));
- }
- if(CORE_TEST_TRUE(iter != end)) {
- CORE_TEST_INT(20, *(iter++));
- }
- if(CORE_TEST_TRUE(iter != end)) {
- CORE_TEST_INT(30, *(iter++));
- }
- CORE_TEST_TRUE(iter == end);
- coreDestroyComponents(&c);
- }
- static void testRemove() {
- CoreComponents c;
- coreInitComponents(&c, sizeof(int));
- *(int*)coreComponentsGetOrAdd(&c, 1) = 10;
- *(int*)coreComponentsGetOrAdd(&c, 5) = 20;
- *(int*)coreComponentsGetOrAdd(&c, 10) = 30;
- CORE_TEST_FALSE(coreComponentsRemove(&c, 20));
- CORE_TEST_TRUE(coreComponentsRemove(&c, 5));
- CORE_TEST_FALSE(coreComponentsRemove(&c, 30));
- *(int*)coreComponentsGetOrAdd(&c, 20) = 40;
- CORE_TEST_TRUE(coreComponentsRemove(&c, 20));
- int* i1 = coreComponentsSearch(&c, 1);
- int* i3 = coreComponentsSearch(&c, 10);
- CORE_TEST_NULL(coreComponentsSearch(&c, 5));
- if(CORE_TEST_NOT_NULL(i1) && CORE_TEST_NOT_NULL(i3)) {
- CORE_TEST_INT(10, *i1);
- CORE_TEST_INT(30, *i3);
- }
- CORE_TEST_TRUE(coreComponentsRemove(&c, 10));
- i1 = coreComponentsSearch(&c, 1);
- CORE_TEST_NULL(coreComponentsSearch(&c, 5));
- CORE_TEST_NULL(coreComponentsSearch(&c, 10));
- if(CORE_TEST_NOT_NULL(i1)) {
- CORE_TEST_INT(10, *i1);
- }
- CORE_TEST_TRUE(coreComponentsRemove(&c, 1));
- CORE_TEST_NULL(coreComponentsSearch(&c, 1));
- CORE_TEST_NULL(coreComponentsSearch(&c, 5));
- CORE_TEST_NULL(coreComponentsSearch(&c, 10));
- coreDestroyComponents(&c);
- }
- void testComponents() {
- testAddForEach();
- testAddComponentForEach();
- testRemove();
- }
|