|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "cx/tree.h" |
|
30 #include <gtest/gtest.h> |
|
31 |
|
32 struct TestNode { |
|
33 TestNode *parent = nullptr; |
|
34 TestNode *prev = nullptr; |
|
35 TestNode *next = nullptr; |
|
36 |
|
37 TestNode *children_begin = nullptr; |
|
38 TestNode *children_end = nullptr; |
|
39 }; |
|
40 |
|
41 TEST(Tree, cx_tree_add_sibling) { |
|
42 // prepare test tree |
|
43 TestNode root, a; |
|
44 root.children_begin = &a; |
|
45 root.children_end = &a; |
|
46 a.parent = &root; |
|
47 |
|
48 // new test nodes |
|
49 TestNode b, c; |
|
50 |
|
51 // test |
|
52 cx_tree_add_sibling(&a, offsetof(TestNode, prev), offsetof(TestNode, next), offsetof(TestNode, parent), &b); |
|
53 EXPECT_EQ(b.parent, &root); |
|
54 EXPECT_EQ(b.prev, &a); |
|
55 EXPECT_EQ(b.next, nullptr); |
|
56 EXPECT_EQ(a.next, &b); |
|
57 |
|
58 cx_tree_add_sibling(&a, -1, offsetof(TestNode, next), -1, &c); |
|
59 EXPECT_EQ(c.parent, nullptr); |
|
60 EXPECT_EQ(c.prev, nullptr); |
|
61 EXPECT_EQ(c.next, nullptr); |
|
62 EXPECT_EQ(b.next, &c); |
|
63 } |
|
64 |
|
65 TEST(Tree, cx_tree_add_child) { |
|
66 TestNode root, a, b, c, a1; |
|
67 |
|
68 cx_tree_add_child( |
|
69 (void **) &root.children_begin, |
|
70 (void **) &root.children_end, |
|
71 offsetof(TestNode, prev), |
|
72 offsetof(TestNode, next), |
|
73 &a, |
|
74 offsetof(TestNode, parent), |
|
75 &root); |
|
76 EXPECT_EQ(root.children_begin, &a); |
|
77 EXPECT_EQ(root.children_end, &a); |
|
78 EXPECT_EQ(a.parent, &root); |
|
79 EXPECT_EQ(a.prev, nullptr); |
|
80 EXPECT_EQ(a.next, nullptr); |
|
81 |
|
82 cx_tree_add_child( |
|
83 (void **) &root.children_begin, |
|
84 (void **) &root.children_end, |
|
85 offsetof(TestNode, prev), |
|
86 offsetof(TestNode, next), |
|
87 &b, |
|
88 offsetof(TestNode, parent), |
|
89 &root); |
|
90 EXPECT_EQ(root.children_begin, &a); |
|
91 EXPECT_EQ(root.children_begin->next, &b); |
|
92 EXPECT_EQ(root.children_end, &b); |
|
93 EXPECT_EQ(b.parent, &root); |
|
94 EXPECT_EQ(b.prev, &a); |
|
95 |
|
96 cx_tree_add_child( |
|
97 (void **) &root.children_begin, |
|
98 nullptr, |
|
99 -1, |
|
100 offsetof(TestNode, next), |
|
101 &c, |
|
102 -1, |
|
103 &root); |
|
104 EXPECT_EQ(root.children_end, &b); // children_end unchanged |
|
105 EXPECT_EQ(b.next, &c); |
|
106 EXPECT_EQ(c.prev, nullptr); |
|
107 EXPECT_EQ(c.next, nullptr); |
|
108 EXPECT_EQ(c.parent, nullptr); |
|
109 |
|
110 cx_tree_add_child( |
|
111 (void **) &a.children_begin, |
|
112 (void **) &a.children_end, |
|
113 offsetof(TestNode, prev), |
|
114 offsetof(TestNode, next), |
|
115 &a1, |
|
116 offsetof(TestNode, parent), |
|
117 &a); |
|
118 EXPECT_EQ(a.children_begin, &a1); |
|
119 EXPECT_EQ(a1.parent, &a); |
|
120 EXPECT_EQ(root.children_begin, &a); |
|
121 EXPECT_EQ(root.children_begin->children_begin, &a1); |
|
122 } |