2017-10-16 10:09:56 -04:00
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test Error Handling
|
|
|
|
***********************************************************************************************************************************/
|
2018-03-12 14:31:22 -04:00
|
|
|
#include <assert.h>
|
2017-10-16 10:09:56 -04:00
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
testTryRecurse - test to blow up try stack
|
|
|
|
***********************************************************************************************************************************/
|
2017-11-19 16:44:33 -05:00
|
|
|
volatile int testTryRecurseTotal = 0;
|
2017-10-16 10:09:56 -04:00
|
|
|
bool testTryRecurseCatch = false;
|
|
|
|
bool testTryRecurseFinally = false;
|
|
|
|
|
2018-01-31 18:22:25 -05:00
|
|
|
void
|
|
|
|
testTryRecurse()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_BEGIN()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
testTryRecurseTotal++;
|
|
|
|
assert(errorContext.tryTotal == testTryRecurseTotal + 1);
|
|
|
|
|
|
|
|
testTryRecurse();
|
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
CATCH(MemoryError)
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
testTryRecurseCatch = true; // {uncoverable - catch should never be executed}
|
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
FINALLY()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
testTryRecurseFinally = true;
|
|
|
|
}
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_END();
|
2017-10-16 10:09:56 -04:00
|
|
|
} // {uncoverable - function throws error, never returns}
|
|
|
|
|
|
|
|
/***********************************************************************************************************************************
|
|
|
|
Test Run
|
|
|
|
***********************************************************************************************************************************/
|
2018-01-31 18:22:25 -05:00
|
|
|
void
|
|
|
|
testRun()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------------
|
|
|
|
if (testBegin("check that try stack is initialized correctly"))
|
|
|
|
{
|
|
|
|
assert(errorContext.tryTotal == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------------
|
2017-11-19 16:30:23 -05:00
|
|
|
if (testBegin("TRY with no errors"))
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
2017-11-19 16:44:33 -05:00
|
|
|
volatile bool tryDone = false;
|
2017-10-16 10:09:56 -04:00
|
|
|
bool catchDone = false;
|
|
|
|
bool finallyDone = false;
|
|
|
|
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_BEGIN()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(errorContext.tryTotal == 1);
|
|
|
|
tryDone = true;
|
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
CATCH_ANY()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
catchDone = true; // {uncoverable - catch should never be executed}
|
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
FINALLY()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(errorContext.tryList[1].state == errorStateFinal);
|
|
|
|
finallyDone = true;
|
|
|
|
}
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_END();
|
2017-10-16 10:09:56 -04:00
|
|
|
|
|
|
|
assert(tryDone);
|
|
|
|
assert(!catchDone);
|
|
|
|
assert(finallyDone);
|
|
|
|
assert(errorContext.tryTotal == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------------
|
2017-11-19 16:30:23 -05:00
|
|
|
if (testBegin("TRY with multiple catches"))
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
2017-11-19 16:44:33 -05:00
|
|
|
volatile bool tryDone = false;
|
|
|
|
volatile bool catchDone = false;
|
|
|
|
volatile bool finallyDone = false;
|
2017-10-16 10:09:56 -04:00
|
|
|
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_BEGIN()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(errorContext.tryTotal == 1);
|
|
|
|
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_BEGIN()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(errorContext.tryTotal == 2);
|
|
|
|
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_BEGIN()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(errorContext.tryTotal == 3);
|
|
|
|
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_BEGIN()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(errorContext.tryTotal == 4);
|
|
|
|
tryDone = true;
|
|
|
|
|
2017-11-13 21:22:13 -05:00
|
|
|
THROW(AssertError, BOGUS_STR);
|
2017-10-16 10:09:56 -04:00
|
|
|
}
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_END();
|
2017-10-16 10:09:56 -04:00
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
CATCH(AssertError)
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
// Finally below should run even though this error has been rethrown
|
2017-11-13 21:22:13 -05:00
|
|
|
RETHROW();
|
2017-10-16 10:09:56 -04:00
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
FINALLY()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
finallyDone = true;
|
|
|
|
}
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_END();
|
2017-10-16 10:09:56 -04:00
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
CATCH_ANY()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
2017-11-13 21:22:13 -05:00
|
|
|
RETHROW();
|
2017-10-16 10:09:56 -04:00
|
|
|
}
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_END();
|
2017-10-16 10:09:56 -04:00
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
CATCH(MemoryError)
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(false); // {uncoverable - catch should never be executed}
|
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
CATCH(RuntimeError)
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(errorContext.tryTotal == 1);
|
|
|
|
assert(errorContext.tryList[1].state == errorStateCatch);
|
|
|
|
|
|
|
|
catchDone = true;
|
|
|
|
}
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_END();
|
2017-10-16 10:09:56 -04:00
|
|
|
|
|
|
|
assert(tryDone);
|
|
|
|
assert(catchDone);
|
|
|
|
assert(finallyDone);
|
|
|
|
assert(errorContext.tryTotal == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------------
|
|
|
|
if (testBegin("too deep recursive TRY_ERROR()"))
|
|
|
|
{
|
2017-11-19 16:44:33 -05:00
|
|
|
volatile bool tryDone = false;
|
2017-10-16 10:09:56 -04:00
|
|
|
bool catchDone = false;
|
|
|
|
bool finallyDone = false;
|
|
|
|
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_BEGIN()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
tryDone = true;
|
|
|
|
testTryRecurse();
|
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
CATCH(AssertError)
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
assert(errorCode() == AssertError.code);
|
|
|
|
assert(errorFileName() != NULL);
|
|
|
|
assert(errorFileLine() >= 1);
|
|
|
|
assert(strcmp(errorMessage(), "too many nested try blocks") == 0);
|
|
|
|
assert(strcmp(errorName(), AssertError.name) == 0);
|
|
|
|
assert(errorType() == &AssertError);
|
|
|
|
assert(errorTypeCode(errorType()) == AssertError.code);
|
|
|
|
assert(strcmp(errorTypeName(errorType()), AssertError.name) == 0);
|
|
|
|
catchDone = true;
|
|
|
|
}
|
2017-11-13 21:22:13 -05:00
|
|
|
FINALLY()
|
2017-10-16 10:09:56 -04:00
|
|
|
{
|
|
|
|
finallyDone = true;
|
|
|
|
}
|
2017-11-19 16:30:23 -05:00
|
|
|
TRY_END();
|
2017-10-16 10:09:56 -04:00
|
|
|
|
|
|
|
assert(tryDone);
|
|
|
|
assert(catchDone);
|
|
|
|
assert(finallyDone);
|
|
|
|
assert(errorContext.tryTotal == 0);
|
|
|
|
|
|
|
|
// This is only ERROR_TRY_MAX - 1 because one try was used up by the wrapper above the recursive function
|
|
|
|
assert(testTryRecurseTotal == ERROR_TRY_MAX - 1);
|
|
|
|
assert(!testTryRecurseCatch);
|
|
|
|
assert(testTryRecurseFinally);
|
|
|
|
}
|
2018-01-16 13:29:27 -05:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------------
|
2018-05-03 11:24:29 -04:00
|
|
|
if (testBegin("THROW_CODE() and THROW_CODE_FMT()"))
|
2018-01-16 13:29:27 -05:00
|
|
|
{
|
|
|
|
TRY_BEGIN()
|
|
|
|
{
|
|
|
|
THROW_CODE(25, "message");
|
|
|
|
}
|
|
|
|
CATCH_ANY()
|
|
|
|
{
|
|
|
|
assert(errorCode() == 25);
|
|
|
|
assert(strcmp(errorMessage(), "message") == 0);
|
|
|
|
}
|
|
|
|
TRY_END();
|
|
|
|
|
2018-05-03 11:24:29 -04:00
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TRY_BEGIN()
|
|
|
|
{
|
|
|
|
THROW_CODE_FMT(122, "message %d", 1);
|
|
|
|
}
|
|
|
|
CATCH_ANY()
|
|
|
|
{
|
|
|
|
assert(errorCode() == 122);
|
|
|
|
assert(strcmp(errorMessage(), "message 1") == 0);
|
|
|
|
}
|
|
|
|
TRY_END();
|
|
|
|
|
2018-01-16 13:29:27 -05:00
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TRY_BEGIN()
|
|
|
|
{
|
|
|
|
THROW_CODE(777, "message");
|
|
|
|
}
|
|
|
|
CATCH_ANY()
|
|
|
|
{
|
|
|
|
assert(errorCode() == AssertError.code);
|
|
|
|
assert(strcmp(errorMessage(), "could not find error type for code '777'") == 0);
|
|
|
|
}
|
|
|
|
TRY_END();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------------------------------------------------
|
2018-05-03 11:24:29 -04:00
|
|
|
if (testBegin("THROW_SYS_ERROR() and THROW_SYS_ERROR_FMT()"))
|
2018-01-16 13:29:27 -05:00
|
|
|
{
|
|
|
|
TRY_BEGIN()
|
|
|
|
{
|
|
|
|
errno = E2BIG;
|
2018-03-24 14:11:29 -04:00
|
|
|
THROW_SYS_ERROR(AssertError, "message");
|
2018-01-16 13:29:27 -05:00
|
|
|
}
|
|
|
|
CATCH_ANY()
|
|
|
|
{
|
|
|
|
printf("%s\n", errorMessage());
|
|
|
|
assert(errorCode() == AssertError.code);
|
|
|
|
assert(strcmp(errorMessage(), "message: [7] Argument list too long") == 0);
|
|
|
|
}
|
|
|
|
TRY_END();
|
2018-05-03 11:24:29 -04:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------------------------------------
|
|
|
|
TRY_BEGIN()
|
|
|
|
{
|
|
|
|
errno = EIO;
|
|
|
|
THROW_SYS_ERROR_FMT(AssertError, "message %d", 1);
|
|
|
|
}
|
|
|
|
CATCH_ANY()
|
|
|
|
{
|
|
|
|
printf("%s\n", errorMessage());
|
|
|
|
assert(errorCode() == AssertError.code);
|
|
|
|
assert(strcmp(errorMessage(), "message 1: [5] Input/output error") == 0);
|
|
|
|
}
|
|
|
|
TRY_END();
|
2018-01-16 13:29:27 -05:00
|
|
|
}
|
2017-10-16 10:09:56 -04:00
|
|
|
}
|