vendor/unity/src/unity.c in ceedling-0.20.3 vs vendor/unity/src/unity.c in ceedling-0.21.0

- old
+ new

@@ -5,105 +5,106 @@ ============================================================================ */ #include "unity.h" #include <stddef.h> -#ifndef UNITY_OUTPUT_CHAR_USE_PUTC -//If defined as something else, make sure we declare it here so it's ready for use -extern int UNITY_OUTPUT_CHAR(int); +/* If omitted from header, declare overrideable prototypes here so they're ready for use */ +#ifdef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION +void UNITY_OUTPUT_CHAR(int); #endif +/* Helpful macros for us to use here */ #define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; longjmp(Unity.AbortFrame, 1); } #define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; longjmp(Unity.AbortFrame, 1); } -/// return prematurely if we are already in failure or ignore state + +/* return prematurely if we are already in failure or ignore state */ #define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} } struct _Unity Unity; -const char UnityStrOk[] = "OK"; -const char UnityStrPass[] = "PASS"; -const char UnityStrFail[] = "FAIL"; -const char UnityStrIgnore[] = "IGNORE"; -const char UnityStrNull[] = "NULL"; -const char UnityStrSpacer[] = ". "; -const char UnityStrExpected[] = " Expected "; -const char UnityStrWas[] = " Was "; -const char UnityStrTo[] = " To "; -const char UnityStrElement[] = " Element "; -const char UnityStrByte[] = " Byte "; -const char UnityStrMemory[] = " Memory Mismatch."; -const char UnityStrDelta[] = " Values Not Within Delta "; -const char UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless."; -const char UnityStrNullPointerForExpected[] = " Expected pointer to be NULL"; -const char UnityStrNullPointerForActual[] = " Actual pointer was NULL"; -const char UnityStrNot[] = "Not "; -const char UnityStrInf[] = "Infinity"; -const char UnityStrNegInf[] = "Negative Infinity"; -const char UnityStrNaN[] = "NaN"; -const char UnityStrDet[] = "Determinate"; -const char UnityStrInvalidFloatTrait[] = "Invalid Float Trait"; -const char UnityStrErrFloat[] = "Unity Floating Point Disabled"; -const char UnityStrErrDouble[] = "Unity Double Precision Disabled"; -const char UnityStrErr64[] = "Unity 64-bit Support Disabled"; -const char UnityStrBreaker[] = "-----------------------"; -const char UnityStrResultsTests[] = " Tests "; -const char UnityStrResultsFailures[] = " Failures "; -const char UnityStrResultsIgnored[] = " Ignored "; -const char UnityStrDetail1Name[] = UNITY_DETAIL1_NAME " "; -const char UnityStrDetail2Name[] = " " UNITY_DETAIL2_NAME " "; +static const char UnityStrOk[] = "OK"; +static const char UnityStrPass[] = "PASS"; +static const char UnityStrFail[] = "FAIL"; +static const char UnityStrIgnore[] = "IGNORE"; +static const char UnityStrNull[] = "NULL"; +static const char UnityStrSpacer[] = ". "; +static const char UnityStrExpected[] = " Expected "; +static const char UnityStrWas[] = " Was "; +static const char UnityStrElement[] = " Element "; +static const char UnityStrByte[] = " Byte "; +static const char UnityStrMemory[] = " Memory Mismatch."; +static const char UnityStrDelta[] = " Values Not Within Delta "; +static const char UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless."; +static const char UnityStrNullPointerForExpected[] = " Expected pointer to be NULL"; +static const char UnityStrNullPointerForActual[] = " Actual pointer was NULL"; +static const char UnityStrNot[] = "Not "; +static const char UnityStrInf[] = "Infinity"; +static const char UnityStrNegInf[] = "Negative Infinity"; +static const char UnityStrNaN[] = "NaN"; +static const char UnityStrDet[] = "Determinate"; +static const char UnityStrInvalidFloatTrait[] = "Invalid Float Trait"; +const char UnityStrErrFloat[] = "Unity Floating Point Disabled"; +const char UnityStrErrDouble[] = "Unity Double Precision Disabled"; +const char UnityStrErr64[] = "Unity 64-bit Support Disabled"; +static const char UnityStrBreaker[] = "-----------------------"; +static const char UnityStrResultsTests[] = " Tests "; +static const char UnityStrResultsFailures[] = " Failures "; +static const char UnityStrResultsIgnored[] = " Ignored "; +static const char UnityStrDetail1Name[] = UNITY_DETAIL1_NAME " "; +static const char UnityStrDetail2Name[] = " " UNITY_DETAIL2_NAME " "; #ifdef UNITY_FLOAT_NEEDS_ZERO -// Dividing by these constants produces +/- infinity. -// The rationale is given in UnityAssertFloatIsInf's body. +/* Dividing by these constants produces +/- infinity. + * The rationale is given in UnityAssertFloatIsInf's body. */ static const _UF f_zero = 0.0f; #endif -// compiler-generic print formatting masks -const _U_UINT UnitySizeMask[] = +/* compiler-generic print formatting masks */ +static const _U_UINT UnitySizeMask[] = { - 255u, // 0xFF - 65535u, // 0xFFFF + 255u, /* 0xFF */ + 65535u, /* 0xFFFF */ 65535u, - 4294967295u, // 0xFFFFFFFF + 4294967295u, /* 0xFFFFFFFF */ 4294967295u, 4294967295u, 4294967295u #ifdef UNITY_SUPPORT_64 ,0xFFFFFFFFFFFFFFFF #endif }; -//----------------------------------------------- -// Pretty Printers & Test Result Output Handlers -//----------------------------------------------- +/*----------------------------------------------- + * Pretty Printers & Test Result Output Handlers + *-----------------------------------------------*/ void UnityPrint(const char* string) { const char* pch = string; if (pch != NULL) { while (*pch) { - // printable characters plus CR & LF are printed + /* printable characters plus CR & LF are printed */ if ((*pch <= 126) && (*pch >= 32)) { UNITY_OUTPUT_CHAR(*pch); } - //write escaped carriage returns + /* write escaped carriage returns */ else if (*pch == 13) { UNITY_OUTPUT_CHAR('\\'); UNITY_OUTPUT_CHAR('r'); } - //write escaped line feeds + /* write escaped line feeds */ else if (*pch == 10) { UNITY_OUTPUT_CHAR('\\'); UNITY_OUTPUT_CHAR('n'); } - // unprintable characters are shown as codes + /* unprintable characters are shown as codes */ else { UNITY_OUTPUT_CHAR('\\'); UnityPrintNumberHex((_U_UINT)*pch, 2); } @@ -119,39 +120,39 @@ if (pch != NULL) { while (*pch && (_UU32)(pch - string) < length) { - // printable characters plus CR & LF are printed + /* printable characters plus CR & LF are printed */ if ((*pch <= 126) && (*pch >= 32)) { UNITY_OUTPUT_CHAR(*pch); } - //write escaped carriage returns + /* write escaped carriage returns */ else if (*pch == 13) { UNITY_OUTPUT_CHAR('\\'); UNITY_OUTPUT_CHAR('r'); } - //write escaped line feeds + /* write escaped line feeds */ else if (*pch == 10) { UNITY_OUTPUT_CHAR('\\'); UNITY_OUTPUT_CHAR('n'); } - // unprintable characters are shown as codes + /* unprintable characters are shown as codes */ else { UNITY_OUTPUT_CHAR('\\'); UnityPrintNumberHex((_U_UINT)*pch, 2); } pch++; } } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityPrintNumberByStyle(const _U_SINT number, const UNITY_DISPLAY_STYLE_T style) { if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) { UnityPrintNumber(number); @@ -164,76 +165,46 @@ { UnityPrintNumberHex((_U_UINT)number, (char)((style & 0x000F) << 1)); } } -//----------------------------------------------- -/// basically do an itoa using as little ram as possible +/*-----------------------------------------------*/ void UnityPrintNumber(const _U_SINT number_to_print) { - _U_UINT divisor = 1; - _U_UINT next_divisor; - _U_UINT number; + _U_UINT number = (_U_UINT)number_to_print; if (number_to_print < 0) { - //A negative number, including MIN negative + /* A negative number, including MIN negative */ UNITY_OUTPUT_CHAR('-'); number = (_U_UINT)(-number_to_print); } - else - { - //Non-negative number - number = (_U_UINT)number_to_print; - } - - // figure out initial divisor - while (number / divisor > 9) - { - next_divisor = divisor * 10; - if (next_divisor > divisor) - divisor = next_divisor; - else - break; - } - - // now mod and print, then divide divisor - do - { - UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); - divisor /= 10; - } - while (divisor > 0); + UnityPrintNumberUnsigned(number); } -//----------------------------------------------- -/// basically do an itoa using as little ram as possible +/*----------------------------------------------- + * basically do an itoa using as little ram as possible */ void UnityPrintNumberUnsigned(const _U_UINT number) { _U_UINT divisor = 1; - _U_UINT next_divisor; - // figure out initial divisor + /* figure out initial divisor */ while (number / divisor > 9) { - next_divisor = divisor * 10; - if (next_divisor > divisor) - divisor = next_divisor; - else - break; + divisor *= 10; } - // now mod and print, then divide divisor + /* now mod and print, then divide divisor */ do { UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10))); divisor /= 10; } while (divisor > 0); } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityPrintNumberHex(const _U_UINT number, const char nibbles_to_print) { _U_UINT nibble; char nibbles = nibbles_to_print; UNITY_OUTPUT_CHAR('0'); @@ -251,11 +222,11 @@ UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble)); } } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityPrintMask(const _U_UINT mask, const _U_UINT number) { _U_UINT current_bit = (_U_UINT)1 << (UNITY_INT_WIDTH - 1); _US32 i; @@ -278,11 +249,11 @@ } current_bit = current_bit >> 1; } } -//----------------------------------------------- +/*-----------------------------------------------*/ #ifdef UNITY_FLOAT_VERBOSE #include <stdio.h> #ifndef UNITY_VERBOSE_NUMBER_MAX_LENGTH # ifdef UNITY_DOUBLE_VERBOSE @@ -298,11 +269,11 @@ snprintf(TempBuffer, sizeof(TempBuffer), "%.6f", number); UnityPrint(TempBuffer); } #endif -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityPrintFail(void); void UnityPrintFail(void) { UnityPrint(UnityStrFail); @@ -312,11 +283,11 @@ void UnityPrintOk(void) { UnityPrint(UnityStrOk); } -//----------------------------------------------- +/*-----------------------------------------------*/ static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line); static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line) { #ifndef UNITY_FIXTURES UnityPrint(file); @@ -329,11 +300,11 @@ UNITY_UNUSED(file); UNITY_UNUSED(line); #endif } -//----------------------------------------------- +/*-----------------------------------------------*/ static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line); static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line) { #ifndef UNITY_FIXTURES UnityTestResultsBegin(Unity.TestFile, line); @@ -342,11 +313,11 @@ #endif UnityPrint(UnityStrFail); UNITY_OUTPUT_CHAR(':'); } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityConcludeTest(void) { if (Unity.CurrentTestIgnored) { Unity.TestIgnores++; @@ -362,13 +333,14 @@ } Unity.CurrentTestFailed = 0; Unity.CurrentTestIgnored = 0; UNITY_PRINT_EOL(); + UNITY_FLUSH_CALL(); } -//----------------------------------------------- +/*-----------------------------------------------*/ static void UnityAddMsgIfSpecified(const char* msg); static void UnityAddMsgIfSpecified(const char* msg) { if (msg) { @@ -388,11 +360,11 @@ #endif UnityPrint(msg); } } -//----------------------------------------------- +/*-----------------------------------------------*/ static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual); static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual) { UnityPrint(UnityStrExpected); if (expected != NULL) @@ -416,11 +388,11 @@ { UnityPrint(UnityStrNull); } } -//----------------------------------------------- +/*-----------------------------------------------*/ static void UnityPrintExpectedAndActualStringsLen(const char* expected, const char* actual, const _UU32 length) { UnityPrint(UnityStrExpected); if (expected != NULL) { @@ -445,45 +417,45 @@ } } -//----------------------------------------------- -// Assertion & Control Helpers -//----------------------------------------------- +/*----------------------------------------------- + * Assertion & Control Helpers + *-----------------------------------------------*/ static int UnityCheckArraysForNull(UNITY_INTERNAL_PTR expected, UNITY_INTERNAL_PTR actual, const UNITY_LINE_TYPE lineNumber, const char* msg) { - //return true if they are both NULL + /* return true if they are both NULL */ if ((expected == NULL) && (actual == NULL)) return 1; - //throw error if just expected is NULL + /* throw error if just expected is NULL */ if (expected == NULL) { UnityTestResultsFailBegin(lineNumber); UnityPrint(UnityStrNullPointerForExpected); UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } - //throw error if just actual is NULL + /* throw error if just actual is NULL */ if (actual == NULL) { UnityTestResultsFailBegin(lineNumber); UnityPrint(UnityStrNullPointerForActual); UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } - //return false if neither is NULL + /* return false if neither is NULL */ return 0; } -//----------------------------------------------- -// Assertion Functions -//----------------------------------------------- +/*----------------------------------------------- + * Assertion Functions + *-----------------------------------------------*/ void UnityAssertBits(const _U_SINT mask, const _U_SINT expected, const _U_SINT actual, const char* msg, @@ -501,11 +473,11 @@ UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertEqualNumber(const _U_SINT expected, const _U_SINT actual, const char* msg, const UNITY_LINE_TYPE lineNumber, const UNITY_DISPLAY_STYLE_T style) @@ -522,11 +494,18 @@ UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } } -//----------------------------------------------- +#define UnityPrintPointlessAndBail() \ +{ \ + UnityTestResultsFailBegin(lineNumber); \ + UnityPrint(UnityStrPointless); \ + UnityAddMsgIfSpecified(msg); \ + UNITY_FAIL_AND_BAIL; } + +/*-----------------------------------------------*/ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, UNITY_INTERNAL_PTR actual, const _UU32 num_elements, const char* msg, const UNITY_LINE_TYPE lineNumber, @@ -538,22 +517,19 @@ UNITY_SKIP_EXECUTION; if (elements == 0) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) return; - // If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case - // as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific - // variants do not. Therefore remove this flag. + /* If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case + * as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific + * variants do not. Therefore remove this flag. */ switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO)) { case UNITY_DISPLAY_STYLE_HEX8: case UNITY_DISPLAY_STYLE_INT8: case UNITY_DISPLAY_STYLE_UINT8: @@ -639,11 +615,11 @@ } break; } } -//----------------------------------------------- +/*-----------------------------------------------*/ #ifndef UNITY_EXCLUDE_FLOAT void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const _UF* expected, UNITY_PTR_ATTRIBUTE const _UF* actual, const _UU32 num_elements, const char* msg, @@ -656,30 +632,27 @@ UNITY_SKIP_EXECUTION; if (elements == 0) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) return; while (elements--) { diff = *ptr_expected - *ptr_actual; if (diff < 0.0f) - diff = 0.0f - diff; + diff = 0.0f - diff; tol = UNITY_FLOAT_PRECISION * *ptr_expected; if (tol < 0.0f) tol = 0.0f - tol; - //This first part of this condition will catch any NaN or Infinite values - if ((diff * 0.0f != 0.0f) || (diff > tol)) + /* This first part of this condition will catch any NaN or Infinite values */ + if (isnan(diff) || isinf(diff) || (diff > tol)) { UnityTestResultsFailBegin(lineNumber); UnityPrint(UnityStrElement); UnityPrintNumberUnsigned(num_elements - elements - 1); #ifdef UNITY_FLOAT_VERBOSE @@ -696,11 +669,11 @@ ptr_expected++; ptr_actual++; } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertFloatsWithin(const _UF delta, const _UF expected, const _UF actual, const char* msg, const UNITY_LINE_TYPE lineNumber) @@ -717,12 +690,12 @@ if (pos_delta < 0.0f) { pos_delta = 0.0f - pos_delta; } - //This first part of this condition will catch any NaN or Infinite values - if ((diff * 0.0f != 0.0f) || (pos_delta < diff)) + /* This first part of this condition will catch any NaN or Infinite values */ + if (isnan(diff) || isinf(diff) || (pos_delta < diff)) { UnityTestResultsFailBegin(lineNumber); #ifdef UNITY_FLOAT_VERBOSE UnityPrint(UnityStrExpected); UnityPrintFloat(expected); @@ -734,11 +707,11 @@ UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertFloatSpecial(const _UF actual, const char* msg, const UNITY_LINE_TYPE lineNumber, const UNITY_FLOAT_TRAIT_T style) { @@ -749,28 +722,28 @@ UNITY_SKIP_EXECUTION; switch(style) { - //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly - //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise + /* To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly + * We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise */ case UNITY_FLOAT_IS_INF: case UNITY_FLOAT_IS_NOT_INF: is_trait = isinf(actual) & ispos(actual); break; case UNITY_FLOAT_IS_NEG_INF: case UNITY_FLOAT_IS_NOT_NEG_INF: is_trait = isinf(actual) & isneg(actual); break; - //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. + /* NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. */ case UNITY_FLOAT_IS_NAN: case UNITY_FLOAT_IS_NOT_NAN: is_trait = isnan(actual); break; - //A determinate number is non infinite and not NaN. (therefore the opposite of the two above) + /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */ case UNITY_FLOAT_IS_DET: case UNITY_FLOAT_IS_NOT_DET: if (isinf(actual) | isnan(actual)) is_trait = 0; else @@ -801,13 +774,13 @@ UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } } -#endif //not UNITY_EXCLUDE_FLOAT +#endif /* not UNITY_EXCLUDE_FLOAT */ -//----------------------------------------------- +/*-----------------------------------------------*/ #ifndef UNITY_EXCLUDE_DOUBLE void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected, UNITY_PTR_ATTRIBUTE const _UD* actual, const _UU32 num_elements, const char* msg, @@ -820,14 +793,11 @@ UNITY_SKIP_EXECUTION; if (elements == 0) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) return; @@ -838,12 +808,12 @@ diff = 0.0 - diff; tol = UNITY_DOUBLE_PRECISION * *ptr_expected; if (tol < 0.0) tol = 0.0 - tol; - //This first part of this condition will catch any NaN or Infinite values - if ((diff * 0.0 != 0.0) || (diff > tol)) + /* This first part of this condition will catch any NaN or Infinite values */ + if (isnan(diff) || isinf(diff) || (diff > tol)) { UnityTestResultsFailBegin(lineNumber); UnityPrint(UnityStrElement); UnityPrintNumberUnsigned(num_elements - elements - 1); #ifdef UNITY_DOUBLE_VERBOSE @@ -860,11 +830,11 @@ ptr_expected++; ptr_actual++; } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertDoublesWithin(const _UD delta, const _UD expected, const _UD actual, const char* msg, const UNITY_LINE_TYPE lineNumber) @@ -881,12 +851,12 @@ if (pos_delta < 0.0) { pos_delta = 0.0 - pos_delta; } - //This first part of this condition will catch any NaN or Infinite values - if ((diff * 0.0 != 0.0) || (pos_delta < diff)) + /* This first part of this condition will catch any NaN or Infinite values */ + if (isnan(diff) || isinf(diff) || (pos_delta < diff)) { UnityTestResultsFailBegin(lineNumber); #ifdef UNITY_DOUBLE_VERBOSE UnityPrint(UnityStrExpected); UnityPrintFloat((float)expected); @@ -898,11 +868,11 @@ UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertDoubleSpecial(const _UD actual, const char* msg, const UNITY_LINE_TYPE lineNumber, const UNITY_FLOAT_TRAIT_T style) @@ -914,28 +884,28 @@ UNITY_SKIP_EXECUTION; switch(style) { - //To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly - //We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise + /* To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly + * We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise */ case UNITY_FLOAT_IS_INF: case UNITY_FLOAT_IS_NOT_INF: is_trait = isinf(actual) & ispos(actual); break; case UNITY_FLOAT_IS_NEG_INF: case UNITY_FLOAT_IS_NOT_NEG_INF: is_trait = isinf(actual) & isneg(actual); break; - //NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. + /* NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN. */ case UNITY_FLOAT_IS_NAN: case UNITY_FLOAT_IS_NOT_NAN: is_trait = isnan(actual); break; - //A determinate number is non infinite and not NaN. (therefore the opposite of the two above) + /* A determinate number is non infinite and not NaN. (therefore the opposite of the two above) */ case UNITY_FLOAT_IS_DET: case UNITY_FLOAT_IS_NOT_DET: if (isinf(actual) | isnan(actual)) is_trait = 0; else @@ -967,13 +937,13 @@ UNITY_FAIL_AND_BAIL; } } -#endif // not UNITY_EXCLUDE_DOUBLE +#endif /* not UNITY_EXCLUDE_DOUBLE */ -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertNumbersWithin( const _U_UINT delta, const _U_SINT expected, const _U_SINT actual, const char* msg, const UNITY_LINE_TYPE lineNumber, @@ -1008,21 +978,21 @@ UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertEqualString(const char* expected, const char* actual, const char* msg, const UNITY_LINE_TYPE lineNumber) { _UU32 i; UNITY_SKIP_EXECUTION; - // if both pointers not null compare the strings + /* if both pointers not null compare the strings */ if (expected && actual) { for (i = 0; expected[i] || actual[i]; i++) { if (expected[i] != actual[i]) @@ -1031,11 +1001,11 @@ break; } } } else - { // handle case of one pointers being null (if both null, test should pass) + { /* handle case of one pointers being null (if both null, test should pass) */ if (expected != actual) { Unity.CurrentTestFailed = 1; } } @@ -1047,22 +1017,22 @@ UnityAddMsgIfSpecified(msg); UNITY_FAIL_AND_BAIL; } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertEqualStringLen(const char* expected, const char* actual, const _UU32 length, const char* msg, const UNITY_LINE_TYPE lineNumber) { _UU32 i; UNITY_SKIP_EXECUTION; - // if both pointers not null compare the strings + /* if both pointers not null compare the strings */ if (expected && actual) { for (i = 0; (expected[i] || actual[i]) && i < length; i++) { if (expected[i] != actual[i]) @@ -1071,11 +1041,11 @@ break; } } } else - { // handle case of one pointers being null (if both null, test should pass) + { /* handle case of one pointers being null (if both null, test should pass) */ if (expected != actual) { Unity.CurrentTestFailed = 1; } } @@ -1088,36 +1058,33 @@ UNITY_FAIL_AND_BAIL; } } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertEqualStringArray( const char** expected, const char** actual, const _UU32 num_elements, const char* msg, const UNITY_LINE_TYPE lineNumber) { _UU32 i, j = 0; UNITY_SKIP_EXECUTION; - // if no elements, it's an error + /* if no elements, it's an error */ if (num_elements == 0) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) return; do { - // if both pointers not null compare the strings + /* if both pointers not null compare the strings */ if (expected[j] && actual[j]) { for (i = 0; expected[j][i] || actual[j][i]; i++) { if (expected[j][i] != actual[j][i]) @@ -1126,11 +1093,11 @@ break; } } } else - { // handle case of one pointers being null (if both null, test should pass) + { /* handle case of one pointers being null (if both null, test should pass) */ if (expected[j] != actual[j]) { Unity.CurrentTestFailed = 1; } } @@ -1148,11 +1115,11 @@ UNITY_FAIL_AND_BAIL; } } while (++j < num_elements); } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected, UNITY_INTERNAL_PTR actual, const _UU32 length, const _UU32 num_elements, const char* msg, @@ -1165,22 +1132,19 @@ UNITY_SKIP_EXECUTION; if ((elements == 0) || (length == 0)) { - UnityTestResultsFailBegin(lineNumber); - UnityPrint(UnityStrPointless); - UnityAddMsgIfSpecified(msg); - UNITY_FAIL_AND_BAIL; + UnityPrintPointlessAndBail(); } if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1) return; while (elements--) { - ///////////////////////////////////// + /* /////////////////////////////////// */ bytes = length; while (bytes--) { if (*ptr_exp != *ptr_act) { @@ -1201,18 +1165,18 @@ UNITY_FAIL_AND_BAIL; } ptr_exp = (UNITY_INTERNAL_PTR)((_UP)ptr_exp + 1); ptr_act = (UNITY_INTERNAL_PTR)((_UP)ptr_act + 1); } - ///////////////////////////////////// + /* /////////////////////////////////// */ } } -//----------------------------------------------- -// Control Functions -//----------------------------------------------- +/*----------------------------------------------- + * Control Functions + *-----------------------------------------------*/ void UnityFail(const char* msg, const UNITY_LINE_TYPE line) { UNITY_SKIP_EXECUTION; @@ -1243,11 +1207,11 @@ } UNITY_FAIL_AND_BAIL; } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line) { UNITY_SKIP_EXECUTION; UnityTestResultsBegin(Unity.TestFile, line); @@ -1259,21 +1223,21 @@ UnityPrint(msg); } UNITY_IGNORE_AND_BAIL; } -//----------------------------------------------- +/*-----------------------------------------------*/ #if defined(UNITY_WEAK_ATTRIBUTE) UNITY_WEAK_ATTRIBUTE void setUp(void) { } UNITY_WEAK_ATTRIBUTE void tearDown(void) { } #elif defined(UNITY_WEAK_PRAGMA) # pragma weak setUp void setUp(void) { } # pragma weak tearDown void tearDown(void) { } #endif -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int FuncLineNum) { Unity.CurrentTestName = FuncName; Unity.CurrentTestLineNumber = (UNITY_LINE_TYPE)FuncLineNum; Unity.NumberOfTests++; @@ -1288,11 +1252,11 @@ tearDown(); } UnityConcludeTest(); } -//----------------------------------------------- +/*-----------------------------------------------*/ void UnityBegin(const char* filename) { Unity.TestFile = filename; Unity.CurrentTestName = NULL; Unity.CurrentTestLineNumber = 0; @@ -1304,11 +1268,11 @@ UNITY_CLR_DETAILS(); UNITY_OUTPUT_START(); } -//----------------------------------------------- +/*-----------------------------------------------*/ int UnityEnd(void) { UNITY_PRINT_EOL(); UnityPrint(UnityStrBreaker); UNITY_PRINT_EOL(); @@ -1324,12 +1288,191 @@ UnityPrintOk(); } else { UnityPrintFail(); +#ifdef UNITY_DIFFERENTIATE_FINAL_FAIL + UNITY_OUTPUT_CHAR('E'); UNITY_OUTPUT_CHAR('D'); +#endif } UNITY_PRINT_EOL(); + UNITY_FLUSH_CALL(); UNITY_OUTPUT_COMPLETE(); return (int)(Unity.TestFailures); } -//----------------------------------------------- +/*----------------------------------------------- + * Command Line Argument Support + *-----------------------------------------------*/ +#ifdef UNITY_USE_COMMAND_LINE_ARGS + +char* UnityOptionIncludeNamed = NULL; +char* UnityOptionExcludeNamed = NULL; +int UnityVerbosity = 1; + +int UnityParseOptions(int argc, char** argv) +{ + UnityOptionIncludeNamed = NULL; + UnityOptionExcludeNamed = NULL; + + for (int i = 1; i < argc; i++) + { + if (argv[i][0] == '-') + { + switch(argv[i][1]) + { + case 'l': /* list tests */ + return -1; + case 'n': /* include tests with name including this string */ + case 'f': /* an alias for -n */ + if (argv[i][2] == '=') + UnityOptionIncludeNamed = &argv[i][3]; + else if (++i < argc) + UnityOptionIncludeNamed = argv[i]; + else + { + UnityPrint("ERROR: No Test String to Include Matches For"); + UNITY_PRINT_EOL(); + return 1; + } + break; + case 'q': /* quiet */ + UnityVerbosity = 0; + break; + case 'v': /* verbose */ + UnityVerbosity = 2; + break; + case 'x': /* exclude tests with name including this string */ + if (argv[i][2] == '=') + UnityOptionExcludeNamed = &argv[i][3]; + else if (++i < argc) + UnityOptionExcludeNamed = argv[i]; + else + { + UnityPrint("ERROR: No Test String to Exclude Matches For"); + UNITY_PRINT_EOL(); + return 1; + } + break; + default: + UnityPrint("ERROR: Unknown Option "); + UNITY_OUTPUT_CHAR(argv[i][1]); + UNITY_PRINT_EOL(); + return 1; + } + } + } + + return 0; +} + +int IsStringInBiggerString(const char* longstring, const char* shortstring) +{ + char* lptr = (char*)longstring; + char* sptr = (char*)shortstring; + char* lnext = lptr; + + if (*sptr == '*') + return 1; + + while (*lptr) + { + lnext = lptr + 1; + + /* If they current bytes match, go on to the next bytes */ + while (*lptr && *sptr && (*lptr == *sptr)) + { + lptr++; + sptr++; + + /* We're done if we match the entire string or up to a wildcard */ + if (*sptr == '*') + return 1; + if (*sptr == ',') + return 1; + if (*sptr == '"') + return 1; + if (*sptr == '\'') + return 1; + if (*sptr == ':') + return 2; + if (*sptr == 0) + return 1; + } + + /* Otherwise we start in the long pointer 1 character further and try again */ + lptr = lnext; + sptr = (char*)shortstring; + } + return 0; +} + +int UnityStringArgumentMatches(const char* str) +{ + int retval; + const char* ptr1; + const char* ptr2; + const char* ptrf; + + //Go through the options and get the substrings for matching one at a time + ptr1 = str; + while (ptr1[0] != 0) + { + if ((ptr1[0] == '"') || (ptr1[0] == '\'')) + ptr1++; + + //look for the start of the next partial + ptr2 = ptr1; + ptrf = 0; + do { + ptr2++; + if ((ptr2[0] == ':') && (ptr2[1] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ',')) + ptrf = &ptr2[1]; + } while ((ptr2[0] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ',')); + while ((ptr2[0] != 0) && ((ptr2[0] == ':') || (ptr2[0] == '\'') || (ptr2[0] == '"') || (ptr2[0] == ','))) + ptr2++; + + //done if complete filename match + retval = IsStringInBiggerString(Unity.TestFile, ptr1); + if (retval == 1) + return retval; + + //done if testname match after filename partial match + if ((retval == 2) && (ptrf != 0)) + { + if (IsStringInBiggerString(Unity.CurrentTestName, ptrf)) + return 1; + } + + //done if complete testname match + if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1) + return 1; + + ptr1 = ptr2; + } + + //we couldn't find a match for any substrings + return 0; +} + +int UnityTestMatches(void) +{ + /* Check if this test name matches the included test pattern */ + int retval; + if (UnityOptionIncludeNamed) + { + retval = UnityStringArgumentMatches(UnityOptionIncludeNamed); + } + else + retval = 1; + + /* Check if this test name matches the excluded test pattern */ + if (UnityOptionExcludeNamed) + { + if (UnityStringArgumentMatches(UnityOptionExcludeNamed)) + retval = 0; + } + return retval; +} + +#endif /* UNITY_USE_COMMAND_LINE_ARGS */ +/*-----------------------------------------------*/