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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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
97 unsigned int expected_count;
98 unsigned int ongoing_count;
99 GNUNET_SCHEDULER_TaskCallback cont;
106 check_count_iter (void *cls,
107 const struct GNUNET_SET_Element *element)
109 struct CountIterClosure *ci_cls = cls;
113 if (ci_cls->expected_count != ci_cls->ongoing_count)
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 "Expected count (what: %s) to be %u, but it's actually %u\n",
118 ci_cls->expected_count, ci_cls->ongoing_count);
122 ci_cls->cont (ci_cls->cont_cls);
126 ci_cls->ongoing_count += 1;
132 check_count (struct GNUNET_SET_Handle *set,
134 unsigned int expected_count,
135 GNUNET_SCHEDULER_TaskCallback cont,
138 struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
140 ci_cls->expected_count = expected_count;
141 ci_cls->ongoing_count = 0;
143 ci_cls->cont_cls = cont_cls;
146 GNUNET_assert (GNUNET_YES ==
147 GNUNET_SET_iterate (set,
154 test_done (void *cls)
156 GNUNET_SCHEDULER_shutdown ();
161 check_new_set_count (void *cls)
172 copy_done (void *cls,
173 struct GNUNET_SET_Handle *new_set)
175 printf ("copy done\n");
177 remove_element_str (set2, "spam");
178 add_element_str (set2, "new1");
179 add_element_str (set2, "new2");
180 remove_element_str (set2, "new2");
181 remove_element_str (set2, "new3");
182 // Check that set1 didn't change.
183 check_count (set1, "old set", 3,
184 &check_new_set_count,
190 test_copy (void *cls)
192 printf ("about to copy\n");
193 GNUNET_SET_copy_lazy (set1,
200 * Function run on shutdown.
205 do_shutdown (void *cls)
209 GNUNET_SCHEDULER_cancel (tt);
214 GNUNET_SET_destroy (set1);
219 GNUNET_SET_destroy (set2);
226 * Signature of the 'main' function for a (single-peer) testcase that
227 * is run using #GNUNET_TESTING_peer_run().
230 * @param cfg configuration of the peer that was started
231 * @param peer identity of the peer that was created
235 const struct GNUNET_CONFIGURATION_Handle *cfg,
236 struct GNUNET_TESTING_Peer *peer)
238 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
241 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
244 GNUNET_TESTING_peer_get_identity (peer,
247 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
248 add_element_str (set1, "foo");
249 add_element_str (set1, "bar");
250 /* duplicate -- ignored */
251 add_element_str (set1, "bar");
252 remove_element_str (set1, "foo");
253 /* non-existent -- ignored */
254 remove_element_str (set1, "nonexist1");
255 add_element_str (set1, "spam");
256 /* duplicate -- ignored */
257 remove_element_str (set1, "foo");
258 add_element_str (set1, "eggs");
269 main (int argc, char **argv)
271 if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
276 "failed to start testing peer\n");