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/>.
20 * @file set/test_set_union_copy.c
21 * @brief testcase for lazy copying of union sets
22 * @author Florian Dold
25 #include "gnunet_util_lib.h"
26 #include "gnunet_common.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_set_service.h"
32 * Value to return from #main().
36 static struct GNUNET_PeerIdentity local_id;
38 static struct GNUNET_SET_Handle *set1;
40 static struct GNUNET_SET_Handle *set2;
42 static const struct GNUNET_CONFIGURATION_Handle *config;
44 static struct GNUNET_SCHEDULER_Task *tt;
48 add_element_str (struct GNUNET_SET_Handle *set,
51 struct GNUNET_SET_Element element;
53 element.element_type = 0;
55 element.size = strlen (str);
56 GNUNET_SET_add_element (set,
64 remove_element_str (struct GNUNET_SET_Handle *set,
67 struct GNUNET_SET_Element element;
69 element.element_type = 0;
71 element.size = strlen (str);
72 GNUNET_SET_remove_element (set,
80 * Signature of the main function of a task.
85 timeout_fail (void *cls)
88 GNUNET_SCHEDULER_shutdown ();
93 struct CountIterClosure
95 unsigned int expected_count;
96 unsigned int ongoing_count;
97 GNUNET_SCHEDULER_TaskCallback cont;
104 check_count_iter (void *cls,
105 const struct GNUNET_SET_Element *element)
107 struct CountIterClosure *ci_cls = cls;
111 if (ci_cls->expected_count != ci_cls->ongoing_count)
113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
114 "Expected count (what: %s) to be %u, but it's actually %u\n",
116 ci_cls->expected_count,
117 ci_cls->ongoing_count);
119 GNUNET_SCHEDULER_shutdown ();
122 ci_cls->cont (ci_cls->cont_cls);
123 GNUNET_free (ci_cls);
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "Set `%s' has element %.*s\n",
130 (const char *) element->data);
132 ci_cls->ongoing_count++;
138 check_count (struct GNUNET_SET_Handle *set,
140 unsigned int expected_count,
141 GNUNET_SCHEDULER_TaskCallback cont,
144 struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 "Checking count of %s\n",
150 ci_cls->expected_count = expected_count;
151 ci_cls->ongoing_count = 0;
153 ci_cls->cont_cls = cont_cls;
156 GNUNET_assert (GNUNET_YES ==
157 GNUNET_SET_iterate (set,
164 test_done (void *cls)
166 GNUNET_SCHEDULER_shutdown ();
171 check_new_set_count (void *cls)
182 copy_done (void *cls,
183 struct GNUNET_SET_Handle *new_set)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 remove_element_str (set2,
190 add_element_str (set2,
192 add_element_str (set2,
194 remove_element_str (set2,
196 remove_element_str (set2,
198 // Check that set1 didn't change.
202 &check_new_set_count,
208 test_copy (void *cls)
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 GNUNET_SET_copy_lazy (set1,
219 * Function run on shutdown.
224 do_shutdown (void *cls)
228 GNUNET_SCHEDULER_cancel (tt);
233 GNUNET_SET_destroy (set1);
238 GNUNET_SET_destroy (set2);
245 * Signature of the 'main' function for a (single-peer) testcase that
246 * is run using #GNUNET_TESTING_peer_run().
249 * @param cfg configuration of the peer that was started
250 * @param peer identity of the peer that was created
254 const struct GNUNET_CONFIGURATION_Handle *cfg,
255 struct GNUNET_TESTING_Peer *peer)
257 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
260 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
263 GNUNET_TESTING_peer_get_identity (peer,
266 set1 = GNUNET_SET_create (cfg,
267 GNUNET_SET_OPERATION_UNION);
268 add_element_str (set1,
270 add_element_str (set1,
272 /* duplicate -- ignored */
273 add_element_str (set1,
275 remove_element_str (set1,
277 /* non-existent -- ignored */
278 remove_element_str (set1,
280 add_element_str (set1,
282 /* duplicate -- ignored */
283 remove_element_str (set1,
285 add_element_str (set1,
297 main (int argc, char **argv)
299 if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
304 "failed to start testing peer\n");