2025-02-14 21:35:40 +03:00
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2025-02-15 20:13:38 +03:00
|
|
|
|
#include <concepts>
|
2025-02-14 23:56:01 +03:00
|
|
|
|
#include <string>
|
2025-02-14 21:01:46 +03:00
|
|
|
|
#define CATCH_CONFIG_MAIN
|
|
|
|
|
#include <catch2/catch_all.hpp>
|
2025-02-14 22:18:00 +03:00
|
|
|
|
|
2025-02-14 21:35:40 +03:00
|
|
|
|
#include "../zecsy.hpp"
|
2025-02-14 21:01:46 +03:00
|
|
|
|
|
2025-02-14 21:35:40 +03:00
|
|
|
|
using namespace zecsy;
|
|
|
|
|
|
|
|
|
|
TEST_CASE("Create a single entity and verify its existence")
|
2025-02-14 21:01:46 +03:00
|
|
|
|
{
|
2025-02-14 21:35:40 +03:00
|
|
|
|
world w;
|
2025-02-14 21:41:36 +03:00
|
|
|
|
|
2025-02-14 21:35:40 +03:00
|
|
|
|
auto e = w.make_entity();
|
2025-02-14 21:47:16 +03:00
|
|
|
|
|
2025-02-14 21:35:40 +03:00
|
|
|
|
REQUIRE(w.is_alive(e));
|
2025-02-14 21:01:46 +03:00
|
|
|
|
}
|
2025-02-14 21:41:36 +03:00
|
|
|
|
|
|
|
|
|
TEST_CASE("Destroy an entity and ensure it no longer exists in the world")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
2025-02-14 21:47:16 +03:00
|
|
|
|
|
2025-02-14 21:41:36 +03:00
|
|
|
|
auto e = w.make_entity();
|
|
|
|
|
w.destroy_entity(e);
|
2025-02-14 21:47:16 +03:00
|
|
|
|
|
2025-02-14 21:41:36 +03:00
|
|
|
|
REQUIRE_FALSE(w.is_alive(e));
|
|
|
|
|
}
|
2025-02-14 21:47:16 +03:00
|
|
|
|
|
|
|
|
|
TEST_CASE("Entity #0 should be reserved and never used")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
|
|
|
|
|
|
|
|
|
REQUIRE(e != 0);
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE_FALSE(w.is_alive(0));
|
2025-02-14 22:18:00 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-14 23:42:01 +03:00
|
|
|
|
struct ChoosenOne
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_CASE("Entity shouldn't have a component that wasn't attached to it")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
|
|
|
|
|
|
|
|
|
REQUIRE_FALSE(w.has<ChoosenOne>(e));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("Attempt of getting non-owned component should throw")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
|
|
|
|
|
|
|
|
|
REQUIRE_THROWS(w.get<ChoosenOne>(e));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("Attach a simple component to an entity and verify it is correctly associated")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
|
|
auto e1 = w.make_entity();
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set(e1, ChoosenOne{});
|
2025-02-14 23:42:01 +03:00
|
|
|
|
|
|
|
|
|
REQUIRE(w.has<ChoosenOne>(e1));
|
|
|
|
|
|
|
|
|
|
auto e2 = w.make_entity();
|
|
|
|
|
w.set(e2, ChoosenOne{});
|
|
|
|
|
|
|
|
|
|
REQUIRE(w.has<ChoosenOne>(e2));
|
|
|
|
|
}
|
2025-02-14 23:56:01 +03:00
|
|
|
|
|
|
|
|
|
struct Name
|
|
|
|
|
{
|
|
|
|
|
std::string value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_CASE("Retrieve a component from an entity and verify its data matches what was set")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set(e, Name{"zecsy!"});
|
2025-02-14 23:56:01 +03:00
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE(w.get<Name>(e).value == "zecsy!");
|
2025-02-14 23:56:01 +03:00
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.get<Name>(e).value = "super-zecsy!";
|
2025-02-14 23:56:01 +03:00
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE(w.get<Name>(e).value == "super-zecsy!");
|
2025-02-14 23:56:01 +03:00
|
|
|
|
}
|
2025-02-15 00:23:01 +03:00
|
|
|
|
|
|
|
|
|
TEST_CASE("Remove a component from an entity and verify it is no longer attached")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set(e, ChoosenOne{});
|
|
|
|
|
REQUIRE_NOTHROW(w.remove<ChoosenOne>(e));
|
2025-02-15 00:23:01 +03:00
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE_FALSE(w.has<ChoosenOne>(e));
|
2025-02-15 00:23:01 +03:00
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set(e, ChoosenOne{});
|
2025-02-15 00:23:01 +03:00
|
|
|
|
REQUIRE_NOTHROW(w.remove<ChoosenOne>(e));
|
|
|
|
|
|
|
|
|
|
REQUIRE_FALSE(w.has<ChoosenOne>(e));
|
|
|
|
|
}
|
2025-02-15 00:36:15 +03:00
|
|
|
|
|
|
|
|
|
TEST_CASE("Addresses of removed components should be reused")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
2025-02-15 22:33:02 +03:00
|
|
|
|
std::vector<entity_id> entities;
|
2025-02-15 00:36:15 +03:00
|
|
|
|
std::vector<ChoosenOne*> addr;
|
|
|
|
|
|
|
|
|
|
const int N = 4;
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
|
{
|
|
|
|
|
for(int j = 0; j < N; ++j)
|
|
|
|
|
{
|
|
|
|
|
entities.emplace_back(w.make_entity());
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set<ChoosenOne>(entities.back());
|
2025-02-15 00:36:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(addr.empty())
|
|
|
|
|
{
|
|
|
|
|
for(int j = 0; j < N; ++j)
|
|
|
|
|
{
|
2025-02-15 22:33:02 +03:00
|
|
|
|
addr.emplace_back(&w.get<ChoosenOne>(entities[j]));
|
2025-02-15 00:36:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for(int j = 0; j < N; ++j)
|
|
|
|
|
{
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE(&w.get<ChoosenOne>(entities[j]) == addr[j]);
|
2025-02-15 00:36:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(auto e: entities)
|
|
|
|
|
{
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.remove<ChoosenOne>(e);
|
2025-02-15 00:36:15 +03:00
|
|
|
|
}
|
|
|
|
|
entities.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-15 01:13:51 +03:00
|
|
|
|
|
|
|
|
|
TEST_CASE("Attach multiple components to an entity and verify all are correctly stored and retrievable")
|
|
|
|
|
{
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set(e, ChoosenOne{}, Name{"zecsy"});
|
2025-02-15 01:13:51 +03:00
|
|
|
|
|
|
|
|
|
REQUIRE(w.has<ChoosenOne, Name>(e));
|
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.remove<ChoosenOne, Name>(e);
|
2025-02-15 01:13:51 +03:00
|
|
|
|
|
|
|
|
|
REQUIRE_FALSE(w.has<ChoosenOne, Name>(e));
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE_FALSE(w.has<ChoosenOne>(e));
|
|
|
|
|
REQUIRE_FALSE(w.has<Name>(e));
|
2025-02-15 01:13:51 +03:00
|
|
|
|
}
|
2025-02-15 18:24:18 +03:00
|
|
|
|
|
|
|
|
|
TEST_CASE("Create a simple system that processes entities with a specific component and verify it executes correctly")
|
|
|
|
|
{
|
|
|
|
|
struct Component
|
|
|
|
|
{
|
|
|
|
|
int value = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
world w;
|
|
|
|
|
auto e0 = w.make_entity(), e1 = w.make_entity();
|
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set<Component>(e0); //or e0.set(Component{})
|
|
|
|
|
w.set(e1, Component{20});
|
2025-02-15 18:24:18 +03:00
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE(w.get<Component>(e0).value == 0);
|
|
|
|
|
REQUIRE(w.get<Component>(e1).value == 20);
|
2025-02-15 20:13:38 +03:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Really wanna deduce it to w.query([](Component&){}),
|
|
|
|
|
* but I have some troubles with it
|
|
|
|
|
*/
|
|
|
|
|
w.query<Component>([](Component& c)
|
|
|
|
|
{
|
|
|
|
|
c.value++;
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE(w.get<Component>(e0).value == 1);
|
|
|
|
|
REQUIRE(w.get<Component>(e1).value == 21);
|
2025-02-15 20:13:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE("Test a system’s ability to query and process only entities with a specific combination of components")
|
|
|
|
|
{
|
|
|
|
|
struct C0
|
|
|
|
|
{
|
|
|
|
|
int value = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct C1
|
|
|
|
|
{
|
|
|
|
|
int value = 10;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
|
|
auto e0 = w.make_entity();
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set(e0, C0{}, C1{});
|
2025-02-15 20:13:38 +03:00
|
|
|
|
|
|
|
|
|
auto e1 = w.make_entity();
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set(e1, C0{});
|
2025-02-15 20:13:38 +03:00
|
|
|
|
|
|
|
|
|
auto e2 = w.make_entity();
|
2025-02-15 22:33:02 +03:00
|
|
|
|
w.set(e2, C1{});
|
2025-02-15 20:13:38 +03:00
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE(w.get<C0>(e0).value == 0);
|
|
|
|
|
REQUIRE(w.get<C1>(e0).value == 10);
|
2025-02-15 20:13:38 +03:00
|
|
|
|
|
|
|
|
|
w.query<C0, C1>([&count](C0& c0, C1& c1)
|
|
|
|
|
{
|
|
|
|
|
c0.value++;
|
|
|
|
|
c1.value++;
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE(w.get<C0>(e0).value == 1);
|
|
|
|
|
REQUIRE(w.get<C1>(e0).value == 11);
|
2025-02-15 20:13:38 +03:00
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE(w.get<C0>(e1).value == 0);
|
|
|
|
|
REQUIRE(w.get<C1>(e2).value == 10);
|
2025-02-15 20:13:38 +03:00
|
|
|
|
|
2025-02-15 22:33:02 +03:00
|
|
|
|
REQUIRE_FALSE(w.has<C1>(e1));
|
|
|
|
|
REQUIRE_FALSE(w.has<C0>(e2));
|
2025-02-15 18:24:18 +03:00
|
|
|
|
}
|