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 <sys/queue.h>
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_lib_object_idset.h"
33 #include "got_lib_sha1.h"
34 #include "got_lib_inflate.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_object.h"
41 static const char *id_str1 = "1111111111111111111111111111111111111111";
42 static const char *id_str2 = "2222222222222222222222222222222222222222";
43 static const char *id_str3 = "ffffffffffffffffffffffffffffffffffffffff";
44 static struct got_object_id id1, id2, id3;
45 static const char *data1 = "data1", *data2 = "data2", *data3 = "data3";
47 static const struct got_error *
48 idset_cb(struct got_object_id *id, void *data, void *arg) {
49 if ((got_object_id_cmp(id, &id1) == 0 && data == (void *)data1) ||
50 (got_object_id_cmp(id, &id3) == 0 && data == (void *)data3))
53 return NULL; /* not reached */
57 idset_add_remove_iter(void)
59 const struct got_error *err = NULL;
60 struct got_object_idset *set;
62 set = got_object_idset_alloc();
64 err = got_error_from_errno("got_object_idset_alloc");
67 if (got_object_idset_num_elements(set) != 0) {
68 err = got_error(GOT_ERR_BAD_OBJ_DATA);
72 if (!got_parse_sha1_digest(id1.sha1, id_str1)) {
73 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
76 if (!got_parse_sha1_digest(id2.sha1, id_str2)) {
77 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
80 if (!got_parse_sha1_digest(id3.sha1, id_str3)) {
81 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
85 err = got_object_idset_add(set, &id1, (void *)data1);
88 if (got_object_idset_num_elements(set) != 1) {
89 err = got_error(GOT_ERR_BAD_OBJ_DATA);
93 if (!got_object_idset_contains(set, &id1)) {
94 err = got_error(GOT_ERR_BAD_OBJ_DATA);
98 err = got_object_idset_add(set, &id2, (void *)data2);
102 if (!got_object_idset_contains(set, &id1)) {
103 err = got_error(GOT_ERR_BAD_OBJ_DATA);
106 if (!got_object_idset_contains(set, &id2)) {
107 err = got_error(GOT_ERR_BAD_OBJ_DATA);
110 if (got_object_idset_num_elements(set) != 2) {
111 err = got_error(GOT_ERR_BAD_OBJ_DATA);
115 err = got_object_idset_add(set, &id3, (void *)data3);
119 if (got_object_idset_get(set, &id1) != (void *)data1) {
120 err = got_error(GOT_ERR_BAD_OBJ_DATA);
123 if (got_object_idset_get(set, &id2) != (void *)data2) {
124 err = got_error(GOT_ERR_BAD_OBJ_DATA);
127 if (got_object_idset_get(set, &id3) != (void *)data3) {
128 err = got_error(GOT_ERR_BAD_OBJ_DATA);
131 if (got_object_idset_num_elements(set) != 3) {
132 err = got_error(GOT_ERR_BAD_OBJ_DATA);
136 err = got_object_idset_remove(NULL, set, &id2);
139 if (got_object_idset_num_elements(set) != 2) {
140 err = got_error(GOT_ERR_BAD_OBJ_DATA);
143 if (got_object_idset_contains(set, &id2)) {
144 err = got_error(GOT_ERR_BAD_OBJ_DATA);
147 if (got_object_idset_get(set, &id2) != NULL) {
148 err = got_error(GOT_ERR_BAD_OBJ_DATA);
152 got_object_idset_for_each(set, idset_cb, NULL);
154 got_object_idset_free(set);
155 return (err == NULL);
158 #define RUN_TEST(expr, name) \
159 { test_ok = (expr); \
160 if (!quiet) printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
161 failure = (failure || !test_ok); }
166 fprintf(stderr, "usage: id_test [-v] [-q]\n");
170 main(int argc, char *argv[])
172 int test_ok = 0, failure = 0;
176 if (pledge("stdio", NULL) == -1)
180 while ((ch = getopt(argc, argv, "vq")) != -1) {
198 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
200 return failure ? 1 : 0;