regress: enable tests
[got-portable.git] / regress / idset / idset_test.c
blob429beb1d097c7200262558eb58712cbbc04591b4
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.
18 #include <limits.h>
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <err.h>
24 #include <zlib.h>
25 #include <time.h>
27 #include "got_error.h"
28 #include "got_object.h"
30 #include "got_lib_object_idset.h"
31 #include "got_lib_sha1.h"
32 #include "got_lib_inflate.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_object.h"
36 static int verbose;
37 static int quiet;
39 void
40 test_printf(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 const char *id_str1 = "1111111111111111111111111111111111111111";
53 static const char *id_str2 = "2222222222222222222222222222222222222222";
54 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
55 static struct got_object_id id1, id2, id3;
56 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
58 static const struct got_error *
59 idset_cb(struct got_object_id *id, void *data, void *arg) {
60 if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
61 (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
62 return NULL;
63 abort();
64 return NULL; /* not reached */
67 static int
68 idset_add_remove_iter(void)
70 const struct got_error *err = NULL;
71 struct got_object_idset *set;
73 set = got_object_idset_alloc();
74 if (set == NULL) {
75 err = got_error_from_errno("got_object_idset_alloc");
76 goto done;
78 if (got_object_idset_num_elements(set) != 0) {
79 err = got_error(GOT_ERR_BAD_OBJ_DATA);
80 goto done;
83 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
84 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
85 goto done;
87 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
88 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
89 goto done;
91 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
92 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
93 goto done;
96 err = got_object_idset_add(set, &id1, (void *)data1);
97 if (err)
98 goto done;
99 if (got_object_idset_num_elements(set) != 1) {
100 err = got_error(GOT_ERR_BAD_OBJ_DATA);
101 goto done;
104 if (!got_object_idset_contains(set, &id1)) {
105 err = got_error(GOT_ERR_BAD_OBJ_DATA);
106 goto done;
109 err = got_object_idset_add(set, &id2, (void *)data2);
110 if (err)
111 goto done;
113 if (!got_object_idset_contains(set, &id1)) {
114 err = got_error(GOT_ERR_BAD_OBJ_DATA);
115 goto done;
117 if (!got_object_idset_contains(set, &id2)) {
118 err = got_error(GOT_ERR_BAD_OBJ_DATA);
119 goto done;
121 if (got_object_idset_num_elements(set) != 2) {
122 err = got_error(GOT_ERR_BAD_OBJ_DATA);
123 goto done;
126 err = got_object_idset_add(set, &id3, (void *)data3);
127 if (err)
128 goto done;
130 if (got_object_idset_get(set, &id1) != (void *)data1) {
131 err = got_error(GOT_ERR_BAD_OBJ_DATA);
132 goto done;
134 if (got_object_idset_get(set, &id2) != (void *)data2) {
135 err = got_error(GOT_ERR_BAD_OBJ_DATA);
136 goto done;
138 if (got_object_idset_get(set, &id3) != (void *)data3) {
139 err = got_error(GOT_ERR_BAD_OBJ_DATA);
140 goto done;
142 if (got_object_idset_num_elements(set) != 3) {
143 err = got_error(GOT_ERR_BAD_OBJ_DATA);
144 goto done;
147 err = got_object_idset_remove(NULL, set, &id2);
148 if (err)
149 goto done;
150 if (got_object_idset_num_elements(set) != 2) {
151 err = got_error(GOT_ERR_BAD_OBJ_DATA);
152 goto done;
154 if (got_object_idset_contains(set, &id2)) {
155 err = got_error(GOT_ERR_BAD_OBJ_DATA);
156 goto done;
158 if (got_object_idset_get(set, &id2) != NULL) {
159 err = got_error(GOT_ERR_BAD_OBJ_DATA);
160 goto done;
163 got_object_idset_for_each(set, idset_cb, NULL);
164 done:
165 got_object_idset_free(set);
166 return (err == NULL);
169 #define RUN_TEST(expr, name) \
170 { test_ok = (expr); \
171 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
172 failure = (failure || !test_ok); }
174 void
175 usage(void)
177 fprintf(stderr, "usage: id_test [-v] [-q]\n");
181 main(int argc, char *argv[])
183 int test_ok = 0, failure = 0;
184 int ch;
186 #ifndef PROFILE
187 if (pledge("stdio", NULL) == -1)
188 err(1, "pledge");
189 #endif
191 while ((ch = getopt(argc, argv, "vq")) != -1) {
192 switch (ch) {
193 case 'v':
194 verbose = 1;
195 quiet = 0;
196 break;
197 case 'q':
198 quiet = 1;
199 verbose = 0;
200 break;
201 default:
202 usage();
203 return 1;
206 argc -= optind;
207 argv += optind;
209 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
211 return failure ? 1 : 0;