42# include <type_traits>
43# include <unordered_set>
45# include <gtest/gtest.h>
63 "HashSet<Key> default backend must be OLhashTable. "
64 "If this changed intentionally, document it as a breaking change.");
68 "HashMap<Key,Data> default backend must be MapOLhash. "
69 "If this changed intentionally, document it as a breaking change.");
73 "DynHashSet<Key> default backend must be OLhashTable.");
76static_assert(std::is_same_v<DynHashMap<int, int>,
78 "DynHashMap<Key,Data> default backend must be MapOLhash.");
87template <
typename Set>
88std::unordered_set<int> collect(Set &s)
90 std::unordered_set<int> result;
91 s.for_each([&](
const int k) { result.insert(
k); });
95template <
typename Map>
96std::unordered_set<int> collect_keys(Map &
m)
98 std::unordered_set<int> result;
99 m.
for_each([&](
const std::pair<int, std::string> &p) { result.insert(p.first); });
107struct HashSetOLTest : ::testing::Test
112TEST_F(HashSetOLTest, InsertAndContains)
114 for (
int i = 0; i <
N; ++i)
117 for (
int i = 0; i <
N; ++i)
118 EXPECT_TRUE(s.contains(i)) <<
"key " << i <<
" missing after insert";
121TEST_F(HashSetOLTest, ContainsReturnsFalseForAbsent)
123 for (
int i = 0; i <
N; ++i)
126 EXPECT_FALSE(s.contains(
N));
127 EXPECT_FALSE(s.contains(-1));
130TEST_F(HashSetOLTest, IterationCoversAllKeys)
132 for (
int i = 0; i <
N; ++i)
135 auto found = collect(s);
136 EXPECT_EQ(found.size(), (
size_t)
N);
137 for (
int i = 0; i <
N; ++i)
138 EXPECT_TRUE(found.count(i)) <<
"key " << i <<
" missing from iteration";
141TEST_F(HashSetOLTest, RemoveKey)
143 for (
int i = 0; i <
N; ++i)
147 EXPECT_FALSE(s.contains(42));
150 for (
int i = 0; i <
N; ++i)
153 EXPECT_TRUE(s.contains(i)) <<
"key " << i <<
" missing after remove(42)";
157TEST_F(HashSetOLTest, DuplicateInsertIsNoOp)
162 s.for_each([&](
const int k) {
if (
k == 7) ++
count; });
163 EXPECT_EQ(
count, 1u);
170struct HashSetODTest : ::testing::Test
175TEST_F(HashSetODTest, InsertAndContains)
177 for (
int i = 0; i <
N; ++i)
180 for (
int i = 0; i <
N; ++i)
181 EXPECT_TRUE(s.contains(i)) <<
"key " << i <<
" missing after insert";
184TEST_F(HashSetODTest, ContainsReturnsFalseForAbsent)
186 for (
int i = 0; i <
N; ++i)
189 EXPECT_FALSE(s.contains(
N));
190 EXPECT_FALSE(s.contains(-1));
193TEST_F(HashSetODTest, IterationCoversAllKeys)
195 for (
int i = 0; i <
N; ++i)
198 auto found = collect(s);
199 EXPECT_EQ(found.size(), (
size_t)
N);
200 for (
int i = 0; i <
N; ++i)
201 EXPECT_TRUE(found.count(i)) <<
"key " << i <<
" missing from iteration";
204TEST_F(HashSetODTest, RemoveKey)
206 for (
int i = 0; i <
N; ++i)
210 EXPECT_FALSE(s.contains(42));
212 for (
int i = 0; i <
N; ++i)
215 EXPECT_TRUE(s.contains(i)) <<
"key " << i <<
" missing after remove(42)";
223TEST(HashSetBackendsEquivalent, SameKeysAfterInsert)
228 for (
int i = 0; i <
N; ++i)
234 EXPECT_EQ(collect(ol), collect(od));
241struct HashMapOLTest : ::testing::Test
246TEST_F(HashMapOLTest, InsertAndSearch)
248 for (
int i = 0; i <
N; ++i)
249 m.
insert(i, std::to_string(i));
251 for (
int i = 0; i <
N; ++i)
254 ASSERT_NE(p,
nullptr) <<
"key " << i <<
" not found";
255 EXPECT_EQ(p->second, std::to_string(i));
259TEST_F(HashMapOLTest, SearchMissingKeyReturnsNull)
261 for (
int i = 0; i <
N; ++i)
262 m.
insert(i, std::to_string(i));
265 EXPECT_EQ(
m.
search(-1),
nullptr);
268TEST_F(HashMapOLTest, IterationCoversAllKeys)
270 for (
int i = 0; i <
N; ++i)
271 m.
insert(i, std::to_string(i));
273 auto keys = collect_keys(
m);
274 EXPECT_EQ(
keys.size(), (
size_t)
N);
275 for (
int i = 0; i <
N; ++i)
276 EXPECT_TRUE(
keys.count(i)) <<
"key " << i <<
" missing from iteration";
283struct HashMapODTest : ::testing::Test
288TEST_F(HashMapODTest, InsertAndSearch)
290 for (
int i = 0; i <
N; ++i)
291 m.
insert(i, std::to_string(i));
293 for (
int i = 0; i <
N; ++i)
296 ASSERT_NE(p,
nullptr) <<
"key " << i <<
" not found";
297 EXPECT_EQ(p->second, std::to_string(i));
301TEST_F(HashMapODTest, SearchMissingKeyReturnsNull)
303 for (
int i = 0; i <
N; ++i)
304 m.
insert(i, std::to_string(i));
307 EXPECT_EQ(
m.
search(-1),
nullptr);
310TEST_F(HashMapODTest, IterationCoversAllKeys)
312 for (
int i = 0; i <
N; ++i)
313 m.
insert(i, std::to_string(i));
315 auto keys = collect_keys(
m);
316 EXPECT_EQ(
keys.size(), (
size_t)
N);
317 for (
int i = 0; i <
N; ++i)
318 EXPECT_TRUE(
keys.count(i)) <<
"key " << i <<
" missing from iteration";
325TEST(HashMapBackendsEquivalent, SameEntriesAfterInsert)
330 for (
int i = 0; i <
N; ++i)
332 ol.insert(i, std::to_string(i));
333 od.insert(i, std::to_string(i));
336 EXPECT_EQ(collect_keys(ol), collect_keys(od));
338 for (
int i = 0; i <
N; ++i)
340 auto *pol = ol.search(i);
341 auto *pod = od.search(i);
342 ASSERT_NE(pol,
nullptr);
343 ASSERT_NE(pod,
nullptr);
344 EXPECT_EQ(pol->second, pod->second);
352TEST(DynHashSetTest, DefaultBackendWorks)
355 for (
int i = 0; i <
N; ++i)
358 auto found = collect(s);
359 EXPECT_EQ(found.size(), (
size_t)
N);
362TEST(DynHashMapTest, DefaultBackendWorks)
365 for (
int i = 0; i <
N; ++i)
366 m.
insert(i, std::to_string(i));
368 auto keys = collect_keys(
m);
369 EXPECT_EQ(
keys.size(), (
size_t)
N);
TEST_F(AdaptiveHuffmanWithoutMemory, ALostByteLeavesTheDecoderFailed)
Number Theoretic Transform over Z / MOD Z.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Main namespace for Aleph-w library functions.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Unified hash table interface.