Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ranked_tree_iterator_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
43#include <gtest/gtest.h>
44
45#include <cstddef>
46#include <limits>
47#include <string>
48
49#include <tpl_array.H>
50
51#include <tpl_dynSetTree.H>
52
54
55using namespace search_trees_test;
56
57namespace
58{
60 using Positional_Iterator_Trees =
61 ::testing::Types<Avl_Tree_Rk<int>, Rb_Tree_Rk<int>, Treap_Rk<int>>;
62
63 template <class Tree>
64 class RankedTreeIterator : public ::testing::Test
65 {
66 protected:
67 Tree tree;
69
70 // The keys 1, 3, 4, 5 and 8, at the positions 0 to 4
71 void SetUp() override
72 {
73 for (const int key : {5, 3, 8, 1, 4})
74 ASSERT_NE(tree.insert(pool.make(key)), nullptr);
75 }
76 };
77
78 TYPED_TEST_SUITE(RankedTreeIterator, Positional_Iterator_Trees, Tree_Names);
79
81 template <class Iterator>
82 Array<int> keys_from(Iterator it)
83 {
85 for (; it.has_curr(); it.next_ne())
86 keys.append(KEY(it.get_curr()));
87 return keys;
88 }
89
91 template <template <typename, class> class Tree>
92 void expect_the_set_iterates_in_order()
93 {
95 for (const int key : {5, 3, 8, 1, 4})
96 set.insert(key);
97
99 for (auto it = set.get_it(); it.has_curr(); it.next_ne())
100 keys.append(it.get_curr());
101 EXPECT_EQ(keys, (Array<int>{1, 3, 4, 5, 8}));
102
103 keys.clear();
104 for (const int key : set)
105 keys.append(key);
106 EXPECT_EQ(keys, (Array<int>{1, 3, 4, 5, 8}));
107 }
108}
109
110// The iterator of Avl_Tree_Rk and Rb_Tree_Rk, BinTreeXt_Iterator, named
111// BinTreeXt_Operation before its header declared it: has_curr() did not
112// compile, so that those trees, and their sets, could not be iterated
113TYPED_TEST(RankedTreeIterator, IteratesInOrderWithThePositions)
114{
115 const Array<int> expected = {1, 3, 4, 5, 8};
116 typename TypeParam::Iterator it(this->tree);
117 size_t pos = 0;
118 for (; it.has_curr(); it.next_ne(), ++pos)
119 {
120 ASSERT_LT(pos, expected.size());
121 EXPECT_EQ(KEY(it.get_curr()), expected[pos]);
122 EXPECT_EQ(it.get_pos(), pos);
123 }
124 EXPECT_EQ(pos, expected.size());
125 EXPECT_EQ(it.get_curr(), nullptr);
126}
127
128TEST(RankedTreeIteratorSet, TheSetsOfTheseTreesIterateInOrder)
129{
130 expect_the_set_iterates_in_order<Avl_Tree_Rk>();
131 expect_the_set_iterates_in_order<Rb_Tree_Rk>();
132 expect_the_set_iterates_in_order<Treap_Rk>();
133}
134
135// Every way of reaching a position without node: get_curr() threw
136// out_of_range from select() inside noexcept, and the program ended
137TYPED_TEST(RankedTreeIterator, APositionPastTheEndIsTheEnd)
138{
139 using Iterator = typename TypeParam::Iterator;
140 auto expect_the_end = [](const Iterator & it, const char * how)
141 {
142 EXPECT_FALSE(it.has_curr()) << how;
143 EXPECT_EQ(it.get_curr(), nullptr) << how;
144 EXPECT_EQ(it.get_curr_ne(), nullptr) << how;
145 };
146
147 Iterator it(this->tree);
148 it.reset_to_pos(5);
149 expect_the_end(it, "reset_to_pos(size())");
150 it.reset_to_pos(7);
151 expect_the_end(it, "reset_to_pos() past the end");
152 expect_the_end(Iterator(this->tree, size_t{7}), "a constructor with a position past the end");
153
154 it.reset_last();
155 it.next_ne();
156 it.next_ne();
157 expect_the_end(it, "next_ne() at the end");
158
159 it.reset_last(); // on the key 8, at the position 4
160 ASSERT_NE(this->tree.remove(1), nullptr);
161 ASSERT_NE(this->tree.remove(3), nullptr);
162 expect_the_end(it, "keys removed from the tree");
163 EXPECT_TRUE(this->tree.verify());
164}
165
166// reset_to_node() of Treap_Rk marked the position as that of an empty tree:
167// has_curr() was false on a node of the tree
168TYPED_TEST(RankedTreeIterator, ResetToNodeStartsAtThatNode)
169{
170 typename TypeParam::Iterator it(this->tree);
171 it.reset_to_node(this->tree.search(4));
172 ASSERT_TRUE(it.has_curr());
173 EXPECT_EQ(it.get_pos(), 2u);
174 EXPECT_EQ(keys_from(it), (Array<int>{4, 5, 8}));
175}
176
177// reset_to_key() moved the position but kept the node that get_curr() had
178// found: get_curr() gave the node of the previous position
179TYPED_TEST(RankedTreeIterator, ResetToKeyGoesToTheNodeOfTheKey)
180{
181 typename TypeParam::Iterator it(this->tree);
182 ASSERT_EQ(KEY(it.get_curr()), 1);
183 it.reset_to_key(5);
184 ASSERT_TRUE(it.has_curr());
185 EXPECT_EQ(KEY(it.get_curr()), 5);
186 EXPECT_EQ(it.get_pos(), 3u);
187 EXPECT_EQ(keys_from(it), (Array<int>{5, 8}));
188}
189
190// An iterator built by default has no tree: get_curr_ne() read the root of
191// the tree that it does not have before it saw that it has no position
192TYPED_TEST(RankedTreeIterator, AnIteratorBuiltByDefaultHasNoNode)
193{
194 typename TypeParam::Iterator it;
195 EXPECT_FALSE(it.has_curr());
196 EXPECT_EQ(it.get_curr_ne(), nullptr);
197 EXPECT_EQ(it.get_curr(), nullptr);
198}
199
200// The position of a node that the iterator was put on is that of the node:
201// among equal keys, the search by its key found another node, which the
202// iterator took as its current one
203TYPED_TEST(RankedTreeIterator, ResetToNodeKeepsTheNodeAmongEqualKeys)
204{
205 using Node = typename TypeParam::Node;
206 Array<Node *> equal = {this->tree.search(4)};
207 for (int i = 0; i < 4; ++i)
208 equal.append(this->tree.insert_dup(this->pool.make(4)));
209
210 Array<Node *> in_order; // by position, without searching by key
211 for (typename TypeParam::Iterator it(this->tree); it.has_curr(); it.next_ne())
212 in_order.append(it.get_curr());
213 ASSERT_EQ(in_order.size(), 9u);
214
215 for (size_t i = 0; i < equal.size(); ++i)
216 {
217 SCOPED_TRACE("the node " + std::to_string(i) + " of the key 4");
218 typename TypeParam::Iterator it(this->tree);
219 it.reset_to_node(equal[i]);
220 ASSERT_TRUE(it.has_curr());
221 EXPECT_EQ(it.get_curr(), equal[i]);
222 const size_t pos = it.get_pos();
223 ASSERT_LT(pos, in_order.size());
224 EXPECT_EQ(in_order[pos], equal[i]);
225 }
226}
227
228// A position past the end that an int cannot hold is the end too: kept in
229// an int, it lost its high bits, and 2^32 + 2 was the position 2. Kept as
230// the greatest int, next_ne() overflowed it, which the sanitizer of
231// undefined behavior reports; so did two steps from the greatest int but
232// one, which an int holds. The positions near the greatest int, which a
233// size_t holds wherever an int is narrower, are tested on every platform;
234// 2^32 + 2, where a size_t holds it
235TYPED_TEST(RankedTreeIterator, APositionThatAnIntCannotHoldIsTheEnd)
236{
237 using Iterator = typename TypeParam::Iterator;
238 constexpr auto int_max = static_cast<size_t>(std::numeric_limits<int>::max());
239 for (const size_t pos : {int_max - 1, int_max})
240 {
241 SCOPED_TRACE("the position " + std::to_string(pos));
242 Iterator it(this->tree, pos);
243 Iterator reset(this->tree);
244 reset.reset_to_pos(pos);
245 for (int i = 0; i < 3; ++i)
246 {
247 EXPECT_FALSE(it.has_curr()) << "after " << i << " steps";
248 EXPECT_EQ(it.get_curr(), nullptr);
249 EXPECT_FALSE(reset.has_curr()) << "after " << i << " steps";
250 EXPECT_EQ(reset.get_curr(), nullptr);
251 it.next_ne();
252 reset.next_ne();
253 }
254 }
255
256 if constexpr (std::numeric_limits<size_t>::max() > std::numeric_limits<unsigned>::max())
257 {
258 const size_t far = size_t{std::numeric_limits<unsigned>::max()} + 3;
259 const Iterator built(this->tree, far);
260 EXPECT_FALSE(built.has_curr());
261 EXPECT_EQ(built.get_curr(), nullptr);
262
263 Iterator it(this->tree);
264 it.reset_to_pos(far);
265 EXPECT_FALSE(it.has_curr());
266 EXPECT_EQ(it.get_curr(), nullptr);
267
268 Iterator moved = built;
269 for (int i = 0; i < 2; ++i)
270 {
271 it.next_ne();
272 moved.next_ne();
273 }
274 EXPECT_FALSE(it.has_curr()) << "next_ne() at that end";
275 EXPECT_EQ(it.get_curr(), nullptr);
276 EXPECT_FALSE(moved.has_curr()) << "next_ne() at that end";
277 EXPECT_EQ(moved.get_curr(), nullptr);
278 }
279}
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Minimal std::expected-style result type for C++20.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
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
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)
bool equal(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Test if two ranges are equal.
Definition ahAlgo.H:482
TYPED_TEST(RankedTreeIterator, IteratesInOrderWithThePositions)
What the tests of the binary search trees share: the list of tree types, what each node carries,...
Red-Black binary search tree with nodes without virtual destructor and with subtree counters for sele...
Definition tpl_rbRk.H:1653
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
Names the instances of a typed test suite after their tree.
int keys[]
Dynamic array container with automatic resizing.
Dynamic set implementations based on balanced binary search trees.