use INT32_MAX rather than INT_MAX for GOT_FILEIDX_MAX_ENTRIES
[got-portable.git] / regress / idset / idset_test.c
blob529d7ecc4770b7fb8547c6baeb5a13156cecb09a
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>
21 #include <limits.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <err.h>
27 #include <zlib.h>
28 #include <time.h>
30 #include "got_error.h"
31 #include "got_object.h"
33 #include "got_lib_object_idset.h"
34 #include "got_lib_hash.h"
35 #include "got_lib_inflate.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
39 static int verbose;
40 static int quiet;
42 static const char *id_str1 = "1111111111111111111111111111111111111111";
43 static const char *id_str2 = "2222222222222222222222222222222222222222";
44 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
45 static struct got_object_id id1, id2, id3;
46 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
48 static const struct got_error *
49 idset_cb(struct got_object_id *id, void *data, void *arg) {
50 if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
51 (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
52 return NULL;
53 abort();
54 return NULL; /* not reached */
57 static int
58 idset_add_remove_iter(void)
60 const struct got_error *err = NULL;
61 struct got_object_idset *set;
63 set = got_object_idset_alloc();
64 if (set == NULL) {
65 err = got_error_from_errno("got_object_idset_alloc");
66 goto done;
68 if (got_object_idset_num_elements(set) != 0) {
69 err = got_error(GOT_ERR_BAD_OBJ_DATA);
70 goto done;
73 if (!got_parse_object_id(&id1, id_str1, GOT_HASH_SHA1)) {
74 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
75 goto done;
77 if (!got_parse_object_id(&id2, id_str2, GOT_HASH_SHA1)) {
78 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
79 goto done;
81 if (!got_parse_object_id(&id3, id_str3, GOT_HASH_SHA1)) {
82 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
83 goto done;
86 err = got_object_idset_add(set, &id1, (void *)data1);
87 if (err)
88 goto done;
89 if (got_object_idset_num_elements(set) != 1) {
90 err = got_error(GOT_ERR_BAD_OBJ_DATA);
91 goto done;
94 if (!got_object_idset_contains(set, &id1)) {
95 err = got_error(GOT_ERR_BAD_OBJ_DATA);
96 goto done;
99 err = got_object_idset_add(set, &id2, (void *)data2);
100 if (err)
101 goto done;
103 if (!got_object_idset_contains(set, &id1)) {
104 err = got_error(GOT_ERR_BAD_OBJ_DATA);
105 goto done;
107 if (!got_object_idset_contains(set, &id2)) {
108 err = got_error(GOT_ERR_BAD_OBJ_DATA);
109 goto done;
111 if (got_object_idset_num_elements(set) != 2) {
112 err = got_error(GOT_ERR_BAD_OBJ_DATA);
113 goto done;
116 err = got_object_idset_add(set, &id3, (void *)data3);
117 if (err)
118 goto done;
120 if (got_object_idset_get(set, &id1) != (void *)data1) {
121 err = got_error(GOT_ERR_BAD_OBJ_DATA);
122 goto done;
124 if (got_object_idset_get(set, &id2) != (void *)data2) {
125 err = got_error(GOT_ERR_BAD_OBJ_DATA);
126 goto done;
128 if (got_object_idset_get(set, &id3) != (void *)data3) {
129 err = got_error(GOT_ERR_BAD_OBJ_DATA);
130 goto done;
132 if (got_object_idset_num_elements(set) != 3) {
133 err = got_error(GOT_ERR_BAD_OBJ_DATA);
134 goto done;
137 err = got_object_idset_remove(NULL, set, &id2);
138 if (err)
139 goto done;
140 if (got_object_idset_num_elements(set) != 2) {
141 err = got_error(GOT_ERR_BAD_OBJ_DATA);
142 goto done;
144 if (got_object_idset_contains(set, &id2)) {
145 err = got_error(GOT_ERR_BAD_OBJ_DATA);
146 goto done;
148 if (got_object_idset_get(set, &id2) != NULL) {
149 err = got_error(GOT_ERR_BAD_OBJ_DATA);
150 goto done;
153 got_object_idset_for_each(set, idset_cb, NULL);
154 done:
155 got_object_idset_free(set);
156 return (err == NULL);
159 #define RUN_TEST(expr, name) \
160 { test_ok = (expr); \
161 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
162 failure = (failure || !test_ok); }
164 static void
165 usage(void)
167 fprintf(stderr, "usage: id_test [-v] [-q]\n");
171 main(int argc, char *argv[])
173 int test_ok = 0, failure = 0;
174 int ch;
176 #ifndef PROFILE
177 if (pledge("stdio", NULL) == -1)
178 err(1, "pledge");
179 #endif
181 while ((ch = getopt(argc, argv, "qv")) != -1) {
182 switch (ch) {
183 case 'q':
184 quiet = 1;
185 verbose = 0;
186 break;
187 case 'v':
188 verbose = 1;
189 quiet = 0;
190 break;
191 default:
192 usage();
193 return 1;
196 argc -= optind;
197 argv += optind;
199 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
201 return failure ? 1 : 0;