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>
28 #include "got_error.h"
32 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
39 test_printf(const char *fmt
, ...)
54 const struct path_cmp_test
{
64 { "//foo", "/bar", 1 },
65 { "/foo", "/bar", 1 },
67 { "/foo/sub", "/bar", 1 },
68 { "/foo", "/bar/sub", 1 },
69 { "/foo/", "/bar", 1 },
70 { "/foo", "/bar/", 1 },
71 { "/foo/", "/bar/", 1 },
72 { "/bar/", "/bar/", 0 },
73 { "/bar/", "/bar", 0 },
74 { "//bar//", "/bar/", 0 },
75 { "//bar//", "/bar////", 0 },
76 { "/bar/sub", "/bar.", -1 },
77 { "/bar/sub", "/bar/", 1 },
78 { "/bar/sub/", "/bar///", 1 },
79 { "/bar/sub/sub2", "/bar/", 1 },
80 { "/bar/sub/sub2", "/bar", 1 },
81 { "/bar.sub.sub2", "/bar", 1 },
82 { "/bar/sub/sub2", "/bar.c", -1 },
86 for (i
= 0; i
< nitems(test_data
); i
++) {
87 const char *path1
= test_data
[i
].path1
;
88 const char *path2
= test_data
[i
].path2
;
89 int expected
= test_data
[i
].expected
;
90 int cmp
= got_path_cmp(path1
, path2
,
91 strlen(path1
), strlen(path2
));
93 if (cmp
!= expected
) {
94 test_printf("%d: '%s' vs '%s' == %d; expected %d\n",
95 i
, path1
, path2
, cmp
, expected
);
103 const char *path_list_input
[] = {
104 "", "/", "a", "/b", "/bar", "bar/sub", "/bar/sub", "/bar/",
105 "/bar.c", "/bar/sub/sub2", "/bar.sub.sub2", "/foo",
106 "/foo/sub", "/foo/", "/foo/", "x/a",
108 const char *path_list_expected
[] = {
122 /* If inserting pathlist_input in reverse the result is slightly different. */
123 const char *path_list_expected_reverse
[] = {
141 const struct got_error
*err
= NULL
;
142 struct got_pathlist_head paths
;
143 struct got_pathlist_entry
*pe
;
147 for (i
= 0; i
< nitems(path_list_input
); i
++) {
148 err
= got_pathlist_insert(NULL
, &paths
, path_list_input
[i
],
151 test_printf("%s\n", __func__
, err
->msg
);
157 TAILQ_FOREACH(pe
, &paths
, entry
) {
158 test_printf("'%s' -- '%s'\n", pe
->path
, path_list_expected
[i
]);
159 if (i
>= nitems(path_list_expected
)) {
160 test_printf("too many elements on list\n");
163 if (strcmp(pe
->path
, path_list_expected
[i
]) != 0) {
164 test_printf("unordered elements on list\n");
170 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_NONE
);
175 path_list_reverse_input(void)
177 const struct got_error
*err
= NULL
;
178 struct got_pathlist_head paths
;
179 struct got_pathlist_entry
*pe
;
183 for (i
= nitems(path_list_input
); i
> 0;) {
184 err
= got_pathlist_insert(NULL
, &paths
, path_list_input
[--i
],
187 test_printf("%s\n", __func__
, err
->msg
);
193 TAILQ_FOREACH(pe
, &paths
, entry
) {
194 test_printf("'%s' -- '%s'\n", pe
->path
,
195 path_list_expected_reverse
[i
]);
196 if (i
>= nitems(path_list_expected_reverse
)) {
197 test_printf("too many elements on list\n");
200 if (strcmp(pe
->path
, path_list_expected_reverse
[i
]) != 0) {
201 test_printf("unordered elements on list\n");
207 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_NONE
);
211 #define RUN_TEST(expr, name) \
212 { test_ok = (expr); \
213 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
214 failure = (failure || !test_ok); }
219 fprintf(stderr
, "usage: path_test [-v] [-q]\n");
223 main(int argc
, char *argv
[])
225 int test_ok
= 0, failure
= 0;
229 if (pledge("stdio", NULL
) == -1)
233 while ((ch
= getopt(argc
, argv
, "qv")) != -1) {
251 RUN_TEST(path_cmp(), "path_cmp");
252 RUN_TEST(path_list(), "path_list");
253 RUN_TEST(path_list_reverse_input(), "path_list_reverse_input");
255 return failure
? 1 : 0;