Switched the test code to use bool wherever appropriate. Will try to do this for the rest of libui, and then I'll deduplicate the allocation and array code.

This commit is contained in:
Pietro Gagliardi 2019-05-30 22:09:45 -04:00
parent b9d445554a
commit c090dacc4a
11 changed files with 46 additions and 54 deletions

View File

@ -12,24 +12,20 @@ testingSet *beforeTests = NULL;
testingTestInSet(beforeTests, Init)
{
uiInitError err;
int ret;
ret = uiInit(NULL, NULL);
if (ret != 0)
testingTErrorf(t, "uiInit() with NULL error succeeded with return value %d; expected failure", ret);
if (uiInit(NULL, NULL))
testingTErrorf(t, "uiInit() with NULL error succeeded; expected failure");
memset(&err, 0, sizeof (uiInitError));
err.Size = 2;
ret = uiInit(NULL, &err);
if (ret != 0)
testingTErrorf(t, "uiInit() with error with invalid size succeeded with return value %d; expected failure", ret);
if (uiInit(NULL, &err))
testingTErrorf(t, "uiInit() with error with invalid size succeeded; expected failure");
err.Size = sizeof (uiInitError);
ret = uiInit(&err, &err);
if (ret != 0)
testingTErrorf(t, "uiInit() with non-NULL options succeeded with return value %d; expected failure", err);
if (uiInit(&err, &err))
testingTErrorf(t, "uiInit() with non-NULL options succeeded; expected failure");
if (strcmp(err.Message, errInvalidOptions) != 0)
diff(t, "uiInit() with non-NULL options returned bad error message", "%s",
err.Message, errInvalidOptions);
@ -38,13 +34,11 @@ testingTestInSet(beforeTests, Init)
testingTest(InitAfterInitialized)
{
uiInitError err;
int ret;
memset(&err, 0, sizeof (uiInitError));
err.Size = sizeof (uiInitError);
ret = uiInit(NULL, &err);
if (ret != 0)
testingTErrorf(t, "uiInit() after a previous successful call succeeded with return value %d; expected failure", ret);
if (uiInit(NULL, &err))
testingTErrorf(t, "uiInit() after a previous successful call succeeded; expected failure");
if (strcmp(err.Message, errAlreadyInitialized) != 0)
diff(t, "uiInit() after a previous successful call returned bad error message", "%s",
err.Message, errAlreadyInitialized);

View File

@ -126,7 +126,7 @@ static bool testingprivTRun(testingT *t, testingprivOutbuf *parentbuf)
const char *status;
timerTime start, end;
char timerstr[timerDurationStringLen];
int printStatus;
bool printStatus;
if (t->opts.Verbose)
testingprivOutbufPrintf(parentbuf, "=== RUN %s\n", t->name);
@ -143,7 +143,7 @@ static bool testingprivTRun(testingT *t, testingprivOutbuf *parentbuf)
status = "PASS";
if (t->failed) {
status = "FAIL";
printStatus = 1; // always print status on failure
printStatus = true; // always print status on failure
} else if (t->skipped)
// note that failed overrides skipped
status = "SKIP";

View File

@ -43,7 +43,7 @@ typedef struct testingSet testingSet;
typedef struct testingOptions testingOptions;
struct testingOptions {
int Verbose;
bool Verbose;
};
extern void testingSetRun(testingSet *set, const struct testingOptions *options, bool *anyRun, bool *anyFailed);

View File

@ -251,7 +251,7 @@ void testingprivOutbufAppendOutbuf(testingprivOutbuf *o, testingprivOutbuf *src)
{
char *buf;
size_t n;
int hasTrailingBlankLine;
bool hasTrailingBlankLine;
size_t trailingBlankLinePos = 0; // silence incorrect MSVC warning
char *lineStart, *lineEnd;
@ -262,9 +262,9 @@ void testingprivOutbufAppendOutbuf(testingprivOutbuf *o, testingprivOutbuf *src)
return;
// strip trailing blank lines, if any
hasTrailingBlankLine = 0;
hasTrailingBlankLine = false;
if (buf[n - 1] == '\n') {
hasTrailingBlankLine = 1;
hasTrailingBlankLine = true;
while (n > 0 && buf[n - 1] == '\n')
n--;
if (n == 0) {

View File

@ -31,10 +31,10 @@ static const struct timerStringPart parts[] = {
static int fillFracPart(char *buf, int precision, int start, uint64_t *unsec)
{
int i;
int print;
bool print;
uint64_t digit;
print = 0;
print = false;
for (i = 0; i < precision; i++) {
digit = *unsec % 10;
print = print || (digit != 0);
@ -69,7 +69,7 @@ static int fillIntPart(char *buf, int start, uint64_t unsec)
void timerDurationString(timerDuration d, char buf[timerDurationStringLen])
{
uint64_t unsec;
int neg;
bool neg;
int start;
const struct timerStringPart *p;
@ -82,7 +82,7 @@ void timerDurationString(timerDuration d, char buf[timerDurationStringLen])
return;
}
unsec = (uint64_t) d;
neg = 0;
neg = false;
if (d < 0) {
#ifdef _MSC_VER
// TODO figure out a more explicit way to do this; until then, just go with what the standard says should happen, because it's what we want (TODO verify this)
@ -93,7 +93,7 @@ void timerDurationString(timerDuration d, char buf[timerDurationStringLen])
#ifdef _MSC_VER
#pragma warning(pop)
#endif
neg = 1;
neg = true;
}
for (p = parts; p->suffix != 0; p++) {
@ -136,7 +136,7 @@ void timerDurationString(timerDuration d, char buf[timerDurationStringLen])
static void int128FromUint64(uint64_t n, timerprivInt128 *out)
{
out->neg = 0;
out->neg = false;
out->high = 0;
out->low = n;
}
@ -147,7 +147,7 @@ static void int128FromInt64(int64_t n, timerprivInt128 *out)
int128FromUint64((uint64_t) n, out);
return;
}
out->neg = 1;
out->neg = true;
out->high = 0;
// C99 §6.2.6.2 resticts the possible signed integer representations in C to either sign-magnitude, 1's complement, or 2's complement.
// Therefore, INT64_MIN will always be either -INT64_MAX or -INT64_MAX - 1, so we can safely do this to see if we need to special-case INT64_MIN as -INT64_MIN cannot be safely represented, or if we can just say -n as that can be safely represented.
@ -230,13 +230,13 @@ static void int128BitSet(timerprivInt128 *x, int i)
static void int128MulDiv64(timerprivInt128 *x, timerprivInt128 *y, timerprivInt128 *z, timerprivInt128 *quot)
{
int finalNeg;
bool finalNeg;
uint64_t x64high, x64low;
uint64_t y64high, y64low;
timerprivInt128 add, numer, rem;
int i;
finalNeg = 0;
finalNeg = false;
if (x->neg)
finalNeg = !finalNeg;
if (y->neg)
@ -248,7 +248,7 @@ static void int128MulDiv64(timerprivInt128 *x, timerprivInt128 *y, timerprivInt1
// first, multiply x and y into numer
// this assumes x->high == y->high == 0
numer.neg = 0;
numer.neg = false;
// the idea is if x = (a * 2^32) + b and y = (c * 2^32) + d, we can express x * y as ((a * 2^32) + b) * ((c * 2^32) + d)...
x64high = (x->low >> 32) & 0xFFFFFFFF;
x64low = x->low & 0xFFFFFFFF;
@ -257,7 +257,7 @@ static void int128MulDiv64(timerprivInt128 *x, timerprivInt128 *y, timerprivInt1
// and we can expand that out to get...
numer.high = x64high * y64high; // a * c * 2^64 +
numer.low = x64low * y64low; // b * d +
add.neg = 0;
add.neg = false;
add.high = x64high * y64low; // a * d * 2^32 +
add.low = (add.high & 0xFFFFFFFF) << 32;
add.high >>= 32;
@ -273,7 +273,7 @@ static void int128MulDiv64(timerprivInt128 *x, timerprivInt128 *y, timerprivInt1
// (Apple also rejects quotients > UINT64_MAX; we won't)
quot->high = 0;
quot->low = 0;
rem.neg = 0;
rem.neg = false;
rem.high = 0;
rem.low = 0;
for (i = 127; i >= 0; i--) {

View File

@ -1,5 +1,6 @@
// 2 may 2019
#include <stdbool.h>
#include <stdint.h>
typedef int64_t timerDuration;
@ -32,6 +33,6 @@ typedef uint64_t timerSysError;
#define timerSysErrorFmtArg(x) strerror((int) x), ((int) x)
#endif
extern timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *data, int *timedOut);
extern timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *data, bool *timedOut);
extern timerSysError timerSleep(timerDuration d);

View File

@ -171,18 +171,18 @@ static void teardownNonReentrance(void)
mustpthread_mutex_unlock(&nonReentranceMutex);
}
timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *data, int *timedOut)
timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *data, bool *timedOut)
{
sigset_t sigalrm, allsigs;
sigset_t prevMask;
volatile int restorePrevMask = 0;
volatile bool restorePrevMask = false;
struct sigaction sig;
volatile int restoreSignal = 0;
volatile bool restoreSignal = false;
struct itimerval duration;
volatile int destroyTimer = 0;
volatile bool destroyTimer = false;
int err = 0;
*timedOut = 0;
*timedOut = false;
err = setupNonReentrance();
if (err != 0)
return (timerSysError) err;
@ -202,7 +202,7 @@ timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *
err = pthread_sigmask(SIG_BLOCK, &sigalrm, &prevMask);
if (err != 0)
return (timerSysError) err;
restorePrevMask = 1;
restorePrevMask = true;
if (setjmp(p.retpos) == 0) {
sig.sa_mask = allsigs;
@ -213,7 +213,7 @@ timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *
err = errno;
goto out;
}
restoreSignal = 1;
restoreSignal = true;
duration.it_interval.tv_sec = 0;
duration.it_interval.tv_usec = 0;
@ -224,7 +224,7 @@ timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *
err = errno;
goto out;
}
destroyTimer = 1;
destroyTimer = true;
// and fire away
err = pthread_sigmask(SIG_UNBLOCK, &sigalrm, NULL);
@ -233,7 +233,7 @@ timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *
(*f)(data);
} else
*timedOut = 1;
*timedOut = true;
err = 0;
out:

View File

@ -324,16 +324,16 @@ static unsigned __stdcall timerThreadProc(void *data)
return 0;
}
timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *data, int *timedOut)
timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *data, bool *timedOut)
{
struct timeoutParams *p;
int doTeardownNonReentrance = 0;
bool doTeardownNonReentrance = false;
MSG msg;
volatile HANDLE timerThread = NULL;
LARGE_INTEGER duration;
HRESULT hr;
*timedOut = 0;
*timedOut = false;
// we use a pointer to heap memory here to avoid volatile kludges
p = (struct timeoutParams *) malloc(sizeof (struct timeoutParams));
if (p == NULL)
@ -343,7 +343,7 @@ timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *
hr = setupNonReentrance(p);
if (hr != S_OK)
goto out;
doTeardownNonReentrance = 1;
doTeardownNonReentrance = true;
// to ensure that the PostThreadMessage() above will not fail because the thread doesn't have a message queue
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
@ -386,12 +386,11 @@ timerSysError timerRunWithTimeout(timerDuration d, void (*f)(void *data), void *
goto out;
(*f)(data);
*timedOut = 0;
} else if (p->hr != S_OK) {
hr = p->hr;
goto out;
} else
*timedOut = 1;
*timedOut = true;
hr = S_OK;
out:

View File

@ -3,7 +3,7 @@
typedef struct timerprivInt128 timerprivInt128;
struct timerprivInt128 {
int neg;
bool neg;
uint64_t high;
uint64_t low;
};

View File

@ -38,11 +38,10 @@ int main(int argc, char *argv[])
testingOptions opts;
bool anyRun = false, anyFailed = false;
uiInitError err;
int ret;
memset(&opts, 0, sizeof (testingOptions));
if (argc == 2 && strcmp(argv[1], "-v") == 0)
opts.Verbose = 1;
opts.Verbose = true;
else if (argc != 1) {
fprintf(stderr, "usage: %s [-v]\n", argv[0]);
return 1;
@ -51,8 +50,7 @@ int main(int argc, char *argv[])
runSetORingResults(beforeTests, &opts, &anyRun, &anyFailed);
memset(&err, 0, sizeof (uiInitError));
err.Size = sizeof (uiInitError);
ret = uiInit(NULL, &err);
if (ret == 0) {
if (!uiInit(NULL, &err)) {
fprintf(stderr, "uiInit() failed: %s; can't continue\n", err.Message);
printf("FAIL\n");
return 1;

View File

@ -14,7 +14,7 @@
extern void timeoutMain(void *data);
#define timeout_uiMain(t, d) { \
timerSysError err; \
int timedOut; \
bool timedOut; \
err = timerRunWithTimeout(d, timeoutMain, NULL, &timedOut); \
if (err != 0) \
testingTErrorf(t, "error running uiMain() in timeout: " timerSysErrorFmt, timerSysErrorFmtArg(err)); \