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 "got_compat.h"
19 #include <sys/queue.h>
29 #include "got_error.h"
33 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
40 test_printf(const char *fmt
, ...)
55 const struct path_cmp_test
{
65 { "//foo", "/bar", 1 },
66 { "/foo", "/bar", 1 },
68 { "/foo/sub", "/bar", 1 },
69 { "/foo", "/bar/sub", 1 },
70 { "/foo/", "/bar", 1 },
71 { "/foo", "/bar/", 1 },
72 { "/foo/", "/bar/", 1 },
73 { "/bar/", "/bar/", 0 },
74 { "/bar/", "/bar", 0 },
75 { "//bar//", "/bar/", 0 },
76 { "//bar//", "/bar////", 0 },
77 { "/bar/sub", "/bar.", -1 },
78 { "/bar/sub", "/bar/", 1 },
79 { "/bar/sub/", "/bar///", 1 },
80 { "/bar/sub/sub2", "/bar/", 1 },
81 { "/bar/sub/sub2", "/bar", 1 },
82 { "/bar.sub.sub2", "/bar", 1 },
83 { "/bar/sub/sub2", "/bar.c", -1 },
87 for (i
= 0; i
< nitems(test_data
); i
++) {
88 const char *path1
= test_data
[i
].path1
;
89 const char *path2
= test_data
[i
].path2
;
90 int expected
= test_data
[i
].expected
;
91 int cmp
= got_path_cmp(path1
, path2
,
92 strlen(path1
), strlen(path2
));
94 if (cmp
!= expected
) {
95 test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
96 i
, path1
, path2
, cmp
, expected
);
104 const char *path_list_input
[] = {
105 "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
106 "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
107 "/foo/sub", "/foo/", "/foo/", "x/a",
109 const char *path_list_expected
[] = {
123 /* If inserting pathlist_input in reverse the result is slightly different. */
124 const char *path_list_expected_reverse
[] = {
142 const struct got_error
*err
= NULL
;
143 struct got_pathlist_head paths
;
144 struct got_pathlist_entry
*pe
;
148 for (i
= 0; i
< nitems(path_list_input
); i
++) {
149 err
= got_pathlist_insert(NULL
, &paths
, path_list_input
[i
],
152 test_printf("%s\n", __func__
, err
->msg
);
158 RB_FOREACH(pe
, got_pathlist_head
, &paths
) {
159 test_printf("'%s' -- '%s'\n", pe
->path
, path_list_expected
[i
]);
160 if (i
>= nitems(path_list_expected
)) {
161 test_printf("too many elements on list\n");
164 if (strcmp(pe
->path
, path_list_expected
[i
]) != 0) {
165 test_printf("unordered elements on list\n");
171 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_NONE
);
176 path_list_reverse_input(void)
178 const struct got_error
*err
= NULL
;
179 struct got_pathlist_head paths
;
180 struct got_pathlist_entry
*pe
;
184 for (i
= nitems(path_list_input
); i
> 0;) {
185 err
= got_pathlist_insert(NULL
, &paths
, path_list_input
[--i
],
188 test_printf("%s\n", __func__
, err
->msg
);
194 RB_FOREACH(pe
, got_pathlist_head
, &paths
) {
195 test_printf("'%s' -- '%s'\n", pe
->path
,
196 path_list_expected_reverse
[i
]);
197 if (i
>= nitems(path_list_expected_reverse
)) {
198 test_printf("too many elements on list\n");
201 if (strcmp(pe
->path
, path_list_expected_reverse
[i
]) != 0) {
202 test_printf("unordered elements on list\n");
208 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_NONE
);
212 #define RUN_TEST(expr, name) \
213 { test_ok = (expr); \
214 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
215 failure = (failure || !test_ok); }
220 fprintf(stderr
, "usage: path_test [-v] [-q]\n");
224 main(int argc
, char *argv
[])
226 int test_ok
= 0, failure
= 0;
230 if (pledge("stdio", NULL
) == -1)
234 while ((ch
= getopt(argc
, argv
, "qv")) != -1) {
252 RUN_TEST(path_cmp(), "path_cmp");
253 RUN_TEST(path_list(), "path_list");
254 RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
256 return failure
? 1 : 0;