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;
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.
78 timeout_fail (void *cls)
80 const struct GNUNET_SCHEDULER_TaskContext *tc;
82 tc = GNUNET_SCHEDULER_get_task_context ();
83 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
85 GNUNET_SCHEDULER_shutdown ();
89 typedef void (*Continuation) (void *cls);
92 struct CountIterClosure
94 unsigned int expected_count;
95 unsigned int ongoing_count;
103 check_count_iter (void *cls,
104 const struct GNUNET_SET_Element *element)
106 struct CountIterClosure *ci_cls = cls;
110 if (ci_cls->expected_count != ci_cls->ongoing_count)
112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113 "Expected count (what: %s) to be %u, but it's actually %u\n",
115 ci_cls->expected_count, ci_cls->ongoing_count);
119 ci_cls->cont (ci_cls->cont_cls);
123 ci_cls->ongoing_count += 1;
130 check_count (struct GNUNET_SET_Handle *set,
132 unsigned int expected_count,
136 struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
138 ci_cls->expected_count = expected_count;
139 ci_cls->ongoing_count = 0;
141 ci_cls->cont_cls = cont_cls;
144 GNUNET_assert (GNUNET_YES == GNUNET_SET_iterate (set, check_count_iter, ci_cls));
148 void test_done (void *cls)
151 GNUNET_SET_destroy (set1);
153 GNUNET_SET_destroy (set2);
155 GNUNET_SCHEDULER_shutdown ();
159 void check_new_set_count (void *cls)
161 check_count (set2, "new set", 4, &test_done, NULL);
165 void copy_done (void *cls, struct GNUNET_SET_Handle *new_set)
167 printf ("copy done\n");
169 remove_element_str (set2, "spam");
170 add_element_str (set2, "new1");
171 add_element_str (set2, "new2");
172 remove_element_str (set2, "new2");
173 remove_element_str (set2, "new3");
174 // Check that set1 didn't change.
175 check_count (set1, "old set", 3,
176 &check_new_set_count, NULL);
180 void test_copy (void *cls)
182 printf ("about to copy\n");
183 GNUNET_SET_copy_lazy (set1, copy_done, NULL);
189 * Signature of the 'main' function for a (single-peer) testcase that
190 * is run using 'GNUNET_TESTING_peer_run'.
193 * @param cfg configuration of the peer that was started
194 * @param peer identity of the peer that was created
198 const struct GNUNET_CONFIGURATION_Handle *cfg,
199 struct GNUNET_TESTING_Peer *peer)
201 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
206 GNUNET_TESTING_peer_get_identity (peer,
209 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
210 add_element_str (set1, "foo");
211 add_element_str (set1, "bar");
212 /* duplicate -- ignored */
213 add_element_str (set1, "bar");
214 remove_element_str (set1, "foo");
215 /* non-existent -- ignored */
216 remove_element_str (set1, "nonexist1");
217 add_element_str (set1, "spam");
218 /* duplicate -- ignored */
219 remove_element_str (set1, "foo");
220 add_element_str (set1, "eggs");
222 check_count (set1, "initial test", 3, &test_copy, NULL);
227 main (int argc, char **argv)
229 if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failed to start testing peer\n");