diff --git a/test/events.c b/test/events.c index 2f822b12..c0931fd3 100644 --- a/test/events.c +++ b/test/events.c @@ -17,26 +17,53 @@ static void handler(void *sender, void *args, void *data) h->args = args; } -#define checkHandlerRun(h, whichSender, whichArgs) \ +struct baseParams { + void (*impl)(testingT *t, void *data); + bool global; + void *sender; + void *args; +}; + +#define checkHandlerRun(h, bp) \ if (!h.run) \ testingTErrorf(t, "%s not run", h.name); \ else { \ - if (h.sender != whichSender) \ + if (h.sender != bp.sender) \ diff_2str(t, "incorrect sender seen by", h.name, \ - "%p", h.sender, whichSender); \ - if (h.args != whichArgs) \ + "%p", h.sender, bp.sender); \ + if (h.args != bp.args) \ diff_2str(t, "incorrect args seen by", h.name, \ - "%p", h.args, whichArgs); \ + "%p", h.args, bp.args); \ } #define checkHandlerNotRun(h) \ if (h.run) \ testingTErrorf(t, "%s run; should not have been", h.name); +static void runArgsSubtests(testingT *t, void *data) +{ + struct baseParams *p = (struct baseParams *) data; + + p->args = &p; + testingTRun(t, "Args", p->impl, data); + p->args = NULL; + testingTRun(t, "NoArgs", p->impl, data); +} + +static void runGlobalSubtests(testingT *t, void *data) +{ + struct baseParams *p = (struct baseParams *) data; + + p->global = true; + p->sender = NULL; + testingTRun(t, "Global", runArgsSubtests, data); + p->global = false; + p->sender = t; + testingTRun(t, "Nonglobal", runArgsSubtests, data); +} + struct basicEventsSingleHandlerParams { - bool global; - void *sender; - void *args; + struct baseParams bp; }; static void basicEventsSingleHandlerImpl(testingT *t, void *data) @@ -48,25 +75,15 @@ static void basicEventsSingleHandlerImpl(testingT *t, void *data) memset(&opts, 0, sizeof (uiEventOptions)); opts.Size = sizeof (uiEventOptions); - opts.Global = p->global; + opts.Global = p->bp.global; e = uiNewEvent(&opts); memset(&h, 0, sizeof (struct handler)); h.name = "handler"; - uiEventAddHandler(e, handler, p->sender, &h); + uiEventAddHandler(e, handler, p->bp.sender, &h); - uiEventFire(e, p->sender, p->args); - checkHandlerRun(h, p->sender, p->args); -} - -static void basicEventsSingleHandlerSubtestArgs(testingT *t, void *data) -{ - struct basicEventsSingleHandlerParams *p = (struct basicEventsSingleHandlerParams *) data; - - p->args = &p; - testingTRun(t, "Args", basicEventsSingleHandlerImpl, p); - p->args = NULL; - testingTRun(t, "NoArgs", basicEventsSingleHandlerImpl, p); + uiEventFire(e, p->bp.sender, p->bp.args); + checkHandlerRun(h, p->bp); } testingTest(BasicEventsSingleHandler) @@ -74,18 +91,12 @@ testingTest(BasicEventsSingleHandler) struct basicEventsSingleHandlerParams p; memset(&p, 0, sizeof (struct basicEventsSingleHandlerParams)); - p.global = true; - p.sender = NULL; - testingTRun(t, "Global", basicEventsSingleHandlerSubtestArgs, &p); - p.global = false; - p.sender = t; - testingTRun(t, "Nonglobal", basicEventsSingleHandlerSubtestArgs, &p); + p.bp.impl = basicEventsSingleHandlerImpl; + runGlobalSubtests(t, &p); } struct basicEventsAddDeleteParams { - bool global; - void *sender; - void *args; + struct baseParams bp; uiEvent *e; struct handler h[6]; struct handler hbase[6]; @@ -93,14 +104,14 @@ struct basicEventsAddDeleteParams { int newHandler1, newHandler2, newHandler3; }; -testingTest(BasicEventsAddDeleteEventHandlers) +static void basicEventsAddDeleteEventHandlersImpl(testingT *t, void *data) { struct basicEventsAddDeleteParams *p = (struct basicEventsAddDeleteParams *) data; uiEventOptions opts; memset(&opts, 0, sizeof (uiEventOptions)); opts.Size = sizeof (uiEventOptions); - opts.Global = p->global; + opts.Global = p->bp.global; p->e = uiNewEvent(&opts); memset(p->hbase, 0, 6 * sizeof (struct handler)); @@ -112,15 +123,15 @@ testingTest(BasicEventsAddDeleteEventHandlers) p->hbase[5].name = "new handler 3"; testingTLogf(t, "*** initial handlers"); - p->handler1 = uiEventAddHandler(p->e, handler, p->sender, p->h + 0); - p->handler2 = uiEventAddHandler(p->e, handler, p->sender, p->h + 1); - p->handler3 = uiEventAddHandler(p->e, handler, p->sender, p->h + 2); + p->handler1 = uiEventAddHandler(p->e, handler, p->bp.sender, p->h + 0); + p->handler2 = uiEventAddHandler(p->e, handler, p->bp.sender, p->h + 1); + p->handler3 = uiEventAddHandler(p->e, handler, p->bp.sender, p->h + 2); memmove(p->h, p->hbase, 6 * sizeof (struct handler)); - uiEventFire(p->e, p->sender, p->args); - checkHandlerRun(p->h[0], p->sender, p->args); - checkHandlerRun(p->h[1], p->sender, p->args); - checkHandlerRun(p->h[2], p->sender, p->args); + uiEventFire(p->e, p->bp.sender, p->bp.args); + checkHandlerRun(p->h[0], p->bp); + checkHandlerRun(p->h[1], p->bp); + checkHandlerRun(p->h[2], p->bp); checkHandlerNotRun(p->h[3]); checkHandlerNotRun(p->h[4]); checkHandlerNotRun(p->h[5]); @@ -129,51 +140,51 @@ testingTest(BasicEventsAddDeleteEventHandlers) uiEventDeleteHandler(p->e, p->handler2); memmove(p->h, p->hbase, 6 * sizeof (struct handler)); - uiEventFire(p->e, p->sender, p->args); - checkHandlerRun(p->h[0], p->sender, p->args); + uiEventFire(p->e, p->bp.sender, p->bp.args); + checkHandlerRun(p->h[0], p->bp); checkHandlerNotRun(p->h[1]); - checkHandlerRun(p->h[2], p->sender, p->args); + checkHandlerRun(p->h[2], p->bp); checkHandlerNotRun(p->h[3]); checkHandlerNotRun(p->h[4]); checkHandlerNotRun(p->h[5]); testingTLogf(t, "*** adding handler after deleting a handler from the middle"); - p->newHandler1 = uiEventAddHandler(p->e, handler, p->sender, p->h + 3); + p->newHandler1 = uiEventAddHandler(p->e, handler, p->bp.sender, p->h + 3); memmove(p->h, p->hbase, 6 * sizeof (struct handler)); - uiEventFire(p->e, p->sender, p->args); - checkHandlerRun(p->h[0], p->sender, p->args); + uiEventFire(p->e, p->bp.sender, p->bp.args); + checkHandlerRun(p->h[0], p->bp); checkHandlerNotRun(p->h[1]); - checkHandlerRun(p->h[2], p->sender, p->args); - checkHandlerRun(p->h[3], p->sender, p->args); + checkHandlerRun(p->h[2], p->bp); + checkHandlerRun(p->h[3], p->bp); checkHandlerNotRun(p->h[4]); checkHandlerNotRun(p->h[5]); testingTLogf(t, "*** deleting first handler added and adding another"); uiEventDeleteHandler(p->e, p->handler1); - p->newHandler2 = uiEventAddHandler(p->e, handler, p->sender, p->h + 4); + p->newHandler2 = uiEventAddHandler(p->e, handler, p->bp.sender, p->h + 4); memmove(p->h, p->hbase, 6 * sizeof (struct handler)); - uiEventFire(p->e, p->sender, p->args); + uiEventFire(p->e, p->bp.sender, p->bp.args); checkHandlerNotRun(p->h[0]); checkHandlerNotRun(p->h[1]); - checkHandlerRun(p->h[2], p->sender, p->args); - checkHandlerRun(p->h[3], p->sender, p->args); - checkHandlerRun(p->h[4], p->sender, p->args); + checkHandlerRun(p->h[2], p->bp); + checkHandlerRun(p->h[3], p->bp); + checkHandlerRun(p->h[4], p->bp); checkHandlerNotRun(p->h[5]); testingTLogf(t, "*** deleting most recently added handler and adding another"); uiEventDeleteHandler(p->e, p->newHandler2); - p->newHandler3 = uiEventAddHandler(p->e, handler, p->sender, p->h + 5); + p->newHandler3 = uiEventAddHandler(p->e, handler, p->bp.sender, p->h + 5); memmove(p->h, p->hbase, 6 * sizeof (struct handler)); - uiEventFire(p->e, p->sender, p->args); + uiEventFire(p->e, p->bp.sender, p->bp.args); checkHandlerNotRun(p->h[0]); checkHandlerNotRun(p->h[1]); - checkHandlerRun(p->h[2], p->sender, p->args); - checkHandlerRun(p->h[3], p->sender, p->args); + checkHandlerRun(p->h[2], p->bp); + checkHandlerRun(p->h[3], p->bp); checkHandlerNotRun(p->h[4]); - checkHandlerRun(p->h[5], p->sender, p->args); + checkHandlerRun(p->h[5], p->bp); testingTLogf(t, "*** deleting all handlers"); uiEventDeleteHandler(p->e, p->handler3); @@ -181,7 +192,7 @@ testingTest(BasicEventsAddDeleteEventHandlers) uiEventDeleteHandler(p->e, p->newHandler3); memmove(p->h, p->hbase, 6 * sizeof (struct handler)); - uiEventFire(p->e, p->sender, p->args); + uiEventFire(p->e, p->bp.sender, p->bp.args); checkHandlerNotRun(p->h[0]); checkHandlerNotRun(p->h[1]); checkHandlerNotRun(p->h[2]); @@ -190,11 +201,11 @@ testingTest(BasicEventsAddDeleteEventHandlers) checkHandlerNotRun(p->h[5]); testingTLogf(t, "*** adding handler after deleting all handlers"); - uiEventAddHandler(p->e, handler, p->sender, p->h + 0); + uiEventAddHandler(p->e, handler, p->bp.sender, p->h + 0); memmove(p->h, p->hbase, 6 * sizeof (struct handler)); - uiEventFire(p->e, p->sender, p->args); - checkHandlerRun(p->h[0], p->sender, p->args); + uiEventFire(p->e, p->bp.sender, p->bp.args); + checkHandlerRun(p->h[0], p->bp); checkHandlerNotRun(p->h[1]); checkHandlerNotRun(p->h[2]); checkHandlerNotRun(p->h[3]); @@ -202,6 +213,15 @@ testingTest(BasicEventsAddDeleteEventHandlers) checkHandlerNotRun(p->h[5]); } +testingTest(BasicEventsAddDeleteEventHandlers) +{ + struct basicEventsAddDeleteParams p; + + memset(&p, 0, sizeof (struct basicEventsAddDeleteParams)); + p.bp.impl = basicEventsAddDeleteEventHandlersImpl; + runGlobalSubtests(t, &p); +} + testingTest(EventErrors) { // TODO