zecsy/tests/zecsy.cpp

416 lines
8.8 KiB
C++
Raw Normal View History

2025-02-14 21:35:40 +03:00
#include <catch2/catch_test_macros.hpp>
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-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
}
TEST_CASE("Destroy an entity and ensure it no longer exists in the world")
{
world w;
2025-02-16 21:18:39 +03:00
auto e = w.make_entity();
w.destroy_entity(e);
2025-02-16 21:18:39 +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;
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
{
};
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));
}
2025-02-16 21:18:39 +03:00
TEST_CASE("Attach a simple component to an entity and verify it is correctly "
"associated")
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
struct Name
2025-02-14 23:56:01 +03:00
{
std::string value;
};
2025-02-16 21:18:39 +03:00
TEST_CASE("Retrieve a component from an entity and verify its data matches "
"what was set")
2025-02-14 23:56:01 +03:00
{
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
2025-02-16 21:18:39 +03:00
TEST_CASE(
"Remove a component from an entity and verify it is no longer attached")
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));
}
TEST_CASE("Addresses of removed components should be reused")
{
2025-02-16 21:18:39 +03:00
world w;
2025-02-15 22:33:02 +03:00
std::vector<entity_id> entities;
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());
}
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-16 21:18:39 +03:00
else
{
/*
* Gotta reverse it because now we reuse ids in LIFO order
*/
std::reverse(addr.begin(), addr.end());
for(int j = 0; j < N; ++j)
{
2025-02-15 22:33:02 +03:00
REQUIRE(&w.get<ChoosenOne>(entities[j]) == addr[j]);
}
}
for(auto e: entities)
{
2025-02-15 22:33:02 +03:00
w.remove<ChoosenOne>(e);
}
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 "
"correctly stored and retrievable")
2025-02-15 01:13:51 +03:00
{
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-16 21:18:39 +03:00
TEST_CASE("Create a simple system that processes entities with a specific "
"component and verify it executes correctly")
{
2025-02-16 21:18:39 +03:00
struct Component
{
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 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-16 21:18:39 +03:00
w.query<Component>([](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 "
"specific combination of components")
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
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++;
count++;
2025-02-15 20:13:38 +03:00
});
REQUIRE(count == 1);
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-16 20:39:41 +03:00
TEST_CASE("Systems execute at correct frequencies")
{
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
}
TEST_CASE("Systems handle zero-frequency gracefully")
{
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);
}
TEST_CASE("Systems handle varying update rates")
{
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);
}
TEST_CASE("Systems handle large time steps")
{
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);
}
TEST_CASE("Systems handle multiple frequencies")
{
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
}
TEST_CASE("Systems handle fractional frequencies")
{
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);
}
TEST_CASE("Systems handle zero delta time")
{
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);
}
TEST_CASE("Systems handle negative delta time")
{
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);
}