From ee7e025680f0298dc2a92b13d4e53c057ab49f71 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 7 Nov 2023 08:58:23 +0100 Subject: [PATCH] test: increase coverage for handle class --- test/entt/entity/handle.cpp | 198 ++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 101 deletions(-) diff --git a/test/entt/entity/handle.cpp b/test/entt/entity/handle.cpp index d0c0a9753..2ec83d9e1 100644 --- a/test/entt/entity/handle.cpp +++ b/test/entt/entity/handle.cpp @@ -4,48 +4,54 @@ #include #include #include +#include #include #include #include -TEST(BasicHandle, Assumptions) { - ASSERT_TRUE(std::is_trivially_copyable_v); - ASSERT_TRUE((std::is_trivially_assignable_v)); - ASSERT_TRUE(std::is_trivially_destructible_v); +template +struct BasicHandle: testing::Test { + using type = Type; +}; - ASSERT_TRUE(std::is_trivially_copyable_v); - ASSERT_TRUE((std::is_trivially_assignable_v)); - ASSERT_TRUE(std::is_trivially_destructible_v); +using BasicHandleTypes = ::testing::Types; + +TYPED_TEST_SUITE(BasicHandle, BasicHandleTypes, ); + +TYPED_TEST(BasicHandle, Assumptions) { + using handle_type = typename TestFixture::type; + static_assert(std::is_trivially_copyable_v, "Trivially copyable type required"); + static_assert((std::is_trivially_assignable_v), "Trivially assignable type required"); + static_assert(std::is_trivially_destructible_v, "Trivially destructible type required"); } -TEST(BasicHandle, DeductionGuide) { - testing::StaticAssertTypeEq(), {}}), entt::basic_handle>(); - testing::StaticAssertTypeEq(), {}}), entt::basic_handle>(); +TYPED_TEST(BasicHandle, DeductionGuide) { + using handle_type = typename TestFixture::type; + testing::StaticAssertTypeEq(), {}}), handle_type>(); } -TEST(BasicHandle, Construction) { +TYPED_TEST(BasicHandle, Construction) { + using handle_type = typename TestFixture::type; + entt::registry registry; const auto entity = registry.create(); - entt::handle handle{registry, entity}; - entt::const_handle chandle{std::as_const(registry), entity}; + handle_type handle{registry, entity}; ASSERT_FALSE(entt::null == handle.entity()); ASSERT_EQ(entity, handle); ASSERT_TRUE(handle); - ASSERT_FALSE(entt::null == chandle.entity()); - ASSERT_EQ(entity, chandle); - ASSERT_TRUE(chandle); + ASSERT_EQ(handle, (entt::handle{registry, entity})); + ASSERT_EQ(handle, (entt::const_handle{registry, entity})); - ASSERT_EQ(handle, chandle); - - testing::StaticAssertTypeEq(); - testing::StaticAssertTypeEq(); + testing::StaticAssertTypeEq(); } -TEST(BasicHandle, Invalidation) { - entt::handle handle; +TYPED_TEST(BasicHandle, Invalidation) { + using handle_type = typename TestFixture::type; + + handle_type handle; ASSERT_FALSE(handle); ASSERT_EQ(handle.registry(), nullptr); @@ -103,51 +109,50 @@ TEST(BasicHandle, Destruction) { ASSERT_EQ(handle.entity(), entt::entity{entt::null}); } -TEST(BasicHandle, Comparison) { - entt::registry registry; - const auto entity = registry.create(); +TYPED_TEST(BasicHandle, Comparison) { + using handle_type = typename TestFixture::type; - entt::handle handle{registry, entity}; - entt::const_handle chandle = handle; - - ASSERT_NE(handle, entt::handle{}); - ASSERT_FALSE(handle == entt::handle{}); - ASSERT_TRUE(handle != entt::handle{}); - - ASSERT_NE(chandle, entt::const_handle{}); - ASSERT_FALSE(chandle == entt::const_handle{}); - ASSERT_TRUE(chandle != entt::const_handle{}); - - ASSERT_EQ(handle, chandle); - ASSERT_TRUE(handle == chandle); - ASSERT_FALSE(handle != chandle); - - ASSERT_EQ(entt::handle{}, entt::const_handle{}); - ASSERT_TRUE(entt::handle{} == entt::const_handle{}); - ASSERT_FALSE(entt::handle{} != entt::const_handle{}); - - handle = {}; - chandle = {}; + handle_type handle{}; ASSERT_EQ(handle, entt::handle{}); ASSERT_TRUE(handle == entt::handle{}); ASSERT_FALSE(handle != entt::handle{}); - ASSERT_EQ(chandle, entt::const_handle{}); - ASSERT_TRUE(chandle == entt::const_handle{}); - ASSERT_FALSE(chandle != entt::const_handle{}); + ASSERT_EQ(handle, entt::const_handle{}); + ASSERT_TRUE(handle == entt::const_handle{}); + ASSERT_FALSE(handle != entt::const_handle{}); - entt::registry other; - const auto entt = other.create(); + entt::registry registry; + const auto entity = registry.create(); + handle = handle_type{registry, entity}; + ASSERT_NE(handle, entt::handle{}); + ASSERT_FALSE(handle == entt::handle{}); + ASSERT_TRUE(handle != entt::handle{}); + + ASSERT_NE(handle, entt::const_handle{}); + ASSERT_FALSE(handle == entt::const_handle{}); + ASSERT_TRUE(handle != entt::const_handle{}); + + handle = {}; + + ASSERT_EQ(handle, entt::handle{}); + ASSERT_TRUE(handle == entt::handle{}); + ASSERT_FALSE(handle != entt::handle{}); + + ASSERT_EQ(handle, entt::const_handle{}); + ASSERT_TRUE(handle == entt::const_handle{}); + ASSERT_FALSE(handle != entt::const_handle{}); + + entt::registry diff; handle = {registry, entity}; - chandle = {other, entt}; + handle_type other = {diff, diff.create()}; - ASSERT_NE(handle, chandle); - ASSERT_FALSE(chandle == handle); - ASSERT_TRUE(chandle != handle); - ASSERT_EQ(handle.entity(), chandle.entity()); - ASSERT_NE(handle.registry(), chandle.registry()); + ASSERT_NE(handle, other); + ASSERT_FALSE(other == handle); + ASSERT_TRUE(other != handle); + ASSERT_EQ(handle.entity(), other.entity()); + ASSERT_NE(handle.registry(), other.registry()); } TEST(BasicHandle, Component) { @@ -194,20 +199,22 @@ TEST(BasicHandle, Component) { ASSERT_EQ(nullptr, std::get<1>(handle.try_get())); } -TEST(BasicHandle, FromEntity) { +TYPED_TEST(BasicHandle, FromEntity) { + using handle_type = typename TestFixture::type; + entt::registry registry; const auto entity = registry.create(); registry.emplace(entity, 42); registry.emplace(entity, 'c'); - entt::handle handle{registry, entity}; + handle_type handle{registry, entity}; ASSERT_TRUE(handle); ASSERT_EQ(entity, handle.entity()); - ASSERT_TRUE((handle.all_of())); - ASSERT_EQ(handle.get(), 42); - ASSERT_EQ(handle.get(), 'c'); + ASSERT_TRUE((handle.template all_of())); + ASSERT_EQ(handle.template get(), 42); + ASSERT_EQ(handle.template get(), 'c'); } TEST(BasicHandle, Lifetime) { @@ -232,45 +239,40 @@ TEST(BasicHandle, Lifetime) { TEST(BasicHandle, ImplicitConversions) { entt::registry registry; const entt::handle handle{registry, registry.create()}; - const entt::const_handle chandle = handle; - const entt::handle_view vhandle = handle; - const entt::const_handle_view cvhandle = vhandle; + const entt::const_handle const_handle = handle; + const entt::handle_view handle_view = handle; + const entt::const_handle_view const_handle_view = handle_view; handle.emplace(42); - ASSERT_EQ(handle.get(), chandle.get()); - ASSERT_EQ(chandle.get(), vhandle.get()); - ASSERT_EQ(vhandle.get(), cvhandle.get()); - ASSERT_EQ(cvhandle.get(), 42); + ASSERT_EQ(handle.get(), const_handle.get()); + ASSERT_EQ(const_handle.get(), handle_view.get()); + ASSERT_EQ(handle_view.get(), const_handle_view.get()); + ASSERT_EQ(const_handle_view.get(), 42); } -TEST(BasicHandle, Storage) { +TYPED_TEST(BasicHandle, Storage) { + using handle_type = typename TestFixture::type; + entt::registry registry; const auto entity = registry.create(); + handle_type handle{registry, entity}; - entt::handle handle{registry, entity}; - entt::const_handle chandle{std::as_const(registry), entity}; - - testing::StaticAssertTypeEq>(); - testing::StaticAssertTypeEq>(); + testing::StaticAssertTypeEq &>>(); ASSERT_EQ(handle.storage().begin(), handle.storage().end()); - ASSERT_EQ(chandle.storage().begin(), chandle.storage().end()); registry.storage(); registry.emplace(entity); ASSERT_NE(handle.storage().begin(), handle.storage().end()); - ASSERT_NE(chandle.storage().begin(), chandle.storage().end()); - ASSERT_EQ(++handle.storage().begin(), handle.storage().end()); - ASSERT_EQ(++chandle.storage().begin(), chandle.storage().end()); - ASSERT_EQ(handle.storage().begin()->second.type(), entt::type_id()); - ASSERT_EQ(chandle.storage().begin()->second.type(), entt::type_id()); } -TEST(BasicHandle, HandleStorageIterator) { +TYPED_TEST(BasicHandle, HandleStorageIterator) { + using handle_type = typename TestFixture::type; + entt::registry registry; const auto entity = registry.create(); @@ -279,27 +281,21 @@ TEST(BasicHandle, HandleStorageIterator) { // required to test the find-first initialization step registry.storage().erase(entity); - auto test = [](auto iterable) { - auto end{iterable.begin()}; - decltype(end) begin{}; - begin = iterable.end(); - std::swap(begin, end); - - ASSERT_EQ(begin, iterable.cbegin()); - ASSERT_EQ(end, iterable.cend()); - ASSERT_NE(begin, end); - - ASSERT_EQ(begin++, iterable.begin()); - ASSERT_EQ(++begin, iterable.end()); - }; - - const auto handle = entt::handle{registry, entity}; - const auto chandle = entt::const_handle{std::as_const(registry), entity}; + handle_type handle{registry, entity}; + auto iterable = handle.storage(); ASSERT_FALSE(registry.valid(entity)); ASSERT_FALSE(handle); - ASSERT_FALSE(chandle); - test(handle.storage()); - test(chandle.storage()); + auto end{iterable.begin()}; + decltype(end) begin{}; + begin = iterable.end(); + std::swap(begin, end); + + ASSERT_EQ(begin, iterable.cbegin()); + ASSERT_EQ(end, iterable.cend()); + ASSERT_NE(begin, end); + + ASSERT_EQ(begin++, iterable.begin()); + ASSERT_EQ(++begin, iterable.end()); }