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.
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"
40 test_printf(char *fmt
, ...)
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
))
64 return NULL
; /* not reached */
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();
75 err
= got_error_from_errno("got_object_idset_alloc");
78 if (got_object_idset_num_elements(set
) != 0) {
79 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
83 if (!got_parse_sha1_digest(id1
.sha1
, id_str1
)) {
84 err
= got_error(GOT_ERR_BAD_OBJ_ID_STR
);
87 if (!got_parse_sha1_digest(id2
.sha1
, id_str2
)) {
88 err
= got_error(GOT_ERR_BAD_OBJ_ID_STR
);
91 if (!got_parse_sha1_digest(id3
.sha1
, id_str3
)) {
92 err
= got_error(GOT_ERR_BAD_OBJ_ID_STR
);
96 err
= got_object_idset_add(set
, &id1
, (void *)data1
);
99 if (got_object_idset_num_elements(set
) != 1) {
100 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
104 if (!got_object_idset_contains(set
, &id1
)) {
105 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
109 err
= got_object_idset_add(set
, &id2
, (void *)data2
);
113 if (!got_object_idset_contains(set
, &id1
)) {
114 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
117 if (!got_object_idset_contains(set
, &id2
)) {
118 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
121 if (got_object_idset_num_elements(set
) != 2) {
122 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
126 err
= got_object_idset_add(set
, &id3
, (void *)data3
);
130 if (got_object_idset_get(set
, &id1
) != (void *)data1
) {
131 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
134 if (got_object_idset_get(set
, &id2
) != (void *)data2
) {
135 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
138 if (got_object_idset_get(set
, &id3
) != (void *)data3
) {
139 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
142 if (got_object_idset_num_elements(set
) != 3) {
143 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
147 err
= got_object_idset_remove(NULL
, set
, &id2
);
150 if (got_object_idset_num_elements(set
) != 2) {
151 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
154 if (got_object_idset_contains(set
, &id2
)) {
155 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
158 if (got_object_idset_get(set
, &id2
) != NULL
) {
159 err
= got_error(GOT_ERR_BAD_OBJ_DATA
);
163 got_object_idset_for_each(set
, idset_cb
, NULL
);
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); }
177 fprintf(stderr
, "usage: id_test [-v] [-q]\n");
181 main(int argc
, char *argv
[])
183 int test_ok
= 0, failure
= 0;
187 if (pledge("stdio", NULL
) == -1)
191 while ((ch
= getopt(argc
, argv
, "vq")) != -1) {
209 RUN_TEST(idset_add_remove_iter(), "idset_add_remove_iter");
211 return failure
? 1 : 0;