aboutsummaryrefslogtreecommitdiff
path: root/src/set/test_set_union_copy.c
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2015-08-30 20:54:44 +0000
committerFlorian Dold <florian.dold@gmail.com>2015-08-30 20:54:44 +0000
commit2e25d8c868521f318381a8b87c9f8c1c7c402b7c (patch)
treea5d9b8fca78e55da9d8c62ff8fe6d1d480a459e0 /src/set/test_set_union_copy.c
parentc92948f72526fe599680c06cbe80d0ffdc11f597 (diff)
downloadgnunet-2e25d8c868521f318381a8b87c9f8c1c7c402b7c.tar.gz
gnunet-2e25d8c868521f318381a8b87c9f8c1c7c402b7c.zip
add test case for 'GNUNET_SET_copy_lazy', fix bugs
Diffstat (limited to 'src/set/test_set_union_copy.c')
-rw-r--r--src/set/test_set_union_copy.c225
1 files changed, 225 insertions, 0 deletions
diff --git a/src/set/test_set_union_copy.c b/src/set/test_set_union_copy.c
new file mode 100644
index 000000000..bb8d57631
--- /dev/null
+++ b/src/set/test_set_union_copy.c
@@ -0,0 +1,225 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2015 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19*/
20
21/**
22 * @file set/test_set_union_copy.c
23 * @brief testcase for lazy copying of union sets
24 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_common.h"
28#include "gnunet_testing_lib.h"
29#include "gnunet_set_service.h"
30
31
32/**
33 * Value to return from #main().
34 */
35static int ret;
36
37static struct GNUNET_PeerIdentity local_id;
38
39static struct GNUNET_SET_Handle *set1;
40
41static struct GNUNET_SET_Handle *set2;
42
43static const struct GNUNET_CONFIGURATION_Handle *config;
44
45
46static void
47add_element_str (struct GNUNET_SET_Handle *set, char *str)
48{
49 struct GNUNET_SET_Element element;
50
51 element.element_type = 0;
52 element.data = str;
53 element.size = strlen (str);
54
55 GNUNET_SET_add_element (set, &element, NULL, NULL);
56}
57
58
59static void
60remove_element_str (struct GNUNET_SET_Handle *set, char *str)
61{
62 struct GNUNET_SET_Element element;
63
64 element.element_type = 0;
65 element.data = str;
66 element.size = strlen (str);
67
68 GNUNET_SET_remove_element (set, &element, NULL, NULL);
69}
70
71
72/**
73 * Signature of the main function of a task.
74 *
75 * @param cls closure
76 * @param tc context information (why was this task triggered now)
77 */
78static void
79timeout_fail (void *cls,
80 const struct GNUNET_SCHEDULER_TaskContext *tc)
81{
82 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
83 return;
84 GNUNET_SCHEDULER_shutdown ();
85 ret = 1;
86}
87
88typedef void (*Continuation) (void *cls);
89
90
91struct CountIterClosure
92{
93 unsigned int expected_count;
94 unsigned int ongoing_count;
95 Continuation cont;
96 void *cont_cls;
97 char *what;
98};
99
100
101static int
102check_count_iter (void *cls,
103 const struct GNUNET_SET_Element *element)
104{
105 struct CountIterClosure *ci_cls = cls;
106
107 if (NULL == element)
108 {
109 if (ci_cls->expected_count != ci_cls->ongoing_count)
110 {
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 "Expected count (what: %s) to be %u, but it's actually %u\n",
113 ci_cls->what,
114 ci_cls->expected_count, ci_cls->ongoing_count);
115 ret = 1;
116 return GNUNET_NO;
117 }
118 ci_cls->cont (ci_cls->cont_cls);
119 return GNUNET_NO;
120 }
121
122 ci_cls->ongoing_count += 1;
123 return GNUNET_YES;
124}
125
126
127
128void
129check_count (struct GNUNET_SET_Handle *set,
130 char *what,
131 unsigned int expected_count,
132 Continuation cont,
133 void *cont_cls)
134{
135 struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
136
137 ci_cls->expected_count = expected_count;
138 ci_cls->ongoing_count = 0;
139 ci_cls->cont = cont;
140 ci_cls->cont_cls = cont_cls;
141 ci_cls->what = what;
142
143 GNUNET_assert (GNUNET_YES == GNUNET_SET_iterate (set, check_count_iter, ci_cls));
144}
145
146
147void test_done (void *cls)
148{
149 if (NULL != set1)
150 GNUNET_SET_destroy (set1);
151 if (NULL != set2)
152 GNUNET_SET_destroy (set2);
153
154 GNUNET_SCHEDULER_shutdown ();
155}
156
157
158void check_new_set_count (void *cls)
159{
160 check_count (set2, "new set", 2, &test_done, NULL);
161}
162
163
164void copy_done (void *cls, struct GNUNET_SET_Handle *new_set)
165{
166 printf ("copy done\n");
167 set2 = new_set;
168 remove_element_str (set2, "spam");
169 // Check that set1 didn't change.
170 check_count (set1, "old set", 3, &check_new_set_count, NULL);
171}
172
173
174void test_copy (void *cls)
175{
176 printf ("about to copy\n");
177 GNUNET_SET_copy_lazy (set1, copy_done, NULL);
178}
179
180
181
182/**
183 * Signature of the 'main' function for a (single-peer) testcase that
184 * is run using 'GNUNET_TESTING_peer_run'.
185 *
186 * @param cls closure
187 * @param cfg configuration of the peer that was started
188 * @param peer identity of the peer that was created
189 */
190static void
191run (void *cls,
192 const struct GNUNET_CONFIGURATION_Handle *cfg,
193 struct GNUNET_TESTING_Peer *peer)
194{
195 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
196 &timeout_fail,
197 NULL);
198
199 config = cfg;
200 GNUNET_TESTING_peer_get_identity (peer,
201 &local_id);
202
203 set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
204 add_element_str (set1, "foo");
205 add_element_str (set1, "bar");
206 remove_element_str (set1, "foo");
207 add_element_str (set1, "spam");
208 add_element_str (set1, "eggs");
209
210 check_count (set1, "initial test", 3, &test_copy, NULL);
211}
212
213
214int
215main (int argc, char **argv)
216{
217 if (0 != GNUNET_TESTING_peer_run ("test_set_api",
218 "test_set.conf",
219 &run, NULL))
220 {
221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failed to start testing peer\n");
222 return 1;
223 }
224 return ret;
225}