test: lib plugin std cpp

This commit is contained in:
Michele Caini
2019-12-24 00:20:31 +01:00
parent e3968a8f9d
commit bad342b840
13 changed files with 420 additions and 4 deletions

View File

@@ -59,11 +59,11 @@ macro(SETUP_PLUGIN_TEST TEST_NAME)
add_library(_${TEST_NAME} MODULE lib/${TEST_NAME}/plugin.cpp)
SETUP_TARGET(_${TEST_NAME})
SETUP_BASIC_TEST(lib_${TEST_NAME} lib/${TEST_NAME}/main.cpp)
target_link_libraries(lib_${TEST_NAME} PRIVATE ${CMAKE_DL_LIBS})
target_include_directories(lib_${TEST_NAME} PRIVATE ${CR_SRC_DIR})
target_compile_definitions(lib_${TEST_NAME} PRIVATE NOMINMAX PLUGIN="$<TARGET_FILE:_${TEST_NAME}>")
target_include_directories(_${TEST_NAME} PRIVATE ${CR_SRC_DIR})
target_compile_definitions(_${TEST_NAME} PRIVATE NOMINMAX)
target_include_directories(lib_${TEST_NAME} PRIVATE ${CR_SRC_DIR})
target_compile_definitions(lib_${TEST_NAME} PRIVATE NOMINMAX PLUGIN="$<TARGET_FILE:_${TEST_NAME}>" ${ARGV1})
target_compile_definitions(_${TEST_NAME} PRIVATE NOMINMAX ${ARGV1})
target_link_libraries(lib_${TEST_NAME} PRIVATE ${CMAKE_DL_LIBS})
endmacro()
# Test benchmark
@@ -95,6 +95,11 @@ if(BUILD_LIB)
SETUP_PLUGIN_TEST(emitter_plugin)
SETUP_PLUGIN_TEST(meta_plugin)
SETUP_PLUGIN_TEST(registry_plugin)
SETUP_PLUGIN_TEST(dispatcher_plugin_std ENTT_STANDARD_CPP)
SETUP_PLUGIN_TEST(emitter_plugin_std ENTT_STANDARD_CPP)
SETUP_PLUGIN_TEST(meta_plugin_std ENTT_STANDARD_CPP)
SETUP_PLUGIN_TEST(registry_plugin_std ENTT_STANDARD_CPP)
endif()
# Test mod

View File

@@ -0,0 +1,30 @@
#define CR_HOST
#include <cr.h>
#include <gtest/gtest.h>
#include <entt/core/utility.hpp>
#include <entt/signal/dispatcher.hpp>
#include "types.h"
struct listener {
void on(message msg) { value = msg.payload; }
int value{};
};
TEST(Lib, Dispatcher) {
entt::dispatcher dispatcher;
listener listener;
ASSERT_EQ(listener.value, 0);
dispatcher.sink<message>().connect<entt::overload<void(message)>(&listener::on)>(listener);
cr_plugin ctx;
ctx.userdata = &dispatcher;
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
ASSERT_EQ(listener.value, 42);
cr_plugin_close(ctx);
}

View File

@@ -0,0 +1,21 @@
#include <cr.h>
#include <entt/signal/dispatcher.hpp>
#include "types.h"
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
switch (operation) {
case CR_STEP:
static_cast<entt::dispatcher *>(ctx->userdata)->trigger<event>();
static_cast<entt::dispatcher *>(ctx->userdata)->trigger<message>(42);
break;
case CR_CLOSE:
static_cast<entt::dispatcher *>(ctx->userdata)->discard<event>();
break;
case CR_LOAD:
case CR_UNLOAD:
// nothing to do here, this is only a test.
break;
}
return 0;
}

View File

