45# include <gtest/gtest.h>
63 constexpr int Keys = 300;
67 size_t new_hash(
const int &
k)
noexcept
69 return static_cast<size_t>(
k) * 40503u + 17;
72 size_t new_second_hash(
const int &
k)
noexcept
74 return static_cast<size_t>(
k) * 3 + 11;
77 size_t new_pair_hash(
const std::pair<int, int> & p)
noexcept
79 return new_hash(p.first);
82 size_t new_second_pair_hash(
const std::pair<int, int> & p)
noexcept
84 return new_second_hash(p.first);
94 static constexpr bool refuses_keys =
false;
96 static bool holds(
Container & c,
int k) {
return c.search(
k) !=
nullptr; }
97 static void set(
Container & c) { c.set_hash_fct(new_hash); }
103 static constexpr bool refuses_keys =
true;
105 static bool holds(
Container & c,
int k) {
return c.search(
k) !=
nullptr; }
106 static void set(
Container & c) { c.set_hash_fct(new_hash); }
109 struct Linear_Probing_Table
112 static constexpr bool refuses_keys =
false;
114 static bool holds(
Container & c,
int k) {
return c.search(
k) !=
nullptr; }
115 static void set(
Container & c) { c.set_hash_fct(new_hash); }
118 struct Double_Hashing_Table
121 static constexpr bool refuses_keys =
false;
123 static bool holds(
Container & c,
int k) {
return c.search(
k) !=
nullptr; }
126 c.set_hash_fct(new_hash);
127 c.set_second_hash_fct(new_second_hash);
131 template <
class Set,
bool Linear>
135 static constexpr bool refuses_keys = Linear;
137 static bool holds(
Container & c,
int k) {
return c.search(
k) !=
nullptr; }
138 static void set(
Container & c) { c.set_hash_fct(new_hash); }
141 struct Chained_Records
144 static constexpr bool refuses_keys =
false;
148 const int *
r = c.search(
k);
149 return r !=
nullptr and *
r == 10 *
k;
151 static void set(
Container & c) { c.set_hash_fct(new_hash); }
155 template <
class Map,
bool Linear,
bool By_Pairs>
159 static constexpr bool refuses_keys = Linear;
163 const auto * p = c.search(
k);
164 return p !=
nullptr and p->second == 10 *
k and c.has(
k);
168 if constexpr (By_Pairs)
169 c.set_hash_fct(new_pair_hash);
171 c.set_hash_fct(new_hash);
176 template <
class Map,
bool Double>
180 static constexpr bool refuses_keys =
false;
184 const auto * p = c.search(
k);
185 return p !=
nullptr and p->second == 10 *
k;
189 c.set_hash_fct(new_pair_hash);
191 c.set_second_hash_fct(new_second_pair_hash);
195 using Hash_Containers = ::testing::Types<
196 Chained_Table, Linear_Table, Linear_Probing_Table, Double_Hashing_Table,
197 A_Set<DynSetLhash<int>,
false>, A_Set<DynSetHash<int>,
false>,
198 A_Set<DynSetLinHash<int>,
true>, Chained_Records,
199 A_Hash_Map<DynMapHash<int, int>,
false,
false>,
200 A_Hash_Map<DynMapHash<int, int>,
false,
true>,
201 A_Hash_Map<DynMapLinHash<int, int>,
true,
false>,
202 A_Hash_Map<DynMapLinHash<int, int>,
true,
true>,
203 An_Open_Map<MapOLhash<int, int>,
false>,
204 An_Open_Map<MapODhash<int, int>,
true>>;
207 class Hash_Function_Change :
public ::testing::Test
215 TYPED_TEST(Hash_Function_Change, OnAnEmptyContainer)
217 typename TypeParam::Container c;
219 for (
int k = 0;
k < Keys; ++
k)
220 TypeParam::insert(c,
k);
222 for (
int k = 0;
k < Keys; ++
k)
223 ASSERT_TRUE(TypeParam::holds(c,
k)) <<
"key " <<
k;
224 EXPECT_FALSE(TypeParam::holds(c, Keys));
232 TYPED_TEST(Hash_Function_Change, OnAContainerWithKeys)
234 typename TypeParam::Container c;
235 for (
int k = 0;
k < Keys; ++
k)
236 TypeParam::insert(c,
k);
238 if constexpr (TypeParam::refuses_keys)
239 EXPECT_THROW(TypeParam::set(c), std::domain_error);
243 for (
int k = 0;
k < Keys; ++
k)
244 ASSERT_TRUE(TypeParam::holds(c,
k)) <<
"key " <<
k <<
", before the container grows";
245 for (
int k = Keys;
k < 2 * Keys; ++
k)
246 TypeParam::insert(c,
k);
247 for (
int k = 0;
k < 2 * Keys; ++
k)
248 ASSERT_TRUE(TypeParam::holds(c,
k)) <<
"key " <<
k;
249 EXPECT_FALSE(TypeParam::holds(c, 2 * Keys));
261 class Map_Hash_Function :
public ::testing::Test
272 void expect_pairs(Map & map,
const int from,
const int to)
274 const Map & const_map = map;
275 for (
int k = from;
k < to; ++
k)
277 auto * p = map.search(
k);
278 ASSERT_NE(p,
nullptr) <<
"key " <<
k;
279 EXPECT_EQ(p->second, 10 *
k);
280 EXPECT_EQ(const_map.search(
k), p);
282 EXPECT_EQ(map.search(std::move(key)), p);
283 EXPECT_TRUE(map.has(
k));
284 EXPECT_TRUE(map.contains(
k));
285 EXPECT_EQ(map.find(
k), 10 *
k);
287 EXPECT_FALSE(map.has(from - 1));
288 EXPECT_FALSE(map.has(to));
291 TYPED_TEST(Map_Hash_Function, FunctionsOfPairsAndOfKeys)
293 TypeParam by_pairs, by_keys, back_to_keys;
294 by_pairs.set_hash_fct(new_pair_hash);
295 by_keys.set_hash_fct(new_hash);
296 back_to_keys.set_hash_fct(new_pair_hash);
297 back_to_keys.set_hash_fct(new_hash);
298 for (
int k = 0;
k < Keys; ++
k)
300 by_pairs.insert(
k, 10 *
k);
301 by_keys.insert(
k, 10 *
k);
302 back_to_keys.insert(
k, 10 *
k);
305 expect_pairs(by_pairs, 0, Keys);
306 expect_pairs(by_keys, 0, Keys);
307 expect_pairs(back_to_keys, 0, Keys);
309 EXPECT_EQ(by_pairs.remove(7), 70);
310 EXPECT_FALSE(by_pairs.has(7));
311 EXPECT_EQ(by_keys.remove(7), 70);
312 EXPECT_FALSE(by_keys.has(7));
313 EXPECT_EQ(by_pairs[8], 80);
314 by_pairs[Keys] = 10 * Keys;
315 EXPECT_EQ(by_pairs.find(Keys), 10 * Keys);
318 TYPED_TEST(Map_Hash_Function, CopiesMovesAndSwapsKeepTheFunction)
320 TypeParam by_pairs, by_keys;
321 by_pairs.set_hash_fct(new_pair_hash);
322 by_keys.set_hash_fct(new_hash);
323 for (
int k = 0;
k < Keys; ++
k)
325 by_pairs.insert(
k, 10 *
k);
326 by_keys.insert(Keys +
k, 10 * (Keys +
k));
329 TypeParam
copy = by_pairs;
330 expect_pairs(
copy, 0, Keys);
331 TypeParam moved = std::move(
copy);
332 expect_pairs(moved, 0, Keys);
335 expect_pairs(assigned, Keys, 2 * Keys);
336 TypeParam move_assigned;
337 move_assigned = std::move(assigned);
338 expect_pairs(move_assigned, Keys, 2 * Keys);
340 by_pairs.swap(by_keys);
341 expect_pairs(by_pairs, Keys, 2 * Keys);
342 expect_pairs(by_keys, 0, Keys);
353 No_Default(
int v) noexcept :
value(v) {}
356 template <
class Key,
class Data>
363 static_assert(
noexcept(std::declval<DynMapHash<int, int> &>().
search(1)));
364 static_assert(
noexcept(std::declval<DynMapLinHash<int, int> &>().has(1)));
365 static_assert(not
noexcept(std::declval<DynMapHash<std::string, int> &>()
366 .
search(std::declval<const std::string &>())));
367 static_assert(not
noexcept(std::declval<const DynMapLinHash<std::string, int> &>()
368 .has(std::declval<const std::string &>())));
369 static_assert(
noexcept(std::declval<DynMapHash<std::string, int> &>()
370 .
search(std::declval<std::string>())));
371 static_assert(
noexcept(std::declval<DynMapHash<std::string, No_Default> &>()
372 .
search(std::declval<const std::string &>())));
376 TEST(Map_Hash_Function, ADataWithNoDefaultConstructorStillSearchesByTheKey)
380 for (
int k = 0;
k < Keys; ++
k)
382 for (
int k = 0;
k < Keys; ++
k)
384 const auto * p = map.
search(
k);
385 ASSERT_NE(p,
nullptr) <<
"key " <<
k;
386 EXPECT_EQ(p->second.value, 10 *
k);
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
TYPED_TEST(ArcHeapLifecycle, ClearNullsTheHandles)
size_t size_t int32_t value
Dynamic hash table mapping keys to records with separate chaining.
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Searches for the pair whose key is key.
void set_hash_fct(Hash_Fct_Ptr fct)
Sets the hash function of the keys.
Generic linear hash table.
BucketType< Key > Bucket
The bucket type.
Open addressing hash table with double hashing collision resolution.
void set_hash_fct(Hash_Fct fct)
Open addressing hash table with linear probing collision resolution.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic hash table mapping keys to records with separate chaining.
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Hash tables with collision resolution by separate chaining.
Linear hashing with chaining.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.