And rewrote events.c to use the new error checking framework. Now to fix build and possibly runtime errors.

This commit is contained in:
Pietro Gagliardi 2019-06-10 11:24:33 -04:00
parent 7b10fa729b
commit a0fc3187e2
1 changed files with 64 additions and 127 deletions

View File

@ -916,146 +916,83 @@ testingTest(EventInvalidateSender)
runArgsSubtests(t, &p); runArgsSubtests(t, &p);
} }
#if 0 struct checkEventErrorsParams {
TODOTODO uiEvent *globalEvent;
// TODO table-ize these uiEvent *nonglobalEvent;
static void testWhileFiring(void *sender, void *args, void *data)
{
testingT *t = (testingT *) data;
uiEvent *firingEvent = (uiEvent *) args;
void *senderPlaceholder, *argsPlaceholder, *dataPlaceholder;
int idPlaceholder;
bool blockedPlaceholder;
senderPlaceholder = NULL;
argsPlaceholder = NULL;
dataPlaceholder = NULL;
idPlaceholder = 0;
blockedPlaceholder = false;
testProgrammerError(t, uiEventFree(firingEvent),
"uiEventFree(): can't change a uiEvent while it is firing");
testProgrammerError(t, uiEventAddHandler(firingEvent, handler, senderPlaceholder, dataPlaceholder),
"uiEventAddHandler(): can't change a uiEvent while it is firing");
testProgrammerError(t, uiEventDeleteHandler(firingEvent, idPlaceholder),
"uiEventDeleteHandler(): can't change a uiEvent while it is firing");
testProgrammerError(t, uiEventFire(firingEvent, senderPlaceholder, argsPlaceholder),
"uiEventFire(): can't recursively fire a uiEvent");
testProgrammerError(t, uiEventSetHandlerBlocked(firingEvent, idPlaceholder, blockedPlaceholder),
"uiEventSetHandlerBlocked(): can't change a uiEvent while it is firing");
testProgrammerError(t, uiEventInvalidateSender(firingEvent, senderPlaceholder),
"uiEventInvalidateSender(): can't change a uiEvent while it is firing");
}
struct deferDeleteFiringHandlerParams {
uiEvent *e;
int id;
};
static void deferDeleteFiringHandler(testingT *t, void *data)
{
struct deferDeleteFiringHandlerParams *p = (struct deferDeleteFiringHandlerParams *) data;
uiEventDeleteHandler(p->e, p->id);
}
testingTest(EventErrors)
{
uiEvent *globalEvent, *nonglobalEvent;
uiEventOptions opts;
uiEventOptions eventOptionsBadSize; uiEventOptions eventOptionsBadSize;
uiEvent *eventPlaceholder; uiEvent *eventPlaceholder;
void *senderPlaceholder, *argsPlaceholder, *dataPlaceholder; uiEventHandler handlerPlaceholder;
void *senderPlaceholder;
void *argsPlaceholder;
void *dataPlaceholder;
void *nonNullSender; void *nonNullSender;
int idPlaceholder; int idPlaceholder;
bool blockedPlaceholder; bool blockedPlaceholder;
uiEvent *firingEvent; uiEvent *firingEvent;
struct deferDeleteFiringHandlerParams *fp; };
testProgrammerError(t, uiNewEvent(NULL), #define checkErrorCase(call, msgWant) \
"uiNewEvent(): invalid null pointer for uiEventOptions"); static void doCheck ## __LINE__(void *data) \
memset(&eventOptionsBadSize, 0, sizeof (uiEventOptions)); { \
eventOptionsBadSize.Size = 1; struct checkEventErrorsParams *p = (struct checkEventErrorsParams *) data; \
testProgrammerError(t, uiNewEvent(&eventOptionsBadSize), (void) p; /* in the event call does not use this */ \
"uiNewEvent(): wrong size 1 for uiEventOptions"); call; \
}
#define checkErrorCaseWhileFiring(call, msgWant) \
static void eventHandler ## __LINE__(void *sender, void *args, void *data) \
{ \
struct checkEventErrorsParams *p = (struct checkEventErrorsParams *) data; \
(void) p; /* in the event call does not use this */ \
call; \
} \
static void doCheck ## __LINE__(void *data) \
{ \
struct checkEventErrorsParams *p = (struct checkEventErrorsParams *) data; \
int id; \
id = uiEventAddHandler(p->firingEvent, eventHandler ## __LINE__, NULL, p); \
uiEventFire(p->firingEvent, NULL, NULL); \
uiEventDeleteHandler(p->firingEvent, id); \
}
#include "events_errors.h"
#undef checkErrorCaseWhileFiring
#undef checkErrorCase
static const struct {
const char *name;
void (*f)(void *data);
const char *msgWant;
} eventErrorCases[] = {
#define checkErrorCase(call, msgWant) { #call, doCheck ## __LINE__, msgWant },
#define checkErrorCaseWhileFiring(call, msgWant) checkErrorCase(call, msgWant)
#include "events_errors.h"
#undef checkErrorCaseWhileFiring
#undef checkErrorCase
{ NULL, NULL, NULL, },
};
testingTest(EventErrors)
{
struct checkEventErrorsParams p;
uiEventOptions opts;
size_t i;
memset(&p, 0, sizeof (struct checkEventErrorsParams));
p.eventOptionsBadSize.Size = 1;
memset(&opts, 0, sizeof (uiEventOptions)); memset(&opts, 0, sizeof (uiEventOptions));
opts.Size = sizeof (uiEventOptions); opts.Size = sizeof (uiEventOptions);
opts.Global = true; opts.Global = true;
globalEvent = uiNewEvent(&opts); p.globalEvent = uiNewEvent(&opts);
testingTDefer(t, deferEventFree, globalEvent); testingTDefer(t, deferEventFree, p.globalEvent);
opts.Global = false; opts.Global = false;
nonglobalEvent = uiNewEvent(&opts); p.nonglobalEvent = uiNewEvent(&opts);
testingTDefer(t, deferEventFree, nonglobalEvent); testingTDefer(t, deferEventFree, p.nonglobalEvent);
p.eventPlaceholder = p.globalEvent;
p.handlerPlaceholder = handler;
p.nonNullSender = &p;
p.firingEvent = p.globalEvent;
testProgrammerError(t, uiEventFree(NULL), for (i = 0; eventErrorCases[i].name != NULL; i++)
"uiEventFree(): invalid null pointer for uiEvent"); checkProgrammerError(t, eventErrorCases[i].name, eventErrorCases[i].f, &p, eventErrorCases[i].msgWant);
// We test trying to free a uiEvent with handlers later, when we actually need to make one for testing firing.
eventPlaceholder = globalEvent;
senderPlaceholder = NULL;
argsPlaceholder = NULL;
dataPlaceholder = NULL;
nonNullSender = &globalEvent;
idPlaceholder = 0;
blockedPlaceholder = false;
testProgrammerError(t, uiEventAddHandler(NULL, handler, senderPlaceholder, dataPlaceholder),
"uiEventAddHandler(): invalid null pointer for uiEvent");
testProgrammerError(t, uiEventAddHandler(eventPlaceholder, NULL, senderPlaceholder, dataPlaceholder),
"uiEventAddHandler(): invalid null pointer for uiEventHandler");
testProgrammerError(t, uiEventAddHandler(globalEvent, handler, nonNullSender, dataPlaceholder),
"uiEventAddHandler(): can't use a non-NULL sender with a global event");
testProgrammerError(t, uiEventAddHandler(nonglobalEvent, handler, NULL, dataPlaceholder),
"uiEventAddHandler(): can't use a NULL sender with a non-global event");
testProgrammerError(t, uiEventDeleteHandler(NULL, idPlaceholder),
"uiEventDeleteHandler(): invalid null pointer for uiEvent");
testProgrammerError(t, uiEventDeleteHandler(eventPlaceholder, 5),
"uiEventDeleteHandler(): event handler 5 not found");
testProgrammerError(t, uiEventFire(NULL, senderPlaceholder, argsPlaceholder),
"uiEventFire(): invalid null pointer for uiEvent");
testProgrammerError(t, uiEventFire(globalEvent, nonNullSender, argsPlaceholder),
"uiEventFire(): can't use a non-NULL sender with a global event");
testProgrammerError(t, uiEventFire(nonglobalEvent, NULL, argsPlaceholder),
"uiEventFire(): can't use a NULL sender with a non-global event");
testProgrammerError(t, uiEventHandlerBlocked(NULL, idPlaceholder),
"uiEventHandlerBlocked(): invalid null pointer for uiEvent");
testProgrammerError(t, uiEventHandlerBlocked(eventPlaceholder, 5),
"uiEventHandlerBlocked(): event handler 5 not found");
testProgrammerError(t, uiEventSetHandlerBlocked(NULL, idPlaceholder, blockedPlaceholder),
"uiEventSetHandlerBlocked(): invalid null pointer for uiEvent");
testProgrammerError(t, uiEventSetHandlerBlocked(eventPlaceholder, 5, blockedPlaceholder),
"uiEventSetHandlerBlocked(): event handler 5 not found");
testProgrammerError(t, uiEventInvalidateSender(NULL, senderPlaceholder),
"uiEventInvalidateSender(): invalid null pointer for uiEvent");
testProgrammerError(t, uiEventInvalidateSender(globalEvent, NULL),
"uiEventInvalidateSender(): can't invalidate a global event");
testProgrammerError(t, uiEventInvalidateSender(nonglobalEvent, NULL),
"uiEventInvalidateSender(): can't use a NULL sender with a non-global event");
memset(&opts, 0, sizeof (uiEventOptions));
opts.Size = sizeof (uiEventOptions);
opts.Global = true;
firingEvent = uiNewEvent(&opts);
testingTDefer(t, deferEventFree, firingEvent);
fp = (struct deferDeleteFiringHandlerParams *) malloc(sizeof (struct deferDeleteFiringHandlerParams));
if (fp == NULL)
testingTFatalf(t, "memory exhausted allocating storage for deleting firing event handler");
testingTDefer(t, deferFree, fp);
fp->e = firingEvent;
fp->id = uiEventAddHandler(fp->e, testWhileFiring, NULL, t);
testingTDefer(t, deferDeleteFiringHandler, fp);
testProgrammerError(t, uiEventFree(firingEvent),
"uiEventFree(): can't free event that still has handlers registered");
uiEventFire(firingEvent, NULL, firingEvent);
} }
#endif
// TODO check deleting each internal event // TODO check deleting each internal event