Browse Source

storage: fix move ctor/op

Michele Caini 4 years ago
parent
commit
7d141cb183
3 changed files with 55 additions and 18 deletions
  1. 4 1
      src/entt/entity/storage.hpp
  2. 25 14
      test/entt/entity/sparse_set.cpp
  3. 26 3
      test/entt/entity/storage.cpp

+ 4 - 1
src/entt/entity/storage.hpp

@@ -302,7 +302,8 @@ public:
      * @param other The instance to move from.
      */
     basic_storage(basic_storage &&other) ENTT_NOEXCEPT
-        : allocator{std::move(other.allocator)},
+        : basic_sparse_set<entity_type, entity_alloc_type>{std::move(other)},
+          allocator{std::move(other.allocator)},
           bucket_allocator{std::move(other.bucket_allocator)},
           packed{std::exchange(other.packed, bucket_alloc_pointer{})},
           bucket{std::exchange(other.bucket, 0u)},
@@ -320,6 +321,8 @@ public:
      * @return This sparse set.
      */
     basic_storage & operator=(basic_storage &&other) ENTT_NOEXCEPT {
+        basic_sparse_set<entity_type, entity_alloc_type>::operator=(std::move(other));
+
         release_memory();
 
         allocator = std::move(other.allocator);

+ 25 - 14
test/entt/entity/sparse_set.cpp

@@ -61,34 +61,45 @@ TEST(SparseSet, Functionalities) {
     ASSERT_EQ(set.at(1u), static_cast<entt::entity>(entt::null));
     ASSERT_EQ(set[0u], entt::entity{42});
 
+    set.clear();
+
+    ASSERT_TRUE(set.empty());
+    ASSERT_EQ(set.size(), 0u);
+    ASSERT_EQ(std::as_const(set).begin(), std::as_const(set).end());
+    ASSERT_EQ(set.begin(), set.end());
+    ASSERT_FALSE(set.contains(entt::entity{0}));
+    ASSERT_FALSE(set.contains(entt::entity{42}));
+}
+
+TEST(SparseSet, Move) {
+    entt::sparse_set set;
+    set.emplace(entt::entity{42});
+
     ASSERT_TRUE(std::is_move_constructible_v<decltype(set)>);
     ASSERT_TRUE(std::is_move_assignable_v<decltype(set)>);
 
     entt::sparse_set other{std::move(set)};
 
+    ASSERT_TRUE(set.empty());
+    ASSERT_FALSE(other.empty());
+    ASSERT_EQ(set.at(0u), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(other.at(0u), entt::entity{42});
+
     set = std::move(other);
 
+    ASSERT_FALSE(set.empty());
+    ASSERT_TRUE(other.empty());
+    ASSERT_EQ(set.at(0u), entt::entity{42});
+    ASSERT_EQ(other.at(0u), static_cast<entt::entity>(entt::null));
+
     other = entt::sparse_set{};
     other.emplace(entt::entity{3});
     other = std::move(set);
 
     ASSERT_TRUE(set.empty());
-    ASSERT_EQ(set.at(0u), static_cast<entt::entity>(entt::null));
-
     ASSERT_FALSE(other.empty());
-    ASSERT_EQ(other.index(entt::entity{42}), 0u);
+    ASSERT_EQ(set.at(0u), static_cast<entt::entity>(entt::null));
     ASSERT_EQ(other.at(0u), entt::entity{42});
-    ASSERT_EQ(other.at(1u), static_cast<entt::entity>(entt::null));
-    ASSERT_EQ(other[0u], entt::entity{42});
-
-    other.clear();
-
-    ASSERT_TRUE(other.empty());
-    ASSERT_EQ(other.size(), 0u);
-    ASSERT_EQ(std::as_const(other).begin(), std::as_const(other).end());
-    ASSERT_EQ(other.begin(), other.end());
-    ASSERT_FALSE(other.contains(entt::entity{0}));
-    ASSERT_FALSE(other.contains(entt::entity{42}));
 }
 
 TEST(SparseSet, Pagination) {

+ 26 - 3
test/entt/entity/storage.cpp

@@ -90,17 +90,40 @@ TEST(Storage, Functionalities) {
     pool.shrink_to_fit();
 
     ASSERT_EQ(pool.capacity(), 0u);
+}
+
+TEST(Storage, Move) {
+    entt::storage<int> pool;
+    pool.emplace(entt::entity{3}, 3);
+
+    ASSERT_TRUE(std::is_move_constructible_v<decltype(pool)>);
+    ASSERT_TRUE(std::is_move_assignable_v<decltype(pool)>);
 
     entt::storage<int> other{std::move(pool)};
 
+    ASSERT_TRUE(pool.empty());
+    ASSERT_FALSE(other.empty());
+    ASSERT_EQ(pool.at(0u), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(other.at(0u), entt::entity{3});
+    ASSERT_EQ(other.get(entt::entity{3}), 3);
+
     pool = std::move(other);
 
+    ASSERT_FALSE(pool.empty());
+    ASSERT_TRUE(other.empty());
+    ASSERT_EQ(pool.at(0u), entt::entity{3});
+    ASSERT_EQ(pool.get(entt::entity{3}), 3);
+    ASSERT_EQ(other.at(0u), static_cast<entt::entity>(entt::null));
+
     other = entt::storage<int>{};
-    other.emplace(entt::entity{3}, 3);
+    other.emplace(entt::entity{42}, 42);
     other = std::move(pool);
 
-    ASSERT_EQ(pool.capacity(), 0u);
-    ASSERT_EQ(other.capacity(), 0u);
+    ASSERT_TRUE(pool.empty());
+    ASSERT_FALSE(other.empty());
+    ASSERT_EQ(pool.at(0u), static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(other.at(0u), entt::entity{3});
+    ASSERT_EQ(other.get(entt::entity{3}), 3);
 }
 
 TEST(Storage, EmptyType) {