43#include <gtest/gtest.h>
49using namespace testing;
58 std::vector<Node *> allocated;
60 Node *
make(
const typename Node::key_type & key)
63 allocated.push_back(p);
69 for (
auto & q : allocated)
79 for (
Node * p : allocated)
87 std::vector<typename Node::key_type>
keys;
88 Aleph::infix_for_each<Node>(
root, [&] (
Node * p) {
keys.push_back(
KEY(p)); });
93TEST(AvlTree, InsertSearchVerifyAndIteratorOrder)
96 NodePool<Avl_Tree<int>> pool;
98 const std::vector<int> input{5, 3, 7, 2, 4, 6, 8};
100 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
105 auto * found = t.
search(4);
106 ASSERT_NE(found,
nullptr);
107 EXPECT_EQ(
KEY(found), 4);
109 std::vector<int> it_keys;
111 it_keys.push_back(
KEY(it.get_curr_ne()));
118TEST(AvlTree, InsertRejectsDuplicatesAndSearchOrInsertReturnsExisting)
121 NodePool<Avl_Tree<int>> pool;
123 auto * a = pool.make(5);
124 auto * b = pool.make(5);
126 ASSERT_NE(t.
insert(a),
nullptr);
127 EXPECT_EQ(t.
insert(b),
nullptr);
129 auto * c = pool.make(5);
131 ASSERT_NE(got,
nullptr);
133 EXPECT_EQ(
KEY(got), 5);
139TEST(AvlTree, DuplicateAtIntermediateLevel)
150 NodePool<Avl_Tree<int>> pool;
152 ASSERT_NE(t.
insert(pool.make(50)),
nullptr);
153 ASSERT_NE(t.
insert(pool.make(25)),
nullptr);
154 ASSERT_NE(t.
insert(pool.make(75)),
nullptr);
155 ASSERT_NE(t.
insert(pool.make(10)),
nullptr);
156 ASSERT_NE(t.
insert(pool.make(30)),
nullptr);
159 EXPECT_EQ(t.
insert(pool.make(25)),
nullptr);
163 auto * dup = pool.make(25);
165 EXPECT_NE(found, dup);
166 EXPECT_EQ(
KEY(found), 25);
170 auto * removed = t.
remove(25);
171 ASSERT_NE(removed,
nullptr);
172 EXPECT_EQ(
KEY(removed), 25);
176TEST(AvlTree, DuplicateAfterOnlyLeftDescents)
186 NodePool<Avl_Tree<int>> pool;
188 ASSERT_NE(t.
insert(pool.make(100)),
nullptr);
189 ASSERT_NE(t.
insert(pool.make(50)),
nullptr);
190 ASSERT_NE(t.
insert(pool.make(25)),
nullptr);
193 ASSERT_NE(t.
insert(pool.make(10)),
nullptr);
197 EXPECT_EQ(t.
insert(pool.make(25)),
nullptr);
201TEST(AvlTree, DuplicateDeepInTree)
205 NodePool<Avl_Tree<int>> pool;
208 for (
int k : {50, 25, 75, 12, 37, 62, 87, 6, 18, 31, 43})
209 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
214 EXPECT_EQ(t.
insert(pool.make(50)),
nullptr);
215 EXPECT_EQ(t.
insert(pool.make(25)),
nullptr);
216 EXPECT_EQ(t.
insert(pool.make(37)),
nullptr);
217 EXPECT_EQ(t.
insert(pool.make(31)),
nullptr);
222 for (
int k : {31, 37, 25, 50})
225 ASSERT_NE(rem,
nullptr);
226 EXPECT_EQ(
KEY(rem),
k);
231TEST(AvlTree, InsertDupAllowsDuplicates)
234 NodePool<Avl_Tree<int>> pool;
236 ASSERT_NE(t.
insert_dup(pool.make(5)),
nullptr);
237 ASSERT_NE(t.
insert_dup(pool.make(5)),
nullptr);
238 ASSERT_NE(t.
insert_dup(pool.make(5)),
nullptr);
244TEST(AvlTree, RemoveMissingReturnsNull)
247 NodePool<Avl_Tree<int>> pool;
248 for (
int k : {1, 2, 3})
249 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
251 EXPECT_EQ(t.
remove(42),
nullptr);
255TEST(AvlTree, RemoveReturnsDetachedNode)
258 NodePool<Avl_Tree<int>> pool;
259 for (
int k : {3, 1, 4, 2})
260 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
262 auto * removed = t.
remove(1);
263 ASSERT_NE(removed,
nullptr);
264 EXPECT_EQ(
KEY(removed), 1);
267 EXPECT_EQ(
static_cast<int>(
DIFF(removed)), 0);
271 pool.forget(removed);
275TEST(AvlTree, Property_RandomInsertRemove_StableInvariants)
278 NodePool<Avl_Tree<int>> pool;
280 std::mt19937
rng(12345);
281 std::uniform_int_distribution<int> dist(0, 500);
283 std::set<int> present;
285 for (
int i = 0; i < 300; ++i)
288 auto * p = pool.make(
k);
301 for (
int i = 0; i < 200; ++i)
305 if (removed !=
nullptr)
308 pool.forget(removed);
315TEST(AvlTree, Rotations_LL_RR_LR_RL)
319 NodePool<Avl_Tree<int>> pool;
320 ASSERT_NE(t.
insert(pool.make(3)),
nullptr);
321 ASSERT_NE(t.
insert(pool.make(2)),
nullptr);
322 ASSERT_NE(t.
insert(pool.make(1)),
nullptr);
329 NodePool<Avl_Tree<int>> pool;
330 ASSERT_NE(t.
insert(pool.make(1)),
nullptr);
331 ASSERT_NE(t.
insert(pool.make(2)),
nullptr);
332 ASSERT_NE(t.
insert(pool.make(3)),
nullptr);
339 NodePool<Avl_Tree<int>> pool;
340 ASSERT_NE(t.
insert(pool.make(3)),
nullptr);
341 ASSERT_NE(t.
insert(pool.make(1)),
nullptr);
342 ASSERT_NE(t.
insert(pool.make(2)),
nullptr);
349 NodePool<Avl_Tree<int>> pool;
350 ASSERT_NE(t.
insert(pool.make(1)),
nullptr);
351 ASSERT_NE(t.
insert(pool.make(3)),
nullptr);
352 ASSERT_NE(t.
insert(pool.make(2)),
nullptr);
358TEST(AvlTree, Property_OracleSet_MatchesInorder)
361 NodePool<Avl_Tree<int>> pool;
363 std::mt19937
rng(123456);
364 std::uniform_int_distribution<int> dist(0, 2000);
365 std::bernoulli_distribution do_insert(0.6);
369 for (
int i = 0; i < 1500; ++i)
374 auto * p = pool.make(
k);
386 if (
auto * removed = t.
remove(
k); removed !=
nullptr)
389 pool.forget(removed);
397 auto got = inorder_keys<Avl_Tree<int>::Node>(t.
getRoot());
402TEST(AvlTree, WorksWithCustomComparator)
406 bool operator()(
int a,
int b)
const noexcept {
return a > b; }
410 NodePool<Avl_Tree<int, Greater>> pool;
412 for (
int k : {1, 2, 3, 4, 5})
413 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
416 auto got = inorder_keys<Avl_Tree<int, Greater>::Node>(t.
getRoot());
417 const std::vector<int>
expected{5, 4, 3, 2, 1};
420 auto * removed = t.
remove(4);
421 ASSERT_NE(removed,
nullptr);
422 pool.forget(removed);
432TEST(AvlTree, DuplicateMaxKeyZeroPopBranch)
435 NodePool<Avl_Tree<int>> pool;
438 ASSERT_NE(t.
insert(pool.make(10)),
nullptr);
439 ASSERT_NE(t.
insert(pool.make(20)),
nullptr);
440 ASSERT_NE(t.
insert(pool.make(30)),
nullptr);
445 EXPECT_EQ(t.
insert(pool.make(30)),
nullptr);
449 auto * dup = pool.make(30);
451 EXPECT_NE(found, dup);
452 EXPECT_EQ(
KEY(found), 30);
458TEST(AvlTree, Property_DuplicateMax_RandomisedStress)
460 std::mt19937
rng(77777);
461 std::uniform_int_distribution<int> n_dist(1, 50);
463 for (
int trial = 0; trial < 200; ++trial)
466 NodePool<Avl_Tree<int>> pool;
470 for (
int i = 1; i <= n; ++i)
472 ASSERT_NE(t.
insert(pool.make(i)),
nullptr);
477 EXPECT_EQ(t.
insert(pool.make(max_key)),
nullptr);
478 ASSERT_TRUE(t.
verify()) <<
"Invariant violated after duplicate-max on trial " << trial;
487TEST(AvlTree, Stress_AscendingInsertion)
490 NodePool<Avl_Tree<int>> pool;
494 for (
int i = 0; i < n; ++i)
496 ASSERT_NE(t.
insert(pool.make(i)),
nullptr) <<
"Insert failed at i=" << i;
497 ASSERT_TRUE(t.
verify()) <<
"AVL invariant violated at i=" << i;
501 for (
int i = 0; i < n; ++i)
502 ASSERT_NE(t.
search(i),
nullptr) <<
"Element " << i <<
" not found";
506TEST(AvlTree, Stress_DescendingInsertion)
509 NodePool<Avl_Tree<int>> pool;
513 for (
int i = n - 1; i >= 0; --i)
515 ASSERT_NE(t.
insert(pool.make(i)),
nullptr);
519 for (
int i = 0; i < n; ++i)
520 ASSERT_NE(t.
search(i),
nullptr);
524TEST(AvlTree, Stress_ZigzagInsertion)
527 NodePool<Avl_Tree<int>> pool;
532 for (
int i = 0; i < n / 2; ++i)
534 ASSERT_NE(t.
insert(pool.make(i)),
nullptr);
536 ASSERT_NE(t.
insert(pool.make(n - 1 - i)),
nullptr);
540 for (
int i = 0; i < n; ++i)
541 ASSERT_NE(t.
search(i),
nullptr);
545TEST(AvlTree, Fuzz_LargeScaleRandomOps)
548 NodePool<Avl_Tree<int>> pool;
550 std::mt19937
rng(99999);
551 std::uniform_int_distribution<int> key_dist(0, 50000);
552 std::uniform_int_distribution<int> op_dist(0, 2);
556 const int num_ops = 20000;
558 for (
int i = 0; i < num_ops; ++i)
560 int key = key_dist(
rng);
561 int op = op_dist(
rng);
567 auto * p = pool.make(key);
573 EXPECT_TRUE(
oracle.count(key) > 0);
577 EXPECT_TRUE(
oracle.count(key) == 0);
584 auto * removed = t.
remove(key);
585 if (removed !=
nullptr)
587 EXPECT_TRUE(
oracle.count(key) > 0);
589 pool.forget(removed);
594 EXPECT_TRUE(
oracle.count(key) == 0);
600 auto * found = t.
search(key);
601 bool in_oracle =
oracle.count(key) > 0;
602 EXPECT_EQ(found !=
nullptr, in_oracle);
604 EXPECT_EQ(
KEY(found), key);
609 ASSERT_TRUE(t.
verify()) <<
"AVL invariant violated at op " << i;
614 auto got = inorder_keys<Avl_Tree<int>::Node>(t.
getRoot());
619TEST(AvlTree, Stress_BulkInsertBulkRemove)
622 NodePool<Avl_Tree<int>> pool;
627 std::vector<int>
keys(n);
628 std::iota(
keys.begin(),
keys.end(), 0);
630 std::mt19937
rng(12345);
635 ASSERT_NE(t.
insert(pool.make(
k)),
nullptr);
646 ASSERT_NE(removed,
nullptr) <<
"Remove failed for key " <<
k;
647 pool.forget(removed);
656TEST(AvlTree, Stress_ManyDuplicates)
659 NodePool<Avl_Tree<int>> pool;
662 const int dups_per_key = 50;
667 for (
int d = 0; d < dups_per_key; ++d)
676 auto keys = inorder_keys<Avl_Tree<int>::Node>(t.
getRoot());
680 EXPECT_TRUE(std::is_sorted(
keys.begin(),
keys.end()));
684TEST(AvlTree, Stress_AlternatingInsertRemove)
687 NodePool<Avl_Tree<int>> pool;
689 std::set<int> present;
690 std::mt19937
rng(54321);
691 std::uniform_int_distribution<int> dist(0, 10000);
695 for (
int i = 0; i < n; ++i)
701 auto * p = pool.make(
k);
702 if (t.
insert(p) ==
nullptr)
714 if (
auto * removed = t.
remove(
k); removed)
717 pool.forget(removed);
722 ASSERT_TRUE(t.
verify()) <<
"AVL invariant violated at i=" << i;
726 std::vector<int>
expected(present.begin(), present.end());
731TEST(AvlTree, Stress_StringKeys)
734 NodePool<Avl_Tree<std::string>> pool;
736 std::mt19937
rng(11111);
737 std::uniform_int_distribution<int> len_dist(1, 30);
738 std::uniform_int_distribution<int> char_dist(
'a',
'z');
741 int len = len_dist(
rng);
744 for (
int i = 0; i < len; ++i)
745 s +=
static_cast<char>(char_dist(
rng));
749 std::set<std::string>
oracle;
753 for (
int i = 0; i < n; ++i)
756 auto * p = pool.make(s);
757 if (t.
insert(p) !=
nullptr)
769 for (
const auto & s :
oracle)
770 ASSERT_NE(t.
search(s),
nullptr) <<
"String key missing: " << s;
static string random_string(std::mt19937 &rng, size_t len)
bool is_avl(Node *p)
Validate that a tree satisfies AVL properties.
#define DIFF(p)
Access the balance factor of node p.
WeightedDigraph::Node Node
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node pointed by p in the tree.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove from an AVL tree the node containing key key.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert a key.
bool verify() const
Return true if the tree is a valid AVL tree: the balance of every node and the order of the keys.
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node p without testing for key duplicity.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
Minimal std::expected-style result type for C++20.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Main namespace for Aleph-w library functions.
std::vector< int > inorder_keys(NodeT *root)
AVL binary search tree with nodes without a virtual destructor.
ValueArg< size_t > num_keys
AVL tree implementation (height-balanced BST).