123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include "../Tests.hpp"
- #include "core/Components.hpp"
- #include "core/Test.hpp"
- template class Core::Components<int>;
- using IntComponent = Core::Components<int>;
- template<typename T>
- 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<IntComponent&>(c);
- testEntityIterator<const IntComponent&>(c);
- }
- template<typename T>
- 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<IntComponent&>(c);
- testComponentIterator<const IntComponent&>(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();
- }
|