Debian Codegraph @

libcxx/test/libcxx-03/containers/associative/tree_right_rotate.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // Not a portable test
10
11 // Precondition: __x->__left_ != nullptr
12 // template <class _NodePtr>
13 // void
14 // __tree_right_rotate(_NodePtr __x);
15
16 #include <__cxx03/__tree>
17 #include <cassert>
18
19 #include "test_macros.h"
20
21 struct Node {
22 Node* __left_;
23 Node* __right_;
24 Node* __parent_;
25
26 Node* __parent_unsafe() const { return __parent_; }
27 void __set_parent(Node* x) { __parent_ = x; }
28
29 Node() : __left_(), __right_(), __parent_() {}
30 };
31
32 void test1() {
33 Node root;
34 Node x;
35 Node y;
36 root.__left_ = &x;
37 x.__left_ = &y;
38 x.__right_ = 0;
39 x.__parent_ = &root;
40 y.__left_ = 0;
41 y.__right_ = 0;
42 y.__parent_ = &x;
43 std::__tree_right_rotate(&x);
44 assert(root.__parent_ == 0);
45 assert(root.__left_ == &y);
46 assert(root.__right_ == 0);
47 assert(y.__parent_ == &root);
48 assert(y.__left_ == 0);
49 assert(y.__right_ == &x);
50 assert(x.__parent_ == &y);
51 assert(x.__left_ == 0);
52 assert(x.__right_ == 0);
53 }
54
55 void test2() {
56 Node root;
57 Node x;
58 Node y;
59 Node a;
60 Node b;
61 Node c;
62 root.__left_ = &x;
63 x.__left_ = &y;
64 x.__right_ = &c;
65 x.__parent_ = &root;
66 y.__left_ = &a;
67 y.__right_ = &b;
68 y.__parent_ = &x;
69 a.__parent_ = &y;
70 b.__parent_ = &y;
71 c.__parent_ = &x;
72 std::__tree_right_rotate(&x);
73 assert(root.__parent_ == 0);
74 assert(root.__left_ == &y);
75 assert(root.__right_ == 0);
76 assert(y.__parent_ == &root);
77 assert(y.__left_ == &a);
78 assert(y.__right_ == &x);
79 assert(x.__parent_ == &y);
80 assert(x.__left_ == &b);
81 assert(x.__right_ == &c);
82 assert(a.__parent_ == &y);
83 assert(a.__left_ == 0);
84 assert(a.__right_ == 0);
85 assert(b.__parent_ == &x);
86 assert(b.__left_ == 0);
87 assert(b.__right_ == 0);
88 assert(c.__parent_ == &x);
89 assert(c.__left_ == 0);
90 assert(c.__right_ == 0);
91 }
92
93 int main(int, char**) {
94 test1();
95 test2();
96
97 return 0;
98 }