Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynLhash_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39#include <tpl_dynLhash.H>
40#include <cctype>
41#include <stdexcept>
42#include <string>
43#include <type_traits>
44#include <vector>
45#include <random>
46
47using namespace Aleph;
48
49// =============================================================================
50// Basic Functionality Tests
51// =============================================================================
52
53class DynLhashTableTest : public ::testing::Test
54{
55protected:
57};
58
59TEST_F(DynLhashTableTest, InsertAndSearch)
60{
61 auto* ptr = table.insert(1, "one");
62
63 ASSERT_NE(ptr, nullptr);
64 EXPECT_EQ(*ptr, "one");
65
66 auto* found = table.search(1);
67 ASSERT_NE(found, nullptr);
68 EXPECT_EQ(*found, "one");
69}
70
71TEST_F(DynLhashTableTest, SearchNonExistent)
72{
73 table.insert(1, "one");
74
75 auto* found = table.search(2);
76 EXPECT_EQ(found, nullptr);
77}
78
79TEST_F(DynLhashTableTest, InsertMultiple)
80{
81 table.insert(1, "one");
82 table.insert(2, "two");
83 table.insert(3, "three");
84
85 EXPECT_EQ(*table.search(1), "one");
86 EXPECT_EQ(*table.search(2), "two");
87 EXPECT_EQ(*table.search(3), "three");
88}
89
91{
92 auto* ptr = table.insert(1, "one");
93 table.insert(2, "two");
94
95 table.remove(ptr);
96
97 EXPECT_EQ(table.search(1), nullptr);
98 EXPECT_NE(table.search(2), nullptr);
99}
100
101TEST_F(DynLhashTableTest, UpdateExisting)
102{
103 table.insert(1, "one");
104
105 auto* ptr = table.search(1);
106 ASSERT_NE(ptr, nullptr);
107 *ptr = "ONE";
108
109 EXPECT_EQ(*table.search(1), "ONE");
110}
111
112// =============================================================================
113// Operator[] Tests
114// =============================================================================
115
116TEST_F(DynLhashTableTest, BracketOperatorInsert)
117{
118 table[1] = "one";
119
120 auto* found = table.search(1);
121 ASSERT_NE(found, nullptr);
122 EXPECT_EQ(*found, "one");
123}
124
125TEST_F(DynLhashTableTest, BracketOperatorUpdate)
126{
127 table[1] = "one";
128 table[1] = "ONE";
129
130 EXPECT_EQ(*table.search(1), "ONE");
131}
132
133TEST_F(DynLhashTableTest, BracketOperatorReadExisting)
134{
135 table.insert(1, "one");
136
137 std::string val = table[1];
138 EXPECT_EQ(val, "one");
139}
140
141TEST_F(DynLhashTableTest, BracketOperatorReadNonExisting)
142{
143 table.insert(1, "one");
144
145 // Reading non-existent key should throw
146 EXPECT_THROW({
147 std::string val = table[2];
148 }, std::invalid_argument);
149}
150
151// =============================================================================
152// Copy and Move Semantics Tests
153// =============================================================================
154
155TEST_F(DynLhashTableTest, CopyConstructor)
156{
157 table.insert(1, "one");
158 table.insert(2, "two");
159
161
162 EXPECT_EQ(*copy.search(1), "one");
163 EXPECT_EQ(*copy.search(2), "two");
164
165 // Modify original, copy should be independent
166 *table.search(1) = "ONE";
167 EXPECT_EQ(*copy.search(1), "one");
168}
169
170TEST_F(DynLhashTableTest, CopyAssignment)
171{
172 table.insert(1, "one");
173 table.insert(2, "two");
174
176 other.insert(3, "three");
177
178 other = table;
179
180 EXPECT_EQ(*other.search(1), "one");
181 EXPECT_EQ(*other.search(2), "two");
182 EXPECT_EQ(other.search(3), nullptr);
183}
184
185TEST_F(DynLhashTableTest, MoveConstructor)
186{
187 table.insert(1, "one");
188 table.insert(2, "two");
189
190 DynLhashTable<int, std::string> moved(std::move(table));
191
192 EXPECT_EQ(*moved.search(1), "one");
193 EXPECT_EQ(*moved.search(2), "two");
194}
195
196TEST_F(DynLhashTableTest, MoveAssignment)
197{
198 table.insert(1, "one");
199 table.insert(2, "two");
200
202 other = std::move(table);
203
204 EXPECT_EQ(*other.search(1), "one");
205 EXPECT_EQ(*other.search(2), "two");
206}
207
208TEST_F(DynLhashTableTest, SelfAssignment)
209{
210 table.insert(1, "one");
211
212 table = table; // Self-assignment
213
214 EXPECT_EQ(*table.search(1), "one");
215}
216
217// =============================================================================
218// String Key Tests
219// =============================================================================
220
221TEST(DynLhashTableStringKey, BasicOperations)
222{
224
225 table.insert("one", 1);
226 table.insert("two", 2);
227 table.insert("three", 3);
228
229 EXPECT_EQ(*table.search("one"), 1);
230 EXPECT_EQ(*table.search("two"), 2);
231 EXPECT_EQ(*table.search("three"), 3);
232 EXPECT_EQ(table.search("four"), nullptr);
233}
234
235TEST(DynLhashTableStringKey, LongStrings)
236{
238
239 std::string longKey(1000, 'x');
240 table.insert(longKey, 42);
241
242 EXPECT_EQ(*table.search(longKey), 42);
243}
244
245// =============================================================================
246// Collision Handling Tests
247// =============================================================================
248
249TEST(DynLhashTableCollision, SameHashBucket)
250{
251 // Use small table size to force collisions
253
254 // Insert values that will likely collide
255 for (int i = 0; i < 100; ++i)
256 table.insert(i, "val" + std::to_string(i));
257
258 // Verify all values are retrievable
259 for (int i = 0; i < 100; ++i)
260 EXPECT_EQ(*table.search(i), "val" + std::to_string(i));
261}
262
263// =============================================================================
264// Move Semantics for Values
265// =============================================================================
266
267TEST(DynLhashTableMoveValue, MoveInsert)
268{
270
271 std::string value = "hello";
272 table.insert(1, std::move(value));
273
274 EXPECT_EQ(*table.search(1), "hello");
275 // value should be moved-from (possibly empty)
276}
277
278TEST(DynLhashTableMoveValue, MoveKeyAndValue)
279{
281
282 std::string key = "key";
283 std::string value = "value";
284 table.insert(std::move(key), std::move(value));
285
286 EXPECT_EQ(*table.search("key"), "value");
287}
288
289// =============================================================================
290// Stress Tests
291// =============================================================================
292
293TEST(DynLhashTableStress, ManyInsertions)
294{
296
297 const int N = 10000;
298 for (int i = 0; i < N; ++i)
299 table.insert(i, i * 2);
300
301 for (int i = 0; i < N; ++i)
302 EXPECT_EQ(*table.search(i), i * 2);
303}
304
305TEST(DynLhashTableStress, ManyInsertionsAndRemovals)
306{
308
309 const int N = 1000;
310 std::vector<int*> pointers;
311
312 // Insert
313 for (int i = 0; i < N; ++i)
314 pointers.push_back(table.insert(i, i));
315
316 // Remove half
317 for (int i = 0; i < N / 2; ++i)
318 table.remove(pointers[i]);
319
320 // Verify remaining
321 for (int i = N / 2; i < N; ++i)
322 EXPECT_EQ(*table.search(i), i);
323
324 // Verify removed are gone
325 for (int i = 0; i < N / 2; ++i)
326 EXPECT_EQ(table.search(i), nullptr);
327}
328
329TEST(DynLhashTableStress, RandomOperations)
330{
332 std::map<int, int*> reference; // Track what's in the table
333
334 std::mt19937 rng(42);
335 std::uniform_int_distribution<> key_dist(0, 999);
336 std::uniform_int_distribution<> op_dist(0, 2);
337
338 for (int iter = 0; iter < 5000; ++iter)
339 {
340 int key = key_dist(rng);
341 int op = op_dist(rng);
342
343 if (op == 0) // Insert (only if not already present to avoid duplicates)
344 {
345 if (reference.count(key) == 0)
346 {
347 auto* ptr = table.insert(key, key * 2);
348 reference[key] = ptr;
349 }
350 }
351 else if (op == 1) // Search
352 {
353 auto* found = table.search(key);
354 if (reference.count(key))
355 {
356 ASSERT_NE(found, nullptr);
357 EXPECT_EQ(*found, key * 2);
358 }
359 }
360 else // Remove
361 {
362 if (reference.count(key))
363 {
364 table.remove(reference[key]);
365 reference.erase(key);
366 }
367 }
368 }
369
370 // Final verification
371 for (auto& [key, ptr] : reference)
372 {
373 auto* found = table.search(key);
374 ASSERT_NE(found, nullptr);
375 EXPECT_EQ(*found, key * 2);
376 }
377
378 // Clean up remaining elements
379 for (auto& [key, ptr] : reference)
380 table.remove(ptr);
381}
382
383// =============================================================================
384// Edge Cases
385// =============================================================================
386
388{
389 EXPECT_EQ(table.search(1), nullptr);
390 EXPECT_EQ(table.search(0), nullptr);
391 EXPECT_EQ(table.search(-1), nullptr);
392}
393
395{
396 table.insert(-1, "negative one");
397 table.insert(-100, "negative hundred");
398 table.insert(0, "zero");
399
400 EXPECT_EQ(*table.search(-1), "negative one");
401 EXPECT_EQ(*table.search(-100), "negative hundred");
402 EXPECT_EQ(*table.search(0), "zero");
403}
404
405TEST_F(DynLhashTableTest, SameKeyMultipleInserts)
406{
407 auto* ptr1 = table.insert(1, "first");
408 auto* ptr2 = table.insert(1, "second"); // Same key, different value
409
410 // The table keeps the first record and refuses the second
411 ASSERT_NE(ptr1, nullptr);
412 EXPECT_EQ(ptr2, nullptr);
413 ASSERT_EQ(table.search(1), ptr1);
414 EXPECT_EQ(*ptr1, "first");
415 EXPECT_EQ(table.size(), 1u);
416
417 table.remove(ptr1);
418}
419
420// =============================================================================
421// Custom Hash Function
422// =============================================================================
423
424size_t custom_hash(const int& key)
425{
426 return static_cast<size_t>(key * 31 + 17);
427}
428
429TEST(DynLhashTableCustomHash, WithCustomHashFunction)
430{
432
433 table.insert(1, "one");
434 table.insert(2, "two");
435
436 EXPECT_EQ(*table.search(1), "one");
437 EXPECT_EQ(*table.search(2), "two");
438}
439
440// =============================================================================
441// Swap Tests
442// =============================================================================
443
444TEST(DynLhashTableSwap, SwapTables)
445{
448
449 table1.insert(1, "one");
450 table1.insert(2, "two");
451 table2.insert(10, "ten");
452
453 table1.swap(table2);
454
455 // table1 should now have table2's contents
456 EXPECT_EQ(table1.search(1), nullptr);
457 EXPECT_EQ(table1.search(2), nullptr);
458 EXPECT_EQ(*table1.search(10), "ten");
459
460 // table2 should now have table1's original contents
461 EXPECT_EQ(*table2.search(1), "one");
462 EXPECT_EQ(*table2.search(2), "two");
463 EXPECT_EQ(table2.search(10), nullptr);
464}
465
466TEST(DynLhashTableSwap, SwapWithEmpty)
467{
470
471 table1.insert(1, "one");
472
473 table1.swap(table2);
474
475 EXPECT_EQ(table1.search(1), nullptr);
476 EXPECT_EQ(*table2.search(1), "one");
477}
478
479// =============================================================================
480// Post-Move State Tests
481// =============================================================================
482
483TEST(DynLhashTablePostMove, MoveConstructorTransfersOwnership)
484{
486 source.insert(1, "one");
487 source.insert(2, "two");
488
489 DynLhashTable<int, std::string> dest(std::move(source));
490
491 // Destination should have all the data
492 EXPECT_EQ(*dest.search(1), "one");
493 EXPECT_EQ(*dest.search(2), "two");
494
495 // Note: source is in moved-from state - only safe for destruction/assignment
496}
497
498TEST(DynLhashTablePostMove, MoveAssignmentTransfersOwnership)
499{
501 source.insert(1, "one");
502 source.insert(2, "two");
503
505 dest.insert(10, "ten"); // Will be replaced
506
507 dest = std::move(source);
508
509 // Destination should have source's data
510 EXPECT_EQ(*dest.search(1), "one");
511 EXPECT_EQ(*dest.search(2), "two");
512 EXPECT_EQ(dest.search(10), nullptr); // Old data gone
513
514 // Note: source is in moved-from state - only safe for destruction/assignment
515}
516
517// =============================================================================
518// Complex Types Tests
519// =============================================================================
520
522{
523 std::string data;
525 static int copy_count;
526 static int move_count;
527
528 ComplexValue() : data(""), counter(0) {}
529 ComplexValue(const std::string& d, int c) : data(d), counter(c) {}
530
532 : data(other.data), counter(other.counter) { ++copy_count; }
533
534 ComplexValue(ComplexValue&& other) noexcept
535 : data(std::move(other.data)), counter(other.counter) { ++move_count; }
536
538 {
539 data = other.data;
540 counter = other.counter;
541 ++copy_count;
542 return *this;
543 }
544
546 {
547 data = std::move(other.data);
548 counter = other.counter;
549 ++move_count;
550 return *this;
551 }
552
553 bool operator==(const ComplexValue& other) const
554 {
555 return data == other.data && counter == other.counter;
556 }
557};
558
561
562TEST(DynLhashTableComplexType, MoveSemanticsPropagated)
563{
566
568
569 ComplexValue val("test", 42);
570 table.insert(1, std::move(val));
571
572 // Should have moved, not copied
573 EXPECT_GT(ComplexValue::move_count, 0);
574
575 auto* found = table.search(1);
576 ASSERT_NE(found, nullptr);
577 EXPECT_EQ(found->data, "test");
578 EXPECT_EQ(found->counter, 42);
579}
580
581// =============================================================================
582// Duplicate Keys Behavior Tests
583// =============================================================================
584
585TEST(DynLhashTableDuplicates, InsertRefusesAKeyAlreadyThere)
586{
588
589 auto* ptr1 = table.insert(1, "first");
590 ASSERT_NE(ptr1, nullptr);
591
592 // The table refuses a key that it holds. insert() used to return the
593 // record of a bucket that it had not stored, and that nobody freed.
594 EXPECT_EQ(table.insert(1, "second"), nullptr);
595 EXPECT_EQ(table.insert(1, std::string("third")), nullptr);
596 const int key = 1;
597 EXPECT_EQ(table.insert(key, "fourth"), nullptr);
598
599 EXPECT_EQ(table.size(), 1u);
600 ASSERT_EQ(table.search(1), ptr1);
601 EXPECT_EQ(*ptr1, "first");
602
603 table.remove(ptr1);
604 EXPECT_TRUE(table.is_empty());
605 EXPECT_EQ(table.search(1), nullptr);
606}
607
608TEST(DynLhashTableDuplicates, RefusedKeyLeavesTheSizeRight)
609{
611 table.set_with_resize(false); // a shrink would recount the keys
612
613 auto* ptr1 = table.insert(1, "first");
614 ASSERT_NE(ptr1, nullptr);
615 ASSERT_EQ(table.insert(1, "second"), nullptr);
616 ASSERT_NE(table.insert(2, "two"), nullptr);
617 EXPECT_EQ(table.size(), 2u);
618
619 table.remove(ptr1);
620 EXPECT_EQ(table.size(), 1u);
621 EXPECT_EQ(table.search(1), nullptr);
622 ASSERT_NE(table.search(2), nullptr);
623 EXPECT_EQ(*table.search(2), "two");
624}
625
626// =============================================================================
627// Large Scale Stress Tests
628// =============================================================================
629
630TEST(DynLhashTableStress, LargeScaleWithVerification)
631{
633 const int N = 100000;
634
635 // Insert many elements
636 for (int i = 0; i < N; ++i)
637 table.insert(i, i * 3);
638
639 // Verify all elements
640 for (int i = 0; i < N; ++i)
641 {
642 auto* found = table.search(i);
643 ASSERT_NE(found, nullptr) << "Key " << i << " not found";
644 EXPECT_EQ(*found, i * 3) << "Wrong value for key " << i;
645 }
646}
647
648TEST(DynLhashTableStress, InterleavedInsertRemove)
649{
651 std::vector<int*> ptrs;
652 const int N = 10000;
653
654 // Insert first batch
655 for (int i = 0; i < N; ++i)
656 ptrs.push_back(table.insert(i, i));
657
658 // Remove odd indices, insert new values
659 for (int i = 1; i < N; i += 2)
660 {
661 table.remove(ptrs[i]);
662 ptrs[i] = nullptr;
663 }
664
665 // Insert replacement values
666 for (int i = N; i < N + N/2; ++i)
667 table.insert(i, i);
668
669 // Verify even indices remain
670 for (int i = 0; i < N; i += 2)
671 {
672 auto* found = table.search(i);
673 ASSERT_NE(found, nullptr);
674 EXPECT_EQ(*found, i);
675 }
676
677 // Verify odd indices removed
678 for (int i = 1; i < N; i += 2)
679 EXPECT_EQ(table.search(i), nullptr);
680
681 // Verify new values
682 for (int i = N; i < N + N/2; ++i)
683 {
684 auto* found = table.search(i);
685 ASSERT_NE(found, nullptr);
686 EXPECT_EQ(*found, i);
687 }
688}
689
690// =============================================================================
691// Iterator Tests
692// =============================================================================
693
694TEST_F(DynLhashTableTest, IteratorSafety)
695{
696 // Test empty table iterator
698 EXPECT_FALSE(it.has_curr());
699 EXPECT_EQ(it.get_curr_ne(), nullptr);
700
701 // Test iterator at the end
702 table.insert(1, "one");
704 ASSERT_TRUE(it2.has_curr());
705 it2.next();
706 EXPECT_FALSE(it2.has_curr());
707 EXPECT_EQ(it2.get_curr_ne(), nullptr);
708}
709
710// =============================================================================
711// Main
712// =============================================================================
713
714// =============================================================================
715// Copies keep the configuration; the comparator is used
716// =============================================================================
717
718namespace
719{
720 // Equal strings up to case, with a hash that agrees with that equality
721 struct No_Case_Equal
722 {
723 bool operator()(const std::string & a, const std::string & b) const
724 {
725 if (a.size() != b.size())
726 return false;
727 for (size_t i = 0; i < a.size(); ++i)
728 if (std::tolower(static_cast<unsigned char>(a[i])) !=
729 std::tolower(static_cast<unsigned char>(b[i])))
730 return false;
731 return true;
732 }
733 };
734
735 size_t no_case_hash(const std::string & s)
736 {
737 size_t h = 0;
738 for (const char c : s)
739 h = h * 31 + std::tolower(static_cast<unsigned char>(c));
740 return h;
741 }
742
743 // A key that the default hash function refuses: it is not trivially
744 // copyable and has no aleph_hash_value(). Its table has its own.
745 struct Name
746 {
747 std::string first;
748 std::string last;
749
750 bool operator==(const Name &) const = default;
751 };
752
753 size_t name_hash(const Name & n)
754 {
755 return no_case_hash(n.first) * 31 + no_case_hash(n.last);
756 }
757
758 // A record whose copies can be made to fail
759 struct Fragile_Record
760 {
761 static inline int copies_before_failure = -1; // negative: none fails
762
763 int value = 0;
764
765 Fragile_Record(const int v = 0) : value(v) {}
766
767 Fragile_Record(const Fragile_Record & other) : value(other.value)
768 {
769 if (copies_before_failure == 0)
770 throw std::runtime_error("no copy");
771 if (copies_before_failure > 0)
772 --copies_before_failure;
773 }
774
775 Fragile_Record & operator=(const Fragile_Record &) = default;
776 };
777}
778
779// DynLhashTable passed its comparator nowhere: the table compared with
780// Aleph::equal_to<Key>.
781TEST(DynLhashTableConfiguration, UsesItsComparator)
782{
783 DynLhashTable<std::string, int, No_Case_Equal> table(101, no_case_hash);
784 table.insert("alice", 1);
785
786 ASSERT_NE(table.search("ALICE"), nullptr);
787 EXPECT_EQ(*table.search("Alice"), 1);
788 EXPECT_TRUE((std::is_same_v<std::decay_t<decltype(table.get_compare())>,
789 No_Case_Equal>));
790}
791
792// The copy was built with the default hash function, thresholds, capacity
793// and resizing, and the keys were placed again with that function.
794TEST(DynLhashTableConfiguration, CopyKeepsTheConfiguration)
795{
796 DynLhashTable<std::string, int, No_Case_Equal> table(211, no_case_hash);
797 for (int i = 0; i < 50; ++i)
798 table.insert("key" + std::to_string(i), i);
799 table.set_upper_alpha(3.0f);
800 table.set_lower_alpha(0.5f);
801 table.set_with_resize(false);
802
804
805 EXPECT_EQ(copy.get_hash_fct()("AbC"), no_case_hash("AbC"));
806 EXPECT_EQ(copy.get_lower_alpha(), 0.5f);
807 EXPECT_EQ(copy.get_upper_alpha(), 3.0f);
808 EXPECT_FALSE(copy.get_with_resize());
809 EXPECT_EQ(copy.capacity(), table.capacity());
810 EXPECT_EQ(copy.size(), 50u);
811 for (int i = 0; i < 50; ++i)
812 {
813 const std::string key = "KEY" + std::to_string(i); // the comparator
814 ASSERT_NE(copy.search(key), nullptr) << key;
815 EXPECT_EQ(*copy.search(key), i) << key;
816 }
817}
818
819// For a key that the default hash function refuses, the copy did not
820// compile: it asked for the default function instead of the table's.
821TEST(DynLhashTableConfiguration, CopiesAKeyWithoutDefaultHash)
822{
823 DynLhashTable<Name, int> table(101, name_hash);
824 for (int i = 0; i < 20; ++i)
825 table.insert(Name{"first" + std::to_string(i), "last"}, i);
826
828 DynLhashTable<Name, int> assigned(11, name_hash);
829 assigned = table;
830
831 for (int i = 0; i < 20; ++i)
832 {
833 const Name key{"first" + std::to_string(i), "last"};
834 ASSERT_NE(copy.search(key), nullptr) << i;
835 EXPECT_EQ(*copy.search(key), i);
836 ASSERT_NE(assigned.search(key), nullptr) << i;
837 EXPECT_EQ(*assigned.search(key), i);
838 }
839 const Name probe{"a", "b"};
840 EXPECT_EQ(copy.get_hash_fct()(probe), name_hash(probe));
841}
842
843// The assignment kept the hash function and thresholds of the target, and
844// a copy that failed midway left it emptied and partly filled.
845TEST(DynLhashTableConfiguration, AssignmentTakesTheConfigurationOrNothing)
846{
848 for (int i = 0; i < 30; ++i)
849 source.insert("k" + std::to_string(i), Fragile_Record(i));
850 source.set_with_resize(false);
851
853 target.insert("old", Fragile_Record(-1));
854
855 Fragile_Record::copies_before_failure = 10;
856 EXPECT_THROW(target = source, std::runtime_error);
857 Fragile_Record::copies_before_failure = -1;
858 EXPECT_EQ(target.size(), 1u);
859 ASSERT_NE(target.search("OLD"), nullptr);
860 EXPECT_TRUE(target.get_with_resize());
861
862 target = source;
863 EXPECT_EQ(target.size(), 30u);
864 EXPECT_EQ(target.search("old"), nullptr);
865 EXPECT_FALSE(target.get_with_resize());
866 EXPECT_EQ(target.capacity(), source.capacity());
867 for (int i = 0; i < 30; ++i)
868 {
869 ASSERT_NE(target.search("K" + std::to_string(i)), nullptr) << i;
870 EXPECT_EQ(target.search("K" + std::to_string(i))->value, i);
871 }
872}
873
874int main(int argc, char **argv)
875{
876 ::testing::InitGoogleTest(&argc, argv);
877 return RUN_ALL_TESTS();
878}
int main()
long double h
Definition btreepic.C:154
size_t size_t int32_t value
Definition ca-c-api.h:116
Dynamic hash table mapping keys to records with separate chaining.
Record * search(const Key &key)
Search for a key in the table.
void remove(Record *record)
Remove an entry from the table.
void swap(DynLhashTable &table) noexcept
Swap contents with another table.
Record * insert(const Key &key, const Record &record)
Insert a key-record pair into the table.
const size_t & size() const noexcept
Returns the number of elements contained in the table.
Definition tpl_lhash.H:697
const size_t & capacity() const noexcept
Returns the table capacity.
Definition tpl_lhash.H:694
constexpr bool is_empty() const noexcept
Definition tpl_lhash.H:703
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
DynLhashTable< int, std::string > table
void set_upper_alpha(float _upper_alpha)
Sets the upper load factor threshold.
Definition hashDry.H:1532
void set_with_resize(const bool with_resize) noexcept
Turns the automatic resizing on or off.
Definition hashDry.H:1580
void set_lower_alpha(float _lower_alpha)
Sets the lower load factor threshold.
Definition hashDry.H:1548
constexpr bool get_with_resize() const noexcept
Returns whether the table resizes itself.
Definition hashDry.H:1569
#define TEST(name)
static mt19937 rng
#define N
Definition fib.C:294
Main namespace for Aleph-w library functions.
std::string tolower(const char *str)
Convert a C std::string to lower-case.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
STL namespace.
ComplexValue & operator=(ComplexValue &&other) noexcept
bool operator==(const ComplexValue &other) const
static int move_count
ComplexValue(const ComplexValue &other)
ComplexValue(const std::string &d, int c)
ComplexValue(ComplexValue &&other) noexcept
ComplexValue & operator=(const ComplexValue &other)
static int copy_count
Dynamic hash table mapping keys to records with separate chaining.
TEST_F(DynLhashTableTest, InsertAndSearch)
size_t custom_hash(const int &key)