tests/test_tree.c

branch
feature/tree_add
changeset 863
4a220afebad0
parent 862
387414a7afd8
child 866
1f636de4a63f
equal deleted inserted replaced
860:558ed4c6abd0 863:4a220afebad0
38 struct tree_node *prev; 38 struct tree_node *prev;
39 struct tree_node *children; 39 struct tree_node *children;
40 int data; 40 int data;
41 } tree_node; 41 } tree_node;
42 42
43 typedef struct tree_node2 {
44 struct tree_node2 *parent;
45 struct tree_node2 *next;
46 struct tree_node2 *prev;
47 struct tree_node2 *children;
48 struct tree_node2 *last_child;
49 int data;
50 } tree_node2;
51
43 #define tree_node_layout \ 52 #define tree_node_layout \
44 offsetof(tree_node, parent), offsetof(tree_node, children), \ 53 offsetof(tree_node, parent), offsetof(tree_node, children), -1, \
45 offsetof(tree_node, prev), offsetof(tree_node, next) 54 offsetof(tree_node, prev), offsetof(tree_node, next)
46 55 #define tree_node2_layout \
47 #define tree_child_list offsetof(tree_node, children),offsetof(tree_node, next) 56 offsetof(tree_node2, parent), offsetof(tree_node2, children),\
57 offsetof(tree_node2, last_child), \
58 offsetof(tree_node2, prev), offsetof(tree_node2, next)
59
60 #define tree_children(type) offsetof(type, children), offsetof(type, next)
48 61
49 CX_TEST(test_tree_link_new_child) { 62 CX_TEST(test_tree_link_new_child) {
50 tree_node parent = {0}; 63 tree_node parent = {0};
51 tree_node child = {0}; 64 tree_node child = {0};
52 65
74 cx_tree_link(&parent, &child2, tree_node_layout); 87 cx_tree_link(&parent, &child2, tree_node_layout);
75 cx_tree_link(&parent, &child3, tree_node_layout); 88 cx_tree_link(&parent, &child3, tree_node_layout);
76 CX_TEST_ASSERT(parent.next == NULL); 89 CX_TEST_ASSERT(parent.next == NULL);
77 CX_TEST_ASSERT(parent.prev == NULL); 90 CX_TEST_ASSERT(parent.prev == NULL);
78 CX_TEST_ASSERT(parent.parent == NULL); 91 CX_TEST_ASSERT(parent.parent == NULL);
79 CX_TEST_ASSERT(parent.children == &child3); 92 CX_TEST_ASSERT(parent.children == &child1);
80 93
81 CX_TEST_ASSERT(child1.parent == &parent); 94 CX_TEST_ASSERT(child1.parent == &parent);
82 CX_TEST_ASSERT(child2.parent == &parent); 95 CX_TEST_ASSERT(child2.parent == &parent);
83 CX_TEST_ASSERT(child3.parent == &parent); 96 CX_TEST_ASSERT(child3.parent == &parent);
84 CX_TEST_ASSERT(child1.children == NULL); 97 CX_TEST_ASSERT(child1.children == NULL);
85 CX_TEST_ASSERT(child2.children == NULL); 98 CX_TEST_ASSERT(child2.children == NULL);
86 CX_TEST_ASSERT(child3.children == NULL); 99 CX_TEST_ASSERT(child3.children == NULL);
87 100
88 CX_TEST_ASSERT(child3.prev == NULL); 101 CX_TEST_ASSERT(child1.prev == NULL);
89 CX_TEST_ASSERT(child3.next == &child2); 102 CX_TEST_ASSERT(child1.next == &child2);
90 CX_TEST_ASSERT(child2.prev == &child3); 103 CX_TEST_ASSERT(child2.prev == &child1);
91 CX_TEST_ASSERT(child2.next == &child1); 104 CX_TEST_ASSERT(child2.next == &child3);
92 CX_TEST_ASSERT(child1.prev == &child2); 105 CX_TEST_ASSERT(child3.prev == &child2);
93 CX_TEST_ASSERT(child1.next == NULL); 106 CX_TEST_ASSERT(child3.next == NULL);
94 } 107 }
95 } 108 }
96 109
97 CX_TEST(test_tree_link_move_to_other_parent) { 110 CX_TEST(test_tree_link_move_to_other_parent) {
98 tree_node parent = {0}; 111 tree_node parent = {0};
107 cx_tree_link(&child3, &child2, tree_node_layout); 120 cx_tree_link(&child3, &child2, tree_node_layout);
108 121
109 CX_TEST_ASSERT(parent.next == NULL); 122 CX_TEST_ASSERT(parent.next == NULL);
110 CX_TEST_ASSERT(parent.prev == NULL); 123 CX_TEST_ASSERT(parent.prev == NULL);
111 CX_TEST_ASSERT(parent.parent == NULL); 124 CX_TEST_ASSERT(parent.parent == NULL);
112 CX_TEST_ASSERT(parent.children == &child3); 125 CX_TEST_ASSERT(parent.children == &child1);
113 126
114 CX_TEST_ASSERT(child1.parent == &parent); 127 CX_TEST_ASSERT(child1.parent == &parent);
115 CX_TEST_ASSERT(child2.parent == &child3); 128 CX_TEST_ASSERT(child2.parent == &child3);
116 CX_TEST_ASSERT(child3.parent == &parent); 129 CX_TEST_ASSERT(child3.parent == &parent);
117 CX_TEST_ASSERT(child1.children == NULL); 130 CX_TEST_ASSERT(child1.children == NULL);
118 CX_TEST_ASSERT(child2.children == NULL); 131 CX_TEST_ASSERT(child2.children == NULL);
119 CX_TEST_ASSERT(child3.children == &child2); 132 CX_TEST_ASSERT(child3.children == &child2);
120 133
121 CX_TEST_ASSERT(child3.prev == NULL); 134 CX_TEST_ASSERT(child1.prev == NULL);
122 CX_TEST_ASSERT(child3.next == &child1); 135 CX_TEST_ASSERT(child1.next == &child3);
123 CX_TEST_ASSERT(child1.prev == &child3); 136 CX_TEST_ASSERT(child3.prev == &child1);
124 CX_TEST_ASSERT(child1.next == NULL); 137 CX_TEST_ASSERT(child3.next == NULL);
125 138
126 CX_TEST_ASSERT(child2.prev == NULL); 139 CX_TEST_ASSERT(child2.prev == NULL);
127 CX_TEST_ASSERT(child2.next == NULL); 140 CX_TEST_ASSERT(child2.next == NULL);
128 } 141 }
129 } 142 }
159 CX_TEST_ASSERT(child3.children == &child2); 172 CX_TEST_ASSERT(child3.children == &child2);
160 CX_TEST_ASSERT(child2.parent == &child3); 173 CX_TEST_ASSERT(child2.parent == &child3);
161 CX_TEST_ASSERT(child2.children == NULL); 174 CX_TEST_ASSERT(child2.children == NULL);
162 CX_TEST_ASSERT(child2.prev == NULL); 175 CX_TEST_ASSERT(child2.prev == NULL);
163 CX_TEST_ASSERT(child2.next == NULL); 176 CX_TEST_ASSERT(child2.next == NULL);
177
178 // unlink last child from parent
179 cx_tree_unlink(&child1, tree_node_layout);
180 CX_TEST_ASSERT(parent.next == NULL);
181 CX_TEST_ASSERT(parent.prev == NULL);
182 CX_TEST_ASSERT(parent.parent == NULL);
183 CX_TEST_ASSERT(parent.children == NULL);
184 CX_TEST_ASSERT(child1.parent == NULL);
185 }
186 }
187
188 CX_TEST(test_tree2_link_new_child) {
189 tree_node2 parent = {0};
190 tree_node2 child = {0};
191
192 CX_TEST_DO {
193 cx_tree_link(&parent, &child, tree_node2_layout);
194 CX_TEST_ASSERT(parent.next == NULL);
195 CX_TEST_ASSERT(parent.prev == NULL);
196 CX_TEST_ASSERT(parent.parent == NULL);
197 CX_TEST_ASSERT(parent.children == &child);
198 CX_TEST_ASSERT(parent.last_child == &child);
199 CX_TEST_ASSERT(child.parent == &parent);
200 CX_TEST_ASSERT(child.next == NULL);
201 CX_TEST_ASSERT(child.prev == NULL);
202 CX_TEST_ASSERT(child.children == NULL);
203 CX_TEST_ASSERT(child.last_child == NULL);
204 }
205 }
206
207 CX_TEST(test_tree2_link_add_child) {
208 tree_node2 parent = {0};
209 tree_node2 child1 = {0};
210 tree_node2 child2 = {0};
211 tree_node2 child3 = {0};
212
213 CX_TEST_DO {
214 cx_tree_link(&parent, &child1, tree_node2_layout);
215 cx_tree_link(&parent, &child2, tree_node2_layout);
216 cx_tree_link(&parent, &child3, tree_node2_layout);
217 CX_TEST_ASSERT(parent.next == NULL);
218 CX_TEST_ASSERT(parent.prev == NULL);
219 CX_TEST_ASSERT(parent.parent == NULL);
220 CX_TEST_ASSERT(parent.children == &child1);
221 CX_TEST_ASSERT(parent.last_child == &child3);
222
223 CX_TEST_ASSERT(child1.parent == &parent);
224 CX_TEST_ASSERT(child2.parent == &parent);
225 CX_TEST_ASSERT(child3.parent == &parent);
226 CX_TEST_ASSERT(child1.children == NULL);
227 CX_TEST_ASSERT(child2.children == NULL);
228 CX_TEST_ASSERT(child3.children == NULL);
229 CX_TEST_ASSERT(child1.last_child == NULL);
230 CX_TEST_ASSERT(child2.last_child == NULL);
231 CX_TEST_ASSERT(child3.last_child == NULL);
232
233 CX_TEST_ASSERT(child1.prev == NULL);
234 CX_TEST_ASSERT(child1.next == &child2);
235 CX_TEST_ASSERT(child2.prev == &child1);
236 CX_TEST_ASSERT(child2.next == &child3);
237 CX_TEST_ASSERT(child3.prev == &child2);
238 CX_TEST_ASSERT(child3.next == NULL);
239 }
240 }
241
242 CX_TEST(test_tree2_link_move_to_other_parent) {
243 tree_node2 parent = {0};
244 tree_node2 child1 = {0};
245 tree_node2 child2 = {0};
246 tree_node2 child3 = {0};
247 cx_tree_link(&parent, &child1, tree_node2_layout);
248 cx_tree_link(&parent, &child2, tree_node2_layout);
249 cx_tree_link(&parent, &child3, tree_node2_layout);
250
251 CX_TEST_DO {
252 cx_tree_link(&child3, &child2, tree_node2_layout);
253
254 CX_TEST_ASSERT(parent.next == NULL);
255 CX_TEST_ASSERT(parent.prev == NULL);
256 CX_TEST_ASSERT(parent.parent == NULL);
257 CX_TEST_ASSERT(parent.children == &child1);
258 CX_TEST_ASSERT(parent.last_child == &child3);
259
260 CX_TEST_ASSERT(child1.parent == &parent);
261 CX_TEST_ASSERT(child2.parent == &child3);
262 CX_TEST_ASSERT(child3.parent == &parent);
263 CX_TEST_ASSERT(child1.children == NULL);
264 CX_TEST_ASSERT(child2.children == NULL);
265 CX_TEST_ASSERT(child3.children == &child2);
266 CX_TEST_ASSERT(child1.last_child == NULL);
267 CX_TEST_ASSERT(child2.last_child == NULL);
268 CX_TEST_ASSERT(child3.last_child == &child2);
269
270 CX_TEST_ASSERT(child1.prev == NULL);
271 CX_TEST_ASSERT(child1.next == &child3);
272 CX_TEST_ASSERT(child3.prev == &child1);
273 CX_TEST_ASSERT(child3.next == NULL);
274
275 CX_TEST_ASSERT(child2.prev == NULL);
276 CX_TEST_ASSERT(child2.next == NULL);
277 }
278 }
279
280 CX_TEST(test_tree2_unlink) {
281 tree_node2 parent = {0};
282 tree_node2 child1 = {0};
283 tree_node2 child2 = {0};
284 tree_node2 child3 = {0};
285 cx_tree_link(&parent, &child1, tree_node2_layout);
286 cx_tree_link(&parent, &child3, tree_node2_layout);
287 cx_tree_link(&child3, &child2, tree_node2_layout);
288
289 CX_TEST_DO {
290 cx_tree_unlink(&child3, tree_node2_layout);
291
292 CX_TEST_ASSERT(parent.next == NULL);
293 CX_TEST_ASSERT(parent.prev == NULL);
294 CX_TEST_ASSERT(parent.parent == NULL);
295 CX_TEST_ASSERT(parent.children == &child1);
296 CX_TEST_ASSERT(parent.last_child == &child1);
297
298 CX_TEST_ASSERT(child1.parent == &parent);
299 CX_TEST_ASSERT(child1.children == NULL);
300 CX_TEST_ASSERT(child1.last_child == NULL);
301 CX_TEST_ASSERT(child1.prev == NULL);
302 CX_TEST_ASSERT(child1.next == NULL);
303
304 // child 3 is unlinked
305 CX_TEST_ASSERT(child3.parent == NULL);
306 CX_TEST_ASSERT(child3.prev == NULL);
307 CX_TEST_ASSERT(child3.next == NULL);
308
309 // child 2 is still child of the unlinked child 3
310 CX_TEST_ASSERT(child3.children == &child2);
311 CX_TEST_ASSERT(child3.last_child == &child2);
312 CX_TEST_ASSERT(child2.parent == &child3);
313 CX_TEST_ASSERT(child2.children == NULL);
314 CX_TEST_ASSERT(child2.last_child == NULL);
315 CX_TEST_ASSERT(child2.prev == NULL);
316 CX_TEST_ASSERT(child2.next == NULL);
317
318 // unlink last child from parent
319 cx_tree_unlink(&child1, tree_node2_layout);
320 CX_TEST_ASSERT(parent.next == NULL);
321 CX_TEST_ASSERT(parent.prev == NULL);
322 CX_TEST_ASSERT(parent.parent == NULL);
323 CX_TEST_ASSERT(parent.children == NULL);
324 CX_TEST_ASSERT(parent.last_child == NULL);
325 CX_TEST_ASSERT(child1.parent == NULL);
164 } 326 }
165 } 327 }
166 328
167 static int test_tree_search_function(void const *n, void const *d) { 329 static int test_tree_search_function(void const *n, void const *d) {
168 tree_node const *node = n; 330 tree_node const *node = n;
213 tree_node *n; 375 tree_node *n;
214 CX_TEST_DO { 376 CX_TEST_DO {
215 for (unsigned i = 0 ; i <= 10 ; i++) { 377 for (unsigned i = 0 ; i <= 10 ; i++) {
216 s = testdata[i]; 378 s = testdata[i];
217 r = cx_tree_search(&root, &s, test_tree_search_function, 379 r = cx_tree_search(&root, &s, test_tree_search_function,
218 (void **) &n, tree_child_list); 380 (void **) &n, tree_children(tree_node));
219 CX_TEST_ASSERT(r == 0); 381 CX_TEST_ASSERT(r == 0);
220 CX_TEST_ASSERT(n == testnodes[i]); 382 CX_TEST_ASSERT(n == testnodes[i]);
221 } 383 }
222 384
223 s = -5; 385 s = -5;
224 r = cx_tree_search(&root, &s, test_tree_search_function, 386 r = cx_tree_search(&root, &s, test_tree_search_function,
225 (void **) &n, tree_child_list); 387 (void **) &n, tree_children(tree_node));
226 CX_TEST_ASSERT(r < 0); 388 CX_TEST_ASSERT(r < 0);
227 CX_TEST_ASSERT(n == NULL); 389 CX_TEST_ASSERT(n == NULL);
228 390
229 s = 26; 391 s = 26;
230 r = cx_tree_search(&root, &s, test_tree_search_function, 392 r = cx_tree_search(&root, &s, test_tree_search_function,
231 (void **) &n, tree_child_list); 393 (void **) &n, tree_children(tree_node));
232 CX_TEST_ASSERT(r > 0); 394 CX_TEST_ASSERT(r > 0);
233 CX_TEST_ASSERT(n == &ba); 395 CX_TEST_ASSERT(n == &ba);
234 396
235 s = 35; 397 s = 35;
236 r = cx_tree_search(&root, &s, test_tree_search_function, 398 r = cx_tree_search(&root, &s, test_tree_search_function,
237 (void **) &n, tree_child_list); 399 (void **) &n, tree_children(tree_node));
238 CX_TEST_ASSERT(r > 0); 400 CX_TEST_ASSERT(r > 0);
239 CX_TEST_ASSERT(n == &cb); 401 CX_TEST_ASSERT(n == &cb);
240 402
241 s = 38; 403 s = 38;
242 r = cx_tree_search(&root, &s, test_tree_search_function, 404 r = cx_tree_search(&root, &s, test_tree_search_function,
243 (void **) &n, tree_child_list); 405 (void **) &n, tree_children(tree_node));
244 CX_TEST_ASSERT(r > 0); 406 CX_TEST_ASSERT(r > 0);
245 CX_TEST_ASSERT(n == &cba); 407 CX_TEST_ASSERT(n == &cba);
246 408
247 s = 42; 409 s = 42;
248 r = cx_tree_search(&root, &s, test_tree_search_function, 410 r = cx_tree_search(&root, &s, test_tree_search_function,
249 (void **) &n, tree_child_list); 411 (void **) &n, tree_children(tree_node));
250 CX_TEST_ASSERT(r > 0); 412 CX_TEST_ASSERT(r > 0);
251 CX_TEST_ASSERT(n == &cc); 413 CX_TEST_ASSERT(n == &cc);
252 } 414 }
253 } 415 }
254 416
255 CX_TEST(test_tree_iterator_create_and_dispose) { 417 CX_TEST(test_tree_iterator_create_and_dispose) {
256 tree_node root; 418 tree_node root;
257 CX_TEST_DO { 419 CX_TEST_DO {
258 CxTreeIterator iter = cx_tree_iterator(&root, false, tree_child_list); 420 CxTreeIterator iter = cx_tree_iterator(&root, false, tree_children(tree_node));
259 CX_TEST_ASSERT(!iter.visit_on_exit); 421 CX_TEST_ASSERT(!iter.visit_on_exit);
260 CX_TEST_ASSERT(!iter.exiting); 422 CX_TEST_ASSERT(!iter.exiting);
261 CX_TEST_ASSERT(iter.counter == 1); 423 CX_TEST_ASSERT(iter.counter == 1);
262 CX_TEST_ASSERT(iter.node == &root); 424 CX_TEST_ASSERT(iter.node == &root);
263 CX_TEST_ASSERT(!iter.base.mutating); 425 CX_TEST_ASSERT(!iter.base.mutating);
286 tree_node cc = {0}; 448 tree_node cc = {0};
287 tree_node cba = {0}; 449 tree_node cba = {0};
288 450
289 tree_node* expected_order[] = { 451 tree_node* expected_order[] = {
290 &root, 452 &root,
291 &c, &cc,&cb, &cba,&ca, 453 &a, &aa, &ab,
292 &b, &ba, 454 &b, &ba,
293 &a, &ab, &aa, 455 &c, &ca, &cb, &cba, &cc,
294 }; 456 };
295 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong 457 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong
296 458
297 cx_tree_link(&root, &a, tree_node_layout); 459 cx_tree_link(&root, &a, tree_node_layout);
298 cx_tree_link(&root, &b, tree_node_layout); 460 cx_tree_link(&root, &b, tree_node_layout);
303 cx_tree_link(&c, &ca, tree_node_layout); 465 cx_tree_link(&c, &ca, tree_node_layout);
304 cx_tree_link(&c, &cb, tree_node_layout); 466 cx_tree_link(&c, &cb, tree_node_layout);
305 cx_tree_link(&c, &cc, tree_node_layout); 467 cx_tree_link(&c, &cc, tree_node_layout);
306 cx_tree_link(&cb, &cba, tree_node_layout); 468 cx_tree_link(&cb, &cba, tree_node_layout);
307 CX_TEST_DO { 469 CX_TEST_DO {
308 CxTreeIterator iter = cx_tree_iterator(&root, false, tree_child_list); 470 CxTreeIterator iter = cx_tree_iterator(&root, false, tree_children(tree_node));
309 unsigned chk = 0; 471 unsigned chk = 0;
310 cx_foreach(tree_node*, node, iter) { 472 cx_foreach(tree_node*, node, iter) {
311 CX_TEST_ASSERT(node->data == 0); 473 CX_TEST_ASSERT(node->data == 0);
312 node->data++; 474 node->data++;
313 actual_order[chk] = node; 475 actual_order[chk] = node;
356 cx_tree_link(&c, &ca, tree_node_layout); 518 cx_tree_link(&c, &ca, tree_node_layout);
357 cx_tree_link(&c, &cb, tree_node_layout); 519 cx_tree_link(&c, &cb, tree_node_layout);
358 cx_tree_link(&c, &cc, tree_node_layout); 520 cx_tree_link(&c, &cc, tree_node_layout);
359 cx_tree_link(&cb, &cba, tree_node_layout); 521 cx_tree_link(&cb, &cba, tree_node_layout);
360 CX_TEST_DO { 522 CX_TEST_DO {
361 CxTreeIterator iter = cx_tree_iterator(&root, true, tree_child_list); 523 CxTreeIterator iter = cx_tree_iterator(&root, true, tree_children(tree_node));
362 unsigned chk = 0; 524 unsigned chk = 0;
363 cx_foreach(tree_node*, node, iter) { 525 cx_foreach(tree_node*, node, iter) {
364 CX_TEST_ASSERT(iter.exiting || node->data == 0); 526 CX_TEST_ASSERT(iter.exiting || node->data == 0);
365 node->data++; 527 node->data++;
366 chk++; 528 chk++;
391 CX_TEST_ASSERT(cba.data == 2); 553 CX_TEST_ASSERT(cba.data == 2);
392 } 554 }
393 } 555 }
394 556
395 typedef struct test_xml_node { 557 typedef struct test_xml_node {
396 struct test_xml_node *parent; 558 struct tree_node base;
397 struct test_xml_node *next;
398 struct test_xml_node *prev;
399 struct test_xml_node *children;
400 char const* name; 559 char const* name;
401 } test_xml_node; 560 } test_xml_node;
402 561
403 CX_TEST(test_tree_iterator_xml) { 562 CX_TEST(test_tree_iterator_xml) {
404 test_xml_node project = {0}; 563 test_xml_node project = {0};
429 target.name = "target"; 588 target.name = "target";
430 target_feature.name = "feature"; 589 target_feature.name = "feature";
431 target_dependencies.name = "dependencies"; 590 target_dependencies.name = "dependencies";
432 target_feature_dependencies.name = "dependencies"; 591 target_feature_dependencies.name = "dependencies";
433 592
593 cx_tree_link(&project, &config, tree_node_layout);
594 cx_tree_link(&project, &dependency1, tree_node_layout);
595 cx_tree_link(&project, &dependency2, tree_node_layout);
434 cx_tree_link(&project, &target, tree_node_layout); 596 cx_tree_link(&project, &target, tree_node_layout);
435 cx_tree_link(&project, &dependency2, tree_node_layout); 597 cx_tree_link(&config, &var1, tree_node_layout);
436 cx_tree_link(&project, &dependency1, tree_node_layout); 598 cx_tree_link(&config, &var2, tree_node_layout);
437 cx_tree_link(&project, &config, tree_node_layout);
438 cx_tree_link(&config, &var3, tree_node_layout); 599 cx_tree_link(&config, &var3, tree_node_layout);
439 cx_tree_link(&config, &var2, tree_node_layout);
440 cx_tree_link(&config, &var1, tree_node_layout);
441 cx_tree_link(&dependency1, &dependency1make, tree_node_layout); 600 cx_tree_link(&dependency1, &dependency1make, tree_node_layout);
601 cx_tree_link(&dependency2, &dependency2lang, tree_node_layout);
442 cx_tree_link(&dependency2, &dependency2make, tree_node_layout); 602 cx_tree_link(&dependency2, &dependency2make, tree_node_layout);
443 cx_tree_link(&dependency2, &dependency2lang, tree_node_layout); 603 cx_tree_link(&target, &target_feature, tree_node_layout);
444 cx_tree_link(&target, &target_dependencies, tree_node_layout); 604 cx_tree_link(&target, &target_dependencies, tree_node_layout);
445 cx_tree_link(&target, &target_feature, tree_node_layout);
446 cx_tree_link(&target_feature, &target_feature_dependencies, tree_node_layout); 605 cx_tree_link(&target_feature, &target_feature_dependencies, tree_node_layout);
447 606
448 char const *expected = 607 char const *expected =
449 "<project><config><var></var><var></var><var></var></config>" 608 "<project><config><var></var><var></var><var></var></config>"
450 "<dependency><make></make></dependency><dependency><lang></lang><make></make></dependency>" 609 "<dependency><make></make></dependency><dependency><lang></lang><make></make></dependency>"
451 "<target><feature><dependencies></dependencies></feature><dependencies></dependencies></target></project>"; 610 "<target><feature><dependencies></dependencies></feature><dependencies></dependencies></target></project>";
452 char *actual = malloc(512); 611 char *actual = malloc(512);
453 CX_TEST_DO { 612 CX_TEST_DO {
454 CxTreeIterator iter = cx_tree_iterator(&project, true, tree_child_list); 613 CxTreeIterator iter = cx_tree_iterator(&project, true, tree_children(tree_node));
455 size_t i = 0; 614 size_t i = 0;
456 cx_foreach(test_xml_node*, node, iter) { 615 cx_foreach(test_xml_node*, node, iter) {
457 size_t len = strlen(node->name); 616 size_t len = strlen(node->name);
458 actual[i++] = '<'; 617 actual[i++] = '<';
459 if (iter.exiting) { 618 if (iter.exiting) {
495 cx_tree_link(c, ca, tree_node_layout); 654 cx_tree_link(c, ca, tree_node_layout);
496 cx_tree_link(c, cb, tree_node_layout); 655 cx_tree_link(c, cb, tree_node_layout);
497 cx_tree_link(c, cc, tree_node_layout); 656 cx_tree_link(c, cc, tree_node_layout);
498 cx_tree_link(cb, cba, tree_node_layout); 657 cx_tree_link(cb, cba, tree_node_layout);
499 658
500 CxTreeIterator iter = cx_tree_iterator(root, true, tree_child_list); 659 CxTreeIterator iter = cx_tree_iterator(root, true, tree_children(tree_node));
501 cx_foreach(tree_node *, node, iter) { 660 cx_foreach(tree_node *, node, iter) {
502 if (iter.exiting) { 661 if (iter.exiting) {
503 cxFree(alloc,node); 662 cxFree(alloc,node);
504 } 663 }
505 } 664 }
512 CX_TEST(test_tree_visitor_create_and_dispose) { 671 CX_TEST(test_tree_visitor_create_and_dispose) {
513 tree_node root; 672 tree_node root;
514 tree_node child; 673 tree_node child;
515 cx_tree_link(&root, &child, tree_node_layout); 674 cx_tree_link(&root, &child, tree_node_layout);
516 CX_TEST_DO { 675 CX_TEST_DO {
517 CxTreeVisitor iter = cx_tree_visitor(&root, tree_child_list); 676 CxTreeVisitor iter = cx_tree_visitor(&root, tree_children(tree_node));
518 CX_TEST_ASSERT(iter.counter == 1); 677 CX_TEST_ASSERT(iter.counter == 1);
519 CX_TEST_ASSERT(iter.node == &root); 678 CX_TEST_ASSERT(iter.node == &root);
520 CX_TEST_ASSERT(!iter.base.mutating); 679 CX_TEST_ASSERT(!iter.base.mutating);
521 CX_TEST_ASSERT(!iter.base.remove); 680 CX_TEST_ASSERT(!iter.base.remove);
522 CX_TEST_ASSERT(iter.queue_next != NULL); 681 CX_TEST_ASSERT(iter.queue_next != NULL);
543 tree_node cc = {0}; 702 tree_node cc = {0};
544 tree_node cba = {0}; 703 tree_node cba = {0};
545 704
546 tree_node* expected_order[] = { 705 tree_node* expected_order[] = {
547 &root, 706 &root,
548 &c, &b, &a, 707 &a, &b, &c,
549 &cc, &cb, &ca, &ba, &ab, &aa, 708 &aa, &ab, &ba, &ca, &cb, &cc,
550 &cba 709 &cba
551 }; 710 };
552 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong 711 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong
553 712
554 cx_tree_link(&root, &a, tree_node_layout); 713 cx_tree_link(&root, &a, tree_node_layout);
560 cx_tree_link(&c, &ca, tree_node_layout); 719 cx_tree_link(&c, &ca, tree_node_layout);
561 cx_tree_link(&c, &cb, tree_node_layout); 720 cx_tree_link(&c, &cb, tree_node_layout);
562 cx_tree_link(&c, &cc, tree_node_layout); 721 cx_tree_link(&c, &cc, tree_node_layout);
563 cx_tree_link(&cb, &cba, tree_node_layout); 722 cx_tree_link(&cb, &cba, tree_node_layout);
564 CX_TEST_DO { 723 CX_TEST_DO {
565 CxTreeVisitor iter = cx_tree_visitor(&root, tree_child_list); 724 CxTreeVisitor iter = cx_tree_visitor(&root, tree_children(tree_node));
566 unsigned chk = 0; 725 unsigned chk = 0;
567 cx_foreach(tree_node*, node, iter) { 726 cx_foreach(tree_node*, node, iter) {
568 CX_TEST_ASSERT(node->data == 0); 727 CX_TEST_ASSERT(node->data == 0);
569 node->data++; 728 node->data++;
570 actual_order[chk] = node; 729 actual_order[chk] = node;
593 752
594 CX_TEST(test_tree_visitor_no_children) { 753 CX_TEST(test_tree_visitor_no_children) {
595 tree_node root = {0}; 754 tree_node root = {0};
596 755
597 CX_TEST_DO { 756 CX_TEST_DO {
598 CxTreeVisitor iter = cx_tree_visitor(&root, tree_child_list); 757 CxTreeVisitor iter = cx_tree_visitor(&root, tree_children(tree_node));
599 unsigned chk = 0; 758 unsigned chk = 0;
600 cx_foreach(tree_node*, node, iter) { 759 cx_foreach(tree_node*, node, iter) {
601 CX_TEST_ASSERT(node == iter.node); 760 CX_TEST_ASSERT(node == iter.node);
602 chk++; 761 chk++;
603 } 762 }
622 cx_tree_link(&root, &a, tree_node_layout); 781 cx_tree_link(&root, &a, tree_node_layout);
623 cx_tree_link(&a, &b, tree_node_layout); 782 cx_tree_link(&a, &b, tree_node_layout);
624 cx_tree_link(&b, &c, tree_node_layout); 783 cx_tree_link(&b, &c, tree_node_layout);
625 784
626 CX_TEST_DO { 785 CX_TEST_DO {
627 CxTreeVisitor iter = cx_tree_visitor(&root, tree_child_list); 786 CxTreeVisitor iter = cx_tree_visitor(&root, tree_children(tree_node));
628 unsigned chk = 0; 787 unsigned chk = 0;
629 cx_foreach(tree_node*, node, iter) { 788 cx_foreach(tree_node*, node, iter) {
630 CX_TEST_ASSERT(node == iter.node); 789 CX_TEST_ASSERT(node == iter.node);
631 CX_TEST_ASSERT(node->data == 0); 790 CX_TEST_ASSERT(node->data == 0);
632 node->data++; 791 node->data++;
657 tree_node cc = {0}; 816 tree_node cc = {0};
658 tree_node cba = {0}; 817 tree_node cba = {0};
659 818
660 tree_node* expected_order[] = { 819 tree_node* expected_order[] = {
661 &root, 820 &root,
662 &c, &b, &a, 821 &a, &b, &c,
663 &ba, &ab, &aa 822 &aa, &ab, &ba
664 }; 823 };
665 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong 824 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong
666 825
667 cx_tree_link(&root, &a, tree_node_layout); 826 cx_tree_link(&root, &a, tree_node_layout);
668 cx_tree_link(&root, &b, tree_node_layout); 827 cx_tree_link(&root, &b, tree_node_layout);
673 cx_tree_link(&c, &ca, tree_node_layout); 832 cx_tree_link(&c, &ca, tree_node_layout);
674 cx_tree_link(&c, &cb, tree_node_layout); 833 cx_tree_link(&c, &cb, tree_node_layout);
675 cx_tree_link(&c, &cc, tree_node_layout); 834 cx_tree_link(&c, &cc, tree_node_layout);
676 cx_tree_link(&cb, &cba, tree_node_layout); 835 cx_tree_link(&cb, &cba, tree_node_layout);
677 CX_TEST_DO { 836 CX_TEST_DO {
678 CxTreeVisitor iter = cx_tree_visitor(&root, tree_child_list); 837 CxTreeVisitor iter = cx_tree_visitor(&root, tree_children(tree_node));
679 unsigned chk = 0; 838 unsigned chk = 0;
680 cx_foreach(tree_node*, node, iter) { 839 cx_foreach(tree_node*, node, iter) {
681 CX_TEST_ASSERT(node->data == 0); 840 CX_TEST_ASSERT(node->data == 0);
682 node->data++; 841 node->data++;
683 actual_order[chk] = node; 842 actual_order[chk] = node;
720 tree_node ca = {0}; 879 tree_node ca = {0};
721 tree_node cb = {0}; 880 tree_node cb = {0};
722 tree_node cc = {0}; 881 tree_node cc = {0};
723 tree_node cba = {0}; 882 tree_node cba = {0};
724 883
725 tree_node* expected_order[] = { 884 tree_node *expected_order[] = {
726 &root, 885 &root,
886 &a, &aa, &ab,
887 &b, &ba,
727 &c, 888 &c,
728 &b, &ba,
729 &a, &ab, &aa,
730 }; 889 };
731 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong 890 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong
732 891
733 cx_tree_link(&root, &a, tree_node_layout); 892 cx_tree_link(&root, &a, tree_node_layout);
734 cx_tree_link(&root, &b, tree_node_layout); 893 cx_tree_link(&root, &b, tree_node_layout);
739 cx_tree_link(&c, &ca, tree_node_layout); 898 cx_tree_link(&c, &ca, tree_node_layout);
740 cx_tree_link(&c, &cb, tree_node_layout); 899 cx_tree_link(&c, &cb, tree_node_layout);
741 cx_tree_link(&c, &cc, tree_node_layout); 900 cx_tree_link(&c, &cc, tree_node_layout);
742 cx_tree_link(&cb, &cba, tree_node_layout); 901 cx_tree_link(&cb, &cba, tree_node_layout);
743 CX_TEST_DO { 902 CX_TEST_DO {
744 CxTreeIterator iter = cx_tree_iterator(&root, false, tree_child_list); 903 CxTreeIterator iter = cx_tree_iterator(&root, false, tree_children(tree_node));
745 unsigned chk = 0; 904 unsigned chk = 0;
746 cx_foreach(tree_node*, node, iter) { 905 cx_foreach(tree_node*, node, iter) {
747 CX_TEST_ASSERT(node->data == 0); 906 CX_TEST_ASSERT(node->data == 0);
748 node->data++; 907 node->data++;
749 actual_order[chk] = node; 908 actual_order[chk] = node;
797 cx_tree_link(&c, &ca, tree_node_layout); 956 cx_tree_link(&c, &ca, tree_node_layout);
798 cx_tree_link(&c, &cb, tree_node_layout); 957 cx_tree_link(&c, &cb, tree_node_layout);
799 cx_tree_link(&c, &cc, tree_node_layout); 958 cx_tree_link(&c, &cc, tree_node_layout);
800 cx_tree_link(&cb, &cba, tree_node_layout); 959 cx_tree_link(&cb, &cba, tree_node_layout);
801 CX_TEST_DO { 960 CX_TEST_DO {
802 CxTreeIterator iter = cx_tree_iterator(&root, true, tree_child_list); 961 CxTreeIterator iter = cx_tree_iterator(&root, true, tree_children(tree_node));
803 unsigned chk = 0; 962 unsigned chk = 0;
804 cx_foreach(tree_node*, node, iter) { 963 cx_foreach(tree_node*, node, iter) {
805 CX_TEST_ASSERT(iter.exiting || node->data == 0); 964 CX_TEST_ASSERT(iter.exiting || node->data == 0);
806 node->data++; 965 node->data++;
807 chk++; 966 chk++;
839 998
840 cx_test_register(suite, test_tree_link_new_child); 999 cx_test_register(suite, test_tree_link_new_child);
841 cx_test_register(suite, test_tree_link_add_child); 1000 cx_test_register(suite, test_tree_link_add_child);
842 cx_test_register(suite, test_tree_link_move_to_other_parent); 1001 cx_test_register(suite, test_tree_link_move_to_other_parent);
843 cx_test_register(suite, test_tree_unlink); 1002 cx_test_register(suite, test_tree_unlink);
1003 cx_test_register(suite, test_tree2_link_new_child);
1004 cx_test_register(suite, test_tree2_link_add_child);
1005 cx_test_register(suite, test_tree2_link_move_to_other_parent);
1006 cx_test_register(suite, test_tree2_unlink);
844 cx_test_register(suite, test_tree_search); 1007 cx_test_register(suite, test_tree_search);
845 cx_test_register(suite, test_tree_iterator_create_and_dispose); 1008 cx_test_register(suite, test_tree_iterator_create_and_dispose);
846 cx_test_register(suite, test_tree_iterator_basic_only_enter); 1009 cx_test_register(suite, test_tree_iterator_basic_only_enter);
847 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit); 1010 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit);
848 cx_test_register(suite, test_tree_iterator_xml); 1011 cx_test_register(suite, test_tree_iterator_xml);

mercurial