Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_binnode_concepts_test.cc
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
3#include <tpl_avl.H>
4#include <tpl_avlRk.H>
5#include <tpl_binNode.H>
6#include <tpl_binNodeUtils.H>
7#include <tpl_binNodeXt.H>
8#include <tpl_rand_tree.H>
9#include <tpl_rb_tree.H>
10#include <tpl_treap.H>
11#include <tpl_treapRk.H>
12
13// Positive/negative checks for BinNodeLike / RankedBinNodeLike (Phase 3 of
14// aleph-concepts-plan.md) and for the node utilities they now constrain.
15
16using namespace Aleph;
17
18static_assert(BinNodeLike<BinNode<int>> and BinNodeLike<BinNodeVtl<int>>);
19static_assert(BinNodeLike<AvlNode<int>> and BinNodeLike<RbNode<int>> and BinNodeLike<TreapNode<int>>);
20static_assert(BinNodeLike<RandNode<int>> and BinNodeLike<BinNodeXt<int>> and BinNodeLike<AvlNodeRk<int>>);
21// Utilities are also instantiated with const nodes, whose getL() returns by value.
22static_assert(BinNodeLike<const BinNode<int>>);
23static_assert(not BinNodeLike<int> and not BinNodeLike<DynList<int>>);
24
25static_assert(RankedBinNodeLike<BinNodeXt<int>> and RankedBinNodeLike<AvlNodeRk<int>>);
26static_assert(RankedBinNodeLike<Treap_Rk_Node<int>> and RankedBinNodeLike<RandNode<int>>);
27static_assert(not RankedBinNodeLike<BinNode<int>> and not RankedBinNodeLike<AvlNode<int>>);
28
29// select() on a node without subtree sizes used to fail inside
30// tpl_binNodeXt.H ("no member named getCount"); now the call is not viable.
31template <class Node>
32concept can_select = requires(Node * r) { Aleph::select(r, size_t(0)); };
33template <class Node>
34concept can_compute_height = requires(Node * r) { computeHeightRec(r); };
35
38
39TEST(AhBinNodeConceptsTest, ConstrainedUtilitiesStillRun)
40{
41 using Node = BinNodeXt<int>;
42 Node * root = Node::NullPtr;
43 for (int k : {5, 2, 8, 1, 9})
44 ASSERT_NE(insert_by_key_xt(root, new Node(k)), Node::NullPtr);
45
46 EXPECT_EQ(KEY(Aleph::select(root, 0)), 1);
47 EXPECT_EQ(KEY(Aleph::select(root, 4)), 9);
48 EXPECT_EQ(computeHeightRec(root), 3u);
50}
51
52// Without a sentinel, nullptr is the empty tree and stays valid.
53TEST(AhBinNodeConceptsTest, NullptrIsTheEmptyTreeWithoutSentinel)
54{
55 BinNode<int> * root = nullptr;
56 EXPECT_EQ(searchInBinTree(root, 1), nullptr);
57}
58
59#ifndef NDEBUG
60// With a sentinel (BinNodeXt), a nullptr root is a corrupted tree: debug
61// builds report it instead of dereferencing it.
62TEST(AhBinNodeConceptsDeathTest, NullptrRootInSentinelTreeIsReported)
63{
64 using Node = BinNodeXt<int>;
65 Node * root = nullptr;
66 EXPECT_DEATH((void) searchInBinTree(root, 1), "use Node::NullPtr");
67}
68#endif
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
Node for extended binary search tree.
Node for binary search tree.
#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
Node * insert_by_key_xt(Node *&r, Node *p, Compare &cmp) noexcept
Insert a node in an extended binary search tree.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Search a key in a binary search tree using optimistic search.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
static int * k
Gsl_Compatible_Rng r
AVL tree with rank (order statistics).
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Extended binary node with subtree count.
Basic binary tree node definitions.
Randomized binary search tree.
Red-Black tree implementation (bottom-up balancing).
Treap with rank (order statistics).
Treap: randomized BST combining tree and heap properties.