123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include "../Tests.h"
- #include "core/Components.h"
- static void testAddForEach() {
- Components c;
- initComponents(&c, sizeof(int));
- int* i1 = getOrAddComponent(&c, 1);
- if(TEST_NOT_NULL(i1)) {
- *i1 = 10;
- }
- int* i2 = getOrAddComponent(&c, 1);
- if(TEST_NOT_NULL(i2)) {
- *i2 = 15;
- }
- int* i3 = getOrAddComponent(&c, 5);
- if(TEST_NOT_NULL(i3)) {
- *i3 = 20;
- }
- int* i4 = getOrAddComponent(&c, 10);
- if(TEST_NOT_NULL(i4)) {
- *i4 = 30;
- }
- TEST_TRUE(i1 == i2);
- ComponentIterator iter;
- initComponentIterator(&iter, &c);
- if(TEST_TRUE(hasNextComponentNode(&iter))) {
- ComponentNode* n = nextComponentNode(&iter);
- TEST_SIZE(1, n->entity);
- TEST_INT(15, *(int*)n->component);
- }
- if(TEST_TRUE(hasNextComponentNode(&iter))) {
- ComponentNode* n = nextComponentNode(&iter);
- TEST_SIZE(5, n->entity);
- TEST_INT(20, *(int*)n->component);
- }
- if(TEST_TRUE(hasNextComponentNode(&iter))) {
- ComponentNode* n = nextComponentNode(&iter);
- TEST_SIZE(10, n->entity);
- TEST_INT(30, *(int*)n->component);
- }
- TEST_FALSE(hasNextComponentNode(&iter));
- destroyComponents(&c);
- }
- static void testAddComponentForEach() {
- Components c;
- initComponents(&c, sizeof(int));
- int* i1 = getOrAddComponent(&c, 1);
- if(TEST_NOT_NULL(i1)) {
- *i1 = 10;
- }
- int* i2 = getOrAddComponent(&c, 5);
- if(TEST_NOT_NULL(i2)) {
- *i2 = 20;
- }
- int* i3 = getOrAddComponent(&c, 10);
- if(TEST_NOT_NULL(i3)) {
- *i3 = 30;
- }
- int* iter = getComponentsStart(&c);
- int* end = getComponentsEnd(&c);
- if(TEST_TRUE(iter != end)) {
- TEST_INT(10, *(iter++));
- }
- if(TEST_TRUE(iter != end)) {
- TEST_INT(20, *(iter++));
- }
- if(TEST_TRUE(iter != end)) {
- TEST_INT(30, *(iter++));
- }
- TEST_TRUE(iter == end);
- destroyComponents(&c);
- }
- static void testRemove() {
- Components c;
- initComponents(&c, sizeof(int));
- *(int*)getOrAddComponent(&c, 1) = 10;
- *(int*)getOrAddComponent(&c, 5) = 20;
- *(int*)getOrAddComponent(&c, 10) = 30;
- TEST_FALSE(removeComponent(&c, 20));
- TEST_TRUE(removeComponent(&c, 5));
- TEST_FALSE(removeComponent(&c, 30));
- *(int*)getOrAddComponent(&c, 20) = 40;
- TEST_TRUE(removeComponent(&c, 20));
- int* i1 = searchComponent(&c, 1);
- int* i3 = searchComponent(&c, 10);
- TEST_NULL(searchComponent(&c, 5));
- if(TEST_NOT_NULL(i1) && TEST_NOT_NULL(i3)) {
- TEST_INT(10, *i1);
- TEST_INT(30, *i3);
- }
- TEST_TRUE(removeComponent(&c, 10));
- i1 = searchComponent(&c, 1);
- TEST_NULL(searchComponent(&c, 5));
- TEST_NULL(searchComponent(&c, 10));
- if(TEST_NOT_NULL(i1)) {
- TEST_INT(10, *i1);
- }
- TEST_TRUE(removeComponent(&c, 1));
- TEST_NULL(searchComponent(&c, 1));
- TEST_NULL(searchComponent(&c, 5));
- TEST_NULL(searchComponent(&c, 10));
- destroyComponents(&c);
- }
- void testComponents() {
- testAddForEach();
- testAddComponentForEach();
- testRemove();
- }
|