2 * Copyright (c) 2018 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 <sys/queue.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_zbuf.h"
33 #include "got_lib_delta.h"
34 #include "got_lib_object.h"
39 test_printf(char *fmt, ...)
51 static const char *id_str1 = "1111111111111111111111111111111111111111";
52 static const char *id_str2 = "2222222222222222222222222222222222222222";
53 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
54 static struct got_object_id id1, id2, id3;
55 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
56 static int iter_count;
59 idset_cb(struct got_object_id *id, void *data, void *arg) {
60 if (iter_count == 0 &&
61 (got_object_id_cmp(id, &id1) != 0 || data != (void *)data1))
63 if (iter_count == 1 &&
64 (got_object_id_cmp(id, &id3) != 0 || data != (void *)data3))
70 idset_add_remove_iter(void)
72 const struct got_error *err = NULL;
73 struct got_object_idset *set;
76 set = got_object_idset_alloc();
78 err = got_error_from_errno();
81 if (got_object_idset_num_elements(set) != 0) {
82 err = got_error(GOT_ERR_BAD_OBJ_DATA);
86 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
87 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
90 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
91 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
94 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
95 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
99 err = got_object_idset_add(&existing_data, set, &id1, (void *)data1);
102 if (existing_data != NULL) {
103 err = got_error(GOT_ERR_BAD_OBJ_DATA);
106 if (got_object_idset_num_elements(set) != 1) {
107 err = got_error(GOT_ERR_BAD_OBJ_DATA);
111 if (!got_object_idset_contains(set, &id1)) {
112 err = got_error(GOT_ERR_BAD_OBJ_DATA);
116 err = got_object_idset_add(&existing_data, set, &id2, (void *)data2);
119 if (existing_data != NULL) {
120 err = got_error(GOT_ERR_BAD_OBJ_DATA);
123 err = got_object_idset_add(&existing_data, set, &id2, NULL);
124 if (existing_data == NULL) {
125 err = got_error(GOT_ERR_BAD_OBJ_DATA);
128 if (err->code != GOT_ERR_OBJ_EXISTS)
130 err = got_object_idset_add(NULL, set, &id2, NULL);
131 if (err->code != GOT_ERR_OBJ_EXISTS)
135 if (!got_object_idset_contains(set, &id1)) {
136 err = got_error(GOT_ERR_BAD_OBJ_DATA);
139 if (!got_object_idset_contains(set, &id2)) {
140 err = got_error(GOT_ERR_BAD_OBJ_DATA);
143 if (got_object_idset_num_elements(set) != 2) {
144 err = got_error(GOT_ERR_BAD_OBJ_DATA);
148 err = got_object_idset_add(NULL, set, &id3, (void *)data3);
152 if (got_object_idset_get(set, &id1) != (void *)data1) {
153 err = got_error(GOT_ERR_BAD_OBJ_DATA);
156 if (got_object_idset_get(set, &id2) != (void *)data2) {
157 err = got_error(GOT_ERR_BAD_OBJ_DATA);
160 if (got_object_idset_get(set, &id3) != (void *)data3) {
161 err = got_error(GOT_ERR_BAD_OBJ_DATA);
164 if (got_object_idset_num_elements(set) != 3) {
165 err = got_error(GOT_ERR_BAD_OBJ_DATA);
169 err = got_object_idset_remove(set, &id2);
172 if (got_object_idset_num_elements(set) != 2) {
173 err = got_error(GOT_ERR_BAD_OBJ_DATA);
176 if (got_object_idset_contains(set, &id2)) {
177 err = got_error(GOT_ERR_BAD_OBJ_DATA);
180 if (got_object_idset_get(set, &id2) != NULL) {
181 err = got_error(GOT_ERR_BAD_OBJ_DATA);
185 got_object_idset_for_each(set, idset_cb, NULL);
187 got_object_idset_free(set);
188 return (err == NULL);
191 #define RUN_TEST(expr, name) \
192 { test_ok = (expr); \
193 printf("test %s %s\n", (name), test_ok ? "ok" : "failed"); \
194 failure = (failure || !test_ok); }
199 fprintf(stderr, "usage: id_test [-v]\n");
203 main(int argc, char *argv[])
205 int test_ok = 0, failure = 0;
208 if (pledge("stdio", NULL) == -1)
211 while ((ch = getopt(argc, argv, "v")) != -1) {
224 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
226 return failure ? 1 : 0;