58#include <gtest/gtest-spi.h>
77 struct Comparison_Error {};
83 static inline long calls = 0;
84 static inline long fail_at = 0;
86 bool operator ()(
const int a,
const int b)
const
88 if (fail_at != 0
and ++calls == fail_at)
89 throw Comparison_Error();
97 explicit Failing_At(
const long k)
noexcept
99 Countdown_Less::calls = 0;
100 Countdown_Less::fail_at =
k;
103 ~Failing_At() { Countdown_Less::fail_at = 0; }
108 constexpr long Max_Calls = 1000;
112 constexpr int Num_Keys = 24;
119 for (
int i = 0; i < Num_Keys; ++i)
126 Array<int> keys_with(
const int added = INT_MIN,
const int removed = INT_MIN)
129 bool pending = added != INT_MIN;
130 for (
int k = 0;
k < 2 * Num_Keys;
k += 2)
132 if (pending
and added <
k)
145 bool is_present(
const int key)
noexcept
147 return key >= 0
and key < 2 * Num_Keys
and key % 2 == 0;
151 long rank_of(
const int key)
noexcept {
return key / 2; }
158 for (
int k = -1;
k < 2 * Num_Keys; ++
k)
163 template <
class Node>
166 if (p == Node::NullPtr)
174 template <
class Node>
182 template <
class Node>
185 if (p == Node::NullPtr)
191 if constexpr (Colored_Node<Node>)
192 state.
append(p->getColor());
193 if constexpr (Avl_Balanced_Node<Node>)
194 state.
append(p->getDiff());
195 if constexpr (Counted_Node<Node>)
196 state.
append(
static_cast<long>(p->getCount()));
197 if constexpr (Prioritized_Node<Node>)
198 state.
append(
static_cast<long>(p->getPriority()));
199 collect_state(
LLINK(p), state);
200 collect_state(
RLINK(p), state);
205 template <
class Node>
209 collect_state(
root, state);
217 long restructured = 0;
227 template <
class Fixture,
class Op,
class Done>
228 void fail_at_every_call(Op op, Done done, Failures & failures)
230 for (
long k = 1;
k <= Max_Calls; ++
k)
232 SCOPED_TRACE(
"the comparison throws at its call " + std::to_string(
k));
234 const auto before = f.state();
237 const Failing_At failing(
k);
242 catch (
const Comparison_Error &)
251 if (Countdown_Less::calls >=
k)
253 ADD_FAILURE() <<
"the comparison threw, but the exception did not reach the caller";
262 if (f.state() != before)
263 ++failures.restructured;
266 ADD_FAILURE() <<
"the operation made more than " << Max_Calls <<
" comparisons";
270 template <
class Tree>
280 if constexpr (Seeded_Tree<Tree>)
282 for (
const int key : insertion_order())
283 EXPECT_NE(tree.
insert(pool.make(key)), nullptr);
286 typename Tree::Node * node(
const int key) {
return pool.
make(key); }
288 [[nodiscard]]
Array<long> state() {
return state_of(tree.getRoot()); }
293 EXPECT_TRUE(t.verify());
294 EXPECT_EQ(keys_of(t.getRoot()),
keys);
295 if constexpr (
requires { t.size(); })
297 EXPECT_EQ(t.size(),
keys.size());
301 void expect_initial() { expect_valid(tree, keys_with()); }
312 for (
const int key : insertion_order())
313 EXPECT_NE(set.
insert(key), nullptr);
316 [[nodiscard]]
Array<long> state() {
return state_of(set.get_root_node()); }
320 EXPECT_TRUE(s.verify());
321 EXPECT_EQ(s.size(),
keys.size());
324 EXPECT_EQ(got,
keys);
327 void expect_initial() { expect_valid(set, keys_with()); }
338 for (
const int key : insertion_order())
339 EXPECT_NE(map.
insert(key, -key), nullptr);
345 map.
for_each([&state] (
const std::pair<int, int> & p)
355 EXPECT_TRUE(
m.verify());
358 m.
for_each([&got] (
const std::pair<int, int> & p)
360 EXPECT_EQ(p.second, -p.first);
363 EXPECT_EQ(got,
keys);
366 void expect_initial() { expect_valid(map, keys_with()); }
374# define TREES_UNDER_TEST(X) \
375 X(BinTree, false, true) \
376 X(Avl_Tree, false, true) \
377 X(Avl_Tree_Rk, false, true) \
378 X(Rb_Tree, false, true) \
379 X(Rb_Tree_Rk, false, false) \
380 X(TdRbTree, true, true) \
381 X(TdRbTreeRk, true, true) \
382 X(HtdRbTree, true, true) \
383 X(HtdRbTreeRk, true, true) \
384 X(Treap, false, true) \
385 X(Treap_Rk, false, true) \
386 X(Rand_Tree, false, true)
390# define DECLARE_TREE_KIND(T, Restructures, Noexcept) \
393 using Tree = T<int, Countdown_Less>; \
394 using Nothrow_Tree = T<int>; \
395 using Set = DynSetTree<int, T, Countdown_Less>; \
396 using Map = DynMapTree<int, int, T, Countdown_Less>; \
397 template <class Cmp> using Tree_With = T<int, Cmp>; \
398 template <class Cmp> \
399 using Set_With = DynSetTree<int, T, Cmp>; \
400 template <class Cmp> \
401 using Map_With = DynMapTree<int, int, T, Cmp>; \
402 static constexpr bool restructures = Restructures; \
403 static constexpr bool noexcept_ops = Noexcept; \
404 static constexpr const char * name = #T; \
409 template <
class First,
class... Kinds>
412 using type = ::testing::Types<Kinds...>;
415# define TREE_KIND_ENTRY(T, Restructures, Noexcept) , T##_Kind
419# undef TREE_KIND_ENTRY
420# undef DECLARE_TREE_KIND
421# undef TREES_UNDER_TEST
425 template <
class Kind>
426 static std::string GetName(
int)
432 std::string trace_of(
const char * op,
const int key)
434 return std::string(op) +
"(" + std::to_string(key) +
")";
442TEST(ThrowingCompareHarness, AnExceptionThatTheOperationCatchesFailsTheTest)
444 using Fixture = Tree_Fixture<Avl_Tree<int, Countdown_Less>>;
446 EXPECT_NONFATAL_FAILURE(fail_at_every_call<Fixture>(
451 (void) f.tree.search(4);
453 catch (
const Comparison_Error &)
455 (void) f.tree.search(4);
458 [] (Fixture & f) { f.expect_initial(); }, failures),
459 "did not reach the caller");
460 EXPECT_EQ(failures.thrown, 0);
472 using Nothrow =
typename TypeParam::Nothrow_Tree;
473 using Node =
typename Nothrow::Node;
474 constexpr bool expected = TypeParam::noexcept_ops;
475 EXPECT_EQ(
noexcept(std::declval<Nothrow &>().insert(std::declval<Node *>())),
expected);
476 EXPECT_EQ(
noexcept(std::declval<Nothrow &>().insert_dup(std::declval<Node *>())),
expected);
477 EXPECT_EQ(
noexcept(std::declval<Nothrow &>().search_or_insert(std::declval<Node *>())),
479 EXPECT_EQ(
noexcept(std::declval<Nothrow &>().
search(1)),
expected);
480 EXPECT_EQ(
noexcept(std::declval<Nothrow &>().
remove(1)),
expected);
482 using Tree =
typename TypeParam::Tree;
484 EXPECT_FALSE(
noexcept(std::declval<Tree &>().insert(std::declval<Tree_Node *>())));
485 EXPECT_FALSE(
noexcept(std::declval<Tree &>().insert_dup(std::declval<Tree_Node *>())));
486 EXPECT_FALSE(
noexcept(std::declval<Tree &>().search_or_insert(std::declval<Tree_Node *>())));
487 EXPECT_FALSE(
noexcept(std::declval<Tree &>().
search(1)));
488 EXPECT_FALSE(
noexcept(std::declval<Tree &>().
remove(1)));
493 using Tree =
typename TypeParam::Tree;
494 using Fixture = Tree_Fixture<Tree>;
498 for (
const int key : target_keys())
500 const bool present = is_present(key);
501 Node * got =
nullptr;
504 SCOPED_TRACE(trace_of(
"insert", key));
505 fail_at_every_call<Fixture>(
506 [&] (Fixture & f) { got = f.tree.insert(p = f.node(key)); },
509 EXPECT_EQ(got, present ?
nullptr : p);
510 Fixture::expect_valid(f.tree, present ? keys_with() : keys_with(key));
514 SCOPED_TRACE(trace_of(
"insert_dup", key));
515 fail_at_every_call<Fixture>(
516 [&] (Fixture & f) { got = f.tree.insert_dup(p = f.node(key)); },
520 Fixture::expect_valid(f.tree, keys_with(key));
524 SCOPED_TRACE(trace_of(
"search_or_insert", key));
525 fail_at_every_call<Fixture>(
526 [&] (Fixture & f) { got = f.tree.search_or_insert(p = f.node(key)); },
529 ASSERT_NE(got,
nullptr);
530 EXPECT_EQ(
KEY(got), key);
531 EXPECT_EQ(got == p, not present);
532 Fixture::expect_valid(f.tree, present ? keys_with() : keys_with(key));
537 EXPECT_GT(failures.thrown, 0);
541 if constexpr (TypeParam::restructures)
543 EXPECT_GT(failures.restructured, 0);
547 EXPECT_EQ(failures.restructured, 0);
553 using Tree =
typename TypeParam::Tree;
554 using Fixture = Tree_Fixture<Tree>;
558 for (
const int key : target_keys())
560 const bool present = is_present(key);
562 SCOPED_TRACE(trace_of(
"search", key));
563 Node * got =
nullptr;
564 fail_at_every_call<Fixture>(
565 [&] (Fixture & f) { got = f.tree.search(key); },
568 EXPECT_EQ(got !=
nullptr, present);
571 EXPECT_EQ(
KEY(got), key);
577 if constexpr (
requires (
Tree & t,
const int k) { t.position(
k); t.find_position(
k); })
579 SCOPED_TRACE(trace_of(
"position", key));
581 fail_at_every_call<Fixture>(
582 [&] (Fixture & f) { pos = f.tree.position(key).first; },
585 EXPECT_EQ(pos, present ? rank_of(key) : -1);
588 fail_at_every_call<Fixture>(
589 [&] (Fixture & f) { pos = f.tree.find_position(key).first; },
594 EXPECT_EQ(pos, rank_of(key));
601 EXPECT_GT(failures.thrown, 0);
602 EXPECT_EQ(failures.restructured, 0);
607 using Tree =
typename TypeParam::Tree;
608 using Fixture = Tree_Fixture<Tree>;
612 for (
const int key : target_keys())
614 SCOPED_TRACE(trace_of(
"remove", key));
615 Node * got =
nullptr;
616 fail_at_every_call<Fixture>(
617 [&] (Fixture & f) { got = f.tree.remove(key); },
620 EXPECT_EQ(got !=
nullptr, is_present(key));
623 EXPECT_EQ(
KEY(got), key);
625 Fixture::expect_valid(f.tree, keys_with(INT_MIN, key));
629 EXPECT_GT(failures.thrown, 0);
630 if constexpr (not TypeParam::restructures)
632 EXPECT_EQ(failures.restructured, 0);
640 template <
class Tree>
649 template <
class Tree>
655 if constexpr (not
noexcept(std::declval<Tree &>().split_key(1, std::declval<Tree &>(),
656 std::declval<Tree &>())))
660 return t.split_key(
k,
l,
r);
664 t.split_key_dup(
k,
l,
r);
677 t.split_dup(
k,
l,
r);
686 template <
class Tree>
687 struct Split_Fixture : Tree_Fixture<Tree>
691 void expect_initial()
693 Tree_Fixture<Tree>::expect_initial();
694 EXPECT_EQ(
l.getRoot(), Tree::Node::NullPtr);
695 EXPECT_EQ(
r.getRoot(), Tree::Node::NullPtr);
701 template <
class Node>
702 void expect_split_by(
Node *
l,
Node *
r,
const int key)
706 for (
const int k :
keys)
708 for (
const int k : right)
710 for (
const int k : right)
712 EXPECT_EQ(
keys, keys_with());
718 using Tree =
typename TypeParam::Tree;
719 using Fixture = Split_Fixture<Tree>;
722 for (
const Split<Tree> & s : splits_that_may_throw<Tree>())
723 for (
const int key : target_keys())
725 SCOPED_TRACE(trace_of(s.name, key));
727 fail_at_every_call<Fixture>(
728 [&] (Fixture & f) {
split = s.split(f.tree, key, f.l, f.r); },
733 EXPECT_TRUE(is_present(key));
737 EXPECT_EQ(f.tree.getRoot(), Tree::Node::NullPtr);
738 EXPECT_TRUE(f.l.verify());
739 EXPECT_TRUE(f.r.verify());
740 expect_split_by(f.l.getRoot(), f.r.getRoot(), key);
744 EXPECT_EQ(failures.restructured, 0);
756 using Set =
typename TypeParam::Set;
757 using Fixture = Set_Fixture<Set>;
758 using Tree =
typename TypeParam::Tree;
761 for (
const int key : target_keys())
763 const bool present = is_present(key);
766 SCOPED_TRACE(trace_of(
"insert", key));
767 fail_at_every_call<Fixture>(
768 [&] (Fixture & f) { got = f.set.insert(key); },
771 EXPECT_EQ(got ==
nullptr, present);
772 Fixture::expect_valid(f.set, present ? keys_with() : keys_with(key));
776 SCOPED_TRACE(trace_of(
"insert_dup", key));
777 fail_at_every_call<Fixture>(
778 [&] (Fixture & f) { got = f.set.insert_dup(key); },
781 EXPECT_NE(got,
nullptr);
782 Fixture::expect_valid(f.set, keys_with(key));
786 SCOPED_TRACE(trace_of(
"search_or_insert", key));
787 fail_at_every_call<Fixture>(
788 [&] (Fixture & f) { got = f.set.search_or_insert(key); },
791 ASSERT_NE(got,
nullptr);
792 EXPECT_EQ(*got, key);
793 Fixture::expect_valid(f.set, present ? keys_with() : keys_with(key));
797 SCOPED_TRACE(trace_of(
"search", key));
798 fail_at_every_call<Fixture>(
799 [&] (Fixture & f) { got = f.set.search(key); },
802 EXPECT_EQ(got !=
nullptr, present);
807 SCOPED_TRACE(trace_of(
"find", key));
809 bool refused =
false;
810 fail_at_every_call<Fixture>(
816 found = f.set.find(key) == key;
818 catch (
const std::domain_error &)
826 EXPECT_EQ(found, present);
827 EXPECT_EQ(refused, not present)
828 <<
"find() throws std::domain_error for a key not in the set, alone";
833 SCOPED_TRACE(trace_of(
"remove", key));
835 fail_at_every_call<Fixture>(
836 [&] (Fixture & f) {
size = f.set.remove(key); },
839 EXPECT_EQ(
size, present ? Num_Keys - 1 : Num_Keys);
840 Fixture::expect_valid(f.set, keys_with(INT_MIN, key));
844 if constexpr (
requires (
Tree & t,
const int k) { t.position(
k); })
846 SCOPED_TRACE(trace_of(
"position", key));
848 fail_at_every_call<Fixture>(
849 [&] (Fixture & f) { pos = f.set.position(key); },
852 EXPECT_EQ(pos, present ? rank_of(key) : -1);
858 EXPECT_GT(failures.thrown, 0);
868 using Map =
typename TypeParam::Map;
869 using Fixture = Map_Fixture<Map>;
872 for (
const int key : target_keys())
874 const bool present = is_present(key);
876 SCOPED_TRACE(trace_of(
"insert", key));
877 std::pair<int, int> * got =
nullptr;
878 fail_at_every_call<Fixture>(
879 [&] (Fixture & f) { got = f.map.insert(key, -key); },
882 EXPECT_EQ(got ==
nullptr, present);
883 Fixture::expect_valid(f.map, present ? keys_with() : keys_with(key));
887 SCOPED_TRACE(trace_of(
"operator[]", key));
888 fail_at_every_call<Fixture>(
889 [&] (Fixture & f) { f.map[key] = -key; },
892 Fixture::expect_valid(f.map, present ? keys_with() : keys_with(key));
896 SCOPED_TRACE(trace_of(
"find", key));
898 bool refused =
false;
899 fail_at_every_call<Fixture>(
905 found = f.map.find(key) == -key;
907 catch (
const std::domain_error &)
915 EXPECT_EQ(found, present);
916 EXPECT_EQ(refused, not present)
917 <<
"find() throws std::domain_error for a key not in the map, alone";
922 SCOPED_TRACE(trace_of(
"remove", key));
923 bool removed =
false;
924 bool refused =
false;
925 fail_at_every_call<Fixture>(
931 removed = f.map.remove(key) == -key;
933 catch (
const std::domain_error &)
941 EXPECT_EQ(removed, present);
942 EXPECT_EQ(refused, not present)
943 <<
"remove() throws std::domain_error for a key not in the map, alone";
944 Fixture::expect_valid(f.map, keys_with(INT_MIN, key));
952 SCOPED_TRACE(trace_of(
"search", key));
953 EXPECT_FALSE(
noexcept(std::declval<Map &>().
search(key)))
954 <<
"search() is noexcept, but the comparison may throw";
955 if constexpr (not
noexcept(std::declval<Map &>().search(key)))
958 fail_at_every_call<Fixture>(
959 [&] (Fixture & f) { found = f.map.search(key) !=
nullptr; },
962 EXPECT_EQ(found, present);
969 EXPECT_GT(failures.thrown, 0);
976 template <
class Node,
class Ops>
985 for (
const int key : insertion_order())
987 if constexpr (Counted_Node<Node>)
993 if (p == Node::NullPtr)
998 Node * node(
const int key) {
return pool.
make(key); }
1000 [[nodiscard]]
Array<long> state() {
return state_of(root); }
1005 if constexpr (Counted_Node<Node>)
1009 EXPECT_EQ(keys_of(
r),
keys);
1012 void expect_initial() { expect_valid(root, keys_with()); }
1018TEST(ThrowingCompareOperations, BinTreeOperationLeavesTheTreeAsItWas)
1021 using Fixture = Ops_Fixture<Node, BinTree_Operation<Node, Countdown_Less>>;
1024 for (
const int key : target_keys())
1026 const bool present = is_present(key);
1027 SCOPED_TRACE(trace_of(
"the operations at", key));
1028 Node * got =
nullptr;
1030 const Array<int> keys_after_insert = present ? keys_with() : keys_with(key);
1032 fail_at_every_call<Fixture>(
1033 [&] (Fixture & f) { got = f.ops.search(f.root, key); },
1036 EXPECT_EQ(got != Node::NullPtr, present);
1040 fail_at_every_call<Fixture>(
1044 RLINK(&head) = f.root;
1045 Node * parent = &head;
1046 got = f.ops.search_parent(f.root, key, parent);
1050 EXPECT_EQ(
KEY(got) == key, present);
1054 fail_at_every_call<Fixture>(
1055 [&] (Fixture & f) { got = f.ops.search_rank_parent(f.root, key); },
1058 EXPECT_EQ(
KEY(got) == key, present);
1062 fail_at_every_call<Fixture>(
1063 [&] (Fixture & f) { got = f.ops.insert(f.root, p = f.node(key)); },
1066 EXPECT_EQ(got == p, not present);
1067 Fixture::expect_valid(f.root, keys_after_insert);
1070 fail_at_every_call<Fixture>(
1071 [&] (Fixture & f) { got = f.ops.insert_dup(f.root, p = f.node(key)); },
1075 Fixture::expect_valid(f.root, keys_with(key));
1078 fail_at_every_call<Fixture>(
1079 [&] (Fixture & f) { got = f.ops.search_or_insert(f.root, p = f.node(key)); },
1082 EXPECT_EQ(
KEY(got), key);
1083 EXPECT_EQ(got == p, not present);
1084 Fixture::expect_valid(f.root, keys_after_insert);
1087 fail_at_every_call<Fixture>(
1088 [&] (Fixture & f) { got = f.ops.remove(f.root, key); },
1091 EXPECT_EQ(got != Node::NullPtr, present);
1092 Fixture::expect_valid(f.root, keys_with(INT_MIN, key));
1095 fail_at_every_call<Fixture>(
1096 [&] (Fixture & f) { got = f.ops.insert_root(f.root, p = f.node(key)); },
1099 EXPECT_EQ(got == p, not present);
1100 EXPECT_EQ(f.root == p, not present);
1101 Fixture::expect_valid(f.root, keys_after_insert);
1104 fail_at_every_call<Fixture>(
1105 [&] (Fixture & f) { got = f.ops.insert_dup_root(f.root, p = f.node(key)); },
1108 EXPECT_EQ(f.root, p);
1109 Fixture::expect_valid(f.root, keys_with(key));
1112 fail_at_every_call<Fixture>(
1115 got = f.ops.insert_root_rec(f.root, p = f.node(key));
1116 if (got != Node::NullPtr)
1121 EXPECT_EQ(got == p, not present);
1122 Fixture::expect_valid(f.root, keys_after_insert);
1125 fail_at_every_call<Fixture>(
1128 got = f.ops.search_or_insert_root_rec(f.root, p = f.node(key));
1134 EXPECT_EQ(
KEY(got), key);
1135 EXPECT_EQ(got == p, not present);
1136 Fixture::expect_valid(f.root, keys_after_insert);
1139 Node * ts = Node::NullPtr;
1140 Node * tg = Node::NullPtr;
1142 fail_at_every_call<Fixture>(
1145 ts = tg = Node::NullPtr;
1146 split = f.ops.split_key_rec(f.root, key, ts, tg);
1150 EXPECT_EQ(
split, not present);
1156 EXPECT_EQ(f.root, Node::NullPtr);
1157 expect_split_by(ts, tg, key);
1162 fail_at_every_call<Fixture>(
1165 ts = tg = Node::NullPtr;
1166 f.ops.split_key_dup_rec(f.root, key, ts, tg);
1170 EXPECT_EQ(f.root, Node::NullPtr);
1171 EXPECT_TRUE(
check_bst(ts, Countdown_Less()));
1172 EXPECT_TRUE(
check_bst(tg, Countdown_Less()));
1173 for (
const int k : keys_of(ts))
1175 expect_split_by(ts, tg, key);
1179 EXPECT_GT(failures.thrown, 0);
1180 EXPECT_EQ(failures.restructured, 0);
1186TEST(ThrowingCompareOperations, BinTreeXtOperationLeavesTheTreeAsItWas)
1189 using Fixture = Ops_Fixture<Node, BinTreeXt_Operation<Node, Countdown_Less>>;
1192 for (
const int key : target_keys())
1194 const bool present = is_present(key);
1195 SCOPED_TRACE(trace_of(
"the operations at", key));
1196 Node * got =
nullptr;
1200 fail_at_every_call<Fixture>(
1201 [&] (Fixture & f) { pos = f.ops.inorder_position(f.root, key, got); },
1204 EXPECT_EQ(pos, present ? rank_of(key) : -1);
1208 fail_at_every_call<Fixture>(
1209 [&] (Fixture & f) { pos = f.ops.find_position(f.root, key, got); },
1214 EXPECT_EQ(pos, rank_of(key));
1219 fail_at_every_call<Fixture>(
1220 [&] (Fixture & f) { got = f.ops.insert_root(f.root, p = f.node(key)); },
1223 EXPECT_EQ(got == p, not present);
1224 Fixture::expect_valid(f.root, present ? keys_with() : keys_with(key));
1227 fail_at_every_call<Fixture>(
1228 [&] (Fixture & f) { got = f.ops.insert_dup_root(f.root, p = f.node(key)); },
1231 EXPECT_EQ(f.root, p);
1232 Fixture::expect_valid(f.root, keys_with(key));
1235 Node *
l = Node::NullPtr;
1236 Node *
r = Node::NullPtr;
1238 fail_at_every_call<Fixture>(
1241 l =
r = Node::NullPtr;
1242 split = f.ops.split_key_rec(f.root, key,
l,
r);
1244 f.root = Node::NullPtr;
1248 EXPECT_EQ(
split, not present);
1256 expect_split_by(
l,
r, key);
1259 fail_at_every_call<Fixture>(
1262 l =
r = Node::NullPtr;
1263 f.ops.split_key_dup_rec(f.root, key,
l,
r);
1264 f.root = Node::NullPtr;
1270 expect_split_by(
l,
r, key);
1274 EXPECT_GT(failures.thrown, 0);
1275 EXPECT_EQ(failures.restructured, 0);
1282 template <
class Tree>
1283 void expect_split_in_the_order_of_the_tree()
1287 if constexpr (Seeded_Tree<Tree>)
1289 for (
int k = 0;
k < 20;
k += 2)
1292 if constexpr (
requires { t.split_key(9,
l,
r); })
1293 (
void) t.split_key(9,
l,
r);
1297 EXPECT_EQ(keys_of(
l.getRoot()), (
Array<int>{18, 16, 14, 12, 10}));
1298 EXPECT_EQ(keys_of(
r.getRoot()), (
Array<int>{8, 6, 4, 2, 0}));
1299 EXPECT_TRUE(
l.verify());
1300 EXPECT_TRUE(
r.verify());
1307TEST(ThrowingCompareOperations, SplitsWithTheComparisonOfTheTree)
1309 expect_split_in_the_order_of_the_tree<Treap<int, Aleph::greater<int>>>();
1310 expect_split_in_the_order_of_the_tree<Treap_Rk<int, Aleph::greater<int>>>();
1311 expect_split_in_the_order_of_the_tree<Rand_Tree<int, Aleph::greater<int>>>();
1312 expect_split_in_the_order_of_the_tree<BinTree<int, Aleph::greater<int>>>();
1317TEST(ThrowingCompareOperations, SplayTreesKeepTheirNoexcept)
1321 static_assert(
noexcept(std::declval<Tree &>().insert(std::declval<Node *>())));
1322 static_assert(
noexcept(std::declval<Tree &>().insert_dup(std::declval<Node *>())));
1323 static_assert(
noexcept(std::declval<Tree &>().search_or_insert(std::declval<Node *>())));
1324 static_assert(
noexcept(std::declval<Tree &>().search(1)));
1325 static_assert(
noexcept(std::declval<Tree &>().remove(1)));
1326 static_assert(
noexcept(std::declval<Tree &>().splay(1)));
1327 static_assert(
noexcept(std::declval<Splay_Tree_Rk<int, Countdown_Less> &>().splay(1)));
1336 static inline bool armed =
false;
1342 operator bool ()
const
1345 throw Comparison_Error();
1350 Result operator ()(
const int & a,
const int & b)
const noexcept
1352 return Result{a < b};
1360 struct Two_Conversions_Less
1362 static inline bool armed =
false;
1368 operator int () const noexcept {
return value; }
1370 explicit operator bool ()
const
1373 throw Comparison_Error();
1378 Result operator ()(
const int & a,
const int & b)
const noexcept
1380 return Result{a < b};
1386 template <
class Key>
1387 struct Mutable_Key_Less
1389 static inline bool armed =
false;
1390 static inline long mutable_calls = 0;
1392 static bool mutable_call(
const bool result)
1396 throw Comparison_Error();
1400 bool operator ()(
const Key & a,
const Key & b)
const noexcept {
return a < b; }
1401 bool operator ()(
const Key & a, Key & b)
const {
return mutable_call(a < b); }
1402 bool operator ()(Key & a,
const Key & b)
const {
return mutable_call(a < b); }
1403 bool operator ()(Key & a, Key & b)
const {
return mutable_call(a < b); }
1407 struct Copy_Throw_Less
1409 static inline bool armed =
false;
1411 Copy_Throw_Less() =
default;
1413 Copy_Throw_Less(
const Copy_Throw_Less &)
1416 throw Comparison_Error();
1419 Copy_Throw_Less & operator = (
const Copy_Throw_Less &) =
default;
1421 bool operator ()(
const int & a,
const int & b)
const noexcept {
return a < b; }
1425 template <
class Cmp>
1428 Armed() noexcept { Cmp::armed =
true; }
1429 ~Armed() { Cmp::armed =
false; }
1430 Armed(
const Armed &) =
delete;
1431 Armed & operator = (
const Armed &) =
delete;
1438 static constexpr size_t Max_Alive = 64;
1439 static inline const Tracked_Less * alive[Max_Alive] = {};
1440 static inline size_t num_alive = 0;
1441 static inline long calls_on_dead = 0;
1443 void born() noexcept
1445 if (num_alive < Max_Alive)
1446 alive[num_alive++] =
this;
1449 Tracked_Less() noexcept { born(); }
1450 Tracked_Less(
const Tracked_Less &)
noexcept { born(); }
1451 Tracked_Less & operator = (
const Tracked_Less &)
noexcept {
return *
this; }
1455 for (
size_t i = 0; i < num_alive; ++i)
1456 if (alive[i] ==
this)
1458 alive[i] = alive[--num_alive];
1463 bool is_alive() const noexcept
1465 for (
size_t i = 0; i < num_alive; ++i)
1466 if (alive[i] ==
this)
1471 bool operator ()(
const int & a,
const int & b)
const noexcept
1481 template <
typename Key,
class Compare>
1482 struct By_Value_Avl :
public Avl_Tree<Key, Compare>
1489 return const_cast<By_Value_Avl &
>(*this).Base::get_compare();
1494 template <
class Tree>
1502 if constexpr (Seeded_Tree<Tree>)
1504 for (
const int key : {5, 3, 8, 1, 4})
1505 EXPECT_NE(tree.
insert(pool.
make(key)),
nullptr);
1512 template <
class Tree>
1513 void expect_the_condition_lets_the_exception_through()
1515 using Less = Two_Conversions_Less;
1517 EXPECT_FALSE(
noexcept(f.tree.search(3)));
1519 const Armed<Less> armed;
1520 EXPECT_THROW((
void) f.tree.search(4), Comparison_Error);
1522 EXPECT_NE(f.tree.search(8),
nullptr);
1524 if constexpr (
requires (
typename Tree::Iterator it,
typename Tree::Node * p)
1525 { it.reset_to_node(p); })
1527 typename Tree::Iterator it(f.tree);
1528 it.reset_to_node(f.tree.search(
KEY(f.tree.getRoot()) == 1 ? 3 : 1));
1529 EXPECT_FALSE(
noexcept(it.has_curr()));
1531 const Armed<Less> armed;
1532 EXPECT_THROW((
void) it.has_curr(), Comparison_Error);
1534 EXPECT_TRUE(it.has_curr());
1541 template <
class Tree>
1542 void expect_the_copy_lets_the_exception_through()
1545 auto & tree = f.tree;
1546 const Armed<Copy_Throw_Less> armed;
1547 if constexpr (
requires { tree.position(3); })
1549 EXPECT_FALSE(
noexcept(tree.position(3)));
1550 EXPECT_FALSE(
noexcept(tree.find_position(3)));
1551 EXPECT_THROW((
void) tree.position(3), Comparison_Error);
1552 EXPECT_THROW((
void) tree.find_position(3), Comparison_Error);
1556 EXPECT_FALSE(
noexcept(tree.
search(3)));
1557 EXPECT_THROW((
void) tree.
search(3), Comparison_Error);
1558 EXPECT_THROW((
void) tree.
insert(f.pool.make(7)), Comparison_Error);
1559 EXPECT_THROW((
void) tree.
remove(3), Comparison_Error);
1561 Copy_Throw_Less::armed =
false;
1562 EXPECT_TRUE(tree.verify());
1563 EXPECT_EQ(keys_of(tree.getRoot()), (
Array<int>{1, 3, 4, 5, 8}));
1569 template <
class Tree>
1570 void expect_the_iterator_lets_the_exception_through()
1572 Tree_Fixture<Tree> f;
1573 auto & tree = f.tree;
1574 using Iterator =
typename Tree::Iterator;
1575 EXPECT_FALSE(
noexcept(std::declval<Iterator &>().reset_to_key(1)));
1576 EXPECT_FALSE(
noexcept(std::declval<const Iterator &>().
has_curr()));
1577 EXPECT_FALSE(
noexcept(std::declval<const Iterator &>() ==
1578 std::declval<const Iterator &>()));
1580 Iterator it(tree,
size_t{3});
1582 const Failing_At failing(1);
1583 EXPECT_THROW(it.reset_to_key(10), Comparison_Error);
1585 EXPECT_EQ(it.get_pos(), 3u);
1586 EXPECT_EQ(
KEY(it.get_curr()), 6);
1589 const int key =
KEY(tree.getRoot()) == 0 ? 2 : 0;
1591 ASSERT_NE(node,
nullptr);
1592 Iterator by_node(tree);
1593 by_node.reset_to_node(node);
1594 const Iterator at_pos(tree,
size_t{3});
1596 const Failing_At failing(1);
1597 EXPECT_THROW((
void) by_node.has_curr(), Comparison_Error);
1600 const Failing_At failing(1);
1601 EXPECT_THROW((
void) (by_node == at_pos), Comparison_Error);
1604 const Failing_At failing(1);
1605 EXPECT_THROW((
void) by_node.get_pos(), Comparison_Error);
1607 ASSERT_TRUE(by_node.has_curr());
1608 EXPECT_EQ(by_node.get_curr(), node);
1609 EXPECT_EQ(by_node.get_pos(),
static_cast<size_t>(rank_of(key)));
1616TEST(ThrowingCompareOperations, TheConversionOfTheResultLetsTheExceptionThrough)
1618 Small_Tree<Avl_Tree<int, Proxy_Less>> f;
1619 EXPECT_FALSE(
noexcept(f.tree.search(3)));
1621 const Armed<Proxy_Less> armed;
1622 EXPECT_THROW((
void) f.tree.search(4), Comparison_Error);
1624 EXPECT_NE(f.tree.search(8),
nullptr);
1627 for (
const int key : {5, 3, 8})
1629 EXPECT_FALSE(
noexcept(map.
search(3)));
1631 const Armed<Proxy_Less> armed;
1632 EXPECT_THROW((
void) map.
search(4), Comparison_Error);
1633 EXPECT_THROW((
void) map.
contains(4), Comparison_Error);
1635 EXPECT_EQ(map.
find(8), -8);
1638 const std::pair<int, int> p1{1, 10}, p2{2, 20};
1640 EXPECT_FALSE(
noexcept(pair_cmp(p1, p2)));
1641 EXPECT_FALSE(
noexcept(pair_cmp(p1,
k)));
1642 EXPECT_FALSE(
noexcept(pair_cmp(
k, p2)));
1644 const Armed<Proxy_Less> armed;
1645 EXPECT_THROW((
void) pair_cmp(p1, p2), Comparison_Error);
1646 EXPECT_THROW((
void) pair_cmp(p1,
k), Comparison_Error);
1647 EXPECT_THROW((
void) pair_cmp(
k, p2), Comparison_Error);
1661TEST(ThrowingCompareOperations, TheConditionOnTheResultLetsTheExceptionThrough)
1663 using Less = Two_Conversions_Less;
1664 expect_the_condition_lets_the_exception_through<Avl_Tree<int, Less>>();
1665 expect_the_condition_lets_the_exception_through<Treap<int, Less>>();
1666 expect_the_condition_lets_the_exception_through<Avl_Tree_Rk<int, Less>>();
1667 expect_the_condition_lets_the_exception_through<Treap_Rk<int, Less>>();
1670 for (
const int key : {5, 3, 8})
1672 EXPECT_FALSE(
noexcept(map.search(3)));
1674 const Armed<Less> armed;
1675 EXPECT_THROW((
void) map.search(4), Comparison_Error);
1676 EXPECT_THROW((
void) map.contains(4), Comparison_Error);
1678 EXPECT_EQ(map.find(8), -8);
1688TEST(ThrowingCompareOperations, AnOverloadForKeysNotConstantLetsTheExceptionThrough)
1690 using Less = Mutable_Key_Less<int>;
1691 Small_Tree<Avl_Tree<int, Less>> f;
1692 EXPECT_FALSE(
noexcept(f.tree.search(3)));
1694 const Armed<Less> armed;
1695 EXPECT_THROW((
void) f.tree.search(4), Comparison_Error);
1697 EXPECT_NE(f.tree.search(8),
nullptr);
1703TEST(ThrowingCompareOperations, TheSearchOfAMapComparesConstantKeys)
1705 using Int_Less = Mutable_Key_Less<int>;
1707 for (
const int key : {5, 3, 8, 1, 4})
1709 Int_Less::mutable_calls = 0;
1710 for (
const int key : {1, 3, 4, 5, 8})
1711 EXPECT_EQ(map.
find(key), -key);
1713 EXPECT_EQ(Int_Less::mutable_calls, 0);
1715 using String_Less = Mutable_Key_Less<std::string>;
1717 for (
const char * name : {
"m",
"d",
"t",
"a",
"g"})
1719 String_Less::mutable_calls = 0;
1722 EXPECT_EQ(String_Less::mutable_calls, 0);
1728TEST(ThrowingCompareOperations, TheCopyOfTheComparisonLetsTheExceptionThrough)
1730 expect_the_copy_lets_the_exception_through<BinTree<int, Copy_Throw_Less>>();
1731 expect_the_copy_lets_the_exception_through<Avl_Tree_Rk<int, Copy_Throw_Less>>();
1732 expect_the_copy_lets_the_exception_through<Rb_Tree_Rk<int, Copy_Throw_Less>>();
1733 expect_the_copy_lets_the_exception_through<TdRbTreeRk<int, Copy_Throw_Less>>();
1734 expect_the_copy_lets_the_exception_through<HtdRbTreeRk<int, Copy_Throw_Less>>();
1735 expect_the_copy_lets_the_exception_through<Treap_Rk<int, Copy_Throw_Less>>();
1736 expect_the_copy_lets_the_exception_through<Rand_Tree<int, Copy_Throw_Less>>();
1740 EXPECT_TRUE(
noexcept(std::declval<
const Avl_Tree_Rk<int> &>().position(1)));
1746TEST(ThrowingCompareOperations, RankedIteratorsLetTheExceptionThrough)
1748 expect_the_iterator_lets_the_exception_through<Avl_Tree_Rk<int, Countdown_Less>>();
1749 expect_the_iterator_lets_the_exception_through<Rb_Tree_Rk<int, Countdown_Less>>();
1750 expect_the_iterator_lets_the_exception_through<Treap_Rk<int, Countdown_Less>>();
1754 EXPECT_TRUE(
noexcept(std::declval<const Iterator &>().
has_curr()));
1755 EXPECT_TRUE(
noexcept(std::declval<Iterator &>().reset_to_key(1)));
1761TEST(ThrowingCompareOperations, AMapOnATreeThatGivesACopyOfItsComparison)
1764 for (
const int key : {5, 3, 8, 1, 4})
1766 Tracked_Less::calls_on_dead = 0;
1767 for (
const int key : {1, 3, 4, 5, 8})
1769 const auto * p = map.
search(key);
1770 ASSERT_NE(p,
nullptr) <<
"key " << key;
1771 EXPECT_EQ(p->second, -key);
1773 EXPECT_EQ(map.
search(7),
nullptr);
1774 EXPECT_EQ(Tracked_Less::calls_on_dead, 0);
1784 struct Poisoned_Less
1786 static inline long operator_calls = 0;
1792 operator bool () const noexcept {
return value; }
1794 bool operator ! ()
const
1801 Result operator ()(
const int & a,
const int & b)
const noexcept {
return Result{a < b}; }
1804 using Poisoned_Result = Poisoned_Less::Result;
1806 bool count_operator(
const bool result)
1808 ++Poisoned_Less::operator_calls;
1812 bool operator && (
const Poisoned_Result & a,
const Poisoned_Result & b)
1814 return count_operator(a.value
and b.value);
1817 bool operator && (
const Poisoned_Result & a,
const bool b) {
return count_operator(a.value
and b); }
1819 bool operator && (
const bool a,
const Poisoned_Result & b) {
return count_operator(a
and b.value); }
1821 bool operator || (
const Poisoned_Result & a,
const Poisoned_Result & b)
1823 return count_operator(a.value or b.value);
1826 bool operator || (
const Poisoned_Result & a,
const bool b) {
return count_operator(a.value or b); }
1828 bool operator || (
const bool a,
const Poisoned_Result & b) {
return count_operator(a or b.value); }
1832 template <
class Tree>
1833 void run_the_operations_of()
1838 if constexpr (Seeded_Tree<Tree>)
1840 for (
const int key : insertion_order())
1841 EXPECT_NE(tree.
insert(pool.make(key)), nullptr);
1842 EXPECT_EQ(tree.
insert(pool.
make(4)),
nullptr);
1843 EXPECT_NE(tree.search_or_insert(pool.
make(5)),
nullptr);
1844 if constexpr (
requires (
Tree t,
Node * p) { t.insert_dup(p); })
1845 EXPECT_NE(tree.insert_dup(pool.
make(6)),
nullptr);
1846 for (
const int key : target_keys())
1848 if constexpr (
requires (
Tree t) { t.position(1); })
1849 for (
const int key : target_keys())
1850 (void) tree.position(key);
1851 for (
const int key : target_keys())
1853 EXPECT_TRUE(tree.verify());
1856 if constexpr (Seeded_Tree<Tree>)
1858 for (
const int key : insertion_order())
1859 EXPECT_NE(whole.
insert(pool.make(key)), nullptr);
1860 if constexpr (
requires (
Tree t) { t.split_key_dup(1, t, t); })
1861 whole.split_key_dup(11,
l,
r);
1862 else if constexpr (
requires (
Tree t) { t.split_dup(1, t, t); })
1863 whole.split_dup(11,
l,
r);
1864 EXPECT_TRUE(
l.verify()
and r.verify());
1873 using Tree =
typename TypeParam::template Tree_With<Poisoned_Less>;
1874 using Set =
typename TypeParam::template Set_With<Poisoned_Less>;
1875 using Map =
typename TypeParam::template Map_With<Poisoned_Less>;
1876 Poisoned_Less::operator_calls = 0;
1878 run_the_operations_of<Tree>();
1879 EXPECT_EQ(Poisoned_Less::operator_calls, 0) <<
"the tree";
1880 Poisoned_Less::operator_calls = 0;
1883 for (
const int key : insertion_order())
1884 EXPECT_NE(set.insert(key),
nullptr);
1885 for (
const int key : target_keys())
1887 (void) set.search(key);
1888 (void) set.contains(key);
1890 for (
const int key : target_keys())
1891 (
void) set.remove(key);
1892 EXPECT_EQ(set.size(), 0u);
1893 EXPECT_EQ(Poisoned_Less::operator_calls, 0) <<
"the set";
1894 Poisoned_Less::operator_calls = 0;
1897 for (
const int key : insertion_order())
1898 EXPECT_NE(map.insert(key, -key),
nullptr);
1899 for (
const int key : target_keys())
1901 (void) map.search(key);
1902 if (map.contains(key))
1903 EXPECT_EQ(map.find(key), -key);
1905 for (
const int key : target_keys())
1906 if (map.contains(key))
1907 (
void) map.remove(key);
1908 EXPECT_EQ(map.size(), 0u);
1909 EXPECT_EQ(Poisoned_Less::operator_calls, 0) <<
"the map";
Standard functor implementations and comparison objects.
WeightedDigraph::Node Node
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Node for extended binary search tree.
Node for binary search tree.
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
bool contains(const Key &key) const noexcept(nothrow_search)
Same as has().
Data & find(const Key &key)
Find the value associated with key.
constexpr Compare & get_compare() noexcept
See key_comp().
Forward declaration used by CRTP helpers before the full node definition.
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.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Node for QuadTree spatial data structure.
QuadTree - Hierarchical spatial index for 2D points.
void remove(const Point &p)
Remove a point from the tree.
Point * search(const Point &p) noexcept
Search for a point in the tree.
void split(Node *node)
Subdivide a leaf node into four children.
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
Owns the nodes of a tree, which does not free them.
Node * make(const int key)
__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)
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
What the tests of the binary search trees share: the list of tree types, what each node carries,...
Ranked AVL tree with nodes without a virtual destructor.
AVL binary search tree with nodes without a virtual destructor.
Gen_Avl_Tree< AvlNode, Key, Compare > Base
Binary search tree with nodes without virtual destructors,.
Default comparator for pair types in hash maps.
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
TYPED_TEST(ThrowingCompareTree, IsNoexceptOnlyWithANothrowComparison)
#define TREES_UNDER_TEST(X)
TYPED_TEST_SUITE(ThrowingCompareTree, Tree_Kinds, Kind_Names)
#define TREE_KIND_ENTRY(T, Restructures, Noexcept)
#define DECLARE_TREE_KIND(T, Restructures, Noexcept)
Dynamic array container with automatic resizing.
Extended binary node with subtree count.
Binary tree operations (split, join, rotate).
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.