build: review example tests

This commit is contained in:
skypjack
2025-12-24 10:26:01 +01:00
parent 9bcd14f31f
commit 1f7efe511d
5 changed files with 104 additions and 94 deletions

View File

@@ -151,10 +151,14 @@ endif()
# Test example
if(ENTT_BUILD_EXAMPLE)
SETUP_BASIC_TEST_DEPRECATED(custom_identifier example/custom_identifier.cpp)
SETUP_BASIC_TEST_DEPRECATED(entity_copy example/entity_copy.cpp)
SETUP_BASIC_TEST_DEPRECATED(reserved_bits example/reserved_bits.cpp)
SETUP_BASIC_TEST_DEPRECATED(signal_less example/signal_less.cpp)
SETUP_BASIC_TEST(
NAME example
SOURCES
example/custom_identifier.cpp
example/entity_copy.cpp
example/reserved_bits.cpp
example/signal_less.cpp
)
endif()
# Test lib

View File

@@ -3,30 +3,32 @@
#include <entt/entity/entity.hpp>
#include <entt/entity/registry.hpp>
struct entity_id final {
using entity_type = std::uint32_t;
static constexpr auto null = entt::null;
struct CustomIdentifier: testing::Test {
struct entity_id final {
using entity_type = std::uint32_t;
static constexpr auto null = entt::null;
constexpr entity_id(entity_type value = null) noexcept
: entt{value} {}
constexpr entity_id(entity_type value = null) noexcept
: entt{value} {}
~entity_id() = default;
~entity_id() = default;
constexpr entity_id(const entity_id &other) = default;
constexpr entity_id(entity_id &&other) noexcept = default;
constexpr entity_id(const entity_id &other) = default;
constexpr entity_id(entity_id &&other) noexcept = default;
constexpr entity_id &operator=(const entity_id &other) = default;
constexpr entity_id &operator=(entity_id &&other) noexcept = default;
constexpr entity_id &operator=(const entity_id &other) = default;
constexpr entity_id &operator=(entity_id &&other) noexcept = default;
constexpr operator entity_type() const noexcept {
return entt;
}
constexpr operator entity_type() const noexcept {
return entt;
}
private:
entity_type entt;
private:
entity_type entt;
};
};
TEST(Example, CustomIdentifier) {
TEST_F(CustomIdentifier, Example) {
entt::basic_registry<entity_id> registry{};
entity_id entity{};

View File

@@ -8,47 +8,50 @@
#include <entt/meta/policy.hpp>
#include <entt/meta/resolve.hpp>
enum class my_entity : entt::id_type {};
struct EntityCopy: testing::Test {
enum class my_entity : entt::id_type {};
enum class other_entity : entt::id_type {};
template<typename Type>
// NOLINTNEXTLINE(*-exception-escape)
struct meta_mixin: Type {
using allocator_type = Type::allocator_type;
using element_type = Type::element_type;
template<typename Type>
// NOLINTNEXTLINE(*-exception-escape)
struct meta_mixin: Type {
using allocator_type = Type::allocator_type;
using element_type = Type::element_type;
explicit meta_mixin(const allocator_type &allocator);
};
explicit meta_mixin(const allocator_type &allocator);
};
template<typename Type, typename Entity>
struct entt::storage_type<Type, Entity> {
using type = meta_mixin<basic_storage<Type, Entity>>;
void TearDown() override {
entt::meta_reset();
}
};
template<typename Type>
meta_mixin<Type>::meta_mixin(const allocator_type &allocator)
struct entt::storage_type<Type, EntityCopy::my_entity> {
using type = EntityCopy::meta_mixin<basic_storage<Type, EntityCopy::my_entity>>;
};
template<typename Type>
struct entt::storage_type<Type, EntityCopy::other_entity> {
using type = EntityCopy::meta_mixin<basic_storage<Type, EntityCopy::other_entity>>;
};
template<typename Type>
EntityCopy::meta_mixin<Type>::meta_mixin(const allocator_type &allocator)
: Type{allocator} {
using namespace entt::literals;
entt::meta_factory<element_type>{}
// cross registry, same type
.template func<entt::overload<entt::storage_for_t<element_type, entt::entity> &(const entt::id_type)>(&entt::basic_registry<entt::entity>::storage<element_type>), entt::as_ref_t>("storage"_hs)
.template func<entt::overload<entt::storage_for_t<element_type, my_entity> &(const entt::id_type)>(&entt::basic_registry<my_entity>::storage<element_type>), entt::as_ref_t>("storage"_hs)
// cross registry, different types
.template func<entt::overload<entt::storage_for_t<element_type, my_entity> &(const entt::id_type)>(&entt::basic_registry<my_entity>::storage<element_type>), entt::as_ref_t>("storage"_hs);
.template func<entt::overload<entt::storage_for_t<element_type, other_entity> &(const entt::id_type)>(&entt::basic_registry<other_entity>::storage<element_type>), entt::as_ref_t>("storage"_hs);
}
template<typename Type>
struct EntityCopy: testing::Test {
using type = Type;
};
using EntityCopyTypes = ::testing::Types<entt::basic_registry<entt::entity>, entt::basic_registry<my_entity>>;
TYPED_TEST_SUITE(EntityCopy, EntityCopyTypes, );
TEST(EntityCopy, SameRegistry) {
TEST_F(EntityCopy, SameRegistry) {
using namespace entt::literals;
entt::registry registry{};
entt::basic_registry<my_entity> registry{};
auto &&custom = registry.storage<double>("custom"_hs);
const auto src = registry.create();
@@ -58,7 +61,7 @@ TEST(EntityCopy, SameRegistry) {
registry.emplace<int>(src, 2);
registry.emplace<char>(src, 'c');
ASSERT_EQ(registry.storage<entt::entity>().size(), 2u);
ASSERT_EQ(registry.storage<my_entity>().size(), 2u);
ASSERT_TRUE(custom.contains(src));
ASSERT_FALSE(custom.contains(dst));
ASSERT_TRUE((registry.all_of<int, char>(src)));
@@ -71,7 +74,7 @@ TEST(EntityCopy, SameRegistry) {
}
}
ASSERT_EQ(registry.storage<entt::entity>().size(), 2u);
ASSERT_EQ(registry.storage<my_entity>().size(), 2u);
ASSERT_TRUE(custom.contains(src));
ASSERT_FALSE(custom.contains(dst));
ASSERT_TRUE((registry.all_of<int, char>(src)));
@@ -81,12 +84,11 @@ TEST(EntityCopy, SameRegistry) {
ASSERT_EQ(registry.get<char>(dst), 'c');
}
TYPED_TEST(EntityCopy, CrossRegistry) {
TEST_F(EntityCopy, CrossRegistry) {
using namespace entt::literals;
entt::basic_registry<entt::entity> src{};
// other registry type, see typed test suite
typename TestFixture::type dst{};
entt::basic_registry<my_entity> src{};
entt::basic_registry<other_entity> dst{};
const auto entity = src.create();
const auto copy = dst.create();
@@ -94,8 +96,8 @@ TYPED_TEST(EntityCopy, CrossRegistry) {
src.emplace<int>(entity, 2);
src.emplace<char>(entity, 'c');
ASSERT_EQ(src.storage<entt::entity>().size(), 1u);
ASSERT_EQ(dst.template storage<typename TestFixture::type::entity_type>().size(), 1u);
ASSERT_EQ(src.storage<my_entity>().size(), 1u);
ASSERT_EQ(dst.storage<other_entity>().size(), 1u);
ASSERT_TRUE((src.all_of<int, char>(entity)));
ASSERT_FALSE((dst.template all_of<int, char>(copy)));
@@ -114,8 +116,8 @@ TYPED_TEST(EntityCopy, CrossRegistry) {
}
}
ASSERT_EQ(src.storage<entt::entity>().size(), 1u);
ASSERT_EQ(dst.template storage<typename TestFixture::type::entity_type>().size(), 1u);
ASSERT_EQ(src.storage<my_entity>().size(), 1u);
ASSERT_EQ(dst.storage<other_entity>().size(), 1u);
ASSERT_TRUE((src.all_of<int, char>(entity)));
ASSERT_TRUE((dst.template all_of<int, char>(copy)));

View File

@@ -10,33 +10,31 @@
#include <entt/entity/registry.hpp>
#include <entt/entity/view.hpp>
enum class my_entity : entt::id_type {
disabled = 0x10000000,
_entt_enum_as_bitmask
};
struct ReservedBits: testing::Test {
enum class my_entity : entt::id_type {
disabled = 0x10000000,
_entt_enum_as_bitmask
};
struct entity_traits {
using value_type = my_entity;
using entity_type = std::uint32_t;
using version_type = std::uint16_t;
static constexpr entity_type entity_mask = 0xFFFF; // 16b
static constexpr entity_type version_mask = 0x0FFF; // 12b
struct entity_traits {
using value_type = my_entity;
using entity_type = std::uint32_t;
using version_type = std::uint16_t;
static constexpr entity_type entity_mask = 0xFFFF; // 16b
static constexpr entity_type version_mask = 0x0FFF; // 12b
};
[[nodiscard]] static bool is_disabled(const my_entity entity) {
return ((entity & my_entity::disabled) == my_entity::disabled);
}
};
template<>
struct entt::entt_traits<my_entity>: entt::basic_entt_traits<entity_traits> {
struct entt::entt_traits<ReservedBits::my_entity>: entt::basic_entt_traits<ReservedBits::entity_traits> {
static constexpr std::size_t page_size = ENTT_SPARSE_PAGE;
};
namespace {
[[nodiscard]] bool is_disabled(const my_entity entity) {
return ((entity & my_entity::disabled) == my_entity::disabled);
}
} // namespace
TEST(Example, DisabledEntity) {
TEST_F(ReservedBits, DisabledEntity) {
entt::basic_registry<my_entity> registry{};
auto view = registry.view<my_entity, int>();

View File

@@ -4,33 +4,37 @@
#include <entt/entity/registry.hpp>
#include <entt/entity/storage.hpp>
template<typename Type, typename Entity>
struct entt::storage_type<Type, Entity> {
struct SignalLess: testing::Test {
enum entity : std::uint32_t {};
template<typename, typename = void>
struct has_on_construct: std::false_type {};
template<typename Type>
struct has_on_construct<Type, std::void_t<decltype(&entt::storage_type_t<Type, entity>::on_construct)>>: std::true_type {};
template<typename Type>
static constexpr auto has_on_construct_v = has_on_construct<Type>::value;
};
template<typename Type>
struct entt::storage_type<Type, SignalLess::entity> {
// no signal regardless of element type ...
using type = basic_storage<Type, Entity>;
using type = basic_storage<Type, SignalLess::entity>;
};
template<typename Entity>
struct entt::storage_type<char, Entity> {
template<>
struct entt::storage_type<char, SignalLess::entity> {
// ... unless it's char, because yes.
using type = sigh_mixin<basic_storage<char, Entity>>;
using type = sigh_mixin<basic_storage<char, SignalLess::entity>>;
};
template<typename, typename, typename = void>
struct has_on_construct: std::false_type {};
template<typename Entity, typename Type>
struct has_on_construct<Entity, Type, std::void_t<decltype(&entt::storage_type_t<Type>::on_construct)>>: std::true_type {};
template<typename Entity, typename Type>
inline constexpr auto has_on_construct_v = has_on_construct<Entity, Type>::value;
TEST(Example, SignalLess) {
TEST_F(SignalLess, Example) {
// invoking registry::on_construct<int> is a compile-time error
ASSERT_FALSE((has_on_construct_v<entt::entity, int>));
ASSERT_TRUE((has_on_construct_v<entt::entity, char>));
ASSERT_FALSE((has_on_construct_v<int>));
ASSERT_TRUE((has_on_construct_v<char>));
entt::registry registry;
entt::basic_registry<entity> registry;
const std::array entity{registry.create()};
// literally a test for storage_adapter_mixin