123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #include "tests/ComponentsTests.h"
- #include "data/Components.h"
- #include "test/Test.h"
- using IntComponent = Core::Components<int>;
- static void testAddForEach() {
- IntComponent c;
- int* i1 = nullptr;
- int* i2 = nullptr;
- int* i3 = nullptr;
- CORE_TEST_ERROR(c.put(i1, 1, 10));
- CORE_TEST_ERROR(c.put(i2, 5, 20));
- CORE_TEST_ERROR(c.put(i3, 10, 30));
- CORE_TEST_NOT_NULL(i1);
- CORE_TEST_NOT_NULL(i2);
- CORE_TEST_NOT_NULL(i3);
- if(i1 != nullptr && i2 != nullptr && i3 != nullptr) {
- CORE_TEST_EQUAL(10, *i1);
- CORE_TEST_EQUAL(20, *i2);
- CORE_TEST_EQUAL(30, *i3);
- }
- auto iter = c.entities();
- auto pos = iter.begin();
- auto end = iter.end();
- CORE_TEST_EQUAL(1, (*pos).entity);
- CORE_TEST_EQUAL(10, (*pos).component);
- CORE_TEST_TRUE(pos != end);
- ++pos;
- CORE_TEST_EQUAL(5, (*pos).entity);
- CORE_TEST_EQUAL(20, (*pos).component);
- CORE_TEST_TRUE(pos != end);
- ++pos;
- CORE_TEST_EQUAL(10, (*pos).entity);
- CORE_TEST_EQUAL(30, (*pos).component);
- CORE_TEST_TRUE(pos != end);
- ++pos;
- CORE_TEST_FALSE(pos != end);
- }
- static void testAddComponentForEach() {
- IntComponent c;
- int* i1 = nullptr;
- int* i2 = nullptr;
- int* i3 = nullptr;
- CORE_TEST_ERROR(c.put(i1, 1, 10));
- CORE_TEST_ERROR(c.put(i2, 5, 20));
- CORE_TEST_ERROR(c.put(i3, 10, 30));
- CORE_TEST_NOT_NULL(i1);
- CORE_TEST_NOT_NULL(i2);
- CORE_TEST_NOT_NULL(i3);
- if(i1 != nullptr && i2 != nullptr && i3 != nullptr) {
- CORE_TEST_EQUAL(10, *i1);
- CORE_TEST_EQUAL(20, *i2);
- CORE_TEST_EQUAL(30, *i3);
- }
- auto iter = c.begin();
- CORE_TEST_EQUAL(10, *iter);
- CORE_TEST_TRUE(iter != c.end());
- ++iter;
- CORE_TEST_EQUAL(20, *iter);
- CORE_TEST_TRUE(iter != c.end());
- ++iter;
- CORE_TEST_EQUAL(30, *iter);
- CORE_TEST_TRUE(iter != c.end());
- ++iter;
- CORE_TEST_FALSE(iter != c.end());
- const IntComponent c2 = Core::move(c);
- auto iter2 = c2.begin();
- CORE_TEST_EQUAL(10, *iter2);
- CORE_TEST_TRUE(iter2 != c2.end());
- ++iter2;
- CORE_TEST_EQUAL(20, *iter2);
- CORE_TEST_TRUE(iter2 != c2.end());
- ++iter2;
- CORE_TEST_EQUAL(30, *iter2);
- CORE_TEST_TRUE(iter2 != c2.end());
- ++iter2;
- CORE_TEST_FALSE(iter2 != c2.end());
- }
- static void testRemove() {
- IntComponent c;
- CORE_TEST_ERROR(c.add(1, 10));
- CORE_TEST_ERROR(c.add(5, 20));
- CORE_TEST_ERROR(c.add(10, 30));
- CORE_TEST_EQUAL(Core::Error::NOT_FOUND, c.remove(20));
- CORE_TEST_ERROR(c.remove(5));
- CORE_TEST_EQUAL(Core::Error::NOT_FOUND, c.remove(30));
- CORE_TEST_ERROR(c.add(20, 40));
- CORE_TEST_ERROR(c.remove(20));
- int* i1 = c.search(1);
- int* i2 = c.search(5);
- int* i3 = c.search(10);
- CORE_TEST_NOT_NULL(i1);
- CORE_TEST_NULL(i2);
- CORE_TEST_NOT_NULL(i3);
- if(i1 != nullptr && i3 != nullptr) {
- CORE_TEST_EQUAL(10, *i1);
- CORE_TEST_EQUAL(30, *i3);
- }
- CORE_TEST_ERROR(c.remove(10));
- i1 = c.search(1);
- i2 = c.search(5);
- i3 = c.search(10);
- CORE_TEST_NOT_NULL(i1);
- CORE_TEST_NULL(i2);
- CORE_TEST_NULL(i3);
- if(i1 != nullptr) {
- CORE_TEST_EQUAL(10, *i1);
- }
- CORE_TEST_ERROR(c.remove(1));
- CORE_TEST_NULL(c.search(1));
- CORE_TEST_NULL(c.search(5));
- CORE_TEST_NULL(c.search(10));
- }
- void Core::ComponentsTests::test() {
- testAddForEach();
- testAddComponentForEach();
- testRemove();
- }
|