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.
17 #include <sys/queue.h>
26 #include "got_error.h"
30 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
37 test_printf(const char *fmt, ...)
52 const struct path_cmp_test {
62 { "//foo", "/bar", 1 },
63 { "/foo", "/bar", 1 },
65 { "/foo/sub", "/bar", 1 },
66 { "/foo", "/bar/sub", 1 },
67 { "/foo/", "/bar", 1 },
68 { "/foo", "/bar/", 1 },
69 { "/foo/", "/bar/", 1 },
70 { "/bar/", "/bar/", 0 },
71 { "/bar/", "/bar", 0 },
72 { "//bar//", "/bar/", 0 },
73 { "//bar//", "/bar////", 0 },
74 { "/bar/sub", "/bar.", -1 },
75 { "/bar/sub", "/bar/", 1 },
76 { "/bar/sub/", "/bar///", 1 },
77 { "/bar/sub/sub2", "/bar/", 1 },
78 { "/bar/sub/sub2", "/bar", 1 },
79 { "/bar.sub.sub2", "/bar", 1 },
80 { "/bar/sub/sub2", "/bar.c", -1 },
84 for (i = 0; i < nitems(test_data); i++) {
85 const char *path1 = test_data[i].path1;
86 const char *path2 = test_data[i].path2;
87 int expected = test_data[i].expected;
88 int cmp = got_path_cmp(path1, path2,
89 strlen(path1), strlen(path2));
91 if (cmp != expected) {
92 test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
93 i, path1, path2, cmp, expected);
101 const char *path_list_input[] = {
102 "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
103 "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
104 "/foo/sub", "/foo/", "/foo/", "x/a",
106 const char *path_list_expected[] = {
120 /* If inserting pathlist_input in reverse the result is slightly different. */
121 const char *path_list_expected_reverse[] = {
139 const struct got_error *err = NULL;
140 struct got_pathlist_head paths;
141 struct got_pathlist_entry *pe;
145 for (i = 0; i < nitems(path_list_input); i++) {
146 err = got_pathlist_insert(NULL, &paths, path_list_input[i],
149 test_printf("%s\n", __func__, err->msg);
155 TAILQ_FOREACH(pe, &paths, entry) {
156 test_printf("'%s' -- '%s'\n", pe->path, path_list_expected[i]);
157 if (i >= nitems(path_list_expected)) {
158 test_printf("too many elements on list\n");
161 if (strcmp(pe->path, path_list_expected[i]) != 0) {
162 test_printf("unordered elements on list\n");
168 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
173 path_list_reverse_input(void)
175 const struct got_error *err = NULL;
176 struct got_pathlist_head paths;
177 struct got_pathlist_entry *pe;
181 for (i = nitems(path_list_input); i > 0;) {
182 err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
185 test_printf("%s\n", __func__, err->msg);
191 TAILQ_FOREACH(pe, &paths, entry) {
192 test_printf("'%s' -- '%s'\n", pe->path,
193 path_list_expected_reverse[i]);
194 if (i >= nitems(path_list_expected_reverse)) {
195 test_printf("too many elements on list\n");
198 if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
199 test_printf("unordered elements on list\n");
205 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
209 #define RUN_TEST(expr, name) \
210 { test_ok = (expr); \
211 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
212 failure = (failure || !test_ok); }
217 fprintf(stderr, "usage: path_test [-v] [-q]\n");
221 main(int argc, char *argv[])
223 int test_ok = 0, failure = 0;
227 if (pledge("stdio", NULL) == -1)
231 while ((ch = getopt(argc, argv, "qv")) != -1) {
249 RUN_TEST(path_cmp(), "path_cmp");
250 RUN_TEST(path_list(), "path_list");
251 RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
253 return failure ? 1 : 0;