2 This file is part of GNUnet.
3 Copyright (C) 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file set/test_set_union_copy.c
23 * @brief testcase for lazy copying of union sets
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_common.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_set_service.h"
34 * Value to return from #main().
38 static struct GNUNET_PeerIdentity local_id;
40 static struct GNUNET_SET_Handle *set1;
42 static struct GNUNET_SET_Handle *set2;
44 static const struct GNUNET_CONFIGURATION_Handle *config;
46 static struct GNUNET_SCHEDULER_Task *tt;
50 add_element_str(struct GNUNET_SET_Handle *set,
53 struct GNUNET_SET_Element element;
55 element.element_type = 0;
57 element.size = strlen(str);
58 GNUNET_SET_add_element(set,
66 remove_element_str(struct GNUNET_SET_Handle *set,
69 struct GNUNET_SET_Element element;
71 element.element_type = 0;
73 element.size = strlen(str);
74 GNUNET_SET_remove_element(set,
82 * Signature of the main function of a task.
87 timeout_fail(void *cls)
90 GNUNET_SCHEDULER_shutdown();
95 struct CountIterClosure {
96 unsigned int expected_count;
97 unsigned int ongoing_count;
98 GNUNET_SCHEDULER_TaskCallback cont;
105 check_count_iter(void *cls,
106 const struct GNUNET_SET_Element *element)
108 struct CountIterClosure *ci_cls = cls;
112 if (ci_cls->expected_count != ci_cls->ongoing_count)
114 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
115 "Expected count (what: %s) to be %u, but it's actually %u\n",
117 ci_cls->expected_count,
118 ci_cls->ongoing_count);
120 GNUNET_SCHEDULER_shutdown();
123 ci_cls->cont(ci_cls->cont_cls);
127 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
128 "Set `%s' has element %.*s\n",
131 (const char *)element->data);
133 ci_cls->ongoing_count++;
139 check_count(struct GNUNET_SET_Handle *set,
141 unsigned int expected_count,
142 GNUNET_SCHEDULER_TaskCallback cont,
145 struct CountIterClosure *ci_cls = GNUNET_new(struct CountIterClosure);
147 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
148 "Checking count of %s\n",
151 ci_cls->expected_count = expected_count;
152 ci_cls->ongoing_count = 0;
154 ci_cls->cont_cls = cont_cls;
157 GNUNET_assert(GNUNET_YES ==
158 GNUNET_SET_iterate(set,
167 GNUNET_SCHEDULER_shutdown();
172 check_new_set_count(void *cls)
184 struct GNUNET_SET_Handle *new_set)
186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
189 remove_element_str(set2,
191 add_element_str(set2,
193 add_element_str(set2,
195 remove_element_str(set2,
197 remove_element_str(set2,
199 // Check that set1 didn't change.
203 &check_new_set_count,
211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
213 GNUNET_SET_copy_lazy(set1,
220 * Function run on shutdown.
225 do_shutdown(void *cls)
229 GNUNET_SCHEDULER_cancel(tt);
234 GNUNET_SET_destroy(set1);
239 GNUNET_SET_destroy(set2);
246 * Signature of the 'main' function for a (single-peer) testcase that
247 * is run using #GNUNET_TESTING_peer_run().
250 * @param cfg configuration of the peer that was started
251 * @param peer identity of the peer that was created
255 const struct GNUNET_CONFIGURATION_Handle *cfg,
256 struct GNUNET_TESTING_Peer *peer)
258 tt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
261 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
264 GNUNET_TESTING_peer_get_identity(peer,
267 set1 = GNUNET_SET_create(cfg,
268 GNUNET_SET_OPERATION_UNION);
269 add_element_str(set1,
271 add_element_str(set1,
273 /* duplicate -- ignored */
274 add_element_str(set1,
276 remove_element_str(set1,
278 /* non-existent -- ignored */
279 remove_element_str(set1,
281 add_element_str(set1,
283 /* duplicate -- ignored */
284 remove_element_str(set1,
286 add_element_str(set1,
298 main(int argc, char **argv)
300 if (0 != GNUNET_TESTING_peer_run("test_set_union_copy",
304 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
305 "failed to start testing peer\n");