tests/test_tree.c

changeset 902
5ed7f634f046
parent 895
ea1ac0e8225c
child 903
a018f5916d3b
equal deleted inserted replaced
901:109567325fe7 902:5ed7f634f046
52 struct tree_node_file *children; 52 struct tree_node_file *children;
53 struct tree_node_file *last_child; 53 struct tree_node_file *last_child;
54 const char *path; 54 const char *path;
55 } tree_node_file; 55 } tree_node_file;
56 56
57 static void *create_tree_node_file( 57 static void *tree_node_file_create(
58 const void *dptr, 58 const void *dptr,
59 void *allocator) { 59 void *allocator) {
60 if (allocator == NULL) allocator = cxDefaultAllocator; 60 if (allocator == NULL) allocator = cxDefaultAllocator;
61 61
62 tree_node_file *node = cxMalloc(allocator, sizeof(tree_node_file)); 62 tree_node_file *node = cxMalloc(allocator, sizeof(tree_node_file));
1055 tree_node_file *foo; 1055 tree_node_file *foo;
1056 int result; 1056 int result;
1057 result = cx_tree_add( 1057 result = cx_tree_add(
1058 "/home/foo/", 1058 "/home/foo/",
1059 tree_node_file_search, 1059 tree_node_file_search,
1060 create_tree_node_file, alloc, 1060 tree_node_file_create, alloc,
1061 (void **) &foo, &root, 1061 (void **) &foo, &root,
1062 tree_node_file_layout 1062 tree_node_file_layout
1063 ); 1063 );
1064 CX_TEST_ASSERT(result == 0); 1064 CX_TEST_ASSERT(result == 0);
1065 CX_TEST_ASSERT(foo != NULL); 1065 CX_TEST_ASSERT(foo != NULL);
1066 const char *bar_path = "/home/foo/bar/"; 1066 const char *bar_path = "/home/foo/bar/";
1067 void *failed; 1067 void *failed;
1068 size_t added = cx_tree_add_array( 1068 size_t added = cx_tree_add_array(
1069 bar_path, 1, sizeof(const char *), 1069 bar_path, 1, sizeof(const char *),
1070 tree_node_file_search, 1070 tree_node_file_search,
1071 create_tree_node_file, alloc, 1071 tree_node_file_create, alloc,
1072 &failed, &root, 1072 &failed, &root,
1073 tree_node_file_layout 1073 tree_node_file_layout
1074 ); 1074 );
1075 CX_TEST_ASSERT(added == 1); 1075 CX_TEST_ASSERT(added == 1);
1076 CX_TEST_ASSERT(failed == NULL); 1076 CX_TEST_ASSERT(failed == NULL);
1082 1082
1083 tree_node_file *new_node; 1083 tree_node_file *new_node;
1084 result = cx_tree_add( 1084 result = cx_tree_add(
1085 "newroot", 1085 "newroot",
1086 tree_node_file_search, 1086 tree_node_file_search,
1087 create_tree_node_file, alloc, 1087 tree_node_file_create, alloc,
1088 (void **) &new_node, &root, 1088 (void **) &new_node, &root,
1089 tree_node_file_layout 1089 tree_node_file_layout
1090 ); 1090 );
1091 CX_TEST_ASSERT(0 != result); 1091 CX_TEST_ASSERT(0 != result);
1092 CX_TEST_ASSERT(NULL != new_node); 1092 CX_TEST_ASSERT(NULL != new_node);
1124 CX_TEST_DO { 1124 CX_TEST_DO {
1125 tree_node_file *node; 1125 tree_node_file *node;
1126 int result = cx_tree_add( 1126 int result = cx_tree_add(
1127 "/usr/lib/", 1127 "/usr/lib/",
1128 tree_node_file_search, 1128 tree_node_file_search,
1129 create_tree_node_file, alloc, 1129 tree_node_file_create, alloc,
1130 (void **) &node, &root, 1130 (void **) &node, &root,
1131 tree_node_file_layout 1131 tree_node_file_layout
1132 ); 1132 );
1133 CX_TEST_ASSERT(result == 0); 1133 CX_TEST_ASSERT(result == 0);
1134 CX_TEST_ASSERT(node != &lib); 1134 CX_TEST_ASSERT(node != &lib);
1149 CX_TEST_DO { 1149 CX_TEST_DO {
1150 tree_node_file *node = NULL; 1150 tree_node_file *node = NULL;
1151 int result = cx_tree_add( 1151 int result = cx_tree_add(
1152 "/usr/lib/", 1152 "/usr/lib/",
1153 tree_node_file_search, 1153 tree_node_file_search,
1154 create_tree_node_file, NULL, 1154 tree_node_file_create, NULL,
1155 (void **) &node, &root, 1155 (void **) &node, &root,
1156 tree_node_file_layout 1156 tree_node_file_layout
1157 ); 1157 );
1158 CX_TEST_ASSERT(result != 0); 1158 CX_TEST_ASSERT(result != 0);
1159 CX_TEST_ASSERT(node != NULL); 1159 CX_TEST_ASSERT(node != NULL);
1162 free(node); 1162 free(node);
1163 node = NULL; 1163 node = NULL;
1164 size_t added = cx_tree_add_array( 1164 size_t added = cx_tree_add_array(
1165 "/", 1, sizeof(const char *), 1165 "/", 1, sizeof(const char *),
1166 tree_node_file_search, 1166 tree_node_file_search,
1167 create_tree_node_file, NULL, 1167 tree_node_file_create, NULL,
1168 (void **) &node, &root, 1168 (void **) &node, &root,
1169 tree_node_file_layout 1169 tree_node_file_layout
1170 ); 1170 );
1171 CX_TEST_ASSERT(added == 0); 1171 CX_TEST_ASSERT(added == 0);
1172 CX_TEST_ASSERT(node != NULL); 1172 CX_TEST_ASSERT(node != NULL);
1182 CX_TEST_DO { 1182 CX_TEST_DO {
1183 tree_node_file *node; 1183 tree_node_file *node;
1184 int result = cx_tree_add( 1184 int result = cx_tree_add(
1185 "/", 1185 "/",
1186 tree_node_file_search, 1186 tree_node_file_search,
1187 create_tree_node_file, NULL, 1187 tree_node_file_create, NULL,
1188 (void **) &node, &root, 1188 (void **) &node, &root,
1189 tree_node_file_layout 1189 tree_node_file_layout
1190 ); 1190 );
1191 CX_TEST_ASSERT(result == 0); 1191 CX_TEST_ASSERT(result == 0);
1192 CX_TEST_ASSERT(root.children == node); 1192 CX_TEST_ASSERT(root.children == node);
1205 void *failed; 1205 void *failed;
1206 1206
1207 size_t processed = cx_tree_add_array( 1207 size_t processed = cx_tree_add_array(
1208 (void *) 0xc0ffee, 0, sizeof(void *), 1208 (void *) 0xc0ffee, 0, sizeof(void *),
1209 tree_node_file_search, 1209 tree_node_file_search,
1210 create_tree_node_file, alloc, 1210 tree_node_file_create, alloc,
1211 &failed, &root, tree_node_file_layout 1211 &failed, &root, tree_node_file_layout
1212 ); 1212 );
1213 CX_TEST_ASSERT(failed == NULL); 1213 CX_TEST_ASSERT(failed == NULL);
1214 CX_TEST_ASSERT(processed == 0); 1214 CX_TEST_ASSERT(processed == 0);
1215 CX_TEST_ASSERT(talloc.alloc_total == 0); 1215 CX_TEST_ASSERT(talloc.alloc_total == 0);
1217 CxIterator iter = cxIterator(NULL, sizeof(void *), 0); 1217 CxIterator iter = cxIterator(NULL, sizeof(void *), 0);
1218 processed = cx_tree_add_iter( 1218 processed = cx_tree_add_iter(
1219 cxIteratorRef(iter), 1219 cxIteratorRef(iter),
1220 10, // deliberately specify more than the iter has 1220 10, // deliberately specify more than the iter has
1221 tree_node_file_search, 1221 tree_node_file_search,
1222 create_tree_node_file, alloc, 1222 tree_node_file_create, alloc,
1223 &failed, &root, tree_node_file_layout 1223 &failed, &root, tree_node_file_layout
1224 ); 1224 );
1225 CX_TEST_ASSERT(processed == 0); 1225 CX_TEST_ASSERT(processed == 0);
1226 CX_TEST_ASSERT(failed == NULL); 1226 CX_TEST_ASSERT(failed == NULL);
1227 CX_TEST_ASSERT(talloc.alloc_total == 0); 1227 CX_TEST_ASSERT(talloc.alloc_total == 0);
1261 }; 1261 };
1262 1262
1263 size_t processed = cx_tree_add_array( 1263 size_t processed = cx_tree_add_array(
1264 paths, 4, sizeof(const char *), 1264 paths, 4, sizeof(const char *),
1265 tree_node_file_search, 1265 tree_node_file_search,
1266 create_tree_node_file, alloc, 1266 tree_node_file_create, alloc,
1267 &failed, &root, tree_node_file_layout 1267 &failed, &root, tree_node_file_layout
1268 ); 1268 );
1269 1269
1270 CX_TEST_ASSERT(failed == NULL); 1270 CX_TEST_ASSERT(failed == NULL);
1271 CX_TEST_ASSERT(processed == 4); 1271 CX_TEST_ASSERT(processed == 4);
1326 // create iterator for 4 elements, but choose to insert only 3 of them 1326 // create iterator for 4 elements, but choose to insert only 3 of them
1327 CxIterator iter = cxIterator(paths, sizeof(const char*), 4); 1327 CxIterator iter = cxIterator(paths, sizeof(const char*), 4);
1328 size_t processed = cx_tree_add_iter( 1328 size_t processed = cx_tree_add_iter(
1329 cxIteratorRef(iter), 3, 1329 cxIteratorRef(iter), 3,
1330 tree_node_file_search, 1330 tree_node_file_search,
1331 create_tree_node_file, alloc, 1331 tree_node_file_create, alloc,
1332 &failed, &root, tree_node_file_layout 1332 &failed, &root, tree_node_file_layout
1333 ); 1333 );
1334 1334
1335 CX_TEST_ASSERT(cxIteratorValid(iter)); 1335 CX_TEST_ASSERT(cxIteratorValid(iter));
1336 CX_TEST_ASSERT(cxIteratorCurrent(iter) == &paths[3]); 1336 CX_TEST_ASSERT(cxIteratorCurrent(iter) == &paths[3]);
1382 }; 1382 };
1383 1383
1384 size_t processed = cx_tree_add_array( 1384 size_t processed = cx_tree_add_array(
1385 paths, 5, sizeof(const char *), 1385 paths, 5, sizeof(const char *),
1386 tree_node_file_search, 1386 tree_node_file_search,
1387 create_tree_node_file, alloc, 1387 tree_node_file_create, alloc,
1388 (void **) &failed, &root, tree_node_file_layout 1388 (void **) &failed, &root, tree_node_file_layout
1389 ); 1389 );
1390 1390
1391 CX_TEST_ASSERT(processed == 3); 1391 CX_TEST_ASSERT(processed == 3);
1392 CX_TEST_ASSERT(failed != NULL); 1392 CX_TEST_ASSERT(failed != NULL);
1426 1426
1427 void *failed; 1427 void *failed;
1428 size_t processed = cx_tree_add_array( 1428 size_t processed = cx_tree_add_array(
1429 paths, 10, sizeof(const char *), 1429 paths, 10, sizeof(const char *),
1430 tree_node_file_search, 1430 tree_node_file_search,
1431 create_tree_node_file, alloc, 1431 tree_node_file_create, alloc,
1432 &failed, &root, tree_node_file_layout 1432 &failed, &root, tree_node_file_layout
1433 ); 1433 );
1434 1434
1435 CX_TEST_ASSERT(failed == NULL); 1435 CX_TEST_ASSERT(failed == NULL);
1436 CX_TEST_ASSERT(processed == 10); 1436 CX_TEST_ASSERT(processed == 10);
1524 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1524 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1525 } 1525 }
1526 cx_testing_allocator_destroy(&talloc); 1526 cx_testing_allocator_destroy(&talloc);
1527 } 1527 }
1528 1528
1529 CX_TEST(test_tree_high_create) {
1530 CxTestingAllocator talloc;
1531 cx_testing_allocator_init(&talloc);
1532 CX_TEST_DO {
1533 CxTree *tree = cxTreeCreate(
1534 &talloc.base,
1535 tree_node_file_create,
1536 tree_node_file_search,
1537 tree_node_file_layout
1538 );
1539 CX_TEST_ASSERT(tree->cl != NULL);
1540 CX_TEST_ASSERT(tree->allocator == &talloc.base);
1541 CX_TEST_ASSERT(tree->node_create == tree_node_file_create);
1542 CX_TEST_ASSERT(tree->search == tree_node_file_search);
1543 CX_TEST_ASSERT(tree->size == 0);
1544 CX_TEST_ASSERT(tree->simple_destructor == NULL);
1545 CX_TEST_ASSERT(tree->advanced_destructor == (cx_destructor_func2) cxFree);
1546 CX_TEST_ASSERT(tree->destructor_data == &talloc.base);
1547 CX_TEST_ASSERT(tree->root == NULL);
1548 CX_TEST_ASSERT(tree->loc_parent == offsetof(tree_node_file, parent));
1549 CX_TEST_ASSERT(tree->loc_children == offsetof(tree_node_file, children));
1550 CX_TEST_ASSERT(tree->loc_last_child == offsetof(tree_node_file, last_child));
1551 CX_TEST_ASSERT(tree->loc_prev == offsetof(tree_node_file, prev));
1552 CX_TEST_ASSERT(tree->loc_next == offsetof(tree_node_file, next));
1553
1554 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
1555 CX_TEST_ASSERT(talloc.alloc_total == 1);
1556
1557 cxTreeDestroy(tree);
1558 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1559 }
1560 cx_testing_allocator_destroy(&talloc);
1561 }
1562
1563 CX_TEST(test_tree_high_create_simple) {
1564 CX_TEST_DO {
1565 CxTree *tree = cxTreeCreateSimple(
1566 tree_node_file_create,
1567 tree_node_file_search
1568 );
1569 CX_TEST_ASSERT(tree->cl != NULL);
1570 CX_TEST_ASSERT(tree->allocator == cxDefaultAllocator);
1571 CX_TEST_ASSERT(tree->node_create == tree_node_file_create);
1572 CX_TEST_ASSERT(tree->search == tree_node_file_search);
1573 CX_TEST_ASSERT(tree->size == 0);
1574 CX_TEST_ASSERT(tree->simple_destructor == NULL);
1575 CX_TEST_ASSERT(tree->advanced_destructor == (cx_destructor_func2) cxFree);
1576 CX_TEST_ASSERT(tree->destructor_data == cxDefaultAllocator);
1577 CX_TEST_ASSERT(tree->root == NULL);
1578 CX_TEST_ASSERT(tree->loc_parent == offsetof(struct cx_tree_node_base_s, parent));
1579 CX_TEST_ASSERT(tree->loc_children == offsetof(struct cx_tree_node_base_s, children));
1580 CX_TEST_ASSERT(tree->loc_last_child == offsetof(struct cx_tree_node_base_s, last_child));
1581 CX_TEST_ASSERT(tree->loc_prev == offsetof(struct cx_tree_node_base_s, prev));
1582 CX_TEST_ASSERT(tree->loc_next == offsetof(struct cx_tree_node_base_s, next));
1583 cxTreeDestroy(tree);
1584 }
1585 }
1586
1587 CX_TEST(test_tree_high_create_wrapped) {
1588 tree_node root = {0}, child1 = {0}, child2 = {0}, child3 = {0};
1589 cx_tree_link(&root, &child1, tree_node_layout);
1590 cx_tree_link(&root, &child2, tree_node_layout);
1591 cx_tree_link(&child1, &child3, tree_node_layout);
1592 CX_TEST_DO {
1593 CxTree *tree = cxTreeCreateWrapped(&root, tree_node_layout);
1594 CX_TEST_ASSERT(tree->cl != NULL);
1595 CX_TEST_ASSERT(tree->allocator == cxDefaultAllocator);
1596 CX_TEST_ASSERT(tree->node_create == NULL);
1597 CX_TEST_ASSERT(tree->search == NULL);
1598 CX_TEST_ASSERT(tree->root == &root);
1599 CX_TEST_ASSERT(tree->size == 4);
1600 CX_TEST_ASSERT(tree->simple_destructor == NULL);
1601 CX_TEST_ASSERT(tree->advanced_destructor == NULL);
1602 CX_TEST_ASSERT(tree->destructor_data == NULL);
1603 CX_TEST_ASSERT(tree->loc_parent == offsetof(tree_node, parent));
1604 CX_TEST_ASSERT(tree->loc_children == offsetof(tree_node, children));
1605 CX_TEST_ASSERT(tree->loc_last_child == -1);
1606 CX_TEST_ASSERT(tree->loc_prev == offsetof(tree_node, prev));
1607 CX_TEST_ASSERT(tree->loc_next == offsetof(tree_node, next));
1608 cxTreeDestroy(tree);
1609 }
1610 }
1529 1611
1530 CxTestSuite *cx_test_suite_tree_low_level(void) { 1612 CxTestSuite *cx_test_suite_tree_low_level(void) {
1531 CxTestSuite *suite = cx_test_suite_new("tree (low level)"); 1613 CxTestSuite *suite = cx_test_suite_new("tree (low level)");
1532 1614
1533 cx_test_register(suite, test_tree_link_new_child); 1615 cx_test_register(suite, test_tree_link_new_child);
1560 cx_test_register(suite, test_tree_add_many_with_dupl_and_no_match); 1642 cx_test_register(suite, test_tree_add_many_with_dupl_and_no_match);
1561 cx_test_register(suite, test_tree_add_create_from_array); 1643 cx_test_register(suite, test_tree_add_create_from_array);
1562 1644
1563 return suite; 1645 return suite;
1564 } 1646 }
1647
1648 CxTestSuite *cx_test_suite_tree_high_level(void) {
1649 CxTestSuite *suite = cx_test_suite_new("tree (high level)");
1650
1651 cx_test_register(suite, test_tree_high_create);
1652 cx_test_register(suite, test_tree_high_create_simple);
1653 cx_test_register(suite, test_tree_high_create_wrapped);
1654
1655 return suite;
1656 }

mercurial