Started deinterlacing the handler got and want parameters into struct handler.

This commit is contained in:
Pietro Gagliardi 2019-05-25 22:07:56 -04:00
parent 3cd08a5b2d
commit a8ab5be01a
1 changed files with 177 additions and 153 deletions

View File

@ -4,9 +4,19 @@
#include "test.h" #include "test.h"
struct handler { struct handler {
bool run; int id;
void *sender; bool validID;
void *args; const char *name;
bool gotRun;
void *gotSender;
void *gotArgs;
bool wantRun;
void *wantSender;
void *wantArgs;
bool wantBlocked;
int *runCount; int *runCount;
}; };
@ -14,64 +24,94 @@ static void handler(void *sender, void *args, void *data)
{ {
struct handler *h = (struct handler *) data; struct handler *h = (struct handler *) data;
h->run = true; h->gotRun = true;
h->sender = sender; h->gotSender = sender;
h->args = args; h->gotArgs = args;
(*(h->runCount))++; (*(h->runCount))++;
} }
struct runParams { static void resetGot(struct handler *h, int *runCount)
uiEvent *e; {
void *sender; h->gotRun = false;
void *args; h->gotSender = NULL;
int nHandlers; h->gotArgs = NULL;
const char **names; h->runCount = runCount;
struct handler *got; }
struct handler *want;
int wantRunCount;
};
#define wantRun(want, s, a) \ static void registerHandler(struct handler *h, uiEvent *e, void *sender, void *args)
want.run = true; \ {
want.sender = s; \ h->wantSender = sender;
want.args = a; h->wantArgs = args;
#define wantNotRun(want) \ h->id = uiEventAddHandler(e, handler, h->wantSender, h);
want.run = false; h->validID = true;
}
static void unregisterHandler(struct handler *h, uiEvent *e)
{
uiEventDeleteHandler(e, h->id);
h->validID = false;
}
static void wantRun(struct handler *h)
{
h->wantRun = true;
h->wantBlocked = false;
}
static void wantNotRun(struct handler *h)
{
h->wantRun = false;
h->wantBlocked = false;
}
static void wantBlocked(struct handler *h)
{
h->wantRun = false;
h->wantBlocked = true;
}
// TODO carry over the file nad line numbers somehow // TODO carry over the file nad line numbers somehow
static void run(testingT *t, struct runParams *p) static void run(testingT *t, uiEvent *e, void *sender, void *args, struct handler *handlers, int n, int wantRunCount)
{ {
int i; int i;
int gotRunCount; int gotRunCount;
struct handler *h;
memset(p->got, 0, p->nHandlers * sizeof (struct handler)); bool gotBlocked;
for (i = 0; i < p->nHandlers; i++)
p->got[i].runCount = &gotRunCount;
gotRunCount = 0; gotRunCount = 0;
uiEventFire(p->e, p->sender, p->args); for (i = 0; i < n; i++)
resetGot(handlers + i, &gotRunCount);
for (i = 0; i < p->nHandlers; i++) { uiEventFire(e, sender, args);
if (!p->want[i].run) {
if (p->got[i].run) h = handlers;
testingTErrorf(t, "%s run; should not have been", p->names[i]); for (i = 0; i < n; i++) {
continue; if (!h->gotRun && h->wantRun)
testingTErrorf(t, "%s not run; should have been", h->name);
else if (h->gotRun && !h->wantRun)
testingTErrorf(t, "%s run; should not have been", h->name);
if (h->gotRun && h->wantRun) {
// only check these if it was correctly run, to reduce noise if the above failed
if (h->gotSender != h->wantSender)
diff_2str(t, "incorrect sender seen by", h->name,
"%p", h->gotSender, h->wantSender);
if (h->gotArgs != h->wantArgs)
diff_2str(t, "incorrect args seen by", h->name,
"%p", h->gotArgs, h->wantArgs);
} }
// otherwise we want it to be run if (h->validID) {
if (!p->got[i].run) { // the following call will fail if the ID isn't valid
testingTErrorf(t, "%s not run; should have been", p->names[i]); gotBlocked = uiEventHandlerBlocked(e, h->id);
continue; if (!gotBlocked && h->wantBlocked)
testingTErrorf(t, "%s not blocked; should have been", h->name);
else if (gotBlocked && !h->wantBlocked)
testingTErrorf(t, "%s blocked; should not have been", h->name);
} }
if (p->got[i].sender != p->want[i].sender) h++;
diff_2str(t, "incorrect sender seen by", p->names[i],
"%p", p->got[i].sender, p->want[i].sender);
if (p->got[i].args != p->want[i].args)
diff_2str(t, "incorrect args seen by", p->names[i],
"%p", p->got[i].args, p->want[i].args);
} }
if (gotRunCount != p->wantRunCount) if (gotRunCount != wantRunCount)
diff(t, "incorrect number of handler runs", diff(t, "incorrect number of handler runs",
"%d", gotRunCount, p->wantRunCount); "%d", gotRunCount, wantRunCount);
} }
struct baseParams { struct baseParams {
@ -106,31 +146,22 @@ static void runGlobalSubtests(testingT *t, void *data)
static void basicEventFunctionalityImpl(testingT *t, void *data) static void basicEventFunctionalityImpl(testingT *t, void *data)
{ {
struct baseParams *p = (struct baseParams *) data; struct baseParams *p = (struct baseParams *) data;
struct runParams rp; uiEvent *e;
uiEventOptions opts; uiEventOptions opts;
const char *names[1]; struct handler h[1];
struct handler got[1];
struct handler want[1];
memset(&rp, 0, sizeof (struct runParams)); memset(h, 0, 1 * sizeof (struct handler));
rp.sender = p->sender; h[0].name = "handler";
rp.args = p->args;
rp.nHandlers = 1;
rp.names = names;
rp.names[0] = "handler";
rp.got = got;
rp.want = want;
memset(rp.want, 0, rp.nHandlers * sizeof (struct handler));
memset(&opts, 0, sizeof (uiEventOptions)); memset(&opts, 0, sizeof (uiEventOptions));
opts.Size = sizeof (uiEventOptions); opts.Size = sizeof (uiEventOptions);
opts.Global = p->global; opts.Global = p->global;
rp.e = uiNewEvent(&opts); e = uiNewEvent(&opts);
uiEventAddHandler(rp.e, handler, p->sender, rp.got + 0); registerHandler(h + 0, e, p->sender, p->args);
wantRun(rp.want[0], p->sender, p->args); wantRun(h + 0);
rp.wantRunCount = 1; run(t, e, p->sender, p->args,
run(t, &rp); h, 1, 1);
} }
testingTest(BasicEventFunctionality) testingTest(BasicEventFunctionality)
@ -145,116 +176,105 @@ testingTest(BasicEventFunctionality)
static void addDeleteEventHandlersImpl(testingT *t, void *data) static void addDeleteEventHandlersImpl(testingT *t, void *data)
{ {
struct baseParams *p = (struct baseParams *) data; struct baseParams *p = (struct baseParams *) data;
struct runParams rp; uiEvent *e;
const char *names[6];
struct handler got[6];
struct handler want[6];
int handler1, handler2, handler3;
int newHandler1, newHandler2, newHandler3;
uiEventOptions opts; uiEventOptions opts;
struct handler h[6];
memset(&rp, 0, sizeof (struct runParams)); memset(h, 0, 6 * sizeof (struct handler));
rp.sender = p->sender; h[0].name = "handler 1";
rp.args = p->args; h[1].name = "handler 2";
rp.nHandlers = 6; h[2].name = "handler 3";
rp.names = names; h[3].name = "new handler 1";
rp.names[0] = "handler 1"; h[4].name = "new handler 2";
rp.names[1] = "handler 2"; h[5].name = "new handler 3";
rp.names[2] = "handler 3";
rp.names[3] = "new handler 1";
rp.names[4] = "new handler 2";
rp.names[5] = "new handler 3";
rp.got = got;
rp.want = want;
memset(rp.want, 0, rp.nHandlers * sizeof (struct handler));
memset(&opts, 0, sizeof (uiEventOptions)); memset(&opts, 0, sizeof (uiEventOptions));
opts.Size = sizeof (uiEventOptions); opts.Size = sizeof (uiEventOptions);
opts.Global = p->global; opts.Global = p->global;
rp.e = uiNewEvent(&opts); e = uiNewEvent(&opts);
testingTLogf(t, "*** initial handlers"); testingTLogf(t, "*** initial handlers");
handler1 = uiEventAddHandler(rp.e, handler, p->sender, rp.got + 0); registerHandler(h + 0, e, p->sender, p->args);
handler2 = uiEventAddHandler(rp.e, handler, p->sender, rp.got + 1); registerHandler(h + 1, e, p->sender, p->args);
handler3 = uiEventAddHandler(rp.e, handler, p->sender, rp.got + 2); registerHandler(h + 2, e, p->sender, p->args);
wantRun(rp.want[0], p->sender, p->args); wantRun(h + 0);
wantRun(rp.want[1], p->sender, p->args); wantRun(h + 1);
wantRun(rp.want[2], p->sender, p->args); wantRun(h + 2);
wantNotRun(rp.want[3]); wantNotRun(h + 3);
wantNotRun(rp.want[4]); wantNotRun(h + 4);
wantNotRun(rp.want[5]); wantNotRun(h + 5);
rp.wantRunCount = 3; run(t, e, p->sender, p->args,
run(t, &rp); h, 6, 3);
testingTLogf(t, "*** deleting a handler from the middle"); testingTLogf(t, "*** deleting a handler from the middle");
uiEventDeleteHandler(rp.e, handler2); unregisterHandler(h + 1, e);
wantRun(rp.want[0], p->sender, p->args); wantRun(h + 0);
wantNotRun(rp.want[1]); wantNotRun(h + 1);
wantRun(rp.want[2], p->sender, p->args); wantRun(h + 2);
wantNotRun(rp.want[3]); wantNotRun(h + 3);
wantNotRun(rp.want[4]); wantNotRun(h + 4);
wantNotRun(rp.want[5]); wantNotRun(h + 5);
rp.wantRunCount = 2; run(t, e, p->sender, p->args,
run(t, &rp); h, 6, 2);
testingTLogf(t, "*** adding handler after deleting a handler from the middle"); testingTLogf(t, "*** adding handler after deleting a handler from the middle");
newHandler1 = uiEventAddHandler(rp.e, handler, p->sender, rp.got + 3); registerHandler(h + 3, e, p->sender, p->args);
wantRun(rp.want[0], p->sender, p->args); wantRun(h + 0);
wantNotRun(rp.want[1]); wantNotRun(h + 1);
wantRun(rp.want[2], p->sender, p->args); wantRun(h + 2);
wantRun(rp.want[3], p->sender, p->args); wantRun(h + 3);
wantNotRun(rp.want[4]); wantNotRun(h + 4);
wantNotRun(rp.want[5]); wantNotRun(h + 5);
rp.wantRunCount = 3; run(t, e, p-> sender, p->args,
run(t, &rp); h, 6, 3);
testingTLogf(t, "*** deleting first handler added and adding another"); testingTLogf(t, "*** deleting first handler added and adding another");
uiEventDeleteHandler(rp.e, handler1); unregisterHandler(h + 0, e);
newHandler2 = uiEventAddHandler(rp.e, handler, p->sender, rp.got + 4); registerHandler(h + 4, e, p->sender, p->args);
wantNotRun(rp.want[0]); wantNotRun(h + 0);
wantNotRun(rp.want[1]); wantNotRun(h + 1);
wantRun(rp.want[2], p->sender, p->args); wantRun(h + 2);
wantRun(rp.want[3], p->sender, p->args); wantRun(h + 3);
wantRun(rp.want[4], p->sender, p->args); wantRun(h + 4);
wantNotRun(rp.want[5]); wantNotRun(h + 5);
rp.wantRunCount = 3; run(t, e, p->sender, p->args,
run(t, &rp); h, 6, 3);
testingTLogf(t, "*** deleting most recently added handler and adding another"); testingTLogf(t, "*** deleting most recently added handler and adding another");
uiEventDeleteHandler(rp.e, newHandler2); unregisterHandler(h + 4, e);
newHandler3 = uiEventAddHandler(rp.e, handler, p->sender, rp.got + 5); registerHandler(h + 5, e, p->sender, p->args);
wantNotRun(rp.want[0]); wantNotRun(h + 0);
wantNotRun(rp.want[1]); wantNotRun(h + 1);
wantRun(rp.want[2], p->sender, p->args); wantRun(h + 2);
wantRun(rp.want[3], p->sender, p->args); wantRun(h + 3);
wantNotRun(rp.want[4]); wantNotRun(h + 4);
wantRun(rp.want[5], p->sender, p->args); wantRun(h + 5);
rp.wantRunCount = 3; run(t, e, p->sender, p->args,
run(t, &rp); h, 6, 3);
testingTLogf(t, "*** deleting all handlers"); testingTLogf(t, "*** deleting all handlers");
uiEventDeleteHandler(rp.e, handler3); unregisterHandler(h + 2, e);
uiEventDeleteHandler(rp.e, newHandler1); unregisterHandler(h + 3, e);
uiEventDeleteHandler(rp.e, newHandler3); unregisterHandler(h + 5, e);
wantNotRun(rp.want[0]); wantNotRun(h + 0);
wantNotRun(rp.want[1]); wantNotRun(h + 1);
wantNotRun(rp.want[2]); wantNotRun(h + 2);
wantNotRun(rp.want[3]); wantNotRun(h + 3);
wantNotRun(rp.want[4]); wantNotRun(h +4);
wantNotRun(rp.want[5]); wantNotRun(h + 5);
rp.wantRunCount = 0; run(t, e, p->sender, p->args,
run(t, &rp); h, 6, 0);
testingTLogf(t, "*** adding handler after deleting all handlers"); testingTLogf(t, "*** adding handler after deleting all handlers");
uiEventAddHandler(rp.e, handler, p->sender, rp.got + 0); registerHandler(h + 0, e, p->sender, p->args);
wantRun(rp.want[0], p->sender, p->args); wantRun(h + 0);
wantNotRun(rp.want[1]); wantNotRun(h + 1);
wantNotRun(rp.want[2]); wantNotRun(h + 2);
wantNotRun(rp.want[3]); wantNotRun(h + 3);
wantNotRun(rp.want[4]); wantNotRun(h + 4);
wantNotRun(rp.want[5]); wantNotRun(h + 5);
rp.wantRunCount = 1; run(t, e, p->sender, p->args,
run(t, &rp); h, 6, 1);
} }
testingTest(AddDeleteEventHandlers) testingTest(AddDeleteEventHandlers)
@ -266,6 +286,8 @@ testingTest(AddDeleteEventHandlers)
runGlobalSubtests(t, &p); runGlobalSubtests(t, &p);
} }
#if 0
struct eventSendersHonoredParams { struct eventSendersHonoredParams {
struct baseParams bp; struct baseParams bp;
const char *names[4]; const char *names[4];
@ -480,3 +502,5 @@ testingTest(EventErrors)
{ {
// TODO // TODO
} }
#endif