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; |