Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/io/path.cpp

raw
1 /*
2 * Copyright (C) 2024 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2024 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 9 февр. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/test-fw/utest.h>
23 #include <lsp-plug.in/common/status.h>
24 #include <lsp-plug.in/io/Path.h>
25 #include <lsp-plug.in/stdlib/string.h>
26
27 #ifdef PLATFORM_WINDOWS
28 #define TEST_PATH1 "C:\\Windows\\system32"
29 #define TEST_PATH2 "C:\\Windows\\system\\lib"
30 #define TEST_PATH2_FIRST "C:\\Windows\\system"
31 #define TEST_PATH2_LAST "lib"
32 #define TEST_PATH3 "C:\\Windows\\system\\bin"
33 #define TEST_PATH3_FIRST "C:\\"
34 #define TEST_PATH3_LAST "Windows\\system\\bin"
35 #define TEST_PATH4 "C:\\Windows\\system"
36 #define TEST_PATH4_LAST "system"
37 #define TEST_PATH5 "C:\\Windows"
38 #define TEST_ROOT "C:\\"
39 #else
40 #define TEST_PATH1 "/usr/local/bin"
41 #define TEST_PATH2 "/usr/share/local/lib"
42 #define TEST_PATH2_FIRST "/usr/share/local"
43 #define TEST_PATH2_LAST "lib"
44 #define TEST_PATH3 "/usr/share/local/bin"
45 #define TEST_PATH3_FIRST "/"
46 #define TEST_PATH3_LAST "usr/share/local/bin"
47 #define TEST_PATH4 "/usr/share/local"
48 #define TEST_PATH4_LAST "local"
49 #define TEST_PATH5 "/usr"
50 #define TEST_ROOT "/"
51 #endif
52
53 using namespace lsp;
54 using namespace lsp::io;
55
56 static char *cnull = NULL;
57 static LSPString *snull = NULL;
58 static Path *pnull = NULL;
59
60 static const char *cpaths[] =
61 {
62 #if !defined(PLATFORM_WINDOWS)
63 ".", "",
64 "./", "",
65 "..", "",
66 "../", "",
67 "./.././a", "a",
68 "a/b/c/../../d/./", "a/d",
69 "a///b/c/.//../../d/./", "a/d",
70 "a/b/c/../../../d/e/./f/", "d/e/f",
71
72 "//", "/",
73 "/.", "/",
74 "/..", "/",
75 "/./", "/",
76 "/../", "/",
77 "/a/b/c/../../d/./", "/a/d",
78 "/a///b/c/.//../../d/./", "/a/d",
79 "/a/b/c/../../../d/e/./f/", "/d/e/f",
80 "/../a/b/c/../../d/./", "/a/d",
81 "/../a///b/c/.//../../d/./", "/a/d",
82 "/./../a/b/c/../../../d/e/./f/", "/d/e/f",
83 #else
84 ".", "",
85 ".\\", "",
86 "..", "",
87 "..\\", "",
88 ".\\..\\.\\a", "a",
89 "a\\b\\c\\..\\..\\d\\.\\", "a\\d",
90 "a\\\\\\b\\c\\.\\\\..\\..\\d\\.\\", "a\\d",
91 "a\\b\\c\\..\\..\\..\\d\\e\\.\\f\\", "d\\e\\f",
92
93 "C:\\\\", "C:\\",
94 "C:\\.", "C:\\",
95 "C:\\..", "C:\\",
96 "C:\\.\\", "C:\\",
97 "C:\\..\\", "C:\\",
98 "C:\\a\\b\\c\\..\\..\\d\\.\\", "C:\\a\\d",
99 "C:\\a\\\\\\b\\c\\.\\\\..\\..\\d\\.\\", "C:\\a\\d",
100 "C:\\a\\b\\c\\..\\..\\..\\d\\e\\.\\f\\", "C:\\d\\e\\f",
101 "C:\\..\\a\\b\\c\\..\\..\\d\\.\\", "C:\\a\\d",
102 "C:\\..\\a\\\\\\b\\c\\.\\\\..\\..\\d\\.\\", "C:\\a\\d",
103 "C:\\.\\..\\a\\b\\c\\..\\..\\..\\d\\e\\.\\f\\", "C:\\d\\e\\f",
104 #endif
105
106 NULL, NULL
107 };
108
109 UTEST_BEGIN("runtime.io", path)
110
111 // status_t get_parent(char *path, size_t maxlen) const;
112 // status_t set_parent(const char *path);
113 // status_t get_parent(LSPString *path) const;
114 // status_t get_parent(Path *path) const;
115 // status_t set_parent(LSPString *path);
116 // status_t set_parent(Path *path);
117 void test_get_set_parent()
118 {
119 Path bp, dp;
120 char path[PATH_MAX];
121 LSPString spath, dpath;
122
123 printf("Testing get_parent and set_parent...\n");
124
125 UTEST_ASSERT(bp.set(TEST_PATH2) == STATUS_OK);
126 UTEST_ASSERT(bp.get_parent(path, PATH_MAX) == STATUS_OK);
127 UTEST_ASSERT(bp.get_parent(path, 2) == STATUS_TOO_BIG);
128 UTEST_ASSERT(bp.get_parent(cnull, PATH_MAX) == STATUS_BAD_ARGUMENTS);
129 UTEST_ASSERT(strcmp(path, TEST_PATH4) == 0);
130
131 UTEST_ASSERT(spath.set_utf8(TEST_PATH4));
132 UTEST_ASSERT(bp.get_parent(snull) == STATUS_BAD_ARGUMENTS);
133 UTEST_ASSERT(bp.get_parent(&dpath) == STATUS_OK);
134 UTEST_ASSERT(dpath.equals(&spath));
135 UTEST_ASSERT(bp.get_parent(&dp) == STATUS_OK);
136 UTEST_ASSERT(dp.equals(&spath));
137
138 UTEST_ASSERT(bp.set(TEST_ROOT) == STATUS_OK);
139 UTEST_ASSERT(bp.get_parent(path, PATH_MAX) == STATUS_NOT_FOUND);
140 UTEST_ASSERT(bp.get_parent(&dpath) == STATUS_NOT_FOUND);
141 UTEST_ASSERT(bp.get_parent(&dp) == STATUS_NOT_FOUND);
142
143 UTEST_ASSERT(bp.set("bin") == STATUS_OK);
144 UTEST_ASSERT(bp.get_parent(path, PATH_MAX) == STATUS_NOT_FOUND);
145 UTEST_ASSERT(bp.get_parent(&dpath) == STATUS_NOT_FOUND);
146 UTEST_ASSERT(bp.get_parent(&dp) == STATUS_NOT_FOUND);
147 }
148
149 // status_t set_last(const char *path);
150 // status_t get_last(char *path, size_t maxlen) const;
151 // status_t set_last(const LSPString *path);
152 // status_t get_last(LSPString *path) const;
153 // status_t set_last(const Path *path);
154 // status_t get_last(Path *path) const;
155 void test_get_set_last()
156 {
157 Path bp, dp, p;
158 char path[PATH_MAX];
159 LSPString spath, dpath, t1;
160
161 printf("Testing get_last and set_last...\n");
162
163 UTEST_ASSERT(dp.set(TEST_PATH2) == STATUS_OK);
164 UTEST_ASSERT(dp.set_last("bin") == STATUS_OK);
165 UTEST_ASSERT(dp.set_last(cnull) == STATUS_BAD_ARGUMENTS);
166 UTEST_ASSERT(dp.equals(TEST_PATH3));
167 UTEST_ASSERT(dp.get_last(path, PATH_MAX) == STATUS_OK);
168 UTEST_ASSERT(strcmp(path, "bin") == 0);
169 UTEST_ASSERT(dp.set_last("") == STATUS_OK);
170 UTEST_ASSERT(dp.equals(TEST_PATH4));
171
172 UTEST_ASSERT(bp.set_last("bin") == STATUS_OK);
173 UTEST_ASSERT(bp.set_last(cnull) == STATUS_BAD_ARGUMENTS);
174 UTEST_ASSERT(bp.equals("bin"));
175 UTEST_ASSERT(bp.get_last(cnull, PATH_MAX) == STATUS_BAD_ARGUMENTS);
176 UTEST_ASSERT(bp.get_last(path, PATH_MAX) == STATUS_OK);
177 UTEST_ASSERT(strcmp(path, "bin") == 0);
178 UTEST_ASSERT(bp.set_last("") == STATUS_OK);
179 UTEST_ASSERT(bp.is_empty());
180
181 UTEST_ASSERT(dpath.set_utf8("bin"));
182 dp.clear();
183 UTEST_ASSERT(spath.set_utf8(TEST_PATH2));
184 UTEST_ASSERT(bp.set(&spath) == STATUS_OK);
185 UTEST_ASSERT(bp.set_last(&dpath) == STATUS_OK);
186 UTEST_ASSERT(bp.equals(TEST_PATH3));
187 UTEST_ASSERT(bp.get_last(&t1) == STATUS_OK);
188 UTEST_ASSERT(bp.get_last(snull) == STATUS_BAD_ARGUMENTS);
189 UTEST_ASSERT(t1.equals(&dpath));
190
191 UTEST_ASSERT(dp.set_last(&dpath) == STATUS_OK);
192 UTEST_ASSERT(dp.equals("bin"));
193 UTEST_ASSERT(dp.get_last(&spath) == STATUS_OK);
194 UTEST_ASSERT(t1.equals(&spath));
195
196 UTEST_ASSERT(bp.set(TEST_PATH2) == STATUS_OK);
197 UTEST_ASSERT(dp.set("bin") == STATUS_OK);
198 UTEST_ASSERT(bp.set_last(&dp) == STATUS_OK);
199 UTEST_ASSERT(bp.set_last(pnull) == STATUS_BAD_ARGUMENTS);
200 UTEST_ASSERT(bp.equals(TEST_PATH3));
201 UTEST_ASSERT(bp.get_last(&p) == STATUS_OK);
202 UTEST_ASSERT(p.equals("bin"));
203 UTEST_ASSERT(bp.set(TEST_ROOT) == STATUS_OK);
204 UTEST_ASSERT(bp.get_last(&p) == STATUS_OK);
205 UTEST_ASSERT(p.is_empty());
206 }
207
208 // status_t set(const char *path);
209 // status_t set(const LSPString *path);
210 // status_t set(const Path *path);
211 // status_t get(char *path, size_t maxlen) const;
212 // status_t get(LSPString *path) const;
213 // status_t get(Path *path) const;
214 void test_get_set()
215 {
216 Path p, dp, bp;
217 char path[PATH_MAX];
218 LSPString spath, dpath;
219
220 printf("Testing get and set...\n");
221
222 UTEST_ASSERT(p.set(TEST_PATH1) == STATUS_OK);
223 UTEST_ASSERT(p.set(cnull) == STATUS_BAD_ARGUMENTS);
224 UTEST_ASSERT(p.get(cnull, PATH_MAX) == STATUS_BAD_ARGUMENTS);
225 UTEST_ASSERT(p.get(path, PATH_MAX) == STATUS_OK);
226 UTEST_ASSERT(p.get(path, 10) == STATUS_TOO_BIG);
227 UTEST_ASSERT(strcmp(path, TEST_PATH1) == 0);
228
229 UTEST_ASSERT(spath.set_utf8(TEST_PATH2));
230 UTEST_ASSERT(p.set(&spath) == STATUS_OK);
231 UTEST_ASSERT(p.set(snull) == STATUS_BAD_ARGUMENTS);
232 UTEST_ASSERT(p.get(snull) == STATUS_BAD_ARGUMENTS);
233 UTEST_ASSERT(p.get(&dpath) == STATUS_OK);
234 UTEST_ASSERT(dpath.equals(&spath));
235
236
237 UTEST_ASSERT(dp.set(&p) == STATUS_OK);
238 UTEST_ASSERT(dp.set(pnull) == STATUS_BAD_ARGUMENTS);
239 UTEST_ASSERT(dp.get(&bp) == STATUS_OK);
240 UTEST_ASSERT(dp.get(pnull) == STATUS_BAD_ARGUMENTS);
241 UTEST_ASSERT(bp.equals(&dp));
242 UTEST_ASSERT(bp.equals(&p));
243 UTEST_ASSERT(bp.equals(TEST_PATH2));
244 UTEST_ASSERT(bp.equals(&spath));
245 }
246
247 // status_t concat(const char *path);
248 // status_t concat(LSPString *path);
249 // status_t concat(Path *path);
250 void test_concat()
251 {
252 Path p;
253 LSPString sstr;
254 Path spath;
255
256 printf("Testing concat...\n");
257
258 UTEST_ASSERT(p.set(TEST_PATH4) == STATUS_OK);
259 UTEST_ASSERT(p.concat(FILE_SEPARATOR_S "bin") == STATUS_OK);
260 UTEST_ASSERT(p.concat(cnull) == STATUS_BAD_ARGUMENTS);
261 UTEST_ASSERT(p.equals(TEST_PATH3));
262
263 p.clear();
264 UTEST_ASSERT(p.concat(TEST_ROOT) == STATUS_OK);
265 UTEST_ASSERT(p.is_root());
266
267 p.clear();
268 UTEST_ASSERT(p.set(TEST_PATH4) == STATUS_OK);
269 UTEST_ASSERT(sstr.set_utf8(FILE_SEPARATOR_S "bin"));
270 UTEST_ASSERT(p.concat(&sstr) == STATUS_OK);
271 UTEST_ASSERT(p.concat(snull) == STATUS_BAD_ARGUMENTS);
272 UTEST_ASSERT(p.equals(TEST_PATH3));
273
274 p.clear();
275 UTEST_ASSERT(p.set(TEST_PATH4) == STATUS_OK);
276 UTEST_ASSERT(spath.set(&sstr) == STATUS_OK);
277 UTEST_ASSERT(p.concat(&spath) == STATUS_OK);
278 UTEST_ASSERT(p.concat(pnull) == STATUS_BAD_ARGUMENTS);
279 UTEST_ASSERT(p.equals(TEST_PATH3));
280
281 p.clear();
282 UTEST_ASSERT(sstr.set_utf8(TEST_ROOT));
283 UTEST_ASSERT(p.concat(&sstr) == STATUS_OK);
284 UTEST_ASSERT(p.is_root());
285
286 p.clear();
287 UTEST_ASSERT(spath.set(&sstr) == STATUS_OK);
288 UTEST_ASSERT(p.concat(&spath) == STATUS_OK);
289 UTEST_ASSERT(p.is_root());
290 }
291
292 // status_t append_child(const char *path);
293 // status_t append_child(LSPString *path);
294 // status_t append_child(Path *path);
295 void test_append_child()
296 {
297 Path p;
298 LSPString sstr;
299 Path spath;
300
301 printf("Testing append_child...\n");
302
303 UTEST_ASSERT(p.set(TEST_PATH4) == STATUS_OK);
304 UTEST_ASSERT(p.append_child("bin") == STATUS_OK);
305 UTEST_ASSERT(p.append_child(cnull) == STATUS_BAD_ARGUMENTS);
306 UTEST_ASSERT(p.equals(TEST_PATH3));
307
308 p.clear();
309 UTEST_ASSERT(sstr.set_utf8("bin"));
310 UTEST_ASSERT(p.set(TEST_PATH4) == STATUS_OK);
311 UTEST_ASSERT(p.append_child(&sstr) == STATUS_OK);
312 UTEST_ASSERT(p.append_child(snull) == STATUS_BAD_ARGUMENTS);
313 UTEST_ASSERT(p.equals(TEST_PATH3));
314
315 p.clear();
316 UTEST_ASSERT(p.set(TEST_PATH4) == STATUS_OK);
317 UTEST_ASSERT(spath.set(&sstr) == STATUS_OK);
318 UTEST_ASSERT(p.append_child(&spath) == STATUS_OK);
319 UTEST_ASSERT(p.append_child(pnull) == STATUS_BAD_ARGUMENTS);
320 UTEST_ASSERT(p.equals(TEST_PATH3));
321
322 UTEST_ASSERT(sstr.set_utf8(TEST_ROOT));
323 UTEST_ASSERT(spath.set(&sstr) == STATUS_OK);
324 UTEST_ASSERT(p.append_child(TEST_ROOT) == STATUS_INVALID_VALUE);
325 UTEST_ASSERT(p.append_child(&sstr) == STATUS_INVALID_VALUE);
326 UTEST_ASSERT(p.append_child(&spath) == STATUS_INVALID_VALUE);
327
328 sstr.clear();
329 spath.clear();
330 UTEST_ASSERT(p.append_child("") == STATUS_OK);
331 UTEST_ASSERT(p.append_child(&sstr) == STATUS_OK);
332 UTEST_ASSERT(p.append_child(&spath) == STATUS_OK);
333 UTEST_ASSERT(p.equals(TEST_PATH3));
334 }
335
336 // status_t remove_last();
337 // status_t remove_last(char *path, size_t maxlen);
338 // status_t remove_last(LSPString *path);
339 // status_t remove_last(Path *path);
340 void test_remove_last()
341 {
342 Path p;
343 char path[PATH_MAX];
344 LSPString sstr, xstr;
345 Path spath;
346
347 printf("Testing remove_last...\n");
348
349 UTEST_ASSERT(p.set(TEST_PATH2) == STATUS_OK);
350 UTEST_ASSERT(p.remove_last() == STATUS_OK);
351 UTEST_ASSERT(p.equals(TEST_PATH4));
352
353 UTEST_ASSERT(p.set(TEST_PATH5) == STATUS_OK);
354 UTEST_ASSERT(p.remove_last() == STATUS_OK);
355 UTEST_ASSERT(p.equals(TEST_ROOT));
356
357 UTEST_ASSERT(p.set(TEST_ROOT) == STATUS_OK);
358 UTEST_ASSERT(p.remove_last() == STATUS_OK);
359 UTEST_ASSERT(p.equals(TEST_ROOT));
360
361 UTEST_ASSERT(p.set(TEST_PATH2) == STATUS_OK);
362 UTEST_ASSERT(p.remove_last(path, PATH_MAX) == STATUS_OK);
363 UTEST_ASSERT(p.remove_last(cnull, PATH_MAX) == STATUS_BAD_ARGUMENTS);
364 UTEST_ASSERT(p.remove_last(path, 3) == STATUS_TOO_BIG);
365 UTEST_ASSERT(p.equals(TEST_PATH2_FIRST));
366 UTEST_ASSERT(strcmp(path, TEST_PATH2_LAST) == 0);
367
368 UTEST_ASSERT(xstr.set_utf8(TEST_PATH2_LAST));
369 UTEST_ASSERT(p.set(TEST_PATH2) == STATUS_OK);
370 UTEST_ASSERT(p.remove_last(&sstr) == STATUS_OK);
371 UTEST_ASSERT(p.remove_last(snull) == STATUS_BAD_ARGUMENTS);
372 UTEST_ASSERT(p.equals(TEST_PATH2_FIRST));
373 UTEST_ASSERT(sstr.equals(&xstr));
374
375 UTEST_ASSERT(p.set(TEST_PATH2) == STATUS_OK);
376 UTEST_ASSERT(p.remove_last(&spath) == STATUS_OK);
377 UTEST_ASSERT(p.remove_last(pnull) == STATUS_BAD_ARGUMENTS);
378 UTEST_ASSERT(p.equals(TEST_PATH2_FIRST));
379 UTEST_ASSERT(spath.equals(&xstr));
380 }
381
382 // status_t without_last(char *path, size_t maxlen);
383 // status_t without_last(LSPString *path);
384 // status_t without_last(Path *path);
385 void test_without_last()
386 {
387 Path p;
388 char path[PATH_MAX];
389 LSPString sstr, xstr;
390 Path spath;
391
392 printf("Testing without_last...\n");
393
394 UTEST_ASSERT(p.set(TEST_PATH2) == STATUS_OK);
395 UTEST_ASSERT(p.without_last(path, PATH_MAX) == STATUS_OK);
396 UTEST_ASSERT(p.without_last(cnull, PATH_MAX) == STATUS_BAD_ARGUMENTS);
397 UTEST_ASSERT(p.without_last(path, 3) == STATUS_TOO_BIG);
398 UTEST_ASSERT(p.equals(TEST_PATH2));
399 UTEST_ASSERT(strcmp(path, TEST_PATH4) == 0);
400
401 UTEST_ASSERT(xstr.set_utf8(TEST_PATH4));
402 UTEST_ASSERT(p.set(TEST_PATH2) == STATUS_OK);
403 UTEST_ASSERT(p.without_last(&sstr) == STATUS_OK);
404 UTEST_ASSERT(p.without_last(snull) == STATUS_BAD_ARGUMENTS);
405 UTEST_ASSERT(p.equals(TEST_PATH2));
406 UTEST_ASSERT(sstr.equals(&xstr));
407
408 UTEST_ASSERT(p.set(TEST_PATH2) == STATUS_OK);
409 UTEST_ASSERT(p.without_last(&spath) == STATUS_OK);
410 UTEST_ASSERT(p.without_last(pnull) == STATUS_BAD_ARGUMENTS);
411 UTEST_ASSERT(p.equals(TEST_PATH2));
412 UTEST_ASSERT(spath.equals(&xstr));
413 }
414
415 // status_t remove_first();
416 // status_t remove_first(char *path, size_t maxlen);
417 // status_t remove_first(LSPString *path);
418 // status_t remove_first(Path *path);
419 void test_remove_first()
420 {
421 Path p;
422 char path[PATH_MAX];
423 LSPString sstr, xstr;
424 Path spath;
425
426 printf("Testing remove_first...\n");
427
428 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
429 UTEST_ASSERT(p.remove_first() == STATUS_OK);
430 UTEST_ASSERT(p.equals(TEST_PATH3_LAST));
431
432 UTEST_ASSERT(p.set(TEST_ROOT) == STATUS_OK);
433 UTEST_ASSERT(p.remove_first() == STATUS_NOT_FOUND);
434 UTEST_ASSERT(p.equals(TEST_ROOT));
435
436 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
437 UTEST_ASSERT(p.remove_first(path, PATH_MAX) == STATUS_OK);
438 UTEST_ASSERT(p.remove_first(cnull, PATH_MAX) == STATUS_BAD_ARGUMENTS);
439 UTEST_ASSERT(p.remove_first(path, 3) == STATUS_TOO_BIG);
440 UTEST_ASSERT(p.equals(TEST_PATH3_LAST));
441 UTEST_ASSERT(strcmp(path, TEST_PATH3_FIRST) == 0);
442
443 UTEST_ASSERT(xstr.set_utf8(TEST_PATH3_FIRST));
444 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
445 UTEST_ASSERT(p.remove_first(&sstr) == STATUS_OK);
446 UTEST_ASSERT(p.remove_first(snull) == STATUS_BAD_ARGUMENTS);
447 UTEST_ASSERT(p.equals(TEST_PATH3_LAST));
448 UTEST_ASSERT(sstr.equals(&xstr));
449
450 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
451 UTEST_ASSERT(p.remove_first(&spath) == STATUS_OK);
452 UTEST_ASSERT(p.remove_first(pnull) == STATUS_BAD_ARGUMENTS);
453 UTEST_ASSERT(p.equals(TEST_PATH3_LAST));
454 UTEST_ASSERT(spath.equals(&xstr));
455 }
456
457 // status_t without_first(char *path, size_t maxlen);
458 // status_t without_first(LSPString *path);
459 // status_t without_first(Path *path);
460 void test_without_first()
461 {
462 Path p;
463 char path[PATH_MAX];
464 LSPString sstr, xstr;
465 Path spath;
466
467 printf("Testing without_first...\n");
468
469 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
470 UTEST_ASSERT(p.without_first(path, PATH_MAX) == STATUS_OK);
471 UTEST_ASSERT(p.without_first(cnull, PATH_MAX) == STATUS_BAD_ARGUMENTS);
472 UTEST_ASSERT(p.without_first(path, 1) == STATUS_TOO_BIG);
473 UTEST_ASSERT(p.equals(TEST_PATH3));
474 UTEST_ASSERT(strcmp(path, TEST_PATH3_LAST) == 0);
475
476 UTEST_ASSERT(xstr.set_utf8(TEST_PATH3_LAST));
477 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
478 UTEST_ASSERT(p.without_first(&sstr) == STATUS_OK);
479 UTEST_ASSERT(p.without_first(snull) == STATUS_BAD_ARGUMENTS);
480 UTEST_ASSERT(p.equals(TEST_PATH3));
481 UTEST_ASSERT(sstr.equals(&xstr));
482
483 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
484 UTEST_ASSERT(p.without_first(&spath) == STATUS_OK);
485 UTEST_ASSERT(p.without_first(pnull) == STATUS_BAD_ARGUMENTS);
486 UTEST_ASSERT(p.equals(TEST_PATH3));
487 UTEST_ASSERT(spath.equals(&xstr));
488 }
489
490 // status_t remove_base(const char *path);
491 // status_t remove_base(const LSPString *path);
492 // status_t remove_base(const Path *path);
493 void test_remove_base()
494 {
495 Path p, xp;
496 LSPString xs;
497
498 printf("Testing remove_base...\n");
499
500 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
501 UTEST_ASSERT(p.remove_base(TEST_PATH4) == STATUS_OK);
502 UTEST_ASSERT(p.remove_base(cnull) == STATUS_BAD_ARGUMENTS);
503 UTEST_ASSERT(p.equals("bin"));
504
505 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
506 UTEST_ASSERT(p.remove_base(TEST_PATH3) == STATUS_OK);
507 UTEST_ASSERT(p.is_empty());
508
509 UTEST_ASSERT(xs.set_utf8(TEST_PATH4));
510 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
511 UTEST_ASSERT(p.remove_base(&xs) == STATUS_OK);
512 UTEST_ASSERT(p.remove_base(snull) == STATUS_BAD_ARGUMENTS);
513 UTEST_ASSERT(p.equals("bin"));
514
515 UTEST_ASSERT(xs.set_utf8(TEST_PATH3));
516 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
517 UTEST_ASSERT(p.remove_base(&xs) == STATUS_OK);
518 UTEST_ASSERT(p.is_empty());
519
520 UTEST_ASSERT(xp.set(TEST_PATH4) == STATUS_OK);
521 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
522 UTEST_ASSERT(p.remove_base(&xp) == STATUS_OK);
523 UTEST_ASSERT(p.remove_base(pnull) == STATUS_BAD_ARGUMENTS);
524 UTEST_ASSERT(p.equals("bin"));
525
526 UTEST_ASSERT(xp.set(TEST_PATH3) == STATUS_OK);
527 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
528 UTEST_ASSERT(p.remove_base(&xp) == STATUS_OK);
529 UTEST_ASSERT(p.is_empty());
530
531 UTEST_ASSERT(p.set(TEST_PATH3) == STATUS_OK);
532 UTEST_ASSERT(p.remove_base() == STATUS_OK);
533 UTEST_ASSERT(p.equals("bin"));
534
535 UTEST_ASSERT(p.set(TEST_PATH4) == STATUS_OK);
536 UTEST_ASSERT(p.remove_base() == STATUS_OK);
537 UTEST_ASSERT(p.equals(TEST_PATH4_LAST));
538 }
539
540 // void clear()
541 // bool is_absolute() const;
542 // bool is_relative() const;
543 // bool is_empty() const
544 // bool is_root() const;
545 // status_t root();
546 void test_flags()
547 {
548 Path p;
549
550 printf("Testing flags...\n");
551
552 UTEST_ASSERT(!p.is_absolute());
553 UTEST_ASSERT(p.is_relative());
554 UTEST_ASSERT(p.is_empty());
555 UTEST_ASSERT(!p.is_root());
556 UTEST_ASSERT(p.root() == STATUS_BAD_STATE);
557
558 UTEST_ASSERT(p.set(TEST_ROOT) == STATUS_OK);
559 UTEST_ASSERT(p.is_absolute());
560 UTEST_ASSERT(!p.is_relative());
561 UTEST_ASSERT(!p.is_empty());
562 UTEST_ASSERT(p.is_root());
563 UTEST_ASSERT(p.root() == STATUS_OK);
564 UTEST_ASSERT(p.equals(TEST_ROOT));
565
566 UTEST_ASSERT(p.set(TEST_PATH1) == STATUS_OK);
567 UTEST_ASSERT(p.is_absolute());
568 UTEST_ASSERT(!p.is_relative());
569 UTEST_ASSERT(!p.is_empty());
570 UTEST_ASSERT(!p.is_root());
571 UTEST_ASSERT(p.root() == STATUS_OK);
572 UTEST_ASSERT(p.equals(TEST_ROOT));
573 UTEST_ASSERT(p.is_root());
574
575 UTEST_ASSERT(p.set("bin") == STATUS_OK);
576 UTEST_ASSERT(!p.is_absolute());
577 UTEST_ASSERT(p.is_relative());
578 UTEST_ASSERT(!p.is_empty());
579 UTEST_ASSERT(!p.is_root());
580 UTEST_ASSERT(p.root() == STATUS_BAD_STATE);
581 UTEST_ASSERT(!p.equals(TEST_ROOT));
582 UTEST_ASSERT(!p.is_root());
583 }
584
585 // status_t get_canonical(char *path, size_t maxlen) const;
586 // status_t get_canonical(LSPString *path) const;
587 // status_t get_canonical(Path *path) const;
588 // bool is_canonical() const;
589 // status_t canonicalize();
590 void test_canonical()
591 {
592 Path p, xp;
593 LSPString xs;
594 char path[PATH_MAX];
595
596 printf("Testing canonical paths...\n");
597
598 // UTEST_ASSERT(p.set(TEST_PATH1) == STATUS_OK);
599 // UTEST_ASSERT(p.is_canonical());
600 // UTEST_ASSERT(p.set(TEST_ROOT) == STATUS_OK);
601 // UTEST_ASSERT(p.is_canonical());
602 // p.clear();
603 // UTEST_ASSERT(p.is_canonical());
604
605 for (const char **cp=cpaths; *cp != NULL; cp += 2)
606 {
607 printf(" testing \"%s\" -> \"%s\"\n", cp[0], cp[1]);
608 UTEST_ASSERT(p.set(cp[0]) == STATUS_OK);
609 UTEST_ASSERT(!p.is_canonical());
610
611 UTEST_ASSERT(p.get_canonical(path, PATH_MAX) == STATUS_OK);
612 UTEST_ASSERT(p.get_canonical(cnull, PATH_MAX) == STATUS_BAD_ARGUMENTS);
613 UTEST_ASSERT_MSG(strcmp(path, cp[1]) == 0, "canonicalized: \"%s\" -> \"%s\"\n", p.get(), path);
614 UTEST_ASSERT(p.equals(cp[0]));
615
616 UTEST_ASSERT(p.get_canonical(&xp) == STATUS_OK);
617 UTEST_ASSERT(p.get_canonical(pnull) == STATUS_BAD_ARGUMENTS);
618 UTEST_ASSERT_MSG(xp.equals(cp[1]), "canonicalized: \"%s\" -> \"%s\"\n", p.get(), xp.get());
619 UTEST_ASSERT(p.equals(cp[0]));
620
621 UTEST_ASSERT(p.get_canonical(&xs) == STATUS_OK);
622 UTEST_ASSERT(p.get_canonical(snull) == STATUS_BAD_ARGUMENTS);
623 UTEST_ASSERT_MSG(xp.equals(&xs), "canonicalized: \"%s\" -> \"%s\"\n", p.get(), xs.get_utf8());
624 UTEST_ASSERT(p.equals(cp[0]));
625
626 UTEST_ASSERT_MSG(p.canonicalize() == STATUS_OK, "canonicalized: \"%s\" -> \"%s\"\n", cp[0], p.get());
627 UTEST_ASSERT(p.is_canonical());
628 UTEST_ASSERT(p.equals(cp[1]));
629 }
630 }
631
632 void test_dots()
633 {
634 struct dot_t
635 {
636 const char *path;
637 bool dot;
638 bool dotdot;
639 };
640
641 static const dot_t dots[] =
642 {
643 { "", false, false },
644 { ".", true, false },
645 { "..", false, true },
646 { "...", false, false },
647 { FILE_SEPARATOR_S "..", false, true },
648 { FILE_SEPARATOR_S ".", true, false },
649 { FILE_SEPARATOR_S "...", false, false },
650 { FILE_SEPARATOR_S, false, false },
651 { "." FILE_SEPARATOR_S "a", false, false },
652 { ".." FILE_SEPARATOR_S "a", false, false },
653 { "a" FILE_SEPARATOR_S ".", true, false },
654 { "a" FILE_SEPARATOR_S "..", false, true },
655 { "a" FILE_SEPARATOR_S "...", false, false },
656 { NULL, false, false }
657 };
658
659 io::Path p;
660 LSPString s;
661
662 for (const dot_t *d = dots; d->path != NULL; ++d)
663 {
664 printf("Testing \"%s\"\n", d->path);
665 UTEST_ASSERT(Path::is_dot(d->path) == d->dot);
666 UTEST_ASSERT(Path::is_dotdot(d->path) == d->dotdot);
667 UTEST_ASSERT(Path::is_dots(d->path) == (d->dot || d->dotdot));
668
669 UTEST_ASSERT(p.set(d->path) == STATUS_OK);
670 UTEST_ASSERT(p.is_dot() == d->dot);
671 UTEST_ASSERT(p.is_dotdot() == d->dotdot);
672 UTEST_ASSERT(p.is_dots() == (d->dot || d->dotdot));
673
674 UTEST_ASSERT(Path::is_dot(&p) == d->dot);
675 UTEST_ASSERT(Path::is_dotdot(&p) == d->dotdot);
676 UTEST_ASSERT(Path::is_dots(&p) == (d->dot || d->dotdot));
677
678 UTEST_ASSERT(s.set_utf8(d->path));
679 UTEST_ASSERT(Path::is_dot(&s) == d->dot);
680 UTEST_ASSERT(Path::is_dotdot(&s) == d->dotdot);
681 UTEST_ASSERT(Path::is_dots(&s) == (d->dot || d->dotdot));
682 }
683 }
684
685 void test_relative()
686 {
687 struct rel_t
688 {
689 const char *child;
690 const char *base;
691 status_t code;
692 const char *res;
693 };
694
695 static const rel_t paths[] =
696 {
697 { "", "", STATUS_NOT_FOUND, "" },
698 { "", "/", STATUS_NOT_FOUND, NULL },
699 { "/", "/", STATUS_OK, "" },
700 { "a", "a", STATUS_OK, "" },
701 { "abc", "a", STATUS_NOT_FOUND, NULL },
702 { "a", "abc", STATUS_NOT_FOUND, NULL },
703 { "/a", "/a", STATUS_OK, "" },
704 { "/a/b", "/a", STATUS_OK, "b" },
705 { "/a/b/c", "/a", STATUS_OK, "b/c" },
706 { "a", "b", STATUS_NOT_FOUND, NULL },
707 { "/a", "/b", STATUS_OK, "../a" },
708 { "/b", "/a", STATUS_OK, "../b" },
709 { "/a/b/c", "/a/b/d", STATUS_OK, "../c" },
710 { "/a/b", "/a/b/d", STATUS_OK, ".." },
711 { "/a/", "/a/b/d/", STATUS_OK, "../.." },
712 { "/a/x", "/a/b/d/", STATUS_OK, "../../x" },
713 { "/a/c/../b", "/a/../a/b/d", STATUS_OK, ".." },
714
715 { NULL, NULL, 0, NULL }
716 };
717
718 printf("Testing as_relative() methods...\n");
719
720 for (const rel_t *d = paths; d->child != NULL; ++d)
721 {
722 if (d->code == STATUS_OK)
723 printf("Testing \"%s\" - \"%s\" -> \"%s\" \n", d->child, d->base, d->res);
724 else
725 printf("Testing \"%s\" - \"%s\" -> error(%d) \n", d->child, d->base, int(d->code));
726
727 io::Path base, child, res;
728 UTEST_ASSERT(base.set(d->base) == STATUS_OK);
729 UTEST_ASSERT(child.set(d->child) == STATUS_OK);
730
731 status_t code = child.as_relative(&base);
732 UTEST_ASSERT_MSG(code == d->code, "Invalid code %d", int(code));
733 if (d->code == STATUS_OK)
734 {
735 UTEST_ASSERT(res.set(d->res) == STATUS_OK);
736 UTEST_ASSERT_MSG(child.equals(&res), "Returned path: %s", child.as_utf8());
737 }
738 }
739 }
740
741 void test_ext()
742 {
743 struct file_t
744 {
745 const char *path;
746 const char *noext;
747 const char *ext;
748 };
749
750 LSPString spath;
751 io::Path ipath;
752 char cpath[32];
753
754 static const file_t files[] =
755 {
756 { "", "", ""},
757 { "a", "a", ""},
758 { "long_file", "long_file", ""},
759 { "file.ext", "file", "ext"},
760 { ".config", "", "config"},
761 { "file.ext1.ext2", "file.ext1", "ext2"},
762
763 { "/path/a", "a", ""},
764 { "/path/long_file", "long_file", ""},
765 { "/path/file.ext", "file", "ext"},
766 { "/path/.config", "", "config"},
767 { "/path/file.ext1.ext2", "file.ext1", "ext2"},
768
769 { NULL, NULL, NULL }
770 };
771
772 printf("Testing get_ext() and get_noext() methods...\n");
773
774 for (const file_t *f = files; f->path != NULL; ++f)
775 {
776 io::Path tmp;
777 UTEST_ASSERT(tmp.set(f->path) == STATUS_OK);
778
779 // noext()
780 printf(" testing noext('%s') \n", f->path);
781 UTEST_ASSERT(tmp.get_last_noext(&spath) == STATUS_OK);
782 UTEST_ASSERT(tmp.get_last_noext(&ipath) == STATUS_OK);
783 UTEST_ASSERT(tmp.get_last_noext(cpath, sizeof(cpath)) == STATUS_OK);
784 UTEST_ASSERT(spath.equals_ascii(f->noext));
785 UTEST_ASSERT(ipath.as_string()->equals_ascii(f->noext));
786 UTEST_ASSERT(strcmp(cpath, f->noext) == 0);
787
788 // ext()
789 printf(" testing ext('%s') \n", f->path);
790 UTEST_ASSERT(tmp.get_ext(&spath) == STATUS_OK);
791 UTEST_ASSERT(tmp.get_ext(&ipath) == STATUS_OK);
792 UTEST_ASSERT(tmp.get_ext(cpath, sizeof(cpath)) == STATUS_OK);
793 UTEST_ASSERT(spath.equals_ascii(f->ext));
794 UTEST_ASSERT(ipath.as_string()->equals_ascii(f->ext));
795 UTEST_ASSERT(strcmp(cpath, f->ext) == 0);
796 }
797 }
798
799 UTEST_MAIN
800 {
801 test_get_set();
802 test_get_set_last();
803 test_get_set_parent();
804 test_concat();
805 test_append_child();
806 test_remove_last();
807 test_without_last();
808 test_remove_first();
809 test_without_first();
810 test_remove_base();
811 test_flags();
812 test_canonical();
813 test_dots();
814 test_relative();
815 test_ext();
816 }
817 UTEST_END;
818
819
820