test: use common types for lib tests (registry)

This commit is contained in:
Michele Caini
2024-02-13 08:26:04 +01:00
parent 1962027c80
commit a0a2ad4404
5 changed files with 43 additions and 53 deletions

View File

@@ -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

View File

@@ -1,16 +1,19 @@
#define CR_HOST
#include <gtest/gtest.h>
#include <common/boxed_type.h>
#include <common/empty.h>
#include <cr.h>
#include <entt/entity/registry.hpp>
#include "../common/types.h"
TEST(Lib, Registry) {
constexpr auto count = 3;
entt::registry registry;
registry.emplace<position>(registry.create(), 0, 0);
registry.emplace<position>(registry.create(), 1, 1);
registry.emplace<position>(registry.create(), 2, 2);
for(auto i = 0; i < count; ++i) {
const auto entity = registry.create();
registry.emplace<test::boxed_int>(entity, i);
}
cr_plugin ctx;
cr_plugin_load(ctx, PLUGIN);
@@ -18,12 +21,11 @@ TEST(Lib, Registry) {
ctx.userdata = &registry;
cr_plugin_update(ctx);
ASSERT_EQ(registry.storage<position>().size(), registry.storage<velocity>().size());
ASSERT_EQ(registry.storage<position>().size(), registry.storage<entt::entity>().size());
ASSERT_EQ(registry.storage<test::boxed_int>().size(), registry.storage<test::empty>().size());
ASSERT_EQ(registry.storage<test::boxed_int>().size(), registry.storage<entt::entity>().size());
registry.view<position>().each([](auto entity, auto &position) {
ASSERT_EQ(position.x, static_cast<int>(entt::to_integral(entity) + 16u));
ASSERT_EQ(position.y, static_cast<int>(entt::to_integral(entity) + 16u));
registry.view<test::boxed_int>().each([count](auto entity, auto &elem) {
ASSERT_EQ(elem.value, entt::to_integral(entity) + count);
});
registry = {};

View File

@@ -1,22 +1,24 @@
#include <common/boxed_type.h>
#include <common/empty.h>
#include <cr.h>
#include <entt/entity/registry.hpp>
#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 &registry = *static_cast<entt::registry *>(ctx->userdata);
// forces the creation of the pool for the velocity component
static_cast<void>(registry.storage<velocity>());
// forces the creation of the pool for the empty component
static_cast<void>(registry.storage<test::empty>());
const auto view = registry.view<position>();
registry.insert(view.begin(), view.end(), velocity{1., 1.});
const auto view = registry.view<test::boxed_int>();
registry.insert(view.begin(), view.end(), test::empty{});
registry.view<position, velocity>().each([](position &pos, velocity &vel) {
pos.x += static_cast<int>(16 * vel.dx);
pos.y += static_cast<int>(16 * vel.dy);
registry.view<test::boxed_int, test::empty>().each([count](test::boxed_int &elem) {
elem.value += count;
});
} break;
case CR_CLOSE:

View File

@@ -1,21 +1,20 @@
#include <common/boxed_type.h>
#include <common/empty.h>
#include <entt/core/attribute.h>
#include <entt/entity/registry.hpp>
#include "../common/types.h"
template class entt::basic_registry<entt::entity>;
ENTT_API void update_position(entt::registry &registry) {
registry.view<position, velocity>().each([](auto &pos, auto &vel) {
pos.x += static_cast<int>(16 * vel.dx);
pos.y += static_cast<int>(16 * vel.dy);
ENTT_API void update(entt::registry &registry, int value) {
registry.view<test::boxed_int, test::empty>().each([value](auto &elem) {
elem.value += value;
});
}
ENTT_API void emplace_velocity(entt::registry &registry) {
// forces the creation of the pool for the velocity component
static_cast<void>(registry.storage<velocity>());
ENTT_API void insert(entt::registry &registry) {
// forces the creation of the pool for the empty type
static_cast<void>(registry.storage<test::empty>());
for(auto entity: registry.view<position>()) {
registry.emplace<velocity>(entity, 1., 1.);
}
const auto view = registry.view<test::boxed_int>();
registry.insert<test::empty>(view.begin(), view.end());
}

View File

@@ -1,27 +1,28 @@
#include <gtest/gtest.h>
#include <common/boxed_type.h>
#include <common/empty.h>
#include <entt/core/attribute.h>
#include <entt/entity/entity.hpp>
#include <entt/entity/registry.hpp>
#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<position>(entity, i, i);
registry.emplace<test::boxed_int>(entity, i);
}
emplace_velocity(registry);
update_position(registry);
insert(registry);
update(registry, count);
ASSERT_EQ(registry.storage<position>().size(), registry.storage<velocity>().size());
ASSERT_EQ(registry.storage<test::boxed_int>().size(), registry.storage<test::empty>().size());
registry.view<position>().each([](auto entity, auto &position) {
ASSERT_EQ(position.x, static_cast<int>(entt::to_integral(entity) + 16));
ASSERT_EQ(position.y, static_cast<int>(entt::to_integral(entity) + 16));
registry.view<test::boxed_int>().each([count](auto entity, auto &elem) {
ASSERT_EQ(elem.value, static_cast<int>(entt::to_integral(entity) + count));
});
}