Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
throwing_compare_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / /
6 / ___ \| | __/ |_) | | | |_____\ V V /
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2026 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
57
58#include <gtest/gtest-spi.h>
59
60#include <climits>
61#include <stdexcept>
62#include <string>
63#include <utility>
64
65#include <ahFunction.H>
66#include <tpl_array.H>
67#include <tpl_binNodeXt.H>
68#include <tpl_binTreeOps.H>
69#include <tpl_dynMapTree.H>
70#include <tpl_dynSetTree.H>
71
72using namespace Aleph;
73using namespace search_trees_test;
74
75namespace
76{
77 struct Comparison_Error {};
78
79 // Orders as less<int> does. Armed, it throws at its call number fail_at
80 // (from 1); it counts in static members because the trees copy it.
81 struct Countdown_Less
82 {
83 static inline long calls = 0;
84 static inline long fail_at = 0; // 0: it does not throw
85
86 bool operator ()(const int a, const int b) const
87 {
88 if (fail_at != 0 and ++calls == fail_at)
89 throw Comparison_Error();
90 return a < b;
91 }
92 };
93
94 // While one lives, Countdown_Less throws at its call k.
95 struct Failing_At
96 {
97 explicit Failing_At(const long k) noexcept
98 {
99 Countdown_Less::calls = 0;
100 Countdown_Less::fail_at = k;
101 }
102
103 ~Failing_At() { Countdown_Less::fail_at = 0; }
104 };
105
106 // More calls than any operation on these trees makes: past it, the
107 // operation is taken as one that does not end.
108 constexpr long Max_Calls = 1000;
109
110 // The trees start with the keys 0, 2, ..., 2 (Num_Keys - 1); the odd
111 // keys, and -1, are not in them.
112 constexpr int Num_Keys = 24;
113
114 // The order in which the keys are inserted: scrambled (7 is prime with
115 // Num_Keys), so that BinTree does not degenerate into a list.
116 Array<int> insertion_order()
117 {
119 for (int i = 0; i < Num_Keys; ++i)
120 keys.append(2 * (i * 7 % Num_Keys));
121 return keys;
122 }
123
124 // The initial keys, sorted, plus `added` and less `removed` when they
125 // are not INT_MIN.
126 Array<int> keys_with(const int added = INT_MIN, const int removed = INT_MIN)
127 {
129 bool pending = added != INT_MIN; // `added`, after the keys not greater
130 for (int k = 0; k < 2 * Num_Keys; k += 2)
131 {
132 if (pending and added < k)
133 {
134 keys.append(added);
135 pending = false;
136 }
137 if (k != removed)
138 keys.append(k);
139 }
140 if (pending)
141 keys.append(added);
142 return keys;
143 }
144
145 bool is_present(const int key) noexcept
146 {
147 return key >= 0 and key < 2 * Num_Keys and key % 2 == 0;
148 }
149
150 // The position that `key` has among the initial keys, if it is one.
151 long rank_of(const int key) noexcept { return key / 2; }
152
153 // The keys at which the operations are tried: every key of the tree,
154 // and every gap around them.
155 Array<int> target_keys()
156 {
158 for (int k = -1; k < 2 * Num_Keys; ++k)
159 keys.append(k);
160 return keys;
161 }
162
163 template <class Node>
164 void collect_keys(Node * p, Array<int> & keys)
165 {
166 if (p == Node::NullPtr)
167 return;
168 collect_keys(LLINK(p), keys);
169 keys.append(KEY(p));
170 collect_keys(RLINK(p), keys);
171 }
172
173 // The keys of the tree of root `root`, in order.
174 template <class Node>
175 Array<int> keys_of(Node * root)
176 {
178 collect_keys(root, keys);
179 return keys;
180 }
181
182 template <class Node>
183 void collect_state(Node * p, Array<long> & state)
184 {
185 if (p == Node::NullPtr)
186 {
187 state.append(LONG_MIN);
188 return;
189 }
190 state.append(KEY(p));
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);
201 }
202
203 // The shape of the tree of root `root` and the data of its nodes, in
204 // preorder: equal states are the same tree.
205 template <class Node>
206 Array<long> state_of(Node * root)
207 {
208 Array<long> state;
209 collect_state(root, state);
210 return state;
211 }
212
213 // How the runs of the operations of a test that threw left their trees.
214 struct Failures
215 {
216 long thrown = 0; // runs that threw
217 long restructured = 0; // of them, those that left a different tree
218 };
219
220 /* Runs `op` on a fresh `Fixture` with the comparison failing at its call
221 k, for k = 1, 2, ... until op makes all its calls, and counts in
222 `failures` the runs that threw. After each one, the fixture must keep
223 its initial keys (`expect_initial()`); after the run that does not
224 throw, `done` checks its result. An exception other than the one of
225 the comparison fails the test, and so does a run that does not throw
226 after the comparison threw: op caught the exception. */
227 template <class Fixture, class Op, class Done>
228 void fail_at_every_call(Op op, Done done, Failures & failures)
229 {
230 for (long k = 1; k <= Max_Calls; ++k)
231 {
232 SCOPED_TRACE("the comparison throws at its call " + std::to_string(k));
233 Fixture f;
234 const auto before = f.state();
235 bool thrown = false;
236 {
237 const Failing_At failing(k);
238 try
239 {
240 op(f);
241 }
242 catch (const Comparison_Error &)
243 {
244 thrown = true;
245 }
246 }
247
248 if (not thrown)
249 {
250 // k calls or more: the call k threw, and op caught it
251 if (Countdown_Less::calls >= k)
252 {
253 ADD_FAILURE() << "the comparison threw, but the exception did not reach the caller";
254 return;
255 }
256 done(f);
257 return;
258 }
259
260 ++failures.thrown;
261 f.expect_initial();
262 if (f.state() != before)
263 ++failures.restructured;
264 }
265
266 ADD_FAILURE() << "the operation made more than " << Max_Calls << " comparisons";
267 }
268
269 // A tree of type Tree with the initial keys.
270 template <class Tree>
271 class Tree_Fixture
272 {
273 Node_Pool<typename Tree::Node> pool; // the tree does not own its nodes
274
275 public:
276 Tree tree;
277
278 Tree_Fixture()
279 {
280 if constexpr (Seeded_Tree<Tree>)
281 tree.set_seed(17);
282 for (const int key : insertion_order())
283 EXPECT_NE(tree.insert(pool.make(key)), nullptr);
284 }
285
286 typename Tree::Node * node(const int key) { return pool.make(key); }
287
288 [[nodiscard]] Array<long> state() { return state_of(tree.getRoot()); }
289
290 // `t` verifies and has `keys`, and as many as them if it counts them.
291 static void expect_valid(Tree & t, const Array<int> & keys)
292 {
293 EXPECT_TRUE(t.verify());
294 EXPECT_EQ(keys_of(t.getRoot()), keys);
295 if constexpr (requires { t.size(); })
296 {
297 EXPECT_EQ(t.size(), keys.size());
298 }
299 }
300
301 void expect_initial() { expect_valid(tree, keys_with()); }
302 };
303
304 // A DynSetTree with the initial keys.
305 template <class Set>
306 struct Set_Fixture
307 {
308 Set set;
309
310 Set_Fixture()
311 {
312 for (const int key : insertion_order())
313 EXPECT_NE(set.insert(key), nullptr);
314 }
315
316 [[nodiscard]] Array<long> state() { return state_of(set.get_root_node()); }
317
318 static void expect_valid(const Set & s, const Array<int> & keys)
319 {
320 EXPECT_TRUE(s.verify());
321 EXPECT_EQ(s.size(), keys.size());
322 Array<int> got;
323 s.for_each([&got] (const int key) { got.append(key); });
324 EXPECT_EQ(got, keys);
325 }
326
327 void expect_initial() { expect_valid(set, keys_with()); }
328 };
329
330 // A DynMapTree with the initial keys, each one mapped to its opposite.
331 template <class Map>
332 struct Map_Fixture
333 {
334 Map map;
335
336 Map_Fixture()
337 {
338 for (const int key : insertion_order())
339 EXPECT_NE(map.insert(key, -key), nullptr);
340 }
341
342 [[nodiscard]] Array<long> state()
343 {
344 Array<long> state;
345 map.for_each([&state] (const std::pair<int, int> & p)
346 {
347 state.append(p.first);
348 state.append(p.second);
349 });
350 return state;
351 }
352
353 static void expect_valid(const Map & m, const Array<int> & keys)
354 {
355 EXPECT_TRUE(m.verify());
356 EXPECT_EQ(m.size(), keys.size());
357 Array<int> got;
358 m.for_each([&got] (const std::pair<int, int> & p)
359 {
360 EXPECT_EQ(p.second, -p.first);
361 got.append(p.first);
362 });
363 EXPECT_EQ(got, keys);
364 }
365
366 void expect_initial() { expect_valid(map, keys_with()); }
367 };
368
369 /* The trees under test. For each one, whether its insertions recolor
370 or rotate on their way down, before their last comparison, and
371 whether its operations are noexcept with a comparison that cannot
372 throw (those of Rb_Tree_Rk never were). A new tree is one more line
373 here. */
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)
387
388 // A tree under test, alone, in a set and in a map; the _With ones take
389 // another comparison.
390# define DECLARE_TREE_KIND(T, Restructures, Noexcept) \
391 struct T##_Kind \
392 { \
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; \
405 };
406
408
409 template <class First, class... Kinds>
410 struct Without_First
411 {
412 using type = ::testing::Types<Kinds...>;
413 };
414
415# define TREE_KIND_ENTRY(T, Restructures, Noexcept) , T##_Kind
416
417 using Tree_Kinds = Without_First<void TREES_UNDER_TEST(TREE_KIND_ENTRY)>::type;
418
419# undef TREE_KIND_ENTRY
420# undef DECLARE_TREE_KIND
421# undef TREES_UNDER_TEST
422
423 struct Kind_Names
424 {
425 template <class Kind>
426 static std::string GetName(int)
427 {
428 return Kind::name;
429 }
430 };
431
432 std::string trace_of(const char * op, const int key)
433 {
434 return std::string(op) + "(" + std::to_string(key) + ")";
435 }
436}
437
438// The harness: an operation that catches what the comparison throws, and
439// tries again, gives the right result, but the exception did not reach its
440// caller. The run of fail_at_every_call() that takes it must fail, instead
441// of taking it as the run where the comparison does not throw.
442TEST(ThrowingCompareHarness, AnExceptionThatTheOperationCatchesFailsTheTest)
443{
444 using Fixture = Tree_Fixture<Avl_Tree<int, Countdown_Less>>;
445 Failures failures;
446 EXPECT_NONFATAL_FAILURE(fail_at_every_call<Fixture>(
447 [] (Fixture & f)
448 {
449 try
450 {
451 (void) f.tree.search(4);
452 }
453 catch (const Comparison_Error &) // the call k is past
454 {
455 (void) f.tree.search(4);
456 }
457 },
458 [] (Fixture & f) { f.expect_initial(); }, failures),
459 "did not reach the caller");
460 EXPECT_EQ(failures.thrown, 0);
461}
462
463template <class Kind>
464struct ThrowingCompareTree : ::testing::Test {};
465
466TYPED_TEST_SUITE(ThrowingCompareTree, Tree_Kinds, Kind_Names);
467
468// With a comparison that cannot throw, the operations keep their
469// noexcept; with one that can, they let through what it throws.
470TYPED_TEST(ThrowingCompareTree, IsNoexceptOnlyWithANothrowComparison)
471{
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 *>())),
478 expected);
479 EXPECT_EQ(noexcept(std::declval<Nothrow &>().search(1)), expected);
480 EXPECT_EQ(noexcept(std::declval<Nothrow &>().remove(1)), expected);
481
482 using Tree = typename TypeParam::Tree;
483 using Tree_Node = typename Tree::Node;
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)));
489}
490
491TYPED_TEST(ThrowingCompareTree, InsertionLetsTheExceptionThrough)
492{
493 using Tree = typename TypeParam::Tree;
494 using Fixture = Tree_Fixture<Tree>;
495 using Node = typename Tree::Node;
496 Failures failures;
497
498 for (const int key : target_keys())
499 {
500 const bool present = is_present(key);
501 Node * got = nullptr;
502 Node * p = nullptr;
503 {
504 SCOPED_TRACE(trace_of("insert", key));
505 fail_at_every_call<Fixture>(
506 [&] (Fixture & f) { got = f.tree.insert(p = f.node(key)); },
507 [&] (Fixture & f)
508 {
509 EXPECT_EQ(got, present ? nullptr : p);
510 Fixture::expect_valid(f.tree, present ? keys_with() : keys_with(key));
511 }, failures);
512 }
513 {
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)); },
517 [&] (Fixture & f)
518 {
519 EXPECT_EQ(got, p);
520 Fixture::expect_valid(f.tree, keys_with(key));
521 }, failures);
522 }
523 {
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)); },
527 [&] (Fixture & f)
528 {
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));
533 }, failures);
534 }
535 }
536
537 EXPECT_GT(failures.thrown, 0);
538 // The top-down trees recolor and rotate before they compare again: they
539 // must be left valid. The others change only after their last
540 // comparison: they must be left as they were.
541 if constexpr (TypeParam::restructures)
542 {
543 EXPECT_GT(failures.restructured, 0);
544 }
545 else
546 {
547 EXPECT_EQ(failures.restructured, 0);
548 }
549}
550
551TYPED_TEST(ThrowingCompareTree, SearchLetsTheExceptionThrough)
552{
553 using Tree = typename TypeParam::Tree;
554 using Fixture = Tree_Fixture<Tree>;
555 using Node = typename Tree::Node;
556 Failures failures;
557
558 for (const int key : target_keys())
559 {
560 const bool present = is_present(key);
561 {
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); },
566 [&] (Fixture & f)
567 {
568 EXPECT_EQ(got != nullptr, present);
569 if (got != nullptr)
570 {
571 EXPECT_EQ(KEY(got), key);
572 }
573 f.expect_initial();
574 }, failures);
575 }
576
577 if constexpr (requires (Tree & t, const int k) { t.position(k); t.find_position(k); })
578 {
579 SCOPED_TRACE(trace_of("position", key));
580 long pos = 0;
581 fail_at_every_call<Fixture>(
582 [&] (Fixture & f) { pos = f.tree.position(key).first; },
583 [&] (Fixture & f)
584 {
585 EXPECT_EQ(pos, present ? rank_of(key) : -1);
586 f.expect_initial();
587 }, failures);
588 fail_at_every_call<Fixture>(
589 [&] (Fixture & f) { pos = f.tree.find_position(key).first; },
590 [&] (Fixture & f)
591 {
592 if (present)
593 {
594 EXPECT_EQ(pos, rank_of(key));
595 }
596 f.expect_initial();
597 }, failures);
598 }
599 }
600
601 EXPECT_GT(failures.thrown, 0);
602 EXPECT_EQ(failures.restructured, 0);
603}
604
605TYPED_TEST(ThrowingCompareTree, RemovalLetsTheExceptionThrough)
606{
607 using Tree = typename TypeParam::Tree;
608 using Fixture = Tree_Fixture<Tree>;
609 using Node = typename Tree::Node;
610 Failures failures;
611
612 for (const int key : target_keys())
613 {
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); },
618 [&] (Fixture & f)
619 {
620 EXPECT_EQ(got != nullptr, is_present(key));
621 if (got != nullptr)
622 {
623 EXPECT_EQ(KEY(got), key);
624 }
625 Fixture::expect_valid(f.tree, keys_with(INT_MIN, key));
626 }, failures);
627 }
628
629 EXPECT_GT(failures.thrown, 0);
630 if constexpr (not TypeParam::restructures)
631 {
632 EXPECT_EQ(failures.restructured, 0);
633 }
634}
635
636namespace
637{
638 // A split by key: it splits `t` by `key` into `l` and `r`, and returns
639 // false if it did not split because `key` is in `t`.
640 template <class Tree>
641 struct Split
642 {
643 const char * name;
644 bool (*split)(Tree & t, int key, Tree & l, Tree & r);
645 };
646
647 // The splits by key of Tree that let through what the comparison
648 // throws. The others move the nodes one by one, and stay noexcept.
649 template <class Tree>
650 Array<Split<Tree>> splits_that_may_throw()
651 {
652 Array<Split<Tree>> splits;
653
654 if constexpr (requires (Tree t, Tree l, Tree r) { t.split_key(1, l, r); })
655 if constexpr (not noexcept(std::declval<Tree &>().split_key(1, std::declval<Tree &>(),
656 std::declval<Tree &>())))
657 {
658 splits.append({"split_key", [] (Tree & t, int k, Tree & l, Tree & r)
659 {
660 return t.split_key(k, l, r);
661 }});
662 splits.append({"split_key_dup", [] (Tree & t, int k, Tree & l, Tree & r)
663 {
664 t.split_key_dup(k, l, r);
665 return true;
666 }});
667 }
668
669 if constexpr (requires (Tree t, Tree l, Tree r) { t.split(1, l, r); })
670 {
671 splits.append({"split", [] (Tree & t, int k, Tree & l, Tree & r)
672 {
673 return t.split(k, l, r);
674 }});
675 splits.append({"split_dup", [] (Tree & t, int k, Tree & l, Tree & r)
676 {
677 t.split_dup(k, l, r);
678 return true;
679 }});
680 }
681
682 return splits;
683 }
684
685 // A tree with the initial keys, and the two empty ones of its split.
686 template <class Tree>
687 struct Split_Fixture : Tree_Fixture<Tree>
688 {
689 Tree l, r;
690
691 void expect_initial()
692 {
693 Tree_Fixture<Tree>::expect_initial();
694 EXPECT_EQ(l.getRoot(), Tree::Node::NullPtr);
695 EXPECT_EQ(r.getRoot(), Tree::Node::NullPtr);
696 }
697 };
698
699 // `l` and `r` split the initial keys by `key`: those before it in `l`,
700 // those after it in `r`, and `key` in either one.
701 template <class Node>
702 void expect_split_by(Node * l, Node * r, const int key)
703 {
704 Array<int> keys = keys_of(l);
705 const Array<int> right = keys_of(r);
706 for (const int k : keys)
707 EXPECT_LE(k, key);
708 for (const int k : right)
709 EXPECT_GE(k, key);
710 for (const int k : right)
711 keys.append(k);
712 EXPECT_EQ(keys, keys_with());
713 }
714}
715
716TYPED_TEST(ThrowingCompareTree, SplitLetsTheExceptionThrough)
717{
718 using Tree = typename TypeParam::Tree;
719 using Fixture = Split_Fixture<Tree>;
720 Failures failures;
721
722 for (const Split<Tree> & s : splits_that_may_throw<Tree>())
723 for (const int key : target_keys())
724 {
725 SCOPED_TRACE(trace_of(s.name, key));
726 bool split = false;
727 fail_at_every_call<Fixture>(
728 [&] (Fixture & f) { split = s.split(f.tree, key, f.l, f.r); },
729 [&] (Fixture & f)
730 {
731 if (not split) // key is in the tree, which stays as it was
732 {
733 EXPECT_TRUE(is_present(key));
734 f.expect_initial();
735 return;
736 }
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);
741 }, failures);
742 }
743
744 EXPECT_EQ(failures.restructured, 0);
745}
746
747template <class Kind>
748struct ThrowingCompareSet : ::testing::Test {};
749
750TYPED_TEST_SUITE(ThrowingCompareSet, Tree_Kinds, Kind_Names);
751
752// The set frees the node of a key whose insertion throws: the sanitizers
753// would report it otherwise.
754TYPED_TEST(ThrowingCompareSet, LetsTheExceptionThrough)
755{
756 using Set = typename TypeParam::Set;
757 using Fixture = Set_Fixture<Set>;
758 using Tree = typename TypeParam::Tree;
759 Failures failures;
760
761 for (const int key : target_keys())
762 {
763 const bool present = is_present(key);
764 int * got = nullptr;
765 {
766 SCOPED_TRACE(trace_of("insert", key));
767 fail_at_every_call<Fixture>(
768 [&] (Fixture & f) { got = f.set.insert(key); },
769 [&] (Fixture & f)
770 {
771 EXPECT_EQ(got == nullptr, present);
772 Fixture::expect_valid(f.set, present ? keys_with() : keys_with(key));
773 }, failures);
774 }
775 {
776 SCOPED_TRACE(trace_of("insert_dup", key));
777 fail_at_every_call<Fixture>(
778 [&] (Fixture & f) { got = f.set.insert_dup(key); },
779 [&] (Fixture & f)
780 {
781 EXPECT_NE(got, nullptr);
782 Fixture::expect_valid(f.set, keys_with(key));
783 }, failures);
784 }
785 {
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); },
789 [&] (Fixture & f)
790 {
791 ASSERT_NE(got, nullptr);
792 EXPECT_EQ(*got, key);
793 Fixture::expect_valid(f.set, present ? keys_with() : keys_with(key));
794 }, failures);
795 }
796 {
797 SCOPED_TRACE(trace_of("search", key));
798 fail_at_every_call<Fixture>(
799 [&] (Fixture & f) { got = f.set.search(key); },
800 [&] (Fixture & f)
801 {
802 EXPECT_EQ(got != nullptr, present);
803 f.expect_initial();
804 }, failures);
805 }
806 {
807 SCOPED_TRACE(trace_of("find", key));
808 bool found = false;
809 bool refused = false; // it threw std::domain_error
810 fail_at_every_call<Fixture>(
811 [&] (Fixture & f)
812 {
813 refused = false;
814 try
815 {
816 found = f.set.find(key) == key;
817 }
818 catch (const std::domain_error &) // the key is not in the set
819 {
820 found = false;
821 refused = true;
822 }
823 },
824 [&] (Fixture & f)
825 {
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";
829 f.expect_initial();
830 }, failures);
831 }
832 {
833 SCOPED_TRACE(trace_of("remove", key));
834 size_t size = 0;
835 fail_at_every_call<Fixture>(
836 [&] (Fixture & f) { size = f.set.remove(key); },
837 [&] (Fixture & f)
838 {
839 EXPECT_EQ(size, present ? Num_Keys - 1 : Num_Keys);
840 Fixture::expect_valid(f.set, keys_with(INT_MIN, key));
841 }, failures);
842 }
843
844 if constexpr (requires (Tree & t, const int k) { t.position(k); })
845 {
846 SCOPED_TRACE(trace_of("position", key));
847 long pos = 0;
848 fail_at_every_call<Fixture>(
849 [&] (Fixture & f) { pos = f.set.position(key); },
850 [&] (Fixture & f)
851 {
852 EXPECT_EQ(pos, present ? rank_of(key) : -1);
853 f.expect_initial();
854 }, failures);
855 }
856 }
857
858 EXPECT_GT(failures.thrown, 0);
859}
860
861template <class Kind>
862struct ThrowingCompareMap : ::testing::Test {};
863
864TYPED_TEST_SUITE(ThrowingCompareMap, Tree_Kinds, Kind_Names);
865
866TYPED_TEST(ThrowingCompareMap, LetsTheExceptionThrough)
867{
868 using Map = typename TypeParam::Map;
869 using Fixture = Map_Fixture<Map>;
870 Failures failures;
871
872 for (const int key : target_keys())
873 {
874 const bool present = is_present(key);
875 {
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); },
880 [&] (Fixture & f)
881 {
882 EXPECT_EQ(got == nullptr, present);
883 Fixture::expect_valid(f.map, present ? keys_with() : keys_with(key));
884 }, failures);
885 }
886 {
887 SCOPED_TRACE(trace_of("operator[]", key));
888 fail_at_every_call<Fixture>(
889 [&] (Fixture & f) { f.map[key] = -key; },
890 [&] (Fixture & f)
891 {
892 Fixture::expect_valid(f.map, present ? keys_with() : keys_with(key));
893 }, failures);
894 }
895 {
896 SCOPED_TRACE(trace_of("find", key));
897 bool found = false;
898 bool refused = false; // it threw std::domain_error
899 fail_at_every_call<Fixture>(
900 [&] (Fixture & f)
901 {
902 refused = false;
903 try
904 {
905 found = f.map.find(key) == -key;
906 }
907 catch (const std::domain_error &) // the key is not in the map
908 {
909 found = false;
910 refused = true;
911 }
912 },
913 [&] (Fixture & f)
914 {
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";
918 f.expect_initial();
919 }, failures);
920 }
921 {
922 SCOPED_TRACE(trace_of("remove", key));
923 bool removed = false;
924 bool refused = false; // it threw std::domain_error
925 fail_at_every_call<Fixture>(
926 [&] (Fixture & f)
927 {
928 refused = false;
929 try
930 {
931 removed = f.map.remove(key) == -key;
932 }
933 catch (const std::domain_error &) // the key is not in the map
934 {
935 removed = false;
936 refused = true;
937 }
938 },
939 [&] (Fixture & f)
940 {
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));
945 }, failures);
946 }
947
948 // search() of DynMapTree lets through what the comparison throws:
949 // a search that went back to noexcept fails here, instead of
950 // skipping its test
951 {
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)))
956 {
957 bool found = false;
958 fail_at_every_call<Fixture>(
959 [&] (Fixture & f) { found = f.map.search(key) != nullptr; },
960 [&] (Fixture & f)
961 {
962 EXPECT_EQ(found, present);
963 f.expect_initial();
964 }, failures);
965 }
966 }
967 }
968
969 EXPECT_GT(failures.thrown, 0);
970}
971
972namespace
973{
974 // A tree of nodes of type Node with the initial keys, built and searched
975 // with the operations Ops.
976 template <class Node, class Ops>
977 struct Ops_Fixture
978 {
979 Node_Pool<Node> pool;
980 Ops ops;
981 Node * root = Node::NullPtr;
982
983 Ops_Fixture()
984 {
985 for (const int key : insertion_order())
986 EXPECT_NE(ops.insert(root, pool.make(key)), Node::NullPtr);
987 if constexpr (Counted_Node<Node>)
988 (void) count(root); // insert() of BinTree_Operation does not count
989 }
990
991 static size_t count(Node * p)
992 {
993 if (p == Node::NullPtr)
994 return 0;
995 return p->getCount() = count(LLINK(p)) + count(RLINK(p)) + 1;
996 }
997
998 Node * node(const int key) { return pool.make(key); }
999
1000 [[nodiscard]] Array<long> state() { return state_of(root); }
1001
1002 static void expect_valid(Node * r, const Array<int> & keys)
1003 {
1004 EXPECT_TRUE(check_bst(r, Countdown_Less()));
1005 if constexpr (Counted_Node<Node>)
1006 {
1007 EXPECT_TRUE(check_rank_tree(r));
1008 }
1009 EXPECT_EQ(keys_of(r), keys);
1010 }
1011
1012 void expect_initial() { expect_valid(root, keys_with()); }
1013 };
1014}
1015
1016// The operations of BinTree_Operation compare before they change the
1017// tree: what the comparison throws leaves it as it was.
1018TEST(ThrowingCompareOperations, BinTreeOperationLeavesTheTreeAsItWas)
1019{
1020 using Node = BinNode<int>;
1021 using Fixture = Ops_Fixture<Node, BinTree_Operation<Node, Countdown_Less>>;
1022 Failures failures;
1023
1024 for (const int key : target_keys())
1025 {
1026 const bool present = is_present(key);
1027 SCOPED_TRACE(trace_of("the operations at", key));
1028 Node * got = nullptr;
1029 Node * p = nullptr;
1030 const Array<int> keys_after_insert = present ? keys_with() : keys_with(key);
1031
1032 fail_at_every_call<Fixture>(
1033 [&] (Fixture & f) { got = f.ops.search(f.root, key); },
1034 [&] (Fixture & f)
1035 {
1036 EXPECT_EQ(got != Node::NullPtr, present);
1037 f.expect_initial();
1038 }, failures);
1039
1040 fail_at_every_call<Fixture>(
1041 [&] (Fixture & f)
1042 {
1043 Node head; // the parent of the root, as search_parent() asks
1044 RLINK(&head) = f.root;
1045 Node * parent = &head;
1046 got = f.ops.search_parent(f.root, key, parent);
1047 },
1048 [&] (Fixture & f)
1049 {
1050 EXPECT_EQ(KEY(got) == key, present);
1051 f.expect_initial();
1052 }, failures);
1053
1054 fail_at_every_call<Fixture>(
1055 [&] (Fixture & f) { got = f.ops.search_rank_parent(f.root, key); },
1056 [&] (Fixture & f)
1057 {
1058 EXPECT_EQ(KEY(got) == key, present);
1059 f.expect_initial();
1060 }, failures);
1061
1062 fail_at_every_call<Fixture>(
1063 [&] (Fixture & f) { got = f.ops.insert(f.root, p = f.node(key)); },
1064 [&] (Fixture & f)
1065 {
1066 EXPECT_EQ(got == p, not present);
1067 Fixture::expect_valid(f.root, keys_after_insert);
1068 }, failures);
1069
1070 fail_at_every_call<Fixture>(
1071 [&] (Fixture & f) { got = f.ops.insert_dup(f.root, p = f.node(key)); },
1072 [&] (Fixture & f)
1073 {
1074 EXPECT_EQ(got, p);
1075 Fixture::expect_valid(f.root, keys_with(key));
1076 }, failures);
1077
1078 fail_at_every_call<Fixture>(
1079 [&] (Fixture & f) { got = f.ops.search_or_insert(f.root, p = f.node(key)); },
1080 [&] (Fixture & f)
1081 {
1082 EXPECT_EQ(KEY(got), key);
1083 EXPECT_EQ(got == p, not present);
1084 Fixture::expect_valid(f.root, keys_after_insert);
1085 }, failures);
1086
1087 fail_at_every_call<Fixture>(
1088 [&] (Fixture & f) { got = f.ops.remove(f.root, key); },
1089 [&] (Fixture & f)
1090 {
1091 EXPECT_EQ(got != Node::NullPtr, present);
1092 Fixture::expect_valid(f.root, keys_with(INT_MIN, key));
1093 }, failures);
1094
1095 fail_at_every_call<Fixture>(
1096 [&] (Fixture & f) { got = f.ops.insert_root(f.root, p = f.node(key)); },
1097 [&] (Fixture & f)
1098 {
1099 EXPECT_EQ(got == p, not present);
1100 EXPECT_EQ(f.root == p, not present);
1101 Fixture::expect_valid(f.root, keys_after_insert);
1102 }, failures);
1103
1104 fail_at_every_call<Fixture>(
1105 [&] (Fixture & f) { got = f.ops.insert_dup_root(f.root, p = f.node(key)); },
1106 [&] (Fixture & f)
1107 {
1108 EXPECT_EQ(f.root, p);
1109 Fixture::expect_valid(f.root, keys_with(key));
1110 }, failures);
1111
1112 fail_at_every_call<Fixture>(
1113 [&] (Fixture & f)
1114 {
1115 got = f.ops.insert_root_rec(f.root, p = f.node(key));
1116 if (got != Node::NullPtr)
1117 f.root = got;
1118 },
1119 [&] (Fixture & f)
1120 {
1121 EXPECT_EQ(got == p, not present);
1122 Fixture::expect_valid(f.root, keys_after_insert);
1123 }, failures);
1124
1125 fail_at_every_call<Fixture>(
1126 [&] (Fixture & f)
1127 {
1128 got = f.ops.search_or_insert_root_rec(f.root, p = f.node(key));
1129 if (got == p)
1130 f.root = got;
1131 },
1132 [&] (Fixture & f)
1133 {
1134 EXPECT_EQ(KEY(got), key);
1135 EXPECT_EQ(got == p, not present);
1136 Fixture::expect_valid(f.root, keys_after_insert);
1137 }, failures);
1138
1139 Node * ts = Node::NullPtr;
1140 Node * tg = Node::NullPtr;
1141 bool split = false;
1142 fail_at_every_call<Fixture>(
1143 [&] (Fixture & f)
1144 {
1145 ts = tg = Node::NullPtr;
1146 split = f.ops.split_key_rec(f.root, key, ts, tg);
1147 },
1148 [&] (Fixture & f)
1149 {
1150 EXPECT_EQ(split, not present);
1151 if (not split)
1152 {
1153 f.expect_initial();
1154 return;
1155 }
1156 EXPECT_EQ(f.root, Node::NullPtr);
1157 expect_split_by(ts, tg, key);
1158 }, failures);
1159
1160 // split_key_dup_rec() put no node in ts or tg, and lost the tree;
1161 // insert_dup_root(), above, kept only the node that it inserted
1162 fail_at_every_call<Fixture>(
1163 [&] (Fixture & f)
1164 {
1165 ts = tg = Node::NullPtr;
1166 f.ops.split_key_dup_rec(f.root, key, ts, tg);
1167 },
1168 [&] (Fixture & f)
1169 {
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))
1174 EXPECT_LT(k, key);
1175 expect_split_by(ts, tg, key);
1176 }, failures);
1177 }
1178
1179 EXPECT_GT(failures.thrown, 0);
1180 EXPECT_EQ(failures.restructured, 0);
1181}
1182
1183// The operations of BinTreeXt_Operation compare before they change the
1184// tree: what the comparison throws leaves it, and its counters, as they
1185// were.
1186TEST(ThrowingCompareOperations, BinTreeXtOperationLeavesTheTreeAsItWas)
1187{
1188 using Node = BinNodeXt<int>;
1189 using Fixture = Ops_Fixture<Node, BinTreeXt_Operation<Node, Countdown_Less>>;
1190 Failures failures;
1191
1192 for (const int key : target_keys())
1193 {
1194 const bool present = is_present(key);
1195 SCOPED_TRACE(trace_of("the operations at", key));
1196 Node * got = nullptr;
1197 Node * p = nullptr;
1198 long pos = 0;
1199
1200 fail_at_every_call<Fixture>(
1201 [&] (Fixture & f) { pos = f.ops.inorder_position(f.root, key, got); },
1202 [&] (Fixture & f)
1203 {
1204 EXPECT_EQ(pos, present ? rank_of(key) : -1);
1205 f.expect_initial();
1206 }, failures);
1207
1208 fail_at_every_call<Fixture>(
1209 [&] (Fixture & f) { pos = f.ops.find_position(f.root, key, got); },
1210 [&] (Fixture & f)
1211 {
1212 if (present)
1213 {
1214 EXPECT_EQ(pos, rank_of(key));
1215 }
1216 f.expect_initial();
1217 }, failures);
1218
1219 fail_at_every_call<Fixture>(
1220 [&] (Fixture & f) { got = f.ops.insert_root(f.root, p = f.node(key)); },
1221 [&] (Fixture & f)
1222 {
1223 EXPECT_EQ(got == p, not present);
1224 Fixture::expect_valid(f.root, present ? keys_with() : keys_with(key));
1225 }, failures);
1226
1227 fail_at_every_call<Fixture>(
1228 [&] (Fixture & f) { got = f.ops.insert_dup_root(f.root, p = f.node(key)); },
1229 [&] (Fixture & f)
1230 {
1231 EXPECT_EQ(f.root, p);
1232 Fixture::expect_valid(f.root, keys_with(key));
1233 }, failures);
1234
1235 Node * l = Node::NullPtr;
1236 Node * r = Node::NullPtr;
1237 bool split = false;
1238 fail_at_every_call<Fixture>(
1239 [&] (Fixture & f)
1240 {
1241 l = r = Node::NullPtr;
1242 split = f.ops.split_key_rec(f.root, key, l, r);
1243 if (split)
1244 f.root = Node::NullPtr; // its nodes are now in l and r
1245 },
1246 [&] (Fixture & f)
1247 {
1248 EXPECT_EQ(split, not present);
1249 if (not split)
1250 {
1251 f.expect_initial();
1252 return;
1253 }
1254 EXPECT_TRUE(check_rank_tree(l));
1255 EXPECT_TRUE(check_rank_tree(r));
1256 expect_split_by(l, r, key);
1257 }, failures);
1258
1259 fail_at_every_call<Fixture>(
1260 [&] (Fixture & f)
1261 {
1262 l = r = Node::NullPtr;
1263 f.ops.split_key_dup_rec(f.root, key, l, r);
1264 f.root = Node::NullPtr; // its nodes are now in l and r
1265 },
1266 [&] (Fixture &)
1267 {
1268 EXPECT_TRUE(check_rank_tree(l));
1269 EXPECT_TRUE(check_rank_tree(r));
1270 expect_split_by(l, r, key);
1271 }, failures);
1272 }
1273
1274 EXPECT_GT(failures.thrown, 0);
1275 EXPECT_EQ(failures.restructured, 0);
1276}
1277
1278namespace
1279{
1280 // Splits a tree with the keys 0, 2, ..., 18, ordered from the greatest,
1281 // by 9: the keys before 9 in the order of the tree are those greater.
1282 template <class Tree>
1283 void expect_split_in_the_order_of_the_tree()
1284 {
1286 Tree t, l, r;
1287 if constexpr (Seeded_Tree<Tree>)
1288 t.set_seed(5);
1289 for (int k = 0; k < 20; k += 2)
1290 ASSERT_NE(t.insert(pool.make(k)), nullptr);
1291
1292 if constexpr (requires { t.split_key(9, l, r); })
1293 (void) t.split_key(9, l, r);
1294 else
1295 (void) t.split(9, l, r);
1296
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());
1301 }
1302}
1303
1304// The splits of the treaps and of Rand_Tree split with the comparison of
1305// the tree: they compared with Aleph::less of the key, and with another
1306// order they lost keys or put them on the wrong side.
1307TEST(ThrowingCompareOperations, SplitsWithTheComparisonOfTheTree)
1308{
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>>>();
1313}
1314
1315// The splay trees keep their noexcept: a splay relinks the tree as it
1316// compares, so their comparison must not throw.
1317TEST(ThrowingCompareOperations, SplayTreesKeepTheirNoexcept)
1318{
1320 using Node = Tree::Node;
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)));
1328}
1329
1330namespace
1331{
1332 // A comparison that does not throw, whose result throws when it is
1333 // converted to bool, while armed
1334 struct Proxy_Less
1335 {
1336 static inline bool armed = false;
1337
1338 struct Result
1339 {
1340 bool value;
1341
1342 operator bool () const
1343 {
1344 if (armed)
1345 throw Comparison_Error();
1346 return value;
1347 }
1348 };
1349
1350 Result operator ()(const int & a, const int & b) const noexcept
1351 {
1352 return Result{a < b};
1353 }
1354 };
1355
1356 // A comparison that does not throw, whose result converts to bool in two
1357 // ways: implicitly, as a return does, through an operator int() that
1358 // does not throw, and contextually, as the condition of an if does,
1359 // through an operator bool() that throws while armed
1360 struct Two_Conversions_Less
1361 {
1362 static inline bool armed = false;
1363
1364 struct Result
1365 {
1366 bool value;
1367
1368 operator int () const noexcept { return value; }
1369
1370 explicit operator bool () const
1371 {
1372 if (armed)
1373 throw Comparison_Error();
1374 return value;
1375 }
1376 };
1377
1378 Result operator ()(const int & a, const int & b) const noexcept
1379 {
1380 return Result{a < b};
1381 }
1382 };
1383
1384 // Its overloads for keys that are not constant count their calls and
1385 // throw, while armed; the one for constant keys does not throw
1386 template <class Key>
1387 struct Mutable_Key_Less
1388 {
1389 static inline bool armed = false;
1390 static inline long mutable_calls = 0;
1391
1392 static bool mutable_call(const bool result)
1393 {
1394 ++mutable_calls;
1395 if (armed)
1396 throw Comparison_Error();
1397 return result;
1398 }
1399
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); }
1404 };
1405
1406 // A comparison that does not throw, whose copy throws while armed
1407 struct Copy_Throw_Less
1408 {
1409 static inline bool armed = false;
1410
1411 Copy_Throw_Less() = default;
1412
1413 Copy_Throw_Less(const Copy_Throw_Less &)
1414 {
1415 if (armed)
1416 throw Comparison_Error();
1417 }
1418
1419 Copy_Throw_Less & operator = (const Copy_Throw_Less &) = default;
1420
1421 bool operator ()(const int & a, const int & b) const noexcept { return a < b; }
1422 };
1423
1424 // While one lives, the comparison Cmp is armed
1425 template <class Cmp>
1426 struct Armed
1427 {
1428 Armed() noexcept { Cmp::armed = true; }
1429 ~Armed() { Cmp::armed = false; }
1430 Armed(const Armed &) = delete;
1431 Armed & operator = (const Armed &) = delete;
1432 };
1433
1434 // A comparison that knows whether it is alive when it is called: each
1435 // one keeps its address among those of the live ones while it lives
1436 struct Tracked_Less
1437 {
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;
1442
1443 void born() noexcept
1444 {
1445 if (num_alive < Max_Alive)
1446 alive[num_alive++] = this;
1447 }
1448
1449 Tracked_Less() noexcept { born(); }
1450 Tracked_Less(const Tracked_Less &) noexcept { born(); }
1451 Tracked_Less & operator = (const Tracked_Less &) noexcept { return *this; }
1452
1453 ~Tracked_Less()
1454 {
1455 for (size_t i = 0; i < num_alive; ++i)
1456 if (alive[i] == this)
1457 {
1458 alive[i] = alive[--num_alive];
1459 return;
1460 }
1461 }
1462
1463 bool is_alive() const noexcept
1464 {
1465 for (size_t i = 0; i < num_alive; ++i)
1466 if (alive[i] == this)
1467 return true;
1468 return false;
1469 }
1470
1471 bool operator ()(const int & a, const int & b) const noexcept
1472 {
1473 if (not is_alive())
1474 ++calls_on_dead;
1475 return a < b;
1476 }
1477 };
1478
1479 // A tree that gives a copy of its comparison, as the concept BSTPolicy
1480 // allows to a tree of the user
1481 template <typename Key, class Compare>
1482 struct By_Value_Avl : public Avl_Tree<Key, Compare>
1483 {
1484 using Base = Avl_Tree<Key, Compare>;
1485 using Base::Base;
1486
1487 Compare get_compare() const
1488 {
1489 return const_cast<By_Value_Avl &>(*this).Base::get_compare();
1490 }
1491 };
1492
1493 // A tree with the keys 1, 3, 4, 5 and 8
1494 template <class Tree>
1495 struct Small_Tree
1496 {
1498 Tree tree;
1499
1500 Small_Tree()
1501 {
1502 if constexpr (Seeded_Tree<Tree>)
1503 tree.set_seed(3);
1504 for (const int key : {5, 3, 8, 1, 4})
1505 EXPECT_NE(tree.insert(pool.make(key)), nullptr);
1506 }
1507 };
1508
1509 // The search of Tree lets through what the condition on the result of
1510 // Two_Conversions_Less throws, and so does the position of a node that
1511 // is not the root, which takes comparisons, in the ranked trees
1512 template <class Tree>
1513 void expect_the_condition_lets_the_exception_through()
1514 {
1515 using Less = Two_Conversions_Less;
1516 Small_Tree<Tree> f;
1517 EXPECT_FALSE(noexcept(f.tree.search(3)));
1518 {
1519 const Armed<Less> armed;
1520 EXPECT_THROW((void) f.tree.search(4), Comparison_Error);
1521 }
1522 EXPECT_NE(f.tree.search(8), nullptr);
1523
1524 if constexpr (requires (typename Tree::Iterator it, typename Tree::Node * p)
1525 { it.reset_to_node(p); })
1526 {
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()));
1530 {
1531 const Armed<Less> armed;
1532 EXPECT_THROW((void) it.has_curr(), Comparison_Error);
1533 }
1534 EXPECT_TRUE(it.has_curr());
1535 }
1536 }
1537
1538 // The operations that copy the comparison into an operation object, as
1539 // those of BinTree and the positions of the ranked trees, let through
1540 // what that copy throws
1541 template <class Tree>
1542 void expect_the_copy_lets_the_exception_through()
1543 {
1544 Small_Tree<Tree> f;
1545 auto & tree = f.tree;
1546 const Armed<Copy_Throw_Less> armed;
1547 if constexpr (requires { tree.position(3); })
1548 {
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);
1553 }
1554 else
1555 {
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);
1560 }
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}));
1564 }
1565
1566 // The iterators of the ranked trees compare to go to a key, and to find
1567 // the position of a node that they were put on: what the comparison
1568 // throws must reach the caller, with the iterator as it was
1569 template <class Tree>
1570 void expect_the_iterator_lets_the_exception_through()
1571 {
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 &>()));
1579
1580 Iterator it(tree, size_t{3}); // on the key 6
1581 {
1582 const Failing_At failing(1);
1583 EXPECT_THROW(it.reset_to_key(10), Comparison_Error);
1584 }
1585 EXPECT_EQ(it.get_pos(), 3u);
1586 EXPECT_EQ(KEY(it.get_curr()), 6);
1587
1588 // a node that is not the root, so that its position takes comparisons
1589 const int key = KEY(tree.getRoot()) == 0 ? 2 : 0;
1590 typename Tree::Node * node = tree.search(key);
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});
1595 {
1596 const Failing_At failing(1);
1597 EXPECT_THROW((void) by_node.has_curr(), Comparison_Error);
1598 }
1599 {
1600 const Failing_At failing(1);
1601 EXPECT_THROW((void) (by_node == at_pos), Comparison_Error);
1602 }
1603 {
1604 const Failing_At failing(1);
1605 EXPECT_THROW((void) by_node.get_pos(), Comparison_Error);
1606 }
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)));
1610 f.expect_initial();
1611 }
1612}
1613
1614// A comparison whose result throws when it is converted to bool: the
1615// operations converted it inside noexcept, and the program ended
1616TEST(ThrowingCompareOperations, TheConversionOfTheResultLetsTheExceptionThrough)
1617{
1618 Small_Tree<Avl_Tree<int, Proxy_Less>> f;
1619 EXPECT_FALSE(noexcept(f.tree.search(3)));
1620 {
1621 const Armed<Proxy_Less> armed;
1622 EXPECT_THROW((void) f.tree.search(4), Comparison_Error);
1623 }
1624 EXPECT_NE(f.tree.search(8), nullptr);
1625
1627 for (const int key : {5, 3, 8})
1628 map.insert(key, -key);
1629 EXPECT_FALSE(noexcept(map.search(3)));
1630 {
1631 const Armed<Proxy_Less> armed;
1632 EXPECT_THROW((void) map.search(4), Comparison_Error);
1633 EXPECT_THROW((void) map.contains(4), Comparison_Error);
1634 }
1635 EXPECT_EQ(map.find(8), -8);
1636
1638 const std::pair<int, int> p1{1, 10}, p2{2, 20};
1639 const int k = 2;
1640 EXPECT_FALSE(noexcept(pair_cmp(p1, p2)));
1641 EXPECT_FALSE(noexcept(pair_cmp(p1, k)));
1642 EXPECT_FALSE(noexcept(pair_cmp(k, p2)));
1643 {
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);
1648 }
1649
1650 // a comparison that gives a bool and does not throw keeps the noexcept
1651 EXPECT_TRUE(noexcept(std::declval<Avl_Tree<int> &>().search(1)));
1652 EXPECT_TRUE(noexcept(std::declval<const DynMapTree<int, int> &>().search(1)));
1653 EXPECT_TRUE(noexcept(std::declval<const Dft_Pair_Cmp<int, int, Aleph::less<int>> &>()
1654 (p1, p2)));
1655}
1656
1657// A result whose conversion to bool as a condition throws, while its
1658// conversion as a returned value does not: the operations looked at the
1659// second one, and the first one threw inside noexcept in their ifs. The
1660// search of a map, which compared pairs, took the second one before
1661TEST(ThrowingCompareOperations, TheConditionOnTheResultLetsTheExceptionThrough)
1662{
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>>();
1668
1670 for (const int key : {5, 3, 8})
1671 map.insert(key, -key);
1672 EXPECT_FALSE(noexcept(map.search(3)));
1673 {
1674 const Armed<Less> armed;
1675 EXPECT_THROW((void) map.search(4), Comparison_Error);
1676 EXPECT_THROW((void) map.contains(4), Comparison_Error);
1677 }
1678 EXPECT_EQ(map.find(8), -8);
1679
1680 // a comparison that gives a bool and does not throw keeps the noexcept
1681 EXPECT_TRUE(noexcept(std::declval<const Avl_Tree_Rk<int>::Iterator &>().has_curr()));
1682 EXPECT_TRUE(noexcept(std::declval<const Treap_Rk<int>::Iterator &>().has_curr()));
1683}
1684
1685// The trees compare the key that they search for with the key of a node,
1686// which is not constant: an overload of the comparison for keys that are
1687// not constant that throws was called inside noexcept
1688TEST(ThrowingCompareOperations, AnOverloadForKeysNotConstantLetsTheExceptionThrough)
1689{
1690 using Less = Mutable_Key_Less<int>;
1691 Small_Tree<Avl_Tree<int, Less>> f;
1692 EXPECT_FALSE(noexcept(f.tree.search(3)));
1693 {
1694 const Armed<Less> armed;
1695 EXPECT_THROW((void) f.tree.search(4), Comparison_Error);
1696 }
1697 EXPECT_NE(f.tree.search(8), nullptr);
1698}
1699
1700// A map compares its key with those of its pairs as the pairs give them,
1701// constant: its search by the key called the overload for keys that are
1702// not constant, which the search by a pair never called
1703TEST(ThrowingCompareOperations, TheSearchOfAMapComparesConstantKeys)
1704{
1705 using Int_Less = Mutable_Key_Less<int>;
1707 for (const int key : {5, 3, 8, 1, 4})
1708 map.insert(key, -key);
1709 Int_Less::mutable_calls = 0;
1710 for (const int key : {1, 3, 4, 5, 8})
1711 EXPECT_EQ(map.find(key), -key);
1712 EXPECT_FALSE(map.contains(7));
1713 EXPECT_EQ(Int_Less::mutable_calls, 0);
1714
1715 using String_Less = Mutable_Key_Less<std::string>;
1717 for (const char * name : {"m", "d", "t", "a", "g"})
1718 names.insert(name, 1);
1719 String_Less::mutable_calls = 0;
1720 EXPECT_TRUE(names.contains("g"));
1721 EXPECT_FALSE(names.contains("h"));
1722 EXPECT_EQ(String_Less::mutable_calls, 0);
1723}
1724
1725// BinTree and the positions of the ranked trees copy the comparison into
1726// an operation object before they compare: they made that copy inside
1727// noexcept, and what it threw ended the program
1728TEST(ThrowingCompareOperations, TheCopyOfTheComparisonLetsTheExceptionThrough)
1729{
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>>();
1737
1738 // a comparison whose copy does not throw keeps the noexcept
1739 EXPECT_TRUE(noexcept(std::declval<BinTree<int> &>().search(1)));
1740 EXPECT_TRUE(noexcept(std::declval<const Avl_Tree_Rk<int> &>().position(1)));
1741}
1742
1743// The iterators of the ranked trees: reset_to_key(), has_curr() and
1744// operator == were noexcept, and what the comparison threw ended the
1745// program
1746TEST(ThrowingCompareOperations, RankedIteratorsLetTheExceptionThrough)
1747{
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>>();
1751
1752 // with a comparison that cannot throw, they keep the noexcept
1753 using Iterator = Avl_Tree_Rk<int>::Iterator;
1754 EXPECT_TRUE(noexcept(std::declval<const Iterator &>().has_curr()));
1755 EXPECT_TRUE(noexcept(std::declval<Iterator &>().reset_to_key(1)));
1756}
1757
1758// A tree of the user may give a copy of its comparison, as BSTPolicy
1759// allows: the search of a map compared with a reference to that copy,
1760// which had died when the search took it
1761TEST(ThrowingCompareOperations, AMapOnATreeThatGivesACopyOfItsComparison)
1762{
1764 for (const int key : {5, 3, 8, 1, 4})
1765 map.insert(key, -key);
1766 Tracked_Less::calls_on_dead = 0;
1767 for (const int key : {1, 3, 4, 5, 8})
1768 {
1769 const auto * p = map.search(key);
1770 ASSERT_NE(p, nullptr) << "key " << key;
1771 EXPECT_EQ(p->second, -key);
1772 }
1773 EXPECT_EQ(map.search(7), nullptr);
1774 EXPECT_EQ(Tracked_Less::calls_on_dead, 0);
1775}
1776
1777namespace
1778{
1779 // A comparison whose result converts to bool without throwing, and has
1780 // an operator!, an operator&& and an operator|| of its own, which count
1781 // their calls. The noexcept of the operations looks at the conversion
1782 // (nothrow_condition_v), not at these operators, which may throw: the
1783 // operations must convert the result before they apply not, and or or.
1784 struct Poisoned_Less
1785 {
1786 static inline long operator_calls = 0;
1787
1788 struct Result
1789 {
1790 bool value;
1791
1792 operator bool () const noexcept { return value; }
1793
1794 bool operator ! () const
1795 {
1796 ++operator_calls;
1797 return not value;
1798 }
1799 };
1800
1801 Result operator ()(const int & a, const int & b) const noexcept { return Result{a < b}; }
1802 };
1803
1804 using Poisoned_Result = Poisoned_Less::Result;
1805
1806 bool count_operator(const bool result)
1807 {
1808 ++Poisoned_Less::operator_calls;
1809 return result;
1810 }
1811
1812 bool operator && (const Poisoned_Result & a, const Poisoned_Result & b)
1813 {
1814 return count_operator(a.value and b.value);
1815 }
1816
1817 bool operator && (const Poisoned_Result & a, const bool b) { return count_operator(a.value and b); }
1818
1819 bool operator && (const bool a, const Poisoned_Result & b) { return count_operator(a and b.value); }
1820
1821 bool operator || (const Poisoned_Result & a, const Poisoned_Result & b)
1822 {
1823 return count_operator(a.value or b.value);
1824 }
1825
1826 bool operator || (const Poisoned_Result & a, const bool b) { return count_operator(a.value or b); }
1827
1828 bool operator || (const bool a, const Poisoned_Result & b) { return count_operator(a or b.value); }
1829
1830 // The insertions, searches, positions, splits and removals of Tree, with
1831 // the initial keys and around them
1832 template <class Tree>
1833 void run_the_operations_of()
1834 {
1835 using Node = typename Tree::Node;
1836 Node_Pool<Node> pool;
1837 Tree tree;
1838 if constexpr (Seeded_Tree<Tree>)
1839 tree.set_seed(17);
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())
1847 (void) tree.search(key);
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())
1852 (void) tree.remove(key);
1853 EXPECT_TRUE(tree.verify());
1854
1855 Tree whole, l, r;
1856 if constexpr (Seeded_Tree<Tree>)
1857 whole.set_seed(5);
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());
1865 }
1866}
1867
1868// The operations take the result of the comparison as a bool: they applied
1869// not, and or or to it, which call the operators of a result that has
1870// them, inside noexcept
1871TYPED_TEST(ThrowingCompareTree, TakesTheResultOfTheComparisonAsABool)
1872{
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;
1877
1878 run_the_operations_of<Tree>();
1879 EXPECT_EQ(Poisoned_Less::operator_calls, 0) << "the tree";
1880 Poisoned_Less::operator_calls = 0;
1881
1882 Set set;
1883 for (const int key : insertion_order())
1884 EXPECT_NE(set.insert(key), nullptr);
1885 for (const int key : target_keys())
1886 {
1887 (void) set.search(key);
1888 (void) set.contains(key);
1889 }
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;
1895
1896 Map map;
1897 for (const int key : insertion_order())
1898 EXPECT_NE(map.insert(key, -key), nullptr);
1899 for (const int key : target_keys())
1900 {
1901 (void) map.search(key);
1902 if (map.contains(key))
1903 EXPECT_EQ(map.find(key), -key);
1904 }
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";
1910}
Standard functor implementations and comparison objects.
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
size_t size_t int32_t value
Definition ca-c-api.h:116
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
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().
Definition tpl_avl.H:484
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.
Definition ah-dry.H:1475
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
Node for QuadTree spatial data structure.
Definition quadnode.H:95
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
void remove(const Point &p)
Remove a point from the tree.
Definition quadtree.H:565
Point * search(const Point &p) noexcept
Search for a point in the tree.
Definition quadtree.H:524
void split(Node *node)
Subdivide a leaf node into four children.
Definition quadtree.H:187
QuadNode Node
Definition quadtree.H:128
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
Definition quadtree.H:281
Owns the nodes of a tree, which does not free them.
#define TEST(name)
__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)
Definition gmpfrxx.h:4138
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.
Definition ah-zip.H:173
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
Definition ahAlgo.H:962
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
Definition ahAlgo.H:309
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.
Definition ahAlgo.H:127
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.
Definition tpl_avlRk.H:1592
AVL binary search tree with nodes without a virtual destructor.
Definition tpl_avl.H:831
Gen_Avl_Tree< AvlNode, Key, Compare > Base
Definition tpl_avl.H:832
Binary search tree with nodes without virtual destructors,.
Default comparator for pair types in hash maps.
Definition ahDry.H:206
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)
#define RLINK(i, n)
int keys[]
#define LLINK(i, n)
static int * k
Gsl_Compatible_Rng r
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.
DynList< int > l