aboutsummaryrefslogtreecommitdiff
path: root/test_brandt.c
diff options
context:
space:
mode:
authorMarkus Teich <markus.teich@stusta.mhn.de>2016-09-23 17:35:31 +0200
committerMarkus Teich <markus.teich@stusta.mhn.de>2016-09-23 17:35:31 +0200
commitf9394ba74208eacf5e4d7a0ca354394d4a34c982 (patch)
treefa3ebaa777356a78f0eaef641fce69b96fc7b46e /test_brandt.c
parent5a9e4cca4050316d4d8a826282cbaa12804586b3 (diff)
downloadlibbrandt-f9394ba74208eacf5e4d7a0ca354394d4a34c982.tar.gz
libbrandt-f9394ba74208eacf5e4d7a0ca354394d4a34c982.zip
test_brandt: actually check the outcome correctnes
Diffstat (limited to 'test_brandt.c')
-rw-r--r--test_brandt.c129
1 files changed, 106 insertions, 23 deletions
diff --git a/test_brandt.c b/test_brandt.c
index e56f076..da74af2 100644
--- a/test_brandt.c
+++ b/test_brandt.c
@@ -38,10 +38,49 @@ struct msg {
38}; 38};
39 39
40 40
41struct test {
42 uint16_t ret;
43 uint16_t m;
44 uint16_t outcome_public;
45};
46
47
41static uint16_t *id; 48static uint16_t *id;
49static uint16_t *result_called;
42static struct BRANDT_Auction **ad; 50static struct BRANDT_Auction **ad;
43static uint16_t bidders = 3; 51static uint16_t unexpected_result;
44static uint16_t prizes = 8; 52
53/* TEST CONFIGURATION */
54static const uint16_t bidders = 4;
55static const uint16_t prizes = 3;
56static const uint16_t bids[] = { 1, 2, 0, 2 };
57
58
59static struct BRANDT_Result *
60expected_outcome (uint16_t m, uint16_t outcome_public, uint16_t i)
61{
62 int32_t highest_bidder = -1;
63 int32_t highest_bid = -1;
64 struct BRANDT_Result *ret;
65
66 for (uint16_t i = 0; i < bidders; i++)
67 {
68 if (bids[i] > highest_bid)
69 {
70 highest_bid = bids[i];
71 highest_bidder = i;
72 }
73 }
74
75 if (!outcome_public && !(i == highest_bidder || i == bidders))
76 return NULL;
77
78 ret = GNUNET_new (struct BRANDT_Result);
79 ret->bidder = highest_bidder;
80 ret->price = highest_bid;
81 ret->status = BRANDT_bidder_won;
82 return ret;
83}
45 84
46 85
47static void 86static void
@@ -134,27 +173,49 @@ cb_result (void *auction_closure,
134 struct BRANDT_Result results[], 173 struct BRANDT_Result results[],
135 uint16_t results_len) 174 uint16_t results_len)
136{ 175{
137 uint16_t *s = (uint16_t *)auction_closure; 176 uint16_t *s = (uint16_t *)auction_closure;
177 struct BRANDT_Result *must = expected_outcome (ad[*s]->m,
178 ad[*s]->outcome_public,
179 *s);
138 180
139 if (0 == results_len) 181 if (0 == results_len)
140 weprintf ("result from agent %d: none", *s); 182 {
183 weprintf ("result from agent %d: %p", *s, must);
184 weprintf ("expected result is: (nil)");
185
186 if (NULL != must)
187 unexpected_result = 1;
188 }
141 189
142 for (uint16_t i = 0; i < results_len; i++) 190 for (uint16_t i = 0; i < results_len; i++)
191 {
143 weprintf ("result from agent %d: bidder %d got status %d with price %d", 192 weprintf ("result from agent %d: bidder %d got status %d with price %d",
144 *s, 193 *s,
145 results[i].bidder, 194 results[i].bidder,
146 results[i].status, 195 results[i].status,
147 results[i].price); 196 results[i].price);
197 weprintf ("expected result is: bidder %d got status %d with price %d",
198 must[i].bidder,
199 must[i].status,
200 must[i].price);
201
202 if (NULL == must ||
203 must[i].bidder != results[i].bidder ||
204 must[i].status != results[i].status ||
205 must[i].price != results[i].price)
206 unexpected_result = 1;
207 }
208 result_called[*s] = 1;
148} 209}
149 210
150 211
151static void 212static void
152run_new_join (void *arg) 213run_auction (void *arg)
153{ 214{
154 int *ret = arg; 215 struct test *asetup = arg;
155 const char description[] = "test description for test_new_join"; 216 const char description[] = "test description for test_auctions";
156 void *desc; 217 void *desc;
157 size_t desc_len; 218 size_t desc_len;
158 219
159 ad = GNUNET_new_array (bidders + 1, struct BRANDT_Auction *); 220 ad = GNUNET_new_array (bidders + 1, struct BRANDT_Auction *);
160 221
@@ -168,9 +229,9 @@ run_new_join (void *arg)
168 sizeof (description), 229 sizeof (description),
169 GNUNET_TIME_absolute_get (), 230 GNUNET_TIME_absolute_get (),
170 GNUNET_TIME_UNIT_MINUTES, 231 GNUNET_TIME_UNIT_MINUTES,
171 prizes, /* amount of possible prizes */ 232 prizes, /* number of prizes */
172 0, /* m */ 233 asetup->m, /* m */
173 1); /* outcome public */ 234 asetup->outcome_public); /* outcome public */
174 if (!ad[bidders]) 235 if (!ad[bidders])
175 { 236 {
176 weprintf ("BRANDT_new() failed."); 237 weprintf ("BRANDT_new() failed.");
@@ -187,7 +248,7 @@ run_new_join (void *arg)
187 desc_len, 248 desc_len,
188 description, 249 description,
189 sizeof (description), 250 sizeof (description),
190 3); /* bid */ 251 bids[i]); /* bid */
191 if (!ad[i]) 252 if (!ad[i])
192 { 253 {
193 weprintf ("BRANDT_join() failed."); 254 weprintf ("BRANDT_join() failed.");
@@ -208,26 +269,48 @@ run_new_join (void *arg)
208 _exit (1); 269 _exit (1);
209 } 270 }
210 } 271 }
211
212 *ret = 1;
213} 272}
214 273
215 274
216static int 275static int
217test_new_join () 276test_auctions ()
218{ 277{
219 int ret = 0;
220
221 id = GNUNET_new_array (bidders + 1, uint16_t); 278 id = GNUNET_new_array (bidders + 1, uint16_t);
222 for (uint16_t i = 0; i <= bidders; i++) 279 for (uint16_t i = 0; i <= bidders; i++)
223 id[i] = i; 280 id[i] = i;
224 281
225 GNUNET_SCHEDULER_run (&run_new_join, &ret); 282 for (size_t atype = 0; atype < auction_last; atype++)
283 {
284 if (auction_firstPrice != atype) /* others not yet implemented */
285 continue;
226 286
227 for (uint16_t i = 0; i <= bidders; i++) 287 for (size_t oc = 0; oc < outcome_last; oc++)
228 BRANDT_destroy (ad[i]); 288 {
289 struct test asetup;
290
291 result_called = GNUNET_new_array (bidders + 1, uint16_t);
292 unexpected_result = 0;
293 asetup.ret = 0;
294 asetup.m = atype;
295 asetup.outcome_public = oc;
296 GNUNET_SCHEDULER_run (&run_auction, &asetup);
297
298 for (uint16_t i = 0; i <= bidders; i++)
299 {
300 BRANDT_destroy (ad[i]);
301 if (!result_called[i])
302 {
303 weprintf ("result callback not called for bidder %d", i);
304 unexpected_result = 1;
305 }
306 }
307
308 if (unexpected_result)
309 return 0;
310 }
311 }
229 312
230 return ret; 313 return 1;
231} 314}
232 315
233 316
@@ -239,7 +322,7 @@ main (int argc, char *argv[])
239 edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16); 322 edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16);
240 BRANDT_init (edc); 323 BRANDT_init (edc);
241 324
242 RUN (test_new_join); 325 RUN (test_auctions);
243 326
244 GNUNET_CRYPTO_ecc_dlog_release (edc); 327 GNUNET_CRYPTO_ecc_dlog_release (edc);
245 return ret; 328 return ret;