2025-02-14 21:35:40 +03:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
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-20 00:09:58 +03:00
|
|
|
#include "../system_scheduler.hpp"
|
2025-02-20 00:17:11 +03:00
|
|
|
#include "../zecsy.hpp"
|
2025-02-14 21:01:46 +03:00
|
|
|
|
2025-02-14 21:35:40 +03:00
|
|
|
using namespace zecsy;
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Create a single entity and verify its existence", "[test]")
|
2025-02-14 21:01:46 +03:00
|
|
|
{
|
2025-02-14 21:35:40 +03:00
|
|
|
world w;
|
2025-02-16 21:18:39 +03:00
|
|
|
|
2025-02-14 21:35:40 +03:00
|
|
|
auto e = w.make_entity();
|
2025-02-16 21:18:39 +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
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Destroy an entity and ensure it no longer exists in the world",
|
|
|
|
"[test]")
|
2025-02-14 21:41:36 +03:00
|
|
|
{
|
|
|
|
world w;
|
2025-02-16 21:18:39 +03:00
|
|
|
|
2025-02-14 21:41:36 +03:00
|
|
|
auto e = w.make_entity();
|
|
|
|
w.destroy_entity(e);
|
2025-02-16 21:18:39 +03:00
|
|
|
|
2025-02-14 21:41:36 +03:00
|
|
|
REQUIRE_FALSE(w.is_alive(e));
|
|
|
|
}
|
2025-02-14 21:47:16 +03:00
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Entity #0 should be reserved and never used", "[test]")
|
2025-02-14 21:47:16 +03:00
|
|
|
{
|
|
|
|
world w;
|
2025-02-16 21:18:39 +03:00
|
|
|
|
2025-02-14 21:47:16 +03:00
|
|
|
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
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Entity shouldn't have a component that wasn't attached to it",
|
|
|
|
"[test]")
|
2025-02-14 23:42:01 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
|
|
|
|
|
|
|
REQUIRE_FALSE(w.has<ChoosenOne>(e));
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Attempt of getting non-owned component should throw", "[test]")
|
2025-02-14 23:42:01 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
|
|
|
|
|
|
|
REQUIRE_THROWS(w.get<ChoosenOne>(e));
|
|
|
|
}
|
|
|
|
|
2025-02-16 21:18:39 +03:00
|
|
|
TEST_CASE("Attach a simple component to an entity and verify it is correctly "
|
2025-02-20 14:35:22 +03:00
|
|
|
"associated",
|
|
|
|
"[test]")
|
2025-02-14 23:42:01 +03:00
|
|
|
{
|
|
|
|
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-16 21:18:39 +03:00
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
struct Comp
|
2025-02-14 23:56:01 +03:00
|
|
|
{
|
2025-02-18 22:27:54 +03:00
|
|
|
int v = 0;
|
2025-02-14 23:56:01 +03:00
|
|
|
};
|
|
|
|
|
2025-02-16 21:18:39 +03:00
|
|
|
TEST_CASE("Retrieve a component from an entity and verify its data matches "
|
2025-02-20 14:35:22 +03:00
|
|
|
"what was set",
|
|
|
|
"[test]")
|
2025-02-14 23:56:01 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
2025-02-18 22:27:54 +03:00
|
|
|
w.set(e, Comp());
|
2025-02-14 23:56:01 +03:00
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
REQUIRE(w.get<Comp>(e).v == 0);
|
2025-02-14 23:56:01 +03:00
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
w.get<Comp>(e).v = 77;
|
2025-02-14 23:56:01 +03:00
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
REQUIRE(w.get<Comp>(e).v == 77);
|
2025-02-14 23:56:01 +03:00
|
|
|
}
|
2025-02-15 00:23:01 +03:00
|
|
|
|
2025-02-16 21:18:39 +03:00
|
|
|
TEST_CASE(
|
2025-02-20 14:35:22 +03:00
|
|
|
"Remove a component from an entity and verify it is no longer attached",
|
|
|
|
"[test]")
|
2025-02-15 00:23:01 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Addresses of removed components should be reused", "[test]")
|
2025-02-15 00:36:15 +03:00
|
|
|
{
|
2025-02-16 21:18:39 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2025-02-16 21:18:39 +03:00
|
|
|
else
|
2025-02-15 00:36:15 +03:00
|
|
|
{
|
2025-02-17 22:50:53 +03:00
|
|
|
/*
|
|
|
|
* Gotta reverse it because now we reuse ids in LIFO order
|
|
|
|
*/
|
|
|
|
std::reverse(addr.begin(), addr.end());
|
2025-02-18 22:44:21 +03:00
|
|
|
|
2025-02-15 00:36:15 +03:00
|
|
|
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
|
|
|
|
2025-02-16 21:18:39 +03:00
|
|
|
TEST_CASE("Attach multiple components to an entity and verify all are "
|
2025-02-20 14:35:22 +03:00
|
|
|
"correctly stored and retrievable",
|
|
|
|
"[test]")
|
2025-02-15 01:13:51 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
|
|
|
|
auto e = w.make_entity();
|
2025-02-18 22:27:54 +03:00
|
|
|
w.set(e, ChoosenOne{}, Comp{});
|
2025-02-15 01:13:51 +03:00
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
REQUIRE(w.has<ChoosenOne, Comp>(e));
|
2025-02-15 01:13:51 +03:00
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
w.remove<ChoosenOne, Comp>(e);
|
2025-02-15 01:13:51 +03:00
|
|
|
|
2025-02-18 22:27:54 +03:00
|
|
|
REQUIRE_FALSE(w.has<ChoosenOne, Comp>(e));
|
2025-02-15 22:33:02 +03:00
|
|
|
REQUIRE_FALSE(w.has<ChoosenOne>(e));
|
2025-02-18 22:27:54 +03:00
|
|
|
REQUIRE_FALSE(w.has<Comp>(e));
|
2025-02-15 01:13:51 +03:00
|
|
|
}
|
2025-02-15 18:24:18 +03:00
|
|
|
|
2025-02-16 21:18:39 +03:00
|
|
|
TEST_CASE("Create a simple system that processes entities with a specific "
|
2025-02-20 14:35:22 +03:00
|
|
|
"component and verify it executes correctly",
|
|
|
|
"[test]")
|
2025-02-15 18:24:18 +03:00
|
|
|
{
|
2025-02-16 21:18:39 +03:00
|
|
|
struct Component
|
2025-02-15 18:24:18 +03:00
|
|
|
{
|
|
|
|
int value = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
world w;
|
|
|
|
auto e0 = w.make_entity(), e1 = w.make_entity();
|
|
|
|
|
2025-02-16 21:18:39 +03:00
|
|
|
w.set<Component>(e0); // or e0.set(Component{})
|
2025-02-15 22:33:02 +03:00
|
|
|
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-16 21:18:39 +03:00
|
|
|
|
2025-02-15 20:13:38 +03:00
|
|
|
/*
|
|
|
|
* Really wanna deduce it to w.query([](Component&){}),
|
|
|
|
* but I have some troubles with it
|
|
|
|
*/
|
2025-02-20 00:17:11 +03:00
|
|
|
w.query<Component>([](entity_id e, Component& c) { c.value++; });
|
2025-02-15 20:13:38 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2025-02-16 21:18:39 +03:00
|
|
|
TEST_CASE("Test a systems ability to query and process only entities with a "
|
2025-02-20 14:35:22 +03:00
|
|
|
"specific combination of components",
|
|
|
|
"[test]")
|
2025-02-15 20:13:38 +03:00
|
|
|
{
|
2025-02-16 21:18:39 +03:00
|
|
|
struct C0
|
2025-02-15 20:13:38 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
w.query<C0, C1>(
|
|
|
|
[e0](entity_id e, C0& c0, C1& c1)
|
|
|
|
{
|
|
|
|
REQUIRE(e == e0);
|
|
|
|
c0.value++;
|
|
|
|
c1.value++;
|
|
|
|
});
|
2025-02-15 20:13:38 +03:00
|
|
|
|
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
|
|
|
}
|
2025-02-16 20:39:41 +03:00
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Systems execute at correct frequencies", "[test]")
|
2025-02-16 20:39:41 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
system_scheduler scheduler;
|
|
|
|
|
|
|
|
int fast_count = 0;
|
|
|
|
int slow_count = 0;
|
|
|
|
|
|
|
|
// Add a fast system (60 Hz)
|
2025-02-16 21:18:39 +03:00
|
|
|
scheduler.add_system(60, [&](float dt) { fast_count++; });
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
// Add a slow system (1 Hz)
|
2025-02-16 21:18:39 +03:00
|
|
|
scheduler.add_system(1, [&](float dt) { slow_count++; });
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
// Simulate 2 seconds of updates at 120 FPS
|
2025-02-16 21:18:39 +03:00
|
|
|
for(int i = 0; i < 240; ++i)
|
2025-02-16 20:41:05 +03:00
|
|
|
{
|
2025-02-16 20:43:08 +03:00
|
|
|
scheduler.update(1.0f / 120.0f);
|
2025-02-16 20:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify counts
|
2025-02-16 21:18:39 +03:00
|
|
|
REQUIRE(fast_count == 120); // 60 Hz system should execute 60 times
|
2025-02-16 20:39:41 +03:00
|
|
|
REQUIRE(slow_count == 2); // 1 Hz system should execute 1 time
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Systems handle zero-frequency gracefully", "[test]")
|
2025-02-16 20:39:41 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
system_scheduler scheduler;
|
|
|
|
|
|
|
|
int zero_count = 0;
|
|
|
|
|
|
|
|
// Add a zero-frequency system (should never execute)
|
2025-02-16 21:18:39 +03:00
|
|
|
scheduler.add_system(0, [&](float dt) { zero_count++; });
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
// Simulate 1 second of updates at 60 FPS
|
2025-02-16 21:18:39 +03:00
|
|
|
for(int i = 0; i < 60; ++i)
|
2025-02-16 20:41:05 +03:00
|
|
|
{
|
2025-02-16 20:43:08 +03:00
|
|
|
scheduler.update(1.0f / 60.0f);
|
2025-02-16 20:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify zero-frequency system never executes
|
|
|
|
REQUIRE(zero_count == 0);
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Systems handle varying update rates", "[test]")
|
2025-02-16 20:39:41 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
system_scheduler scheduler;
|
|
|
|
|
|
|
|
int varying_count = 0;
|
|
|
|
|
|
|
|
// Add a system with varying frequency (10 Hz)
|
2025-02-16 21:18:39 +03:00
|
|
|
scheduler.add_system(10, [&](float dt) { varying_count++; });
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
// Simulate 1 second of updates at 30 FPS
|
2025-02-16 21:18:39 +03:00
|
|
|
for(int i = 0; i < 30; ++i)
|
2025-02-16 20:41:05 +03:00
|
|
|
{
|
2025-02-16 20:43:08 +03:00
|
|
|
scheduler.update(1.0f / 30.0f);
|
2025-02-16 20:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify varying-frequency system executes 10 times
|
|
|
|
REQUIRE(varying_count == 10);
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Systems handle large time steps", "[test]")
|
2025-02-16 20:39:41 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
system_scheduler scheduler;
|
|
|
|
|
|
|
|
int large_step_count = 0;
|
|
|
|
|
|
|
|
// Add a system (1 Hz)
|
2025-02-16 21:18:39 +03:00
|
|
|
scheduler.add_system(1, [&](float dt) { large_step_count++; });
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
// Simulate a large time step (2 seconds)
|
|
|
|
scheduler.update(2.0f);
|
|
|
|
|
|
|
|
// Verify system executes twice (accumulator handles large steps)
|
|
|
|
REQUIRE(large_step_count == 2);
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Systems handle multiple frequencies", "[test]")
|
2025-02-16 20:39:41 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
system_scheduler scheduler;
|
|
|
|
|
|
|
|
int fast_count = 0;
|
|
|
|
int medium_count = 0;
|
|
|
|
int slow_count = 0;
|
|
|
|
|
|
|
|
// Add systems with different frequencies
|
|
|
|
scheduler.add_system(60, [&](float dt) { fast_count++; });
|
|
|
|
scheduler.add_system(30, [&](float dt) { medium_count++; });
|
|
|
|
scheduler.add_system(1, [&](float dt) { slow_count++; });
|
|
|
|
|
|
|
|
// Simulate 1 second of updates at 120 FPS
|
2025-02-16 21:18:39 +03:00
|
|
|
for(int i = 0; i < 120; ++i)
|
2025-02-16 20:41:05 +03:00
|
|
|
{
|
2025-02-16 20:43:08 +03:00
|
|
|
scheduler.update(1.0f / 120.0f);
|
2025-02-16 20:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify counts
|
|
|
|
REQUIRE(fast_count == 60); // 60 Hz system
|
|
|
|
REQUIRE(medium_count == 30); // 30 Hz system
|
|
|
|
REQUIRE(slow_count == 1); // 1 Hz system
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Systems handle fractional frequencies", "[test]")
|
2025-02-16 20:39:41 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
system_scheduler scheduler;
|
|
|
|
|
|
|
|
int fractional_count = 0;
|
|
|
|
|
|
|
|
// Add a system with fractional frequency (0.5 Hz)
|
2025-02-16 21:18:39 +03:00
|
|
|
scheduler.add_system(0.5f, [&](float dt) { fractional_count++; });
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
// Simulate 4 seconds of updates at 60 FPS
|
2025-02-16 21:18:39 +03:00
|
|
|
for(int i = 0; i < 240; ++i)
|
2025-02-16 20:41:05 +03:00
|
|
|
{
|
2025-02-16 20:43:08 +03:00
|
|
|
scheduler.update(1.0f / 60.0f);
|
2025-02-16 20:39:41 +03:00
|
|
|
}
|
|
|
|
|
2025-02-16 21:18:39 +03:00
|
|
|
// Verify fractional-frequency system executes twice (0.5 Hz = 2 times in 4
|
|
|
|
// seconds)
|
2025-02-16 20:39:41 +03:00
|
|
|
REQUIRE(fractional_count == 2);
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Systems handle zero delta time", "[test]")
|
2025-02-16 20:39:41 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
system_scheduler scheduler;
|
|
|
|
|
|
|
|
int zero_dt_count = 0;
|
|
|
|
|
|
|
|
// Add a system (1 Hz)
|
2025-02-16 21:18:39 +03:00
|
|
|
scheduler.add_system(1, [&](float dt) { zero_dt_count++; });
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
// Simulate zero delta time
|
|
|
|
scheduler.update(0.0f);
|
|
|
|
|
|
|
|
// Verify system does not execute
|
|
|
|
REQUIRE(zero_dt_count == 0);
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Systems handle negative delta time", "[test]")
|
2025-02-16 20:39:41 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
system_scheduler scheduler;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
// Add a system (1 Hz)
|
2025-02-16 21:18:39 +03:00
|
|
|
scheduler.add_system(1, [&](float dt) { count++; });
|
2025-02-16 20:39:41 +03:00
|
|
|
|
|
|
|
// Simulate negative delta time
|
|
|
|
scheduler.update(-1.0f);
|
|
|
|
|
|
|
|
// Verify system does not execute
|
|
|
|
REQUIRE(count == 0);
|
|
|
|
|
|
|
|
scheduler.update(2.0f);
|
|
|
|
|
|
|
|
REQUIRE(count == 2);
|
|
|
|
}
|
2025-02-20 02:39:24 +03:00
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Entity count tracking", "[test]")
|
2025-02-20 02:39:24 +03:00
|
|
|
{
|
|
|
|
world w;
|
|
|
|
REQUIRE(w.entity_count() == 0);
|
|
|
|
|
|
|
|
const auto e1 = w.make_entity();
|
|
|
|
const auto e2 = w.make_entity();
|
|
|
|
REQUIRE(w.entity_count() == 2);
|
|
|
|
|
|
|
|
w.destroy_entity(e1);
|
|
|
|
REQUIRE(w.entity_count() == 1);
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Component counting mechanisms", "[test]")
|
2025-02-20 02:39:24 +03:00
|
|
|
{
|
|
|
|
struct Health
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Position
|
|
|
|
{
|
|
|
|
float x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
world w;
|
|
|
|
auto e = w.make_entity();
|
|
|
|
|
|
|
|
REQUIRE(w.component_count<Health>() == 0);
|
|
|
|
REQUIRE(w.total_component_count() == 0);
|
|
|
|
|
|
|
|
w.set<Health>(e);
|
|
|
|
REQUIRE(w.component_count<Health>() == 1);
|
|
|
|
REQUIRE(w.total_component_count() == 1);
|
|
|
|
|
|
|
|
w.set<Position>(e);
|
|
|
|
REQUIRE(w.component_count<Position>() == 1);
|
|
|
|
REQUIRE(w.total_component_count() == 2);
|
|
|
|
|
|
|
|
w.remove<Health>(e);
|
|
|
|
REQUIRE(w.component_count<Health>() == 0);
|
|
|
|
REQUIRE(w.total_component_count() == 1);
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Archetype signature management", "[test]")
|
2025-02-20 02:39:24 +03:00
|
|
|
{
|
|
|
|
struct A
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
struct B
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
struct C
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
// Initial state: empty archetype
|
|
|
|
REQUIRE(w.archetype_count() == 0);
|
|
|
|
|
2025-02-20 02:55:04 +03:00
|
|
|
auto e0 = w.make_entity();
|
|
|
|
REQUIRE(w.archetype_count() == 1); //<>
|
2025-02-20 02:39:24 +03:00
|
|
|
|
|
|
|
// Add first component
|
2025-02-20 02:55:04 +03:00
|
|
|
w.set<A>(e0);
|
|
|
|
REQUIRE(w.archetype_count() == 1); //<A>
|
|
|
|
|
|
|
|
w.set<B>(e0);
|
|
|
|
REQUIRE(w.archetype_count() == 1); //<A, B>
|
|
|
|
|
|
|
|
w.set<C>(e0);
|
|
|
|
REQUIRE(w.archetype_count() == 1); //<A, B, C>
|
|
|
|
|
|
|
|
w.remove<A, B>(e0);
|
|
|
|
REQUIRE(w.archetype_count() == 1); //<C>
|
|
|
|
|
|
|
|
auto e1 = w.make_entity();
|
|
|
|
w.set<A, B>(e1);
|
|
|
|
REQUIRE(w.archetype_count() == 2); //<C>, <A, B>
|
|
|
|
|
|
|
|
w.remove<C>(e0);
|
|
|
|
REQUIRE(w.archetype_count() == 2); //<>, <A, B>
|
|
|
|
|
|
|
|
w.set<A>(e0);
|
|
|
|
REQUIRE(w.archetype_count() == 2); //<A>, <A, B>
|
|
|
|
|
|
|
|
w.set<B>(e0);
|
|
|
|
REQUIRE(w.archetype_count() == 1); //<A, B>
|
|
|
|
|
|
|
|
w.destroy_entity(e0);
|
|
|
|
REQUIRE(w.archetype_count() == 1); //<A, B>
|
|
|
|
|
|
|
|
w.destroy_entity(e1);
|
|
|
|
REQUIRE(w.archetype_count() == 0);
|
2025-02-20 02:39:24 +03:00
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Component distribution across archetypes", "[test]")
|
2025-02-20 02:39:24 +03:00
|
|
|
{
|
|
|
|
struct A
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
struct B
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
world w;
|
|
|
|
|
|
|
|
// Create 10 entities in different configurations
|
|
|
|
for(int i = 0; i < 5; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<A>(e);
|
|
|
|
}
|
2025-02-20 02:55:04 +03:00
|
|
|
|
2025-02-20 02:39:24 +03:00
|
|
|
for(int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<A, B>(e);
|
|
|
|
}
|
2025-02-20 02:55:04 +03:00
|
|
|
|
2025-02-20 02:39:24 +03:00
|
|
|
for(int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<B>(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify distribution
|
|
|
|
REQUIRE(w.entity_count() == 10);
|
|
|
|
REQUIRE(w.component_count<A>() == 8);
|
|
|
|
REQUIRE(w.component_count<B>() == 5);
|
|
|
|
REQUIRE(w.archetype_count() == 3); //<A>, <A, B>, <B>
|
|
|
|
}
|
|
|
|
|
2025-02-20 14:35:22 +03:00
|
|
|
TEST_CASE("Entity inspection", "[test]")
|
2025-02-20 02:39:24 +03:00
|
|
|
{
|
|
|
|
struct Transform
|
|
|
|
{
|
|
|
|
float x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Renderable
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
world w;
|
|
|
|
auto e = w.make_entity();
|
|
|
|
|
|
|
|
REQUIRE(w.components_in_entity(e) == 0);
|
|
|
|
|
|
|
|
w.set<Transform>(e);
|
|
|
|
REQUIRE(w.components_in_entity(e) == 1);
|
|
|
|
|
|
|
|
w.set<Renderable>(e);
|
|
|
|
REQUIRE(w.components_in_entity(e) == 2);
|
|
|
|
|
|
|
|
w.remove<Transform>(e);
|
|
|
|
REQUIRE(w.components_in_entity(e) == 1);
|
|
|
|
}
|
2025-02-20 14:35:22 +03:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// Benchmark components
|
|
|
|
struct Position
|
|
|
|
{
|
|
|
|
float x, y;
|
|
|
|
};
|
|
|
|
struct Velocity
|
|
|
|
{
|
|
|
|
float dx, dy;
|
|
|
|
};
|
|
|
|
struct Health
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
struct BigData
|
|
|
|
{
|
|
|
|
char buffer[4096];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Benchmark entity counts
|
|
|
|
constexpr int SMALL = 1'000;
|
|
|
|
constexpr int MEDIUM = 5'000;
|
|
|
|
constexpr int LARGE = 10'000;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_CASE("Core operations benchmarks", "[benchmark]")
|
|
|
|
{
|
|
|
|
BENCHMARK_ADVANCED("Create entities [1000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < SMALL; ++i)
|
|
|
|
{
|
|
|
|
w.make_entity();
|
|
|
|
}
|
|
|
|
return w.entity_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Create entities [5000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < MEDIUM; ++i)
|
|
|
|
{
|
|
|
|
w.make_entity();
|
|
|
|
}
|
|
|
|
return w.entity_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Create entities [10000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < LARGE; ++i)
|
|
|
|
{
|
|
|
|
w.make_entity();
|
|
|
|
}
|
|
|
|
return w.entity_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Create entities with components [1000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < SMALL; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Position>(e, {1.0f, 2.0f});
|
|
|
|
w.set<Velocity>(e, {3.0f, 4.0f});
|
|
|
|
}
|
|
|
|
return w.total_component_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Create entities with components [5000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < MEDIUM; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Position>(e, {1.0f, 2.0f});
|
|
|
|
w.set<Velocity>(e, {3.0f, 4.0f});
|
|
|
|
}
|
|
|
|
return w.total_component_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Create entities with components [10000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < LARGE; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Position>(e, {1.0f, 2.0f});
|
|
|
|
w.set<Velocity>(e, {3.0f, 4.0f});
|
|
|
|
}
|
|
|
|
return w.total_component_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Component operations benchmarks", "[benchmark]")
|
|
|
|
{
|
|
|
|
BENCHMARK_ADVANCED("Add component to existing entities [1000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < SMALL; ++i)
|
|
|
|
{
|
|
|
|
entities.push_back(w.make_entity());
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.set<Health>(e, {100});
|
|
|
|
}
|
|
|
|
return w.component_count<Health>();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Add component to existing entities [5000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < MEDIUM; ++i)
|
|
|
|
{
|
|
|
|
entities.push_back(w.make_entity());
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.set<Health>(e, {100});
|
|
|
|
}
|
|
|
|
return w.component_count<Health>();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Add component to existing entities [10000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < LARGE; ++i)
|
|
|
|
{
|
|
|
|
entities.push_back(w.make_entity());
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.set<Health>(e, {100});
|
|
|
|
}
|
|
|
|
return w.component_count<Health>();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Remove component from entities [1000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < SMALL; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Health>(e, {100});
|
|
|
|
entities.push_back(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.remove<Health>(e);
|
|
|
|
}
|
|
|
|
return w.component_count<Health>();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Remove component from entities [5000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < MEDIUM; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Health>(e, {100});
|
|
|
|
entities.push_back(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.remove<Health>(e);
|
|
|
|
}
|
|
|
|
return w.component_count<Health>();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Remove component from entities [10000]")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < LARGE; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Health>(e, {100});
|
|
|
|
entities.push_back(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.remove<Health>(e);
|
|
|
|
}
|
|
|
|
return w.component_count<Health>();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Query performance benchmarks", "[benchmark]")
|
|
|
|
{
|
|
|
|
BENCHMARK_ADVANCED("Dense query (90% match)")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < LARGE; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Position>(e);
|
|
|
|
if(i % 10 != 0)
|
|
|
|
w.set<Velocity>(e); // 90% match
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
w.query<Position, Velocity>([&](auto...) { count++; });
|
|
|
|
return count;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Sparse query (10% match)")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < LARGE; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Position>(e);
|
|
|
|
if(i % 10 == 0)
|
|
|
|
w.set<Velocity>(e); // 10% match
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
w.query<Position, Velocity>([&](auto...) { count++; });
|
|
|
|
return count;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Memory intensive benchmarks", "[benchmark]")
|
|
|
|
{
|
|
|
|
BENCHMARK_ADVANCED("Large component allocation")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
for(int i = 0; i < SMALL; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<BigData>(e);
|
|
|
|
}
|
|
|
|
return w.total_component_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Component memory reuse")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < SMALL; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<BigData>(e);
|
|
|
|
entities.push_back(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
// Remove and re-add components
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.remove<BigData>(e);
|
|
|
|
w.set<BigData>(e);
|
|
|
|
}
|
|
|
|
return w.component_count<BigData>();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Archetype transition benchmarks", "[benchmark]")
|
|
|
|
{
|
|
|
|
BENCHMARK_ADVANCED("Single component addition")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < MEDIUM; ++i)
|
|
|
|
{
|
|
|
|
entities.push_back(w.make_entity());
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.set<Health>(e);
|
|
|
|
}
|
|
|
|
return w.archetype_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_ADVANCED("Multi-component transition")(
|
|
|
|
Catch::Benchmark::Chronometer meter)
|
|
|
|
{
|
|
|
|
world w;
|
|
|
|
std::vector<entity_id> entities;
|
|
|
|
for(int i = 0; i < MEDIUM; ++i)
|
|
|
|
{
|
|
|
|
auto e = w.make_entity();
|
|
|
|
w.set<Position>(e);
|
|
|
|
entities.push_back(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
meter.measure(
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
for(auto e: entities)
|
|
|
|
{
|
|
|
|
w.set<Velocity>(e);
|
|
|
|
w.set<Health>(e);
|
|
|
|
}
|
|
|
|
return w.archetype_count();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|