test/test_tree.c

changeset 449
68ad5750ba6b
parent 443
d6d8712e15bc
child 453
bb144d08cd44
equal deleted inserted replaced
448:37c5d2fdb36b 449:68ad5750ba6b
38 TestNode *prev; 38 TestNode *prev;
39 TestNode *next; 39 TestNode *next;
40 40
41 TestNode *children_begin; 41 TestNode *children_begin;
42 TestNode *children_end; 42 TestNode *children_end;
43
44 int content;
45 }; 43 };
46 44
47 void test_cx_tree_add_node() { 45 void test_cx_tree_add_node(void) {
48 // prepare test tree 46 // prepare test tree
49 TestNode root; 47 TestNode root;
50 memset(&root, 0, sizeof(TestNode)); 48 memset(&root, 0, sizeof(TestNode));
51 49
52 TestNode a; 50 TestNode a;
53 memset(&a, 0, sizeof(TestNode)); 51 memset(&a, 0, sizeof(TestNode));
54 root.children_begin = &a; 52 root.children_begin = &a;
55 root.children_end = &a; 53 root.children_end = &a;
56 a.parent = &root; 54 a.parent = &root;
57 a.content = 1; 55
58
59 // new test nodes 56 // new test nodes
60 TestNode b; 57 TestNode b;
61 memset(&b, 0, sizeof(TestNode)); 58 memset(&b, 0, sizeof(TestNode));
62 TestNode c; 59 TestNode c;
63 memset(&c, 0, sizeof(TestNode)); 60 memset(&c, 0, sizeof(TestNode));
64 61
65 // test 62 // test
66 b.content = 2;
67 int ret = cx_tree_add_node(&a, offsetof(TestNode, parent), offsetof(TestNode, prev), offsetof(TestNode, next), &b); 63 int ret = cx_tree_add_node(&a, offsetof(TestNode, parent), offsetof(TestNode, prev), offsetof(TestNode, next), &b);
68 CU_ASSERT_EQUAL(ret, 0); 64 CU_ASSERT_EQUAL(ret, 0)
69 CU_ASSERT_PTR_EQUAL(b.parent, &root); 65 CU_ASSERT_PTR_EQUAL(b.parent, &root)
70 CU_ASSERT_PTR_EQUAL(b.prev, &a); 66 CU_ASSERT_PTR_EQUAL(b.prev, &a)
71 CU_ASSERT_PTR_EQUAL(b.next, NULL); 67 CU_ASSERT_PTR_NULL(b.next)
72 CU_ASSERT_PTR_EQUAL(a.next, &b); 68 CU_ASSERT_PTR_EQUAL(a.next, &b)
73 69
74 c.content = 3;
75 ret = cx_tree_add_node(&a, -1, -1, offsetof(TestNode, next), &c); 70 ret = cx_tree_add_node(&a, -1, -1, offsetof(TestNode, next), &c);
76 CU_ASSERT_EQUAL(ret, 0); 71 CU_ASSERT_EQUAL(ret, 0)
77 CU_ASSERT_PTR_EQUAL(c.parent, NULL); 72 CU_ASSERT_PTR_NULL(c.parent)
78 CU_ASSERT_PTR_EQUAL(c.prev, NULL); 73 CU_ASSERT_PTR_NULL(c.prev)
79 CU_ASSERT_PTR_EQUAL(c.next, NULL); 74 CU_ASSERT_PTR_NULL(c.next)
80 CU_ASSERT_PTR_EQUAL(b.next, &c); 75 CU_ASSERT_PTR_EQUAL(b.next, &c)
81 } 76 }
82 77
83 void test_cx_tree_add_child_node() { 78 void test_cx_tree_add_child_node(void) {
84 // prepare test tree 79 // prepare test tree
85 TestNode root; 80 TestNode root;
86 memset(&root, 0, sizeof(TestNode)); 81 memset(&root, 0, sizeof(TestNode));
87 82
88 TestNode a; 83 TestNode a;
95 memset(&a1, 0, sizeof(TestNode)); 90 memset(&a1, 0, sizeof(TestNode));
96 91
97 int ret; 92 int ret;
98 93
99 // test 94 // test
100 a.content = 1;
101 ret = cx_tree_add_child_node( 95 ret = cx_tree_add_child_node(
102 &root, 96 &root,
103 offsetof(TestNode, parent), 97 offsetof(TestNode, parent),
104 offsetof(TestNode, prev), 98 offsetof(TestNode, prev),
105 offsetof(TestNode, next), 99 offsetof(TestNode, next),
106 (void**)&root.children_begin, 100 (void**)&root.children_begin,
107 (void**)&root.children_end, 101 (void**)&root.children_end,
108 &a); 102 &a);
109 CU_ASSERT_EQUAL(ret, 0); 103 CU_ASSERT_EQUAL(ret, 0)
110 CU_ASSERT_PTR_EQUAL(root.children_begin, &a); 104 CU_ASSERT_PTR_EQUAL(root.children_begin, &a)
111 CU_ASSERT_PTR_EQUAL(root.children_end, &a); 105 CU_ASSERT_PTR_EQUAL(root.children_end, &a)
112 CU_ASSERT_PTR_EQUAL(a.parent, &root); 106 CU_ASSERT_PTR_EQUAL(a.parent, &root)
113 CU_ASSERT_PTR_EQUAL(a.prev, NULL); 107 CU_ASSERT_PTR_NULL(a.prev)
114 CU_ASSERT_PTR_EQUAL(a.next, NULL); 108 CU_ASSERT_PTR_NULL(a.next)
115 109
116 b.content = 2;
117 ret = cx_tree_add_child_node( 110 ret = cx_tree_add_child_node(
118 &root, 111 &root,
119 offsetof(TestNode, parent), 112 offsetof(TestNode, parent),
120 offsetof(TestNode, prev), 113 offsetof(TestNode, prev),
121 offsetof(TestNode, next), 114 offsetof(TestNode, next),
122 (void**)&root.children_begin, 115 (void**)&root.children_begin,
123 (void**)&root.children_end, 116 (void**)&root.children_end,
124 &b); 117 &b);
125 CU_ASSERT_EQUAL(ret, 0); 118 CU_ASSERT_EQUAL(ret, 0)
126 CU_ASSERT_TRUE(root.children_begin ? root.children_begin->next == &b : 0); 119 CU_ASSERT_PTR_NOT_NULL(root.children_begin)
127 CU_ASSERT_PTR_EQUAL(root.children_end, &b); 120 CU_ASSERT_PTR_EQUAL(root.children_begin->next, &b)
128 CU_ASSERT_PTR_EQUAL(b.parent, &root); 121 CU_ASSERT_PTR_EQUAL(root.children_end, &b)
129 CU_ASSERT_PTR_EQUAL(b.prev, &a); 122 CU_ASSERT_PTR_EQUAL(b.parent, &root)
123 CU_ASSERT_PTR_EQUAL(b.prev, &a)
130 124
131 c.content = 3;
132 ret = cx_tree_add_child_node( 125 ret = cx_tree_add_child_node(
133 &root, 126 &root,
134 -1, 127 -1,
135 -1, 128 -1,
136 offsetof(TestNode, next), 129 offsetof(TestNode, next),
137 (void**)&root.children_begin, 130 (void**)&root.children_begin,
138 NULL, 131 NULL,
139 &c); 132 &c);
140 CU_ASSERT_EQUAL(ret, 0); 133 CU_ASSERT_EQUAL(ret, 0)
141 CU_ASSERT_PTR_EQUAL(root.children_end, &b); // children_end unchanged 134 CU_ASSERT_PTR_EQUAL(root.children_end, &b) // children_end unchanged
142 CU_ASSERT_PTR_EQUAL(b.next, &c); 135 CU_ASSERT_PTR_EQUAL(b.next, &c)
143 CU_ASSERT_PTR_EQUAL(c.prev, NULL); 136 CU_ASSERT_PTR_NULL(c.prev)
144 CU_ASSERT_PTR_EQUAL(c.next, NULL); 137 CU_ASSERT_PTR_NULL(c.next)
145 CU_ASSERT_PTR_EQUAL(c.parent, NULL); 138 CU_ASSERT_PTR_NULL(c.parent)
146 139
147 a1.content = 11;
148 ret = cx_tree_add_child_node( 140 ret = cx_tree_add_child_node(
149 &a, 141 &a,
150 offsetof(TestNode, parent), 142 offsetof(TestNode, parent),
151 offsetof(TestNode, prev), 143 offsetof(TestNode, prev),
152 offsetof(TestNode, next), 144 offsetof(TestNode, next),
154 (void**)&a.children_end, 146 (void**)&a.children_end,
155 &a1); 147 &a1);
156 CU_ASSERT_EQUAL(ret, 0); 148 CU_ASSERT_EQUAL(ret, 0);
157 CU_ASSERT_PTR_EQUAL(a.children_begin, &a1); 149 CU_ASSERT_PTR_EQUAL(a.children_begin, &a1);
158 CU_ASSERT_PTR_EQUAL(a1.parent, &a); 150 CU_ASSERT_PTR_EQUAL(a1.parent, &a);
159 CU_ASSERT_TRUE(root.children_begin ? root.children_begin->children_begin == &a1 : 0); 151 CU_ASSERT_PTR_NOT_NULL(root.children_begin)
152 CU_ASSERT_PTR_EQUAL(root.children_begin->children_begin, &a1)
160 } 153 }
161 154
162 int main() { 155 int main() {
163 CU_pSuite suite = NULL; 156 CU_pSuite suite = NULL;
164 157

mercurial