Skip to content

Commit

Permalink
Fix bug when copying an empy map with the copy constructor (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackLem authored and Tessil committed Aug 22, 2019
1 parent 162cc7b commit 4f829b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/tsl/sparse_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Allocator>::value &&
Expand Down
22 changes: 22 additions & 0 deletions tests/sparse_map_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, int> map(0);
tsl::sparse_map<std::string, int> 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<std::string, int> map(0);
tsl::sparse_map<std::string, int> 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
Expand Down

0 comments on commit 4f829b9

Please sign in to comment.