From 6d3e0600a1c50cc027981c98654b84fedf7579c2 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sat, 28 Aug 2021 09:52:19 +0200 Subject: [PATCH] storage: * increase code coverage * remove a pretty much useless if constexpr --- src/entt/entity/storage.hpp | 12 +-- test/entt/entity/storage.cpp | 202 ++++++++++++++++++----------------- 2 files changed, 109 insertions(+), 105 deletions(-) diff --git a/src/entt/entity/storage.hpp b/src/entt/entity/storage.hpp index 3888abeb5..91201a009 100644 --- a/src/entt/entity/storage.hpp +++ b/src/entt/entity/storage.hpp @@ -272,10 +272,8 @@ class basic_storage: public basic_sparse_set void consume_range(It first, It last, Generator generator) { - if constexpr(comp_traits::in_place_delete::value) { - for(const auto sz = base_type::size(); first != last && base_type::slot() != sz; ++first) { - emplace(*first, generator()); - } + for(const auto sz = base_type::size(); first != last && base_type::slot() != sz; ++first) { + emplace(*first, generator()); } const auto req = base_type::size() + std::distance(first, last); @@ -848,10 +846,8 @@ public: */ template void insert(It first, It last, Args &&...) { - if constexpr(comp_traits::in_place_delete::value) { - for(const auto sz = base_type::size(); first != last && base_type::slot() != sz; ++first) { - emplace(*first); - } + for(const auto sz = base_type::size(); first != last && base_type::slot() != sz; ++first) { + emplace(*first); } base_type::reserve(base_type::size() + std::distance(first, last)); diff --git a/test/entt/entity/storage.cpp b/test/entt/entity/storage.cpp index 4a4d20742..7d67beb0f 100644 --- a/test/entt/entity/storage.cpp +++ b/test/entt/entity/storage.cpp @@ -1224,6 +1224,111 @@ TEST(Storage, UpdateFromDestructor) { test(entt::entity{0u}); } +TEST(Storage, CustomAllocator) { + test::throwing_allocator allocator{}; + entt::basic_storage> pool{allocator}; + + ASSERT_EQ(pool.get_allocator(), allocator); + + pool.reserve(1u); + + ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE); + + pool.emplace(entt::entity{0}, 3); + pool.emplace(entt::entity{1}, 42); + + entt::basic_storage> other{std::move(pool), allocator}; + + ASSERT_TRUE(pool.empty()); + ASSERT_FALSE(other.empty()); + ASSERT_EQ(pool.capacity(), 0u); + ASSERT_EQ(other.capacity(), ENTT_PACKED_PAGE); + ASSERT_EQ(other.size(), 2u); + + pool = std::move(other); + + ASSERT_FALSE(pool.empty()); + ASSERT_TRUE(other.empty()); + ASSERT_EQ(other.capacity(), 0u); + ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE); + ASSERT_EQ(pool.size(), 2u); + + pool.swap(other); + pool = std::move(other); + + ASSERT_FALSE(pool.empty()); + ASSERT_TRUE(other.empty()); + ASSERT_EQ(other.capacity(), 0u); + ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE); + ASSERT_EQ(pool.size(), 2u); + + pool.clear(); + + ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE); + ASSERT_EQ(pool.size(), 0u); + + pool.shrink_to_fit(); + + ASSERT_EQ(pool.capacity(), 0u); +} + +TEST(Storage, ThrowingAllocator) { + entt::basic_storage> pool; + typename std::decay_t::base_type &base = pool; + + test::throwing_allocator::trigger_on_allocate = true; + + // strong exception safety + ASSERT_THROW(pool.reserve(1u), test::throwing_allocator::exception_type); + ASSERT_EQ(pool.capacity(), 0u); + + test::throwing_allocator::trigger_after_allocate = true; + + // strong exception safety + ASSERT_THROW(pool.reserve(2 * ENTT_PACKED_PAGE), test::throwing_allocator::exception_type); + ASSERT_EQ(pool.capacity(), 0u); + + pool.shrink_to_fit(); + test::throwing_allocator::trigger_on_allocate = true; + + // strong exception safety + ASSERT_THROW(pool.emplace(entt::entity{0}, 0), test::throwing_allocator::exception_type); + ASSERT_FALSE(pool.contains(entt::entity{0})); + ASSERT_TRUE(pool.empty()); + + test::throwing_allocator::trigger_on_allocate = true; + + // strong exception safety + ASSERT_THROW(pool.emplace(entt::entity{0}, 0), test::throwing_allocator::exception_type); + ASSERT_FALSE(pool.contains(entt::entity{0})); + ASSERT_TRUE(pool.empty()); + + test::throwing_allocator::trigger_on_allocate = true; + + // strong exception safety + ASSERT_THROW(base.emplace(entt::entity{0}), test::throwing_allocator::exception_type); + ASSERT_FALSE(base.contains(entt::entity{0})); + ASSERT_TRUE(base.empty()); + + pool.emplace(entt::entity{0}, 0); + const entt::entity entities[2u]{entt::entity{1}, entt::entity{ENTT_SPARSE_PAGE}}; + test::throwing_allocator::trigger_after_allocate = true; + + // basic exception safety + ASSERT_THROW(pool.insert(std::begin(entities), std::end(entities), 0), test::throwing_allocator::exception_type); + ASSERT_TRUE(pool.contains(entt::entity{1})); + ASSERT_FALSE(pool.contains(entt::entity{ENTT_SPARSE_PAGE})); + + pool.erase(entt::entity{1}); + const int components[2u]{1, ENTT_SPARSE_PAGE}; + test::throwing_allocator::trigger_on_allocate = true; + + // basic exception safety + ASSERT_THROW(pool.insert(std::begin(entities), std::end(entities), std::begin(components)), test::throwing_allocator::exception_type); + ASSERT_TRUE(pool.contains(entt::entity{1})); + ASSERT_FALSE(pool.contains(entt::entity{ENTT_SPARSE_PAGE})); +} + TEST(Storage, ThrowingComponent) { entt::storage pool; test::throwing_component::trigger_on_value = 42; @@ -1275,100 +1380,3 @@ TEST(Storage, ThrowingComponent) { ASSERT_EQ(pool.at(0u), entt::entity{42}); ASSERT_EQ(pool.get(entt::entity{42}), 42); } - -TEST(Storage, CustomAllocator) { - test::throwing_allocator allocator{}; - entt::basic_storage> pool{allocator}; - - ASSERT_EQ(pool.get_allocator(), allocator); - - pool.reserve(1u); - - ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE); - - pool.emplace(entt::entity{0}, 3); - pool.emplace(entt::entity{1}, 42); - - entt::basic_storage> other{std::move(pool), allocator}; - - ASSERT_TRUE(pool.empty()); - ASSERT_FALSE(other.empty()); - ASSERT_EQ(pool.capacity(), 0u); - ASSERT_EQ(other.capacity(), ENTT_PACKED_PAGE); - ASSERT_EQ(other.size(), 2u); - - pool = std::move(other); - - ASSERT_FALSE(pool.empty()); - ASSERT_TRUE(other.empty()); - ASSERT_EQ(other.capacity(), 0u); - ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE); - ASSERT_EQ(pool.size(), 2u); - - pool.swap(other); - pool = std::move(other); - - ASSERT_FALSE(pool.empty()); - ASSERT_TRUE(other.empty()); - ASSERT_EQ(other.capacity(), 0u); - ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE); - ASSERT_EQ(pool.size(), 2u); - - pool.clear(); - - ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE); - ASSERT_EQ(pool.size(), 0u); - - pool.shrink_to_fit(); - - ASSERT_EQ(pool.capacity(), 0u); -} - -TEST(Storage, ThrowingAllocator) { - entt::basic_storage> pool; - - test::throwing_allocator::trigger_on_allocate = true; - - // strong exception safety - ASSERT_THROW(pool.reserve(1u), test::throwing_allocator::exception_type); - ASSERT_EQ(pool.capacity(), 0u); - - test::throwing_allocator::trigger_after_allocate = true; - - // strong exception safety - ASSERT_THROW(pool.reserve(2 * ENTT_PACKED_PAGE), test::throwing_allocator::exception_type); - ASSERT_EQ(pool.capacity(), 0u); - - pool.shrink_to_fit(); - test::throwing_allocator::trigger_on_allocate = true; - - // strong exception safety - ASSERT_THROW(pool.emplace(entt::entity{0}, 0), test::throwing_allocator::exception_type); - ASSERT_FALSE(pool.contains(entt::entity{0})); - ASSERT_TRUE(pool.empty()); - - test::throwing_allocator::trigger_on_allocate = true; - - // strong exception safety - ASSERT_THROW(pool.emplace(entt::entity{0}, 0), test::throwing_allocator::exception_type); - ASSERT_FALSE(pool.contains(entt::entity{0})); - ASSERT_TRUE(pool.empty()); - - pool.emplace(entt::entity{0}, 0); - const entt::entity entities[2u]{entt::entity{1}, entt::entity{ENTT_SPARSE_PAGE}}; - test::throwing_allocator::trigger_after_allocate = true; - - // basic exception safety - ASSERT_THROW(pool.insert(std::begin(entities), std::end(entities), 0), test::throwing_allocator::exception_type); - ASSERT_TRUE(pool.contains(entt::entity{1})); - ASSERT_FALSE(pool.contains(entt::entity{ENTT_SPARSE_PAGE})); - - pool.erase(entt::entity{1}); - const int components[2u]{1, ENTT_SPARSE_PAGE}; - test::throwing_allocator::trigger_on_allocate = true; - - // basic exception safety - ASSERT_THROW(pool.insert(std::begin(entities), std::end(entities), std::begin(components)), test::throwing_allocator::exception_type); - ASSERT_TRUE(pool.contains(entt::entity{1})); - ASSERT_FALSE(pool.contains(entt::entity{ENTT_SPARSE_PAGE})); -}