From 24d1f1e5bff8bf3e3454c05024dd5c141e61978f Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 24 Aug 2023 18:50:48 +0300 Subject: [PATCH] Add more hash_table tests --- libs/util/tests/hash_table.cpp | 219 +++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) diff --git a/libs/util/tests/hash_table.cpp b/libs/util/tests/hash_table.cpp index 110847f2..48c539e3 100644 --- a/libs/util/tests/hash_table.cpp +++ b/libs/util/tests/hash_table.cpp @@ -10,6 +10,112 @@ using namespace psemek; using namespace psemek::util; +test_case(util_hash__set_empty) +{ + hash_set set; + expect(set.size() == 0); + expect(set.empty()); + + int call_count = 0; + for ([[maybe_unused]] auto value : set) + ++call_count; + expect_equal(call_count, 0); +} + +test_case(util_hash__set_insert) +{ + hash_set set; + + int const count = 1024 * 16; + for (int i = 0; i < count; ++i) + expect(set.insert(i * i).second); + + expect_equal(set.size(), count); + + for (int i = 0; i < count; ++i) + { + expect(set.contains(i * i)); + auto it = set.find(i * i); + expect(it != set.end()); + expect_equal(*it, i * i); + } + + for (int i = count; i < 2 * count; ++i) + { + expect(!set.contains(i * i)); + expect(set.find(i * i) == set.end()); + } +} + +test_case(util_hash__set_clear) +{ + hash_set set; + + int const count = 1024 * 1024; + for (int i = 0; i < count; ++i) + expect(set.insert(i).second); + + expect_equal(set.size(), count); + + set.clear(); + + expect(set.empty()); + expect_equal(set.size(), 0); + + int call_count = 0; + for ([[maybe_unused]] auto value : set) + ++call_count; + expect_equal(call_count, 0); +} + +test_case(util_hash__set_move) +{ + hash_set set1; + + int const count = 1024 * 1024; + for (int i = 0; i < count; ++i) + expect(set1.insert(i).second); + + expect_equal(set1.size(), count); + + hash_set set2 = std::move(set1); + + expect(set1.empty()); + expect_equal(set1.size(), 0); + + expect(!set2.empty()); + expect_equal(set2.size(), count); + for (int i = 0; i < count; ++i) + { + expect(!set1.contains(i)); + expect(set2.contains(i)); + } + for (int i = count; i < 2 * count; ++i) + { + expect(!set1.contains(i)); + expect(!set2.contains(i)); + } + + hash_set set3; + set3 = std::move(set2); + + expect(set2.empty()); + expect_equal(set2.size(), 0); + + expect(!set3.empty()); + expect_equal(set3.size(), count); + for (int i = 0; i < count; ++i) + { + expect(!set2.contains(i)); + expect(set3.contains(i)); + } + for (int i = count; i < 2 * count; ++i) + { + expect(!set2.contains(i)); + expect(!set3.contains(i)); + } +} + test_case(util_hash__set_benchmark) { random::generator rng; @@ -96,6 +202,119 @@ test_case(util_hash__set_benchmark) } } +test_case(util_hash__map_empty) +{ + hash_map map; + expect(map.size() == 0); + expect(map.empty()); + + int call_count = 0; + for ([[maybe_unused]] auto const & pair : map) + ++call_count; + expect_equal(call_count, 0); +} + +test_case(util_hash__map_insert) +{ + hash_map map; + + int const count = 1024 * 16; + for (int i = 0; i < count; ++i) + expect(map.insert({i * i, i}).second); + + expect_equal(map.size(), count); + + for (int i = 0; i < count; ++i) + { + expect(map.contains(i * i)); + auto it = map.find(i * i); + expect(it != map.end()); + expect_equal(it->first, i * i); + expect_equal(it->second, i); + } + + for (int i = count; i < 2 * count; ++i) + { + expect(!map.contains(i * i)); + expect(map.find(i * i) == map.end()); + } +} + +test_case(util_hash__map_clear) +{ + hash_map map; + + int const count = 1024 * 1024; + for (int i = 0; i < count; ++i) + expect(map.insert({i, (i * 19) % count}).second); + + expect_equal(map.size(), count); + + map.clear(); + + expect(map.empty()); + expect_equal(map.size(), 0); + + int call_count = 0; + for ([[maybe_unused]] auto value : map) + ++call_count; + expect_equal(call_count, 0); +} + +test_case(util_hash__map_move) +{ + hash_map map1; + + int const count = 1024 * 1024; + for (int i = 0; i < count; ++i) + expect(map1.insert({i, (i * 19) % count}).second); + + expect_equal(map1.size(), count); + + hash_map map2 = std::move(map1); + + expect(map1.empty()); + expect_equal(map1.size(), 0); + + expect(!map2.empty()); + expect_equal(map2.size(), count); + for (int i = 0; i < count; ++i) + { + expect(!map1.contains(i)); + auto it = map2.find(i); + expect(it != map2.end()); + expect_equal(it->first, i); + expect_equal(it->second, (i * 19) % count); + } + for (int i = count; i < 2 * count; ++i) + { + expect(!map1.contains(i)); + expect(!map2.contains(i)); + } + + hash_map map3; + map3 = std::move(map2); + + expect(map2.empty()); + expect_equal(map2.size(), 0); + + expect(!map3.empty()); + expect_equal(map3.size(), count); + for (int i = 0; i < count; ++i) + { + expect(!map2.contains(i)); + auto it = map3.find(i); + expect(it != map3.end()); + expect_equal(it->first, i); + expect_equal(it->second, (i * 19) % count); + } + for (int i = count; i < 2 * count; ++i) + { + expect(!map2.contains(i)); + expect(!map3.contains(i)); + } +} + test_case(util_hash__map_benchmark) { random::generator rng;