@@ -0,0 +1,33 @@
#ifndef ENTT_LIB_DISPATCHER_PLUGIN_STD_TYPES_H
#define ENTT_LIB_DISPATCHER_PLUGIN_STD_TYPES_H
#include <type_traits>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
template<typename>
struct event_id;
#define ASSIGN_TYPE_ID(clazz)\
template<>\
struct event_id<clazz>\
: std::integral_constant<ENTT_ID_TYPE, entt::basic_hashed_string<std::remove_cv_t<std::remove_pointer_t<std::decay_t<decltype(#clazz)>>>>{#clazz}>\
{}
template<typename Type>
struct entt::type_info<Type> {
static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT {
return event_id<Type>::value;
}
};
struct message {
int payload;
};
struct event {};
ASSIGN_TYPE_ID(message);
ASSIGN_TYPE_ID(event);
#endif

View File

@@ -0,0 +1,24 @@
#define CR_HOST
#include <cr.h>
#include <gtest/gtest.h>
#include <entt/signal/emitter.hpp>
#include "types.h"
TEST(Lib, Emitter) {
test_emitter emitter;
int value{};
ASSERT_EQ(value, 0);
emitter.once<message>([&](message msg, test_emitter &) { value = msg.payload; });
cr_plugin ctx;
ctx.userdata = &emitter;
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
ASSERT_EQ(value, 42);
cr_plugin_close(ctx);
}

View File

@@ -0,0 +1,22 @@
#include <cr.h>
#include <entt/signal/emitter.hpp>
#include "types.h"
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
switch (operation) {
case CR_STEP:
static_cast<test_emitter *>(ctx->userdata)->publish<event>();
static_cast<test_emitter *>(ctx->userdata)->publish<message>(42);
static_cast<test_emitter *>(ctx->userdata)->publish<message>(3);
break;
case CR_CLOSE:
static_cast<test_emitter *>(ctx->userdata)->discard<event>();
break;
case CR_LOAD:
case CR_UNLOAD:
// nothing to do here, this is only a test.
break;
}
return 0;
}

View File

@@ -0,0 +1,38 @@
#ifndef ENTT_LIB_EMITTER_PLUGIN_STD_TYPES_H
#define ENTT_LIB_EMITTER_PLUGIN_STD_TYPES_H
#include <type_traits>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
#include <entt/signal/emitter.hpp>
template<typename>
struct event_id;
#define ASSIGN_TYPE_ID(clazz)\
template<>\
struct event_id<clazz>\
: std::integral_constant<ENTT_ID_TYPE, entt::basic_hashed_string<std::remove_cv_t<std::remove_pointer_t<std::decay_t<decltype(#clazz)>>>>{#clazz}>\
{}
template<typename Type>
struct entt::type_info<Type> {
static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT {
return event_id<Type>::value;
}
};
struct test_emitter
: entt::emitter<test_emitter>
{};
struct message {
int payload;
};
struct event {};
ASSIGN_TYPE_ID(message);
ASSIGN_TYPE_ID(event);
#endif

View File

@@ -0,0 +1,51 @@
#define CR_HOST
#include <cr.h>
#include <gtest/gtest.h>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include "types.h"
TEST(Lib, Meta) {
ASSERT_FALSE(entt::resolve("position"_hs));
userdata ud{};
cr_plugin ctx;
ctx.userdata = &ud;
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
entt::meta<double>().conv<int>();
ASSERT_TRUE(entt::resolve("position"_hs));
ASSERT_TRUE(entt::resolve("velocity"_hs));
auto pos = entt::resolve("position"_hs).construct(42., 3.);
auto vel = entt::resolve("velocity"_hs).ctor().invoke();
ASSERT_TRUE(pos && vel);
ASSERT_EQ(pos.type().data("x"_hs).type(), entt::resolve<int>());
ASSERT_TRUE(pos.type().data("y"_hs).get(*pos).try_cast<int>());
ASSERT_EQ(pos.type().data("x"_hs).get(*pos).cast<int>(), 42);
ASSERT_EQ(pos.type().data("y"_hs).get(*pos).cast<int>(), 3);
ASSERT_EQ(vel.type().data("dx"_hs).type(), entt::resolve<double>());
ASSERT_TRUE(vel.type().data("dy"_hs).get(*vel).convert<double>());
ASSERT_EQ(vel.type().data("dx"_hs).get(*vel).cast<double>(), 0.);
ASSERT_EQ(vel.type().data("dy"_hs).get(*vel).cast<double>(), 0.);
ASSERT_EQ(ud.any.type(), entt::resolve<int>());
ASSERT_EQ(ud.any.cast<int>(), 42);
// these objects have been initialized from a different context
pos.emplace<void>();
vel.emplace<void>();
ud.any.emplace<void>();
cr_plugin_close(ctx);
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("velocity"_hs));
}

View File

