Branch data Line data Source code
1 : : // Copyright 2020-2022 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : /*
5 : : Tests that compare results with std::filesystem::path. Also serves as a
6 : : handy record of divergence between zix, the C++ standard library, and
7 : : different implementations of it. The inconsistencies are confined to
8 : : Windows.
9 : : */
10 : :
11 : : #undef NDEBUG
12 : :
13 : : #include <zix/path.h>
14 : : #include <zix/string_view.h>
15 : :
16 : : #include <cassert>
17 : : #include <cstdlib>
18 : : #include <filesystem>
19 : : #include <string>
20 : :
21 : : // IWYU pragma: no_include <version>
22 : :
23 : : namespace {
24 : :
25 : : struct BinaryCase {
26 : : const char* lhs;
27 : : const char* rhs;
28 : : };
29 : :
30 : : const BinaryCase joins[] = {
31 : : {"", ""}, {"", "/b/"}, {"", "b"}, {"/", ""},
32 : : {"..", ".."}, {"..", "name"}, {"..", "/"}, {"/", ".."},
33 : : {"/", nullptr}, {"//host", ""}, {"//host", "a"}, {"//host/", "a"},
34 : : {"/a", ""}, {"/a", "/b"}, {"/a", "/b/"}, {"/a", "b"},
35 : : {"/a", "b/"}, {"/a", nullptr}, {"/a/", ""}, {"/a/", "b"},
36 : : {"/a/", "b/"}, {"/a/", nullptr}, {"/a/b", nullptr}, {"/a/b/", ""},
37 : : {"/a/b/", nullptr}, {"/a/c", "b"}, {"/a/c/", "/b/d"}, {"/a/c/", "b"},
38 : : {"C:", ""}, {"C:/a", "/b"}, {"C:/a", "C:/b"}, {"C:/a", "C:b"},
39 : : {"C:/a", "D:b"}, {"C:/a", "b"}, {"C:/a", "b/"}, {"C:/a", "c:/b"},
40 : : {"C:/a", "c:b"}, {"C:\\a", "b"}, {"C:\\a", "b\\"}, {"C:a", "/b"},
41 : : {"C:a", "C:/b"}, {"C:a", "C:\\b"}, {"C:a", "C:b"}, {"C:a", "b"},
42 : : {"C:a", "c:/b"}, {"C:a", "c:\\b"}, {"C:a", "c:b"}, {"a", ""},
43 : : {"a", "/b"}, {"a", "C:"}, {"a", "C:/b"}, {"a", "C:\\b"},
44 : : {"a", "\\b"}, {"a", "b"}, {"a", "b/"}, {"a", nullptr},
45 : : {"a/", ""}, {"a/", "/b"}, {"a/", "b"}, {"a/", "b/"},
46 : : {"a/", nullptr}, {"a/b", ""}, {"a/b", nullptr}, {"a/b/", ""},
47 : : {"a/b/", nullptr}, {"a\\", "\\b"}, {"a\\", "b"}, {nullptr, "/b"},
48 : : {nullptr, "/b/c"}, {nullptr, "b"}, {nullptr, "b/c"}, {nullptr, nullptr},
49 : : };
50 : :
51 : : const BinaryCase lexical_relatives[] = {
52 : : {"", ""},
53 : : {"", "."},
54 : : {".", "."},
55 : : {"../", "../"},
56 : : {"../", "./"},
57 : : {"../", "a"},
58 : : {"../../a", "../b"},
59 : : {"../a", "../b"},
60 : : {"/", "/a/b"},
61 : : {"/", "/a/b/c"},
62 : : {"/", "a"},
63 : : {"/", "a/b"},
64 : : {"/", "a/b/c"},
65 : : {"//host", "//host"},
66 : : {"//host/", "//host/"},
67 : : {"//host/a/b", "//host/a/b"},
68 : : {"/a", "/b/c/"},
69 : : {"/a/", "/a/b"},
70 : : {"/a/", "/b/c"},
71 : : {"/a/", "/b/c/"},
72 : : {"/a/", "b"},
73 : : {"/a/", "b/c/"},
74 : : {"/a/b", "a/b"},
75 : : {"/a/b/c", "/a/b/d/"},
76 : : {"/a/b/c", "/a/b/d/e/"},
77 : : {"/a/b/c", "/a/d"},
78 : : {"C:", "C:a.txt"},
79 : : {"C:", "D:a.txt"},
80 : : {"C:../", "C:../"},
81 : : {"C:../", "C:./"},
82 : : {"C:../", "C:a"},
83 : : {"C:../../a", "C:../b"},
84 : : {"C:../a", "C:../b"},
85 : : {"C:/", "C:a.txt"},
86 : : {"C:/", "D:a.txt"},
87 : : {"C:/D/", "C:F"},
88 : : {"C:/D/", "C:F.txt"},
89 : : {"C:/D/S/", "F"},
90 : : {"C:/D/S/", "F.txt"},
91 : : {"C:/Dir/", "C:/Dir/File.txt"},
92 : : {"C:/Dir/", "C:File.txt"},
93 : : {"C:/Dir/File.txt", "C:/Dir/Sub/"},
94 : : {"C:/Dir/File.txt", "C:/Other.txt"},
95 : : {"C:/Dir/Sub/", "File.txt"},
96 : : {"C:/a.txt", "C:/b/c.txt"},
97 : : {"C:/a/", "C:/a/b.txt"},
98 : : {"C:/a/", "C:b.txt"},
99 : : {"C:/a/b.txt", "C:/a/b/"},
100 : : {"C:/a/b.txt", "C:/d.txt"},
101 : : {"C:/a/b/", "d.txt"},
102 : : {"C:/b/", "C:a.txt"},
103 : : {"C:/b/c/", "a.txt"},
104 : : {"C:F", "D:G"},
105 : : {"C:File.txt", "D:Other.txt"},
106 : : {"C:a.txt", "C:b.txt"},
107 : : {"C:a.txt", "D:"},
108 : : {"C:a.txt", "D:/"},
109 : : {"C:a.txt", "D:e.txt"},
110 : : {"C:a/b/c", "C:../"},
111 : : {"C:a/b/c", "C:../../"},
112 : : {"a", "a"},
113 : : {"a", "a/b/c"},
114 : : {"a", "b/c"},
115 : : {"a/b", "a/b"},
116 : : {"a/b", "c/d"},
117 : : {"a/b/c", "../"},
118 : : {"a/b/c", "../../"},
119 : : {"a/b/c", "a/b/c"},
120 : : {"a/b/c", "a/b/c/x/y"},
121 : : {"a/b/c/", "a/b/c/"},
122 : :
123 : : // Network paths that aren't supported by MinGW
124 : : #if !(defined(_WIN32) && defined(__GLIBCXX__))
125 : : {"//host", "//host/"},
126 : : {"//host", "a"},
127 : : {"//host", "a"},
128 : : {"//host/", "a"},
129 : : {"//host/", "a"},
130 : : #endif
131 : : };
132 : :
133 : : const char* const paths[] = {
134 : : // Valid paths handled consistently on all platforms
135 : : "",
136 : : ".",
137 : : "..",
138 : : "../",
139 : : "../..",
140 : : "../../",
141 : : "../../a",
142 : : "../a",
143 : : "../name",
144 : : "..\\..\\a",
145 : : "..\\a",
146 : : "..name",
147 : : "./",
148 : : "./.",
149 : : "./..",
150 : : ".//a//.//./b/.//",
151 : : "./a/././b/./",
152 : : "./name",
153 : : ".hidden",
154 : : ".hidden.txt",
155 : : "/",
156 : : "/.",
157 : : "/..",
158 : : "/../",
159 : : "/../..",
160 : : "/../../",
161 : : "/../a",
162 : : "/../a/../..",
163 : : "//",
164 : : "///",
165 : : "///dir/",
166 : : "///dir///",
167 : : "///dir///name",
168 : : "///dir///sub/////",
169 : : "///name",
170 : : "/a",
171 : : "/a/",
172 : : "/a/b",
173 : : "/a/b/",
174 : : "/a\\",
175 : : "/a\\b",
176 : : "/a\\b/",
177 : : "/dir/",
178 : : "/dir/.",
179 : : "/dir/..",
180 : : "/dir/../..",
181 : : "/dir/.hidden",
182 : : "/dir//name",
183 : : "/dir//sub/suub/",
184 : : "/dir/name",
185 : : "/dir/name.tar.gz",
186 : : "/dir/name.txt",
187 : : "/dir/name\\",
188 : : "/dir/sub/",
189 : : "/dir/sub/./",
190 : : "/dir/sub//",
191 : : "/dir/sub///",
192 : : "/dir/sub//name",
193 : : "/dir/sub/name",
194 : : "/dir/sub/suub/",
195 : : "/dir/sub/suub/../",
196 : : "/dir/sub/suub/../d",
197 : : "/dir/sub/suub/../d/",
198 : : "/dir/sub/suub/suuub/",
199 : : "/dir/sub\\name",
200 : : "/name",
201 : : "/name.",
202 : : "/name.txt",
203 : : "/name.txt.",
204 : : "C:",
205 : : "C:.",
206 : : "C:..",
207 : : "C:/",
208 : : "C:/.",
209 : : "C:/..",
210 : : "C:/../../name",
211 : : "C:/../name",
212 : : "C:/..dir/..name",
213 : : "C:/..name",
214 : : "C:/./name",
215 : : "C:/.dir/../name",
216 : : "C:/.dir/.hidden",
217 : : "C:/.hidden",
218 : : "C:/dir/",
219 : : "C:/dir/.",
220 : : "C:/dir/..",
221 : : "C:/dir/name",
222 : : "C:/dir/sub/",
223 : : "C:/dir/sub/name",
224 : : "C:/dir/sub/suub/",
225 : : "C:/dir/sub/suub/suuub/",
226 : : "C:/name",
227 : : "C:/name.",
228 : : "C:/name.txt",
229 : : "C:/name\\horror",
230 : : "C:/name\\horror/",
231 : : "C:\\",
232 : : "C:\\a",
233 : : "C:\\a/",
234 : : "C:\\a/b",
235 : : "C:\\a/b\\",
236 : : "C:\\a\\",
237 : : "C:\\a\\b",
238 : : "C:\\a\\b\\",
239 : : "C:\\a\\b\\c",
240 : : "C:\\a\\b\\d\\",
241 : : "C:\\a\\b\\d\\e\\",
242 : : "C:\\b",
243 : : "C:\\b\\c\\",
244 : : "C:a",
245 : : "C:dir/",
246 : : "C:dir/name",
247 : : "C:dir\\",
248 : : "C:name",
249 : : "C:name/",
250 : : "C:name\\horror",
251 : : "D|\\dir\\name",
252 : : "D|\\name",
253 : : "D|name",
254 : : "Z:/a/b",
255 : : "Z:\\a\\b",
256 : : "Z:b",
257 : : "\\",
258 : : "\\a\\/b\\/c\\",
259 : : "\\a\\b\\c\\",
260 : : "\\b",
261 : : "a",
262 : : "a.",
263 : : "a..txt",
264 : : "a.txt",
265 : : "a/b",
266 : : "a/b\\",
267 : : "c:/name",
268 : : "c:\\name",
269 : : "c:name",
270 : : "dir/",
271 : : "dir/.",
272 : : "dir/..",
273 : : "dir/../",
274 : : "dir/../b",
275 : : "dir/../b/../..///",
276 : : "dir/../b/..//..///../",
277 : : "dir/../sub/../name",
278 : : "dir/./",
279 : : "dir/.///b/../",
280 : : "dir/./b",
281 : : "dir/./b/.",
282 : : "dir/./b/..",
283 : : "dir/./sub/./name",
284 : : "dir///b",
285 : : "dir//b",
286 : : "dir/\\b",
287 : : "dir/b",
288 : : "dir/b.",
289 : : "dir/name",
290 : : "dir/name.",
291 : : "dir/name.tar.gz",
292 : : "dir/name.txt",
293 : : "dir/name.txt.",
294 : : "dir/name\\with\\backslashes",
295 : : "dir/sub/",
296 : : "dir/sub/.",
297 : : "dir/sub/..",
298 : : "dir/sub/../",
299 : : "dir/sub/../..",
300 : : "dir/sub/../../",
301 : : "dir/sub/../name",
302 : : "dir/sub/./",
303 : : "dir/sub/./..",
304 : : "dir/sub/./../",
305 : : "dir/sub/./name",
306 : : "dir/sub//",
307 : : "dir/sub///",
308 : : "dir/sub/name",
309 : : "dir/sub/suub/../..",
310 : : "dir/sub/suub/../../",
311 : : "dir/weird<sub/weird%name",
312 : : "dir\\",
313 : : "dir\\/\\a",
314 : : "dir\\/a",
315 : : "dir\\a",
316 : : "dir\\a/",
317 : : "name",
318 : : "name-snakey",
319 : : "name.",
320 : : "name..",
321 : : "name.txt",
322 : : "name.txt.",
323 : : "name/",
324 : : "name//",
325 : : "name///",
326 : : "name_sneaky",
327 : :
328 : : // Filenames with colons that are handled consistently everywhere
329 : : "C:/a/C:/b",
330 : : "C:/a/c:/b",
331 : : "C:a/C:/b",
332 : : "C:a/C:\\b",
333 : : "C:a/c:/b",
334 : : "C:a/c:\\b",
335 : :
336 : : #ifndef _WIN32
337 : : // Filenames with colons that aren't handled consistently on Windows
338 : : "C:/a/C:b",
339 : : "C:/a/D:b",
340 : : "C:/a/c:b",
341 : : "C:a/C:b",
342 : : "C:a/c:b",
343 : : "NO:DRIVE",
344 : : "NODRIVE:",
345 : : "dir/C:",
346 : : "dir/C:/name",
347 : : "dir/C:\\name",
348 : : "no:drive",
349 : : "nodrive:",
350 : : #endif
351 : :
352 : : #ifndef _WIN32
353 : : // Invalid root and network paths that aren't handled consistently on Windows
354 : : "//a//",
355 : : "//host/dir/../../../a",
356 : : "//host/dir/../../a",
357 : : "C://",
358 : : #endif
359 : :
360 : : #if !(defined(_WIN32) && defined(__GLIBCXX__))
361 : : // Network paths that aren't supported by MinGW
362 : : "//h",
363 : : "//h/",
364 : : "//h/a",
365 : : "//h/a.txt",
366 : : "//h/d/a.txt",
367 : : "//h/d/s/a.txt",
368 : : "//host",
369 : : "//host/",
370 : : "//host/.",
371 : : "//host/..",
372 : : "//host/../dir/",
373 : : "//host/../name",
374 : : "//host/..dir/",
375 : : "//host/..name",
376 : : "//host/./dir/",
377 : : "//host/./name",
378 : : "//host/.dir/",
379 : : "//host/.name",
380 : : "//host/C:/dir/name.txt",
381 : : "//host/C:/name",
382 : : "//host/dir/.",
383 : : "//host/dir/..",
384 : : "//host/dir/../name",
385 : : "//host/dir/./name",
386 : : "//host/dir/name",
387 : : "//host/dir/sub/name",
388 : : "//host/name",
389 : : "//host/name.",
390 : : "//host/name..",
391 : : "//host\\",
392 : : "//host\\name.txt",
393 : : "\\\\host",
394 : : "\\\\host\\",
395 : : "\\\\host\\C:\\name",
396 : : "\\\\host\\dir\\name.txt",
397 : : "\\\\host\\name.txt",
398 : : #endif
399 : : };
400 : :
401 : : /// Abort if `path` doesn't match `result` when loosely parsed
402 : : bool
403 : 400 : match(const std::filesystem::path& path, char* const result)
404 : : {
405 [ + + + + : 400 : const bool success = (path.empty() && !result) || (result && path == result);
+ - + - +
- + + -
- ]
406 : :
407 : 400 : free(result);
408 : 400 : return success;
409 : : }
410 : :
411 : : /// Abort if `path` doesn't equal `result`
412 : : bool
413 : 322 : equal(const std::filesystem::path& path, char* const result)
414 : : {
415 : : const bool success =
416 [ + + + - : 322 : (path.empty() && !result) || (result && path.u8string() == result);
+ - + - +
- + - -
- ]
417 : :
418 : 322 : free(result);
419 : 322 : return success;
420 : : }
421 : :
422 : : /// Abort if `path` doesn't match `view` when loosely parsed
423 : : bool
424 : 750 : match(const std::filesystem::path& path, const ZixStringView view)
425 : : {
426 [ + + - + ]: 1109 : return (path.empty() && !view.length) ||
427 [ + - + - : 1109 : (view.length && path == std::string{view.data, view.length});
+ - + - +
+ + + + +
- - - - -
- ]
428 : : }
429 : :
430 : : /// Abort if `path` doesn't equal `view`
431 : : bool
432 : 1250 : equal(const std::filesystem::path& path, const ZixStringView view)
433 : : {
434 [ + + - + ]: 1909 : return (path.empty() && !view.length) ||
435 [ + - + - ]: 1318 : (view.length &&
436 [ + - + - : 1909 : path.u8string() == std::string{view.data, view.length});
+ + + + +
+ - - - -
- - ]
437 : : }
438 : :
439 : : void
440 : 1 : run()
441 : : {
442 : : using Path = std::filesystem::path;
443 : :
444 [ + + ]: 251 : for (const char* string : paths) {
445 [ + - ]: 250 : const Path path{string};
446 : :
447 : : // Decomposition
448 [ + - + - : 250 : assert(equal(path.root_name(), zix_path_root_name(string)));
+ - - + ]
449 [ + - + - : 250 : assert(match(path.root_directory(), zix_path_root_directory(string)));
+ - - + ]
450 [ + - + - : 250 : assert(match(path.root_path(), zix_path_root_path(string)));
+ - - + ]
451 [ + - + - : 250 : assert(equal(path.relative_path(), zix_path_relative_path(string)));
+ - - + ]
452 [ + - + - : 250 : assert(match(path.parent_path(), zix_path_parent_path(string)));
+ - - + ]
453 [ + - + - : 250 : assert(equal(path.filename(), zix_path_filename(string)));
+ - - + ]
454 [ + - + - : 250 : assert(equal(path.stem(), zix_path_stem(string)));
+ - - + ]
455 [ + - + - : 250 : assert(equal(path.extension(), zix_path_extension(string)));
+ - - + ]
456 : :
457 : : // Queries
458 [ + - - + ]: 250 : assert(path.has_root_path() == zix_path_has_root_path(string));
459 [ + - - + ]: 250 : assert(path.has_root_name() == zix_path_has_root_name(string));
460 [ + - - + ]: 250 : assert(path.has_root_directory() == zix_path_has_root_directory(string));
461 [ + - - + ]: 250 : assert(path.has_relative_path() == zix_path_has_relative_path(string));
462 [ + - - + ]: 250 : assert(path.has_parent_path() == zix_path_has_parent_path(string));
463 [ + - - + ]: 250 : assert(path.has_filename() == zix_path_has_filename(string));
464 [ + - - + ]: 250 : assert(path.has_stem() == zix_path_has_stem(string));
465 [ + - - + ]: 250 : assert(path.has_extension() == zix_path_has_extension(string));
466 [ + - - + ]: 250 : assert(path.is_absolute() == zix_path_is_absolute(string));
467 [ + - - + ]: 250 : assert(path.is_relative() == zix_path_is_relative(string));
468 : :
469 : : // Generation
470 : :
471 [ + - + - : 250 : assert(
+ - - + ]
472 : : equal(Path{path}.make_preferred(), zix_path_preferred(nullptr, string)));
473 : :
474 [ + - + - : 250 : assert(match(path.lexically_normal(),
+ - - + ]
475 : : zix_path_lexically_normal(nullptr, string)));
476 : 250 : }
477 : :
478 [ + + ]: 73 : for (const auto& join : joins) {
479 [ + - ]: 72 : char* const joined = zix_path_join(nullptr, join.lhs, join.rhs);
480 [ + + + - ]: 72 : const Path l = join.lhs ? Path{join.lhs} : Path{};
481 [ + + + - ]: 72 : const Path r = join.rhs ? Path{join.rhs} : Path{};
482 : :
483 [ + - + - : 72 : assert(equal(l / r, joined));
- + ]
484 : 72 : }
485 : :
486 [ + + ]: 76 : for (const auto& relatives : lexical_relatives) {
487 [ - + ]: 75 : assert(relatives.lhs);
488 [ - + ]: 75 : assert(relatives.rhs);
489 : :
490 [ + - ]: 75 : const Path l = Path{relatives.lhs};
491 [ + - ]: 75 : const Path r = Path{relatives.rhs};
492 : :
493 [ + - + - : 75 : assert(match(
+ - - + ]
494 : : l.lexically_relative(r),
495 : : zix_path_lexically_relative(nullptr, relatives.lhs, relatives.rhs)));
496 : :
497 [ + - + - : 75 : assert(match(
+ - - + ]
498 : : r.lexically_relative(l),
499 : : zix_path_lexically_relative(nullptr, relatives.rhs, relatives.lhs)));
500 : 75 : }
501 : 1 : }
502 : :
503 : : } // namespace
504 : :
505 : : int
506 : 1 : main()
507 : : {
508 : 1 : run();
509 : 1 : return 0;
510 : : }
|