2 This file is part of GNUnet.
3 Copyright (C) 2015 Christian Grothoff (and other contributing authors)
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;
47 add_element_str (struct GNUNET_SET_Handle *set, char *str)
49 struct GNUNET_SET_Element element;
51 element.element_type = 0;
53 element.size = strlen (str);
55 GNUNET_SET_add_element (set, &element, NULL, NULL);
60 remove_element_str (struct GNUNET_SET_Handle *set, char *str)
62 struct GNUNET_SET_Element element;
64 element.element_type = 0;
66 element.size = strlen (str);
68 GNUNET_SET_remove_element (set, &element, NULL, NULL);
73 * Signature of the main function of a task.
76 * @param tc context information (why was this task triggered now)
79 timeout_fail (void *cls,
80 const struct GNUNET_SCHEDULER_TaskContext *tc)
82 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
84 GNUNET_SCHEDULER_shutdown ();
88 typedef void (*Continuation) (void *cls);
91 struct CountIterClosure
93 unsigned int expected_count;
94 unsigned int ongoing_count;
102 check_count_iter (void *cls,
103 const struct GNUNET_SET_Element *element)
105 struct CountIterClosure *ci_cls = cls;
109 if (ci_cls->expected_count != ci_cls->ongoing_count)
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 "Expected count (what: %s) to be %u, but it's actually %u\n",
114 ci_cls->expected_count, ci_cls->ongoing_count);
118 ci_cls->cont (ci_cls->cont_cls);
122 ci_cls->ongoing_count += 1;
129 check_count (struct GNUNET_SET_Handle *set,
131 unsigned int expected_count,
135 struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
137 ci_cls->expected_count = expected_count;
138 ci_cls->ongoing_count = 0;
140 ci_cls->cont_cls = cont_cls;
143 GNUNET_assert (GNUNET_YES == GNUNET_SET_iterate (set, check_count_iter, ci_cls));
147 void test_done (void *cls)
150 GNUNET_SET_destroy (set1);
152 GNUNET_SET_destroy (set2);
154 GNUNET_SCHEDULER_shutdown ();
158 void check_new_set_count (void *cls)
160 check_count (set2, "new set", 4, &test_done, NULL);
164 void copy_done (void *cls, struct GNUNET_SET_Handle *new_set)
166 printf ("copy done\n");
168 remove_element_str (set2, "spam");
169 add_element_str (set2, "new1");
170 add_element_str (set2, "new2");
171 remove_element_str (set2, "new2");
172 remove_element_str (set2, "new3");
173 // Check that set1 didn't change.
174 check_count (set1, "old set", 3,
175 &check_new_set_count, NULL);
179 void test_copy (void *cls)
181 printf ("about to copy\n");
182 GNUNET_SET_copy_lazy (set1, copy_done, NULL);
188 * Signature of the 'main' function for a (single-peer) testcase that
189 * is run using 'GNUNET_TESTING_peer_run'.
192 * @param cfg configuration of the peer that was started
193 * @param peer identity of the peer that was created
197 const struct GNUNET_CONFIGURATION_Handle *cfg,
198 struct GNUNET_TESTING_Peer *peer)
200 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
205 GNUNET_TESTING_peer_get_identity (peer,
208 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
209 add_element_str (set1, "foo");
210 add_element_str (set1, "bar");
211 /* duplicate -- ignored */
212 add_element_str (set1, "bar");
213 remove_element_str (set1, "foo");
214 /* non-existent -- ignored */
215 remove_element_str (set1, "nonexist1");
216 add_element_str (set1, "spam");
217 /* duplicate -- ignored */
218 remove_element_str (set1, "foo");
219 add_element_str (set1, "eggs");
221 check_count (set1, "initial test", 3, &test_copy, NULL);
226 main (int argc, char **argv)
228 if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failed to start testing peer\n");