From 4f829b91320edcf8ee9ace3b0836a1f2ba479447 Mon Sep 17 00:00:00 2001 From: andreyasu Date: Thu, 22 Aug 2019 20:31:41 +0300 Subject: [PATCH] Fix bug when copying an empy map with the copy constructor (#9) --- include/tsl/sparse_hash.h | 2 +- tests/sparse_map_tests.cpp | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/include/tsl/sparse_hash.h b/include/tsl/sparse_hash.h index d7c9793..b96f61c 100644 --- a/include/tsl/sparse_hash.h +++ b/include/tsl/sparse_hash.h @@ -1154,7 +1154,7 @@ class sparse_hash: private Allocator, private Hash, private KeyEqual, private Gr m_max_load_factor(other.m_max_load_factor) { copy_buckets_from(other), - m_sparse_buckets = m_sparse_buckets_data.data(); + m_sparse_buckets = m_sparse_buckets_data.empty() ? static_empty_sparse_bucket_ptr() : m_sparse_buckets_data.data(); } sparse_hash(sparse_hash&& other) noexcept(std::is_nothrow_move_constructible::value && diff --git a/tests/sparse_map_tests.cpp b/tests/sparse_map_tests.cpp index 1f5d78c..b7bcdbe 100644 --- a/tests/sparse_map_tests.cpp +++ b/tests/sparse_map_tests.cpp @@ -845,6 +845,28 @@ BOOST_AUTO_TEST_CASE(test_copy_constructor_and_operator) { BOOST_CHECK(map_copy == map_copy3); } +BOOST_AUTO_TEST_CASE(test_copy_constructor_empty) { + tsl::sparse_map map(0); + tsl::sparse_map map_copy(map); + + BOOST_CHECK(map.empty()); + BOOST_CHECK(map_copy.empty()); + + BOOST_CHECK(map.find("") == map.end()); + BOOST_CHECK(map_copy.find("") == map_copy.end()); +} + +BOOST_AUTO_TEST_CASE(test_copy_operator_empty) { + tsl::sparse_map map(0); + tsl::sparse_map map_copy(16); + map_copy = map; + + BOOST_CHECK(map.empty()); + BOOST_CHECK(map_copy.empty()); + + BOOST_CHECK(map.find("") == map.end()); + BOOST_CHECK(map_copy.find("") == map_copy.end()); +} /** * at