From 0f4ad940ae1f039bd2d55fdad279e7118429846e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 25 Sep 2015 11:51:18 +0930 Subject: [PATCH] test/test_state_coverage: remove_event / add_event / have_event helpers. Signed-off-by: Rusty Russell --- test/test_state_coverage.c | 60 +++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/test/test_state_coverage.c b/test/test_state_coverage.c index a48bcc9e0..247756263 100644 --- a/test/test_state_coverage.c +++ b/test/test_state_coverage.c @@ -389,15 +389,51 @@ struct bitcoin_tx *bitcoin_anchor(const tal_t *ctx, return bitcoin_tx("anchor"); } -static void add_event(uint64_t *events, enum state_input input) +static bool have_event(uint64_t events, enum state_input input) +{ + return events & (1ULL << input); +} + +static bool add_event_(uint64_t *events, enum state_input input) { /* This is how they say "no event please" */ if (input == INPUT_NONE) - return; + return true; assert(input < 64); - assert(!(*events & (1ULL << input))); + if (have_event(*events, input)) + return false; *events |= (1ULL << input); + return true; +} + +static bool remove_event_(uint64_t *events, enum state_input input) +{ + /* This is how they say "no event please" */ + if (input == INPUT_NONE) + return true; + + assert(input < 64); + if (!have_event(*events, input)) + return false; + *events &= ~(1ULL << input); + return true; +} + +static void remove_event(uint64_t *events, enum state_input input) +{ +#ifdef NDEBUG +#error "Don't run tests with NDEBUG" +#endif + assert(remove_event_(events, input)); +} + +static void add_event(uint64_t *events, enum state_input input) +{ +#ifdef NDEBUG +#error "Don't run tests with NDEBUG" +#endif + assert(add_event_(events, input)); } struct watch { @@ -639,11 +675,12 @@ static const char *apply_effects(struct state_data *sdata, so make exceptions for BITCOIN_STEAL_DONE/BITCOIN_SPEND_THEIRS_DONE */ if (sdata->event_notifies & (1ULL << BITCOIN_STEAL_DONE) & effect->watch->events) - effect->watch->events &= ~(1ULL << BITCOIN_STEAL_DONE); + remove_event(&effect->watch->events, BITCOIN_STEAL_DONE); if (sdata->event_notifies & (1ULL << BITCOIN_SPEND_THEIRS_DONE) & effect->watch->events) - effect->watch->events &= ~(1ULL << BITCOIN_SPEND_THEIRS_DONE); + remove_event(&effect->watch->events, + BITCOIN_SPEND_THEIRS_DONE); if (sdata->event_notifies & effect->watch->events) return "event set twice"; @@ -682,7 +719,8 @@ static const char *apply_effects(struct state_data *sdata, sdata->pkt_inputs = false; /* Can no longer receive packet timeouts, either. */ - sdata->event_notifies &= ~(1ULL<event_notifies, + INPUT_CLOSE_COMPLETE_TIMEOUT); } if (effect->stop_commands) { if (!sdata->cmd_inputs) @@ -1029,12 +1067,12 @@ static void activate_event(struct state_data *sdata, enum state_input i) /* Events are not independent. */ switch (i) { case BITCOIN_ANCHOR_DEPTHOK: - /* Can't sent TIMEOUT */ - sdata->event_notifies &= ~(1ULL<event_notifies, BITCOIN_ANCHOR_TIMEOUT); break; case BITCOIN_ANCHOR_TIMEOUT: /* Can't sent DEPTHOK */ - sdata->event_notifies &= ~(1ULL<event_notifies, BITCOIN_ANCHOR_DEPTHOK); break; default: ; @@ -1059,13 +1097,13 @@ static struct trail *run_peer(const struct state_data *sdata, /* Try the event notifiers */ old_notifies = copy.event_notifies; for (i = 0; i < 64; i++) { - if (!(copy.event_notifies & (1ULL << i))) + if (!have_event(copy.event_notifies, i)) continue; /* Don't re-fire (except OTHERSPEND/THEIRSPEND can reoccur) */ if (i != BITCOIN_ANCHOR_OTHERSPEND && i != BITCOIN_ANCHOR_THEIRSPEND) - copy.event_notifies &= ~(1ULL << i); + remove_event(©.event_notifies, i); activate_event(©, i); t = try_input(©, i, normalpath, errorpath, depth, hist); if (t)