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>
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"
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
))
54 return NULL
; /* not reached */
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();
65 err
= got_error_from_errno("got_object_idset_alloc");
68 if (got_object_idset_num_elements(set
) != 0) {
69 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
73 if (!got_parse_object_id(&id1
, id_str1
, GOT_HASH_SHA1
)) {
74 err
= got_error(GOT_ERR_BAD_OBJ_ID_STR
);
77 if (!got_parse_object_id(&id2
, id_str2
, GOT_HASH_SHA1
)) {
78 err
= got_error(GOT_ERR_BAD_OBJ_ID_STR
);
81 if (!got_parse_object_id(&id3
, id_str3
, GOT_HASH_SHA1
)) {
82 err
= got_error(GOT_ERR_BAD_OBJ_ID_STR
);
86 err
= got_object_idset_add(set
, &id1
, (void *)data1
);
89 if (got_object_idset_num_elements(set
) != 1) {
90 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
94 if (!got_object_idset_contains(set
, &id1
)) {
95 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
99 err
= got_object_idset_add(set
, &id2
, (void *)data2
);
103 if (!got_object_idset_contains(set
, &id1
)) {
104 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
107 if (!got_object_idset_contains(set
, &id2
)) {
108 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
111 if (got_object_idset_num_elements(set
) != 2) {
112 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
116 err
= got_object_idset_add(set
, &id3
, (void *)data3
);
120 if (got_object_idset_get(set
, &id1
) != (void *)data1
) {
121 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
124 if (got_object_idset_get(set
, &id2
) != (void *)data2
) {
125 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
128 if (got_object_idset_get(set
, &id3
) != (void *)data3
) {
129 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
132 if (got_object_idset_num_elements(set
) != 3) {
133 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
137 err
= got_object_idset_remove(NULL
, set
, &id2
);
140 if (got_object_idset_num_elements(set
) != 2) {
141 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
144 if (got_object_idset_contains(set
, &id2
)) {
145 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
148 if (got_object_idset_get(set
, &id2
) != NULL
) {
149 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
153 got_object_idset_for_each(set
, idset_cb
, NULL
);
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); }
167 fprintf(stderr
, "usage: id_test [-v] [-q]\n");
171 main(int argc
, char *argv
[])
173 int test_ok
= 0, failure
= 0;
177 if (pledge("stdio", NULL
) == -1)
181 while ((ch
= getopt(argc
, argv
, "qv")) != -1) {
199 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
201 return failure
? 1 : 0;