Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <err.h>
23 #include "got_error.h"
25 #include "got_lib_path.h"
27 #ifndef nitems
28 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
29 #endif
31 static int verbose;
33 void
34 test_printf(char *fmt, ...)
35 {
36 va_list ap;
38 if (!verbose)
39 return;
41 va_start(ap, fmt);
42 vprintf(fmt, ap);
43 va_end(ap);
44 }
46 static int
47 path_cmp(void)
48 {
49 struct path_cmp_test {
50 const char *path1;
51 const char *path2;
52 int expected;
53 } test_data[] = {
54 { "", "", 0 },
55 { "/", "/", 0 },
56 { "/a", "/b", -1 },
57 { "x/a", "x.a", -1 },
58 { "x.a", "x/a", 1 },
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 },
72 };
73 int i;
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);
84 return 0;
85 }
86 }
88 return 1;
89 }
91 #define RUN_TEST(expr, name) \
92 { test_ok = (expr); \
93 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
94 failure = (failure || !test_ok); }
96 void
97 usage(void)
98 {
99 fprintf(stderr, "usage: path_test [-v]\n");
102 int
103 main(int argc, char *argv[])
105 int test_ok = 0, failure = 0;
106 int ch;
108 #ifndef PROFILE
109 if (pledge("stdio", NULL) == -1)
110 err(1, "pledge");
111 #endif
113 while ((ch = getopt(argc, argv, "v")) != -1) {
114 switch (ch) {
115 case 'v':
116 verbose = 1;
117 break;
118 default:
119 usage();
120 return 1;
123 argc -= optind;
124 argv += optind;
126 RUN_TEST(path_cmp(), "path_cmp");
128 return failure ? 1 : 0;