Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug correction, set a moved hash map/set in a valid state so that it can still be used even after a move. #17

Merged
merged 2 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions tests/custom_allocator_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,19 @@ bool operator!=(const custom_allocator<T>&, const custom_allocator<U>&) {
BOOST_AUTO_TEST_SUITE(test_custom_allocator)

BOOST_AUTO_TEST_CASE(test_custom_allocator_1) {
// nb_global_new = 0;
nb_custom_allocs = 0;
tsl::ordered_map<int, int, std::hash<int>, std::equal_to<int>,
custom_allocator<std::pair<int, int>>> map;
const int nb_elements = 10000;
for(int i = 0; i < nb_elements; i++) {
map.insert({i, i*2});
}
BOOST_CHECK_NE(nb_custom_allocs, 0);
// BOOST_CHECK_EQUAL(nb_global_new, 0);
// nb_global_new = 0;
nb_custom_allocs = 0;

tsl::ordered_map<int, int, std::hash<int>, std::equal_to<int>,
custom_allocator<std::pair<int, int>>> map;

const int nb_elements = 10000;
for(int i = 0; i < nb_elements; i++) {
map.insert({i, i*2});
}

BOOST_CHECK_NE(nb_custom_allocs, 0);
// BOOST_CHECK_EQUAL(nb_global_new, 0);
}

BOOST_AUTO_TEST_SUITE_END()
106 changes: 103 additions & 3 deletions tests/ordered_map_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_insert, HMap, test_types) {
using key_tt = typename HMap::key_type; using value_tt = typename HMap:: mapped_type;

const std::size_t nb_values = 1000;
HMap map(0);
BOOST_CHECK_EQUAL(map.bucket_count(), 0);

HMap map;
typename HMap::iterator it;
bool inserted;

Expand Down Expand Up @@ -416,6 +417,26 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_erase_loop, HMap, test_types) {
BOOST_CHECK(map.empty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(test_erase_loop_range, HMap, test_types) {
// insert x values, delete all five by five
const std::size_t range = 5;
std::size_t nb_values = 1000;

BOOST_REQUIRE_EQUAL(nb_values % range, 0);

HMap map = utils::get_filled_hash_map<HMap>(nb_values);

auto it = map.begin();
while(it != map.end()) {
it = map.erase(it, std::next(it, range));
nb_values -= range;

BOOST_CHECK_EQUAL(map.size(), nb_values);
}

BOOST_CHECK(map.empty());
}

BOOST_AUTO_TEST_CASE_TEMPLATE(test_insert_erase_insert, HMap, test_types) {
// insert x/2 values, delete x/4 values, insert x/2 values, find each value, check order of values
using key_tt = typename HMap::key_type; using value_tt = typename HMap:: mapped_type;
Expand Down Expand Up @@ -466,6 +487,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_insert_erase_insert, HMap, test_types) {
else {
it = map.find(utils::get_key<key_tt>(i));

BOOST_REQUIRE(it != map.end());
BOOST_CHECK_EQUAL(it->first, utils::get_key<key_tt>(i));
BOOST_CHECK_EQUAL(it->second, utils::get_value<value_tt>(i));
}
Expand Down Expand Up @@ -529,6 +551,23 @@ BOOST_AUTO_TEST_CASE(test_unordered_erase) {
BOOST_CHECK_EQUAL(map.size(), 0);
}

/**
* rehash
*/
BOOST_AUTO_TEST_CASE(test_rehash_empty) {
const std::size_t nb_values = 100;
auto map = utils::get_filled_hash_map<tsl::ordered_map<std::int64_t, std::int64_t>>(nb_values);

const std::size_t bucket_count = map.bucket_count();
BOOST_CHECK(bucket_count >= nb_values);

map.clear();
BOOST_CHECK_EQUAL(map.bucket_count(), bucket_count);

map.rehash(0);
BOOST_CHECK_EQUAL(map.bucket_count(), 0);
}


/**
* operator== and operator!=
Expand Down Expand Up @@ -721,6 +760,15 @@ BOOST_AUTO_TEST_CASE(test_modify_value) {
}


/**
* constructor
*/
BOOST_AUTO_TEST_CASE(test_extreme_bucket_count_value_construction) {
BOOST_CHECK_THROW((tsl::ordered_map<int, int>(std::numeric_limits<std::size_t>::max())), std::length_error);
}



/**
* operator=(std::initializer_list)
*/
Expand Down Expand Up @@ -830,6 +878,51 @@ BOOST_AUTO_TEST_CASE(test_copy_constructor_operator) {
BOOST_CHECK(map_copy == map_copy3);
}

BOOST_AUTO_TEST_CASE(test_use_after_move_constructor) {
using HMap = tsl::ordered_map<std::string, move_only_test>;

const std::size_t nb_values = 100;
HMap map = utils::get_filled_hash_map<HMap>(nb_values);
HMap map_move(std::move(map));


BOOST_CHECK(map == (HMap()));
BOOST_CHECK_EQUAL(map.size(), 0);
BOOST_CHECK_EQUAL(map.bucket_count(), 0);
BOOST_CHECK_EQUAL(map.erase("a"), 0);
BOOST_CHECK(map.find("a") == map.end());

for(std::size_t i = 0; i < nb_values; i++) {
map.insert({utils::get_key<std::string>(i), utils::get_value<move_only_test>(i)});
}

BOOST_CHECK_EQUAL(map.size(), nb_values);
BOOST_CHECK(map == map_move);
}

BOOST_AUTO_TEST_CASE(test_use_after_move_operator) {
using HMap = tsl::ordered_map<std::string, move_only_test>;

const std::size_t nb_values = 100;
HMap map = utils::get_filled_hash_map<HMap>(nb_values);
HMap map_move(0);
map_move = std::move(map);


BOOST_CHECK(map == (HMap()));
BOOST_CHECK_EQUAL(map.size(), 0);
BOOST_CHECK_EQUAL(map.bucket_count(), 0);
BOOST_CHECK_EQUAL(map.erase("a"), 0);
BOOST_CHECK(map.find("a") == map.end());

for(std::size_t i = 0; i < nb_values; i++) {
map.insert({utils::get_key<std::string>(i), utils::get_value<move_only_test>(i)});
}

BOOST_CHECK_EQUAL(map.size(), nb_values);
BOOST_CHECK(map == map_move);
}


/**
* at
Expand Down Expand Up @@ -896,6 +989,12 @@ BOOST_AUTO_TEST_CASE(test_swap) {

BOOST_CHECK(map == (tsl::ordered_map<std::int64_t, std::int64_t>{{4, 40}, {5, 50}}));
BOOST_CHECK(map2 == (tsl::ordered_map<std::int64_t, std::int64_t>{{1, 10}, {8, 80}, {3, 30}}));

map.insert({6, 60});
map2.insert({4, 40});

BOOST_CHECK(map == (tsl::ordered_map<std::int64_t, std::int64_t>{{4, 40}, {5, 50}, {6, 60}}));
BOOST_CHECK(map2 == (tsl::ordered_map<std::int64_t, std::int64_t>{{1, 10}, {8, 80}, {3, 30}, {4, 40}}));
}

/**
Expand Down Expand Up @@ -1035,6 +1134,7 @@ BOOST_AUTO_TEST_CASE(test_heterogeneous_lookups) {
BOOST_AUTO_TEST_CASE(test_empty_map) {
tsl::ordered_map<std::string, int> map(0);

BOOST_CHECK_EQUAL(map.bucket_count(), 0);
BOOST_CHECK_EQUAL(map.size(), 0);
BOOST_CHECK(map.empty());

Expand Down Expand Up @@ -1064,8 +1164,8 @@ BOOST_AUTO_TEST_CASE(test_empty_map) {
* Test precalculated hash
*/
BOOST_AUTO_TEST_CASE(test_precalculated_hash) {
tsl::ordered_map<int, int, std::hash<int>> map = {{1, -1}, {2, -2}, {3, -3}, {4, -4}, {5, -5}, {6, -6}};
const tsl::ordered_map<int, int> map_const = map;
tsl::ordered_map<int, int, identity_hash<int>> map = {{1, -1}, {2, -2}, {3, -3}, {4, -4}, {5, -5}, {6, -6}};
const tsl::ordered_map<int, int, identity_hash<int>> map_const = map;

/**
* find
Expand Down
16 changes: 16 additions & 0 deletions tests/ordered_set_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_insert, HSet, test_types) {
}
}

BOOST_AUTO_TEST_CASE(test_compare) {
const tsl::ordered_set<std::string> map = {"D", "L", "A"};

BOOST_ASSERT(map == (tsl::ordered_set<std::string>{"D", "L", "A"}));
BOOST_ASSERT(map != (tsl::ordered_set<std::string>{"L", "D", "A"}));


BOOST_ASSERT(map < (tsl::ordered_set<std::string>{"D", "L", "B"}));
BOOST_ASSERT(map <= (tsl::ordered_set<std::string>{"D", "L", "B"}));
BOOST_ASSERT(map <= (tsl::ordered_set<std::string>{"D", "L", "A"}));

BOOST_ASSERT(map > (tsl::ordered_set<std::string>{"D", {"K", 2}, "A"}));
BOOST_ASSERT(map >= (tsl::ordered_set<std::string>{"D", {"K", 2}, "A"}));
BOOST_ASSERT(map >= (tsl::ordered_set<std::string>{"D", "L", "A"}));
}



BOOST_AUTO_TEST_SUITE_END()
8 changes: 8 additions & 0 deletions tests/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
#include <utility>


template<typename T>
class identity_hash {
public:
std::size_t operator()(const T& value) const {
return static_cast<std::size_t>(value);
}
};

template<unsigned int MOD>
class mod_hash {
public:
Expand Down
Loading