tests/test_tree.c

changeset 890
54565fd74e74
parent 871
e29c1f96646d
child 893
0a2b328f5b91
equal deleted inserted replaced
889:f549fd9fbd8f 890:54565fd74e74
53 struct tree_node_file *parent; 53 struct tree_node_file *parent;
54 struct tree_node_file *next; 54 struct tree_node_file *next;
55 struct tree_node_file *prev; 55 struct tree_node_file *prev;
56 struct tree_node_file *children; 56 struct tree_node_file *children;
57 struct tree_node_file *last_child; 57 struct tree_node_file *last_child;
58 char const *path; 58 const char *path;
59 } tree_node_file; 59 } tree_node_file;
60 60
61 static void *create_tree_node_file( 61 static void *create_tree_node_file(
62 void const *dptr, 62 const void *dptr,
63 void *allocator) { 63 void *allocator) {
64 if (allocator == NULL) allocator = cxDefaultAllocator; 64 if (allocator == NULL) allocator = cxDefaultAllocator;
65 65
66 tree_node_file *node = cxMalloc(allocator, sizeof(tree_node_file)); 66 tree_node_file *node = cxMalloc(allocator, sizeof(tree_node_file));
67 node->path = dptr; 67 node->path = dptr;
74 node->last_child = (void *) 0xf00ba5; 74 node->last_child = (void *) 0xf00ba5;
75 75
76 return node; 76 return node;
77 } 77 }
78 78
79 static int tree_node_file_search(void const *l, void const *r) { 79 static int tree_node_file_search(const void *l, const void *r) {
80 tree_node_file const *left = l; 80 const tree_node_file *left = l;
81 tree_node_file const *right = r; 81 const tree_node_file *right = r;
82 size_t len1 = strlen(left->path); 82 size_t len1 = strlen(left->path);
83 size_t len2 = strlen(right->path); 83 size_t len2 = strlen(right->path);
84 if (len1 <= len2) { 84 if (len1 <= len2) {
85 if (memcmp(left->path, right->path, len1) == 0) { 85 if (memcmp(left->path, right->path, len1) == 0) {
86 return (int) (len2 - len1); 86 return (int) (len2 - len1);
369 CX_TEST_ASSERT(parent.last_child == NULL); 369 CX_TEST_ASSERT(parent.last_child == NULL);
370 CX_TEST_ASSERT(child1.parent == NULL); 370 CX_TEST_ASSERT(child1.parent == NULL);
371 } 371 }
372 } 372 }
373 373
374 static int test_tree_search_function(void const *n, void const *d) { 374 static int test_tree_search_function(const void *n, const void *d) {
375 tree_node const *node = n; 375 const tree_node *node = n;
376 int data = *((int const*)d); 376 int data = *((const int *)d);
377 377
378 if (data < node->data) return -1; 378 if (data < node->data) return -1;
379 else if (data == node->data) return 0; 379 else if (data == node->data) return 0;
380 else return data - node->data; 380 else return data - node->data;
381 } 381 }
599 } 599 }
600 } 600 }
601 601
602 typedef struct test_xml_node { 602 typedef struct test_xml_node {
603 struct tree_node base; 603 struct tree_node base;
604 char const* name; 604 const char *name;
605 } test_xml_node; 605 } test_xml_node;
606 606
607 CX_TEST(test_tree_iterator_xml) { 607 CX_TEST(test_tree_iterator_xml) {
608 test_xml_node project = {0}; 608 test_xml_node project = {0};
609 test_xml_node config = {0}; 609 test_xml_node config = {0};
647 cx_tree_link(&dependency2, &dependency2make, tree_node_layout); 647 cx_tree_link(&dependency2, &dependency2make, tree_node_layout);
648 cx_tree_link(&target, &target_feature, tree_node_layout); 648 cx_tree_link(&target, &target_feature, tree_node_layout);
649 cx_tree_link(&target, &target_dependencies, tree_node_layout); 649 cx_tree_link(&target, &target_dependencies, tree_node_layout);
650 cx_tree_link(&target_feature, &target_feature_dependencies, tree_node_layout); 650 cx_tree_link(&target_feature, &target_feature_dependencies, tree_node_layout);
651 651
652 char const *expected = 652 const char *expected =
653 "<project><config><var></var><var></var><var></var></config>" 653 "<project><config><var></var><var></var><var></var></config>"
654 "<dependency><make></make></dependency><dependency><lang></lang><make></make></dependency>" 654 "<dependency><make></make></dependency><dependency><lang></lang><make></make></dependency>"
655 "<target><feature><dependencies></dependencies></feature><dependencies></dependencies></target></project>"; 655 "<target><feature><dependencies></dependencies></feature><dependencies></dependencies></target></project>";
656 char *actual = malloc(512); 656 char *actual = malloc(512);
657 CX_TEST_DO { 657 CX_TEST_DO {
1065 (void **) &foo, &root, 1065 (void **) &foo, &root,
1066 tree_node_file_layout 1066 tree_node_file_layout
1067 ); 1067 );
1068 CX_TEST_ASSERT(result == 0); 1068 CX_TEST_ASSERT(result == 0);
1069 CX_TEST_ASSERT(foo != NULL); 1069 CX_TEST_ASSERT(foo != NULL);
1070 char const *bar_path = "/home/foo/bar/"; 1070 const char *bar_path = "/home/foo/bar/";
1071 void *failed; 1071 void *failed;
1072 size_t added = cx_tree_add_array( 1072 size_t added = cx_tree_add_array(
1073 bar_path, 1, sizeof(char const *), 1073 bar_path, 1, sizeof(const char *),
1074 tree_node_file_search, 1074 tree_node_file_search,
1075 create_tree_node_file, alloc, 1075 create_tree_node_file, alloc,
1076 &failed, &root, 1076 &failed, &root,
1077 tree_node_file_layout 1077 tree_node_file_layout
1078 ); 1078 );
1164 CX_TEST_ASSERT(node->parent == NULL); 1164 CX_TEST_ASSERT(node->parent == NULL);
1165 CX_TEST_ASSERT(node->children == NULL); 1165 CX_TEST_ASSERT(node->children == NULL);
1166 free(node); 1166 free(node);
1167 node = NULL; 1167 node = NULL;
1168 size_t added = cx_tree_add_array( 1168 size_t added = cx_tree_add_array(
1169 "/", 1, sizeof(char const *), 1169 "/", 1, sizeof(const char *),
1170 tree_node_file_search, 1170 tree_node_file_search,
1171 create_tree_node_file, NULL, 1171 create_tree_node_file, NULL,
1172 (void **) &node, &root, 1172 (void **) &node, &root,
1173 tree_node_file_layout 1173 tree_node_file_layout
1174 ); 1174 );
1254 cx_tree_link(&usr, &lib, tree_node_file_layout); 1254 cx_tree_link(&usr, &lib, tree_node_file_layout);
1255 1255
1256 CX_TEST_DO { 1256 CX_TEST_DO {
1257 void *failed; 1257 void *failed;
1258 1258
1259 char const *paths[] = { 1259 const char *paths[] = {
1260 "/home/foo/", 1260 "/home/foo/",
1261 "/home/foo/bar", 1261 "/home/foo/bar",
1262 "/usr/lib64/", 1262 "/usr/lib64/",
1263 "/usr/lib/foo.so" 1263 "/usr/lib/foo.so"
1264 }; 1264 };
1265 1265
1266 size_t processed = cx_tree_add_array( 1266 size_t processed = cx_tree_add_array(
1267 paths, 4, sizeof(char const *), 1267 paths, 4, sizeof(const char *),
1268 tree_node_file_search, 1268 tree_node_file_search,
1269 create_tree_node_file, alloc, 1269 create_tree_node_file, alloc,
1270 &failed, &root, tree_node_file_layout 1270 &failed, &root, tree_node_file_layout
1271 ); 1271 );
1272 1272
1309 root.path = "/mnt/"; 1309 root.path = "/mnt/";
1310 1310
1311 CX_TEST_DO { 1311 CX_TEST_DO {
1312 tree_node_file *failed; 1312 tree_node_file *failed;
1313 1313
1314 char const *paths[] = { 1314 const char *paths[] = {
1315 "/mnt/sdcard/", 1315 "/mnt/sdcard/",
1316 "/mnt/foo/", 1316 "/mnt/foo/",
1317 "/mnt/sdcard/", 1317 "/mnt/sdcard/",
1318 "/home/", 1318 "/home/",
1319 "/usr/" 1319 "/usr/"
1320 }; 1320 };
1321 1321
1322 size_t processed = cx_tree_add_array( 1322 size_t processed = cx_tree_add_array(
1323 paths, 5, sizeof(char const *), 1323 paths, 5, sizeof(const char *),
1324 tree_node_file_search, 1324 tree_node_file_search,
1325 create_tree_node_file, alloc, 1325 create_tree_node_file, alloc,
1326 (void **) &failed, &root, tree_node_file_layout 1326 (void **) &failed, &root, tree_node_file_layout
1327 ); 1327 );
1328 1328
1356 } 1356 }
1357 cx_testing_allocator_destroy(&talloc); 1357 cx_testing_allocator_destroy(&talloc);
1358 } 1358 }
1359 1359
1360 static CX_TEST_SUBROUTINE(test_tree_add_create_from_array_impl, 1360 static CX_TEST_SUBROUTINE(test_tree_add_create_from_array_impl,
1361 CxAllocator *alloc, char const **paths) { 1361 CxAllocator *alloc, const char **paths) {
1362 tree_node_file root = {0}; 1362 tree_node_file root = {0};
1363 root.path = "/"; 1363 root.path = "/";
1364 1364
1365 void *failed; 1365 void *failed;
1366 size_t processed = cx_tree_add_array( 1366 size_t processed = cx_tree_add_array(
1367 paths, 10, sizeof(char const *), 1367 paths, 10, sizeof(const char *),
1368 tree_node_file_search, 1368 tree_node_file_search,
1369 create_tree_node_file, alloc, 1369 create_tree_node_file, alloc,
1370 &failed, &root, tree_node_file_layout 1370 &failed, &root, tree_node_file_layout
1371 ); 1371 );
1372 1372
1373 CX_TEST_ASSERT(failed == NULL); 1373 CX_TEST_ASSERT(failed == NULL);
1374 CX_TEST_ASSERT(processed == 10); 1374 CX_TEST_ASSERT(processed == 10);
1375 1375
1376 char const *exp_order[] = { 1376 const char *exp_order[] = {
1377 "/", 1377 "/",
1378 "/usr/", 1378 "/usr/",
1379 "/usr/lib/", 1379 "/usr/lib/",
1380 "/usr/lib/libbumm.so", 1380 "/usr/lib/libbumm.so",
1381 "/home/", 1381 "/home/",
1424 CxTestingAllocator talloc; 1424 CxTestingAllocator talloc;
1425 cx_testing_allocator_init(&talloc); 1425 cx_testing_allocator_init(&talloc);
1426 CxAllocator *alloc = &talloc.base; 1426 CxAllocator *alloc = &talloc.base;
1427 1427
1428 CX_TEST_DO { 1428 CX_TEST_DO {
1429 char const *paths[] = { 1429 const char *paths[] = {
1430 "/usr/", 1430 "/usr/",
1431 "/home/", 1431 "/home/",
1432 "/usr/lib/", 1432 "/usr/lib/",
1433 "/usr/lib/libbumm.so", 1433 "/usr/lib/libbumm.so",
1434 "/var/", 1434 "/var/",
1437 "/var/www/vhosts/live/", 1437 "/var/www/vhosts/live/",
1438 "/var/www/vhosts/live/htdocs/", 1438 "/var/www/vhosts/live/htdocs/",
1439 "/home/foo/" 1439 "/home/foo/"
1440 }; 1440 };
1441 1441
1442 char const *scrambled_paths[] = { 1442 const char *scrambled_paths[] = {
1443 "/usr/", 1443 "/usr/",
1444 "/home/", 1444 "/home/",
1445 "/var/www/vhosts/live/", 1445 "/var/www/vhosts/live/",
1446 "/usr/lib/", 1446 "/usr/lib/",
1447 "/var/", 1447 "/var/",

mercurial