38#include <gtest/gtest.h>
98 EXPECT_EQ(map.size(), 0u);
99 EXPECT_TRUE(map.is_empty());
104 EXPECT_EQ(map.size(), 0u);
105 EXPECT_TRUE(map.is_empty());
108TEST(MapOpenHashConstruction, WithCustomSize)
111 EXPECT_EQ(map.size(), 0u);
114TEST(MapOpenHashConstruction, CopyConstruction)
121 EXPECT_EQ(
copy.size(), original.size());
122 EXPECT_TRUE(
copy.has(
"a"));
123 EXPECT_TRUE(
copy.has(
"b"));
124 EXPECT_EQ(
copy[
"a"], 1);
125 EXPECT_EQ(
copy[
"b"], 2);
128TEST(MapOpenHashConstruction, MoveConstruction)
133 size_t orig_size = original.size();
136 EXPECT_EQ(moved.size(), orig_size);
137 EXPECT_TRUE(moved.
has(
"x"));
138 EXPECT_TRUE(moved.
has(
"y"));
147 std::string key =
"test";
151 ASSERT_NE(
pair,
nullptr);
152 EXPECT_EQ(
pair->first,
"test");
153 EXPECT_EQ(
pair->second, 42);
154 EXPECT_EQ(map.size(), 1u);
159 std::string key =
"moved";
160 std::string
value =
"hello_world";
162 auto *
pair = int_map.insert(100, std::move(
value));
163 ASSERT_NE(
pair,
nullptr);
164 EXPECT_EQ(
pair->first, 100);
165 EXPECT_EQ(
pair->second,
"hello_world");
170 std::string key =
"key";
173 auto *
pair = map.insert(std::move(key), std::move(
value));
174 ASSERT_NE(
pair,
nullptr);
175 EXPECT_EQ(
pair->first,
"key");
176 EXPECT_EQ(
pair->second, 99);
181 std::string key =
"movekey";
184 auto *
pair = map.insert(std::move(key),
value);
185 ASSERT_NE(
pair,
nullptr);
186 EXPECT_EQ(
pair->first,
"movekey");
187 EXPECT_EQ(
pair->second, 77);
192 map.insert(
"dup", 1);
193 auto *
pair = map.insert(
"dup", 2);
194 EXPECT_EQ(
pair,
nullptr);
195 EXPECT_EQ(map[
"dup"], 1);
201 EXPECT_EQ(map.size(), 5u);
202 EXPECT_TRUE(map.has(
"one"));
203 EXPECT_TRUE(map.has(
"five"));
214 auto *
pair = map.search(
"three");
215 ASSERT_NE(
pair,
nullptr);
216 EXPECT_EQ(
pair->first,
"three");
217 EXPECT_EQ(
pair->second, 3);
224 auto *
pair = map.search(
"nonexistent");
225 EXPECT_EQ(
pair,
nullptr);
232 std::string key =
"two";
233 auto *
pair = map.search(std::move(key));
234 ASSERT_NE(
pair,
nullptr);
235 EXPECT_EQ(
pair->second, 2);
240 auto *
pair = map.search(
"anything");
241 EXPECT_EQ(
pair,
nullptr);
251 EXPECT_TRUE(map.has(
"one"));
252 EXPECT_TRUE(map.has(
"five"));
258 EXPECT_FALSE(map.has(
"nonexistent"));
259 EXPECT_FALSE(map.has(
""));
265 std::string key =
"three";
266 EXPECT_TRUE(map.has(std::move(key)));
272 EXPECT_EQ(map.contains(
"one"), map.has(
"one"));
273 EXPECT_EQ(map.contains(
"nonexistent"), map.has(
"nonexistent"));
279 std::string key =
"two";
280 EXPECT_TRUE(map.contains(std::move(key)));
291 int &
value = map.find(
"three");
299 map.find(
"two") = 22;
300 EXPECT_EQ(map.find(
"two"), 22);
306 EXPECT_THROW((
void)map.find(
"nonexistent"), std::domain_error);
312 std::string key =
"four";
313 EXPECT_EQ(map.find(std::move(key)), 4);
319 const auto & const_map = map;
321 const int &
value = const_map.find(
"one");
328 const auto & const_map = map;
330 EXPECT_THROW((
void)const_map.find(
"missing"), std::domain_error);
340 EXPECT_EQ(map[
"one"], 1);
341 EXPECT_EQ(map[
"five"], 5);
346 map[
"new_key"] = 100;
347 EXPECT_TRUE(map.has(
"new_key"));
348 EXPECT_EQ(map[
"new_key"], 100);
353 int &
value = map[
"defaulted"];
356 EXPECT_EQ(map[
"defaulted"], 42);
361 std::string key =
"moved_key";
362 map[std::move(key)] = 55;
363 EXPECT_EQ(map[
"moved_key"], 55);
369 const auto & const_map = map;
371 EXPECT_EQ(const_map[
"two"], 2);
377 const auto & const_map = map;
379 EXPECT_THROW((
void)const_map[
"missing"], std::domain_error);
389 size_t original_size = map.size();
393 EXPECT_EQ(map.size(), original_size - 1);
394 EXPECT_FALSE(map.has(
"three"));
400 EXPECT_THROW(map.remove(
"nonexistent"), std::domain_error);
406 std::string key =
"two";
408 map.remove(std::move(key));
409 EXPECT_FALSE(map.has(
"two"));
422 EXPECT_EQ(map.size(), 0u);
423 EXPECT_TRUE(map.is_empty());
429 auto *
pair = map.search(
"three");
430 ASSERT_NE(
pair,
nullptr);
432 map.remove_by_data(
pair->second);
433 EXPECT_FALSE(map.has(
"three"));
444 auto key_list = map.keys();
445 EXPECT_EQ(key_list.size(), 5u);
448 bool has_one =
false, has_two =
false, has_three =
false;
449 bool has_four =
false, has_five =
false;
451 key_list.traverse([&](
const std::string &
k) {
452 if (
k ==
"one") has_one =
true;
453 else if (
k ==
"two") has_two =
true;
454 else if (
k ==
"three") has_three =
true;
455 else if (
k ==
"four") has_four =
true;
456 else if (
k ==
"five") has_five =
true;
460 EXPECT_TRUE(has_one);
461 EXPECT_TRUE(has_two);
462 EXPECT_TRUE(has_three);
463 EXPECT_TRUE(has_four);
464 EXPECT_TRUE(has_five);
471 auto value_list = map.values();
472 EXPECT_EQ(value_list.size(), 5u);
476 value_list.traverse([&
sum](
int v) {
487 auto ptr_list = map.values_ptr();
488 EXPECT_EQ(ptr_list.size(), 5u);
491 ptr_list.traverse([](
int * p) {
497 EXPECT_EQ(map[
"one"], 10);
498 EXPECT_EQ(map[
"two"], 20);
505 auto ptr_list = map.items_ptr();
506 EXPECT_EQ(ptr_list.size(), 5u);
509 ptr_list.traverse([](
auto *
pair) {
510 EXPECT_FALSE(
pair->first.empty());
517 auto key_list = map.keys();
518 EXPECT_EQ(key_list.size(), 0u);
523 auto value_list = map.values();
524 EXPECT_EQ(value_list.size(), 0u);
534 auto *
pair = map.search(
"one");
535 ASSERT_NE(
pair,
nullptr);
538 EXPECT_EQ(recovered_pair,
pair);
544 auto *
pair = map.search(
"two");
545 ASSERT_NE(
pair,
nullptr);
548 EXPECT_EQ(recovered_pair,
pair);
549 EXPECT_EQ(recovered_pair->first,
"two");
555 auto *
pair = map.search(
"three");
556 ASSERT_NE(
pair,
nullptr);
563 EXPECT_EQ(map[
"three"], 333);
569 auto *
pair = map.search(
"four");
570 ASSERT_NE(
pair,
nullptr);
573 EXPECT_EQ(key,
"four");
579 const auto & const_map = map;
580 const auto *
pair = const_map.search(
"one");
581 ASSERT_NE(
pair,
nullptr);
584 EXPECT_EQ(recovered_pair,
pair);
590 const auto & const_map = map;
591 const auto *
pair = const_map.search(
"two");
592 ASSERT_NE(
pair,
nullptr);
595 EXPECT_EQ(recovered_pair,
pair);
601 const auto & const_map = map;
602 const auto *
pair = const_map.search(
"three");
603 ASSERT_NE(
pair,
nullptr);
612 const auto & const_map = map;
613 const auto *
pair = const_map.search(
"four");
614 ASSERT_NE(
pair,
nullptr);
617 EXPECT_EQ(key,
"four");
631 auto &
pair = it.get_curr();
632 EXPECT_FALSE(
pair.first.empty());
636 EXPECT_EQ(
count, 5u);
642 EXPECT_FALSE(it.has_curr());
650 map.traverse([&
sum](
const auto &
pair) {
663 bool result = map.traverse([&
count](
const auto &) {
668 EXPECT_FALSE(result);
680 EXPECT_TRUE(map.has(
"alpha"));
681 EXPECT_TRUE(map.has(
"beta"));
682 EXPECT_TRUE(map.has(
"gamma"));
683 EXPECT_EQ(map.size(), 3u);
688 map.insert(
"test", 42);
689 auto *
pair = map.search(
"test");
690 ASSERT_NE(
pair,
nullptr);
691 EXPECT_EQ(
pair->second, 42);
699 EXPECT_FALSE(map.has(
"beta"));
700 EXPECT_TRUE(map.has(
"alpha"));
701 EXPECT_TRUE(map.has(
"gamma"));
708TEST(MapOpenHashScaling, InsertManyElements)
712 constexpr int N = 10000;
713 for (
int i = 0; i <
N; ++i)
716 EXPECT_EQ(map.size(),
static_cast<size_t>(
N));
719 for (
int i = 0; i <
N; ++i)
721 ASSERT_TRUE(map.
has(i)) <<
"Missing key: " << i;
722 EXPECT_EQ(map[i], i * 2);
726TEST(MapOpenHashScaling, RemoveManyElements)
730 constexpr int N = 1000;
731 for (
int i = 0; i <
N; ++i)
735 for (
int i = 0; i <
N; i += 2)
738 EXPECT_EQ(map.size(),
static_cast<size_t>(
N / 2));
741 for (
int i = 0; i <
N; ++i)
744 EXPECT_FALSE(map.
has(i));
746 EXPECT_TRUE(map.
has(i));
750TEST(MapOpenHashScaling, InsertRemoveInterleaved)
754 for (
int i = 0; i < 500; ++i)
762 EXPECT_EQ(map.size(), 101u);
763 EXPECT_TRUE(map.
has(0));
764 for (
int i = 400; i < 500; ++i)
765 EXPECT_TRUE(map.
has(i));
775 EXPECT_TRUE(map.has(
""));
776 EXPECT_EQ(map[
""], 0);
781 map.insert(
"neg", -100);
782 EXPECT_EQ(map[
"neg"], -100);
785TEST(MapOpenHashEdgeCases, ZeroKey)
789 EXPECT_TRUE(map.
has(0));
790 EXPECT_EQ(map[0],
"zero");
793TEST(MapOpenHashEdgeCases, NegativeKey)
799 EXPECT_TRUE(map.
has(-5));
800 EXPECT_TRUE(map.
has(-100));
801 EXPECT_EQ(map[-5], 50);
802 EXPECT_EQ(map[-100], 1000);
809TEST(MapOpenHashAssignment, CopyAssignment)
820 EXPECT_EQ(
copy.size(), 2u);
821 EXPECT_TRUE(
copy.has(
"a"));
822 EXPECT_TRUE(
copy.has(
"b"));
823 EXPECT_FALSE(
copy.has(
"x"));
826TEST(MapOpenHashAssignment, MoveAssignment)
833 target = std::move(original);
835 EXPECT_EQ(target.size(), 2u);
836 EXPECT_TRUE(target.
has(
"m"));
837 EXPECT_TRUE(target.
has(
"n"));
840TEST(MapOpenHashAssignment, SelfAssignment)
847 EXPECT_EQ(map.size(), 1u);
848 EXPECT_EQ(map[
"self"], 42);
858 static_assert(std::is_same_v<MapODhash<std::string, int>::Key_Type, std::string>);
859 static_assert(std::is_same_v<MapODhash<std::string, int>::Data_Type,
int>);
860 static_assert(std::is_same_v<MapODhash<std::string, int>::Value_Type,
int>);
861 static_assert(std::is_same_v<MapODhash<std::string, int>::Item_Type,
862 std::pair<std::string, int>>);
869TEST(MapVariantComparison, BothVariantsProduceSameResults)
875 for (
int i = 0; i < 100; ++i)
877 std::string val = std::to_string(i * 10);
882 EXPECT_EQ(od_map.size(), ol_map.size());
885 for (
int i = 0; i < 100; ++i)
887 EXPECT_TRUE(od_map.
has(i));
888 EXPECT_TRUE(ol_map.
has(i));
889 EXPECT_EQ(od_map[i], ol_map[i]);
901 auto filtered = map.filter([](
const auto &
pair) {
902 return pair.second > 2;
905 EXPECT_EQ(filtered.size(), 3u);
912 auto doubled = map.template maps<int>([](
const auto &
pair) {
913 return pair.second * 2;
917 doubled.traverse([&
sum](
int v) {
929 int sum = map.template foldl<int>(0, [](
int acc,
const auto &
pair) {
930 return acc +
pair.second;
940 bool all_positive = map.all([](
const auto &
pair) {
941 return pair.second > 0;
944 EXPECT_TRUE(all_positive);
948 all_positive = map.all([](
const auto &
pair) {
949 return pair.second > 0;
952 EXPECT_FALSE(all_positive);
959 bool has_three = map.exists([](
const auto &
pair) {
960 return pair.second == 3;
963 EXPECT_TRUE(has_three);
965 bool has_ten = map.exists([](
const auto &
pair) {
966 return pair.second == 10;
969 EXPECT_FALSE(has_ten);
978 size_t a_key_hash(
const long &
k)
noexcept
980 return static_cast<size_t>(
k) * 40503u + 7;
989TEST(MapHashOf, GivesAPairTheHashOfItsKey)
991 const auto pair_hash = hash_fct_detail::map_hash_of<long, std::string>(a_key_hash);
992 static_assert(
sizeof(pair_hash) ==
sizeof(
void *));
993 static_assert(std::is_trivially_copyable_v<
decltype(pair_hash)>);
994 static_assert(std::is_nothrow_copy_constructible_v<
decltype(pair_hash)>);
996 for (
long k = -5;
k < 100; ++
k)
997 EXPECT_EQ(pair_hash(std::pair<long, std::string>(
k,
"v")), a_key_hash(
k));
1001TEST(MapHashOf, AMapFindsItsPairsWithAHashFunctionOfItsOwn)
1005 for (
long k = 0;
k < 500; ++
k)
1010 for (
long k = 0;
k < 500; ++
k)
1012 ASSERT_NE(od.
search(
k),
nullptr) <<
k;
1013 EXPECT_EQ(od.
search(
k)->second, std::to_string(
k));
1014 ASSERT_NE(ol.
search(
k),
nullptr) <<
k;
1015 EXPECT_EQ(ol.
search(
k)->second, std::to_string(
k));
1017 EXPECT_EQ(od.
search(500),
nullptr);
1018 EXPECT_EQ(ol.
search(500),
nullptr);
1027 ::testing::InitGoogleTest(&argc, argv);
1028 return RUN_ALL_TESTS();
size_t size_t int32_t value
Number Theoretic Transform over Z / MOD Z.
MapODhash< std::string, int > map
MapODhash< int, std::string > int_map
MapOLhash< std::string, int > map
TEST_F(MapODhashTest, DefaultConstruction)
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Open addressing hash map using double hashing.
Open addressing hash map using linear probing.
void remove(const Key &key)
Remove an entry by key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Search for a key in the map.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
bool has(const Key &key) const noexcept(nothrow_search< const Key & >)
Check if a key exists in the map.
Dynamic map with open hashing.