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