tests/test_tree.c

changeset 839
62d3aecc5bb7
parent 838
1ce90ab4fab9
child 840
4f02995ce44e
equal deleted inserted replaced
838:1ce90ab4fab9 839:62d3aecc5bb7
386 CX_TEST_ASSERT(cc.data == 2); 386 CX_TEST_ASSERT(cc.data == 2);
387 CX_TEST_ASSERT(cba.data == 2); 387 CX_TEST_ASSERT(cba.data == 2);
388 } 388 }
389 } 389 }
390 390
391 typedef struct test_xml_node {
392 struct test_xml_node *parent;
393 struct test_xml_node *next;
394 struct test_xml_node *prev;
395 struct test_xml_node *children;
396 char const* name;
397 } test_xml_node;
398
399 CX_TEST(test_tree_iterator_xml) {
400 test_xml_node project = {0};
401 test_xml_node config = {0};
402 test_xml_node var1 = {0};
403 test_xml_node var2 = {0};
404 test_xml_node var3 = {0};
405 test_xml_node dependency1 = {0};
406 test_xml_node dependency1make = {0};
407 test_xml_node dependency2 = {0};
408 test_xml_node dependency2lang = {0};
409 test_xml_node dependency2make = {0};
410 test_xml_node target = {0};
411 test_xml_node target_feature = {0};
412 test_xml_node target_feature_dependencies = {0};
413 test_xml_node target_dependencies = {0};
414
415 project.name = "project";
416 config.name = "config";
417 var1.name = "var";
418 var2.name = "var";
419 var3.name = "var";
420 dependency1.name = "dependency";
421 dependency1make.name = "make";
422 dependency2.name = "dependency";
423 dependency2lang.name = "lang";
424 dependency2make.name = "make";
425 target.name = "target";
426 target_feature.name = "feature";
427 target_dependencies.name = "dependencies";
428 target_feature_dependencies.name = "dependencies";
429
430 cx_tree_link(&project, &target, tree_node_layout);
431 cx_tree_link(&project, &dependency2, tree_node_layout);
432 cx_tree_link(&project, &dependency1, tree_node_layout);
433 cx_tree_link(&project, &config, tree_node_layout);
434 cx_tree_link(&config, &var3, tree_node_layout);
435 cx_tree_link(&config, &var2, tree_node_layout);
436 cx_tree_link(&config, &var1, tree_node_layout);
437 cx_tree_link(&dependency1, &dependency1make, tree_node_layout);
438 cx_tree_link(&dependency2, &dependency2make, tree_node_layout);
439 cx_tree_link(&dependency2, &dependency2lang, tree_node_layout);
440 cx_tree_link(&target, &target_dependencies, tree_node_layout);
441 cx_tree_link(&target, &target_feature, tree_node_layout);
442 cx_tree_link(&target_feature, &target_feature_dependencies, tree_node_layout);
443
444 char const *expected =
445 "<project><config><var></var><var></var><var></var></config>"
446 "<dependency><make></make></dependency><dependency><lang></lang><make></make></dependency>"
447 "<target><feature><dependencies></dependencies></feature><dependencies></dependencies></target></project>";
448 char *actual = malloc(512);
449 CX_TEST_DO {
450 CxTreeIterator iter = cx_tree_iterator(&project, true, tree_child_list);
451 size_t i = 0;
452 cx_foreach(test_xml_node*, node, iter) {
453 size_t len = strlen(node->name);
454 actual[i++] = '<';
455 if (iter.exiting) {
456 actual[i++] = '/';
457 }
458 memcpy(actual+i, node->name, len);
459 i += len;
460 actual[i++] = '>';
461 }
462 actual[i] = '\0';
463 CX_TEST_ASSERT(0 == strcmp(expected, actual));
464 }
465 free(actual);
466 }
467
391 CxTestSuite *cx_test_suite_tree_low_level(void) { 468 CxTestSuite *cx_test_suite_tree_low_level(void) {
392 CxTestSuite *suite = cx_test_suite_new("tree (low level)"); 469 CxTestSuite *suite = cx_test_suite_new("tree (low level)");
393 470
394 cx_test_register(suite, test_tree_link_new_child); 471 cx_test_register(suite, test_tree_link_new_child);
395 cx_test_register(suite, test_tree_link_add_child); 472 cx_test_register(suite, test_tree_link_add_child);
397 cx_test_register(suite, test_tree_unlink); 474 cx_test_register(suite, test_tree_unlink);
398 cx_test_register(suite, test_tree_search); 475 cx_test_register(suite, test_tree_search);
399 cx_test_register(suite, test_tree_iterator_create_and_dispose); 476 cx_test_register(suite, test_tree_iterator_create_and_dispose);
400 cx_test_register(suite, test_tree_iterator_basic_only_enter); 477 cx_test_register(suite, test_tree_iterator_basic_only_enter);
401 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit); 478 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit);
479 cx_test_register(suite, test_tree_iterator_xml);
402 480
403 return suite; 481 return suite;
404 } 482 }

mercurial