2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include "got_error.h"
25 #include "got_lib_path.h"
28 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
34 test_printf(char *fmt, ...)
49 struct path_cmp_test {
59 { "//foo", "/bar", -1 },
60 { "/foo", "/bar", 1 },
61 { "/foo/sub", "/bar", 1 },
62 { "/foo", "/bar/sub", 1 },
63 { "/foo/", "/bar", 1 },
64 { "/foo", "/bar/", 1 },
65 { "/foo/", "/bar/", 1 },
66 { "/bar/", "/bar/", 0 },
67 { "/bar/sub", "/bar/", 1 },
68 { "/bar/sub/sub2", "/bar/", 1 },
69 { "/bar/sub/sub2", "/bar", 1 },
70 { "/bar.sub.sub2", "/bar", 1 },
71 { "/bar/sub/sub2", "/bar.c", -1 },
75 for (i = 0; i < nitems(test_data); i++) {
76 const char *path1 = test_data[i].path1;
77 const char *path2 = test_data[i].path2;
78 int expected = test_data[i].expected;
79 int cmp = got_path_cmp(path1, path2);
81 if (cmp != expected) {
82 test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
83 i, path1, path2, cmp, expected);
91 #define RUN_TEST(expr, name) \
93 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
94 failure = (failure || !test_ok); }
99 fprintf(stderr, "usage: path_test [-v]\n");
103 main(int argc, char *argv[])
105 int test_ok = 0, failure = 0;
109 if (pledge("stdio", NULL) == -1)
113 while ((ch = getopt(argc, argv, "v")) != -1) {
126 RUN_TEST(path_cmp(), "path_cmp");
128 return failure ? 1 : 0;