51#include <gtest/gtest.h>
72#if defined(__SANITIZE_THREAD__)
73# define ALEPH_PREFIX_TREE_TEST_UNDER_TSAN 1
74#elif defined(__has_feature)
75# if __has_feature(thread_sanitizer)
76# define ALEPH_PREFIX_TREE_TEST_UNDER_TSAN 1
79#ifndef ALEPH_PREFIX_TREE_TEST_UNDER_TSAN
80# define ALEPH_PREFIX_TREE_TEST_UNDER_TSAN 0
85 std::atomic<int> allocation_countdown{-1};
86 std::atomic<bool> track_allocations{
false};
87 std::atomic<int> tracked_balance{0};
89#if !ALEPH_PREFIX_TREE_TEST_UNDER_TSAN
90 bool should_fail_allocation()
noexcept
92 int remaining = allocation_countdown.load(std::memory_order_relaxed);
93 while (remaining >= 0)
97 if (allocation_countdown.compare_exchange_weak(
98 remaining, remaining - 1, std::memory_order_relaxed))
105 void * allocate_or_throw(std::size_t
size)
107 if (should_fail_allocation())
108 throw std::bad_alloc();
113 void *ptr = std::malloc(
size);
115 throw std::bad_alloc();
117 if (track_allocations.load(std::memory_order_relaxed))
118 tracked_balance.fetch_add(1, std::memory_order_relaxed);
123 void release_allocation(
void *ptr)
noexcept
128 if (track_allocations.load(std::memory_order_relaxed))
129 tracked_balance.fetch_sub(1, std::memory_order_relaxed);
135 class AllocationFailureScope
138 explicit AllocationFailureScope(
const int successful_allocations_before_failure)
140 tracked_balance.store(0, std::memory_order_relaxed);
141 track_allocations.store(
true, std::memory_order_relaxed);
142 allocation_countdown.store(successful_allocations_before_failure,
143 std::memory_order_relaxed);
146 ~AllocationFailureScope()
148 allocation_countdown.store(-1, std::memory_order_relaxed);
149 track_allocations.store(
false, std::memory_order_relaxed);
152 [[nodiscard]]
static int balance()
noexcept
154 return tracked_balance.load(std::memory_order_relaxed);
159#if !ALEPH_PREFIX_TREE_TEST_UNDER_TSAN
161void *
operator new(std::size_t
size)
163 return allocate_or_throw(
size);
166void *
operator new[](std::size_t
size)
168 return allocate_or_throw(
size);
171void *
operator new(std::size_t
size,
const std::nothrow_t &)
noexcept
175 return allocate_or_throw(
size);
183void *
operator new[](std::size_t
size,
const std::nothrow_t &)
noexcept
187 return allocate_or_throw(
size);
195void operator delete(
void *ptr)
noexcept
197 release_allocation(ptr);
200void operator delete[](
void *ptr)
noexcept
202 release_allocation(ptr);
205void operator delete(
void *ptr, std::size_t)
noexcept
207 release_allocation(ptr);
210void operator delete[](
void *ptr, std::size_t)
noexcept
212 release_allocation(ptr);
215void operator delete(
void *ptr,
const std::nothrow_t &)
noexcept
217 release_allocation(ptr);
220void operator delete[](
void *ptr,
const std::nothrow_t &)
noexcept
222 release_allocation(ptr);
227using namespace Aleph;
229static_assert(std::is_same_v<decltype(std::declval<Cnode &>().search_child(
'a')),
231static_assert(std::is_same_v<decltype(std::declval<const Cnode &>().search_child(
'a')),
233static_assert(std::is_same_v<decltype(std::declval<Cnode &>().greater_child(
'a')),
235static_assert(std::is_same_v<decltype(std::declval<const Cnode &>().greater_child(
'a')),
237static_assert(std::is_same_v<decltype(std::declval<Cnode &>().search_prefix(
"a")),
238 std::tuple<Cnode *, const char *>>);
239static_assert(std::is_same_v<decltype(std::declval<const Cnode &>().search_prefix(
"a")),
240 std::tuple<const Cnode *, const char *>>);
241static_assert(std::is_same_v<decltype(std::declval<Prefix_Tree &>().root()),
243static_assert(std::is_same_v<decltype(std::declval<const Prefix_Tree &>().root()),
245static_assert(std::is_move_constructible_v<Prefix_Tree_Map<int>>);
246static_assert(std::is_copy_constructible_v<Prefix_Tree_Map<int>>);
247static_assert(std::is_move_constructible_v<Prefix_Tree_Map<std::unique_ptr<int>>>);
248static_assert(not std::is_copy_constructible_v<Prefix_Tree_Map<std::unique_ptr<int>>>);
252 std::vector<std::string> ret;
253 words.
for_each([&ret](
const std::string &
w) { ret.push_back(
w); });
254 std::sort(ret.begin(), ret.end());
260 std::vector<std::string> ret;
261 ret.reserve(words.
size());
262 for (
const auto &
w : words)
264 std::sort(ret.begin(), ret.end());
299 EXPECT_EQ(node.
symbol(),
'a');
305 EXPECT_EQ(node.
symbol(),
'x');
308 EXPECT_EQ(node2.
symbol(),
'$');
314 EXPECT_TRUE(node.
children().is_empty());
330 EXPECT_TRUE(node.
children().is_empty());
339 EXPECT_EQ(
root->search_child(
'a'),
nullptr);
344 auto * child =
new Cnode(
'a');
345 root->insert_child(child);
347 EXPECT_EQ(
root->search_child(
'a'), child);
348 EXPECT_EQ(
root->search_child(
'b'),
nullptr);
357 auto kids =
root->children();
358 ASSERT_EQ(kids.size(), 3);
361 auto it = kids.get_it();
362 EXPECT_EQ(it.get_curr()->symbol(),
'a');
364 EXPECT_EQ(it.get_curr()->symbol(),
'b');
366 EXPECT_EQ(it.get_curr()->symbol(),
'c');
375 EXPECT_EQ(
root->greater_child(
'b')->symbol(),
'c');
376 EXPECT_EQ(
root->greater_child(
'd')->symbol(),
'e');
377 EXPECT_EQ(
root->greater_child(
'e'),
nullptr);
386 EXPECT_TRUE(
root->insert_word(
"hello"));
387 EXPECT_TRUE(
root->contains(
"hello"));
392 EXPECT_TRUE(
root->insert_word(
"hello"));
393 EXPECT_FALSE(
root->insert_word(
"hello"));
398 EXPECT_TRUE(
root->insert_word(
"hello"));
399 EXPECT_TRUE(
root->insert_word(
"help"));
400 EXPECT_TRUE(
root->insert_word(
"world"));
402 EXPECT_TRUE(
root->contains(
"hello"));
403 EXPECT_TRUE(
root->contains(
"help"));
404 EXPECT_TRUE(
root->contains(
"world"));
409 EXPECT_TRUE(
root->insert_word(
"test"));
410 EXPECT_TRUE(
root->insert_word(
"testing"));
411 EXPECT_TRUE(
root->insert_word(
"tester"));
413 EXPECT_TRUE(
root->contains(
"test"));
414 EXPECT_TRUE(
root->contains(
"testing"));
415 EXPECT_TRUE(
root->contains(
"tester"));
420 EXPECT_TRUE(
root->insert_word(
""));
421 EXPECT_TRUE(
root->contains(
""));
422 EXPECT_FALSE(
root->insert_word(
""));
427 EXPECT_TRUE(
root->insert_word(
"a"));
428 EXPECT_TRUE(
root->contains(
"a"));
433 EXPECT_TRUE(
root->insert_word(
"a"));
434 EXPECT_TRUE(
root->insert_word(
"a!"));
436 EXPECT_TRUE(
root->contains(
"a"));
437 EXPECT_TRUE(
root->contains(
"a!"));
438 EXPECT_EQ(
root->count(), 2);
442 (std::vector<std::string>{
"a",
"a!"}));
447#if ALEPH_PREFIX_TREE_TEST_UNDER_TSAN
448 GTEST_SKIP() <<
"AllocationFailureScope needs a custom global operator "
449 "new/delete, which conflicts with TSan's own at link time.";
451 EXPECT_TRUE(
root->insert_word(
"mango"));
457 AllocationFailureScope fail_after_one_node(2);
460 root->insert_word(
"abc");
462 catch (
const std::bad_alloc &)
466 balance = fail_after_one_node.balance();
470 EXPECT_EQ(balance, 0);
472 EXPECT_TRUE(
root->contains(
"mango"));
473 EXPECT_FALSE(
root->contains(
"a"));
474 EXPECT_FALSE(
root->contains(
"ab"));
475 EXPECT_FALSE(
root->contains(
"abc"));
476 EXPECT_EQ(
root->count(), 1);
481 EXPECT_TRUE(
root->insert_word(
"testing"));
483 EXPECT_TRUE(
root->contains(
"testing"));
484 EXPECT_FALSE(
root->contains(
"test"));
485 EXPECT_FALSE(
root->contains(
"tes"));
486 EXPECT_FALSE(
root->contains(
"te"));
487 EXPECT_FALSE(
root->contains(
"t"));
492 EXPECT_TRUE(
root->insert_word(
"test"));
493 EXPECT_TRUE(
root->insert_word(
"testing"));
495 EXPECT_TRUE(
root->contains(
"test"));
496 EXPECT_TRUE(
root->contains(
"testing"));
501 EXPECT_TRUE(
root->insert_word(
"testing"));
502 EXPECT_TRUE(
root->insert_word(
"test"));
504 EXPECT_TRUE(
root->contains(
"test"));
505 EXPECT_TRUE(
root->contains(
"testing"));
514 root->insert_word(
"hello");
516 EXPECT_EQ(
root->search_word(
"world"),
nullptr);
517 EXPECT_EQ(
root->search_word(
"hel"),
nullptr);
522 root->insert_word(
"hello");
524 auto result =
root->search_word(
"hello");
525 ASSERT_NE(result,
nullptr);
526 EXPECT_EQ(result->symbol(),
'o');
531 root->insert_word(
"hello");
533 EXPECT_FALSE(
root->contains(
"world"));
534 EXPECT_FALSE(
root->contains(
"helloworld"));
535 EXPECT_FALSE(
root->contains(
"hell"));
544 auto [node, remaining] =
root->search_prefix(
"");
546 EXPECT_EQ(node,
root);
547 EXPECT_STREQ(remaining,
"");
552 root->insert_word(
"hello");
554 auto [node, remaining] =
root->search_prefix(
"hel");
556 EXPECT_EQ(node->symbol(),
'l');
557 EXPECT_STREQ(remaining,
"");
562 root->insert_word(
"hello");
564 auto [node, remaining] =
root->search_prefix(
"helping");
566 EXPECT_EQ(node->symbol(),
'l');
567 EXPECT_STREQ(remaining,
"ping");
572 root->insert_word(
"hello");
574 auto [node, remaining] =
root->search_prefix(
"world");
576 EXPECT_EQ(node,
root);
577 EXPECT_STREQ(remaining,
"world");
586 auto words =
root->words();
587 EXPECT_TRUE(words.is_empty());
592 root->insert_word(
"hello");
594 auto words =
root->words();
595 ASSERT_EQ(words.size(), 1);
596 EXPECT_EQ(std::string(words[0]),
"hello");
601 root->insert_word(
"hello");
602 root->insert_word(
"help");
603 root->insert_word(
"world");
605 auto words =
root->words();
606 ASSERT_EQ(words.size(), 3);
609 std::vector<std::string> v;
610 words.for_each([&v](
const std::string&
w) { v.push_back(
w); });
611 std::sort(v.begin(), v.end());
613 EXPECT_EQ(v[0], std::string(
"hello"));
614 EXPECT_EQ(v[1], std::string(
"help"));
615 EXPECT_EQ(v[2], std::string(
"world"));
620 root->insert_word(
"a");
621 root->insert_word(
"ab");
622 root->insert_word(
"abc");
623 root->insert_word(
"abcd");
625 auto words =
root->words();
626 ASSERT_EQ(words.size(), 4);
631 EXPECT_TRUE(
root->insert_word(
"$"));
632 EXPECT_TRUE(
root->insert_word(
"a$"));
633 EXPECT_TRUE(
root->insert_word(
"a$b"));
635 EXPECT_TRUE(
root->contains(
"$"));
636 EXPECT_TRUE(
root->contains(
"a$"));
637 EXPECT_TRUE(
root->contains(
"a$b"));
640 (std::vector<std::string>{
"$",
"a$",
"a$b"}));
645 EXPECT_TRUE(
root->insert_word(
""));
646 EXPECT_TRUE(
root->insert_word(
"!"));
648 EXPECT_TRUE(
root->contains(
""));
649 EXPECT_TRUE(
root->contains(
"!"));
651 (std::vector<std::string>{
"",
"!"}));
663 EXPECT_TRUE(cloned->
children().is_empty());
672 root->insert_word(
"help");
673 root->insert_word(
"world");
678 EXPECT_TRUE(cloned->
contains(
"hello"));
679 EXPECT_TRUE(cloned->
contains(
"help"));
680 EXPECT_TRUE(cloned->
contains(
"world"));
683 root->insert_word(
"test");
684 EXPECT_TRUE(
root->contains(
"test"));
685 EXPECT_FALSE(cloned->
contains(
"test"));
694 root->insert_word(
"$");
695 root->insert_word(
"a!");
696 root->insert_word(
"a");
702 EXPECT_TRUE(cloned->
contains(
"a!"));
705 (std::vector<std::string>{
"",
"$",
"a",
"a!"}));
713#if ALEPH_PREFIX_TREE_TEST_UNDER_TSAN
714 GTEST_SKIP() <<
"AllocationFailureScope needs a custom global operator "
715 "new/delete, which conflicts with TSan's own at link time.";
718 root->insert_word(
"beta");
719 root->insert_word(
"$");
725 AllocationFailureScope fail_after_two_nodes(2);
732 catch (
const std::bad_alloc &)
736 balance = fail_after_two_nodes.balance();
740 EXPECT_EQ(balance, 0);
742 EXPECT_TRUE(
root->contains(
"alpha"));
743 EXPECT_TRUE(
root->contains(
"beta"));
744 EXPECT_TRUE(
root->contains(
"$"));
753 std::string str =
root->to_str();
755 EXPECT_FALSE(str.empty());
760 root->insert_word(
"ab");
761 std::string str =
root->to_str();
764 EXPECT_NE(str.find(
'a'), std::string::npos);
765 EXPECT_NE(str.find(
'b'), std::string::npos);
774 std::string longWord(20,
'a');
775 EXPECT_TRUE(
root->insert_word(longWord));
776 EXPECT_TRUE(
root->contains(longWord));
782 for (
int i = 0; i <
N; ++i)
783 root->insert_word(
"w" + std::to_string(i));
785 for (
int i = 0; i <
N; ++i)
786 EXPECT_TRUE(
root->contains(
"w" + std::to_string(i)));
788 auto words =
root->words();
789 EXPECT_EQ(words.size(),
N);
794 EXPECT_TRUE(
root->insert_word(
"hello-world"));
795 EXPECT_TRUE(
root->insert_word(
"test_case"));
796 EXPECT_TRUE(
root->insert_word(
"foo.bar"));
798 EXPECT_TRUE(
root->contains(
"hello-world"));
799 EXPECT_TRUE(
root->contains(
"test_case"));
800 EXPECT_TRUE(
root->contains(
"foo.bar"));
805 EXPECT_TRUE(
root->insert_word(
"123"));
806 EXPECT_TRUE(
root->insert_word(
"456"));
808 EXPECT_TRUE(
root->contains(
"123"));
809 EXPECT_TRUE(
root->contains(
"456"));
810 EXPECT_FALSE(
root->contains(
"789"));
819 EXPECT_EQ(
root->count(), 0);
824 root->insert_word(
"hello");
825 EXPECT_EQ(
root->count(), 1);
830 root->insert_word(
"hello");
831 root->insert_word(
"help");
832 root->insert_word(
"world");
833 EXPECT_EQ(
root->count(), 3);
838 root->insert_word(
"a");
839 root->insert_word(
"ab");
840 root->insert_word(
"abc");
841 EXPECT_EQ(
root->count(), 3);
850 root->insert_word(
"hello");
851 auto words =
root->words_with_prefix(
"xyz");
852 EXPECT_TRUE(words.is_empty());
857 root->insert_word(
"hello");
858 root->insert_word(
"help");
859 root->insert_word(
"helicopter");
860 root->insert_word(
"world");
862 auto words =
root->words_with_prefix(
"hel");
863 EXPECT_EQ(words.size(), 3);
868 root->insert_word(
"test");
869 root->insert_word(
"testing");
870 root->insert_word(
"tester");
872 const auto words =
root->words_with_prefix(
"test");
873 EXPECT_EQ(words.size(), 3);
878 root->insert_word(
"apple");
879 root->insert_word(
"application");
881 auto words =
root->words_with_prefix(
"ban");
882 EXPECT_TRUE(words.is_empty());
887 root->insert_word(
"");
888 root->insert_word(
"hello");
889 root->insert_word(
"help");
890 root->insert_word(
"world");
896TEST(PrefixTreeDestroyTest, DestroyWorks)
899 auto * tree =
new Cnode(
'\0');
900 tree->insert_word(
"hi");
901 tree->insert_word(
"bye");
903 EXPECT_TRUE(tree->contains(
"hi"));
904 EXPECT_TRUE(tree->contains(
"bye"));
916TEST(PrefixTreeWrapperTest, DelegatesWordOperations)
926 EXPECT_TRUE(tree.
contains(
"alpha"));
927 EXPECT_TRUE(tree.
contains(
"alphabet"));
928 EXPECT_FALSE(tree.
contains(
"alpine"));
929 EXPECT_EQ(tree.
count(), 3);
930 EXPECT_EQ(tree.
size(), 3);
932 (std::vector<std::string>{
"",
"alpha",
"alphabet"}));
934 (std::vector<std::string>{
"alpha",
"alphabet"}));
937TEST(PrefixTreeWrapperTest, CachedCountTracksWrapperInsertions)
941 EXPECT_EQ(tree.
count(), 0);
942 EXPECT_EQ(tree.
size(), 0);
946 EXPECT_EQ(tree.
count(), 2);
947 EXPECT_EQ(tree.
size(), 2);
950TEST(PrefixTreeWrapperTest, CachedCountRecomputesAfterMutableRootAccess)
954 EXPECT_EQ(tree.
count(), 1);
957 ASSERT_NE(
root,
nullptr);
958 EXPECT_TRUE(
root->insert_word(
"beta"));
959 EXPECT_TRUE(
root->insert_word(
"gamma"));
961 EXPECT_TRUE(tree.
contains(
"gamma"));
963 EXPECT_EQ(tree.
count(), 3);
964 EXPECT_EQ(tree.
size(), 3);
966 EXPECT_EQ(tree.
count(), 4);
967 EXPECT_TRUE(
root->insert_word(
"epsilon"));
968 EXPECT_EQ(tree.
count(), 5);
969 EXPECT_EQ(tree.
size(), 5);
972TEST(PrefixTreeWrapperTest, OwnsRootAndDestroysNodes)
974#if ALEPH_PREFIX_TREE_TEST_UNDER_TSAN
975 GTEST_SKIP() <<
"AllocationFailureScope needs a custom global operator "
976 "new/delete, which conflicts with TSan's own at link time.";
981 AllocationFailureScope track_only(-1);
988 balance = track_only.balance();
992 EXPECT_EQ(balance, 0);
995TEST(PrefixTreeWrapperTest, RootExposesLowLevelAccessWithoutOwnershipTransfer)
1000 ASSERT_NE(tree.
root(),
nullptr);
1002 EXPECT_EQ(const_tree.
root(), tree.
root());
1008TEST(PrefixTreeWrapperTest, CopyConstructorCreatesIndependentTree)
1016 copy.insert_word(
"gamma");
1018 EXPECT_TRUE(
copy.contains(
"alpha"));
1019 EXPECT_TRUE(
copy.contains(
"$"));
1020 EXPECT_TRUE(
copy.contains(
"gamma"));
1021 EXPECT_FALSE(
copy.contains(
"beta"));
1022 EXPECT_TRUE(tree.
contains(
"beta"));
1023 EXPECT_FALSE(tree.
contains(
"gamma"));
1024 EXPECT_EQ(tree.
count(), 3);
1025 EXPECT_EQ(
copy.count(), 3);
1028TEST(PrefixTreeWrapperTest, CopyAssignmentCreatesIndependentTree)
1031 source.insert_word(
"alpha");
1032 source.insert_word(
"beta");
1038 source.insert_word(
"gamma");
1041 EXPECT_TRUE(target.
contains(
"alpha"));
1042 EXPECT_TRUE(target.
contains(
"beta"));
1043 EXPECT_TRUE(target.
contains(
"delta"));
1044 EXPECT_FALSE(target.
contains(
"old"));
1045 EXPECT_FALSE(target.
contains(
"gamma"));
1046 EXPECT_TRUE(source.contains(
"gamma"));
1049 EXPECT_TRUE(target.
contains(
"alpha"));
1050 EXPECT_TRUE(target.
contains(
"delta"));
1053TEST(PrefixTreeWrapperTest, MoveConstructorTransfersOwnership)
1056 source.insert_word(
"alpha");
1057 source.insert_word(
"$");
1061 EXPECT_TRUE(moved.
contains(
"alpha"));
1063 EXPECT_EQ(moved.
count(), 2);
1065 EXPECT_EQ(source.count(), 0);
1066 EXPECT_FALSE(source.contains(
"alpha"));
1067 EXPECT_TRUE(source.words().is_empty());
1068 EXPECT_TRUE(source.words_with_prefix(
"a").is_empty());
1069 EXPECT_TRUE(source.insert_word(
"reused"));
1070 EXPECT_TRUE(source.contains(
"reused"));
1071 EXPECT_EQ(source.count(), 1);
1074TEST(PrefixTreeWrapperTest, MoveAssignmentTransfersOwnershipAndFreesOldRoot)
1077 source.insert_word(
"alpha");
1078 source.insert_word(
"beta");
1083 target = std::move(source);
1085 EXPECT_TRUE(target.
contains(
"alpha"));
1086 EXPECT_TRUE(target.
contains(
"beta"));
1087 EXPECT_FALSE(target.
contains(
"old"));
1088 EXPECT_EQ(target.
count(), 2);
1090 EXPECT_EQ(source.count(), 0);
1091 EXPECT_FALSE(source.contains(
"alpha"));
1092 EXPECT_TRUE(source.words().is_empty());
1093 EXPECT_TRUE(source.words_with_prefix(
"a").is_empty());
1094 EXPECT_TRUE(source.insert_word(
"reused"));
1095 EXPECT_TRUE(source.contains(
"reused"));
1096 EXPECT_EQ(source.count(), 1);
1099TEST(PrefixTreeWrapperTest, CopyAssignmentKeepsOldTreeOnAllocationFailure)
1101#if ALEPH_PREFIX_TREE_TEST_UNDER_TSAN
1102 GTEST_SKIP() <<
"AllocationFailureScope needs a custom global operator "
1103 "new/delete, which conflicts with TSan's own at link time.";
1106 source.insert_word(
"alpha");
1107 source.insert_word(
"beta");
1108 source.insert_word(
"$");
1117 AllocationFailureScope fail_after_two_nodes(2);
1122 catch (
const std::bad_alloc &)
1126 balance = fail_after_two_nodes.balance();
1130 EXPECT_EQ(balance, 0);
1132 EXPECT_TRUE(target.
contains(
"old"));
1133 EXPECT_FALSE(target.
contains(
"alpha"));
1134 EXPECT_FALSE(target.
contains(
"beta"));
1135 EXPECT_FALSE(target.
contains(
"$"));
1142TEST(PrefixTreeMapTest, DefaultConstructedMapIsEmpty)
1147 EXPECT_EQ(map.
size(), 0);
1148 EXPECT_EQ(map.
count(), 0);
1149 EXPECT_FALSE(map.
contains(
"alpha"));
1150 EXPECT_EQ(map.
find(
"alpha"),
nullptr);
1151 EXPECT_TRUE(map.
words().is_empty());
1154TEST(PrefixTreeMapTest, InsertAndFindValues)
1158 EXPECT_TRUE(map.
insert(
"", 0));
1159 EXPECT_TRUE(map.
insert(
"alpha", 1));
1160 EXPECT_TRUE(map.
insert(
"alphabet", 2));
1162 ASSERT_NE(map.
find(
""),
nullptr);
1163 ASSERT_NE(map.
find(
"alpha"),
nullptr);
1164 ASSERT_NE(map.
find(
"alphabet"),
nullptr);
1165 EXPECT_EQ(*map.
find(
""), 0);
1166 EXPECT_EQ(*map.
find(
"alpha"), 1);
1167 EXPECT_EQ(*map.
find(
"alphabet"), 2);
1169 EXPECT_TRUE(map.
contains(
"alpha"));
1170 EXPECT_FALSE(map.
contains(
"alpine"));
1171 EXPECT_EQ(map.
size(), 3);
1174TEST(PrefixTreeMapTest, DuplicateInsertKeepsOriginalValue)
1178 EXPECT_TRUE(map.
insert(
"alpha", 1));
1179 EXPECT_FALSE(map.
insert(
"alpha", 99));
1180 ASSERT_NE(map.
find(
"alpha"),
nullptr);
1181 EXPECT_EQ(*map.
find(
"alpha"), 1);
1182 EXPECT_EQ(map.
size(), 1);
1185TEST(PrefixTreeMapTest, DuplicateMoveInsertDoesNotConsumeValue)
1188 ASSERT_TRUE(map.
insert(
"alpha", std::make_unique<int>(1)));
1190 auto duplicate = std::make_unique<int>(2);
1191 EXPECT_FALSE(map.
insert(
"alpha", std::move(duplicate)));
1192 ASSERT_NE(duplicate,
nullptr);
1193 EXPECT_EQ(*duplicate, 2);
1194 ASSERT_NE(map.
find(
"alpha"),
nullptr);
1195 ASSERT_NE(*map.
find(
"alpha"),
nullptr);
1196 EXPECT_EQ(**map.
find(
"alpha"), 1);
1199TEST(PrefixTreeMapTest, InsertOrAssignInsertsThenOverwrites)
1204 ASSERT_NE(map.
find(
"alpha"),
nullptr);
1205 EXPECT_EQ(*map.
find(
"alpha"),
"one");
1208 ASSERT_NE(map.
find(
"alpha"),
nullptr);
1209 EXPECT_EQ(*map.
find(
"alpha"),
"uno");
1212 ASSERT_NE(map.
find(
"beta"),
nullptr);
1213 EXPECT_EQ(*map.
find(
"beta"),
"two");
1214 EXPECT_EQ(map.
size(), 2);
1217TEST(PrefixTreeMapTest, MutableFindAllowsInPlaceUpdate)
1220 ASSERT_TRUE(map.
insert(
"alpha", 1));
1222 int *slot = map.
find(
"alpha");
1223 ASSERT_NE(slot,
nullptr);
1226 const auto &const_map = map;
1227 ASSERT_NE(const_map.find(
"alpha"),
nullptr);
1228 EXPECT_EQ(*const_map.find(
"alpha"), 42);
1231TEST(PrefixTreeMapTest, EraseRemovesLogicalKeyAndAllowsReinsert)
1234 ASSERT_TRUE(map.
insert(
"alpha", 1));
1235 ASSERT_TRUE(map.
insert(
"alphabet", 2));
1237 EXPECT_TRUE(map.
erase(
"alpha"));
1238 EXPECT_FALSE(map.
contains(
"alpha"));
1239 EXPECT_TRUE(map.
contains(
"alphabet"));
1240 EXPECT_FALSE(map.
erase(
"alpha"));
1241 EXPECT_EQ(map.
size(), 1);
1243 EXPECT_TRUE(map.
insert(
"alpha", 3));
1244 ASSERT_NE(map.
find(
"alpha"),
nullptr);
1245 EXPECT_EQ(*map.
find(
"alpha"), 3);
1246 EXPECT_EQ(map.
size(), 2);
1249TEST(PrefixTreeMapTest, WordsAndWordsWithPrefixReturnStoredKeys)
1252 ASSERT_TRUE(map.
insert(
"", 0));
1253 ASSERT_TRUE(map.
insert(
"app", 1));
1254 ASSERT_TRUE(map.
insert(
"apple", 2));
1255 ASSERT_TRUE(map.
insert(
"banana", 3));
1258 (std::vector<std::string>{
"",
"app",
"apple",
"banana"}));
1260 (std::vector<std::string>{
"app",
"apple"}));
1264TEST(PrefixTreeMapTest, CopyConstructorCreatesIndependentClone)
1267 ASSERT_TRUE(map.
insert(
"alpha",
"one"));
1268 ASSERT_TRUE(map.
insert(
"beta",
"two"));
1271 copy.insert_or_assign(
"alpha",
"uno");
1272 copy.insert_or_assign(
"gamma",
"three");
1274 ASSERT_NE(map.
find(
"alpha"),
nullptr);
1275 ASSERT_NE(
copy.find(
"alpha"),
nullptr);
1276 EXPECT_EQ(*map.
find(
"alpha"),
"one");
1277 EXPECT_EQ(*
copy.find(
"alpha"),
"uno");
1278 EXPECT_FALSE(map.
contains(
"gamma"));
1279 EXPECT_TRUE(
copy.contains(
"gamma"));
1280 EXPECT_EQ(map.
size(), 2);
1281 EXPECT_EQ(
copy.size(), 3);
1284TEST(PrefixTreeMapTest, MoveConstructorTransfersContents)
1287 ASSERT_TRUE(source.insert(
"alpha", 1));
1288 ASSERT_TRUE(source.insert(
"beta", 2));
1292 EXPECT_TRUE(source.is_empty());
1293 EXPECT_TRUE(moved.
contains(
"alpha"));
1294 EXPECT_TRUE(moved.
contains(
"beta"));
1295 ASSERT_NE(moved.
find(
"alpha"),
nullptr);
1296 ASSERT_NE(moved.
find(
"beta"),
nullptr);
1297 EXPECT_EQ(*moved.
find(
"alpha"), 1);
1298 EXPECT_EQ(*moved.
find(
"beta"), 2);
1301TEST(PrefixTreeMapTest, ClearRemovesAllKeys)
1304 ASSERT_TRUE(map.
insert(
"alpha", 1));
1305 ASSERT_TRUE(map.
insert(
"beta", 2));
1310 EXPECT_FALSE(map.
contains(
"alpha"));
1311 EXPECT_FALSE(map.
contains(
"beta"));
1312 EXPECT_TRUE(map.
words().is_empty());
1315TEST(PrefixTreeMapTest, RandomizedOperationsMatchStdMap)
1317 const std::vector<std::string>
keys =
1319 "",
"a",
"app",
"apple",
"application",
"banana",
"band",
"bandana",
1320 "car",
"carbon",
"cart",
"dog",
"door",
"dorm"
1324 std::map<std::string, int> reference;
1326 for (
size_t step = 0; step < 500; ++step)
1328 const std::string &key =
keys[(step * 7 + 3) %
keys.size()];
1329 const int value =
static_cast<int>(step);
1331 if ((step % 4) == 0)
1333 reference.emplace(key,
value).second);
1334 else if ((step % 4) == 1)
1337 reference[key] =
value;
1339 else if ((step % 4) == 2)
1340 EXPECT_EQ(subject.
erase(key), reference.erase(key) != 0);
1343 const int *subject_value = subject.
find(key);
1344 const auto it = reference.find(key);
1345 ASSERT_EQ(subject_value !=
nullptr, it != reference.end());
1346 if (subject_value !=
nullptr)
1347 EXPECT_EQ(*subject_value, it->second);
1350 ASSERT_EQ(subject.
size(), reference.size()) <<
"step " << step;
1354 for (
const auto &kv : reference)
1357 for (
const auto &kv : reference)
1359 ASSERT_NE(subject.
find(kv.first),
nullptr);
1360 EXPECT_EQ(*subject.
find(kv.first), kv.second);
1364TEST(PrefixTreeMapTest, MatchesPrefixTreeAndRadixTree)
1366 const std::vector<std::string> words =
1368 "",
"app",
"apple",
"application",
"apt",
"banana",
"band",
1369 "bandana",
"car",
"carbon",
"cart"
1376 for (
size_t i = 0; i < words.size(); ++i)
1378 const int value =
static_cast<int>(i * 10);
1380 ASSERT_TRUE(reference_map.
insert(words[i],
value));
1383 for (
size_t i = 0; i < words.size(); ++i)
1385 ASSERT_TRUE(subject.
contains(words[i]));
1386 ASSERT_TRUE(reference_set.
contains(words[i]));
1387 ASSERT_NE(subject.
find(words[i]),
nullptr);
1388 ASSERT_NE(reference_map.
find(words[i]),
nullptr);
1389 EXPECT_EQ(*subject.
find(words[i]), *reference_map.
find(words[i]));
1392 EXPECT_EQ(subject.
size(), reference_set.
size());
1396 for (
const auto &
prefix : {
"",
"app",
"ban",
"car",
"z"})
1400 <<
"Prefix_Tree disagreement for prefix " <<
prefix;
1403 <<
"RadixTree disagreement for prefix " <<
prefix;
1413 ::testing::InitGoogleTest(&argc, argv);
1414 return RUN_ALL_TESTS();
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Low-level prefix tree node for storing character sequences.
static void clone(const Tree_Node< char > *src, Tree_Node< char > *tgt)
Clone helper - copies children from src to tgt.
bool contains(const std::string &word) const noexcept
Check if a word exists in the tree.
void destroy() noexcept
Destroy all children of this node.
void mark_end_word()
Mark this node as the end of a word.
bool is_end_word() const noexcept
Check if this node marks the end of a word.
char symbol() const noexcept
Return the character stored in this node.
bool insert_word(const std::string &word)
Insert a word into the tree.
DynArray< std::string > words(size_t max_word_length=2048) const
Get all words stored in this subtree.
DynList< Cnode * > children() const
Return a list of all child nodes.
Number Theoretic Transform over Z / MOD Z.
Owning prefix tree map from strings to values.
DynArray< Key > words(const size_t max_word_length=2048) const
Get all keys stored in the map.
size_t count() const noexcept
Return the number of stored key-value pairs.
bool contains(const Key &key) const noexcept
Check whether key is stored.
const Value * find(const Key &key) const noexcept
Look up key.
bool insert(const Key &key, const Value &value)
Insert key with a copied value if absent.
DynArray< Key > words_with_prefix(const Key &prefix, const size_t max_word_length=2048) const
Get all keys with a given prefix.
void insert_or_assign(const Key &key, Value value)
Insert key or overwrite its mapped value.
bool erase(const Key &key) noexcept
Remove key if present.
void clear()
Remove every key-value pair from the map.
size_t size() const noexcept
Return the number of stored key-value pairs.
bool is_empty() const noexcept
Check whether the map has no keys.
Owning prefix tree wrapper.
bool insert_word(const std::string &word)
Insert a word into the tree.
DynArray< std::string > words(const size_t max_word_length=2048) const
Get all words stored in the tree.
DynArray< std::string > words_with_prefix(const std::string &prefix, const size_t max_word_length=2048) const
Get all words with a given prefix.
Cnode * root() noexcept
Return the mutable root node.
size_t count() const noexcept
Count the words stored in the tree.
bool contains(const std::string &word) const noexcept
Check whether a word exists in the tree.
size_t size() const noexcept
Return the number of words stored in the tree.
Compressed prefix tree mapping std::basic_string<Char> keys to values of type T.
const T * find(const Key &key) const noexcept
Look up key.
bool insert(const Key &key, const T &value)
Insert key with a copy of value, only if key is absent.
Array< Key > keys_with_prefix(const Key &prefix) const
Return every stored key that starts with prefix.
Minimal std::expected-style result type for C++20.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
static void prefix(Node *root, DynList< Node * > &acc)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Trie (prefix tree) implementation.
static std::vector< std::string > to_sorted_vector(const DynArray< std::string > &words)
TEST_F(PrefixTreeTest, NodeConstruction)
Compressed prefix tree (Aleph::RadixTree) mapping string keys to values.