tests/test_tree.c

changeset 930
6540096c17b7
parent 918
ec1f2015ec79
child 931
be71809e69d1
equal deleted inserted replaced
929:192a440b99df 930:6540096c17b7
427 int r; 427 int r;
428 tree_node *n; 428 tree_node *n;
429 CX_TEST_DO { 429 CX_TEST_DO {
430 for (unsigned i = 0 ; i <= 10 ; i++) { 430 for (unsigned i = 0 ; i <= 10 ; i++) {
431 s = testdata[i]; 431 s = testdata[i];
432 r = cx_tree_search_data(&root, &s, test_tree_search_function, 432 r = cx_tree_search_data(&root, 0, &s, test_tree_search_function,
433 (void **) &n, tree_children(tree_node)); 433 (void **) &n, tree_children(tree_node));
434 CX_TEST_ASSERT(r == 0); 434 CX_TEST_ASSERT(r == 0);
435 CX_TEST_ASSERT(n == testnodes[i]); 435 CX_TEST_ASSERT(n == testnodes[i]);
436 } 436 }
437 437
438 s = -5; 438 s = -5;
439 r = cx_tree_search_data(&root, &s, test_tree_search_function, 439 r = cx_tree_search_data(&root, 0, &s, test_tree_search_function,
440 (void **) &n, tree_children(tree_node)); 440 (void **) &n, tree_children(tree_node));
441 CX_TEST_ASSERT(r < 0); 441 CX_TEST_ASSERT(r < 0);
442 CX_TEST_ASSERT(n == NULL); 442 CX_TEST_ASSERT(n == NULL);
443 443
444 s = 26; 444 s = 26;
445 r = cx_tree_search_data(&root, &s, test_tree_search_function, 445 r = cx_tree_search_data(&root, 0, &s, test_tree_search_function,
446 (void **) &n, tree_children(tree_node)); 446 (void **) &n, tree_children(tree_node));
447 CX_TEST_ASSERT(r > 0); 447 CX_TEST_ASSERT(r > 0);
448 CX_TEST_ASSERT(n == &ba); 448 CX_TEST_ASSERT(n == &ba);
449 449
450 s = 35; 450 s = 35;
451 r = cx_tree_search_data(&root, &s, test_tree_search_function, 451 r = cx_tree_search_data(&root, 0, &s, test_tree_search_function,
452 (void **) &n, tree_children(tree_node)); 452 (void **) &n, tree_children(tree_node));
453 CX_TEST_ASSERT(r > 0); 453 CX_TEST_ASSERT(r > 0);
454 CX_TEST_ASSERT(n == &cb); 454 CX_TEST_ASSERT(n == &cb);
455 455
456 s = 38; 456 s = 38;
457 r = cx_tree_search_data(&root, &s, test_tree_search_function, 457 r = cx_tree_search_data(&root, 0, &s, test_tree_search_function,
458 (void **) &n, tree_children(tree_node)); 458 (void **) &n, tree_children(tree_node));
459 CX_TEST_ASSERT(r > 0); 459 CX_TEST_ASSERT(r > 0);
460 CX_TEST_ASSERT(n == &cba); 460 CX_TEST_ASSERT(n == &cba);
461 461
462 s = 42; 462 s = 42;
463 r = cx_tree_search_data(&root, &s, test_tree_search_function, 463 r = cx_tree_search_data(&root, 0, &s, test_tree_search_function,
464 (void **) &n, tree_children(tree_node)); 464 (void **) &n, tree_children(tree_node));
465 CX_TEST_ASSERT(r > 0); 465 CX_TEST_ASSERT(r > 0);
466 CX_TEST_ASSERT(n == &cc); 466 CX_TEST_ASSERT(n == &cc);
467 }
468 }
469
470 CX_TEST(test_tree_search_with_max_depth) {
471 tree_node_file root = {0};
472 root.path = "/";
473 tree_node_file usr = {0};
474 usr.path = "/usr/";
475 cx_tree_link(&root, &usr, tree_node_file_layout);
476 tree_node_file home = {0};
477 home.path = "/home/";
478 cx_tree_link(&root, &home, tree_node_file_layout);
479 tree_node_file doe = {0};
480 doe.path = "/home/doe/";
481 cx_tree_link(&home, &doe, tree_node_file_layout);
482 tree_node_file lib = {0};
483 lib.path = "/usr/lib/";
484 cx_tree_link(&usr, &lib, tree_node_file_layout);
485 tree_node_file modules = {0};
486 modules.path = "/usr/lib/modules/";
487 cx_tree_link(&lib, &modules, tree_node_file_layout);
488
489 CX_TEST_DO {
490 int result;
491 void *found;
492
493 result = cx_tree_search_data(
494 &root, 1, "/",
495 tree_node_file_search_data, &found,
496 tree_children(tree_node_file)
497 );
498 CX_TEST_ASSERTM(result == 0, "root not found");
499 CX_TEST_ASSERT(found == &root);
500
501 result = cx_tree_search_data(
502 &root, 1, "/usr/",
503 tree_node_file_search_data, &found,
504 tree_children(tree_node_file)
505 );
506 CX_TEST_ASSERT(result > 0);
507 CX_TEST_ASSERT(found == &root);
508
509 result = cx_tree_search_data(
510 &root, 2, "/usr/",
511 tree_node_file_search_data, &found,
512 tree_children(tree_node_file)
513 );
514 CX_TEST_ASSERT(result == 0);
515 CX_TEST_ASSERT(found == &usr);
516
517 result = cx_tree_search_data(
518 &root, 2, "/usr/lib/",
519 tree_node_file_search_data, &found,
520 tree_children(tree_node_file)
521 );
522 CX_TEST_ASSERT(result > 0);
523 CX_TEST_ASSERT(found == &usr);
524
525 result = cx_tree_search_data(
526 &root, 3, "/usr/lib/",
527 tree_node_file_search_data, &found,
528 tree_children(tree_node_file)
529 );
530 CX_TEST_ASSERTM(result == 0, "lib not found");
531 CX_TEST_ASSERT(found == &lib);
532
533 result = cx_tree_search_data(
534 &root, 1, "/home/",
535 tree_node_file_search_data, &found,
536 tree_children(tree_node_file)
537 );
538 CX_TEST_ASSERT(result > 0);
539 CX_TEST_ASSERT(found == &root);
540
541 result = cx_tree_search_data(
542 &root, 2, "/home/",
543 tree_node_file_search_data, &found,
544 tree_children(tree_node_file)
545 );
546 CX_TEST_ASSERTM(result == 0, "home not found");
547 CX_TEST_ASSERT(found == &home);
548
549 result = cx_tree_search_data(
550 &root, 2, "/home/doe/",
551 tree_node_file_search_data, &found,
552 tree_children(tree_node_file)
553 );
554 CX_TEST_ASSERT(result > 0);
555 CX_TEST_ASSERT(found == &home);
556
557 result = cx_tree_search_data(
558 &root, 3, "/home/doe/",
559 tree_node_file_search_data, &found,
560 tree_children(tree_node_file)
561 );
562 CX_TEST_ASSERTM(result == 0, "doe not found");
563 CX_TEST_ASSERT(found == &doe);
564
565 result = cx_tree_search_data(
566 &root, 3, "/usr/lib/modules/",
567 tree_node_file_search_data, &found,
568 tree_children(tree_node_file)
569 );
570 CX_TEST_ASSERT(result > 0);
571 CX_TEST_ASSERT(found == &lib);
572
573 result = cx_tree_search_data(
574 &root, 4, "/usr/lib/modules/",
575 tree_node_file_search_data, &found,
576 tree_children(tree_node_file)
577 );
578 CX_TEST_ASSERTM(result == 0, "modules not found (start=root)");
579 CX_TEST_ASSERT(found == &modules);
580
581 result = cx_tree_search_data(
582 &usr, 3, "/usr/lib/modules/",
583 tree_node_file_search_data, &found,
584 tree_children(tree_node_file)
585 );
586 CX_TEST_ASSERTM(result == 0, "modules not found (start=usr)");
587 CX_TEST_ASSERT(found == &modules);
467 } 588 }
468 } 589 }
469 590
470 CX_TEST(test_tree_iterator_create_and_dispose) { 591 CX_TEST(test_tree_iterator_create_and_dispose) {
471 tree_node root; 592 tree_node root;
1823 CX_TEST_ASSERT(NULL != baz->parent); 1944 CX_TEST_ASSERT(NULL != baz->parent);
1824 CX_TEST_ASSERT(0 == strcmp("/home/", baz->parent->path)); 1945 CX_TEST_ASSERT(0 == strcmp("/home/", baz->parent->path));
1825 CX_TEST_ASSERT(tree->size == 7); 1946 CX_TEST_ASSERT(tree->size == 7);
1826 1947
1827 tree_node_file *home = cxTreeFind(tree, "/home/"); 1948 tree_node_file *home = cxTreeFind(tree, "/home/");
1828 CX_TEST_ASSERT(NULL == cxTreeFindInSubtree(tree, "/usr/", foo)); 1949 CX_TEST_ASSERT(NULL == cxTreeFindInSubtree(tree, "/usr/", foo, 0));
1829 tree_node_file *bar = cxTreeFindInSubtree(tree, "/home/foo/bar/", home); 1950 tree_node_file *bar = cxTreeFindInSubtree(tree, "/home/foo/bar/", home, 0);
1830 CX_TEST_ASSERT(NULL != bar); 1951 CX_TEST_ASSERT(NULL != bar);
1831 CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path)); 1952 CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path));
1832 CX_TEST_ASSERT(bar->parent == foo); 1953 CX_TEST_ASSERT(bar->parent == foo);
1833 1954
1834 tree_node_file *share = cxCalloc(alloc, 1, sizeof(tree_node_file)); 1955 tree_node_file *share = cxCalloc(alloc, 1, sizeof(tree_node_file));
1914 CX_TEST_ASSERT(NULL != baz->parent); 2035 CX_TEST_ASSERT(NULL != baz->parent);
1915 CX_TEST_ASSERT(0 == strcmp("/home/", baz->parent->path)); 2036 CX_TEST_ASSERT(0 == strcmp("/home/", baz->parent->path));
1916 CX_TEST_ASSERT(tree->size == 7); 2037 CX_TEST_ASSERT(tree->size == 7);
1917 2038
1918 tree_node_file *home = cxTreeFind(tree, "/home/"); 2039 tree_node_file *home = cxTreeFind(tree, "/home/");
1919 CX_TEST_ASSERT(NULL == cxTreeFindInSubtree(tree, "/usr/", foo)); 2040 CX_TEST_ASSERT(NULL == cxTreeFindInSubtree(tree, "/usr/", foo, 0));
1920 tree_node_file *bar = cxTreeFindInSubtree(tree, "/home/foo/bar/", home); 2041 tree_node_file *bar = cxTreeFindInSubtree(tree, "/home/foo/bar/", home, 0);
1921 CX_TEST_ASSERT(NULL != bar); 2042 CX_TEST_ASSERT(NULL != bar);
1922 CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path)); 2043 CX_TEST_ASSERT(0 == strcmp("/home/foo/bar/", bar->path));
1923 CX_TEST_ASSERT(bar->parent == foo); 2044 CX_TEST_ASSERT(bar->parent == foo);
1924 2045
1925 tree_node_file *share = cxCalloc(alloc, 1, sizeof(tree_node_file)); 2046 tree_node_file *share = cxCalloc(alloc, 1, sizeof(tree_node_file));
2030 cx_test_register(suite, test_tree2_link_new_child); 2151 cx_test_register(suite, test_tree2_link_new_child);
2031 cx_test_register(suite, test_tree2_link_add_child); 2152 cx_test_register(suite, test_tree2_link_add_child);
2032 cx_test_register(suite, test_tree2_link_move_to_other_parent); 2153 cx_test_register(suite, test_tree2_link_move_to_other_parent);
2033 cx_test_register(suite, test_tree2_unlink); 2154 cx_test_register(suite, test_tree2_unlink);
2034 cx_test_register(suite, test_tree_search); 2155 cx_test_register(suite, test_tree_search);
2156 cx_test_register(suite, test_tree_search_with_max_depth);
2035 cx_test_register(suite, test_tree_iterator_create_and_dispose); 2157 cx_test_register(suite, test_tree_iterator_create_and_dispose);
2036 cx_test_register(suite, test_tree_iterator_create_for_empty_tree); 2158 cx_test_register(suite, test_tree_iterator_create_for_empty_tree);
2037 cx_test_register(suite, test_tree_iterator_basic_only_enter); 2159 cx_test_register(suite, test_tree_iterator_basic_only_enter);
2038 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit); 2160 cx_test_register(suite, test_tree_iterator_basic_enter_and_exit);
2039 cx_test_register(suite, test_tree_iterator_xml); 2161 cx_test_register(suite, test_tree_iterator_xml);

mercurial