2 This file is part of GNUnet.
3 Copyright (C) 2015 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
26 #include "gnunet_util_lib.h"
27 #include "gnunet_common.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_set_service.h"
33 * Value to return from #main().
37 static struct GNUNET_PeerIdentity local_id;
39 static struct GNUNET_SET_Handle *set1;
41 static struct GNUNET_SET_Handle *set2;
43 static const struct GNUNET_CONFIGURATION_Handle *config;
45 static struct GNUNET_SCHEDULER_Task *tt;
49 add_element_str (struct GNUNET_SET_Handle *set, char *str)
51 struct GNUNET_SET_Element element;
53 element.element_type = 0;
55 element.size = strlen (str);
57 GNUNET_SET_add_element (set, &element, NULL, NULL);
62 remove_element_str (struct GNUNET_SET_Handle *set, char *str)
64 struct GNUNET_SET_Element element;
66 element.element_type = 0;
68 element.size = strlen (str);
70 GNUNET_SET_remove_element (set, &element, NULL, NULL);
75 * Signature of the main function of a task.
80 timeout_fail (void *cls)
83 GNUNET_SCHEDULER_shutdown ();
87 typedef void (*Continuation) (void *cls);
90 struct CountIterClosure
92 unsigned int expected_count;
93 unsigned int ongoing_count;
101 check_count_iter (void *cls,
102 const struct GNUNET_SET_Element *element)
104 struct CountIterClosure *ci_cls = cls;
108 if (ci_cls->expected_count != ci_cls->ongoing_count)
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111 "Expected count (what: %s) to be %u, but it's actually %u\n",
113 ci_cls->expected_count, ci_cls->ongoing_count);
117 ci_cls->cont (ci_cls->cont_cls);
121 ci_cls->ongoing_count += 1;
127 check_count (struct GNUNET_SET_Handle *set,
129 unsigned int expected_count,
133 struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
135 ci_cls->expected_count = expected_count;
136 ci_cls->ongoing_count = 0;
138 ci_cls->cont_cls = cont_cls;
141 GNUNET_assert (GNUNET_YES == GNUNET_SET_iterate (set, check_count_iter, ci_cls));
146 test_done (void *cls)
149 GNUNET_SET_destroy (set1);
151 GNUNET_SET_destroy (set2);
152 GNUNET_SCHEDULER_cancel (tt);
154 GNUNET_SCHEDULER_shutdown ();
159 check_new_set_count (void *cls)
161 check_count (set2, "new set", 4, &test_done, NULL);
166 copy_done (void *cls, struct GNUNET_SET_Handle *new_set)
168 printf ("copy done\n");
170 remove_element_str (set2, "spam");
171 add_element_str (set2, "new1");
172 add_element_str (set2, "new2");
173 remove_element_str (set2, "new2");
174 remove_element_str (set2, "new3");
175 // Check that set1 didn't change.
176 check_count (set1, "old set", 3,
177 &check_new_set_count, NULL);
182 test_copy (void *cls)
184 printf ("about to copy\n");
185 GNUNET_SET_copy_lazy (set1, copy_done, NULL);
191 * Signature of the 'main' function for a (single-peer) testcase that
192 * is run using 'GNUNET_TESTING_peer_run'.
195 * @param cfg configuration of the peer that was started
196 * @param peer identity of the peer that was created
200 const struct GNUNET_CONFIGURATION_Handle *cfg,
201 struct GNUNET_TESTING_Peer *peer)
203 tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
208 GNUNET_TESTING_peer_get_identity (peer,
211 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
212 add_element_str (set1, "foo");
213 add_element_str (set1, "bar");
214 /* duplicate -- ignored */
215 add_element_str (set1, "bar");
216 remove_element_str (set1, "foo");
217 /* non-existent -- ignored */
218 remove_element_str (set1, "nonexist1");
219 add_element_str (set1, "spam");
220 /* duplicate -- ignored */
221 remove_element_str (set1, "foo");
222 add_element_str (set1, "eggs");
224 check_count (set1, "initial test", 3, &test_copy, NULL);
229 main (int argc, char **argv)
231 if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failed to start testing peer\n");