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.
25 #include "got_error.h"
29 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
36 test_printf(char *fmt
, ...)
51 struct path_cmp_test
{
61 { "//foo", "/bar", 1 },
62 { "/foo", "/bar", 1 },
64 { "/foo/sub", "/bar", 1 },
65 { "/foo", "/bar/sub", 1 },
66 { "/foo/", "/bar", 1 },
67 { "/foo", "/bar/", 1 },
68 { "/foo/", "/bar/", 1 },
69 { "/bar/", "/bar/", 0 },
70 { "/bar/", "/bar", 0 },
71 { "//bar//", "/bar/", 0 },
72 { "//bar//", "/bar////", 0 },
73 { "/bar/sub", "/bar.", -1 },
74 { "/bar/sub", "/bar/", 1 },
75 { "/bar/sub/", "/bar///", 1 },
76 { "/bar/sub/sub2", "/bar/", 1 },
77 { "/bar/sub/sub2", "/bar", 1 },
78 { "/bar.sub.sub2", "/bar", 1 },
79 { "/bar/sub/sub2", "/bar.c", -1 },
83 for (i
= 0; i
< nitems(test_data
); i
++) {
84 const char *path1
= test_data
[i
].path1
;
85 const char *path2
= test_data
[i
].path2
;
86 int expected
= test_data
[i
].expected
;
87 int cmp
= got_path_cmp(path1
, path2
,
88 strlen(path1
), strlen(path2
));
90 if (cmp
!= expected
) {
91 test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
92 i
, path1
, path2
, cmp
, expected
);
100 const char *path_list_input
[] = {
101 "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
102 "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
103 "/foo/sub", "/foo/", "/foo/", "x/a",
105 const char *path_list_expected
[] = {
119 /* If inserting pathlist_input in reverse the result is slightly different. */
120 const char *path_list_expected_reverse
[] = {
138 const struct got_error
*err
= NULL
;
139 struct got_pathlist_head paths
;
140 struct got_pathlist_entry
*pe
;
144 for (i
= 0; i
< nitems(path_list_input
); i
++) {
145 err
= got_pathlist_insert(NULL
, &paths
, path_list_input
[i
],
148 test_printf("%s\n", __func__
, err
->msg
);
154 TAILQ_FOREACH(pe
, &paths
, entry
) {
155 test_printf("'%s' -- '%s'\n", pe
->path
, path_list_expected
[i
]);
156 if (i
>= nitems(path_list_expected
)) {
157 test_printf("too many elements on list\n");
160 if (strcmp(pe
->path
, path_list_expected
[i
]) != 0) {
161 test_printf("unordered elements on list\n");
167 got_pathlist_free(&paths
);
172 path_list_reverse_input(void)
174 const struct got_error
*err
= NULL
;
175 struct got_pathlist_head paths
;
176 struct got_pathlist_entry
*pe
;
180 for (i
= nitems(path_list_input
); i
> 0;) {
181 err
= got_pathlist_insert(NULL
, &paths
, path_list_input
[--i
],
184 test_printf("%s\n", __func__
, err
->msg
);
190 TAILQ_FOREACH(pe
, &paths
, entry
) {
191 test_printf("'%s' -- '%s'\n", pe
->path
,
192 path_list_expected_reverse
[i
]);
193 if (i
>= nitems(path_list_expected_reverse
)) {
194 test_printf("too many elements on list\n");
197 if (strcmp(pe
->path
, path_list_expected_reverse
[i
]) != 0) {
198 test_printf("unordered elements on list\n");
204 got_pathlist_free(&paths
);
208 #define RUN_TEST(expr, name) \
209 { test_ok = (expr); \
210 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
211 failure = (failure || !test_ok); }
216 fprintf(stderr
, "usage: path_test [-v] [-q]\n");
220 main(int argc
, char *argv
[])
222 int test_ok
= 0, failure
= 0;
226 if (pledge("stdio", NULL
) == -1)
230 while ((ch
= getopt(argc
, argv
, "vq")) != -1) {
248 RUN_TEST(path_cmp(), "path_cmp");
249 RUN_TEST(path_list(), "path_list");
250 RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
252 return failure
? 1 : 0;