59#include <gtest/gtest.h>
95 Heavy() : weight(40,
'x') {}
97 Heavy(
const int i) : id(i), weight(40,
'y') {}
99 friend bool operator < (
const Heavy & a,
const Heavy & b)
noexcept {
return a.id < b.id; }
109 Copied() : weight(40,
'x') {}
111 Copied(
const int i) : id(i), weight(40,
'y') {}
113 Copied(
const Copied & o) : id(o.id), weight(o.weight) {}
115 Copied & operator = (
const Copied & o)
122 friend bool operator < (
const Copied & a,
const Copied & b)
noexcept {
return a.id < b.id; }
125 int id_of(
const int key)
noexcept {
return key; }
127 int id_of(
const Heavy & key)
noexcept {
return key.id; }
129 int id_of(
const Copied & key)
noexcept {
return key.id; }
134 template <
typename Key>
137 bool descending =
false;
139 bool operator () (
const Key & a,
const Key & b)
const noexcept
141 return descending ? b < a : a < b;
148 template <
typename Key>
149 struct Less_That_Tells_A_Move
151 bool moved_from =
false;
153 Less_That_Tells_A_Move() noexcept = default;
155 Less_That_Tells_A_Move(const Less_That_Tells_A_Move &) noexcept = default;
157 Less_That_Tells_A_Move(Less_That_Tells_A_Move && o) noexcept { o.moved_from =
true; }
159 Less_That_Tells_A_Move & operator = (
const Less_That_Tells_A_Move &)
noexcept =
default;
161 bool operator () (
const Key & a,
const Key & b)
const noexcept {
return a < b; }
165 struct Copy_Failure : std::runtime_error
167 Copy_Failure() :
std::runtime_error(
"the copy of the comparison failed") {}
170 bool copies_fail =
false;
173 template <
typename Key>
174 struct Less_Whose_Copy_Fails
176 Less_Whose_Copy_Fails() noexcept = default;
178 Less_Whose_Copy_Fails(const Less_Whose_Copy_Fails &)
181 throw Copy_Failure();
184 Less_Whose_Copy_Fails & operator = (
const Less_Whose_Copy_Fails &)
noexcept =
default;
186 bool operator () (
const Key & a,
const Key & b)
const noexcept {
return a < b; }
190 constexpr int Ids[] = {50, 30, 80, 10, 40, 70, 90, 20, 60};
191 constexpr size_t N = std::size(Ids);
194 template <
class Node>
197 if (
root == Node::NullPtr)
204 template <
class Tree>
208 ids_in_order(tree.getRoot(), ids);
212 template <
class Tree>
213 size_t count_of(
Tree & tree) {
return ids_of(tree).
size(); }
218 template <
class Tree>
222 using Key =
typename Node::key_type;
228 ADD_FAILURE() <<
"the tree did not take " << id;
231 EXPECT_EQ(q, p) <<
"the tree took " <<
id <<
" and gave back another node";
237 template <
class Tree>
238 size_t insert_ids(
Tree & tree,
const int first,
const int last,
const int step = 1)
241 for (
int id = first;
id <= last;
id += step, ++n)
242 (
void) insert_own(tree,
id);
249 template <
class Tree>
250 void free_nodes(
Tree & tree)
252 using Key =
typename Tree::Node::key_type;
254 for (
size_t i = 0; i < ids.
size(); ++i)
256 auto * p = tree.
remove(Key(ids(i)));
257 EXPECT_NE(p,
nullptr) <<
"the tree does not find " << ids(i);
260 EXPECT_EQ(tree.getRoot(), Tree::Node::NullPtr);
265 template <
class Tree>
271 explicit Freeing_Nodes(
Tree & t) noexcept : tree(t) {}
273 ~Freeing_Nodes() { free_nodes(tree); }
275 Freeing_Nodes(
const Freeing_Nodes &) =
delete;
276 Freeing_Nodes & operator = (
const Freeing_Nodes &) =
delete;
279 template <
class Tree>
280 void expect_valid(
Tree & tree,
const char * which)
282 EXPECT_TRUE(tree.verify()) << which;
283 if constexpr (
requires { tree.size(); })
284 EXPECT_EQ(tree.size(), count_of(tree)) << which <<
": its count of nodes";
288 template <
class Tree>
296 template <
class... Args>
297 explicit Filled(Args &&... args) : tree(
std::forward<Args>(args)...)
299 for (
size_t i = 0; i <
N; ++i)
300 nodes[i] = insert_own(tree, Ids[i]);
303 ~Filled() { free_nodes(tree); }
305 Filled(
const Filled &) =
delete;
306 Filled & operator = (
const Filled &) =
delete;
310 template <
class Tree>
311 void expect_the_nodes_of(
const Filled<Tree> & filled,
Tree & tree,
const char * which)
313 using Key =
typename Tree::Node::key_type;
314 EXPECT_EQ(count_of(tree),
N) << which;
315 for (
size_t i = 0; i <
N; ++i)
316 EXPECT_EQ(tree.
search(Key(Ids[i])), filled.nodes[i]) << which <<
", key " << Ids[i];
317 expect_valid(tree, which);
320 template <
class Tree>
321 void expect_empty(
Tree & tree,
const char * which)
323 using Key =
typename Tree::Node::key_type;
324 EXPECT_EQ(tree.getRoot(), Tree::Node::NullPtr) << which;
325 EXPECT_EQ(tree.
search(Key(Ids[0])),
nullptr) << which;
326 expect_valid(tree, which);
330 template <
class Tree>
331 void expect_usable(
Tree & tree,
const char * which)
333 using Key =
typename Tree::Node::key_type;
334 const size_t before = count_of(tree);
335 const size_t added = insert_ids(tree, 101, 119, 2);
336 EXPECT_EQ(count_of(tree), before + added) << which;
337 expect_valid(tree, which);
338 for (
int id = 101;
id <= 119;
id += 2)
340 auto * p = tree.
remove(Key(
id));
341 ASSERT_NE(p,
nullptr) << which <<
", key " << id;
342 EXPECT_EQ(id_of(
KEY(p)),
id) << which;
345 EXPECT_EQ(count_of(tree), before) << which;
346 expect_valid(tree, which);
350 template <
template <
typename,
class>
class T>
353 template <
typename Key,
class Compare = Aleph::less<Key>>
357 template <
class Kind>
358 class Tree_Move :
public ::testing::Test {};
360 using Tree_Kinds = ::testing::Types<
361 Of<BinTree>, Of<BinTreeVtl>, Of<Avl_Tree>, Of<Avl_Tree_Vtl>,
362 Of<Avl_Tree_Rk>, Of<Avl_Tree_Rk_Vtl>, Of<Rb_Tree>, Of<Rb_Tree_Vtl>,
363 Of<Rb_Tree_Rk>, Of<Rb_Tree_Rk_Vtl>, Of<HtdRbTree>, Of<HtdRbTreeVtl>,
364 Of<HtdRbTreeRk>, Of<HtdRbTreeRkVtl>, Of<TdRbTree>, Of<TdRbTreeVtl>,
365 Of<TdRbTreeRk>, Of<TdRbTreeRkVtl>, Of<Splay_Tree>, Of<Splay_Tree_Vtl>,
366 Of<Splay_Tree_Rk>, Of<Splay_Tree_Rk_Vtl>, Of<Treap>, Of<Treap_Vtl>,
367 Of<Treap_Rk>, Of<Treap_Rk_Vtl>, Of<Rand_Tree>, Of<Rand_Tree_Vtl>>;
372 template <
class Tree>
373 void check_the_move_of_a_tree_with_nodes()
376 Tree taken(std::move(filled.tree));
377 const Freeing_Nodes<Tree> freeing_taken(taken);
378 expect_the_nodes_of(filled, taken,
"the tree that took the nodes");
379 expect_empty(filled.tree,
"the tree that was moved from");
380 expect_usable(filled.tree,
"the tree that was moved from");
381 expect_usable(taken,
"the tree that took the nodes");
382 expect_the_nodes_of(filled, taken,
"the tree that took the nodes, after its use");
387 template <
class Tree>
388 void check_the_move_of_a_tree_without_nodes()
391 const Freeing_Nodes<Tree> freeing_empty(empty);
392 Tree from_empty(std::move(empty));
393 const Freeing_Nodes<Tree> freeing_from_empty(from_empty);
394 expect_empty(empty,
"the empty tree that was moved from");
395 expect_empty(from_empty,
"the tree built from an empty one");
396 expect_usable(empty,
"the empty tree that was moved from");
397 expect_usable(from_empty,
"the tree built from an empty one");
400 Tree taken(std::move(filled.tree));
401 const Freeing_Nodes<Tree> freeing_taken(taken);
402 Tree from_moved(std::move(filled.tree));
403 const Freeing_Nodes<Tree> freeing_from_moved(from_moved);
404 expect_empty(filled.tree,
"the tree that was moved from twice");
405 expect_empty(from_moved,
"the tree built from one that was moved from");
406 expect_usable(filled.tree,
"the tree that was moved from twice");
407 expect_usable(from_moved,
"the tree built from one that was moved from");
409 Tree last(std::move(taken));
410 const Freeing_Nodes<Tree> freeing_last(last);
411 expect_empty(taken,
"the second tree");
412 expect_the_nodes_of(filled, last,
"the third tree");
413 expect_usable(taken,
"the second tree");
417TYPED_TEST(Tree_Move, TakesTheNodesAndLeavesTheOtherTreeEmptyAndUsable)
419 check_the_move_of_a_tree_with_nodes<typename TypeParam::template Tree<int>>();
420 check_the_move_of_a_tree_with_nodes<typename TypeParam::template Tree<Heavy>>();
421 check_the_move_of_a_tree_with_nodes<typename TypeParam::template Tree<Copied>>();
426 check_the_move_of_a_tree_without_nodes<typename TypeParam::template Tree<int>>();
427 check_the_move_of_a_tree_without_nodes<typename TypeParam::template Tree<Heavy>>();
428 check_the_move_of_a_tree_without_nodes<typename TypeParam::template Tree<Copied>>();
436 using Less = Flag_Less<int>;
438 Filled<Tree> filled(
Less{
true});
439 Tree taken(std::move(filled.tree));
440 const Freeing_Nodes<Tree> freeing_taken(taken);
441 EXPECT_TRUE(taken.get_compare().descending);
442 EXPECT_TRUE(filled.tree.get_compare().descending);
443 expect_the_nodes_of(filled, taken,
"the tree that took the nodes");
447 (void) insert_ids(filled.tree, 1, 2);
449 ASSERT_EQ(ids.
size(), 2u);
450 EXPECT_EQ(ids(0), 2);
451 EXPECT_EQ(ids(1), 1);
452 expect_valid(filled.tree,
"the tree that was moved from");
455 ASSERT_EQ(ids.
size(),
N);
456 for (
size_t i = 1; i <
N; ++i)
457 EXPECT_GT(ids(i - 1), ids(i));
463TYPED_TEST(Tree_Move, TheComparisonOfTheOtherTreeIsNotMovedFrom)
467 ASSERT_FALSE(filled.tree.get_compare().moved_from);
468 Tree taken(std::move(filled.tree));
469 const Freeing_Nodes<Tree> freeing_taken(taken);
470 EXPECT_FALSE(filled.tree.get_compare().moved_from);
471 EXPECT_FALSE(taken.get_compare().moved_from);
472 expect_the_nodes_of(filled, taken,
"the tree that took the nodes");
477TYPED_TEST(Tree_Move, ACopyOfTheComparisonThatThrowsLeavesTheOtherTreeAsItWas)
480 ASSERT_FALSE(std::is_nothrow_move_constructible_v<Tree>);
483 EXPECT_THROW(
Tree taken(std::move(filled.tree)), Copy_Failure);
485 expect_the_nodes_of(filled, filled.tree,
"the tree whose move threw");
486 expect_usable(filled.tree,
"the tree whose move threw");
488 Tree taken(std::move(filled.tree));
489 const Freeing_Nodes<Tree> freeing_taken(taken);
490 expect_the_nodes_of(filled, taken,
"the tree that took the nodes");
491 expect_empty(filled.tree,
"the tree that was moved from");
501 Tree taken(std::move(filled.tree));
502 const Freeing_Nodes<Tree> freeing_taken(taken);
503 if constexpr (
requires (
Tree & t) { t.size(); })
505 EXPECT_EQ(taken.size(),
N);
506 EXPECT_EQ(filled.tree.size(), 0u);
508 if constexpr (
requires (
Tree & t) { t.select(
size_t{0}); })
511 for (
size_t i = 0; i < ids.
size(); ++i)
512 EXPECT_EQ(
KEY(taken.select(i)), ids(i)) <<
"position " << i;
520TEST(Tree_Move_Assignment, OfTheRankedSplayTreeExchangesTheTrees)
523 static_assert(std::is_nothrow_move_assignable_v<Tree>);
524 static_assert(not std::is_copy_assignable_v<Tree>);
527 const Freeing_Nodes<Tree> freeing_other(other);
528 (void) insert_ids(other, 1, 3);
529 other = std::move(filled.tree);
530 expect_the_nodes_of(filled, other,
"the tree that was assigned");
532 ASSERT_EQ(ids.
size(), 3u);
533 EXPECT_EQ(ids(0), 1);
534 EXPECT_EQ(ids(2), 3);
535 expect_usable(filled.tree,
"the tree that was moved from");
547 struct Less_Copied_From_Non_Const
549 bool descending =
false;
551 Less_Copied_From_Non_Const() noexcept = default;
553 Less_Copied_From_Non_Const(Less_Copied_From_Non_Const & o) noexcept
554 : descending(o.descending) {}
556 Less_Copied_From_Non_Const(
const Less_Copied_From_Non_Const &) =
delete;
558 Less_Copied_From_Non_Const(Less_Copied_From_Non_Const && o) noexcept
559 : descending(std::exchange(o.descending,
false)) {}
561 Less_Copied_From_Non_Const & operator = (Less_Copied_From_Non_Const && o)
noexcept
563 descending = std::exchange(o.descending,
false);
567 bool operator () (
const int a,
const int b)
const noexcept
569 return descending ? b < a : a < b;
576 struct Less_Only_Moved
578 bool descending =
false;
580 Less_Only_Moved() noexcept = default;
582 Less_Only_Moved(Less_Only_Moved && o) noexcept : descending(
std::exchange(o.descending, false)) {}
584 Less_Only_Moved & operator = (Less_Only_Moved && o)
noexcept
586 descending = std::exchange(o.descending,
false);
590 bool operator () (
const int a,
const int b)
const noexcept
592 return descending ? b < a : a < b;
599 template <
class Tree>
600 void check_the_move_with_a_comparison_that_is_not_copied_from_const(
const bool the_other_orders_down)
604 const Freeing_Nodes<Tree> freeing_tree(tree);
605 tree.get_compare().descending =
true;
606 for (
const int id : Ids)
607 ASSERT_NE(insert_own(tree, id), nullptr);
609 Tree taken(std::move(tree));
610 const Freeing_Nodes<Tree> freeing_taken(taken);
611 EXPECT_TRUE(taken.get_compare().descending);
612 EXPECT_EQ(tree.get_compare().descending, the_other_orders_down);
613 EXPECT_EQ(tree.getRoot(), Node::NullPtr);
615 ASSERT_EQ(ids.
size(),
N);
616 for (
size_t i = 1; i <
N; ++i)
617 EXPECT_GT(ids(i - 1), ids(i));
618 EXPECT_TRUE(taken.verify());
621 (void) insert_ids(tree, 1, 2);
623 ASSERT_EQ(ids.
size(), 2u);
624 EXPECT_EQ(ids(0), the_other_orders_down ? 2 : 1);
625 EXPECT_TRUE(tree.verify());
632TEST(Tree_Move_Comparison, CopiedFromOneThatIsNotConst)
634 using Less = Less_Copied_From_Non_Const;
635 static_assert(not std::is_copy_constructible_v<Less>);
636 static_assert(std::is_constructible_v<Less, Less &>);
637 static_assert(std::is_nothrow_move_constructible_v<Avl_Tree<int, Less>>);
638 check_the_move_with_a_comparison_that_is_not_copied_from_const<BinTree<int, Less>>(
true);
639 check_the_move_with_a_comparison_that_is_not_copied_from_const<Avl_Tree<int, Less>>(
true);
640 check_the_move_with_a_comparison_that_is_not_copied_from_const<Avl_Tree_Rk<int, Less>>(
true);
641 check_the_move_with_a_comparison_that_is_not_copied_from_const<Rb_Tree<int, Less>>(
true);
642 check_the_move_with_a_comparison_that_is_not_copied_from_const<Rb_Tree_Rk<int, Less>>(
true);
643 check_the_move_with_a_comparison_that_is_not_copied_from_const<HtdRbTree<int, Less>>(
true);
644 check_the_move_with_a_comparison_that_is_not_copied_from_const<HtdRbTreeRk<int, Less>>(
true);
645 check_the_move_with_a_comparison_that_is_not_copied_from_const<TdRbTree<int, Less>>(
true);
646 check_the_move_with_a_comparison_that_is_not_copied_from_const<TdRbTreeRk<int, Less>>(
true);
647 check_the_move_with_a_comparison_that_is_not_copied_from_const<Treap_Rk<int, Less>>(
true);
652TEST(Tree_Move_Comparison, OnlyMoved)
654 using Less = Less_Only_Moved;
655 static_assert(not std::is_constructible_v<Less, Less &>);
656 static_assert(std::is_nothrow_move_constructible_v<TdRbTree<int, Less>>);
657 check_the_move_with_a_comparison_that_is_not_copied_from_const<TdRbTree<int, Less>>(
false);
658 check_the_move_with_a_comparison_that_is_not_copied_from_const<TdRbTreeRk<int, Less>>(
false);
665 struct Node_Whose_Key_May_Throw
669 Node_Whose_Key_May_Throw() {}
671 explicit Node_Whose_Key_May_Throw(
int &&
k) noexcept : key(
k) {}
673 int & get_key() {
return key; }
681TEST(Tree_Move_Header, TheKeyOfTheOtherHeaderIsTakenOnlyWhenNothingThrows)
683 using Node = Node_Whose_Key_May_Throw;
684 static_assert(not bin_node_detail::nothrow_from_key_of_v<Node>);
685 static_assert(not bin_node_detail::nothrow_header_from_v<Node>);
691 EXPECT_EQ(header.key, 0) <<
"the key of the other header node was taken";
692 EXPECT_EQ(other.key, 7);
696 static_assert(bin_node_detail::nothrow_from_key_of_v<AvlNode<std::string>>);
697 static_assert(bin_node_detail::nothrow_header_from_v<AvlNode<std::string>>);
bool operator<(const Time &l, const Time &r)
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
Number Theoretic Transform over Z / MOD Z.
Node for QuadTree spatial data structure.
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.
__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)
DynArray< Graph::Node * > nodes
Node header_from(Node &other) noexcept(nothrow_header_from_v< Node >)
Builds a header node of a tree that takes the nodes of another tree.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Dynamic array container with automatic resizing.
AVL tree with rank (order statistics).
AVL tree implementation (height-balanced BST).
Utility functions for binary tree operations.
Generic unbalanced binary search tree.
Hybrid top-down/bottom-up red-black tree with rank support.
Hybrid top-down/bottom-up red-black tree implementation.
Randomized binary search tree.
Red-Black tree with rank (order statistics).
Red-Black tree implementation (bottom-up balancing).
Top-down splay tree with rank support.
Top-down splay tree implementation (without rank support).
Top-down Red-Black tree with rank support.
Top-down Red-Black tree implementation.
Treap with rank (order statistics).
Treap: randomized BST combining tree and heap properties.
TYPED_TEST(Tree_Move, TakesTheNodesAndLeavesTheOtherTreeEmptyAndUsable)