tests/test_tree.c

branch
feature/tree_add
changeset 871
e29c1f96646d
parent 866
1f636de4a63f
child 890
54565fd74e74
equal deleted inserted replaced
870:af0092d8789a 871:e29c1f96646d
58 char const *path; 58 char const *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 void const *dptr,
63 void const *nptr,
64 void *allocator) { 63 void *allocator) {
65 if (allocator == NULL) allocator = cxDefaultAllocator; 64 if (allocator == NULL) allocator = cxDefaultAllocator;
66 65
67 char const *data = dptr; 66 tree_node_file *node = cxMalloc(allocator, sizeof(tree_node_file));
68 tree_node_file const *existing = nptr; 67 node->path = dptr;
69 68
70 if (existing != NULL && strcmp(existing->path, data) == 0) { 69 // intentionally write garbage into the pointers, it's part of the test
71 return (void *) existing; 70 node->parent = (void *) 0xf00ba1;
72 } else { 71 node->next = (void *) 0xf00ba2;
73 tree_node_file *node = cxMalloc(allocator, sizeof(tree_node_file)); 72 node->prev = (void *) 0xf00ba3;
74 73 node->children = (void *) 0xf00ba4;
75 node->path = data; 74 node->last_child = (void *) 0xf00ba5;
76 75
77 // intentionally write garbage into the pointers, it's part of the test 76 return node;
78 node->parent = (void *) 0xf00ba1; 77 }
79 node->next = (void *) 0xf00ba2; 78
80 node->prev = (void *) 0xf00ba3; 79 static int tree_node_file_search(void const *l, void const *r) {
81 node->children = (void *) 0xf00ba4; 80 tree_node_file const *left = l;
82 node->last_child = (void *) 0xf00ba5; 81 tree_node_file const *right = r;
83 82 size_t len1 = strlen(left->path);
84 return node; 83 size_t len2 = strlen(right->path);
85 }
86 }
87
88 static int tree_node_file_search(void const *nptr, void const *data) {
89 tree_node_file const *node = nptr;
90 size_t len1 = strlen(node->path);
91 size_t len2 = strlen(data);
92 if (len1 <= len2) { 84 if (len1 <= len2) {
93 if (memcmp(node->path, data, len1) == 0) { 85 if (memcmp(left->path, right->path, len1) == 0) {
94 return (int) (len2 - len1); 86 return (int) (len2 - len1);
95 } else { 87 } else {
96 return -1; 88 return -1;
97 } 89 }
98 } else { 90 } else {
427 int r; 419 int r;
428 tree_node *n; 420 tree_node *n;
429 CX_TEST_DO { 421 CX_TEST_DO {
430 for (unsigned i = 0 ; i <= 10 ; i++) { 422 for (unsigned i = 0 ; i <= 10 ; i++) {
431 s = testdata[i]; 423 s = testdata[i];
432 r = cx_tree_search(&root, &s, test_tree_search_function, 424 r = cx_tree_search_data(&root, &s, test_tree_search_function,
433 (void **) &n, tree_children(tree_node)); 425 (void **) &n, tree_children(tree_node));
434 CX_TEST_ASSERT(r == 0); 426 CX_TEST_ASSERT(r == 0);
435 CX_TEST_ASSERT(n == testnodes[i]); 427 CX_TEST_ASSERT(n == testnodes[i]);
436 } 428 }
437 429
438 s = -5; 430 s = -5;
439 r = cx_tree_search(&root, &s, test_tree_search_function, 431 r = cx_tree_search_data(&root, &s, test_tree_search_function,
440 (void **) &n, tree_children(tree_node)); 432 (void **) &n, tree_children(tree_node));
441 CX_TEST_ASSERT(r < 0); 433 CX_TEST_ASSERT(r < 0);
442 CX_TEST_ASSERT(n == NULL); 434 CX_TEST_ASSERT(n == NULL);
443 435
444 s = 26; 436 s = 26;
445 r = cx_tree_search(&root, &s, test_tree_search_function, 437 r = cx_tree_search_data(&root, &s, test_tree_search_function,
446 (void **) &n, tree_children(tree_node)); 438 (void **) &n, tree_children(tree_node));
447 CX_TEST_ASSERT(r > 0); 439 CX_TEST_ASSERT(r > 0);
448 CX_TEST_ASSERT(n == &ba); 440 CX_TEST_ASSERT(n == &ba);
449 441
450 s = 35; 442 s = 35;
451 r = cx_tree_search(&root, &s, test_tree_search_function, 443 r = cx_tree_search_data(&root, &s, test_tree_search_function,
452 (void **) &n, tree_children(tree_node)); 444 (void **) &n, tree_children(tree_node));
453 CX_TEST_ASSERT(r > 0); 445 CX_TEST_ASSERT(r > 0);
454 CX_TEST_ASSERT(n == &cb); 446 CX_TEST_ASSERT(n == &cb);
455 447
456 s = 38; 448 s = 38;
457 r = cx_tree_search(&root, &s, test_tree_search_function, 449 r = cx_tree_search_data(&root, &s, test_tree_search_function,
458 (void **) &n, tree_children(tree_node)); 450 (void **) &n, tree_children(tree_node));
459 CX_TEST_ASSERT(r > 0); 451 CX_TEST_ASSERT(r > 0);
460 CX_TEST_ASSERT(n == &cba); 452 CX_TEST_ASSERT(n == &cba);
461 453
462 s = 42; 454 s = 42;
463 r = cx_tree_search(&root, &s, test_tree_search_function, 455 r = cx_tree_search_data(&root, &s, test_tree_search_function,
464 (void **) &n, tree_children(tree_node)); 456 (void **) &n, tree_children(tree_node));
465 CX_TEST_ASSERT(r > 0); 457 CX_TEST_ASSERT(r > 0);
466 CX_TEST_ASSERT(n == &cc); 458 CX_TEST_ASSERT(n == &cc);
467 } 459 }
468 } 460 }
1062 tree_node_file lib = {0}; 1054 tree_node_file lib = {0};
1063 lib.path = "/usr/lib/"; 1055 lib.path = "/usr/lib/";
1064 cx_tree_link(&usr, &lib, tree_node_file_layout); 1056 cx_tree_link(&usr, &lib, tree_node_file_layout);
1065 1057
1066 CX_TEST_DO { 1058 CX_TEST_DO {
1067 void *newroot = &root; 1059 tree_node_file *foo;
1068 1060 int result;
1069 tree_node_file *foo = cx_tree_add( 1061 result = cx_tree_add(
1070 "/home/foo/", 1062 "/home/foo/",
1071 tree_node_file_search, 1063 tree_node_file_search,
1072 create_tree_node_file, alloc, 1064 create_tree_node_file, alloc,
1073 &newroot, tree_node_file_layout 1065 (void **) &foo, &root,
1066 tree_node_file_layout
1074 ); 1067 );
1075 CX_TEST_ASSERT(newroot == &root); 1068 CX_TEST_ASSERT(result == 0);
1076 tree_node_file *bar = cx_tree_add( 1069 CX_TEST_ASSERT(foo != NULL);
1077 "/home/foo/bar/", 1070 char const *bar_path = "/home/foo/bar/";
1071 void *failed;
1072 size_t added = cx_tree_add_array(
1073 bar_path, 1, sizeof(char const *),
1078 tree_node_file_search, 1074 tree_node_file_search,
1079 create_tree_node_file, alloc, 1075 create_tree_node_file, alloc,
1080 &newroot, tree_node_file_layout 1076 &failed, &root,
1077 tree_node_file_layout
1081 ); 1078 );
1082 CX_TEST_ASSERT(newroot == &root); 1079 CX_TEST_ASSERT(added == 1);
1083 1080 CX_TEST_ASSERT(failed == NULL);
1081 tree_node_file *bar = foo->children;
1082 CX_TEST_ASSERT(bar != NULL);
1084 CX_TEST_ASSERT(bar->parent == foo); 1083 CX_TEST_ASSERT(bar->parent == foo);
1084 CX_TEST_ASSERT(bar->path == bar_path);
1085 CX_TEST_ASSERT(foo->parent == &home); 1085 CX_TEST_ASSERT(foo->parent == &home);
1086 1086
1087 CX_TEST_ASSERT(talloc.alloc_total == 2); 1087 tree_node_file *new_node;
1088 1088 result = cx_tree_add(
1089 cxFree(&talloc.base, foo); 1089 "newroot",
1090 cxFree(&talloc.base, bar); 1090 tree_node_file_search,
1091 create_tree_node_file, alloc,
1092 (void **) &new_node, &root,
1093 tree_node_file_layout
1094 );
1095 CX_TEST_ASSERT(0 != result);
1096 CX_TEST_ASSERT(NULL != new_node);
1097 CX_TEST_ASSERT(new_node->children == NULL);
1098 CX_TEST_ASSERT(new_node->parent == NULL);
1099
1100 CX_TEST_ASSERT(talloc.alloc_total == 3);
1101
1102 cxFree(alloc, foo);
1103 cxFree(alloc, bar);
1104 cxFree(alloc, new_node);
1091 1105
1092 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1106 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1093 } 1107 }
1094 cx_testing_allocator_destroy(&talloc); 1108 cx_testing_allocator_destroy(&talloc);
1095 }
1096
1097 CX_TEST(test_tree_add_one_to_empty_tree) {
1098 tree_node_file *node = NULL;
1099 CX_TEST_DO {
1100 tree_node_file *root = NULL;
1101 char const *data = "/home/foo/bar/";
1102
1103 node = cx_tree_add(
1104 data,
1105 tree_node_file_search,
1106 create_tree_node_file, NULL,
1107 (void **) &root, tree_node_file_layout
1108 );
1109
1110 CX_TEST_ASSERT(root != NULL);
1111 CX_TEST_ASSERT(root->path == data);
1112 CX_TEST_ASSERT(root->next == NULL);
1113 CX_TEST_ASSERT(root->prev == NULL);
1114 CX_TEST_ASSERT(root->children == NULL);
1115 CX_TEST_ASSERT(root->last_child == NULL);
1116 CX_TEST_ASSERT(root->parent == NULL);
1117 }
1118 free(node);
1119 } 1109 }
1120 1110
1121 CX_TEST(test_tree_add_one_existing) { 1111 CX_TEST(test_tree_add_one_existing) {
1122 CxTestingAllocator talloc; 1112 CxTestingAllocator talloc;
1123 cx_testing_allocator_init(&talloc); 1113 cx_testing_allocator_init(&talloc);
1134 tree_node_file lib = {0}; 1124 tree_node_file lib = {0};
1135 lib.path = "/usr/lib/"; 1125 lib.path = "/usr/lib/";
1136 cx_tree_link(&usr, &lib, tree_node_file_layout); 1126 cx_tree_link(&usr, &lib, tree_node_file_layout);
1137 1127
1138 CX_TEST_DO { 1128 CX_TEST_DO {
1139 void *newroot = &root;
1140
1141 tree_node_file *node; 1129 tree_node_file *node;
1142 node = cx_tree_add( 1130 int result = cx_tree_add(
1143 "/usr/lib/", 1131 "/usr/lib/",
1144 tree_node_file_search, 1132 tree_node_file_search,
1145 create_tree_node_file, alloc, 1133 create_tree_node_file, alloc,
1146 &newroot, tree_node_file_layout 1134 (void **) &node, &root,
1135 tree_node_file_layout
1147 ); 1136 );
1148 CX_TEST_ASSERT(newroot == &root); 1137 CX_TEST_ASSERT(result == 0);
1149 CX_TEST_ASSERT(node == &lib); 1138 CX_TEST_ASSERT(node != &lib);
1150 1139 CX_TEST_ASSERT(node->prev == &lib);
1151 node = cx_tree_add( 1140 CX_TEST_ASSERT(lib.next == node);
1152 "/home/", 1141 CX_TEST_ASSERT(node->parent == &usr);
1142 CX_TEST_ASSERT(talloc.alloc_total == 1);
1143 cxFree(alloc, node);
1144 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1145 }
1146 cx_testing_allocator_destroy(&talloc);
1147 }
1148
1149 CX_TEST(test_tree_add_one_no_match) {
1150 tree_node_file root = {0};
1151 root.path = "/mnt/";
1152
1153 CX_TEST_DO {
1154 tree_node_file *node = NULL;
1155 int result = cx_tree_add(
1156 "/usr/lib/",
1157 tree_node_file_search,
1158 create_tree_node_file, NULL,
1159 (void **) &node, &root,
1160 tree_node_file_layout
1161 );
1162 CX_TEST_ASSERT(result != 0);
1163 CX_TEST_ASSERT(node != NULL);
1164 CX_TEST_ASSERT(node->parent == NULL);
1165 CX_TEST_ASSERT(node->children == NULL);
1166 free(node);
1167 node = NULL;
1168 size_t added = cx_tree_add_array(
1169 "/", 1, sizeof(char const *),
1170 tree_node_file_search,
1171 create_tree_node_file, NULL,
1172 (void **) &node, &root,
1173 tree_node_file_layout
1174 );
1175 CX_TEST_ASSERT(added == 0);
1176 CX_TEST_ASSERT(node != NULL);
1177 CX_TEST_ASSERT(node->parent == NULL);
1178 CX_TEST_ASSERT(node->children == NULL);
1179 free(node);
1180 }
1181 }
1182
1183 CX_TEST(test_tree_add_duplicate_root) {
1184 tree_node_file root = {0};
1185 root.path = "/";
1186 CX_TEST_DO {
1187 tree_node_file *node;
1188 int result = cx_tree_add(
1189 "/",
1190 tree_node_file_search,
1191 create_tree_node_file, NULL,
1192 (void **) &node, &root,
1193 tree_node_file_layout
1194 );
1195 CX_TEST_ASSERT(result == 0);
1196 CX_TEST_ASSERT(root.children == node);
1197 CX_TEST_ASSERT(node->parent == &root);
1198 }
1199 }
1200
1201 CX_TEST(test_tree_add_zero) {
1202 CxTestingAllocator talloc;
1203 cx_testing_allocator_init(&talloc);
1204 CxAllocator *alloc = &talloc.base;
1205
1206 tree_node_file root = {0};
1207 root.path = "/";
1208 CX_TEST_DO {
1209 void *failed;
1210
1211 size_t processed = cx_tree_add_array(
1212 (void *) 0xc0ffee, 0, sizeof(void *),
1153 tree_node_file_search, 1213 tree_node_file_search,
1154 create_tree_node_file, alloc, 1214 create_tree_node_file, alloc,
1155 &newroot, tree_node_file_layout 1215 &failed, &root, tree_node_file_layout
1156 ); 1216 );
1157 CX_TEST_ASSERT(newroot == &root); 1217 CX_TEST_ASSERT(failed == NULL);
1158 CX_TEST_ASSERT(node == &home); 1218 CX_TEST_ASSERT(processed == 0);
1159
1160 CX_TEST_ASSERT(talloc.alloc_total == 0); 1219 CX_TEST_ASSERT(talloc.alloc_total == 0);
1220
1221 CxIterator iter = cxIterator(NULL, sizeof(void *), 0);
1222 processed = cx_tree_add_iter(
1223 cxIteratorRef(iter),
1224 tree_node_file_search,
1225 create_tree_node_file, alloc,
1226 &failed, &root, tree_node_file_layout
1227 );
1228 CX_TEST_ASSERT(processed == 0);
1229 CX_TEST_ASSERT(failed == NULL);
1230 CX_TEST_ASSERT(talloc.alloc_total == 0);
1231
1161 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1232 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1162 } 1233 }
1163 cx_testing_allocator_destroy(&talloc); 1234 cx_testing_allocator_destroy(&talloc);
1164 } 1235 }
1165 1236
1181 tree_node_file lib = {0}; 1252 tree_node_file lib = {0};
1182 lib.path = "/usr/lib/"; 1253 lib.path = "/usr/lib/";
1183 cx_tree_link(&usr, &lib, tree_node_file_layout); 1254 cx_tree_link(&usr, &lib, tree_node_file_layout);
1184 1255
1185 CX_TEST_DO { 1256 CX_TEST_DO {
1186 void *newroot = &root; 1257 void *failed;
1187 1258
1188 char const *paths[] = { 1259 char const *paths[] = {
1189 "/home/foo/", 1260 "/home/foo/",
1190 "/home/foo/bar", 1261 "/home/foo/bar",
1191 "/usr/lib64/", 1262 "/usr/lib64/",
1194 1265
1195 size_t processed = cx_tree_add_array( 1266 size_t processed = cx_tree_add_array(
1196 paths, 4, sizeof(char const *), 1267 paths, 4, sizeof(char const *),
1197 tree_node_file_search, 1268 tree_node_file_search,
1198 create_tree_node_file, alloc, 1269 create_tree_node_file, alloc,
1199 &newroot, tree_node_file_layout 1270 &failed, &root, tree_node_file_layout
1200 ); 1271 );
1201 CX_TEST_ASSERT(newroot == &root); 1272
1202 1273 CX_TEST_ASSERT(failed == NULL);
1203 CX_TEST_ASSERT(processed == 4); 1274 CX_TEST_ASSERT(processed == 4);
1204 CX_TEST_ASSERT(talloc.alloc_total == 4); 1275 CX_TEST_ASSERT(talloc.alloc_total == 4);
1205 1276
1206 CX_TEST_ASSERT(home.children == home.last_child); 1277 CX_TEST_ASSERT(home.children == home.last_child);
1207 tree_node_file *foo = home.children; 1278 tree_node_file *foo = home.children;
1217 CX_TEST_ASSERT(lib64->parent == &usr); 1288 CX_TEST_ASSERT(lib64->parent == &usr);
1218 CX_TEST_ASSERT(lib.children != NULL); 1289 CX_TEST_ASSERT(lib.children != NULL);
1219 tree_node_file *libfoo = lib.children; 1290 tree_node_file *libfoo = lib.children;
1220 CX_TEST_ASSERT(libfoo != NULL && libfoo->path == paths[3]); 1291 CX_TEST_ASSERT(libfoo != NULL && libfoo->path == paths[3]);
1221 1292
1222 cxFree(&talloc.base, foo); 1293 cxFree(alloc, foo);
1223 cxFree(&talloc.base, bar); 1294 cxFree(alloc, bar);
1224 cxFree(&talloc.base, lib64); 1295 cxFree(alloc, lib64);
1225 cxFree(&talloc.base, libfoo); 1296 cxFree(alloc, libfoo);
1226 1297
1227 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1298 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1228 } 1299 }
1229 cx_testing_allocator_destroy(&talloc); 1300 cx_testing_allocator_destroy(&talloc);
1230 } 1301 }
1231 1302
1232 CX_TEST(test_tree_add_many_skip_duplicates) { 1303 CX_TEST(test_tree_add_many_with_dupl_and_no_match) {
1233 // adds many elements to an existing tree
1234 CxTestingAllocator talloc; 1304 CxTestingAllocator talloc;
1235 cx_testing_allocator_init(&talloc); 1305 cx_testing_allocator_init(&talloc);
1236 CxAllocator *alloc = &talloc.base; 1306 CxAllocator *alloc = &talloc.base;
1237 1307
1238 tree_node_file root = {0}; 1308 tree_node_file root = {0};
1239 root.path = "/"; 1309 root.path = "/mnt/";
1240 tree_node_file usr = {0}; 1310
1241 usr.path = "/usr/"; 1311 CX_TEST_DO {
1242 cx_tree_link(&root, &usr, tree_node_file_layout); 1312 tree_node_file *failed;
1243 tree_node_file home = {0};
1244 home.path = "/home/";
1245 cx_tree_link(&root, &home, tree_node_file_layout);
1246 tree_node_file lib = {0};
1247 lib.path = "/usr/lib/";
1248 cx_tree_link(&usr, &lib, tree_node_file_layout);
1249
1250 CX_TEST_DO {
1251 void *newroot = &root;
1252 1313
1253 char const *paths[] = { 1314 char const *paths[] = {
1254 "/home/foo/", 1315 "/mnt/sdcard/",
1255 "/home/foo/bar", 1316 "/mnt/foo/",
1256 "/usr/lib/", 1317 "/mnt/sdcard/",
1257 "/usr/lib/foo.so" 1318 "/home/",
1319 "/usr/"
1258 }; 1320 };
1259 1321
1260 size_t processed = cx_tree_add_array( 1322 size_t processed = cx_tree_add_array(
1261 paths, 4, sizeof(char const *), 1323 paths, 5, sizeof(char const *),
1262 tree_node_file_search, 1324 tree_node_file_search,
1263 create_tree_node_file, alloc, 1325 create_tree_node_file, alloc,
1264 &newroot, tree_node_file_layout 1326 (void **) &failed, &root, tree_node_file_layout
1265 ); 1327 );
1266 CX_TEST_ASSERT(newroot == &root); 1328
1267 1329 CX_TEST_ASSERT(processed == 3);
1268 CX_TEST_ASSERT(processed == 4); 1330 CX_TEST_ASSERT(failed != NULL);
1269 CX_TEST_ASSERT(talloc.alloc_total == 3); 1331 CX_TEST_ASSERT(failed->parent == NULL);
1270 1332 CX_TEST_ASSERT(failed->children == NULL);
1271 CX_TEST_ASSERT(home.children == home.last_child); 1333 CX_TEST_ASSERT(strcmp(failed->path, "/home/") == 0);
1272 tree_node_file *foo = home.children; 1334 cxFree(alloc, failed);
1273 CX_TEST_ASSERT(foo != NULL && foo->path == paths[0]); 1335
1274 CX_TEST_ASSERT(foo->children == foo->last_child); 1336 CX_TEST_ASSERT(root.children != root.last_child);
1275 tree_node_file *bar = foo->children; 1337 CX_TEST_ASSERT(strcmp(root.children->path, "/mnt/sdcard/") == 0);
1276 CX_TEST_ASSERT(bar != NULL && bar->path == paths[1]); 1338 CX_TEST_ASSERT(strcmp(root.last_child->path, "/mnt/sdcard/") == 0);
1277 CX_TEST_ASSERT(usr.children == usr.last_child); 1339 CX_TEST_ASSERT(strcmp(root.children->next->path, "/mnt/foo/") == 0);
1278 CX_TEST_ASSERT(usr.children == &lib); 1340 CX_TEST_ASSERT(strcmp(root.last_child->prev->path, "/mnt/foo/") == 0);
1279 CX_TEST_ASSERT(lib.children != NULL); 1341
1280 tree_node_file *libfoo = lib.children; 1342 CxTreeIterator iter = cx_tree_iterator(
1281 CX_TEST_ASSERT(libfoo != NULL && libfoo->path == paths[3]); 1343 &root, true,
1282 1344 offsetof(tree_node_file, children),
1283 cxFree(&talloc.base, foo); 1345 offsetof(tree_node_file, next)
1284 cxFree(&talloc.base, bar); 1346 );
1285 cxFree(&talloc.base, libfoo); 1347 cx_foreach(tree_node_file *, node, iter) {
1348 if (iter.exiting) {
1349 if (node != &root) {
1350 cxFree(alloc, node);
1351 }
1352 }
1353 }
1286 1354
1287 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1355 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1288 } 1356 }
1289 cx_testing_allocator_destroy(&talloc); 1357 cx_testing_allocator_destroy(&talloc);
1358 }
1359
1360 static CX_TEST_SUBROUTINE(test_tree_add_create_from_array_impl,
1361 CxAllocator *alloc, char const **paths) {
1362 tree_node_file root = {0};
1363 root.path = "/";
1364
1365 void *failed;
1366 size_t processed = cx_tree_add_array(
1367 paths, 10, sizeof(char const *),
1368 tree_node_file_search,
1369 create_tree_node_file, alloc,
1370 &failed, &root, tree_node_file_layout
1371 );
1372
1373 CX_TEST_ASSERT(failed == NULL);
1374 CX_TEST_ASSERT(processed == 10);
1375
1376 char const *exp_order[] = {
1377 "/",
1378 "/usr/",
1379 "/usr/lib/",
1380 "/usr/lib/libbumm.so",
1381 "/home/",
1382 "/home/foo/",
1383 "/var/",
1384 "/var/www/",
1385 "/var/www/vhosts/",
1386 "/var/www/vhosts/live/",
1387 "/var/www/vhosts/live/htdocs/"
1388 };
1389 unsigned exp_depth[] = {
1390 1,
1391 2,
1392 3,
1393 4,
1394 2,
1395 3,
1396 2,
1397 3,
1398 4,
1399 5,
1400 6
1401 };
1402
1403 CxTreeIterator iter = cx_tree_iterator(
1404 &root, true,
1405 offsetof(tree_node_file, children),
1406 offsetof(tree_node_file, next)
1407 );
1408 cx_foreach(tree_node_file *, node, iter) {
1409 if (iter.exiting) {
1410 if (node != &root) {
1411 cxFree(alloc, node);
1412 }
1413 } else {
1414 CX_TEST_ASSERT(iter.counter <= 11);
1415 CX_TEST_ASSERT(iter.depth == exp_depth[iter.counter - 1]);
1416 CX_TEST_ASSERT(strcmp(node->path, exp_order[iter.counter - 1]) == 0);
1417 }
1418 }
1290 } 1419 }
1291 1420
1292 CX_TEST(test_tree_add_create_from_array) { 1421 CX_TEST(test_tree_add_create_from_array) {
1293 // creates an entirely new tree from an array 1422 // creates an entirely new tree from an array
1294 CX_TEST_DO { 1423
1295 1424 CxTestingAllocator talloc;
1296 } 1425 cx_testing_allocator_init(&talloc);
1426 CxAllocator *alloc = &talloc.base;
1427
1428 CX_TEST_DO {
1429 char const *paths[] = {
1430 "/usr/",
1431 "/home/",
1432 "/usr/lib/",
1433 "/usr/lib/libbumm.so",
1434 "/var/",
1435 "/var/www/",
1436 "/var/www/vhosts/",
1437 "/var/www/vhosts/live/",
1438 "/var/www/vhosts/live/htdocs/",
1439 "/home/foo/"
1440 };
1441
1442 char const *scrambled_paths[] = {
1443 "/usr/",
1444 "/home/",
1445 "/var/www/vhosts/live/",
1446 "/usr/lib/",
1447 "/var/",
1448 "/var/www/",
1449 "/usr/lib/libbumm.so",
1450 "/var/www/vhosts/",
1451 "/var/www/vhosts/live/htdocs/",
1452 "/home/foo/"
1453 };
1454
1455 // no matter how the original array is sorted,
1456 // the resulting tree should be the same
1457 CX_TEST_CALL_SUBROUTINE(test_tree_add_create_from_array_impl, alloc,
1458 paths);
1459 CX_TEST_CALL_SUBROUTINE(test_tree_add_create_from_array_impl, alloc,
1460 scrambled_paths);
1461
1462 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1463 }
1464 cx_testing_allocator_destroy(&talloc);
1297 } 1465 }
1298 1466
1299 1467
1300 CxTestSuite *cx_test_suite_tree_low_level(void) { 1468 CxTestSuite *cx_test_suite_tree_low_level(void) {
1301 CxTestSuite *suite = cx_test_suite_new("tree (low level)"); 1469 CxTestSuite *suite = cx_test_suite_new("tree (low level)");
1320 cx_test_register(suite, test_tree_visitor_continue); 1488 cx_test_register(suite, test_tree_visitor_continue);
1321 cx_test_register(suite, test_tree_iterator_continue); 1489 cx_test_register(suite, test_tree_iterator_continue);
1322 cx_test_register(suite, test_tree_iterator_continue_with_exit); 1490 cx_test_register(suite, test_tree_iterator_continue_with_exit);
1323 cx_test_register(suite, test_tree_add_one); 1491 cx_test_register(suite, test_tree_add_one);
1324 cx_test_register(suite, test_tree_add_one_existing); 1492 cx_test_register(suite, test_tree_add_one_existing);
1325 cx_test_register(suite, test_tree_add_one_to_empty_tree); 1493 cx_test_register(suite, test_tree_add_one_no_match);
1494 cx_test_register(suite, test_tree_add_duplicate_root);
1495 cx_test_register(suite, test_tree_add_zero);
1326 cx_test_register(suite, test_tree_add_many); 1496 cx_test_register(suite, test_tree_add_many);
1327 cx_test_register(suite, test_tree_add_many_skip_duplicates); 1497 cx_test_register(suite, test_tree_add_many_with_dupl_and_no_match);
1328 cx_test_register(suite, test_tree_add_create_from_array); 1498 cx_test_register(suite, test_tree_add_create_from_array);
1329 1499
1330 return suite; 1500 return suite;
1331 } 1501 }

mercurial