rework test_brandt
This commit is contained in:
parent
45b930823c
commit
e333dd0183
191
test_brandt.c
191
test_brandt.c
@ -36,42 +36,39 @@ struct msg {
|
|||||||
size_t buf_len;
|
size_t buf_len;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct testcase {
|
||||||
struct test {
|
uint16_t n;
|
||||||
uint16_t ret;
|
uint16_t k;
|
||||||
|
uint16_t *bids;
|
||||||
uint16_t m;
|
uint16_t m;
|
||||||
uint16_t outcome_public;
|
uint16_t outcome_public;
|
||||||
|
uint16_t ret;
|
||||||
|
struct BRANDT_Auction **ad;
|
||||||
|
uint16_t *id;
|
||||||
|
uint16_t *result_called;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static uint16_t *id;
|
static struct testcase tcase;
|
||||||
static uint16_t *result_called;
|
|
||||||
static struct BRANDT_Auction **ad;
|
|
||||||
static uint16_t unexpected_result;
|
|
||||||
|
|
||||||
/* TEST CONFIGURATION */
|
|
||||||
static const uint16_t bidders = 4;
|
|
||||||
static const uint16_t prizes = 3;
|
|
||||||
static const uint16_t bids[] = { 1, 2, 0, 2 };
|
|
||||||
|
|
||||||
|
|
||||||
static struct BRANDT_Result *
|
static struct BRANDT_Result *
|
||||||
expected_outcome (uint16_t m, uint16_t outcome_public, uint16_t i)
|
expected_outcome (uint16_t i)
|
||||||
{
|
{
|
||||||
int32_t highest_bidder = -1;
|
int32_t highest_bidder = -1;
|
||||||
int32_t highest_bid = -1;
|
int32_t highest_bid = -1;
|
||||||
struct BRANDT_Result *ret;
|
struct BRANDT_Result *ret;
|
||||||
|
|
||||||
for (uint16_t i = 0; i < bidders; i++)
|
for (uint16_t h = 0; h < tcase.n; h++)
|
||||||
{
|
{
|
||||||
if (bids[i] > highest_bid)
|
if (tcase.bids[h] > highest_bid)
|
||||||
{
|
{
|
||||||
highest_bid = bids[i];
|
highest_bid = tcase.bids[h];
|
||||||
highest_bidder = i;
|
highest_bidder = h;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!outcome_public && !(i == highest_bidder || i == bidders))
|
if (!tcase.outcome_public && !(i == highest_bidder || i == tcase.n))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
ret = GNUNET_new (struct BRANDT_Result);
|
ret = GNUNET_new (struct BRANDT_Result);
|
||||||
@ -88,7 +85,7 @@ bidder_start (void *arg)
|
|||||||
uint16_t i = *(uint16_t *)arg;
|
uint16_t i = *(uint16_t *)arg;
|
||||||
|
|
||||||
weprintf ("starting bidder %d", i);
|
weprintf ("starting bidder %d", i);
|
||||||
BRANDT_bidder_start (ad[i], i, bidders);
|
BRANDT_bidder_start (tcase.ad[i], i, tcase.n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -100,8 +97,9 @@ transfer_message (void *arg)
|
|||||||
|
|
||||||
weprintf ("xfer msg %d %x from %d to %d", ntohl (
|
weprintf ("xfer msg %d %x from %d to %d", ntohl (
|
||||||
h->msg_type), arg, m->sender, m->receiver);
|
h->msg_type), arg, m->sender, m->receiver);
|
||||||
BRANDT_got_message (ad[m->receiver], m->sender, m->buf, m->buf_len);
|
BRANDT_got_message (tcase.ad[m->receiver], m->sender, m->buf, m->buf_len);
|
||||||
free (arg);
|
GNUNET_free (m->buf);
|
||||||
|
GNUNET_free (m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -110,16 +108,16 @@ cb_start (void *auction_closure)
|
|||||||
{
|
{
|
||||||
uint16_t *s = (uint16_t *)auction_closure;
|
uint16_t *s = (uint16_t *)auction_closure;
|
||||||
|
|
||||||
if (!s || bidders != *s)
|
if (tcase.n != *s)
|
||||||
{
|
{
|
||||||
weprintf ("start callback called from bidder");
|
weprintf ("start callback called from bidder");
|
||||||
_exit (1);
|
_exit (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint16_t i = 0; i < bidders; i++)
|
for (uint16_t i = 0; i < tcase.n; i++)
|
||||||
GNUNET_SCHEDULER_add_now (&bidder_start, &id[i]);
|
GNUNET_SCHEDULER_add_now (&bidder_start, &tcase.id[i]);
|
||||||
|
|
||||||
return bidders;
|
return tcase.n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -131,7 +129,7 @@ cb_broadcast (void *auction_closure,
|
|||||||
uint16_t *s = (uint16_t *)auction_closure;
|
uint16_t *s = (uint16_t *)auction_closure;
|
||||||
struct msg *m;
|
struct msg *m;
|
||||||
|
|
||||||
for (uint16_t i = 0; i <= bidders; i++)
|
for (uint16_t i = 0; i <= tcase.n; i++)
|
||||||
{
|
{
|
||||||
if (i == *s)
|
if (i == *s)
|
||||||
continue;
|
continue;
|
||||||
@ -157,7 +155,7 @@ cb_unicast (void *auction_closure,
|
|||||||
|
|
||||||
m = GNUNET_new (struct msg);
|
m = GNUNET_new (struct msg);
|
||||||
m->sender = *s;
|
m->sender = *s;
|
||||||
m->receiver = bidders; /* == seller */
|
m->receiver = tcase.n; /* == seller */
|
||||||
m->buf = GNUNET_new_array (msg_len, unsigned char);
|
m->buf = GNUNET_new_array (msg_len, unsigned char);
|
||||||
memcpy (m->buf, msg, msg_len);
|
memcpy (m->buf, msg, msg_len);
|
||||||
m->buf_len = msg_len;
|
m->buf_len = msg_len;
|
||||||
@ -173,143 +171,142 @@ cb_result (void *auction_closure,
|
|||||||
uint16_t results_len)
|
uint16_t results_len)
|
||||||
{
|
{
|
||||||
uint16_t *s = (uint16_t *)auction_closure;
|
uint16_t *s = (uint16_t *)auction_closure;
|
||||||
struct BRANDT_Result *must = expected_outcome (ad[*s]->m,
|
struct BRANDT_Result *must = expected_outcome (*s);
|
||||||
ad[*s]->outcome_public,
|
|
||||||
*s);
|
|
||||||
|
|
||||||
if (0 == results_len)
|
if (0 == results_len)
|
||||||
{
|
{
|
||||||
weprintf ("result from agent %d: %p", *s, must);
|
weprintf ("expected result is: %p", must);
|
||||||
weprintf ("expected result is: (nil)");
|
weprintf ("computed result is: (nil) (from agent %d)", *s);
|
||||||
|
|
||||||
if (NULL != must)
|
if (NULL != must)
|
||||||
unexpected_result = 1;
|
tcase.ret = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint16_t i = 0; i < results_len; i++)
|
for (uint16_t i = 0; i < results_len; i++)
|
||||||
{
|
{
|
||||||
weprintf ("result from agent %d: bidder %d got status %d with price %d",
|
|
||||||
*s,
|
|
||||||
results[i].bidder,
|
|
||||||
results[i].status,
|
|
||||||
results[i].price);
|
|
||||||
weprintf ("expected result is: bidder %d got status %d with price %d",
|
weprintf ("expected result is: bidder %d got status %d with price %d",
|
||||||
must[i].bidder,
|
must[i].bidder,
|
||||||
must[i].status,
|
must[i].status,
|
||||||
must[i].price);
|
must[i].price);
|
||||||
|
weprintf (
|
||||||
|
"computed result is: bidder %d got status %d with price %d (from agent %d)",
|
||||||
|
results[i].bidder,
|
||||||
|
results[i].status,
|
||||||
|
results[i].price,
|
||||||
|
*s);
|
||||||
|
|
||||||
if (NULL == must ||
|
if (NULL == must ||
|
||||||
must[i].bidder != results[i].bidder ||
|
must[i].bidder != results[i].bidder ||
|
||||||
must[i].status != results[i].status ||
|
must[i].status != results[i].status ||
|
||||||
must[i].price != results[i].price)
|
must[i].price != results[i].price)
|
||||||
unexpected_result = 1;
|
tcase.ret = 1;
|
||||||
}
|
}
|
||||||
result_called[*s] = 1;
|
|
||||||
|
tcase.result_called[*s] = 1;
|
||||||
|
if (must)
|
||||||
|
GNUNET_free (must);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
run_auction (void *arg)
|
run_auction (void *arg)
|
||||||
{
|
{
|
||||||
struct test *asetup = arg;
|
const char description[] = "test description for test_auction";
|
||||||
const char description[] = "test description for test_auctions";
|
|
||||||
void *desc;
|
void *desc;
|
||||||
size_t desc_len;
|
size_t desc_len;
|
||||||
|
|
||||||
ad = GNUNET_new_array (bidders + 1, struct BRANDT_Auction *);
|
tcase.ad[tcase.n] = BRANDT_new (&cb_result,
|
||||||
|
|
||||||
ad[bidders] = BRANDT_new (&cb_result,
|
|
||||||
&cb_broadcast,
|
&cb_broadcast,
|
||||||
&cb_start,
|
&cb_start,
|
||||||
&id[bidders],
|
&tcase.id[tcase.n],
|
||||||
&desc,
|
&desc,
|
||||||
&desc_len,
|
&desc_len,
|
||||||
description,
|
description,
|
||||||
sizeof (description),
|
sizeof (description),
|
||||||
GNUNET_TIME_absolute_get (),
|
GNUNET_TIME_absolute_get (),
|
||||||
GNUNET_TIME_UNIT_MINUTES,
|
GNUNET_TIME_UNIT_MINUTES,
|
||||||
prizes, /* number of prizes */
|
tcase.k, /* number of prizes */
|
||||||
asetup->m, /* m */
|
tcase.m, /* m */
|
||||||
asetup->outcome_public); /* outcome public */
|
tcase.outcome_public); /* outcome public */
|
||||||
if (!ad[bidders])
|
if (!tcase.ad[tcase.n])
|
||||||
{
|
{
|
||||||
weprintf ("BRANDT_new() failed.");
|
weprintf ("BRANDT_new() failed.");
|
||||||
_exit (1);
|
_exit (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint16_t i = 0; i < bidders; i++)
|
for (uint16_t i = 0; i < tcase.n; i++)
|
||||||
{
|
{
|
||||||
ad[i] = BRANDT_join (&cb_result,
|
tcase.ad[i] = BRANDT_join (&cb_result,
|
||||||
&cb_broadcast,
|
&cb_broadcast,
|
||||||
&cb_unicast,
|
&cb_unicast,
|
||||||
&id[i],
|
&tcase.id[i],
|
||||||
desc,
|
desc,
|
||||||
desc_len,
|
desc_len,
|
||||||
description,
|
description,
|
||||||
sizeof (description),
|
sizeof (description),
|
||||||
bids[i]); /* bid */
|
tcase.bids[i]); /* bid */
|
||||||
if (!ad[i])
|
if (!tcase.ad[i])
|
||||||
{
|
{
|
||||||
weprintf ("BRANDT_join() failed.");
|
weprintf ("BRANDT_join() failed.");
|
||||||
_exit (1);
|
tcase.ret = 1;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ad[bidders]->k != ad[i]->k ||
|
if (tcase.ad[tcase.n]->k != tcase.ad[i]->k ||
|
||||||
ad[bidders]->m != ad[i]->m ||
|
tcase.ad[tcase.n]->m != tcase.ad[i]->m ||
|
||||||
ad[bidders]->outcome_public != ad[i]->outcome_public ||
|
tcase.ad[tcase.n]->outcome_public != tcase.ad[i]->outcome_public ||
|
||||||
ad[bidders]->time_start.abs_value_us
|
tcase.ad[tcase.n]->time_start.abs_value_us
|
||||||
!= ad[i]->time_start.abs_value_us ||
|
!= tcase.ad[i]->time_start.abs_value_us ||
|
||||||
ad[bidders]->time_round.rel_value_us
|
tcase.ad[tcase.n]->time_round.rel_value_us
|
||||||
!= ad[i]->time_round.rel_value_us ||
|
!= tcase.ad[i]->time_round.rel_value_us ||
|
||||||
!ad[bidders]->seller_mode || /* todo: split out */
|
!tcase.ad[tcase.n]->seller_mode || /* todo: split out */
|
||||||
ad[i]->seller_mode)
|
tcase.ad[i]->seller_mode)
|
||||||
{
|
{
|
||||||
weprintf ("error/mismatch in basic auction data");
|
weprintf ("error/mismatch in basic auction data");
|
||||||
_exit (1);
|
tcase.ret = 1;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
test_auctions ()
|
test_auction (uint16_t n,
|
||||||
|
uint16_t k,
|
||||||
|
uint16_t *bids,
|
||||||
|
uint16_t m,
|
||||||
|
uint16_t outcome_public)
|
||||||
{
|
{
|
||||||
id = GNUNET_new_array (bidders + 1, uint16_t);
|
tcase.n = n;
|
||||||
for (uint16_t i = 0; i <= bidders; i++)
|
tcase.k = k;
|
||||||
id[i] = i;
|
tcase.bids = bids;
|
||||||
|
tcase.m = m;
|
||||||
|
tcase.outcome_public = outcome_public;
|
||||||
|
tcase.ret = 0;
|
||||||
|
|
||||||
for (size_t atype = 0; atype < auction_last; atype++)
|
tcase.ad = GNUNET_new_array (tcase.n + 1, struct BRANDT_Auction *);
|
||||||
|
tcase.id = GNUNET_new_array (tcase.n + 1, uint16_t);
|
||||||
|
for (uint16_t i = 0; i <= tcase.n; i++)
|
||||||
|
tcase.id[i] = i;
|
||||||
|
tcase.result_called = GNUNET_new_array (tcase.n + 1, uint16_t);
|
||||||
|
|
||||||
|
GNUNET_SCHEDULER_run (&run_auction, NULL);
|
||||||
|
|
||||||
|
for (uint16_t i = 0; i <= tcase.n; i++)
|
||||||
{
|
{
|
||||||
if (auction_firstPrice != atype) /* others not yet implemented */
|
BRANDT_destroy (tcase.ad[i]);
|
||||||
continue;
|
if (!tcase.result_called[i])
|
||||||
|
|
||||||
for (size_t oc = 0; oc < outcome_last; oc++)
|
|
||||||
{
|
|
||||||
struct test asetup;
|
|
||||||
|
|
||||||
result_called = GNUNET_new_array (bidders + 1, uint16_t);
|
|
||||||
unexpected_result = 0;
|
|
||||||
asetup.ret = 0;
|
|
||||||
asetup.m = atype;
|
|
||||||
asetup.outcome_public = oc;
|
|
||||||
GNUNET_SCHEDULER_run (&run_auction, &asetup);
|
|
||||||
|
|
||||||
for (uint16_t i = 0; i <= bidders; i++)
|
|
||||||
{
|
|
||||||
BRANDT_destroy (ad[i]);
|
|
||||||
if (!result_called[i])
|
|
||||||
{
|
{
|
||||||
weprintf ("result callback not called for bidder %d", i);
|
weprintf ("result callback not called for bidder %d", i);
|
||||||
unexpected_result = 1;
|
tcase.ret = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unexpected_result)
|
GNUNET_free (tcase.ad);
|
||||||
return 0;
|
GNUNET_free (tcase.id);
|
||||||
}
|
GNUNET_free (tcase.result_called);
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
return tcase.ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -322,8 +319,8 @@ main (int argc, char *argv[])
|
|||||||
edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16);
|
edc = GNUNET_CRYPTO_ecc_dlog_prepare (1024, 16);
|
||||||
BRANDT_init (edc);
|
BRANDT_init (edc);
|
||||||
|
|
||||||
if (!test_auctions())
|
ret |= test_auction (4, 3, (uint16_t[]) { 1, 2, 0, 2 }, 0, 1) ||
|
||||||
ret = 1;
|
test_auction (4, 3, (uint16_t[]) { 1, 2, 0, 2 }, 0, 0);
|
||||||
|
|
||||||
GNUNET_CRYPTO_ecc_dlog_release (edc);
|
GNUNET_CRYPTO_ecc_dlog_release (edc);
|
||||||
return ret;
|
return ret;
|
||||||
|
Loading…
Reference in New Issue
Block a user