未验证 提交 695dd04d 编写于 作者: M Mark VanderVoord 提交者: GitHub

Merge pull request #365 from Kochise/patch-2

Patch 2 (Thanks, @Kochise !)
...@@ -67,6 +67,7 @@ static const char UnityStrDetail2Name[] = " " UNITY_DETAIL2_NAME " "; ...@@ -67,6 +67,7 @@ static const char UnityStrDetail2Name[] = " " UNITY_DETAIL2_NAME " ";
* Pretty Printers & Test Result Output Handlers * Pretty Printers & Test Result Output Handlers
*-----------------------------------------------*/ *-----------------------------------------------*/
/*-----------------------------------------------*/
void UnityPrint(const char* string) void UnityPrint(const char* string)
{ {
const char* pch = string; const char* pch = string;
...@@ -116,6 +117,7 @@ void UnityPrint(const char* string) ...@@ -116,6 +117,7 @@ void UnityPrint(const char* string)
} }
} }
/*-----------------------------------------------*/
void UnityPrintLen(const char* string, const UNITY_UINT32 length) void UnityPrintLen(const char* string, const UNITY_UINT32 length)
{ {
const char* pch = string; const char* pch = string;
...@@ -212,7 +214,9 @@ void UnityPrintNumberHex(const UNITY_UINT number, const char nibbles_to_print) ...@@ -212,7 +214,9 @@ void UnityPrintNumberHex(const UNITY_UINT number, const char nibbles_to_print)
int nibble; int nibble;
char nibbles = nibbles_to_print; char nibbles = nibbles_to_print;
if ((unsigned)nibbles > (2 * sizeof(number))) if ((unsigned)nibbles > (2 * sizeof(number)))
{
nibbles = 2 * sizeof(number); nibbles = 2 * sizeof(number);
}
while (nibbles > 0) while (nibbles > 0)
{ {
...@@ -275,9 +279,18 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number) ...@@ -275,9 +279,18 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
} }
/* handle zero, NaN, and +/- infinity */ /* handle zero, NaN, and +/- infinity */
if (number == 0.0f) UnityPrint("0"); if (number == 0.0f)
else if (isnan(number)) UnityPrint("nan"); {
else if (isinf(number)) UnityPrint("inf"); UnityPrint("0");
}
else if (isnan(number))
{
UnityPrint("nan");
}
else if (isinf(number))
{
UnityPrint("inf");
}
else else
{ {
int exponent = 0; int exponent = 0;
...@@ -479,6 +492,7 @@ static void UnityPrintExpectedAndActualStringsLen(const char* expected, ...@@ -479,6 +492,7 @@ static void UnityPrintExpectedAndActualStringsLen(const char* expected,
* Assertion & Control Helpers * Assertion & Control Helpers
*-----------------------------------------------*/ *-----------------------------------------------*/
/*-----------------------------------------------*/
static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected, static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,
UNITY_INTERNAL_PTR actual, UNITY_INTERNAL_PTR actual,
const UNITY_LINE_TYPE lineNumber, const UNITY_LINE_TYPE lineNumber,
...@@ -511,6 +525,7 @@ static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected, ...@@ -511,6 +525,7 @@ static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,
* Assertion Functions * Assertion Functions
*-----------------------------------------------*/ *-----------------------------------------------*/
/*-----------------------------------------------*/
void UnityAssertBits(const UNITY_INT mask, void UnityAssertBits(const UNITY_INT mask,
const UNITY_INT expected, const UNITY_INT expected,
const UNITY_INT actual, const UNITY_INT actual,
...@@ -617,9 +632,15 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, ...@@ -617,9 +632,15 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
UnityPrintPointlessAndBail(); UnityPrintPointlessAndBail();
} }
if (expected == actual) return; /* Both are NULL or same pointer */ if (expected == actual)
{
return; /* Both are NULL or same pointer */
}
if (UnityIsOneArrayNull(expected, actual, lineNumber, msg)) if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
{
UNITY_FAIL_AND_BAIL; UNITY_FAIL_AND_BAIL;
}
while ((elements > 0) && elements--) while ((elements > 0) && elements--)
{ {
...@@ -704,12 +725,14 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, ...@@ -704,12 +725,14 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
UnityPrint(UnityStrDelta) UnityPrint(UnityStrDelta)
#endif /* UNITY_EXCLUDE_FLOAT_PRINT */ #endif /* UNITY_EXCLUDE_FLOAT_PRINT */
/*-----------------------------------------------*/
static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual) static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual)
{ {
UNITY_FLOAT diff; UNITY_FLOAT diff;
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff); UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
} }
/*-----------------------------------------------*/
void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected, void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual, UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,
const UNITY_UINT32 num_elements, const UNITY_UINT32 num_elements,
...@@ -728,9 +751,15 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected, ...@@ -728,9 +751,15 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
UnityPrintPointlessAndBail(); UnityPrintPointlessAndBail();
} }
if (expected == actual) return; /* Both are NULL or same pointer */ if (expected == actual)
{
return; /* Both are NULL or same pointer */
}
if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg)) if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
{
UNITY_FAIL_AND_BAIL; UNITY_FAIL_AND_BAIL;
}
while (elements--) while (elements--)
{ {
...@@ -815,14 +844,18 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual, ...@@ -815,14 +844,18 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual,
UnityTestResultsFailBegin(lineNumber); UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected); UnityPrint(UnityStrExpected);
if (!should_be_trait) if (!should_be_trait)
{
UnityPrint(UnityStrNot); UnityPrint(UnityStrNot);
}
UnityPrint(trait_names[trait_index]); UnityPrint(trait_names[trait_index]);
UnityPrint(UnityStrWas); UnityPrint(UnityStrWas);
#ifndef UNITY_EXCLUDE_FLOAT_PRINT #ifndef UNITY_EXCLUDE_FLOAT_PRINT
UnityPrintFloat((UNITY_DOUBLE)actual); UnityPrintFloat((UNITY_DOUBLE)actual);
#else #else
if (should_be_trait) if (should_be_trait)
{
UnityPrint(UnityStrNot); UnityPrint(UnityStrNot);
}
UnityPrint(trait_names[trait_index]); UnityPrint(trait_names[trait_index]);
#endif #endif
UnityAddMsgIfSpecified(msg); UnityAddMsgIfSpecified(msg);
...@@ -840,6 +873,7 @@ static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_D ...@@ -840,6 +873,7 @@ static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_D
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff); UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
} }
/*-----------------------------------------------*/
void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected, void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected,
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual, UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual,
const UNITY_UINT32 num_elements, const UNITY_UINT32 num_elements,
...@@ -858,9 +892,15 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expecte ...@@ -858,9 +892,15 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expecte
UnityPrintPointlessAndBail(); UnityPrintPointlessAndBail();
} }
if (expected == actual) return; /* Both are NULL or same pointer */ if (expected == actual)
{
return; /* Both are NULL or same pointer */
}
if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg)) if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))
{
UNITY_FAIL_AND_BAIL; UNITY_FAIL_AND_BAIL;
}
while (elements--) while (elements--)
{ {
...@@ -900,7 +940,6 @@ void UnityAssertDoublesWithin(const UNITY_DOUBLE delta, ...@@ -900,7 +940,6 @@ void UnityAssertDoublesWithin(const UNITY_DOUBLE delta,
} }
/*-----------------------------------------------*/ /*-----------------------------------------------*/
void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual, void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
const char* msg, const char* msg,
const UNITY_LINE_TYPE lineNumber, const UNITY_LINE_TYPE lineNumber,
...@@ -945,14 +984,18 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual, ...@@ -945,14 +984,18 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
UnityTestResultsFailBegin(lineNumber); UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected); UnityPrint(UnityStrExpected);
if (!should_be_trait) if (!should_be_trait)
{
UnityPrint(UnityStrNot); UnityPrint(UnityStrNot);
}
UnityPrint(trait_names[trait_index]); UnityPrint(trait_names[trait_index]);
UnityPrint(UnityStrWas); UnityPrint(UnityStrWas);
#ifndef UNITY_EXCLUDE_FLOAT_PRINT #ifndef UNITY_EXCLUDE_FLOAT_PRINT
UnityPrintFloat(actual); UnityPrintFloat(actual);
#else #else
if (should_be_trait) if (should_be_trait)
{
UnityPrint(UnityStrNot); UnityPrint(UnityStrNot);
}
UnityPrint(trait_names[trait_index]); UnityPrint(trait_names[trait_index]);
#endif #endif
UnityAddMsgIfSpecified(msg); UnityAddMsgIfSpecified(msg);
...@@ -975,16 +1018,24 @@ void UnityAssertNumbersWithin(const UNITY_UINT delta, ...@@ -975,16 +1018,24 @@ void UnityAssertNumbersWithin(const UNITY_UINT delta,
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
{ {
if (actual > expected) if (actual > expected)
{
Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta); Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta);
}
else else
{
Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta); Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta);
}
} }
else else
{ {
if ((UNITY_UINT)actual > (UNITY_UINT)expected) if ((UNITY_UINT)actual > (UNITY_UINT)expected)
{
Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta); Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta);
}
else else
{
Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta); Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta);
}
} }
if (Unity.CurrentTestFailed) if (Unity.CurrentTestFailed)
...@@ -1180,9 +1231,15 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected, ...@@ -1180,9 +1231,15 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
UnityPrintPointlessAndBail(); UnityPrintPointlessAndBail();
} }
if (expected == actual) return; /* Both are NULL or same pointer */ if (expected == actual)
{
return; /* Both are NULL or same pointer */
}
if (UnityIsOneArrayNull(expected, actual, lineNumber, msg)) if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))
{
UNITY_FAIL_AND_BAIL; UNITY_FAIL_AND_BAIL;
}
while (elements--) while (elements--)
{ {
...@@ -1259,6 +1316,7 @@ UNITY_INTERNAL_PTR UnityNumToPtr(const UNITY_INT num, const UNITY_UINT8 size) ...@@ -1259,6 +1316,7 @@ UNITY_INTERNAL_PTR UnityNumToPtr(const UNITY_INT num, const UNITY_UINT8 size)
} }
#ifndef UNITY_EXCLUDE_FLOAT #ifndef UNITY_EXCLUDE_FLOAT
/*-----------------------------------------------*/
UNITY_INTERNAL_PTR UnityFloatToPtr(const float num) UNITY_INTERNAL_PTR UnityFloatToPtr(const float num)
{ {
UnityQuickCompare.f = num; UnityQuickCompare.f = num;
...@@ -1267,6 +1325,7 @@ UNITY_INTERNAL_PTR UnityFloatToPtr(const float num) ...@@ -1267,6 +1325,7 @@ UNITY_INTERNAL_PTR UnityFloatToPtr(const float num)
#endif #endif
#ifndef UNITY_EXCLUDE_DOUBLE #ifndef UNITY_EXCLUDE_DOUBLE
/*-----------------------------------------------*/
UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num) UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num)
{ {
UnityQuickCompare.d = num; UnityQuickCompare.d = num;
...@@ -1278,6 +1337,7 @@ UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num) ...@@ -1278,6 +1337,7 @@ UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num)
* Control Functions * Control Functions
*-----------------------------------------------*/ *-----------------------------------------------*/
/*-----------------------------------------------*/
void UnityFail(const char* msg, const UNITY_LINE_TYPE line) void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
{ {
RETURN_IF_FAIL_OR_IGNORE; RETURN_IF_FAIL_OR_IGNORE;
...@@ -1402,6 +1462,7 @@ char* UnityOptionIncludeNamed = NULL; ...@@ -1402,6 +1462,7 @@ char* UnityOptionIncludeNamed = NULL;
char* UnityOptionExcludeNamed = NULL; char* UnityOptionExcludeNamed = NULL;
int UnityVerbosity = 1; int UnityVerbosity = 1;
/*-----------------------------------------------*/
int UnityParseOptions(int argc, char** argv) int UnityParseOptions(int argc, char** argv)
{ {
UnityOptionIncludeNamed = NULL; UnityOptionIncludeNamed = NULL;
...@@ -1418,9 +1479,13 @@ int UnityParseOptions(int argc, char** argv) ...@@ -1418,9 +1479,13 @@ int UnityParseOptions(int argc, char** argv)
case 'n': /* include tests with name including this string */ case 'n': /* include tests with name including this string */
case 'f': /* an alias for -n */ case 'f': /* an alias for -n */
if (argv[i][2] == '=') if (argv[i][2] == '=')
{
UnityOptionIncludeNamed = &argv[i][3]; UnityOptionIncludeNamed = &argv[i][3];
}
else if (++i < argc) else if (++i < argc)
{
UnityOptionIncludeNamed = argv[i]; UnityOptionIncludeNamed = argv[i];
}
else else
{ {
UnityPrint("ERROR: No Test String to Include Matches For"); UnityPrint("ERROR: No Test String to Include Matches For");
...@@ -1436,9 +1501,13 @@ int UnityParseOptions(int argc, char** argv) ...@@ -1436,9 +1501,13 @@ int UnityParseOptions(int argc, char** argv)
break; break;
case 'x': /* exclude tests with name including this string */ case 'x': /* exclude tests with name including this string */
if (argv[i][2] == '=') if (argv[i][2] == '=')
{
UnityOptionExcludeNamed = &argv[i][3]; UnityOptionExcludeNamed = &argv[i][3];
}
else if (++i < argc) else if (++i < argc)
{
UnityOptionExcludeNamed = argv[i]; UnityOptionExcludeNamed = argv[i];
}
else else
{ {
UnityPrint("ERROR: No Test String to Exclude Matches For"); UnityPrint("ERROR: No Test String to Exclude Matches For");
...@@ -1458,6 +1527,7 @@ int UnityParseOptions(int argc, char** argv) ...@@ -1458,6 +1527,7 @@ int UnityParseOptions(int argc, char** argv)
return 0; return 0;
} }
/*-----------------------------------------------*/
int IsStringInBiggerString(const char* longstring, const char* shortstring) int IsStringInBiggerString(const char* longstring, const char* shortstring)
{ {
const char* lptr = longstring; const char* lptr = longstring;
...@@ -1465,7 +1535,9 @@ int IsStringInBiggerString(const char* longstring, const char* shortstring) ...@@ -1465,7 +1535,9 @@ int IsStringInBiggerString(const char* longstring, const char* shortstring)
const char* lnext = lptr; const char* lnext = lptr;
if (*sptr == '*') if (*sptr == '*')
{
return 1; return 1;
}
while (*lptr) while (*lptr)
{ {
...@@ -1496,9 +1568,11 @@ int IsStringInBiggerString(const char* longstring, const char* shortstring) ...@@ -1496,9 +1568,11 @@ int IsStringInBiggerString(const char* longstring, const char* shortstring)
lptr = lnext; lptr = lnext;
sptr = shortstring; sptr = shortstring;
} }
return 0; return 0;
} }
/*-----------------------------------------------*/
int UnityStringArgumentMatches(const char* str) int UnityStringArgumentMatches(const char* str)
{ {
int retval; int retval;
...@@ -1511,7 +1585,9 @@ int UnityStringArgumentMatches(const char* str) ...@@ -1511,7 +1585,9 @@ int UnityStringArgumentMatches(const char* str)
while (ptr1[0] != 0) while (ptr1[0] != 0)
{ {
if ((ptr1[0] == '"') || (ptr1[0] == '\'')) if ((ptr1[0] == '"') || (ptr1[0] == '\''))
{
ptr1++; ptr1++;
}
/* look for the start of the next partial */ /* look for the start of the next partial */
ptr2 = ptr1; ptr2 = ptr1;
...@@ -1520,26 +1596,37 @@ int UnityStringArgumentMatches(const char* str) ...@@ -1520,26 +1596,37 @@ int UnityStringArgumentMatches(const char* str)
{ {
ptr2++; ptr2++;
if ((ptr2[0] == ':') && (ptr2[1] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ',')) if ((ptr2[0] == ':') && (ptr2[1] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ','))
{
ptrf = &ptr2[1]; ptrf = &ptr2[1];
}
} while ((ptr2[0] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ',')); } while ((ptr2[0] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ','));
while ((ptr2[0] != 0) && ((ptr2[0] == ':') || (ptr2[0] == '\'') || (ptr2[0] == '"') || (ptr2[0] == ','))) while ((ptr2[0] != 0) && ((ptr2[0] == ':') || (ptr2[0] == '\'') || (ptr2[0] == '"') || (ptr2[0] == ',')))
{
ptr2++; ptr2++;
}
/* done if complete filename match */ /* done if complete filename match */
retval = IsStringInBiggerString(Unity.TestFile, ptr1); retval = IsStringInBiggerString(Unity.TestFile, ptr1);
if (retval == 1) if (retval == 1)
{
return retval; return retval;
}
/* done if testname match after filename partial match */ /* done if testname match after filename partial match */
if ((retval == 2) && (ptrf != 0)) if ((retval == 2) && (ptrf != 0))
{ {
if (IsStringInBiggerString(Unity.CurrentTestName, ptrf)) if (IsStringInBiggerString(Unity.CurrentTestName, ptrf))
{
return 1; return 1;
}
} }
/* done if complete testname match */ /* done if complete testname match */
if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1) if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1)
{
return 1; return 1;
}
ptr1 = ptr2; ptr1 = ptr2;
} }
...@@ -1548,6 +1635,7 @@ int UnityStringArgumentMatches(const char* str) ...@@ -1548,6 +1635,7 @@ int UnityStringArgumentMatches(const char* str)
return 0; return 0;
} }
/*-----------------------------------------------*/
int UnityTestMatches(void) int UnityTestMatches(void)
{ {
/* Check if this test name matches the included test pattern */ /* Check if this test name matches the included test pattern */
...@@ -1557,14 +1645,19 @@ int UnityTestMatches(void) ...@@ -1557,14 +1645,19 @@ int UnityTestMatches(void)
retval = UnityStringArgumentMatches(UnityOptionIncludeNamed); retval = UnityStringArgumentMatches(UnityOptionIncludeNamed);
} }
else else
{
retval = 1; retval = 1;
}
/* Check if this test name matches the excluded test pattern */ /* Check if this test name matches the excluded test pattern */
if (UnityOptionExcludeNamed) if (UnityOptionExcludeNamed)
{ {
if (UnityStringArgumentMatches(UnityOptionExcludeNamed)) if (UnityStringArgumentMatches(UnityOptionExcludeNamed))
{
retval = 0; retval = 0;
}
} }
return retval; return retval;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册