38#include <gtest/gtest.h>
61 auto* ptr = table.
insert(1,
"one");
63 ASSERT_NE(ptr,
nullptr);
64 EXPECT_EQ(*ptr,
"one");
66 auto* found = table.search(1);
67 ASSERT_NE(found,
nullptr);
68 EXPECT_EQ(*found,
"one");
73 table.insert(1,
"one");
75 auto* found = table.search(2);
76 EXPECT_EQ(found,
nullptr);
81 table.insert(1,
"one");
82 table.insert(2,
"two");
83 table.insert(3,
"three");
85 EXPECT_EQ(*table.search(1),
"one");
86 EXPECT_EQ(*table.search(2),
"two");
87 EXPECT_EQ(*table.search(3),
"three");
92 auto* ptr = table.insert(1,
"one");
93 table.insert(2,
"two");
97 EXPECT_EQ(table.search(1),
nullptr);
98 EXPECT_NE(table.search(2),
nullptr);
103 table.insert(1,
"one");
105 auto* ptr = table.search(1);
106 ASSERT_NE(ptr,
nullptr);
109 EXPECT_EQ(*table.search(1),
"ONE");
120 auto* found = table.search(1);
121 ASSERT_NE(found,
nullptr);
122 EXPECT_EQ(*found,
"one");
130 EXPECT_EQ(*table.search(1),
"ONE");
135 table.insert(1,
"one");
137 std::string val = table[1];
138 EXPECT_EQ(val,
"one");
143 table.insert(1,
"one");
147 std::string val = table[2];
148 }, std::invalid_argument);
157 table.insert(1,
"one");
158 table.insert(2,
"two");
162 EXPECT_EQ(*
copy.search(1),
"one");
163 EXPECT_EQ(*
copy.search(2),
"two");
166 *table.search(1) =
"ONE";
167 EXPECT_EQ(*
copy.search(1),
"one");
172 table.insert(1,
"one");
173 table.insert(2,
"two");
180 EXPECT_EQ(*other.
search(1),
"one");
181 EXPECT_EQ(*other.
search(2),
"two");
182 EXPECT_EQ(other.
search(3),
nullptr);
187 table.insert(1,
"one");
188 table.insert(2,
"two");
192 EXPECT_EQ(*moved.
search(1),
"one");
193 EXPECT_EQ(*moved.
search(2),
"two");
198 table.insert(1,
"one");
199 table.insert(2,
"two");
202 other = std::move(table);
204 EXPECT_EQ(*other.
search(1),
"one");
205 EXPECT_EQ(*other.
search(2),
"two");
210 table.insert(1,
"one");
214 EXPECT_EQ(*table.search(1),
"one");
221TEST(DynLhashTableStringKey, BasicOperations)
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);
235TEST(DynLhashTableStringKey, LongStrings)
239 std::string longKey(1000,
'x');
240 table.
insert(longKey, 42);
242 EXPECT_EQ(*table.
search(longKey), 42);
249TEST(DynLhashTableCollision, SameHashBucket)
255 for (
int i = 0; i < 100; ++i)
256 table.
insert(i,
"val" + std::to_string(i));
259 for (
int i = 0; i < 100; ++i)
260 EXPECT_EQ(*table.
search(i),
"val" + std::to_string(i));
267TEST(DynLhashTableMoveValue, MoveInsert)
271 std::string
value =
"hello";
274 EXPECT_EQ(*table.
search(1),
"hello");
278TEST(DynLhashTableMoveValue, MoveKeyAndValue)
282 std::string key =
"key";
283 std::string
value =
"value";
286 EXPECT_EQ(*table.
search(
"key"),
"value");
293TEST(DynLhashTableStress, ManyInsertions)
298 for (
int i = 0; i <
N; ++i)
301 for (
int i = 0; i <
N; ++i)
302 EXPECT_EQ(*table.
search(i), i * 2);
305TEST(DynLhashTableStress, ManyInsertionsAndRemovals)
310 std::vector<int*> pointers;
313 for (
int i = 0; i <
N; ++i)
314 pointers.push_back(table.
insert(i, i));
317 for (
int i = 0; i <
N / 2; ++i)
318 table.
remove(pointers[i]);
321 for (
int i =
N / 2; i <
N; ++i)
322 EXPECT_EQ(*table.
search(i), i);
325 for (
int i = 0; i <
N / 2; ++i)
326 EXPECT_EQ(table.
search(i),
nullptr);
329TEST(DynLhashTableStress, RandomOperations)
332 std::map<int, int*> reference;
334 std::mt19937
rng(42);
335 std::uniform_int_distribution<> key_dist(0, 999);
336 std::uniform_int_distribution<> op_dist(0, 2);
338 for (
int iter = 0; iter < 5000; ++iter)
340 int key = key_dist(
rng);
341 int op = op_dist(
rng);
345 if (reference.count(key) == 0)
347 auto* ptr = table.
insert(key, key * 2);
348 reference[key] = ptr;
353 auto* found = table.
search(key);
354 if (reference.count(key))
356 ASSERT_NE(found,
nullptr);
357 EXPECT_EQ(*found, key * 2);
362 if (reference.count(key))
364 table.
remove(reference[key]);
365 reference.erase(key);
371 for (
auto& [key, ptr] : reference)
373 auto* found = table.
search(key);
374 ASSERT_NE(found,
nullptr);
375 EXPECT_EQ(*found, key * 2);
379 for (
auto& [key, ptr] : reference)
389 EXPECT_EQ(table.search(1),
nullptr);
390 EXPECT_EQ(table.search(0),
nullptr);
391 EXPECT_EQ(table.search(-1),
nullptr);
396 table.insert(-1,
"negative one");
397 table.insert(-100,
"negative hundred");
398 table.insert(0,
"zero");
400 EXPECT_EQ(*table.search(-1),
"negative one");
401 EXPECT_EQ(*table.search(-100),
"negative hundred");
402 EXPECT_EQ(*table.search(0),
"zero");
407 auto* ptr1 = table.insert(1,
"first");
408 auto* ptr2 = table.insert(1,
"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);
426 return static_cast<size_t>(key * 31 + 17);
429TEST(DynLhashTableCustomHash, WithCustomHashFunction)
436 EXPECT_EQ(*table.
search(1),
"one");
437 EXPECT_EQ(*table.
search(2),
"two");
444TEST(DynLhashTableSwap, SwapTables)
456 EXPECT_EQ(table1.
search(1),
nullptr);
457 EXPECT_EQ(table1.
search(2),
nullptr);
458 EXPECT_EQ(*table1.
search(10),
"ten");
461 EXPECT_EQ(*table2.
search(1),
"one");
462 EXPECT_EQ(*table2.
search(2),
"two");
463 EXPECT_EQ(table2.
search(10),
nullptr);
466TEST(DynLhashTableSwap, SwapWithEmpty)
475 EXPECT_EQ(table1.
search(1),
nullptr);
476 EXPECT_EQ(*table2.
search(1),
"one");
483TEST(DynLhashTablePostMove, MoveConstructorTransfersOwnership)
486 source.insert(1,
"one");
487 source.insert(2,
"two");
492 EXPECT_EQ(*dest.
search(1),
"one");
493 EXPECT_EQ(*dest.
search(2),
"two");
498TEST(DynLhashTablePostMove, MoveAssignmentTransfersOwnership)
501 source.insert(1,
"one");
502 source.insert(2,
"two");
507 dest = std::move(source);
510 EXPECT_EQ(*dest.
search(1),
"one");
511 EXPECT_EQ(*dest.
search(2),
"two");
512 EXPECT_EQ(dest.
search(10),
nullptr);
547 data = std::move(other.data);
562TEST(DynLhashTableComplexType, MoveSemanticsPropagated)
570 table.
insert(1, std::move(val));
575 auto* found = table.
search(1);
576 ASSERT_NE(found,
nullptr);
577 EXPECT_EQ(found->data,
"test");
578 EXPECT_EQ(found->counter, 42);
585TEST(DynLhashTableDuplicates, InsertRefusesAKeyAlreadyThere)
589 auto* ptr1 = table.
insert(1,
"first");
590 ASSERT_NE(ptr1,
nullptr);
594 EXPECT_EQ(table.
insert(1,
"second"),
nullptr);
595 EXPECT_EQ(table.
insert(1, std::string(
"third")),
nullptr);
597 EXPECT_EQ(table.
insert(key,
"fourth"),
nullptr);
599 EXPECT_EQ(table.
size(), 1u);
600 ASSERT_EQ(table.
search(1), ptr1);
601 EXPECT_EQ(*ptr1,
"first");
605 EXPECT_EQ(table.
search(1),
nullptr);
608TEST(DynLhashTableDuplicates, RefusedKeyLeavesTheSizeRight)
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);
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");
630TEST(DynLhashTableStress, LargeScaleWithVerification)
633 const int N = 100000;
636 for (
int i = 0; i <
N; ++i)
640 for (
int i = 0; i <
N; ++i)
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;
648TEST(DynLhashTableStress, InterleavedInsertRemove)
651 std::vector<int*> ptrs;
655 for (
int i = 0; i <
N; ++i)
656 ptrs.push_back(table.
insert(i, i));
659 for (
int i = 1; i <
N; i += 2)
666 for (
int i =
N; i <
N +
N/2; ++i)
670 for (
int i = 0; i <
N; i += 2)
672 auto* found = table.
search(i);
673 ASSERT_NE(found,
nullptr);
674 EXPECT_EQ(*found, i);
678 for (
int i = 1; i <
N; i += 2)
679 EXPECT_EQ(table.
search(i),
nullptr);
682 for (
int i =
N; i <
N +
N/2; ++i)
684 auto* found = table.
search(i);
685 ASSERT_NE(found,
nullptr);
686 EXPECT_EQ(*found, i);
698 EXPECT_FALSE(it.has_curr());
699 EXPECT_EQ(it.get_curr_ne(),
nullptr);
702 table.insert(1,
"one");
704 ASSERT_TRUE(it2.has_curr());
706 EXPECT_FALSE(it2.has_curr());
707 EXPECT_EQ(it2.get_curr_ne(),
nullptr);
723 bool operator()(
const std::string & a,
const std::string & b)
const
725 if (a.size() != b.size())
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])))
735 size_t no_case_hash(
const std::string & s)
738 for (
const char c : s)
739 h =
h * 31 +
std::
tolower(static_cast<unsigned char>(c));
750 bool operator==(
const Name &)
const =
default;
753 size_t name_hash(
const Name & n)
755 return no_case_hash(n.first) * 31 + no_case_hash(n.last);
759 struct Fragile_Record
761 static inline int copies_before_failure = -1;
765 Fragile_Record(
const int v = 0) :
value(v) {}
767 Fragile_Record(
const Fragile_Record & other) :
value(other.
value)
769 if (copies_before_failure == 0)
770 throw std::runtime_error(
"no copy");
771 if (copies_before_failure > 0)
772 --copies_before_failure;
775 Fragile_Record & operator=(
const Fragile_Record &) =
default;
781TEST(DynLhashTableConfiguration, UsesItsComparator)
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())>,
794TEST(DynLhashTableConfiguration, CopyKeepsTheConfiguration)
797 for (
int i = 0; i < 50; ++i)
798 table.
insert(
"key" + std::to_string(i), i);
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());
810 EXPECT_EQ(
copy.size(), 50u);
811 for (
int i = 0; i < 50; ++i)
813 const std::string key =
"KEY" + std::to_string(i);
814 ASSERT_NE(
copy.search(key),
nullptr) << key;
815 EXPECT_EQ(*
copy.search(key), i) << key;
821TEST(DynLhashTableConfiguration, CopiesAKeyWithoutDefaultHash)
824 for (
int i = 0; i < 20; ++i)
825 table.
insert(Name{
"first" + std::to_string(i),
"last"}, i);
831 for (
int i = 0; i < 20; ++i)
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);
839 const Name probe{
"a",
"b"};
840 EXPECT_EQ(
copy.get_hash_fct()(probe), name_hash(probe));
845TEST(DynLhashTableConfiguration, AssignmentTakesTheConfigurationOrNothing)
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);
853 target.
insert(
"old", Fragile_Record(-1));
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);
863 EXPECT_EQ(target.
size(), 30u);
864 EXPECT_EQ(target.
search(
"old"),
nullptr);
866 EXPECT_EQ(target.
capacity(), source.capacity());
867 for (
int i = 0; i < 30; ++i)
869 ASSERT_NE(target.
search(
"K" + std::to_string(i)),
nullptr) << i;
870 EXPECT_EQ(target.
search(
"K" + std::to_string(i))->value, i);
876 ::testing::InitGoogleTest(&argc, argv);
877 return RUN_ALL_TESTS();
size_t size_t int32_t value
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.
const size_t & capacity() const noexcept
Returns the table capacity.
constexpr bool is_empty() const noexcept
Number Theoretic Transform over Z / MOD Z.
DynLhashTable< int, std::string > table
void set_upper_alpha(float _upper_alpha)
Sets the upper load factor threshold.
void set_with_resize(const bool with_resize) noexcept
Turns the automatic resizing on or off.
void set_lower_alpha(float _lower_alpha)
Sets the lower load factor threshold.
constexpr bool get_with_resize() const noexcept
Returns whether the table resizes itself.
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.
ComplexValue & operator=(ComplexValue &&other) noexcept
bool operator==(const ComplexValue &other) const
ComplexValue(const ComplexValue &other)
ComplexValue(const std::string &d, int c)
ComplexValue(ComplexValue &&other) noexcept
ComplexValue & operator=(const ComplexValue &other)
Dynamic hash table mapping keys to records with separate chaining.
TEST_F(DynLhashTableTest, InsertAndSearch)
size_t custom_hash(const int &key)