@@ -0,0 +1,46 @@
#include <cr.h>
#include <entt/core/hashed_string.hpp>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include "types.h"
position create_position(int x, int y) {
return position{x, y};
}
void set_up() {
entt::meta<position>()
.type("position"_hs)
.ctor<&create_position>()
.data<&position::x>("x"_hs)
.data<&position::y>("y"_hs);
entt::meta<velocity>()
.type("velocity"_hs)
.ctor<>()
.data<&velocity::dx>("dx"_hs)
.data<&velocity::dy>("dy"_hs);
}
void tear_down() {
entt::meta<position>().reset();
entt::meta<velocity>().reset();
}
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
switch (operation) {
case CR_LOAD:
entt::meta_ctx::bind(static_cast<userdata *>(ctx->userdata)->ctx);
set_up();
break;
case CR_STEP:
static_cast<userdata *>(ctx->userdata)->any = 42;
break;
case CR_UNLOAD:
case CR_CLOSE:
tear_down();
break;
}
return 0;
}

View File

@@ -0,0 +1,47 @@
#ifndef ENTT_LIB_META_PLUGIN_TYPES_STD_H
#define ENTT_LIB_META_PLUGIN_TYPES_STD_H
#include <type_traits>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
#include <entt/meta/meta.hpp>
template<typename>
struct type_id;
#define ASSIGN_TYPE_ID(clazz)\
template<>\
struct type_id<clazz>\
: std::integral_constant<ENTT_ID_TYPE, entt::basic_hashed_string<std::remove_cv_t<std::remove_pointer_t<std::decay_t<decltype(#clazz)>>>>{#clazz}>\
{}
template<typename Type>
struct entt::type_info<Type> {
static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT {
return type_id<Type>::value;
}
};
struct position {
int x{};
int y{};
};
struct velocity {
double dx{};
double dy{};
};
struct userdata {
entt::meta_ctx ctx;
entt::meta_any any;
};
ASSIGN_TYPE_ID(void);
ASSIGN_TYPE_ID(std::size_t);
ASSIGN_TYPE_ID(position);
ASSIGN_TYPE_ID(velocity);
ASSIGN_TYPE_ID(double);
ASSIGN_TYPE_ID(int);
#endif

View File

@@ -0,0 +1,29 @@
#define CR_HOST
#include <cr.h>
#include <gtest/gtest.h>
#include <entt/entity/registry.hpp>
#include "types.h"
TEST(Lib, Registry) {
entt::registry registry;
for(auto i = 0; i < 3; ++i) {
const auto entity = registry.create();
registry.assign<position>(entity, i, i);
}
cr_plugin ctx;
ctx.userdata = &registry;
cr_plugin_load(ctx, PLUGIN);
cr_plugin_update(ctx);
ASSERT_EQ(registry.size<position>(), registry.size<velocity>());
registry.view<position>().each([](auto entity, auto &position) {
ASSERT_EQ(position.x, entt::to_integral(entity) + 16);
ASSERT_EQ(position.y, entt::to_integral(entity) + 16);
});
cr_plugin_close(ctx);
}

View File

@@ -0,0 +1,33 @@
#include <cr.h>
#include <entt/entity/registry.hpp>
#include "types.h"
CR_EXPORT int cr_main(cr_plugin *ctx, cr_op operation) {
switch (operation) {
case CR_STEP:
[ctx]() {
auto *registry = static_cast<entt::registry *>(ctx->userdata);
// forces the creation of the pool for the velocity component
registry->prepare<velocity>();
for(auto entity: registry->view<position>()) {
registry->assign<velocity>(entity, 1., 1.);
}
registry->view<position, velocity>().each([](auto &pos, auto &vel) {
pos.x += 16 * vel.dx;
pos.y += 16 * vel.dy;
});
}();
break;
case CR_CLOSE:
static_cast<entt::registry *>(ctx->userdata)->discard<velocity>();
break;
case CR_LOAD:
case CR_UNLOAD:
// nothing to do here, this is only a test.
break;
}
return 0;
}

View File

@@ -0,0 +1,37 @@
#ifndef ENTT_LIB_REGISTRY_PLUGIN_STD_TYPES_H
#define ENTT_LIB_REGISTRY_PLUGIN_STD_TYPES_H
#include <type_traits>
#include <entt/core/hashed_string.hpp>
#include <entt/core/type_info.hpp>
template<typename>
struct component_id;
#define ASSIGN_TYPE_ID(clazz)\
template<>\
struct component_id<clazz>\
: std::integral_constant<ENTT_ID_TYPE, entt::basic_hashed_string<std::remove_cv_t<std::remove_pointer_t<std::decay_t<decltype(#clazz)>>>>{#clazz}>\
{}
template<typename Type>
struct entt::type_info<Type> {
static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT {
return component_id<Type>::value;
}
};
struct position {
int x;
int y;
};
struct velocity {
double dx;
double dy;
};
ASSIGN_TYPE_ID(position);
ASSIGN_TYPE_ID(velocity);
#endif