Browse Source

test: code coverage for the storage class for empty types

Michele Caini 4 years ago
parent
commit
7b6715c7b5
2 changed files with 59 additions and 37 deletions
  1. 8 0
      src/entt/entity/storage.hpp
  2. 51 37
      test/entt/entity/storage.cpp

+ 8 - 0
src/entt/entity/storage.hpp

@@ -759,6 +759,14 @@ public:
      */
     basic_storage(basic_storage &&other) ENTT_NOEXCEPT = default;
 
+    /**
+     * @brief Allocator-extended move constructor.
+     * @param other The instance to move from.
+     * @param allocator The allocator to use.
+     */
+    basic_storage(basic_storage &&other, const allocator_type &allocator) ENTT_NOEXCEPT
+        : base_type{std::move(other), allocator} {}
+
     /**
      * @brief Move assignment operator.
      * @param other The instance to move from.

+ 51 - 37
test/entt/entity/storage.cpp

@@ -10,7 +10,7 @@
 #include "throwing_allocator.hpp"
 #include "throwing_component.hpp"
 
-struct empty_type {};
+struct empty_stable_type {};
 
 struct boxed_int {
     int value;
@@ -61,7 +61,7 @@ struct entt::component_traits<stable_type>: basic_component_traits {
 };
 
 template<>
-struct entt::component_traits<empty_type>: basic_component_traits {
+struct entt::component_traits<empty_stable_type>: basic_component_traits {
     using in_place_delete = std::true_type;
 };
 
@@ -220,12 +220,22 @@ TEST(Storage, StableSwap) {
 }
 
 TEST(Storage, EmptyType) {
-    entt::storage<empty_type> pool;
+    entt::storage<empty_stable_type> pool;
     pool.emplace(entt::entity{99});
 
     ASSERT_NO_THROW([[maybe_unused]] auto alloc = pool.get_allocator());
     ASSERT_TRUE(pool.contains(entt::entity{99}));
     ASSERT_DEATH(pool.get(entt::entity{}), "");
+
+    entt::storage<empty_stable_type> other{std::move(pool)};
+
+    ASSERT_FALSE(pool.contains(entt::entity{99}));
+    ASSERT_TRUE(other.contains(entt::entity{99}));
+
+    pool = std::move(other);
+
+    ASSERT_TRUE(pool.contains(entt::entity{99}));
+    ASSERT_FALSE(other.contains(entt::entity{99}));
 }
 
 TEST(Storage, Insert) {
@@ -255,7 +265,7 @@ TEST(Storage, Insert) {
 }
 
 TEST(Storage, InsertEmptyType) {
-    entt::storage<empty_type> pool;
+    entt::storage<empty_stable_type> pool;
     entt::entity entities[2u]{entt::entity{3}, entt::entity{42}};
 
     pool.insert(std::begin(entities), std::end(entities));
@@ -267,7 +277,7 @@ TEST(Storage, InsertEmptyType) {
     ASSERT_EQ(pool.size(), 2u);
 
     pool.erase(std::begin(entities), std::end(entities));
-    const empty_type values[2u]{};
+    const empty_stable_type values[2u]{};
     pool.insert(std::rbegin(entities), std::rend(entities), std::begin(values));
 
     ASSERT_EQ(pool.size(), 2u);
@@ -586,7 +596,7 @@ TEST(Storage, TypeFromBase) {
 }
 
 TEST(Storage, EmptyTypeFromBase) {
-    entt::storage<empty_type> pool;
+    entt::storage<empty_stable_type> pool;
     entt::sparse_set &base = pool;
     entt::entity entities[2u]{entt::entity{3}, entt::entity{42}};
 
@@ -1259,51 +1269,55 @@ TEST(Storage, UpdateFromDestructor) {
 }
 
 TEST(Storage, CustomAllocator) {
-    test::throwing_allocator<entt::entity> allocator{};
-    entt::basic_storage<entt::entity, int, test::throwing_allocator<entt::entity>> pool{allocator};
+    auto test = [](auto pool, auto alloc) {
+        ASSERT_EQ(pool.get_allocator(), alloc);
 
-    ASSERT_EQ(pool.get_allocator(), allocator);
+        pool.reserve(1u);
 
-    pool.reserve(1u);
+        ASSERT_NE(pool.capacity(), 0u);
 
-    ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE);
+        pool.emplace(entt::entity{0});
+        pool.emplace(entt::entity{1});
 
-    pool.emplace(entt::entity{0}, 3);
-    pool.emplace(entt::entity{1}, 42);
+        decltype(pool) other{std::move(pool), alloc};
 
-    entt::basic_storage<entt::entity, int, test::throwing_allocator<entt::entity>> other{std::move(pool), allocator};
+        ASSERT_TRUE(pool.empty());
+        ASSERT_FALSE(other.empty());
+        ASSERT_EQ(pool.capacity(), 0u);
+        ASSERT_NE(other.capacity(), 0u);
+        ASSERT_EQ(other.size(), 2u);
 
-    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);
 
-    pool = std::move(other);
+        ASSERT_FALSE(pool.empty());
+        ASSERT_TRUE(other.empty());
+        ASSERT_EQ(other.capacity(), 0u);
+        ASSERT_NE(pool.capacity(), 0u);
+        ASSERT_EQ(pool.size(), 2u);
 
-    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);
 
-    pool.swap(other);
-    pool = std::move(other);
+        ASSERT_FALSE(pool.empty());
+        ASSERT_TRUE(other.empty());
+        ASSERT_EQ(other.capacity(), 0u);
+        ASSERT_NE(pool.capacity(), 0u);
+        ASSERT_EQ(pool.size(), 2u);
 
-    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();
 
-    pool.clear();
+        ASSERT_NE(pool.capacity(), 0u);
+        ASSERT_EQ(pool.size(), 0u);
 
-    ASSERT_EQ(pool.capacity(), ENTT_PACKED_PAGE);
-    ASSERT_EQ(pool.size(), 0u);
+        pool.shrink_to_fit();
 
-    pool.shrink_to_fit();
+        ASSERT_EQ(pool.capacity(), 0u);
+    };
 
-    ASSERT_EQ(pool.capacity(), 0u);
+    test::throwing_allocator<entt::entity> allocator{};
+
+    test(entt::basic_storage<entt::entity, int, test::throwing_allocator<entt::entity>>{allocator}, allocator);
+    test(entt::basic_storage<entt::entity, std::true_type, test::throwing_allocator<entt::entity>>{allocator}, allocator);
 }
 
 TEST(Storage, ThrowingAllocator) {