tests/test_tree.c

changeset 893
0a2b328f5b91
parent 890
54565fd74e74
child 895
ea1ac0e8225c
equal deleted inserted replaced
892:6ebf6fdfbc2c 893:0a2b328f5b91
1219 CX_TEST_ASSERT(talloc.alloc_total == 0); 1219 CX_TEST_ASSERT(talloc.alloc_total == 0);
1220 1220
1221 CxIterator iter = cxIterator(NULL, sizeof(void *), 0); 1221 CxIterator iter = cxIterator(NULL, sizeof(void *), 0);
1222 processed = cx_tree_add_iter( 1222 processed = cx_tree_add_iter(
1223 cxIteratorRef(iter), 1223 cxIteratorRef(iter),
1224 10, // deliberately specify more than the iter has
1224 tree_node_file_search, 1225 tree_node_file_search,
1225 create_tree_node_file, alloc, 1226 create_tree_node_file, alloc,
1226 &failed, &root, tree_node_file_layout 1227 &failed, &root, tree_node_file_layout
1227 ); 1228 );
1228 CX_TEST_ASSERT(processed == 0); 1229 CX_TEST_ASSERT(processed == 0);
1292 1293
1293 cxFree(alloc, foo); 1294 cxFree(alloc, foo);
1294 cxFree(alloc, bar); 1295 cxFree(alloc, bar);
1295 cxFree(alloc, lib64); 1296 cxFree(alloc, lib64);
1296 cxFree(alloc, libfoo); 1297 cxFree(alloc, libfoo);
1298
1299 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1300 }
1301 cx_testing_allocator_destroy(&talloc);
1302 }
1303
1304 CX_TEST(test_tree_add_many_but_not_all) {
1305 CxTestingAllocator talloc;
1306 cx_testing_allocator_init(&talloc);
1307 CxAllocator *alloc = &talloc.base;
1308
1309 tree_node_file root = {0};
1310 root.path = "/";
1311 tree_node_file usr = {0};
1312 usr.path = "/usr/";
1313 cx_tree_link(&root, &usr, tree_node_file_layout);
1314 tree_node_file home = {0};
1315 home.path = "/home/";
1316 cx_tree_link(&root, &home, tree_node_file_layout);
1317 tree_node_file lib = {0};
1318 lib.path = "/usr/lib/";
1319 cx_tree_link(&usr, &lib, tree_node_file_layout);
1320
1321 CX_TEST_DO {
1322 void *failed;
1323
1324 const char *paths[] = {
1325 "/home/foo/",
1326 "/home/foo/bar",
1327 "/usr/lib64/",
1328 "/usr/lib/foo.so"
1329 };
1330 // create iterator for 4 elements, but choose to insert only 3 of them
1331 CxIterator iter = cxIterator(paths, sizeof(const char*), 4);
1332 size_t processed = cx_tree_add_iter(
1333 cxIteratorRef(iter), 3,
1334 tree_node_file_search,
1335 create_tree_node_file, alloc,
1336 &failed, &root, tree_node_file_layout
1337 );
1338
1339 CX_TEST_ASSERT(cxIteratorValid(iter));
1340 CX_TEST_ASSERT(cxIteratorCurrent(iter) == &paths[3]);
1341
1342 CX_TEST_ASSERT(failed == NULL);
1343 CX_TEST_ASSERT(processed == 3);
1344 CX_TEST_ASSERT(talloc.alloc_total == 3);
1345
1346 CX_TEST_ASSERT(home.children == home.last_child);
1347 tree_node_file *foo = home.children;
1348 CX_TEST_ASSERT(foo != NULL && foo->path == paths[0]);
1349 CX_TEST_ASSERT(foo->children == foo->last_child);
1350 tree_node_file *bar = foo->children;
1351 CX_TEST_ASSERT(bar != NULL && bar->path == paths[1]);
1352 CX_TEST_ASSERT(usr.children != usr.last_child);
1353 tree_node_file *lib64 = usr.last_child;
1354 CX_TEST_ASSERT(lib64 != NULL);
1355 CX_TEST_ASSERT(lib64->path == paths[2]);
1356 CX_TEST_ASSERT(lib64->prev == &lib);
1357 CX_TEST_ASSERT(lib64->parent == &usr);
1358 CX_TEST_ASSERT(lib.children == NULL);
1359
1360 cxFree(alloc, foo);
1361 cxFree(alloc, bar);
1362 cxFree(alloc, lib64);
1297 1363
1298 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1364 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1299 } 1365 }
1300 cx_testing_allocator_destroy(&talloc); 1366 cx_testing_allocator_destroy(&talloc);
1301 } 1367 }
1492 cx_test_register(suite, test_tree_add_one_existing); 1558 cx_test_register(suite, test_tree_add_one_existing);
1493 cx_test_register(suite, test_tree_add_one_no_match); 1559 cx_test_register(suite, test_tree_add_one_no_match);
1494 cx_test_register(suite, test_tree_add_duplicate_root); 1560 cx_test_register(suite, test_tree_add_duplicate_root);
1495 cx_test_register(suite, test_tree_add_zero); 1561 cx_test_register(suite, test_tree_add_zero);
1496 cx_test_register(suite, test_tree_add_many); 1562 cx_test_register(suite, test_tree_add_many);
1563 cx_test_register(suite, test_tree_add_many_but_not_all);
1497 cx_test_register(suite, test_tree_add_many_with_dupl_and_no_match); 1564 cx_test_register(suite, test_tree_add_many_with_dupl_and_no_match);
1498 cx_test_register(suite, test_tree_add_create_from_array); 1565 cx_test_register(suite, test_tree_add_create_from_array);
1499 1566
1500 return suite; 1567 return suite;
1501 } 1568 }

mercurial