43#include <gtest/gtest.h>
60 using Positional_Iterator_Trees =
64 class RankedTreeIterator :
public ::testing::Test
73 for (
const int key : {5, 3, 8, 1, 4})
74 ASSERT_NE(tree.
insert(pool.
make(key)),
nullptr);
81 template <
class Iterator>
85 for (; it.has_curr(); it.next_ne())
91 template <
template <
typename,
class>
class Tree>
92 void expect_the_set_iterates_in_order()
95 for (
const int key : {5, 3, 8, 1, 4})
99 for (
auto it = set.
get_it(); it.has_curr(); it.next_ne())
104 for (
const int key : set)
113TYPED_TEST(RankedTreeIterator, IteratesInOrderWithThePositions)
116 typename TypeParam::Iterator it(this->tree);
118 for (; it.has_curr(); it.next_ne(), ++pos)
122 EXPECT_EQ(it.get_pos(), pos);
125 EXPECT_EQ(it.get_curr(),
nullptr);
128TEST(RankedTreeIteratorSet, TheSetsOfTheseTreesIterateInOrder)
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>();
139 using Iterator =
typename TypeParam::Iterator;
140 auto expect_the_end = [](
const Iterator & it,
const char * how)
142 EXPECT_FALSE(it.has_curr()) << how;
143 EXPECT_EQ(it.get_curr(),
nullptr) << how;
144 EXPECT_EQ(it.get_curr_ne(),
nullptr) << how;
147 Iterator it(this->tree);
149 expect_the_end(it,
"reset_to_pos(size())");
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");
157 expect_the_end(it,
"next_ne() at the end");
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());
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}));
179TYPED_TEST(RankedTreeIterator, ResetToKeyGoesToTheNodeOfTheKey)
181 typename TypeParam::Iterator it(this->tree);
182 ASSERT_EQ(
KEY(it.get_curr()), 1);
184 ASSERT_TRUE(it.has_curr());
185 EXPECT_EQ(
KEY(it.get_curr()), 5);
186 EXPECT_EQ(it.get_pos(), 3u);
192TYPED_TEST(RankedTreeIterator, AnIteratorBuiltByDefaultHasNoNode)
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);
203TYPED_TEST(RankedTreeIterator, ResetToNodeKeepsTheNodeAmongEqualKeys)
205 using Node =
typename TypeParam::Node;
207 for (
int i = 0; i < 4; ++i)
208 equal.append(this->tree.insert_dup(this->pool.make(4)));
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);
215 for (
size_t i = 0; i <
equal.size(); ++i)
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]);
235TYPED_TEST(RankedTreeIterator, APositionThatAnIntCannotHoldIsTheEnd)
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})
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)
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);
256 if constexpr (std::numeric_limits<size_t>::max() > std::numeric_limits<unsigned>::max())
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);
263 Iterator it(this->tree);
264 it.reset_to_pos(far);
265 EXPECT_FALSE(it.has_curr());
266 EXPECT_EQ(it.get_curr(),
nullptr);
268 Iterator moved = built;
269 for (
int i = 0; i < 2; ++i)
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);
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
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.
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.
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)
bool equal(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Test if two ranges are equal.
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...
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.
Dynamic array container with automatic resizing.
Dynamic set implementations based on balanced binary search trees.