- another fix to generation handling and lazy copying
[oweals/gnunet.git] / src / set / test_set_union_copy.c
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  */
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
46 static void
47 add_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
59 static void
60 remove_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  */
78 static void
79 timeout_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
88 typedef void (*Continuation) (void *cls);
89
90
91 struct 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
101 static int
102 check_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
128 void
129 check_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
147 void 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
158 void check_new_set_count (void *cls)
159 {
160   check_count (set2, "new set", 4, &test_done, NULL);
161 }
162
163
164 void 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   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);
176 }
177
178
179 void test_copy (void *cls)
180 {
181   printf ("about to copy\n");
182   GNUNET_SET_copy_lazy (set1, copy_done, NULL);
183 }
184
185
186
187 /**
188  * Signature of the 'main' function for a (single-peer) testcase that
189  * is run using 'GNUNET_TESTING_peer_run'.
190  *
191  * @param cls closure
192  * @param cfg configuration of the peer that was started
193  * @param peer identity of the peer that was created
194  */
195 static void
196 run (void *cls,
197      const struct GNUNET_CONFIGURATION_Handle *cfg,
198      struct GNUNET_TESTING_Peer *peer)
199 {
200   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
201                                 &timeout_fail,
202                                 NULL);
203
204   config = cfg;
205   GNUNET_TESTING_peer_get_identity (peer,
206                                     &local_id);
207
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");
220
221   check_count (set1, "initial test", 3, &test_copy, NULL);
222 }
223
224
225 int
226 main (int argc, char **argv)
227 {
228   if (0 != GNUNET_TESTING_peer_run ("test_set_union_copy",
229                                     "test_set.conf",
230                                     &run, NULL))
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failed to start testing peer\n");
233     return 1;
234   }
235   return ret;
236 }