switched format of catches to pass in a variable to fill, more like C++.

git-svn-id: http://cexception.svn.sourceforge.net/svnroot/cexception/trunk@5 50f63946-2846-0410-8d77-f904c773002e
This commit is contained in:
mvandervoord
2008-12-19 17:17:53 +00:00
parent ecc3e29299
commit 959e43da62
6 changed files with 268 additions and 178 deletions
+30
View File
@@ -0,0 +1,30 @@
#ifndef _EXCEPTION_H
#define _EXCEPTION_H
//Optionally define the exception type (something like an int which can be directly assigned)
#define EXCEPTION_T int
// Optionally define the reserved value representing NO EXCEPTION
#define EXCEPTION_NONE (1234)
// Multi-Tasking environments will need a couple of macros defined to make this library
// properly handle multiple exception stacks. You will need to include and required
// definitions, then define the following macros:
// EXCEPTION_GET_ID - returns the id of the current task indexed 0 to (numtasks - 1)
// EXCEPTION_NUM_ID - returns the number of tasks that might be returned
//
// For example, Quadros might include the following implementation:
#ifndef TEST
#include "OSAPI.h"
#define EXCEPTION_GET_ID (KS_GetTaskID())
#define EXCEPTION_NUM_ID (NTASKS + 1)
#endif
// INCLUDE THE ACTUAL CEXCEPTION LIBRARY
#include "CException.h"
//This could be a good place to define/include some error ID's:
#define ERROR_ID_EVERYTHING_IS_BROKEN (0x88)
#define ERROR_ID_ONLY_THIS_IS_BROKEN (0x77)
#endif // _EXCEPTION_H
+123 -50
View File
@@ -12,36 +12,64 @@ void tearDown(void)
void test_BasicTryDoesNothingIfNoThrow(void)
{
int i;
EXCEPTION_T e = 0x5a5a;
Try
{
i += 1;
}
Catch
Catch(e)
{
TEST_FAIL("Should Not Enter Catch If Not Thrown")
}
//verify that e was untouched
TEST_ASSERT_EQUAL(0x5a5a, e);
}
void test_BasicThrowAndCatch(void)
{
volatile unsigned int ID = 0;
EXCEPTION_T e;
Try
{
Throw(0xBEEFBEEF);
TEST_FAIL("Should Have Thrown An Error")
}
Catch
Catch(e)
{
ID = EXCEPTION_ID;
//verify that e has the right data
TEST_ASSERT_EQUAL(0xBEEFBEEF, e)
}
TEST_ASSERT_EQUAL(0xBEEFBEEF, ID);
//verify that e STILL has the right data
TEST_ASSERT_EQUAL(0xBEEFBEEF, e);
}
void test_BasicThrowAndCatch_WithMiniSyntax(void)
{
EXCEPTION_T e;
//Mini Throw and Catch
Try
Throw(0xBEEFBEEF);
Catch(e)
TEST_ASSERT_EQUAL(0xBEEFBEEF, e);
TEST_ASSERT_EQUAL(0xBEEFBEEF, e);
//Mini Passthrough
Try
e = 0;
Catch(e)
TEST_FAIL("I shouldn't be caught because there was no throw");
TEST_ASSERT_EQUAL(0, e);
}
void test_VerifyVolatilesSurviveThrowAndCatch(void)
{
volatile unsigned int VolVal = 0;
EXCEPTION_T e;
Try
{
@@ -49,24 +77,28 @@ void test_VerifyVolatilesSurviveThrowAndCatch(void)
Throw(0xBEEFBEEF);
TEST_FAIL("Should Have Thrown An Error")
}
Catch
Catch(e)
{
VolVal += 2;
TEST_ASSERT_EQUAL(0xBEEFBEEF, EXCEPTION_ID);
TEST_ASSERT_EQUAL(0xBEEFBEEF, e);
}
TEST_ASSERT_EQUAL(4, VolVal);
TEST_ASSERT_EQUAL(0xBEEFBEEF, e);
}
void HappyExceptionThrower(unsigned int ID)
{
if (ID != 0)
Throw(ID);
{
Throw(ID);
}
}
void test_ThrowFromASubFunctionAndCatchInRootFunc(void)
{
volatile unsigned int ID = 0;
EXCEPTION_T e;
Try
{
@@ -74,26 +106,29 @@ void test_ThrowFromASubFunctionAndCatchInRootFunc(void)
HappyExceptionThrower(0xBADDF00D);
TEST_FAIL("Should Have Thrown An Exception");
}
Catch
Catch(e)
{
ID = EXCEPTION_ID;
ID = e;
}
TEST_ASSERT_EQUAL(0xBADDF00D, ID);
//verify that I can pass that value to something else
TEST_ASSERT_EQUAL(0xBADDF00D, e);
}
void HappyExceptionRethrower(unsigned int ID)
{
EXCEPTION_T e;
Try
{
Throw(ID);
}
Catch
Catch(e)
{
switch (EXCEPTION_ID)
switch (e)
{
case 0xBADDF00D:
Rethrow();
Throw(0xBADDBEEF);
break;
default:
break;
@@ -104,41 +139,96 @@ void HappyExceptionRethrower(unsigned int ID)
void test_ThrowAndCatchFromASubFunctionAndRethrowToCatchInRootFunc(void)
{
volatile unsigned int ID = 0;
EXCEPTION_T e;
Try
{
HappyExceptionRethrower(0xBADDF00D);
TEST_FAIL("Should Have Rethrown Exception");
}
Catch
Catch(e)
{
ID = EXCEPTION_ID;
ID = 1;
}
TEST_ASSERT_EQUAL(0xBADDF00D, ID);
TEST_ASSERT_EQUAL(0xBADDBEEF, e);
TEST_ASSERT_EQUAL(1, ID);
}
void test_ThrowAndCatchFromASubFunctionAndNoRethrowToCatchInRootFunc(void)
{
EXCEPTION_T e = 3;
Try
{
HappyExceptionRethrower(0xBADDBEEF);
}
Catch
Catch(e)
{
TEST_FAIL("Should Not Have Thrown Error");
TEST_FAIL("Should Not Have Re-thrown Error (it should have already been caught)");
}
//verify that THIS e is still untouched, even though subfunction was touched
TEST_ASSERT_EQUAL(3, e);
}
void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThisDoesntCorruptExceptionId(void)
{
EXCEPTION_T e;
Try
{
HappyExceptionThrower(0xBADDBEEF);
TEST_FAIL("Should Have Thrown Exception");
}
Catch(e)
{
TEST_ASSERT_EQUAL(0xBADDBEEF, e);
HappyExceptionRethrower(0x12345678);
TEST_ASSERT_EQUAL(0xBADDBEEF, e);
}
TEST_ASSERT_EQUAL(0xBADDBEEF, e);
}
void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThatEachExceptionIdIndependent(void)
{
EXCEPTION_T e1, e2;
Try
{
HappyExceptionThrower(0xBADDBEEF);
TEST_FAIL("Should Have Thrown Exception");
}
Catch(e1)
{
TEST_ASSERT_EQUAL(0xBADDBEEF, e1);
Try
{
HappyExceptionThrower(0x12345678);
}
Catch(e2)
{
TEST_ASSERT_EQUAL(0x12345678, e2);
}
TEST_ASSERT_EQUAL(0x12345678, e2);
TEST_ASSERT_EQUAL(0xBADDBEEF, e1);
}
TEST_ASSERT_EQUAL(0x12345678, e2);
TEST_ASSERT_EQUAL(0xBADDBEEF, e1);
}
void test_CanHaveMultipleTryBlocksInASingleFunction(void)
{
EXCEPTION_T e;
Try
{
HappyExceptionThrower(0x01234567);
TEST_FAIL("Should Have Thrown Exception");
}
Catch
Catch(e)
{
TEST_ASSERT_EQUAL(0x01234567, EXCEPTION_ID);
TEST_ASSERT_EQUAL(0x01234567, e);
}
Try
@@ -146,15 +236,17 @@ void test_CanHaveMultipleTryBlocksInASingleFunction(void)
HappyExceptionThrower(0xF00D8888);
TEST_FAIL("Should Have Thrown Exception");
}
Catch
Catch(e)
{
TEST_ASSERT_EQUAL(0xF00D8888, EXCEPTION_ID);
TEST_ASSERT_EQUAL(0xF00D8888, e);
}
}
void test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside(void)
{
int i = 0;
EXCEPTION_T e;
Try
{
Try
@@ -163,12 +255,12 @@ void test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside(void)
i = 1;
TEST_FAIL("Should Have Rethrown Exception");
}
Catch
Catch(e)
{
TEST_ASSERT_EQUAL(0x01234567, EXCEPTION_ID);
TEST_ASSERT_EQUAL(0x01234567, e);
}
}
Catch
Catch(e)
{
TEST_FAIL("Should Have Been Caught By Inside Catch");
}
@@ -177,42 +269,23 @@ void test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside(void)
void test_CanHaveNestedTryBlocksInASingleFunction_ThrowOutside(void)
{
int i = 0;
EXCEPTION_T e;
Try
{
Try
{
i = 2;
}
Catch
Catch(e)
{
TEST_FAIL("Should NotBe Caught Here");
}
HappyExceptionThrower(0x01234567);
TEST_FAIL("Should Have Rethrown Exception");
}
Catch
Catch(e)
{
TEST_ASSERT_EQUAL(0x01234567, EXCEPTION_ID);
TEST_ASSERT_EQUAL(0x01234567, e);
}
}
void HappyDetailedExceptionThrower(unsigned int ID, unsigned int Details)
{
if (ID != 0)
ThrowDetailed(ID, Details);
}
void test_CanThrowADetailedExceptionAndCheckOutTheResults(void)
{
Try
{
HappyDetailedExceptionThrower(0x12345678, 0x90ABCDEF);
TEST_FAIL("Should Have Thrown An Exception");
}
Catch
{
TEST_ASSERT_EQUAL(0x12345678, EXCEPTION_ID);
TEST_ASSERT_EQUAL(0x90ABCDEF, EXCEPTION_DETAILS);
}
}
+8 -3
View File
@@ -7,6 +7,7 @@ extern void tearDown(void);
extern void test_BasicTryDoesNothingIfNoThrow(void);
extern void test_BasicThrowAndCatch(void);
extern void test_BasicThrowAndCatch_WithMiniSyntax(void);
extern void test_VerifyVolatilesSurviveThrowAndCatch(void);
extern void test_ThrowFromASubFunctionAndCatchInRootFunc(void);
extern void test_ThrowAndCatchFromASubFunctionAndRethrowToCatchInRootFunc(void);
@@ -14,10 +15,12 @@ extern void test_ThrowAndCatchFromASubFunctionAndNoRethrowToCatchInRootFunc(void
extern void test_CanHaveMultipleTryBlocksInASingleFunction(void);
extern void test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside(void);
extern void test_CanHaveNestedTryBlocksInASingleFunction_ThrowOutside(void);
extern void test_CanThrowADetailedExceptionAndCheckOutTheResults(void);
extern void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThisDoesntCorruptExceptionId(void);
extern void test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThatEachExceptionIdIndependent(void);
static void runTest(UnityTestFunction test)
{
EXCEPTION_T e;
if (TEST_PROTECT())
{
setUp();
@@ -25,7 +28,7 @@ static void runTest(UnityTestFunction test)
{
test();
}
Catch
Catch(e)
{
TEST_FAIL("Unexpected exception!")
}
@@ -42,6 +45,7 @@ int main(void)
// RUN_TEST calls runTest
RUN_TEST(test_BasicTryDoesNothingIfNoThrow);
RUN_TEST(test_BasicThrowAndCatch);
RUN_TEST(test_BasicThrowAndCatch_WithMiniSyntax);
RUN_TEST(test_VerifyVolatilesSurviveThrowAndCatch);
RUN_TEST(test_ThrowFromASubFunctionAndCatchInRootFunc);
RUN_TEST(test_ThrowAndCatchFromASubFunctionAndRethrowToCatchInRootFunc);
@@ -49,7 +53,8 @@ int main(void)
RUN_TEST(test_CanHaveMultipleTryBlocksInASingleFunction);
RUN_TEST(test_CanHaveNestedTryBlocksInASingleFunction_ThrowInside);
RUN_TEST(test_CanHaveNestedTryBlocksInASingleFunction_ThrowOutside);
RUN_TEST(test_CanThrowADetailedExceptionAndCheckOutTheResults);
RUN_TEST(test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThisDoesntCorruptExceptionId);
RUN_TEST(test_ThrowAnErrorThenEnterATryBlockFromWithinCatch_VerifyThatEachExceptionIdIndependent);
UnityEnd();