diff --git a/src/unity.c b/src/unity.c index 9e42f75..dfb1feb 100644 --- a/src/unity.c +++ b/src/unity.c @@ -2,23 +2,7 @@ #include #include -struct _Unity Unity = -{ - NULL, - NULL, - 0, - 0, - 0, - 0, - 0, - 1e-4f, - {0}, -}; - -void UnityPrintChar(const char ch) -{ - putchar(ch); -} +struct _Unity Unity = { 0 }; void UnityPrint(const char* string) { @@ -28,7 +12,7 @@ void UnityPrint(const char* string) { while (*pch) { - UnityPrintChar(*pch); + UNITY_OUTPUT_CHAR(*pch); pch++; } } @@ -55,7 +39,7 @@ void UnityPrintNumber(const long number_to_print) if (number < 0) { - UnityPrintChar('-'); + UNITY_OUTPUT_CHAR('-'); number = -number; } @@ -72,7 +56,7 @@ void UnityPrintNumber(const long number_to_print) // now mod and print, then divide divisor do { - UnityPrintChar((char)('0' + (number / divisor % 10))); + UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); divisor /= 10; } while (divisor > 0); @@ -97,7 +81,7 @@ void UnityPrintNumberUnsigned(const unsigned long number) // now mod and print, then divide divisor do { - UnityPrintChar((char)('0' + (number / divisor % 10))); + UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); divisor /= 10; } while (divisor > 0); @@ -114,11 +98,11 @@ void UnityPrintNumberHex(const unsigned long number, const char nibbles_to_print nibble = (number >> (--nibbles << 2)) & 0x0000000F; if (nibble <= 9) { - UnityPrintChar((char)('0' + nibble)); + UNITY_OUTPUT_CHAR((char)('0' + nibble)); } else { - UnityPrintChar((char)('A' - 10 + nibble)); + UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble)); } } } @@ -134,16 +118,16 @@ void UnityPrintMask(const unsigned long mask, const unsigned long number) { if (bit & number) { - UnityPrintChar('1'); + UNITY_OUTPUT_CHAR('1'); } else { - UnityPrintChar('0'); + UNITY_OUTPUT_CHAR('0'); } } else { - UnityPrintChar('X'); + UNITY_OUTPUT_CHAR('X'); } bit = bit << 1; } @@ -152,11 +136,11 @@ void UnityPrintMask(const unsigned long mask, const unsigned long number) void UnityTestResultsBegin(const long line) { UnityPrint(Unity.TestFile); - UnityPrintChar(':'); + UNITY_OUTPUT_CHAR(':'); UnityPrintNumber(line); - UnityPrintChar(':'); + UNITY_OUTPUT_CHAR(':'); UnityPrint(Unity.CurrentTestName); - UnityPrintChar(':'); + UNITY_OUTPUT_CHAR(':'); } void UnityConcludeTest() @@ -194,13 +178,13 @@ void UnityAssertBits(const long mask, UnityPrintMask(mask, expected); UnityPrint(" was "); UnityPrintMask(mask, actual); - UnityPrintChar('.'); + UNITY_OUTPUT_CHAR('.'); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } @@ -219,13 +203,13 @@ void UnityAssertEqualNumber(const long expected, UnityPrintNumberByStyle(expected, style); UnityPrint(" was "); UnityPrintNumberByStyle(actual, style); - UnityPrintChar('.'); + UNITY_OUTPUT_CHAR('.'); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } @@ -244,13 +228,13 @@ void UnityAssertEqualNumberUnsigned(const unsigned long expected, UnityPrintNumberByStyle(expected, style); UnityPrint(" was "); UnityPrintNumberByStyle(actual, style); - UnityPrintChar('.'); + UNITY_OUTPUT_CHAR('.'); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } @@ -273,10 +257,10 @@ void UnityAssertEqualIntArray(const int* expected, UnityPrint("You asked me to compare 0 elements of an array, which was pointless."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); return; } @@ -293,13 +277,13 @@ void UnityAssertEqualIntArray(const int* expected, UnityPrintNumberByStyle(*--ptr_expected, style); UnityPrint(" was "); UnityPrintNumberByStyle(*--ptr_actual, style); - UnityPrintChar('.'); + UNITY_OUTPUT_CHAR('.'); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); return; } } @@ -324,10 +308,10 @@ void UnityAssertEqualUnsignedIntArray(const unsigned int* expected, UnityPrint("You asked me to compare nothing, which was pointless."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); return; } @@ -344,26 +328,27 @@ void UnityAssertEqualUnsignedIntArray(const unsigned int* expected, UnityPrintNumberByStyle(*--ptr_expected, style); UnityPrint(" was "); UnityPrintNumberByStyle(*--ptr_actual, style); - UnityPrintChar('.'); + UNITY_OUTPUT_CHAR('.'); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); return; } } } -void UnityAssertFloatsWithin(const float delta, - const float expected, - const float actual, +#ifndef UNITY_EXCLUDE_UFOAT +void UnityAssertFloatsWithin(const _UF delta, + const _UF expected, + const _UF actual, const char* msg, const unsigned short lineNumber) { - float diff = actual - expected; - float pos_delta = delta; + _UF diff = actual - expected; + _UF pos_delta = delta; if (diff < 0) { @@ -381,12 +366,13 @@ void UnityAssertFloatsWithin(const float delta, UnityPrint("Floats not within delta."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } +#endif void UnityAssertNumbersWithin(const long delta, const long expected, @@ -408,10 +394,10 @@ void UnityAssertNumbersWithin(const long delta, UnityPrint("Values not within delta."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } @@ -430,10 +416,10 @@ void UnityAssertNumbersUnsignedWithin(const unsigned long delta, UnityPrint("Values not within delta."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } @@ -470,14 +456,14 @@ void UnityAssertEqualString(const char* expected, UnityPrint(expected); UnityPrint("' was '"); UnityPrint(actual); - UnityPrintChar('\''); - UnityPrintChar('.'); + UNITY_OUTPUT_CHAR('\''); + UNITY_OUTPUT_CHAR('.'); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } @@ -496,10 +482,10 @@ void UnityAssertEqualMemory(const void* expected, UnityPrint("You asked me to compare nothing, which was pointless."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); return; } @@ -525,10 +511,10 @@ void UnityAssertEqualMemory(const void* expected, UnityPrint("Memory Mismatch."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } @@ -550,10 +536,10 @@ void UnityAssertEqualMemoryArray(const void* expected, UnityPrint("You asked me to compare nothing, which was pointless."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); return; } @@ -587,10 +573,10 @@ void UnityAssertEqualMemoryArray(const void* expected, UnityPrint(" Memory Mismatch."); if (msg) { - UnityPrintChar(' '); + UNITY_OUTPUT_CHAR(' '); UnityPrint(msg); } - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } } @@ -599,7 +585,7 @@ void UnityFail(const char* message, const long line) Unity.CurrentTestFailed = 1; UnityTestResultsBegin(line); UnityPrint(message); - UnityPrintChar('\n'); + UNITY_OUTPUT_CHAR('\n'); } void UnityIgnore(const char* message, const long line) diff --git a/src/unity.h b/src/unity.h index 1b52da5..b5f96c9 100644 --- a/src/unity.h +++ b/src/unity.h @@ -6,23 +6,40 @@ #include #include - //------------------------------------------------------- -// Bus Width Management +// Float Support //------------------------------------------------------- +// define UNITY_EXCLUDE_FLOAT to disallow floating point comparisons +// define UNITY_FLOAT_DELTA to specify the precision to use when doing TEST_ASSERT_EQUAL_FLOAT +// define UNITY_FLOAT_TYPE to specify doubles instead of single precision floats -#ifndef INT_WIDTH -#define INT_WIDTH (32) +#ifndef UNITY_EXCLUDE_FLOAT +#ifndef UNITY_FLOAT_PRECISION +#define UNITY_FLOAT_PRECISION (0.00001f) +#endif +#ifndef UNITY_FLOAT_TYPE +#define UNITY_FLOAT_TYPE float +#endif + typedef UNITY_FLOAT_TYPE _UF; #endif -#if (INT_WIDTH == 32) +//------------------------------------------------------- +// Int Support +//------------------------------------------------------- +// Unity assumes 32 bit integers by default +// If your compiler treats ints of a different size, define UNITY_INT_WIDTH + +#ifndef UNITY_INT_WIDTH +#define UNITY_INT_WIDTH (32) +#endif +#if (UNITY_INT_WIDTH == 32) typedef unsigned char _UU8; typedef unsigned short _UU16; typedef unsigned int _UU32; typedef signed char _US8; typedef signed short _US16; typedef signed int _US32; -#elif (INT_WIDTH == 16) +#elif (UNITY_INT_WIDTH == 16) typedef unsigned char _UU8; typedef unsigned int _UU16; typedef unsigned long _UU32; @@ -30,18 +47,19 @@ typedef signed int _US16; typedef signed long _US32; #else - #error Invalid INT_WIDTH specified! (32 or 16 only are currently supported) - #error Defaults to INT_WIDTH=32 if unspecified + #error Invalid UNITY_INT_WIDTH specified! (32 or 16 only are currently supported) #endif +//------------------------------------------------------- +// Output Method +//------------------------------------------------------- + +#ifndef UNITY_OUTPUT_CHAR +#define UNITY_OUTPUT_CHAR(a) putchar(a) +#endif //------------------------------------------------------- -// Internal Functions and Structs Needed For Unity To Run -// -// (use the macros below this section instead of calling -// these directly. The macros have a consistent naming -// convention and will pull in file and line information -// for you.) +// Internal Structs Needed //------------------------------------------------------- typedef void (*UnityTestFunction)(void); @@ -64,13 +82,11 @@ struct _Unity unsigned char TestIgnores; unsigned char CurrentTestFailed; unsigned char CurrentTestIgnored; - float DefaultDelta; jmp_buf AbortFrame; }; extern struct _Unity Unity; - //------------------------------------------------------- // Test Suite Management //------------------------------------------------------- @@ -79,12 +95,10 @@ void UnityBegin(void); void UnityEnd(void); void UnityConcludeTest(void); - //------------------------------------------------------- // Test Output //------------------------------------------------------- -void UnityPrintChar(const char ch); void UnityPrint(const char* string); void UnityPrintMask(const unsigned long mask, const unsigned long number); void UnityPrintNumberByStyle(const long number, const UNITY_DISPLAY_STYLE_T style); @@ -92,10 +106,13 @@ void UnityPrintNumber(const long number); void UnityPrintNumberUnsigned(const unsigned long number); void UnityPrintNumberHex(const unsigned long number, const char nibbles); - //------------------------------------------------------- // Test Assertion Fuctions //------------------------------------------------------- +// Use the macros below this section instead of calling +// these directly. The macros have a consistent naming +// convention and will pull in file and line information +// for you. void UnityAssertEqualNumber(const long expected, const long actual, @@ -147,12 +164,6 @@ void UnityAssertEqualMemoryArray(const void* expected, const char* msg, const unsigned short lineNumber ); -void UnityAssertFloatsWithin(const float delta, - const float expected, - const float actual, - const char* msg, - const unsigned short lineNumber); - void UnityAssertNumbersWithin(const long delta, const long expected, const long actual, @@ -169,6 +180,13 @@ void UnityFail(const char* message, const long line); void UnityIgnore(const char* message, const long line); +#ifndef UNITY_EXCLUDE_FLOAT +void UnityAssertFloatsWithin(const _UF delta, + const _UF expected, + const _UF actual, + const char* msg, + const unsigned short lineNumber); +#endif //------------------------------------------------------- // Test Running Macros @@ -186,13 +204,11 @@ void UnityIgnore(const char* message, const long line); Unity.NumberOfTests ++; \ runTest(func); \ UnityConcludeTest(); - //------------------------------------------------------- // Test Asserts -// -// (these are the macros you are looking for) //------------------------------------------------------- +// these are the macros you are looking for #define TEST_ASSERT_MESSAGE(condition, message) if (condition) {} else {TEST_FAIL(message);} #define TEST_ASSERT(condition) TEST_ASSERT_MESSAGE(condition, NULL) @@ -308,14 +324,6 @@ void UnityIgnore(const char* message, const long line); ABORT_IF_NECESSARY(); #define TEST_ASSERT_BIT_LOW(bit, actual) TEST_ASSERT_BIT_LOW_MESSAGE(bit, actual, NULL) -#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) \ - Unity.TestFile=__FILE__; \ - UnityAssertFloatsWithin((delta), (expected), (actual), (message), (unsigned short)__LINE__); \ - ABORT_IF_NECESSARY(); -#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, NULL) -#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, message) TEST_ASSERT_FLOAT_WITHIN_MESSAGE(expected / 10000.0f, expected, actual, message) -#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE((expected) / 10000.0f, expected, actual, NULL) - #define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) \ Unity.TestFile=__FILE__; \ UnityAssertEqualString((expected), (actual), (message), (unsigned short)__LINE__); \ @@ -338,4 +346,19 @@ void UnityIgnore(const char* message, const long line); #define TEST_IGNORE_MESSAGE(message) { Unity.TestFile=__FILE__; UnityIgnore((message), (unsigned short)__LINE__); TEST_ABORT(); } #define TEST_IGNORE() TEST_IGNORE_MESSAGE(NULL) +#ifdef UNITY_EXCLUDE_FLOAT +#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) TEST_FAIL("Unity Floating Point Disabled"); +#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) TEST_FAIL("Unity Floating Point Disabled"); +#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, message) TEST_FAIL("Unity Floating Point Disabled"); +#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) TEST_FAIL("Unity Floating Point Disabled"); +#else +#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) \ + Unity.TestFile=__FILE__; \ + UnityAssertFloatsWithin((delta), (expected), (actual), (message), (unsigned short)__LINE__); \ + ABORT_IF_NECESSARY(); +#define TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, NULL) +#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, message) TEST_ASSERT_FLOAT_WITHIN_MESSAGE((expected) * UNITY_FLOAT_PRECISION, expected, actual, message) +#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) TEST_ASSERT_FLOAT_WITHIN_MESSAGE((expected) * UNITY_FLOAT_PRECISION, expected, actual, NULL) +#endif + #endif