Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tree_moves_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
37// A tree does not own its nodes: its user allocates them and frees them.
38// So the move of a tree must take the very nodes of the other tree, leave
39// it without them, and leave the two trees usable. Until 7.9.0 four trees
40// (Avl_Tree, Avl_Tree_Rk, Splay_Tree and Splay_Tree_Rk) had no move: the
41// compiler wrote a copy for them, which std::move() took, and the two
42// trees were left with the same nodes; BinTree had none at all; and the
43// move of the others built the key of each header node by default and
44// moved the comparison out of the other tree.
45//
46// These tests move every tree, and the one of each whose nodes have a
47// virtual destructor, with the three kinds of key that a header node
48// tells apart (see bin_node_detail::header_from()):
49//
50// | Key | Its default construction | Its move |
51// |---|---|---|
52// | int | cannot throw | cannot throw |
53// | Heavy | allocates, may throw | cannot throw |
54// | Copied | allocates, may throw | is a copy: allocates, may throw |
55//
56// in each state of the tree that is moved from: empty, with nodes, and
57// moved from already.
58
59#include <gtest/gtest.h>
60
61#include <iterator>
62#include <stdexcept>
63#include <string>
64#include <type_traits>
65#include <utility>
66
67#include <tpl_array.H>
68#include <tpl_avl.H>
69#include <tpl_avlRk.H>
70#include <tpl_binNodeUtils.H>
71#include <tpl_binTree.H>
72#include <tpl_hRbTree.H>
73#include <tpl_hRbTreeRk.H>
74#include <tpl_rand_tree.H>
75#include <tpl_rb_tree.H>
76#include <tpl_rbRk.H>
77#include <tpl_splay_tree.H>
78#include <tpl_splay_treeRk.H>
79#include <tpl_tdRbTree.H>
80#include <tpl_tdRbTreeRk.H>
81#include <tpl_treap.H>
82#include <tpl_treapRk.H>
83
84using namespace Aleph;
85
86namespace
87{
88 // A key whose default construction allocates, and may throw, and whose
89 // move, the one that the compiler writes, does not
90 struct Heavy
91 {
92 int id = 0;
93 std::string weight;
94
95 Heavy() : weight(40, 'x') {}
96
97 Heavy(const int i) : id(i), weight(40, 'y') {}
98
99 friend bool operator < (const Heavy & a, const Heavy & b) noexcept { return a.id < b.id; }
100 };
101
102 // As Heavy, with a copy written by hand and no move, as a class of
103 // before C++11: moving it copies it, which allocates and may throw
104 struct Copied
105 {
106 int id = 0;
107 std::string weight;
108
109 Copied() : weight(40, 'x') {}
110
111 Copied(const int i) : id(i), weight(40, 'y') {}
112
113 Copied(const Copied & o) : id(o.id), weight(o.weight) {}
114
115 Copied & operator = (const Copied & o)
116 {
117 id = o.id;
118 weight = o.weight;
119 return *this;
120 }
121
122 friend bool operator < (const Copied & a, const Copied & b) noexcept { return a.id < b.id; }
123 };
124
125 int id_of(const int key) noexcept { return key; }
126
127 int id_of(const Heavy & key) noexcept { return key.id; }
128
129 int id_of(const Copied & key) noexcept { return key.id; }
130
131 // A comparison with state: it orders down when it says so. Two of them
132 // are told apart, and a tree that kept one built by default would order
133 // up
134 template <typename Key>
135 struct Flag_Less
136 {
137 bool descending = false;
138
139 bool operator () (const Key & a, const Key & b) const noexcept
140 {
141 return descending ? b < a : a < b;
142 }
143 };
144
145 // A comparison that tells whether it was moved from: one that is not
146 // trivial, as one that keeps a std::function, is left without what it
147 // had
148 template <typename Key>
149 struct Less_That_Tells_A_Move
150 {
151 bool moved_from = false;
152
153 Less_That_Tells_A_Move() noexcept = default;
154
155 Less_That_Tells_A_Move(const Less_That_Tells_A_Move &) noexcept = default;
156
157 Less_That_Tells_A_Move(Less_That_Tells_A_Move && o) noexcept { o.moved_from = true; }
158
159 Less_That_Tells_A_Move & operator = (const Less_That_Tells_A_Move &) noexcept = default;
160
161 bool operator () (const Key & a, const Key & b) const noexcept { return a < b; }
162 };
163
164 // What the comparison below throws when its copy is asked to fail
165 struct Copy_Failure : std::runtime_error
166 {
167 Copy_Failure() : std::runtime_error("the copy of the comparison failed") {}
168 };
169
170 bool copies_fail = false;
171
172 // A comparison whose copy throws when the test asks it to
173 template <typename Key>
174 struct Less_Whose_Copy_Fails
175 {
176 Less_Whose_Copy_Fails() noexcept = default;
177
178 Less_Whose_Copy_Fails(const Less_Whose_Copy_Fails &)
179 {
180 if (copies_fail)
181 throw Copy_Failure();
182 }
183
184 Less_Whose_Copy_Fails & operator = (const Less_Whose_Copy_Fails &) noexcept = default;
185
186 bool operator () (const Key & a, const Key & b) const noexcept { return a < b; }
187 };
188
189 // The keys of the tests, in an order of insertion that is not sorted
190 constexpr int Ids[] = {50, 30, 80, 10, 40, 70, 90, 20, 60};
191 constexpr size_t N = std::size(Ids);
192
193 // The ids of the keys of the tree of root `root`, in order
194 template <class Node>
195 void ids_in_order(Node * root, Array<int> & ids)
196 {
197 if (root == Node::NullPtr)
198 return;
199 ids_in_order(LLINK(root), ids);
200 ids.append(id_of(KEY(root)));
201 ids_in_order(RLINK(root), ids);
202 }
203
204 template <class Tree>
205 Array<int> ids_of(Tree & tree)
206 {
207 Array<int> ids;
208 ids_in_order(tree.getRoot(), ids);
209 return ids;
210 }
211
212 template <class Tree>
213 size_t count_of(Tree & tree) { return ids_of(tree).size(); }
214
215 // Inserts a node of its own of the key `id` and returns it. The tree
216 // does not own it; a node that the tree does not take is freed here,
217 // the test fails and the result is null
218 template <class Tree>
219 typename Tree::Node * insert_own(Tree & tree, const int id)
220 {
221 using Node = typename Tree::Node;
222 using Key = typename Node::key_type;
223 Node * p = new Node(Key(id));
224 Node * q = tree.insert(p);
225 if (q == nullptr)
226 {
227 delete p;
228 ADD_FAILURE() << "the tree did not take " << id;
229 return nullptr;
230 }
231 EXPECT_EQ(q, p) << "the tree took " << id << " and gave back another node";
232 return p;
233 }
234
235 // Inserts a node of its own for each id from `first` to `last`, by
236 // `step`, and returns how many
237 template <class Tree>
238 size_t insert_ids(Tree & tree, const int first, const int last, const int step = 1)
239 {
240 size_t n = 0;
241 for (int id = first; id <= last; id += step, ++n)
242 (void) insert_own(tree, id);
243 return n;
244 }
245
246 // Takes every node out of the tree, through its own removal, and frees
247 // it: the tree is left empty, and a tree that held a node that it should
248 // not, or missed one, fails here or under a sanitizer
249 template <class Tree>
250 void free_nodes(Tree & tree)
251 {
252 using Key = typename Tree::Node::key_type;
253 const Array<int> ids = ids_of(tree);
254 for (size_t i = 0; i < ids.size(); ++i)
255 {
256 auto * p = tree.remove(Key(ids(i)));
257 EXPECT_NE(p, nullptr) << "the tree does not find " << ids(i);
258 delete p;
259 }
260 EXPECT_EQ(tree.getRoot(), Tree::Node::NullPtr);
261 }
262
263 // Frees, when it goes out of scope, the nodes that a tree holds then:
264 // a fatal assertion that ends a test before the end frees them too
265 template <class Tree>
266 class Freeing_Nodes
267 {
268 Tree & tree;
269
270 public:
271 explicit Freeing_Nodes(Tree & t) noexcept : tree(t) {}
272
273 ~Freeing_Nodes() { free_nodes(tree); }
274
275 Freeing_Nodes(const Freeing_Nodes &) = delete;
276 Freeing_Nodes & operator = (const Freeing_Nodes &) = delete;
277 };
278
279 template <class Tree>
280 void expect_valid(Tree & tree, const char * which)
281 {
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";
285 }
286
287 // A tree of the nine keys, and the node of each key
288 template <class Tree>
289 struct Filled
290 {
291 using Node = typename Tree::Node;
292
293 Tree tree;
294 Node * nodes[N];
295
296 template <class... Args>
297 explicit Filled(Args &&... args) : tree(std::forward<Args>(args)...)
298 {
299 for (size_t i = 0; i < N; ++i)
300 nodes[i] = insert_own(tree, Ids[i]);
301 }
302
303 ~Filled() { free_nodes(tree); }
304
305 Filled(const Filled &) = delete;
306 Filled & operator = (const Filled &) = delete;
307 };
308
309 // Whether `tree` holds the nine nodes of `filled`, and no other
310 template <class Tree>
311 void expect_the_nodes_of(const Filled<Tree> & filled, Tree & tree, const char * which)
312 {
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);
318 }
319
320 template <class Tree>
321 void expect_empty(Tree & tree, const char * which)
322 {
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);
327 }
328
329 // A tree takes nodes, finds them, gives them back and is left empty
330 template <class Tree>
331 void expect_usable(Tree & tree, const char * which)
332 {
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)
339 {
340 auto * p = tree.remove(Key(id));
341 ASSERT_NE(p, nullptr) << which << ", key " << id;
342 EXPECT_EQ(id_of(KEY(p)), id) << which;
343 delete p;
344 }
345 EXPECT_EQ(count_of(tree), before) << which;
346 expect_valid(tree, which);
347 }
348
349 // The checks of a tree template with each of the three kinds of key
350 template <template <typename, class> class T>
351 struct Of
352 {
353 template <typename Key, class Compare = Aleph::less<Key>>
354 using Tree = T<Key, Compare>;
355 };
356
357 template <class Kind>
358 class Tree_Move : public ::testing::Test {};
359
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>>;
368 TYPED_TEST_SUITE(Tree_Move, Tree_Kinds);
369
370 // The move of a tree with nodes: the new tree holds those very nodes,
371 // the other one is empty, and both take and give nodes
372 template <class Tree>
373 void check_the_move_of_a_tree_with_nodes()
374 {
375 Filled<Tree> filled;
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");
383 }
384
385 // The move of an empty tree, and of one that was moved from, whose
386 // header nodes may hold keys that were moved from
387 template <class Tree>
388 void check_the_move_of_a_tree_without_nodes()
389 {
390 Tree empty;
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");
398
399 Filled<Tree> filled;
400 Tree taken(std::move(filled.tree));
401 const Freeing_Nodes<Tree> freeing_taken(taken);
402 Tree from_moved(std::move(filled.tree)); // moved from already
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");
408
409 Tree last(std::move(taken)); // and the nodes go on, to a third tree
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");
414 }
415} // namespace
416
417TYPED_TEST(Tree_Move, TakesTheNodesAndLeavesTheOtherTreeEmptyAndUsable)
418{
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>>();
422}
423
424TYPED_TEST(Tree_Move, OfAnEmptyTreeAndOfOneThatWasMovedFrom)
425{
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>>();
429}
430
431// The tree that is moved from keeps its comparison, which the other one
432// copies: both go on ordering as the tree did. It kept one that was moved
433// from, or one built by default
434TYPED_TEST(Tree_Move, BothTreesKeepTheComparison)
435{
436 using Less = Flag_Less<int>;
437 using Tree = typename TypeParam::template Tree<int, Less>;
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");
444
445 // both order down: the root of a tree of two nodes that are inserted in
446 // this order has the greater one at its left only if it orders down
447 (void) insert_ids(filled.tree, 1, 2);
448 Array<int> ids = ids_of(filled.tree);
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");
453
454 ids = ids_of(taken);
455 ASSERT_EQ(ids.size(), N);
456 for (size_t i = 1; i < N; ++i)
457 EXPECT_GT(ids(i - 1), ids(i));
458}
459
460// The comparison of the tree that is moved from is copied, and not moved:
461// one whose move leaves it without what it had is whole after the move of
462// its tree
463TYPED_TEST(Tree_Move, TheComparisonOfTheOtherTreeIsNotMovedFrom)
464{
465 using Tree = typename TypeParam::template Tree<int, Less_That_Tells_A_Move<int>>;
466 Filled<Tree> filled;
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");
473}
474
475// A copy of the comparison that throws leaves the tree that was to be
476// moved from with its nodes, in order, and usable
477TYPED_TEST(Tree_Move, ACopyOfTheComparisonThatThrowsLeavesTheOtherTreeAsItWas)
478{
479 using Tree = typename TypeParam::template Tree<Heavy, Less_Whose_Copy_Fails<Heavy>>;
480 ASSERT_FALSE(std::is_nothrow_move_constructible_v<Tree>);
481 Filled<Tree> filled;
482 copies_fail = true;
483 EXPECT_THROW(Tree taken(std::move(filled.tree)), Copy_Failure);
484 copies_fail = false;
485 expect_the_nodes_of(filled, filled.tree, "the tree whose move threw");
486 expect_usable(filled.tree, "the tree whose move threw");
487
488 Tree taken(std::move(filled.tree)); // and it moves once the copy does not throw
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");
492}
493
494// The trees that count their nodes, or that select them by their
495// position, do so after a move: the counts are in the nodes, and the one
496// of the tree goes with them
497TYPED_TEST(Tree_Move, TheCountsOfTheNodesGoWithThem)
498{
499 using Tree = typename TypeParam::template Tree<int>;
500 Filled<Tree> filled;
501 Tree taken(std::move(filled.tree));
502 const Freeing_Nodes<Tree> freeing_taken(taken);
503 if constexpr (requires (Tree & t) { t.size(); })
504 {
505 EXPECT_EQ(taken.size(), N);
506 EXPECT_EQ(filled.tree.size(), 0u);
507 }
508 if constexpr (requires (Tree & t) { t.select(size_t{0}); })
509 {
510 const Array<int> ids = ids_of(taken);
511 for (size_t i = 0; i < ids.size(); ++i)
512 EXPECT_EQ(KEY(taken.select(i)), ids(i)) << "position " << i;
513 }
514}
515
516// The tree without header nodes had the assignments that the compiler
517// wrote, which copied its root. Its move assignment exchanges the two
518// trees, as its swap, since a tree does not own the nodes that it would
519// drop
520TEST(Tree_Move_Assignment, OfTheRankedSplayTreeExchangesTheTrees)
521{
522 using Tree = Splay_Tree_Rk<int>;
523 static_assert(std::is_nothrow_move_assignable_v<Tree>);
524 static_assert(not std::is_copy_assignable_v<Tree>);
525 Filled<Tree> filled;
526 Tree other;
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");
531 const Array<int> ids = ids_of(filled.tree);
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");
536}
537
538namespace
539{
540 // A comparison whose only copy is from one that is not const: that from a
541 // const one is deleted, as some functors of the user do, and it has a move
542 // of its own, which leaves the one moved from ordering up. A tree is built
543 // with it, by default or from it, and a tree that takes the nodes of
544 // another copies it from the one that is not const: the other tree keeps
545 // its order. Copied from a const one, the move did not compile; moved, the
546 // other tree would order up
547 struct Less_Copied_From_Non_Const
548 {
549 bool descending = false;
550
551 Less_Copied_From_Non_Const() noexcept = default;
552
553 Less_Copied_From_Non_Const(Less_Copied_From_Non_Const & o) noexcept
554 : descending(o.descending) {}
555
556 Less_Copied_From_Non_Const(const Less_Copied_From_Non_Const &) = delete;
557
558 Less_Copied_From_Non_Const(Less_Copied_From_Non_Const && o) noexcept
559 : descending(std::exchange(o.descending, false)) {}
560
561 Less_Copied_From_Non_Const & operator = (Less_Copied_From_Non_Const && o) noexcept
562 {
563 descending = std::exchange(o.descending, false);
564 return *this;
565 }
566
567 bool operator () (const int a, const int b) const noexcept
568 {
569 return descending ? b < a : a < b;
570 }
571 };
572
573 // A comparison that is only moved: a tree that is built by default
574 // builds it, and a tree that takes the nodes of another moves it, so
575 // that the other tree keeps one that was moved from, which orders up
576 struct Less_Only_Moved
577 {
578 bool descending = false;
579
580 Less_Only_Moved() noexcept = default;
581
582 Less_Only_Moved(Less_Only_Moved && o) noexcept : descending(std::exchange(o.descending, false)) {}
583
584 Less_Only_Moved & operator = (Less_Only_Moved && o) noexcept
585 {
586 descending = std::exchange(o.descending, false);
587 return *this;
588 }
589
590 bool operator () (const int a, const int b) const noexcept
591 {
592 return descending ? b < a : a < b;
593 }
594 };
595
596 // Moves a tree that orders down, with the comparison Less, and checks
597 // the order of the two trees after the move: the one that took the nodes
598 // orders down, and the other one orders as `the_other_orders_down` says
599 template <class Tree>
600 void check_the_move_with_a_comparison_that_is_not_copied_from_const(const bool the_other_orders_down)
601 {
602 using Node = typename Tree::Node;
603 Tree tree;
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);
608
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);
614 Array<int> ids = ids_of(taken);
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());
619
620 // the other tree takes nodes again, in its order
621 (void) insert_ids(tree, 1, 2);
622 ids = ids_of(tree);
623 ASSERT_EQ(ids.size(), 2u);
624 EXPECT_EQ(ids(0), the_other_orders_down ? 2 : 1);
625 EXPECT_TRUE(tree.verify());
626 }
627} // namespace
628
629// The trees that can be built with a comparison copied only from one that
630// is not const: Splay_Tree, Splay_Tree_Rk, Treap and Rand_Tree cannot, nor
631// can TdRbTree and TdRbTreeRk from a comparison
632TEST(Tree_Move_Comparison, CopiedFromOneThatIsNotConst)
633{
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);
648}
649
650// The trees that can be built with a comparison that is only moved: those
651// whose default constructor builds it in place
652TEST(Tree_Move_Comparison, OnlyMoved)
653{
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);
659}
660
661namespace
662{
663 // What a node of the user may be: a get_key() that may throw, a default
664 // construction that may throw, and a construction from a key that cannot
665 struct Node_Whose_Key_May_Throw
666 {
667 int key = 0;
668
669 Node_Whose_Key_May_Throw() {}
670
671 explicit Node_Whose_Key_May_Throw(int && k) noexcept : key(k) {}
672
673 int & get_key() { return key; }
674 };
675} // namespace
676
677// header_from() takes the key of the other header node only when reading it
678// and building a node from it cannot throw: with a get_key() that may
679// throw, it builds the node by default, and its noexcept says that it may
680// throw, instead of ending the program inside it
681TEST(Tree_Move_Header, TheKeyOfTheOtherHeaderIsTakenOnlyWhenNothingThrows)
682{
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>);
686 static_assert(not noexcept(bin_node_detail::header_from(std::declval<Node &>())));
687
688 Node other;
689 other.key = 7;
690 const Node header = bin_node_detail::header_from(other);
691 EXPECT_EQ(header.key, 0) << "the key of the other header node was taken";
692 EXPECT_EQ(other.key, 7);
693
694 // and with the nodes of the library, which read their key without
695 // throwing, it takes it when building one by default may throw
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>>);
698}
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:143
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
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Node for QuadTree spatial data structure.
Definition quadnode.H:95
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
QuadNode Node
Definition quadtree.H:128
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
Definition quadtree.H:281
#define TEST(name)
#define N
Definition fib.C:294
__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
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
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
Definition ah-zip.H:152
STL namespace.
#define RLINK(i, n)
#define LLINK(i, n)
static int * k
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)