From b52c9c1676f200231d76f5ab478e97dcf1f919be Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 27 Aug 2021 15:47:48 +0200 Subject: [PATCH] test: use storage (and sigh storage mixin) from base type --- test/CMakeLists.txt | 1 + test/entt/entity/sigh_storage_mixin.cpp | 205 ++++++++++++++++++++++++ test/entt/entity/storage.cpp | 91 ++++++++++- 3 files changed, 296 insertions(+), 1 deletion(-) create mode 100644 test/entt/entity/sigh_storage_mixin.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 329ee17d2..62728d560 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -186,6 +186,7 @@ SETUP_BASIC_TEST(organizer entt/entity/organizer.cpp) SETUP_BASIC_TEST(registry entt/entity/registry.cpp) SETUP_BASIC_TEST(registry_no_eto entt/entity/registry_no_eto.cpp ENTT_NO_ETO) SETUP_BASIC_TEST(runtime_view entt/entity/runtime_view.cpp) +SETUP_BASIC_TEST(sigh_storage_mixin entt/entity/sigh_storage_mixin.cpp) SETUP_BASIC_TEST(snapshot entt/entity/snapshot.cpp) SETUP_BASIC_TEST(sparse_set entt/entity/sparse_set.cpp) SETUP_BASIC_TEST(storage entt/entity/storage.cpp) diff --git a/test/entt/entity/sigh_storage_mixin.cpp b/test/entt/entity/sigh_storage_mixin.cpp new file mode 100644 index 000000000..1c1773f6a --- /dev/null +++ b/test/entt/entity/sigh_storage_mixin.cpp @@ -0,0 +1,205 @@ +#include +#include +#include +#include +#include +#include + +struct empty_type {}; +struct stable_type { int value; }; + +struct non_default_constructible { + non_default_constructible() = delete; + non_default_constructible(int v): value{v} {} + int value; +}; + +template<> +struct entt::component_traits: basic_component_traits { + using in_place_delete = std::true_type; +}; + +struct counter { + int value{}; +}; + +void listener(counter &counter, entt::registry &, entt::entity) { + ++counter.value; +} + +TEST(SighStorageMixin, GenericType) { + entt::sigh_storage_mixin> pool; + entt::sparse_set &base = pool; + entt::entity entities[2u]{entt::entity{3}, entt::entity{42}}; + entt::registry registry{}; + + counter on_construct{}; + counter on_destroy{}; + + pool.on_construct().connect<&listener>(on_construct); + pool.on_destroy().connect<&listener>(on_destroy); + + base.emplace(entities[0u], ®istry); + pool.emplace(registry, entities[1u]); + + ASSERT_EQ(on_construct.value, 2); + ASSERT_EQ(on_destroy.value, 0); + ASSERT_FALSE(pool.empty()); + + ASSERT_EQ(pool.get(entities[0u]), 0); + ASSERT_EQ(pool.get(entities[1u]), 0); + + base.erase(entities[0u], ®istry); + pool.erase(entities[1u], ®istry); + + ASSERT_EQ(on_construct.value, 2); + ASSERT_EQ(on_destroy.value, 2); + ASSERT_TRUE(pool.empty()); + + base.insert(std::begin(entities), std::end(entities), ®istry); + + ASSERT_EQ(pool.get(entities[0u]), 0); + ASSERT_EQ(pool.get(entities[1u]), 0); + ASSERT_FALSE(pool.empty()); + + base.erase(entities[1u], ®istry); + + ASSERT_EQ(on_construct.value, 4); + ASSERT_EQ(on_destroy.value, 3); + ASSERT_FALSE(pool.empty()); + + base.erase(entities[0u], ®istry); + + ASSERT_EQ(on_construct.value, 4); + ASSERT_EQ(on_destroy.value, 4); + ASSERT_TRUE(pool.empty()); + + pool.insert(registry, std::begin(entities), std::end(entities), 3); + + ASSERT_EQ(on_construct.value, 6); + ASSERT_EQ(on_destroy.value, 4); + ASSERT_FALSE(pool.empty()); + + ASSERT_EQ(pool.get(entities[0u]), 3); + ASSERT_EQ(pool.get(entities[1u]), 3); + + pool.erase(std::begin(entities), std::end(entities), ®istry); + + ASSERT_EQ(on_construct.value, 6); + ASSERT_EQ(on_destroy.value, 6); + ASSERT_TRUE(pool.empty()); +} + +TEST(SighStorageMixin, EmptyType) { + entt::sigh_storage_mixin> pool; + entt::sparse_set &base = pool; + entt::entity entities[2u]{entt::entity{3}, entt::entity{42}}; + entt::registry registry{}; + + counter on_construct{}; + counter on_destroy{}; + + pool.on_construct().connect<&listener>(on_construct); + pool.on_destroy().connect<&listener>(on_destroy); + + base.emplace(entities[0u], ®istry); + pool.emplace(registry, entities[1u]); + + ASSERT_EQ(on_construct.value, 2); + ASSERT_EQ(on_destroy.value, 0); + ASSERT_FALSE(pool.empty()); + + ASSERT_TRUE(pool.contains(entities[0u])); + ASSERT_TRUE(pool.contains(entities[1u])); + + base.erase(entities[0u], ®istry); + pool.erase(entities[1u], ®istry); + + ASSERT_EQ(on_construct.value, 2); + ASSERT_EQ(on_destroy.value, 2); + ASSERT_TRUE(pool.empty()); + + base.insert(std::begin(entities), std::end(entities), ®istry); + + ASSERT_TRUE(pool.contains(entities[0u])); + ASSERT_TRUE(pool.contains(entities[1u])); + ASSERT_FALSE(pool.empty()); + + base.erase(entities[1u], ®istry); + + ASSERT_EQ(on_construct.value, 4); + ASSERT_EQ(on_destroy.value, 3); + ASSERT_FALSE(pool.empty()); + + base.erase(entities[0u], ®istry); + + ASSERT_EQ(on_construct.value, 4); + ASSERT_EQ(on_destroy.value, 4); + ASSERT_TRUE(pool.empty()); + + pool.insert(registry, std::begin(entities), std::end(entities)); + + ASSERT_EQ(on_construct.value, 6); + ASSERT_EQ(on_destroy.value, 4); + ASSERT_FALSE(pool.empty()); + + ASSERT_TRUE(pool.contains(entities[0u])); + ASSERT_TRUE(pool.contains(entities[1u])); + + pool.erase(std::begin(entities), std::end(entities), ®istry); + + ASSERT_EQ(on_construct.value, 6); + ASSERT_EQ(on_destroy.value, 6); + ASSERT_TRUE(pool.empty()); +} + +TEST(SighStorageMixin, NonDefaultConstructibleType) { + entt::sigh_storage_mixin> pool; + entt::sparse_set &base = pool; + entt::entity entities[2u]{entt::entity{3}, entt::entity{42}}; + entt::registry registry{}; + + counter on_construct{}; + counter on_destroy{}; + + pool.on_construct().connect<&listener>(on_construct); + pool.on_destroy().connect<&listener>(on_destroy); + + ASSERT_DEATH(base.emplace(entities[0u], ®istry), ""); + + pool.emplace(registry, entities[1u], 3); + + ASSERT_EQ(on_construct.value, 1); + ASSERT_EQ(on_destroy.value, 0); + ASSERT_FALSE(pool.empty()); + + ASSERT_FALSE(pool.contains(entities[0u])); + ASSERT_EQ(pool.get(entities[1u]).value, 3); + + base.erase(entities[1u], ®istry); + + ASSERT_EQ(on_construct.value, 1); + ASSERT_EQ(on_destroy.value, 1); + ASSERT_TRUE(pool.empty()); + + ASSERT_DEATH(base.insert(std::begin(entities), std::end(entities), ®istry), ""); + + ASSERT_FALSE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + ASSERT_TRUE(pool.empty()); + + pool.insert(registry, std::begin(entities), std::end(entities), 3); + + ASSERT_EQ(on_construct.value, 3); + ASSERT_EQ(on_destroy.value, 1); + ASSERT_FALSE(pool.empty()); + + ASSERT_EQ(pool.get(entities[0u]).value, 3); + ASSERT_EQ(pool.get(entities[1u]).value, 3); + + pool.erase(std::begin(entities), std::end(entities), ®istry); + + ASSERT_EQ(on_construct.value, 3); + ASSERT_EQ(on_destroy.value, 3); + ASSERT_TRUE(pool.empty()); +} diff --git a/test/entt/entity/storage.cpp b/test/entt/entity/storage.cpp index 76b619b1f..4a4d20742 100644 --- a/test/entt/entity/storage.cpp +++ b/test/entt/entity/storage.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include "throwing_allocator.hpp" #include "throwing_component.hpp" @@ -15,6 +14,12 @@ struct empty_type {}; struct boxed_int { int value; }; struct stable_type { int value; }; +struct non_default_constructible { + non_default_constructible() = delete; + non_default_constructible(int v): value{v} {} + int value; +}; + struct update_from_destructor { update_from_destructor(entt::storage &ref, entt::entity other) : storage{&ref}, @@ -542,6 +547,90 @@ TEST(Storage, StableRemove) { ASSERT_EQ(pool.get(entities[2u]).value, 1); } +TEST(Storage, TypeFromBase) { + entt::storage pool; + entt::sparse_set &base = pool; + entt::entity entities[2u]{entt::entity{3}, entt::entity{42}}; + + ASSERT_FALSE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + + base.emplace(entities[0u]); + + ASSERT_TRUE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + ASSERT_EQ(pool.get(entities[0u]), 0); + + base.erase(entities[0u]); + base.insert(std::begin(entities), std::end(entities)); + + ASSERT_TRUE(pool.contains(entities[0u])); + ASSERT_TRUE(pool.contains(entities[1u])); + ASSERT_EQ(pool.get(entities[1u]), 0); + + base.erase(std::begin(entities), std::end(entities)); + + ASSERT_TRUE(pool.empty()); +} + +TEST(Storage, EmptyTypeFromBase) { + entt::storage pool; + entt::sparse_set &base = pool; + entt::entity entities[2u]{entt::entity{3}, entt::entity{42}}; + + ASSERT_FALSE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + + base.emplace(entities[0u]); + + ASSERT_TRUE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + + base.erase(entities[0u]); + base.insert(std::begin(entities), std::end(entities)); + + ASSERT_TRUE(pool.contains(entities[0u])); + ASSERT_TRUE(pool.contains(entities[1u])); + + base.erase(std::begin(entities), std::end(entities)); + + ASSERT_TRUE(pool.empty()); +} + +TEST(Storage, NonDefaultConstructibleTypeFromBase) { + entt::storage pool; + entt::sparse_set &base = pool; + entt::entity entities[2u]{entt::entity{3}, entt::entity{42}}; + + ASSERT_FALSE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + + ASSERT_DEATH(base.emplace(entities[0u]), ""); + + ASSERT_FALSE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + ASSERT_EQ(base.find(entities[0u]), base.end()); + ASSERT_TRUE(pool.empty()); + + pool.emplace(entities[0u], 3); + + ASSERT_TRUE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + + base.erase(entities[0u]); + + ASSERT_TRUE(pool.empty()); + ASSERT_FALSE(pool.contains(entities[0u])); + + ASSERT_DEATH(base.insert(std::begin(entities), std::end(entities)), ""); + + ASSERT_FALSE(pool.contains(entities[0u])); + ASSERT_FALSE(pool.contains(entities[1u])); + ASSERT_EQ(base.find(entities[0u]), base.end()); + ASSERT_EQ(base.find(entities[1u]), base.end()); + ASSERT_TRUE(pool.empty()); +} + TEST(Storage, Compact) { entt::storage pool;