#include "../Tests.hpp" #include "core/Components.hpp" #include "core/Test.hpp" template class Core::Components; using IntComponent = Core::Components; template static void testEntityIterator(T c) { auto iter = c.entities(); auto pos = iter.begin(); auto end = iter.end(); if(TEST_TRUE(pos != end)) { TEST(1, (*pos).entity); TEST(10, (*pos).component); TEST_TRUE(pos != end); ++pos; } if(TEST_TRUE(pos != end)) { TEST(5, (*pos).entity); TEST(20, (*pos).component); TEST_TRUE(pos != end); ++pos; } if(TEST_TRUE(pos != end)) { TEST(10, (*pos).entity); TEST(30, (*pos).component); TEST_TRUE(pos != end); ++pos; } TEST_FALSE(pos != end); } static void testAddForEach() { IntComponent c; int* i1 = nullptr; int* i2 = nullptr; int* i3 = nullptr; TEST_TRUE(c.put(i1, 1, 10)); TEST_FALSE(c.put(i1, 1, 20)); TEST_TRUE(c.put(i2, 5, 20)); TEST_TRUE(c.put(i3, 10, 30)); if(TEST_NOT_NULL(i1) && TEST_NOT_NULL(i2) && TEST_NOT_NULL(i3)) { TEST(10, *i1); TEST(20, *i2); TEST(30, *i3); } testEntityIterator(c); testEntityIterator(c); } template static void testComponentIterator(T c) { auto iter = c.begin(); auto end = c.end(); if(TEST_TRUE(iter != end)) { TEST(10, *(iter++)); } if(TEST_TRUE(iter != end)) { TEST(20, *(iter++)); } if(TEST_TRUE(iter != end)) { TEST(30, *(iter++)); } TEST_FALSE(iter != c.end()); } static void testAddComponentForEach() { IntComponent c; int* i1 = nullptr; int* i2 = nullptr; int* i3 = nullptr; TEST_TRUE(c.put(i1, 1, 10)); TEST_TRUE(c.put(i2, 5, 20)); TEST_TRUE(c.put(i3, 10, 30)); if(TEST_NOT_NULL(i1) && TEST_NOT_NULL(i2) && TEST_NOT_NULL(i3)) { TEST(10, *i1); TEST(20, *i2); TEST(30, *i3); } testComponentIterator(c); testComponentIterator(c); } static void testRemove() { IntComponent c; TEST_TRUE(c.add(1, 10)); TEST_TRUE(c.add(5, 20)); TEST_TRUE(c.add(10, 30)); TEST_FALSE(c.remove(20)); TEST_TRUE(c.remove(5)); TEST_FALSE(c.remove(30)); TEST_TRUE(c.add(20, 40)); TEST_TRUE(c.remove(20)); int* i1 = c.search(1); TEST_NULL(c.search(5)); int* i3 = c.search(10); if(TEST_NOT_NULL(i1) && TEST_NOT_NULL(i3)) { TEST(10, *i1); TEST(30, *i3); } TEST_TRUE(c.remove(10)); i1 = c.search(1); TEST_NULL(c.search(5)); TEST_NULL(c.search(10)); if(TEST_NOT_NULL(i1)) { TEST(10, *i1); } TEST_TRUE(c.remove(1)); TEST_NULL(c.search(1)); TEST_NULL(c.search(5)); TEST_NULL(c.search(10)); } static void testConstSearch() { IntComponent c; int* i = nullptr; TEST_TRUE(c.put(i, 1, 10)); const IntComponent& cc = c; const int* component = cc.search(1); if(TEST_TRUE(component != nullptr)) { TEST(10, *component); } TEST_NULL(cc.search(2)); } void testComponents() { testAddForEach(); testAddComponentForEach(); testRemove(); testConstSearch(); }