From a0a2ad4404f2f90dcb3cf5fee52b17fd6f980b50 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 13 Feb 2024 08:26:04 +0100 Subject: [PATCH] test: use common types for lib tests (registry) --- test/lib/registry/common/types.h | 14 -------------- test/lib/registry/plugin/main.cpp | 20 +++++++++++--------- test/lib/registry/plugin/plugin.cpp | 18 ++++++++++-------- test/lib/registry/shared/lib.cpp | 21 ++++++++++----------- test/lib/registry/shared/main.cpp | 23 ++++++++++++----------- 5 files changed, 43 insertions(+), 53 deletions(-) delete mode 100644 test/lib/registry/common/types.h diff --git a/test/lib/registry/common/types.h b/test/lib/registry/common/types.h deleted file mode 100644 index cf7cb4a5e..000000000 --- a/test/lib/registry/common/types.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef ENTT_LIB_REGISTRY_COMMON_TYPES_H -#define ENTT_LIB_REGISTRY_COMMON_TYPES_H - -struct position { - int x{}; - int y{}; -}; - -struct velocity { - double dx{}; - double dy{}; -}; - -#endif diff --git a/test/lib/registry/plugin/main.cpp b/test/lib/registry/plugin/main.cpp index b9387777e..63cb3274b 100644 --- a/test/lib/registry/plugin/main.cpp +++ b/test/lib/registry/plugin/main.cpp @@ -1,16 +1,19 @@ #define CR_HOST #include +#include +#include #include #include -#include "../common/types.h" TEST(Lib, Registry) { + constexpr auto count = 3; entt::registry registry; - registry.emplace(registry.create(), 0, 0); - registry.emplace(registry.create(), 1, 1); - registry.emplace(registry.create(), 2, 2); + for(auto i = 0; i < count; ++i) { + const auto entity = registry.create(); + registry.emplace(entity, i); + } cr_plugin ctx; cr_plugin_load(ctx, PLUGIN); @@ -18,12 +21,11 @@ TEST(Lib, Registry) { ctx.userdata = ®istry; cr_plugin_update(ctx); - ASSERT_EQ(registry.storage().size(), registry.storage().size()); - ASSERT_EQ(registry.storage().size(), registry.storage().size()); + ASSERT_EQ(registry.storage().size(), registry.storage().size()); + ASSERT_EQ(registry.storage().size(), registry.storage().size()); - registry.view().each([](auto entity, auto &position) { - ASSERT_EQ(position.x, static_cast(entt::to_integral(entity) + 16u)); - ASSERT_EQ(position.y, static_cast(entt::to_integral(entity) + 16u)); + registry.view().each([count](auto entity, auto &elem) { + ASSERT_EQ(elem.value, entt::to_integral(entity) + count); }); registry = {}; diff --git a/test/lib/registry/plugin/plugin.cpp b/test/lib/registry/plugin/plugin.cpp index dafc06ffa..7fcb93d4e 100644 --- a/test/lib/registry/plugin/plugin.cpp +++ b/test/lib/registry/plugin/plugin.cpp @@ -1,22 +1,24 @@ +#include +#include #include #include -#include "../common/types.h" CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) { + constexpr auto count = 3; + switch(operation) { case CR_STEP: { // forces things to break auto ®istry = *static_cast(ctx->userdata); - // forces the creation of the pool for the velocity component - static_cast(registry.storage()); + // forces the creation of the pool for the empty component + static_cast(registry.storage()); - const auto view = registry.view(); - registry.insert(view.begin(), view.end(), velocity{1., 1.}); + const auto view = registry.view(); + registry.insert(view.begin(), view.end(), test::empty{}); - registry.view().each([](position &pos, velocity &vel) { - pos.x += static_cast(16 * vel.dx); - pos.y += static_cast(16 * vel.dy); + registry.view().each([count](test::boxed_int &elem) { + elem.value += count; }); } break; case CR_CLOSE: diff --git a/test/lib/registry/shared/lib.cpp b/test/lib/registry/shared/lib.cpp index 96189fda8..01a367cd0 100644 --- a/test/lib/registry/shared/lib.cpp +++ b/test/lib/registry/shared/lib.cpp @@ -1,21 +1,20 @@ +#include +#include #include #include -#include "../common/types.h" template class entt::basic_registry; -ENTT_API void update_position(entt::registry ®istry) { - registry.view().each([](auto &pos, auto &vel) { - pos.x += static_cast(16 * vel.dx); - pos.y += static_cast(16 * vel.dy); +ENTT_API void update(entt::registry ®istry, int value) { + registry.view().each([value](auto &elem) { + elem.value += value; }); } -ENTT_API void emplace_velocity(entt::registry ®istry) { - // forces the creation of the pool for the velocity component - static_cast(registry.storage()); +ENTT_API void insert(entt::registry ®istry) { + // forces the creation of the pool for the empty type + static_cast(registry.storage()); - for(auto entity: registry.view()) { - registry.emplace(entity, 1., 1.); - } + const auto view = registry.view(); + registry.insert(view.begin(), view.end()); } diff --git a/test/lib/registry/shared/main.cpp b/test/lib/registry/shared/main.cpp index 3aa8c0d12..c1b80e27b 100644 --- a/test/lib/registry/shared/main.cpp +++ b/test/lib/registry/shared/main.cpp @@ -1,27 +1,28 @@ #include +#include +#include #include #include #include -#include "../common/types.h" -ENTT_API void update_position(entt::registry &); -ENTT_API void emplace_velocity(entt::registry &); +ENTT_API void update(entt::registry &, int); +ENTT_API void insert(entt::registry &); TEST(Lib, Registry) { + constexpr auto count = 3; entt::registry registry; - for(auto i = 0; i < 3; ++i) { + for(auto i = 0; i < count; ++i) { const auto entity = registry.create(); - registry.emplace(entity, i, i); + registry.emplace(entity, i); } - emplace_velocity(registry); - update_position(registry); + insert(registry); + update(registry, count); - ASSERT_EQ(registry.storage().size(), registry.storage().size()); + ASSERT_EQ(registry.storage().size(), registry.storage().size()); - registry.view().each([](auto entity, auto &position) { - ASSERT_EQ(position.x, static_cast(entt::to_integral(entity) + 16)); - ASSERT_EQ(position.y, static_cast(entt::to_integral(entity) + 16)); + registry.view().each([count](auto entity, auto &elem) { + ASSERT_EQ(elem.value, static_cast(entt::to_integral(entity) + count)); }); }