test: lib std cpp

This commit is contained in:
Michele Caini
2019-12-23 22:21:28 +01:00
parent f577183c24
commit e3968a8f9d
19 changed files with 285 additions and 35 deletions

2
TODO
View File

@@ -26,6 +26,6 @@
- ::group improve, reduce code
* Mission: get rid of named types
- add tests for type_info fallback and type_info specializations
- add tests for type_info specializations (eg dispatcher_plugin_std)
- update doc: dispatcher, emitter, registry, meta, across boundaries
- review and suppress warnings, if any

View File

@@ -7,35 +7,21 @@
#include "hashed_string.hpp"
namespace entt {
/**
* @brief Types identifiers.
* @tparam Type Type for which to generate an identifier.
*/
template<typename Type, typename = void>
struct ENTT_API type_info {
/**
* @brief Returns the numeric representation of a given type.
* @return The numeric representation of the given type.
*/
#if defined ENTT_PRETTY_FUNCTION
static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT {
return entt::hashed_string{ENTT_PRETTY_FUNCTION};
}
#ifdef ENTT_PRETTY_FUNCTION
# define ENTT_TYPE_ID_API
#else
static ENTT_ID_TYPE id() ENTT_NOEXCEPT;
};
# define ENTT_TYPE_ID_API ENTT_API
#endif
#ifndef ENTT_PRETTY_FUNCTION
/**
* @cond TURN_OFF_DOXYGEN
* Internal details not to be documented.
*/
namespace internal {
namespace entt::internal {
struct ENTT_API type_id_generator {
@@ -53,12 +39,31 @@ struct ENTT_API type_id_generator {
* Internal details not to be documented.
* @endcond TURN_OFF_DOXYGEN
*/
#endif
template<typename Type, typename Cond>
ENTT_ID_TYPE type_info<Type, Cond>::id() ENTT_NOEXCEPT {
static const ENTT_ID_TYPE value = internal::type_id_generator::next();
return value;
namespace entt {
/**
* @brief Types identifiers.
* @tparam Type Type for which to generate an identifier.
*/
template<typename Type, typename = void>
struct ENTT_TYPE_ID_API type_info {
/**
* @brief Returns the numeric representation of a given type.
* @return The numeric representation of the given type.
*/
#ifdef ENTT_PRETTY_FUNCTION
static constexpr ENTT_ID_TYPE id() ENTT_NOEXCEPT {
return entt::hashed_string{ENTT_PRETTY_FUNCTION};
}
#else
static ENTT_ID_TYPE id() ENTT_NOEXCEPT {
static const ENTT_ID_TYPE value = internal::type_id_generator::next();
return value;
}
#endif
};

View File

@@ -50,8 +50,8 @@ macro(SETUP_LIB_TEST TEST_NAME)
add_library(_${TEST_NAME} SHARED lib/${TEST_NAME}/lib.cpp)
SETUP_TARGET(_${TEST_NAME})
SETUP_BASIC_TEST(lib_${TEST_NAME} lib/${TEST_NAME}/main.cpp)
target_compile_definitions(_${TEST_NAME} PRIVATE ENTT_API_EXPORT)
target_compile_definitions(lib_${TEST_NAME} PRIVATE ENTT_API_IMPORT)
target_compile_definitions(_${TEST_NAME} PRIVATE ENTT_API_EXPORT ${ARGV1})
target_compile_definitions(lib_${TEST_NAME} PRIVATE ENTT_API_IMPORT ${ARGV1})
target_link_libraries(lib_${TEST_NAME} PRIVATE _${TEST_NAME})
endmacro()
@@ -86,6 +86,11 @@ if(BUILD_LIB)
SETUP_LIB_TEST(meta)
SETUP_LIB_TEST(registry)
SETUP_LIB_TEST(dispatcher_std ENTT_STANDARD_CPP)
SETUP_LIB_TEST(emitter_std ENTT_STANDARD_CPP)
SETUP_LIB_TEST(meta_std ENTT_STANDARD_CPP)
SETUP_LIB_TEST(registry_std ENTT_STANDARD_CPP)
SETUP_PLUGIN_TEST(dispatcher_plugin)
SETUP_PLUGIN_TEST(emitter_plugin)
SETUP_PLUGIN_TEST(meta_plugin)

View File

@@ -1,5 +1,5 @@
#ifndef ENTT_PLUGIN_DISPATCHER_TYPES_H
#define ENTT_PLUGIN_DISPATCHER_TYPES_H
#ifndef ENTT_LIB_DISPATCHER_PLUGIN_TYPES_H
#define ENTT_LIB_DISPATCHER_PLUGIN_TYPES_H
struct message {
int payload;

View File

@@ -0,0 +1,8 @@
#include <entt/core/attribute.h>
#include <entt/signal/dispatcher.hpp>
#include "types.h"
ENTT_API void trigger(entt::dispatcher &dispatcher) {
dispatcher.trigger<event>();
dispatcher.trigger<message>(42);
}

View File

@@ -0,0 +1,24 @@
#include <gtest/gtest.h>
#include <entt/core/attribute.h>
#include <entt/core/utility.hpp>
#include <entt/signal/dispatcher.hpp>
#include "types.h"
ENTT_API void trigger(entt::dispatcher &);
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);
trigger(dispatcher);
ASSERT_EQ(listener.value, 42);
}

View File

@@ -0,0 +1,12 @@
#ifndef ENTT_LIB_DISPATCHER_STD_TYPES_H
#define ENTT_LIB_DISPATCHER_STD_TYPES_H
#include <entt/core/attribute.h>
struct ENTT_API message {
int payload;
};
struct ENTT_API event {};
#endif

View File

@@ -1,5 +1,5 @@
#ifndef ENTT_PLUGIN_EMITTER_TYPES_H
#define ENTT_PLUGIN_EMITTER_TYPES_H
#ifndef ENTT_LIB_EMITTER_PLUGIN_TYPES_H
#define ENTT_LIB_EMITTER_PLUGIN_TYPES_H
#include <entt/signal/emitter.hpp>

View File

@@ -0,0 +1,9 @@
#include <entt/core/attribute.h>
#include <entt/signal/emitter.hpp>
#include "types.h"
ENTT_API void emit(test_emitter &emitter) {
emitter.publish<event>();
emitter.publish<message>(42);
emitter.publish<message>(3);
}

View File

@@ -0,0 +1,18 @@
#include <gtest/gtest.h>
#include <entt/core/attribute.h>
#include <entt/signal/emitter.hpp>
#include "types.h"
ENTT_API void emit(test_emitter &);
TEST(Lib, Emitter) {
test_emitter emitter;
int value{};
ASSERT_EQ(value, 0);
emitter.once<message>([&](message msg, test_emitter &) { value = msg.payload; });
emit(emitter);
ASSERT_EQ(value, 42);
}

View File

@@ -0,0 +1,17 @@
#ifndef ENTT_LIB_EMITTER_STD_TYPES_H
#define ENTT_LIB_EMITTER_STD_TYPES_H
#include <entt/core/attribute.h>
#include <entt/signal/emitter.hpp>
struct ENTT_API test_emitter
: entt::emitter<test_emitter>
{};
struct ENTT_API message {
int payload;
};
struct ENTT_API event {};
#endif

View File

@@ -1,5 +1,5 @@
#ifndef ENTT_PLUGIN_META_TYPES_H
#define ENTT_PLUGIN_META_TYPES_H
#ifndef ENTT_LIB_META_PLUGIN_TYPES_H
#define ENTT_LIB_META_PLUGIN_TYPES_H
#include <entt/meta/meta.hpp>

34
test/lib/meta_std/lib.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <entt/core/attribute.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};
}
ENTT_API void set_up(entt::meta_ctx ctx) {
entt::meta_ctx::bind(ctx);
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);
}
ENTT_API void tear_down() {
entt::meta<position>().reset();
entt::meta<velocity>().reset();
}
ENTT_API entt::meta_any wrap_int(int value) {
return value;
}

View File

@@ -0,0 +1,42 @@
#include <gtest/gtest.h>
#include <entt/core/attribute.h>
#include <entt/meta/factory.hpp>
#include <entt/meta/meta.hpp>
#include "types.h"
ENTT_API void set_up(entt::meta_ctx);
ENTT_API void tear_down();
ENTT_API entt::meta_any wrap_int(int);
TEST(Lib, Meta) {
ASSERT_FALSE(entt::resolve("position"_hs));
set_up(entt::meta_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(wrap_int(42).type(), entt::resolve<int>());
ASSERT_EQ(wrap_int(42).cast<int>(), 42);
tear_down();
ASSERT_FALSE(entt::resolve("position"_hs));
ASSERT_FALSE(entt::resolve("velocity"_hs));
}

14
test/lib/meta_std/types.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef ENTT_LIB_META_STD_TYPES_H
#define ENTT_LIB_META_STD_TYPES_H
struct position {
int x{};
int y{};
};
struct velocity {
double dx{};
double dy{};
};
#endif

View File

@@ -1,5 +1,5 @@
#ifndef ENTT_PLUGIN_REGISTRY_TYPES_H
#define ENTT_PLUGIN_REGISTRY_TYPES_H
#ifndef ENTT_LIB_REGISTRY_PLUGIN_TYPES_H
#define ENTT_LIB_REGISTRY_PLUGIN_TYPES_H
struct position {
int x;

View File

@@ -0,0 +1,19 @@
#include <entt/core/attribute.h>
#include <entt/entity/registry.hpp>
#include "types.h"
ENTT_API void update_position(entt::registry &registry) {
registry.view<position, velocity>().each([](auto &pos, auto &vel) {
pos.x += 16 * vel.dx;
pos.y += 16 * vel.dy;
});
}
ENTT_API void assign_velocity(entt::registry &registry) {
// 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.);
}
}

View File

@@ -0,0 +1,27 @@
#include <gtest/gtest.h>
#include <entt/core/attribute.h>
#include <entt/entity/entity.hpp>
#include <entt/entity/registry.hpp>
#include "types.h"
ENTT_API void update_position(entt::registry &);
ENTT_API void assign_velocity(entt::registry &);
TEST(Lib, Registry) {
entt::registry registry;
for(auto i = 0; i < 3; ++i) {
const auto entity = registry.create();
registry.assign<position>(entity, i, i);
}
assign_velocity(registry);
update_position(registry);
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);
});
}

View File

@@ -0,0 +1,16 @@
#ifndef ENTT_LIB_REGISTRY_STD_TYPES_H
#define ENTT_LIB_REGISTRY_STD_TYPES_H
#include <entt/core/attribute.h>
struct ENTT_API position {
int x;
int y;
};
struct ENTT_API velocity {
double dx;
double dy;
};
#endif