284 tree_node ca = {0}; |
284 tree_node ca = {0}; |
285 tree_node cb = {0}; |
285 tree_node cb = {0}; |
286 tree_node cc = {0}; |
286 tree_node cc = {0}; |
287 tree_node cba = {0}; |
287 tree_node cba = {0}; |
288 |
288 |
|
289 tree_node* expected_order[] = { |
|
290 &root, |
|
291 &c, &cc,&cb, &cba,&ca, |
|
292 &b, &ba, |
|
293 &a, &ab, &aa, |
|
294 }; |
|
295 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong |
|
296 |
289 cx_tree_link(&root, &a, tree_node_layout); |
297 cx_tree_link(&root, &a, tree_node_layout); |
290 cx_tree_link(&root, &b, tree_node_layout); |
298 cx_tree_link(&root, &b, tree_node_layout); |
291 cx_tree_link(&root, &c, tree_node_layout); |
299 cx_tree_link(&root, &c, tree_node_layout); |
292 cx_tree_link(&a, &aa, tree_node_layout); |
300 cx_tree_link(&a, &aa, tree_node_layout); |
293 cx_tree_link(&a, &ab, tree_node_layout); |
301 cx_tree_link(&a, &ab, tree_node_layout); |
315 CX_TEST_ASSERT(iter.depth == 3); |
324 CX_TEST_ASSERT(iter.depth == 3); |
316 } |
325 } |
317 } |
326 } |
318 CX_TEST_ASSERT(iter.counter == 11); |
327 CX_TEST_ASSERT(iter.counter == 11); |
319 CX_TEST_ASSERT(chk == 11); |
328 CX_TEST_ASSERT(chk == 11); |
|
329 for (unsigned i = 0 ; i < chk ; i++) { |
|
330 CX_TEST_ASSERT(actual_order[i] == expected_order[i]); |
|
331 CX_TEST_ASSERT(actual_order[i]->data == 1); |
|
332 } |
320 CX_TEST_ASSERT(iter.stack == NULL); |
333 CX_TEST_ASSERT(iter.stack == NULL); |
321 CX_TEST_ASSERT(root.data == 1); |
|
322 CX_TEST_ASSERT(a.data == 1); |
|
323 CX_TEST_ASSERT(b.data == 1); |
|
324 CX_TEST_ASSERT(c.data == 1); |
|
325 CX_TEST_ASSERT(aa.data == 1); |
|
326 CX_TEST_ASSERT(ab.data == 1); |
|
327 CX_TEST_ASSERT(ba.data == 1); |
|
328 CX_TEST_ASSERT(ca.data == 1); |
|
329 CX_TEST_ASSERT(cb.data == 1); |
|
330 CX_TEST_ASSERT(cc.data == 1); |
|
331 CX_TEST_ASSERT(cba.data == 1); |
|
332 } |
334 } |
333 } |
335 } |
334 |
336 |
335 CX_TEST(test_tree_iterator_basic_enter_and_exit) { |
337 CX_TEST(test_tree_iterator_basic_enter_and_exit) { |
336 tree_node root = {0}; |
338 tree_node root = {0}; |
505 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
507 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
506 } |
508 } |
507 cx_testing_allocator_destroy(&talloc); |
509 cx_testing_allocator_destroy(&talloc); |
508 } |
510 } |
509 |
511 |
|
512 CX_TEST(test_tree_visitor) { |
|
513 tree_node root = {0}; |
|
514 tree_node a = {0}; |
|
515 tree_node b = {0}; |
|
516 tree_node c = {0}; |
|
517 tree_node aa = {0}; |
|
518 tree_node ab = {0}; |
|
519 tree_node ba = {0}; |
|
520 tree_node ca = {0}; |
|
521 tree_node cb = {0}; |
|
522 tree_node cc = {0}; |
|
523 tree_node cba = {0}; |
|
524 |
|
525 tree_node* expected_order[] = { |
|
526 &root, |
|
527 &c, &b, &a, |
|
528 &cc, &cb, &ca, &ba, &ab, &aa, |
|
529 &cba |
|
530 }; |
|
531 tree_node* actual_order[16]; // reserve more space in case s.t. goes wrong |
|
532 |
|
533 cx_tree_link(&root, &a, tree_node_layout); |
|
534 cx_tree_link(&root, &b, tree_node_layout); |
|
535 cx_tree_link(&root, &c, tree_node_layout); |
|
536 cx_tree_link(&a, &aa, tree_node_layout); |
|
537 cx_tree_link(&a, &ab, tree_node_layout); |
|
538 cx_tree_link(&b, &ba, tree_node_layout); |
|
539 cx_tree_link(&c, &ca, tree_node_layout); |
|
540 cx_tree_link(&c, &cb, tree_node_layout); |
|
541 cx_tree_link(&c, &cc, tree_node_layout); |
|
542 cx_tree_link(&cb, &cba, tree_node_layout); |
|
543 CX_TEST_DO { |
|
544 CxTreeVisitor iter = cx_tree_visitor(&root, tree_child_list); |
|
545 unsigned chk = 0; |
|
546 cx_foreach(tree_node*, node, iter) { |
|
547 CX_TEST_ASSERT(node->data == 0); |
|
548 node->data++; |
|
549 actual_order[chk] = node; |
|
550 chk++; |
|
551 CX_TEST_ASSERT(node == iter.node); |
|
552 if (node == &root) { |
|
553 CX_TEST_ASSERT(iter.depth == 1); |
|
554 } else if (node == &a || node == &b || node == &c) { |
|
555 CX_TEST_ASSERT(iter.depth == 2); |
|
556 } else if (node == &cba) { |
|
557 CX_TEST_ASSERT(iter.depth == 4); |
|
558 } else { |
|
559 CX_TEST_ASSERT(iter.depth == 3); |
|
560 } |
|
561 } |
|
562 CX_TEST_ASSERT(iter.counter == 11); |
|
563 CX_TEST_ASSERT(chk == 11); |
|
564 for (unsigned i = 0 ; i < chk ; i++) { |
|
565 CX_TEST_ASSERT(actual_order[i] == expected_order[i]); |
|
566 CX_TEST_ASSERT(actual_order[i]->data == 1); |
|
567 } |
|
568 CX_TEST_ASSERT(iter.queue_next == NULL); |
|
569 CX_TEST_ASSERT(iter.queue_last == NULL); |
|
570 } |
|
571 } |
|
572 |
|
573 CX_TEST(test_tree_visitor_no_children) { |
|
574 tree_node root = {0}; |
|
575 |
|
576 CX_TEST_DO { |
|
577 CxTreeVisitor iter = cx_tree_visitor(&root, tree_child_list); |
|
578 unsigned chk = 0; |
|
579 cx_foreach(tree_node*, node, iter) { |
|
580 CX_TEST_ASSERT(node == iter.node); |
|
581 chk++; |
|
582 } |
|
583 CX_TEST_ASSERT(iter.counter == 1); |
|
584 CX_TEST_ASSERT(chk == 1); |
|
585 CX_TEST_ASSERT(iter.queue_next == NULL); |
|
586 CX_TEST_ASSERT(iter.queue_last == NULL); |
|
587 } |
|
588 } |
|
589 |
|
590 CX_TEST(test_tree_visitor_no_branching) { |
|
591 tree_node root = {0}; |
|
592 tree_node a = {0}; |
|
593 tree_node b = {0}; |
|
594 tree_node c = {0}; |
|
595 |
|
596 tree_node* expected_order[] = { |
|
597 &root, &a, &b, &c |
|
598 }; |
|
599 tree_node* actual_order[4]; |
|
600 |
|
601 cx_tree_link(&root, &a, tree_node_layout); |
|
602 cx_tree_link(&a, &b, tree_node_layout); |
|
603 cx_tree_link(&b, &c, tree_node_layout); |
|
604 |
|
605 CX_TEST_DO { |
|
606 CxTreeVisitor iter = cx_tree_visitor(&root, tree_child_list); |
|
607 unsigned chk = 0; |
|
608 cx_foreach(tree_node*, node, iter) { |
|
609 CX_TEST_ASSERT(node == iter.node); |
|
610 CX_TEST_ASSERT(node->data == 0); |
|
611 node->data++; |
|
612 actual_order[chk] = node; |
|
613 chk++; |
|
614 } |
|
615 CX_TEST_ASSERT(iter.counter == 4); |
|
616 CX_TEST_ASSERT(chk == 4); |
|
617 CX_TEST_ASSERT(iter.queue_next == NULL); |
|
618 CX_TEST_ASSERT(iter.queue_last == NULL); |
|
619 for (unsigned i = 0 ; i < chk ; i++) { |
|
620 CX_TEST_ASSERT(actual_order[i] == expected_order[i]); |
|
621 CX_TEST_ASSERT(actual_order[i]->data == 1); |
|
622 } |
|
623 } |
|
624 } |
|
625 |
510 CxTestSuite *cx_test_suite_tree_low_level(void) { |
626 CxTestSuite *cx_test_suite_tree_low_level(void) { |
511 CxTestSuite *suite = cx_test_suite_new("tree (low level)"); |
627 CxTestSuite *suite = cx_test_suite_new("tree (low level)"); |
512 |
628 |
513 cx_test_register(suite, test_tree_link_new_child); |
629 cx_test_register(suite, test_tree_link_new_child); |
514 cx_test_register(suite, test_tree_link_add_child); |
630 cx_test_register(suite, test_tree_link_add_child); |
518 cx_test_register(suite, test_tree_iterator_create_and_dispose); |
634 cx_test_register(suite, test_tree_iterator_create_and_dispose); |
519 cx_test_register(suite, test_tree_iterator_basic_only_enter); |
635 cx_test_register(suite, test_tree_iterator_basic_only_enter); |
520 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit); |
636 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit); |
521 cx_test_register(suite, test_tree_iterator_xml); |
637 cx_test_register(suite, test_tree_iterator_xml); |
522 cx_test_register(suite, test_tree_iterator_free_nodes); |
638 cx_test_register(suite, test_tree_iterator_free_nodes); |
|
639 cx_test_register(suite, test_tree_visitor); |
|
640 cx_test_register(suite, test_tree_visitor_no_children); |
|
641 cx_test_register(suite, test_tree_visitor_no_branching); |
523 |
642 |
524 return suite; |
643 return suite; |
525 } |
644 } |