test/test_tree.c

changeset 515
6d3909bf1609
parent 514
6f9d97a53d67
child 516
7bcea73303ce
equal deleted inserted replaced
514:6f9d97a53d67 515:6d3909bf1609
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 "test_config.h"
31
32 #include <stddef.h>
33
34 typedef struct TestNode TestNode;
35
36 struct TestNode {
37 TestNode *parent;
38 TestNode *prev;
39 TestNode *next;
40
41 TestNode *children_begin;
42 TestNode *children_end;
43 };
44
45 void test_tree_add_sibling(void) {
46 // prepare test tree
47 TestNode root;
48 memset(&root, 0, sizeof(TestNode));
49
50 TestNode a;
51 memset(&a, 0, sizeof(TestNode));
52 root.children_begin = &a;
53 root.children_end = &a;
54 a.parent = &root;
55
56 // new test nodes
57 TestNode b;
58 memset(&b, 0, sizeof(TestNode));
59 TestNode c;
60 memset(&c, 0, sizeof(TestNode));
61
62 // test
63 cx_tree_add_sibling(&a, offsetof(TestNode, prev), offsetof(TestNode, next), offsetof(TestNode, parent), &b);
64 CU_ASSERT_PTR_EQUAL(b.parent, &root)
65 CU_ASSERT_PTR_EQUAL(b.prev, &a)
66 CU_ASSERT_PTR_NULL(b.next)
67 CU_ASSERT_PTR_EQUAL(a.next, &b)
68
69 cx_tree_add_sibling(&a, -1, offsetof(TestNode, next), -1, &c);
70 CU_ASSERT_PTR_NULL(c.parent)
71 CU_ASSERT_PTR_NULL(c.prev)
72 CU_ASSERT_PTR_NULL(c.next)
73 CU_ASSERT_PTR_EQUAL(b.next, &c)
74 }
75
76 void test_tree_add_child(void) {
77 // prepare test tree
78 TestNode root;
79 memset(&root, 0, sizeof(TestNode));
80
81 TestNode a;
82 memset(&a, 0, sizeof(TestNode));
83 TestNode b;
84 memset(&b, 0, sizeof(TestNode));
85 TestNode c;
86 memset(&c, 0, sizeof(TestNode));
87 TestNode a1;
88 memset(&a1, 0, sizeof(TestNode));
89
90 // test
91 cx_tree_add_child(
92 (void **) &root.children_begin,
93 (void **) &root.children_end,
94 offsetof(TestNode, prev),
95 offsetof(TestNode, next),
96 &a,
97 offsetof(TestNode, parent),
98 &root);
99 CU_ASSERT_PTR_EQUAL(root.children_begin, &a)
100 CU_ASSERT_PTR_EQUAL(root.children_end, &a)
101 CU_ASSERT_PTR_EQUAL(a.parent, &root)
102 CU_ASSERT_PTR_NULL(a.prev)
103 CU_ASSERT_PTR_NULL(a.next)
104
105 cx_tree_add_child(
106 (void **) &root.children_begin,
107 (void **) &root.children_end,
108 offsetof(TestNode, prev),
109 offsetof(TestNode, next),
110 &b,
111 offsetof(TestNode, parent),
112 &root);
113 CU_ASSERT_PTR_NOT_NULL(root.children_begin)
114 CU_ASSERT_PTR_EQUAL(root.children_begin->next, &b)
115 CU_ASSERT_PTR_EQUAL(root.children_end, &b)
116 CU_ASSERT_PTR_EQUAL(b.parent, &root)
117 CU_ASSERT_PTR_EQUAL(b.prev, &a)
118
119 cx_tree_add_child(
120 (void **) &root.children_begin,
121 NULL,
122 -1,
123 offsetof(TestNode, next),
124 &c,
125 -1,
126 &root);
127 CU_ASSERT_PTR_EQUAL(root.children_end, &b) // children_end unchanged
128 CU_ASSERT_PTR_EQUAL(b.next, &c)
129 CU_ASSERT_PTR_NULL(c.prev)
130 CU_ASSERT_PTR_NULL(c.next)
131 CU_ASSERT_PTR_NULL(c.parent)
132
133 cx_tree_add_child(
134 (void **) &a.children_begin,
135 (void **) &a.children_end,
136 offsetof(TestNode, prev),
137 offsetof(TestNode, next),
138 &a1,
139 offsetof(TestNode, parent),
140 &a);
141 CU_ASSERT_PTR_EQUAL(a.children_begin, &a1);
142 CU_ASSERT_PTR_EQUAL(a1.parent, &a);
143 CU_ASSERT_PTR_NOT_NULL(root.children_begin)
144 CU_ASSERT_PTR_EQUAL(root.children_begin->children_begin, &a1)
145 }
146
147 int main() {
148 CU_pSuite suite = NULL;
149
150 if (CUE_SUCCESS != CU_initialize_registry()) {
151 return CU_get_error();
152 }
153
154 suite = CU_add_suite("tree suite", NULL, NULL);
155
156 cu_add_test(suite, test_tree_add_sibling);
157 cu_add_test(suite, test_tree_add_child);
158
159 CU_basic_set_mode(UCX_CU_BRM);
160
161 int exitcode;
162 if (CU_basic_run_tests()) {
163 exitcode = CU_get_error();
164 } else {
165 exitcode = CU_get_number_of_failures() == 0 ? 0 : 1;
166 }
167 CU_cleanup_registry();
168 return exitcode;
169 }

mercurial