make got-read-gotconfig clear its imsgbuf before exit in an error case
[got-portable.git] / regress / path / path_test.c
blobe043f1bac9378493f94400177ff116330fa161c9
1 /*
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>
20 #include <sys/tree.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <err.h>
29 #include "got_error.h"
30 #include "got_path.h"
32 #ifndef nitems
33 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
34 #endif
36 static int verbose;
37 static int quiet;
39 static void
40 test_printf(const char *fmt, ...)
42 va_list ap;
44 if (!verbose)
45 return;
47 va_start(ap, fmt);
48 vprintf(fmt, ap);
49 va_end(ap);
52 static int
53 path_cmp(void)
55 const struct path_cmp_test {
56 const char *path1;
57 const char *path2;
58 int expected;
59 } test_data[] = {
60 { "", "", 0 },
61 { "/", "/", 0 },
62 { "/a", "/b", -1 },
63 { "x/a", "x.a", -1 },
64 { "x.a", "x/a", 1 },
65 { "//foo", "/bar", 1 },
66 { "/foo", "/bar", 1 },
67 { "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 },
85 size_t i;
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);
97 return 0;
101 return 1;
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[] = {
111 "a",
112 "/b",
113 "/bar",
114 "bar/sub",
115 "/bar/sub/sub2",
116 "/bar.c",
117 "/bar.sub.sub2",
118 "/foo",
119 "/foo/sub",
120 "x/a",
123 /* If inserting pathlist_input in reverse the result is slightly different. */
124 const char *path_list_expected_reverse[] = {
125 "/",
126 "a",
127 "/b",
128 "/bar/",
129 "/bar/sub",
130 "/bar/sub/sub2",
131 "/bar.c",
132 "/bar.sub.sub2",
133 "/foo/",
134 "/foo/sub",
135 "x/a",
139 static int
140 path_list(void)
142 const struct got_error *err = NULL;
143 struct got_pathlist_head paths;
144 struct got_pathlist_entry *pe;
145 size_t i;
147 RB_INIT(&paths);
148 for (i = 0; i < nitems(path_list_input); i++) {
149 err = got_pathlist_insert(NULL, &paths, path_list_input[i],
150 NULL);
151 if (err) {
152 test_printf("%s\n", __func__, err->msg);
153 return 0;
157 i = 0;
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");
162 return 0;
164 if (strcmp(pe->path, path_list_expected[i]) != 0) {
165 test_printf("unordered elements on list\n");
166 return 0;
168 i++;
171 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
172 return 1;
175 static int
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;
181 size_t i;
183 RB_INIT(&paths);
184 for (i = nitems(path_list_input); i > 0;) {
185 err = got_pathlist_insert(NULL, &paths, path_list_input[--i],
186 NULL);
187 if (err) {
188 test_printf("%s\n", __func__, err->msg);
189 return 0;
193 i = 0;
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");
199 return 0;
201 if (strcmp(pe->path, path_list_expected_reverse[i]) != 0) {
202 test_printf("unordered elements on list\n");
203 return 0;
205 i++;
208 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
209 return 1;
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); }
217 static void
218 usage(void)
220 fprintf(stderr, "usage: path_test [-v] [-q]\n");
224 main(int argc, char *argv[])
226 int test_ok = 0, failure = 0;
227 int ch;
229 #ifndef PROFILE
230 if (pledge("stdio", NULL) == -1)
231 err(1, "pledge");
232 #endif
234 while ((ch = getopt(argc, argv, "qv")) != -1) {
235 switch (ch) {
236 case 'q':
237 quiet = 1;
238 verbose = 0;
239 break;
240 case 'v':
241 verbose = 1;
242 quiet = 0;
243 break;
244 default:
245 usage();
246 return 1;
249 argc -= optind;
250 argv += optind;
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;