use c99
[oweals/gnunet.git] / src / set / test_set_union_copy.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015 GNUnet e.V.
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  */
35 static int ret;
36
37 static struct GNUNET_PeerIdentity local_id;
38
39 static struct GNUNET_SET_Handle *set1;
40
41 static struct GNUNET_SET_Handle *set2;
42
43 static const struct GNUNET_CONFIGURATION_Handle *config;
44
45 static struct GNUNET_SCHEDULER_Task *tt;
46
47
48 static void
49 add_element_str (struct GNUNET_SET_Handle *set, char *str)
50 {
51   struct GNUNET_SET_Element element;
52
53   element.element_type = 0;
54   element.data = str;
55   element.size = strlen (str);
56
57   GNUNET_SET_add_element (set, &element, NULL, NULL);
58 }
59
60
61 static void
62 remove_element_str (struct GNUNET_SET_Handle *set, char *str)
63 {
64   struct GNUNET_SET_Element element;
65
66   element.element_type = 0;
67   element.data = str;
68   element.size = strlen (str);
69
70   GNUNET_SET_remove_element (set, &element, NULL, NULL);
71 }
72
73
74 /**
75  * Signature of the main function of a task.
76  *
77  * @param cls closure
78  */
79 static void
80 timeout_fail (void *cls)
81 {
82   tt = NULL;
83   GNUNET_SCHEDULER_shutdown ();
84   ret = 1;
85 }
86
87 typedef void (*Continuation) (void *cls);
88
89
90 struct CountIterClosure
91 {
92   unsigned int expected_count;
93   unsigned int ongoing_count;
94   Continuation cont;
95   void *cont_cls;
96   char *what;
97 };
98
99
100 static int
101 check_count_iter (void *cls,
102                   const struct GNUNET_SET_Element *element)
103 {
104   struct CountIterClosure *ci_cls = cls;
105
106   if (NULL == element)
107   {
108     if (ci_cls->expected_count != ci_cls->ongoing_count)
109     {
110       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111                   "Expected count (what: %s) to be %u, but it's actually %u\n",
112                   ci_cls->what,
113                   ci_cls->expected_count, ci_cls->ongoing_count);
114       ret = 1;
115       return GNUNET_NO;
116     }
117     ci_cls->cont (ci_cls->cont_cls);
118     return GNUNET_NO;
119   }
120
121   ci_cls->ongoing_count += 1;
122   return GNUNET_YES;
123 }
124
125
126 static void
127 check_count (struct GNUNET_SET_Handle *set,
128              char *what,
129              unsigned int expected_count,
130              Continuation cont,
131              void *cont_cls)
132 {
133   struct CountIterClosure *ci_cls = GNUNET_new (struct CountIterClosure);
134
135   ci_cls->expected_count = expected_count;
136   ci_cls->ongoing_count = 0;
137   ci_cls->cont = cont;
138   ci_cls->cont_cls = cont_cls;
139   ci_cls->what = what;
140
141   GNUNET_assert (GNUNET_YES == GNUNET_SET_iterate (set, check_count_iter, ci_cls));
142 }
143
144
145 static void
146 test_done (void *cls)
147 {
148   if (NULL != set1)
149     GNUNET_SET_destroy (set1);
150   if (NULL != set2)
151     GNUNET_SET_destroy (set2);
152   GNUNET_SCHEDULER_cancel (tt);
153   tt = NULL;
154   GNUNET_SCHEDULER_shutdown ();
155 }
156
157
158 static void
159 check_new_set_count (void *cls)
160 {
161   check_count (set2, "new set", 4, &test_done, NULL);
162 }
163
164
165 static void
166 copy_done (void *cls, struct GNUNET_SET_Handle *new_set)
167 {
168   printf ("copy done\n");
169   set2 = new_set;
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);
178 }
179
180
181 static void
182 test_copy (void *cls)
183 {
184   printf ("about to copy\n");
185   GNUNET_SET_copy_lazy (set1, copy_done, NULL);
186 }
187
188
189
190 /**
191  * Signature of the 'main' function for a (single-peer) testcase that
192  * is run using 'GNUNET_TESTING_peer_run'.
193  *
194  * @param cls closure
195  * @param cfg configuration of the peer that was started
196  * @param peer identity of the peer that was created
197  */
198 static void
199 run (void *cls,
200      const struct GNUNET_CONFIGURATION_Handle *cfg,
201      struct GNUNET_TESTING_Peer *peer)
202 {
203   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
204                                      &timeout_fail,
205                                      NULL);
206
207   config = cfg;
208   GNUNET_TESTING_peer_get_identity (peer,
209                                     &local_id);
210
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");
223
224   check_count (set1, "initial test", 3, &test_copy, NULL);
225 }
226
227
228 int
229 main (int argc, char **argv)
230 {
231   if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
232                                     "test_set.conf",
233                                     &run, NULL))
234   {
235     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failed to start testing peer\n");
236     return 1;
237   }
238   return ret;
239 }