Branch data Line data Source code
1 : : // Copyright 2020-2025 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #undef NDEBUG
5 : :
6 : : #include "failing_allocator.h"
7 : :
8 : : #include <zix/allocator.h>
9 : : #include <zix/path.h>
10 : : #include <zix/string_view.h>
11 : :
12 : : #include <assert.h>
13 : : #include <stdbool.h>
14 : : #include <stdlib.h>
15 : : #include <string.h>
16 : :
17 : : /// Return whether `string` equals `expected`
18 : : static bool
19 : 139 : equal(char* const string, const char* const expected)
20 : : {
21 : 139 : const bool result =
22 [ + + - + : 139 : (!string && !expected) || (string && expected && !strcmp(string, expected));
+ - + - +
- ]
23 : :
24 : 139 : free(string);
25 : 139 : return result;
26 : : }
27 : :
28 : : /// Return whether `string` matches `expected` with preferred separators
29 : : static bool
30 : 80 : match(char* const string, const char* const expected)
31 : : {
32 [ + + ]: 80 : char* const e = expected ? zix_path_preferred(NULL, expected) : NULL;
33 [ + + ]: 80 : char* const s = string ? zix_path_preferred(NULL, string) : NULL;
34 : :
35 [ + + - + : 80 : const bool result = (!s && !e) || (s && e && !strcmp(s, e));
+ - + - +
- ]
36 : :
37 : 80 : free(s);
38 : 80 : free(e);
39 : 80 : free(string);
40 : 80 : return result;
41 : : }
42 : :
43 : : /// Return whether `view` equals `expected`
44 : : static bool
45 : 229 : view_equal(const ZixStringView view, const char* const expected)
46 : : {
47 : 229 : const bool result =
48 [ + + - + ]: 348 : (!view.length && !expected) ||
49 [ + - + - : 119 : (view.length && expected && strlen(expected) == view.length &&
+ - ]
50 [ + - ]: 119 : !strncmp(view.data, expected, view.length));
51 : :
52 : 229 : return result;
53 : : }
54 : :
55 : : static void
56 : 1 : test_path_join(void)
57 : : {
58 : : // Trivial cases
59 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "", ""), ""));
60 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "", "/b/"), "/b/"));
61 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "", "b"), "b"));
62 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/", ""), "/"));
63 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/", ""), "/a/"));
64 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/b/", ""), "/a/b/"));
65 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/", ""), "a/"));
66 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/b/", ""), "a/b/"));
67 : :
68 : : // Null is treated like an empty path
69 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/", NULL), "/"));
70 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/", NULL), "/a/"));
71 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/b/", NULL), "/a/b/"));
72 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/", NULL), "a/"));
73 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/b/", NULL), "a/b/"));
74 [ - + ]: 1 : assert(equal(zix_path_join(NULL, NULL, "/b"), "/b"));
75 [ - + ]: 1 : assert(equal(zix_path_join(NULL, NULL, "/b/c"), "/b/c"));
76 [ - + ]: 1 : assert(equal(zix_path_join(NULL, NULL, "b"), "b"));
77 [ - + ]: 1 : assert(equal(zix_path_join(NULL, NULL, "b/c"), "b/c"));
78 [ - + ]: 1 : assert(equal(zix_path_join(NULL, NULL, NULL), ""));
79 : :
80 : : // Joining an absolute path discards the left path
81 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a", "/b"), "/b"));
82 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a", "/b/"), "/b/"));
83 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", "/b"), "/b"));
84 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/", "/b"), "/b"));
85 : :
86 : : // Concatenation cases
87 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/", "b"), "/a/b"));
88 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/", "b/"), "/a/b/"));
89 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/", "b"), "a/b"));
90 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/", "b/"), "a/b/"));
91 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/c/", "/b/d"), "/b/d"));
92 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/c/", "b"), "/a/c/b"));
93 : :
94 : : #ifdef _WIN32
95 : : // "C:" is a drive letter, "//host" is a share, backslash is a separator
96 : : assert(equal(zix_path_join(NULL, "//host", ""), "//host\\"));
97 : : assert(equal(zix_path_join(NULL, "//host", "a"), "//host\\a"));
98 : : assert(equal(zix_path_join(NULL, "//host/", "a"), "//host/a"));
99 : : assert(equal(zix_path_join(NULL, "/a", ""), "/a\\"));
100 : : assert(equal(zix_path_join(NULL, "/a", "b"), "/a\\b"));
101 : : assert(equal(zix_path_join(NULL, "/a", "b/"), "/a\\b/"));
102 : : assert(equal(zix_path_join(NULL, "/a", NULL), "/a\\"));
103 : : assert(equal(zix_path_join(NULL, "/a/b", NULL), "/a/b\\"));
104 : : assert(equal(zix_path_join(NULL, "/a/c", "b"), "/a/c\\b"));
105 : : assert(equal(zix_path_join(NULL, "C:", ""), "C:"));
106 : : assert(equal(zix_path_join(NULL, "C:/a", "/b"), "C:/b"));
107 : : assert(equal(zix_path_join(NULL, "C:/a", "C:/b"), "C:/b"));
108 : : assert(equal(zix_path_join(NULL, "C:/a", "C:b"), "C:/a\\b"));
109 : : assert(equal(zix_path_join(NULL, "C:/a", "D:b"), "D:b"));
110 : : assert(equal(zix_path_join(NULL, "C:/a", "b"), "C:/a\\b"));
111 : : assert(equal(zix_path_join(NULL, "C:/a", "b/"), "C:/a\\b/"));
112 : : assert(equal(zix_path_join(NULL, "C:/a", "c:/b"), "c:/b"));
113 : : assert(equal(zix_path_join(NULL, "C:/a", "c:b"), "c:b"));
114 : : assert(equal(zix_path_join(NULL, "C:\\a", "b"), "C:\\a\\b"));
115 : : assert(equal(zix_path_join(NULL, "C:\\a", "b\\"), "C:\\a\\b\\"));
116 : : assert(equal(zix_path_join(NULL, "C:a", "/b"), "C:/b"));
117 : : assert(equal(zix_path_join(NULL, "C:a", "C:/b"), "C:/b"));
118 : : assert(equal(zix_path_join(NULL, "C:a", "C:\\b"), "C:\\b"));
119 : : assert(equal(zix_path_join(NULL, "C:a", "C:b"), "C:a\\b"));
120 : : assert(equal(zix_path_join(NULL, "C:a", "b"), "C:a\\b"));
121 : : assert(equal(zix_path_join(NULL, "C:a", "c:/b"), "c:/b"));
122 : : assert(equal(zix_path_join(NULL, "C:a", "c:\\b"), "c:\\b"));
123 : : assert(equal(zix_path_join(NULL, "C:a", "c:b"), "c:b"));
124 : : assert(equal(zix_path_join(NULL, "a", ""), "a\\"));
125 : : assert(equal(zix_path_join(NULL, "a", "C:"), "C:"));
126 : : assert(equal(zix_path_join(NULL, "a", "C:/b"), "C:/b"));
127 : : assert(equal(zix_path_join(NULL, "a", "C:\\b"), "C:\\b"));
128 : : assert(equal(zix_path_join(NULL, "a", "\\b"), "\\b"));
129 : : assert(equal(zix_path_join(NULL, "a", "b"), "a\\b"));
130 : : assert(equal(zix_path_join(NULL, "a", "b/"), "a\\b/"));
131 : : assert(equal(zix_path_join(NULL, "a", NULL), "a\\"));
132 : : assert(equal(zix_path_join(NULL, "a/b", ""), "a/b\\"));
133 : : assert(equal(zix_path_join(NULL, "a/b", NULL), "a/b\\"));
134 : : assert(equal(zix_path_join(NULL, "a\\", "\\b"), "\\b"));
135 : : assert(equal(zix_path_join(NULL, "a\\", "b"), "a\\b"));
136 : : #else
137 : : // "C:" isn't special, "//host" has extra slashes, slash is the only separator
138 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "//host", ""), "//host/"));
139 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "//host", "a"), "//host/a"));
140 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "//host/", "a"), "//host/a"));
141 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a", ""), "/a/"));
142 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a", "b"), "/a/b"));
143 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a", "b/"), "/a/b/"));
144 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a", NULL), "/a/"));
145 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/b", NULL), "/a/b/"));
146 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "/a/c", "b"), "/a/c/b"));
147 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:", ""), "C:/"));
148 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:/a", "/b"), "/b"));
149 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:/a", "C:/b"), "C:/a/C:/b"));
150 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:/a", "C:b"), "C:/a/C:b"));
151 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:/a", "D:b"), "C:/a/D:b"));
152 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:/a", "b"), "C:/a/b"));
153 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:/a", "b/"), "C:/a/b/"));
154 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:/a", "c:/b"), "C:/a/c:/b"));
155 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:/a", "c:b"), "C:/a/c:b"));
156 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:\\a", "b"), "C:\\a/b"));
157 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:\\a", "b\\"), "C:\\a/b\\"));
158 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:a", "/b"), "/b"));
159 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:a", "C:/b"), "C:a/C:/b"));
160 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:a", "C:\\b"), "C:a/C:\\b"));
161 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:a", "C:b"), "C:a/C:b"));
162 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:a", "b"), "C:a/b"));
163 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:a", "c:/b"), "C:a/c:/b"));
164 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:a", "c:\\b"), "C:a/c:\\b"));
165 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "C:a", "c:b"), "C:a/c:b"));
166 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", ""), "a/"));
167 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", "C:"), "a/C:"));
168 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", "C:/b"), "a/C:/b"));
169 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", "C:\\b"), "a/C:\\b"));
170 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", "\\b"), "a/\\b"));
171 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", "b"), "a/b"));
172 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", "b/"), "a/b/"));
173 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a", NULL), "a/"));
174 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/b", ""), "a/b/"));
175 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a/b", NULL), "a/b/"));
176 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a\\", "\\b"), "a\\/\\b"));
177 [ - + ]: 1 : assert(equal(zix_path_join(NULL, "a\\", "b"), "a\\/b"));
178 : : #endif
179 : 1 : }
180 : :
181 : : static void
182 : 1 : test_path_preferred(void)
183 : : {
184 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, ""), ""));
185 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "some-name"), "some-name"));
186 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "some_name"), "some_name"));
187 : :
188 : : #ifdef _WIN32
189 : : // Backslash is the preferred separator
190 : : assert(equal(zix_path_preferred(NULL, "/"), "\\"));
191 : : assert(equal(zix_path_preferred(NULL, "/."), "\\."));
192 : : assert(equal(zix_path_preferred(NULL, "//a"), "\\\\a"));
193 : : assert(equal(zix_path_preferred(NULL, "//a/"), "\\\\a\\"));
194 : : assert(equal(zix_path_preferred(NULL, "//a//"), "\\\\a\\\\"));
195 : : assert(equal(zix_path_preferred(NULL, "/a//b/c/"), "\\a\\\\b\\c\\"));
196 : : assert(equal(zix_path_preferred(NULL, "/a/b/c/"), "\\a\\b\\c\\"));
197 : : assert(equal(zix_path_preferred(NULL, "\\"), "\\"));
198 : : assert(
199 : : equal(zix_path_preferred(NULL, "\\a\\//b\\/c\\"), "\\a\\\\\\b\\\\c\\"));
200 : : assert(equal(zix_path_preferred(NULL, "\\a\\/b\\/c\\"), "\\a\\\\b\\\\c\\"));
201 : : assert(equal(zix_path_preferred(NULL, "\\a\\b\\c\\"), "\\a\\b\\c\\"));
202 : : #else
203 : : // Slash is the only separator
204 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "/"), "/"));
205 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "/."), "/."));
206 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "//a"), "//a"));
207 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "//a/"), "//a/"));
208 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "//a//"), "//a//"));
209 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "/a//b/c/"), "/a//b/c/"));
210 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "/a/b/c/"), "/a/b/c/"));
211 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "\\"), "\\"));
212 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "\\a\\//b\\/c\\"), "\\a\\//b\\/c\\"));
213 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "\\a\\/b\\/c\\"), "\\a\\/b\\/c\\"));
214 [ - + ]: 1 : assert(equal(zix_path_preferred(NULL, "\\a\\b\\c\\"), "\\a\\b\\c\\"));
215 : : #endif
216 : 1 : }
217 : :
218 : : static void
219 : 1 : test_path_lexically_normal(void)
220 : : {
221 : : // Identities
222 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, ""), ""));
223 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "."), "."));
224 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, ".."), ".."));
225 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "../.."), "../.."));
226 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/a/b/"), "/a/b/"));
227 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/a/b/c"), "/a/b/c"));
228 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b"), "a/b"));
229 : :
230 : : // "Out of bounds" leading dot-dot entries are removed
231 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/../"), "/"));
232 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/../.."), "/"));
233 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/../../"), "/"));
234 : :
235 : : // Windows drive letters are preserved
236 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "C:"), "C:"));
237 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "C:a"), "C:a"));
238 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "C:/"), "C:/"));
239 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "C:/a"), "C:/a"));
240 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "C:/a/"), "C:/a/"));
241 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "C:/a/b"), "C:/a/b"));
242 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "C:a/"), "C:a/"));
243 : :
244 : : // Slashes are replaced with a single preferred-separator
245 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/"), "/"));
246 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "//"), "/"));
247 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "///"), "/"));
248 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "///a///b/////"), "/a/b/"));
249 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/a/b//c"), "/a/b/c"));
250 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a///b"), "a/b"));
251 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a//b"), "a/b"));
252 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b"), "a/b"));
253 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/"), "a/b/"));
254 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b//"), "a/b/"));
255 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b///"), "a/b/"));
256 : :
257 : : // Dot directories are removed
258 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "./.."), ".."));
259 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, ".//a//.//./b/.//"), "a/b/"));
260 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "./a/././b/./"), "a/b/"));
261 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/."), "/"));
262 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/a/b/./"), "/a/b/"));
263 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/."), "a/"));
264 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/./b/."), "a/b/"));
265 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/."), "a/b/"));
266 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/./"), "a/b/"));
267 : :
268 : : // Real entries before a dot-dot entry are removed
269 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "a/.."), "."));
270 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "a/../"), "."));
271 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "a/b/../.."), "."));
272 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "a/b/../../"), "."));
273 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/a/b/c/../"), "/a/b/"));
274 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/a/b/c/../d"), "/a/b/d"));
275 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/a/b/c/../d/"), "/a/b/d/"));
276 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/.."), "a/"));
277 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/../"), "a/"));
278 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/./.."), "a/"));
279 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/./../"), "a/"));
280 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/c/../.."), "a/"));
281 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/b/c/../../"), "a/"));
282 : :
283 : : // Both dot directories and dot-dot entries
284 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/.///b/../"), "a/"));
285 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/./b/.."), "a/"));
286 : :
287 : : // Dot-dot entries after a root are removed
288 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/.."), "/"));
289 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/../"), "/"));
290 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/../.."), "/"));
291 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/../../"), "/"));
292 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/../a"), "/a"));
293 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/../a/../.."), "/"));
294 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "/a/../.."), "/"));
295 : :
296 : : // No trailing separator after a trailing dot-dot entry
297 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "../"), ".."));
298 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "../../"), "../.."));
299 [ - + ]: 1 : assert(match(zix_path_lexically_normal(NULL, "a/../b/../..///"), ".."));
300 [ - + ]: 1 : assert(
301 : : match(zix_path_lexically_normal(NULL, "a/../b/..//..///../"), "../.."));
302 : :
303 : : // Effectively empty paths are a dot
304 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "."), "."));
305 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "./"), "."));
306 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "./."), "."));
307 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "a/.."), "."));
308 : :
309 : : #ifdef _WIN32
310 : :
311 : : // Paths like "C:/filename" have a drive letter
312 : : assert(equal(zix_path_lexically_normal(NULL, "C:/a\\b"), "C:\\a\\b"));
313 : : assert(equal(zix_path_lexically_normal(NULL, "C:\\"), "C:\\"));
314 : : assert(equal(zix_path_lexically_normal(NULL, "C:\\a"), "C:\\a"));
315 : : assert(equal(zix_path_lexically_normal(NULL, "C:\\a/"), "C:\\a\\"));
316 : : assert(equal(zix_path_lexically_normal(NULL, "C:\\a\\"), "C:\\a\\"));
317 : : assert(equal(zix_path_lexically_normal(NULL, "C:a\\"), "C:a\\"));
318 : :
319 : : // Paths like "//host/dir/" have a network root
320 : : assert(equal(zix_path_lexically_normal(NULL, "//a/"), "\\\\a\\"));
321 : : assert(equal(zix_path_lexically_normal(NULL, "//a/.."), "\\\\a\\"));
322 : : assert(equal(zix_path_lexically_normal(NULL, "//a/b/"), "\\\\a\\b\\"));
323 : : assert(equal(zix_path_lexically_normal(NULL, "//a/b/."), "\\\\a\\b\\"));
324 : :
325 : : #else
326 : :
327 : : // "C:" is just a strange directory or file name prefix
328 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "C:/a\\b"), "C:/a\\b"));
329 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "C:\\"), "C:\\"));
330 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "C:\\a"), "C:\\a"));
331 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "C:\\a/"), "C:\\a/"));
332 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "C:\\a\\"), "C:\\a\\"));
333 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "C:a\\"), "C:a\\"));
334 : :
335 : : // Paths like "//host/dir/" have redundant leading slashes
336 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "//a/"), "/a/"));
337 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "//a/.."), "/"));
338 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "//a/b/"), "/a/b/"));
339 [ - + ]: 1 : assert(equal(zix_path_lexically_normal(NULL, "//a/b/."), "/a/b/"));
340 : :
341 : : #endif
342 : 1 : }
343 : :
344 : : static char*
345 : 60 : lexically_relative(const char* const path, const char* const base)
346 : : {
347 : 60 : return zix_path_lexically_relative(NULL, path, base);
348 : : }
349 : :
350 : : static void
351 : 1 : test_path_lexically_relative(void)
352 : : {
353 [ - + ]: 1 : assert(equal(lexically_relative("", ""), "."));
354 [ - + ]: 1 : assert(equal(lexically_relative("", "."), "."));
355 [ - + ]: 1 : assert(equal(lexically_relative(".", ""), "."));
356 [ - + ]: 1 : assert(equal(lexically_relative(".", "."), "."));
357 [ - + ]: 1 : assert(equal(lexically_relative("//base", "a"), NULL));
358 [ - + ]: 1 : assert(equal(lexically_relative("//host", "//host"), "."));
359 [ - + ]: 1 : assert(equal(lexically_relative("//host", "a"), NULL));
360 [ - + ]: 1 : assert(equal(lexically_relative("//host/", "//host/"), "."));
361 [ - + ]: 1 : assert(equal(lexically_relative("//host/", "a"), NULL));
362 [ - + ]: 1 : assert(equal(lexically_relative("//host/a/b", "//host/a/b"), "."));
363 [ - + ]: 1 : assert(equal(lexically_relative("/a/b", "/a/"), "b"));
364 [ - + ]: 1 : assert(equal(lexically_relative("C:/a/b", "C:/a/"), "b"));
365 [ - + ]: 1 : assert(equal(lexically_relative("a", "/"), NULL));
366 [ - + ]: 1 : assert(equal(lexically_relative("a", "//host"), NULL));
367 [ - + ]: 1 : assert(equal(lexically_relative("a", "//host/"), NULL));
368 [ - + ]: 1 : assert(equal(lexically_relative("a", "a"), "."));
369 [ - + ]: 1 : assert(equal(lexically_relative("a/b", "/a/b"), NULL));
370 [ - + ]: 1 : assert(equal(lexically_relative("a/b", "a/b"), "."));
371 [ - + ]: 1 : assert(equal(lexically_relative("a/b/c", "a"), "b/c"));
372 [ - + ]: 1 : assert(equal(lexically_relative("a/b/c", "a/b/c"), "."));
373 [ - + ]: 1 : assert(equal(lexically_relative("a/b/c/", "a/b/c/"), "."));
374 [ - + ]: 1 : assert(match(lexically_relative("../", "../"), "."));
375 [ - + ]: 1 : assert(match(lexically_relative("../", "./"), "../"));
376 [ - + ]: 1 : assert(match(lexically_relative("../", "a"), "../../"));
377 [ - + ]: 1 : assert(match(lexically_relative("../../a", "../b"), "../../a"));
378 [ - + ]: 1 : assert(match(lexically_relative("../a", "../b"), "../a"));
379 [ - + ]: 1 : assert(match(lexically_relative("/a", "/b/c/"), "../../a"));
380 [ - + ]: 1 : assert(match(lexically_relative("/a/b/c", "/a/b/d/"), "../c"));
381 [ - + ]: 1 : assert(match(lexically_relative("/a/b/c", "/a/b/d/e/"), "../../c"));
382 [ - + ]: 1 : assert(match(lexically_relative("/a/b/c", "/a/d"), "../b/c"));
383 [ - + ]: 1 : assert(match(lexically_relative("/a/d", "/a/b/c"), "../../d"));
384 [ - + ]: 1 : assert(match(lexically_relative("C:/D/", "C:/D/F.txt"), "../"));
385 [ - + ]: 1 : assert(match(lexically_relative("C:/D/F", "C:/D/S/"), "../F"));
386 [ - + ]: 1 : assert(match(lexically_relative("C:/D/F", "C:/G"), "../D/F"));
387 [ - + ]: 1 : assert(match(lexically_relative("C:/D/F.txt", "C:/D/"), "F.txt"));
388 [ - + ]: 1 : assert(match(lexically_relative("C:/E", "C:/D/G"), "../../E"));
389 [ - + ]: 1 : assert(match(lexically_relative("C:/a", "C:/b/c/"), "../../a"));
390 [ - + ]: 1 : assert(match(lexically_relative("C:/a/b/c", "C:/a/b/d/"), "../c"));
391 [ - + ]: 1 : assert(match(lexically_relative("a/b", "c/d"), "../../a/b"));
392 [ - + ]: 1 : assert(match(lexically_relative("a/b/c", "../"), NULL));
393 [ - + ]: 1 : assert(match(lexically_relative("a/b/c", "../../"), NULL));
394 [ - + ]: 1 : assert(match(lexically_relative("a/b/c", "a/b/c/x/y"), "../.."));
395 : :
396 : : #ifdef _WIN32
397 : : assert(equal(lexically_relative("/", "a"), "/"));
398 : : assert(equal(lexically_relative("//host", "//host/"), NULL));
399 : : assert(equal(lexically_relative("//host/", "//host"), "/"));
400 : : assert(equal(lexically_relative("//host/", "/a"), NULL));
401 : : assert(equal(lexically_relative("/a", "//host"), NULL));
402 : : assert(equal(lexically_relative("/a", "//host/"), NULL));
403 : : assert(equal(lexically_relative("C:/D/", "C:F.txt"), NULL));
404 : : assert(equal(lexically_relative("C:/D/S/", "F.txt"), NULL));
405 : : assert(equal(lexically_relative("C:F", "C:/D/"), NULL));
406 : : assert(equal(lexically_relative("C:F", "D:G"), NULL));
407 : : assert(equal(lexically_relative("F", "C:/D/S/"), NULL));
408 : : assert(match(lexically_relative("C:../", "C:../"), "."));
409 : : assert(match(lexically_relative("C:../", "C:./"), "../"));
410 : : assert(match(lexically_relative("C:../", "C:a"), "../../"));
411 : : assert(match(lexically_relative("C:../../a", "C:../b"), "../../a"));
412 : : assert(match(lexically_relative("C:../a", "C:../b"), "../a"));
413 : : assert(match(lexically_relative("C:a/b/c", "C:../"), NULL));
414 : : assert(match(lexically_relative("C:a/b/c", "C:../../"), NULL));
415 : : #else
416 [ - + ]: 1 : assert(equal(lexically_relative("/", "a"), NULL));
417 [ - + ]: 1 : assert(equal(lexically_relative("//host", "//host/"), "."));
418 [ - + ]: 1 : assert(equal(lexically_relative("//host/", "//host"), "."));
419 [ - + ]: 1 : assert(equal(lexically_relative("//host/", "/a"), "../host/"));
420 [ - + ]: 1 : assert(equal(lexically_relative("/a", "//host"), "../a"));
421 [ - + ]: 1 : assert(equal(lexically_relative("/a", "//host/"), "../a"));
422 [ - + ]: 1 : assert(equal(lexically_relative("C:/D/", "C:F.txt"), "../C:/D/"));
423 [ - + ]: 1 : assert(equal(lexically_relative("C:/D/S/", "F.txt"), "../C:/D/S/"));
424 [ - + ]: 1 : assert(equal(lexically_relative("C:F", "C:/D/"), "../../C:F"));
425 [ - + ]: 1 : assert(equal(lexically_relative("C:F", "D:G"), "../C:F"));
426 [ - + ]: 1 : assert(equal(lexically_relative("F", "C:/D/S/"), "../../../F"));
427 [ - + ]: 1 : assert(match(lexically_relative("C:../", "C:../"), "."));
428 [ - + ]: 1 : assert(match(lexically_relative("C:../", "C:./"), "../C:../"));
429 [ - + ]: 1 : assert(match(lexically_relative("C:../", "C:a"), "../C:../"));
430 [ - + ]: 1 : assert(match(lexically_relative("C:../../a", "C:../b"), "../../a"));
431 [ - + ]: 1 : assert(match(lexically_relative("C:../a", "C:../b"), "../a"));
432 [ - + ]: 1 : assert(match(lexically_relative("C:a/b/c", "C:../"), "../C:a/b/c"));
433 [ - + ]: 1 : assert(match(lexically_relative("C:a/b/c", "C:../../"), "C:a/b/c"));
434 : : #endif
435 : 1 : }
436 : :
437 : : static void
438 : 1 : test_path_root_name(void)
439 : : {
440 : : // Relative paths with no root
441 [ - + ]: 1 : assert(view_equal(zix_path_root_name(""), NULL));
442 [ - + ]: 1 : assert(view_equal(zix_path_root_name("."), NULL));
443 [ - + ]: 1 : assert(view_equal(zix_path_root_name(".."), NULL));
444 [ - + ]: 1 : assert(view_equal(zix_path_root_name("../"), NULL));
445 [ - + ]: 1 : assert(view_equal(zix_path_root_name("./"), NULL));
446 [ - + ]: 1 : assert(view_equal(zix_path_root_name("NONDRIVE:"), NULL));
447 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a"), NULL));
448 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a/"), NULL));
449 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a/."), NULL));
450 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a/.."), NULL));
451 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a/../"), NULL));
452 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a/../b"), NULL));
453 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a/./"), NULL));
454 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a/./b"), NULL));
455 [ - + ]: 1 : assert(view_equal(zix_path_root_name("a/b"), NULL));
456 : :
457 : : // Absolute paths with a POSIX-style root
458 [ - + ]: 1 : assert(view_equal(zix_path_root_name("/"), NULL));
459 [ - + ]: 1 : assert(view_equal(zix_path_root_name("/."), NULL));
460 [ - + ]: 1 : assert(view_equal(zix_path_root_name("/.."), NULL));
461 [ - + ]: 1 : assert(view_equal(zix_path_root_name("//"), NULL));
462 [ - + ]: 1 : assert(view_equal(zix_path_root_name("///a///"), NULL));
463 [ - + ]: 1 : assert(view_equal(zix_path_root_name("///a///b"), NULL));
464 [ - + ]: 1 : assert(view_equal(zix_path_root_name("/a"), NULL));
465 [ - + ]: 1 : assert(view_equal(zix_path_root_name("/a/"), NULL));
466 [ - + ]: 1 : assert(view_equal(zix_path_root_name("/a//b"), NULL));
467 : :
468 : : #ifdef _WIN32
469 : :
470 : : // Paths like "C:/filename" have a drive letter
471 : : assert(view_equal(zix_path_root_name("C:"), "C:"));
472 : : assert(view_equal(zix_path_root_name("C:/"), "C:"));
473 : : assert(view_equal(zix_path_root_name("C:/a"), "C:"));
474 : : assert(view_equal(zix_path_root_name("C:/a/"), "C:"));
475 : : assert(view_equal(zix_path_root_name("C:/a/b"), "C:"));
476 : : assert(view_equal(zix_path_root_name("C:a"), "C:"));
477 : : assert(view_equal(zix_path_root_name("C:a/"), "C:"));
478 : :
479 : : // Paths like "//host/" are network roots
480 : : assert(view_equal(zix_path_root_name("//host"), "//host"));
481 : : assert(view_equal(zix_path_root_name("//host/"), "//host"));
482 : : assert(view_equal(zix_path_root_name("//host/a"), "//host"));
483 : :
484 : : // Backslash is a directory separator
485 : : assert(view_equal(zix_path_root_name("C:/a\\b"), "C:"));
486 : : assert(view_equal(zix_path_root_name("C:\\"), "C:"));
487 : : assert(view_equal(zix_path_root_name("C:\\a"), "C:"));
488 : : assert(view_equal(zix_path_root_name("C:\\a/"), "C:"));
489 : : assert(view_equal(zix_path_root_name("C:\\a\\"), "C:"));
490 : : assert(view_equal(zix_path_root_name("C:a\\"), "C:"));
491 : :
492 : : #else
493 : :
494 : : // "C:" is just a strange directory or file name prefix
495 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:"), NULL));
496 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:/"), NULL));
497 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:/a"), NULL));
498 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:/a/"), NULL));
499 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:/a/b"), NULL));
500 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:a"), NULL));
501 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:a/"), NULL));
502 : :
503 : : // Paths like "//host/" have redundant leading slashes
504 [ - + ]: 1 : assert(view_equal(zix_path_root_name("//host"), NULL));
505 [ - + ]: 1 : assert(view_equal(zix_path_root_name("//host/"), NULL));
506 [ - + ]: 1 : assert(view_equal(zix_path_root_name("//host/a"), NULL));
507 : :
508 : : // Backslash is a character in a directory or file name
509 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:/a\\b"), NULL));
510 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:\\"), NULL));
511 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:\\a"), NULL));
512 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:\\a/"), NULL));
513 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:\\a\\"), NULL));
514 [ - + ]: 1 : assert(view_equal(zix_path_root_name("C:a\\"), NULL));
515 : :
516 : : #endif
517 : 1 : }
518 : :
519 : : static void
520 : 1 : test_path_root(void)
521 : : {
522 : : // Relative paths with no root
523 [ - + ]: 1 : assert(view_equal(zix_path_root_path(""), NULL));
524 [ - + ]: 1 : assert(view_equal(zix_path_root_path("."), NULL));
525 [ - + ]: 1 : assert(view_equal(zix_path_root_path(".."), NULL));
526 [ - + ]: 1 : assert(view_equal(zix_path_root_path("../"), NULL));
527 [ - + ]: 1 : assert(view_equal(zix_path_root_path("./"), NULL));
528 [ - + ]: 1 : assert(view_equal(zix_path_root_path("NONDRIVE:"), NULL));
529 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a"), NULL));
530 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a/"), NULL));
531 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a/."), NULL));
532 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a/.."), NULL));
533 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a/../"), NULL));
534 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a/../b"), NULL));
535 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a/./"), NULL));
536 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a/./b"), NULL));
537 [ - + ]: 1 : assert(view_equal(zix_path_root_path("a/b"), NULL));
538 : :
539 : : // Absolute paths with a POSIX-style root
540 [ - + ]: 1 : assert(view_equal(zix_path_root_path("/"), "/"));
541 [ - + ]: 1 : assert(view_equal(zix_path_root_path("/."), "/"));
542 [ - + ]: 1 : assert(view_equal(zix_path_root_path("/.."), "/"));
543 [ - + ]: 1 : assert(view_equal(zix_path_root_path("//"), "/"));
544 [ - + ]: 1 : assert(view_equal(zix_path_root_path("///a///"), "/"));
545 [ - + ]: 1 : assert(view_equal(zix_path_root_path("///a///b"), "/"));
546 [ - + ]: 1 : assert(view_equal(zix_path_root_path("/a"), "/"));
547 [ - + ]: 1 : assert(view_equal(zix_path_root_path("/a/"), "/"));
548 [ - + ]: 1 : assert(view_equal(zix_path_root_path("/a//b"), "/"));
549 : :
550 : : #ifdef _WIN32
551 : :
552 : : // Paths like "C:/filename" have a drive letter
553 : : assert(view_equal(zix_path_root_path("C:"), "C:"));
554 : : assert(view_equal(zix_path_root_path("C:/"), "C:/"));
555 : : assert(view_equal(zix_path_root_path("C:/a"), "C:/"));
556 : : assert(view_equal(zix_path_root_path("C:/a/"), "C:/"));
557 : : assert(view_equal(zix_path_root_path("C:/a/b"), "C:/"));
558 : : assert(view_equal(zix_path_root_path("C:a"), "C:"));
559 : : assert(view_equal(zix_path_root_path("C:a/"), "C:"));
560 : :
561 : : // Paths like "//host/" are network roots
562 : : assert(view_equal(zix_path_root_path("//host"), "//host"));
563 : : assert(view_equal(zix_path_root_path("//host/"), "//host/"));
564 : : assert(view_equal(zix_path_root_path("//host/a"), "//host/"));
565 : :
566 : : // Backslash is a directory separator
567 : : assert(view_equal(zix_path_root_path("C:/a\\b"), "C:/"));
568 : : assert(view_equal(zix_path_root_path("C:\\"), "C:\\"));
569 : : assert(view_equal(zix_path_root_path("C:\\a"), "C:\\"));
570 : : assert(view_equal(zix_path_root_path("C:\\a/"), "C:\\"));
571 : : assert(view_equal(zix_path_root_path("C:\\a\\"), "C:\\"));
572 : : assert(view_equal(zix_path_root_path("C:a\\"), "C:"));
573 : :
574 : : #else
575 : :
576 : : // "C:" is just a strange directory or file name prefix
577 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:"), NULL));
578 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:/"), NULL));
579 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:/a"), NULL));
580 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:/a/"), NULL));
581 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:/a/b"), NULL));
582 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:a"), NULL));
583 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:a/"), NULL));
584 : :
585 : : // Paths like "//host/" have redundant leading slashes
586 [ - + ]: 1 : assert(view_equal(zix_path_root_path("//host"), "/"));
587 [ - + ]: 1 : assert(view_equal(zix_path_root_path("//host/"), "/"));
588 [ - + ]: 1 : assert(view_equal(zix_path_root_path("//host/a"), "/"));
589 : :
590 : : // Backslash is a character in a directory or file name
591 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:/a\\b"), NULL));
592 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:\\"), NULL));
593 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:\\a"), NULL));
594 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:\\a/"), NULL));
595 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:\\a\\"), NULL));
596 [ - + ]: 1 : assert(view_equal(zix_path_root_path("C:a\\"), NULL));
597 : :
598 : : #endif
599 : 1 : }
600 : :
601 : : static void
602 : 1 : test_path_parent(void)
603 : : {
604 : : // Absolute paths
605 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("/"), "/"));
606 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("/."), "/"));
607 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("/.."), "/"));
608 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("//"), "/"));
609 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("/a"), "/"));
610 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("/a/"), "/a"));
611 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("/a//b"), "/a"));
612 : :
613 : : // Relative paths with no parent
614 [ - + ]: 1 : assert(view_equal(zix_path_parent_path(""), NULL));
615 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("."), NULL));
616 [ - + ]: 1 : assert(view_equal(zix_path_parent_path(".."), NULL));
617 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("NONDRIVE:"), NULL));
618 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a"), NULL));
619 : :
620 : : // Relative paths with a parent
621 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("../"), ".."));
622 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("./"), "."));
623 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a/"), "a"));
624 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a/b"), "a"));
625 : :
626 : : // Superfluous leading and trailing separators
627 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("///a///"), "/a"));
628 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("///a///b"), "/a"));
629 : :
630 : : // Relative paths with dot and dot-dot entries
631 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a/."), "a"));
632 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a/.."), "a"));
633 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a/../"), "a/.."));
634 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a/../b"), "a/.."));
635 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a/./"), "a/."));
636 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("a/./b"), "a/."));
637 : :
638 : : #ifdef _WIN32
639 : :
640 : : // Paths like "C:/filename" have a drive letter
641 : : assert(view_equal(zix_path_parent_path("C:"), "C:"));
642 : : assert(view_equal(zix_path_parent_path("C:/"), "C:/"));
643 : : assert(view_equal(zix_path_parent_path("C:/a"), "C:/"));
644 : : assert(view_equal(zix_path_parent_path("C:/a/"), "C:/a"));
645 : : assert(view_equal(zix_path_parent_path("C:/a/b"), "C:/a"));
646 : : assert(view_equal(zix_path_parent_path("C:/a\\b"), "C:/a"));
647 : : assert(view_equal(zix_path_parent_path("C:\\"), "C:\\"));
648 : : assert(view_equal(zix_path_parent_path("C:\\a"), "C:\\"));
649 : : assert(view_equal(zix_path_parent_path("C:\\a/"), "C:\\a"));
650 : : assert(view_equal(zix_path_parent_path("C:\\a\\"), "C:\\a"));
651 : : assert(view_equal(zix_path_parent_path("C:a"), "C:"));
652 : : assert(view_equal(zix_path_parent_path("C:a/"), "C:a"));
653 : : assert(view_equal(zix_path_parent_path("C:a\\"), "C:a"));
654 : :
655 : : // Paths like "//host/" are network shares
656 : : assert(view_equal(zix_path_parent_path("//host"), "//host"));
657 : : assert(view_equal(zix_path_parent_path("//host/"), "//host/"));
658 : : assert(view_equal(zix_path_parent_path("//host/a"), "//host/"));
659 : :
660 : : #else
661 : :
662 : : // "C:" is just a strange directory or file name prefix
663 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:"), NULL));
664 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:/"), "C:"));
665 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:/a"), "C:"));
666 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:/a/"), "C:/a"));
667 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:/a/b"), "C:/a"));
668 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:/a\\b"), "C:"));
669 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:\\"), NULL));
670 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:\\a"), NULL));
671 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:\\a/"), "C:\\a"));
672 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:\\a\\"), NULL));
673 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:a"), NULL));
674 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:a/"), "C:a"));
675 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("C:a\\"), NULL));
676 : :
677 : : // Paths like "//host/" have redundant leading slashes
678 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("//host"), "/"));
679 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("//host/"), "/host"));
680 [ - + ]: 1 : assert(view_equal(zix_path_parent_path("//host/a"), "/host"));
681 : :
682 : : #endif
683 : 1 : }
684 : :
685 : : static void
686 : 1 : test_path_filename(void)
687 : : {
688 : : // Cases from <https://en.cppreference.com/w/cpp/filesystem/path/filename>
689 [ - + ]: 1 : assert(view_equal(zix_path_filename("."), "."));
690 [ - + ]: 1 : assert(view_equal(zix_path_filename(".."), ".."));
691 [ - + ]: 1 : assert(view_equal(zix_path_filename("/"), NULL));
692 [ - + ]: 1 : assert(view_equal(zix_path_filename("/foo/."), "."));
693 [ - + ]: 1 : assert(view_equal(zix_path_filename("/foo/.."), ".."));
694 [ - + ]: 1 : assert(view_equal(zix_path_filename("/foo/.bar"), ".bar"));
695 [ - + ]: 1 : assert(view_equal(zix_path_filename("/foo/bar.txt"), "bar.txt"));
696 [ - + ]: 1 : assert(view_equal(zix_path_filename("/foo/bar/"), NULL));
697 : :
698 : : // Identities
699 [ - + ]: 1 : assert(view_equal(zix_path_filename("."), "."));
700 [ - + ]: 1 : assert(view_equal(zix_path_filename(".."), ".."));
701 [ - + ]: 1 : assert(view_equal(zix_path_filename("a"), "a"));
702 : :
703 : : // Absolute paths without filenames
704 [ - + ]: 1 : assert(view_equal(zix_path_filename("/"), NULL));
705 [ - + ]: 1 : assert(view_equal(zix_path_filename("//"), NULL));
706 [ - + ]: 1 : assert(view_equal(zix_path_filename("///a///"), NULL));
707 [ - + ]: 1 : assert(view_equal(zix_path_filename("/a/"), NULL));
708 : :
709 : : // Absolute paths with filenames
710 [ - + ]: 1 : assert(view_equal(zix_path_filename("///a///b"), "b"));
711 [ - + ]: 1 : assert(view_equal(zix_path_filename("/a"), "a"));
712 [ - + ]: 1 : assert(view_equal(zix_path_filename("/a//b"), "b"));
713 : :
714 : : // Relative paths without filenames
715 [ - + ]: 1 : assert(view_equal(zix_path_filename(""), NULL));
716 [ - + ]: 1 : assert(view_equal(zix_path_filename("../"), NULL));
717 [ - + ]: 1 : assert(view_equal(zix_path_filename("./"), NULL));
718 [ - + ]: 1 : assert(view_equal(zix_path_filename("a/"), NULL));
719 : :
720 : : // Relative paths with filenames
721 [ - + ]: 1 : assert(view_equal(zix_path_filename("a/b"), "b"));
722 : :
723 : : // Windows absolute network paths conveniently work generically
724 [ - + ]: 1 : assert(view_equal(zix_path_filename("//host/"), NULL));
725 [ - + ]: 1 : assert(view_equal(zix_path_filename("//host/a"), "a"));
726 : :
727 : : // Paths with dot and dot-dot entries
728 [ - + ]: 1 : assert(view_equal(zix_path_filename("/."), "."));
729 [ - + ]: 1 : assert(view_equal(zix_path_filename("/.."), ".."));
730 [ - + ]: 1 : assert(view_equal(zix_path_filename("a/."), "."));
731 [ - + ]: 1 : assert(view_equal(zix_path_filename("a/.."), ".."));
732 [ - + ]: 1 : assert(view_equal(zix_path_filename("a/../b"), "b"));
733 [ - + ]: 1 : assert(view_equal(zix_path_filename("a/./b"), "b"));
734 : :
735 : : // Paths with colons (maybe drive letters) that conveniently work generically
736 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:/"), NULL));
737 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:/a"), "a"));
738 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:/a/"), NULL));
739 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:/a/b"), "b"));
740 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:a/"), NULL));
741 [ - + ]: 1 : assert(view_equal(zix_path_filename("NONDRIVE:"), "NONDRIVE:"));
742 : :
743 : : #ifdef _WIN32
744 : :
745 : : // Relative paths can have a drive letter like "C:file"
746 : : assert(view_equal(zix_path_filename("C:"), NULL));
747 : : assert(view_equal(zix_path_filename("C:a"), "a"));
748 : : assert(view_equal(zix_path_filename("C:a\\"), NULL));
749 : :
750 : : // Paths like "//host" are network roots
751 : : assert(view_equal(zix_path_filename("//host"), NULL));
752 : :
753 : : // Backslash is a directory separator
754 : : assert(view_equal(zix_path_filename("C:/a\\b"), "b"));
755 : : assert(view_equal(zix_path_filename("C:\\"), NULL));
756 : : assert(view_equal(zix_path_filename("C:\\a"), "a"));
757 : : assert(view_equal(zix_path_filename("C:\\a/"), NULL));
758 : : assert(view_equal(zix_path_filename("C:\\a\\"), NULL));
759 : : assert(view_equal(zix_path_filename("C:\\a\\b"), "b"));
760 : : assert(view_equal(zix_path_filename("a\\b"), "b"));
761 : :
762 : : #else
763 : :
764 : : // "C:" is just a strange directory or file name prefix
765 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:"), "C:"));
766 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:a"), "C:a"));
767 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:a\\"), "C:a\\"));
768 : :
769 : : // Redundant reading slashes are ignored
770 [ - + ]: 1 : assert(view_equal(zix_path_filename("//host"), "host"));
771 : :
772 : : // Backslash is just a strange character in a directory or file name
773 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:/a\\b"), "a\\b"));
774 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:\\"), "C:\\"));
775 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:\\a"), "C:\\a"));
776 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:\\a/"), NULL));
777 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:\\a\\"), "C:\\a\\"));
778 [ - + ]: 1 : assert(view_equal(zix_path_filename("C:\\a\\b"), "C:\\a\\b"));
779 [ - + ]: 1 : assert(view_equal(zix_path_filename("a\\b"), "a\\b"));
780 : :
781 : : #endif
782 : 1 : }
783 : :
784 : : static void
785 : 1 : test_path_stem(void)
786 : : {
787 [ - + ]: 1 : assert(view_equal(zix_path_stem(""), NULL));
788 [ - + ]: 1 : assert(view_equal(zix_path_stem("."), "."));
789 [ - + ]: 1 : assert(view_equal(zix_path_stem(".."), ".."));
790 [ - + ]: 1 : assert(view_equal(zix_path_stem(".a"), ".a"));
791 [ - + ]: 1 : assert(view_equal(zix_path_stem(".hidden"), ".hidden"));
792 [ - + ]: 1 : assert(view_equal(zix_path_stem(".hidden.txt"), ".hidden"));
793 [ - + ]: 1 : assert(view_equal(zix_path_stem("/"), NULL));
794 [ - + ]: 1 : assert(view_equal(zix_path_stem("//host/name"), "name"));
795 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a."), "a"));
796 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a.txt"), "a"));
797 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a/."), "."));
798 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a/.."), ".."));
799 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a/.hidden"), ".hidden"));
800 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a/b."), "b"));
801 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a/b.tar.gz"), "b.tar"));
802 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a/b.txt"), "b"));
803 [ - + ]: 1 : assert(view_equal(zix_path_stem("/a/b/.hidden"), ".hidden"));
804 [ - + ]: 1 : assert(view_equal(zix_path_stem("C:/name"), "name"));
805 [ - + ]: 1 : assert(view_equal(zix_path_stem("C:dir/name"), "name"));
806 [ - + ]: 1 : assert(view_equal(zix_path_stem("a"), "a"));
807 [ - + ]: 1 : assert(view_equal(zix_path_stem("a."), "a"));
808 [ - + ]: 1 : assert(view_equal(zix_path_stem("a..txt"), "a."));
809 [ - + ]: 1 : assert(view_equal(zix_path_stem("a.txt"), "a"));
810 [ - + ]: 1 : assert(view_equal(zix_path_stem("a/."), "."));
811 [ - + ]: 1 : assert(view_equal(zix_path_stem("a/.."), ".."));
812 [ - + ]: 1 : assert(view_equal(zix_path_stem("a/b."), "b"));
813 [ - + ]: 1 : assert(view_equal(zix_path_stem("a/b.tar.gz"), "b.tar"));
814 [ - + ]: 1 : assert(view_equal(zix_path_stem("a/b.txt"), "b"));
815 : :
816 : : #ifdef _WIN32
817 : : assert(view_equal(zix_path_stem("C:."), "."));
818 : : assert(view_equal(zix_path_stem("C:.a"), ".a"));
819 : : assert(view_equal(zix_path_stem("C:a"), "a"));
820 : : #else
821 [ - + ]: 1 : assert(view_equal(zix_path_stem("C:."), "C:"));
822 [ - + ]: 1 : assert(view_equal(zix_path_stem("C:.a"), "C:"));
823 [ - + ]: 1 : assert(view_equal(zix_path_stem("C:a"), "C:a"));
824 : : #endif
825 : 1 : }
826 : :
827 : : static void
828 : 1 : test_path_extension(void)
829 : : {
830 [ - + ]: 1 : assert(view_equal(zix_path_extension(""), NULL));
831 [ - + ]: 1 : assert(view_equal(zix_path_extension("."), NULL));
832 [ - + ]: 1 : assert(view_equal(zix_path_extension(".."), NULL));
833 [ - + ]: 1 : assert(view_equal(zix_path_extension(".a"), NULL));
834 [ - + ]: 1 : assert(view_equal(zix_path_extension(".hidden"), NULL));
835 [ - + ]: 1 : assert(view_equal(zix_path_extension(".hidden.txt"), ".txt"));
836 [ - + ]: 1 : assert(view_equal(zix_path_extension("/"), NULL));
837 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a."), "."));
838 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a.txt"), ".txt"));
839 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a/."), NULL));
840 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a/.."), NULL));
841 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a/.hidden"), NULL));
842 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a/b."), "."));
843 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a/b.tar.gz"), ".gz"));
844 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a/b.txt"), ".txt"));
845 [ - + ]: 1 : assert(view_equal(zix_path_extension("/a/b/.hidden"), NULL));
846 [ - + ]: 1 : assert(view_equal(zix_path_extension("C:/.hidden.txt"), ".txt"));
847 [ - + ]: 1 : assert(view_equal(zix_path_extension("C:a.txt"), ".txt"));
848 [ - + ]: 1 : assert(view_equal(zix_path_extension("a"), NULL));
849 [ - + ]: 1 : assert(view_equal(zix_path_extension("a."), "."));
850 [ - + ]: 1 : assert(view_equal(zix_path_extension("a..txt"), ".txt"));
851 [ - + ]: 1 : assert(view_equal(zix_path_extension("a.tar.gz"), ".gz"));
852 [ - + ]: 1 : assert(view_equal(zix_path_extension("a/."), NULL));
853 [ - + ]: 1 : assert(view_equal(zix_path_extension("a/.."), NULL));
854 [ - + ]: 1 : assert(view_equal(zix_path_extension("a/b."), "."));
855 [ - + ]: 1 : assert(view_equal(zix_path_extension("a/b.tar.gz"), ".gz"));
856 [ - + ]: 1 : assert(view_equal(zix_path_extension("a/b.txt"), ".txt"));
857 : :
858 : : #ifdef _WIN32
859 : : assert(view_equal(zix_path_extension("C:."), NULL));
860 : : assert(view_equal(zix_path_extension("C:/.hidden"), NULL));
861 : : assert(view_equal(zix_path_extension("C:/a.txt"), ".txt"));
862 : : #else
863 [ - + ]: 1 : assert(view_equal(zix_path_extension("C:."), "."));
864 [ - + ]: 1 : assert(view_equal(zix_path_extension("C:/.hidden"), NULL));
865 [ - + ]: 1 : assert(view_equal(zix_path_extension("C:/a.txt"), ".txt"));
866 : : #endif
867 : 1 : }
868 : :
869 : : static void
870 : 1 : test_path_is_absolute(void)
871 : : {
872 [ - + ]: 1 : assert(!zix_path_is_absolute("."));
873 [ - + ]: 1 : assert(!zix_path_is_absolute(".."));
874 [ - + ]: 1 : assert(!zix_path_is_absolute("../"));
875 [ - + ]: 1 : assert(!zix_path_is_absolute("../a"));
876 [ - + ]: 1 : assert(!zix_path_is_absolute("../a/"));
877 [ - + ]: 1 : assert(!zix_path_is_absolute("a"));
878 [ - + ]: 1 : assert(!zix_path_is_absolute("a/b"));
879 [ - + ]: 1 : assert(!zix_path_is_relative("//host/a"));
880 [ - + ]: 1 : assert(zix_path_is_absolute("//host/a"));
881 [ - + ]: 1 : assert(zix_path_is_relative("."));
882 [ - + ]: 1 : assert(zix_path_is_relative(".."));
883 [ - + ]: 1 : assert(zix_path_is_relative("../"));
884 [ - + ]: 1 : assert(zix_path_is_relative("../a"));
885 [ - + ]: 1 : assert(zix_path_is_relative("../a/"));
886 [ - + ]: 1 : assert(zix_path_is_relative("a"));
887 [ - + ]: 1 : assert(zix_path_is_relative("a/b"));
888 : :
889 : : #ifdef _WIN32
890 : : // Paths starting with root names are absolute
891 : : assert(!zix_path_is_absolute("/"));
892 : : assert(!zix_path_is_absolute("/a"));
893 : : assert(!zix_path_is_absolute("/a/b"));
894 : : assert(!zix_path_is_relative("C:/a/b"));
895 : : assert(!zix_path_is_relative("C:\\a\\b"));
896 : : assert(!zix_path_is_relative("D:/a/b"));
897 : : assert(!zix_path_is_relative("D:\\a\\b"));
898 : : assert(zix_path_is_absolute("C:/a/b"));
899 : : assert(zix_path_is_absolute("C:\\a\\b"));
900 : : assert(zix_path_is_absolute("D:/a/b"));
901 : : assert(zix_path_is_absolute("D:\\a\\b"));
902 : : assert(zix_path_is_relative("/"));
903 : : assert(zix_path_is_relative("/a"));
904 : : assert(zix_path_is_relative("/a/b"));
905 : : #else
906 : : // Paths starting with slashes are absolute
907 [ - + ]: 1 : assert(!zix_path_is_absolute("C:/a/b"));
908 [ - + ]: 1 : assert(!zix_path_is_absolute("C:\\a\\b"));
909 [ - + ]: 1 : assert(!zix_path_is_absolute("D:/a/b"));
910 [ - + ]: 1 : assert(!zix_path_is_absolute("D:\\a\\b"));
911 [ - + ]: 1 : assert(!zix_path_is_relative("/"));
912 [ - + ]: 1 : assert(!zix_path_is_relative("/a"));
913 [ - + ]: 1 : assert(!zix_path_is_relative("/a/b"));
914 [ - + ]: 1 : assert(zix_path_is_absolute("/"));
915 [ - + ]: 1 : assert(zix_path_is_absolute("/a"));
916 [ - + ]: 1 : assert(zix_path_is_absolute("/a/b"));
917 [ - + ]: 1 : assert(zix_path_is_relative("C:/a/b"));
918 [ - + ]: 1 : assert(zix_path_is_relative("C:\\a\\b"));
919 [ - + ]: 1 : assert(zix_path_is_relative("D:/a/b"));
920 [ - + ]: 1 : assert(zix_path_is_relative("D:\\a\\b"));
921 : : #endif
922 : 1 : }
923 : :
924 : : static void
925 : 1 : test_null_queries(void)
926 : : {
927 [ - + ]: 1 : assert(!zix_path_has_root_path(NULL));
928 [ - + ]: 1 : assert(!zix_path_has_root_name(NULL));
929 [ - + ]: 1 : assert(!zix_path_has_root_directory(NULL));
930 [ - + ]: 1 : assert(!zix_path_has_relative_path(NULL));
931 [ - + ]: 1 : assert(!zix_path_has_parent_path(NULL));
932 [ - + ]: 1 : assert(!zix_path_has_filename(NULL));
933 [ - + ]: 1 : assert(!zix_path_has_stem(NULL));
934 [ - + ]: 1 : assert(!zix_path_has_extension(NULL));
935 [ - + ]: 1 : assert(!zix_path_is_absolute(NULL));
936 [ - + ]: 1 : assert(zix_path_is_relative(NULL));
937 : 1 : }
938 : :
939 : : typedef char* (*AllocCheckFunc)(ZixAllocator*);
940 : :
941 : : static char*
942 : 2 : check_join_alloc(ZixAllocator* const allocator)
943 : : {
944 : 2 : return zix_path_join(allocator, "a", "b");
945 : : }
946 : :
947 : : static char*
948 : 2 : check_join_empty_alloc(ZixAllocator* const allocator)
949 : : {
950 : 2 : return zix_path_join(allocator, "", "b");
951 : : }
952 : :
953 : : static char*
954 : 2 : check_preferred_alloc(ZixAllocator* const allocator)
955 : : {
956 : 2 : return zix_path_preferred(allocator, "/a/b");
957 : : }
958 : :
959 : : static char*
960 : 2 : check_lexically_normal_alloc(ZixAllocator* const allocator)
961 : : {
962 : 2 : return zix_path_lexically_normal(allocator, "./a/././b/./");
963 : : }
964 : :
965 : : static char*
966 : 2 : check_lexically_normal_empty_alloc(ZixAllocator* const allocator)
967 : : {
968 : 2 : return zix_path_lexically_normal(allocator, "");
969 : : }
970 : :
971 : : static char*
972 : 2 : check_lexically_relative_alloc(ZixAllocator* const allocator)
973 : : {
974 : 2 : return zix_path_lexically_relative(allocator, "../../a", "../b");
975 : : }
976 : :
977 : : static char*
978 : 2 : check_lexically_relative_equal_alloc(ZixAllocator* const allocator)
979 : : {
980 : 2 : return zix_path_lexically_relative(allocator, "a", "a");
981 : : }
982 : :
983 : : static char*
984 : 2 : check_lexically_relative_dot_alloc(ZixAllocator* const allocator)
985 : : {
986 : 2 : return zix_path_lexically_relative(allocator, "", ".");
987 : : }
988 : :
989 : : static void
990 : 8 : check_failed_alloc(const AllocCheckFunc check)
991 : : {
992 : 8 : ZixFailingAllocator allocator = zix_failing_allocator();
993 : :
994 : : // Successfully perform check to count the number of allocations
995 : : {
996 : 8 : char* const result = check(&allocator.base);
997 [ - + ]: 8 : assert(result);
998 : 8 : zix_free(&allocator.base, result);
999 : : }
1000 : :
1001 : : // Test that each allocation failing is handled gracefully
1002 : 8 : const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0);
1003 [ + + ]: 16 : for (size_t i = 0U; i < n_new_allocs; ++i) {
1004 : 8 : zix_failing_allocator_reset(&allocator, i);
1005 [ - + ]: 8 : assert(!check(&allocator.base));
1006 : : }
1007 : 8 : }
1008 : :
1009 : : static void
1010 : 1 : test_failed_alloc(void)
1011 : : {
1012 : 1 : check_failed_alloc(check_join_alloc);
1013 : 1 : check_failed_alloc(check_join_empty_alloc);
1014 : 1 : check_failed_alloc(check_preferred_alloc);
1015 : 1 : check_failed_alloc(check_lexically_normal_alloc);
1016 : 1 : check_failed_alloc(check_lexically_normal_empty_alloc);
1017 : 1 : check_failed_alloc(check_lexically_relative_alloc);
1018 : 1 : check_failed_alloc(check_lexically_relative_equal_alloc);
1019 : 1 : check_failed_alloc(check_lexically_relative_dot_alloc);
1020 : 1 : }
1021 : :
1022 : : int
1023 : 1 : main(void)
1024 : : {
1025 : 1 : test_path_root_name();
1026 : 1 : test_path_root();
1027 : 1 : test_path_parent();
1028 : 1 : test_path_filename();
1029 : 1 : test_path_stem();
1030 : 1 : test_path_extension();
1031 : 1 : test_path_is_absolute();
1032 : 1 : test_null_queries();
1033 : :
1034 : 1 : test_path_join();
1035 : 1 : test_path_preferred();
1036 : 1 : test_path_lexically_normal();
1037 : 1 : test_path_lexically_relative();
1038 : :
1039 : 1 : test_failed_alloc();
1040 : :
1041 : 1 : return 0;
1042 : : }
|