1
0
mirror of https://github.com/ThrowTheSwitch/Unity.git synced 2026-01-24 17:01:35 +01:00

Merge branch 'master' into feature-printf

# Conflicts:
#	src/unity.c
This commit is contained in:
Fabian Zahn
2019-01-24 18:32:07 +01:00
21 changed files with 514 additions and 203 deletions

View File

@@ -319,7 +319,7 @@ void UnityPrintNumber(const UNITY_INT number_to_print)
{
/* A negative number, including MIN negative */
UNITY_OUTPUT_CHAR('-');
number = (UNITY_UINT)(-number_to_print);
number = -number;
}
UnityPrintNumberUnsigned(number);
}
@@ -350,7 +350,9 @@ void UnityPrintNumberHex(const UNITY_UINT number, const char nibbles_to_print)
int nibble;
char nibbles = nibbles_to_print;
if ((unsigned)nibbles > (2 * sizeof(number)))
{
nibbles = 2 * sizeof(number);
}
while (nibbles > 0)
{
@@ -396,13 +398,25 @@ void UnityPrintMask(const UNITY_UINT mask, const UNITY_UINT number)
/*-----------------------------------------------*/
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
/* This function prints a floating-point value in a format similar to
* printf("%.6g"). It can work with either single- or double-precision,
* but for simplicity, it prints only 6 significant digits in either case.
* Printing more than 6 digits accurately is hard (at least in the single-
* precision case) and isn't attempted here. */
/*
* This function prints a floating-point value in a format similar to
* printf("%.7g") on a single-precision machine or printf("%.9g") on a
* double-precision machine. The 7th digit won't always be totally correct
* in single-precision operation (for that level of accuracy, a more
* complicated algorithm would be needed).
*/
void UnityPrintFloat(const UNITY_DOUBLE input_number)
{
#ifdef UNITY_INCLUDE_DOUBLE
static const int sig_digits = 9;
static const UNITY_INT32 min_scaled = 100000000;
static const UNITY_INT32 max_scaled = 1000000000;
#else
static const int sig_digits = 7;
static const UNITY_INT32 min_scaled = 1000000;
static const UNITY_INT32 max_scaled = 10000000;
#endif
UNITY_DOUBLE number = input_number;
/* print minus sign (including for negative zero) */
@@ -413,32 +427,85 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
}
/* handle zero, NaN, and +/- infinity */
if (number == 0.0f) UnityPrint("0");
else if (isnan(number)) UnityPrint("nan");
else if (isinf(number)) UnityPrint("inf");
if (number == 0.0f)
{
UnityPrint("0");
}
else if (isnan(number))
{
UnityPrint("nan");
}
else if (isinf(number))
{
UnityPrint("inf");
}
else
{
UNITY_INT32 n_int = 0, n;
int exponent = 0;
int decimals, digits;
UNITY_INT32 n;
char buf[16];
char buf[16] = {0};
/* scale up or down by powers of 10 */
while (number < 100000.0f / 1e6f) { number *= 1e6f; exponent -= 6; }
while (number < 100000.0f) { number *= 10.0f; exponent--; }
while (number > 1000000.0f * 1e6f) { number /= 1e6f; exponent += 6; }
while (number > 1000000.0f) { number /= 10.0f; exponent++; }
/*
* Scale up or down by powers of 10. To minimize rounding error,
* start with a factor/divisor of 10^10, which is the largest
* power of 10 that can be represented exactly. Finally, compute
* (exactly) the remaining power of 10 and perform one more
* multiplication or division.
*/
if (number < 1.0f)
{
UNITY_DOUBLE factor = 1.0f;
while (number < (UNITY_DOUBLE)max_scaled / 1e10f) { number *= 1e10f; exponent -= 10; }
while (number * factor < (UNITY_DOUBLE)min_scaled) { factor *= 10.0f; exponent--; }
number *= factor;
}
else if (number > (UNITY_DOUBLE)max_scaled)
{
UNITY_DOUBLE divisor = 1.0f;
while (number > (UNITY_DOUBLE)min_scaled * 1e10f) { number /= 1e10f; exponent += 10; }
while (number / divisor > (UNITY_DOUBLE)max_scaled) { divisor *= 10.0f; exponent++; }
number /= divisor;
}
else
{
/*
* In this range, we can split off the integer part before
* doing any multiplications. This reduces rounding error by
* freeing up significant bits in the fractional part.
*/
UNITY_DOUBLE factor = 1.0f;
n_int = (UNITY_INT32)number;
number -= (UNITY_DOUBLE)n_int;
while (n_int < min_scaled) { n_int *= 10; factor *= 10.0f; exponent--; }
number *= factor;
}
/* round to nearest integer */
n = ((UNITY_INT32)(number + number) + 1) / 2;
if (n > 999999)
#ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO
/* round to even if exactly between two integers */
if ((n & 1) && ((UNITY_DOUBLE)n - number == 0.5f))
n--;
#endif
n += n_int;
if (n >= max_scaled)
{
n = 100000;
n = min_scaled;
exponent++;
}
/* determine where to place decimal point */
decimals = (exponent <= 0 && exponent >= -9) ? -exponent : 5;
decimals = (exponent <= 0 && exponent >= -(sig_digits + 3)) ? -exponent : (sig_digits - 1);
exponent += decimals;
/* truncate trailing zeroes after decimal point */
@@ -617,6 +684,7 @@ static void UnityPrintExpectedAndActualStringsLen(const char* expected,
* Assertion & Control Helpers
*-----------------------------------------------*/
/*-----------------------------------------------*/
static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,
UNITY_INTERNAL_PTR actual,
const UNITY_LINE_TYPE lineNumber,
@@ -649,6 +717,7 @@ static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,
* Assertion Functions
*-----------------------------------------------*/
/*-----------------------------------------------*/
void UnityAssertBits(const UNITY_INT mask,
const UNITY_INT expected,
const UNITY_INT actual,
@@ -755,9 +824,15 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
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))
{
UNITY_FAIL_AND_BAIL;
}
while ((elements > 0) && elements--)
{
@@ -842,12 +917,14 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
UnityPrint(UnityStrDelta)
#endif /* UNITY_EXCLUDE_FLOAT_PRINT */
/*-----------------------------------------------*/
static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual)
{
UNITY_FLOAT diff;
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
}
/*-----------------------------------------------*/
void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,
const UNITY_UINT32 num_elements,
@@ -866,9 +943,15 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
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))
{
UNITY_FAIL_AND_BAIL;
}
while (elements--)
{
@@ -953,14 +1036,18 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual,
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
if (!should_be_trait)
{
UnityPrint(UnityStrNot);
}
UnityPrint(trait_names[trait_index]);
UnityPrint(UnityStrWas);
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
UnityPrintFloat((UNITY_DOUBLE)actual);
#else
if (should_be_trait)
{
UnityPrint(UnityStrNot);
}
UnityPrint(trait_names[trait_index]);
#endif
UnityAddMsgIfSpecified(msg);
@@ -978,6 +1065,7 @@ static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_D
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
}
/*-----------------------------------------------*/
void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected,
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual,
const UNITY_UINT32 num_elements,
@@ -996,9 +1084,15 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expecte
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))
{
UNITY_FAIL_AND_BAIL;
}
while (elements--)
{
@@ -1038,7 +1132,6 @@ void UnityAssertDoublesWithin(const UNITY_DOUBLE delta,
}
/*-----------------------------------------------*/
void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
@@ -1083,14 +1176,18 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
if (!should_be_trait)
{
UnityPrint(UnityStrNot);
}
UnityPrint(trait_names[trait_index]);
UnityPrint(UnityStrWas);
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
UnityPrintFloat(actual);
#else
if (should_be_trait)
{
UnityPrint(UnityStrNot);
}
UnityPrint(trait_names[trait_index]);
#endif
UnityAddMsgIfSpecified(msg);
@@ -1113,16 +1210,24 @@ void UnityAssertNumbersWithin(const UNITY_UINT delta,
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
{
if (actual > expected)
Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta);
{
Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
}
else
Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta);
{
Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
}
}
else
{
if ((UNITY_UINT)actual > (UNITY_UINT)expected)
Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta);
{
Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
}
else
Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta);
{
Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
}
}
if (Unity.CurrentTestFailed)
@@ -1318,9 +1423,15 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
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))
{
UNITY_FAIL_AND_BAIL;
}
while (elements--)
{
@@ -1397,6 +1508,7 @@ UNITY_INTERNAL_PTR UnityNumToPtr(const UNITY_INT num, const UNITY_UINT8 size)
}
#ifndef UNITY_EXCLUDE_FLOAT
/*-----------------------------------------------*/
UNITY_INTERNAL_PTR UnityFloatToPtr(const float num)
{
UnityQuickCompare.f = num;
@@ -1405,6 +1517,7 @@ UNITY_INTERNAL_PTR UnityFloatToPtr(const float num)
#endif
#ifndef UNITY_EXCLUDE_DOUBLE
/*-----------------------------------------------*/
UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num)
{
UnityQuickCompare.d = num;
@@ -1416,6 +1529,7 @@ UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num)
* Control Functions
*-----------------------------------------------*/
/*-----------------------------------------------*/
void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
{
RETURN_IF_FAIL_OR_IGNORE;
@@ -1540,6 +1654,7 @@ char* UnityOptionIncludeNamed = NULL;
char* UnityOptionExcludeNamed = NULL;
int UnityVerbosity = 1;
/*-----------------------------------------------*/
int UnityParseOptions(int argc, char** argv)
{
UnityOptionIncludeNamed = NULL;
@@ -1556,9 +1671,13 @@ int UnityParseOptions(int argc, char** argv)
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");
@@ -1574,9 +1693,13 @@ int UnityParseOptions(int argc, char** argv)
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");
@@ -1596,6 +1719,7 @@ int UnityParseOptions(int argc, char** argv)
return 0;
}
/*-----------------------------------------------*/
int IsStringInBiggerString(const char* longstring, const char* shortstring)
{
const char* lptr = longstring;
@@ -1603,7 +1727,9 @@ int IsStringInBiggerString(const char* longstring, const char* shortstring)
const char* lnext = lptr;
if (*sptr == '*')
{
return 1;
}
while (*lptr)
{
@@ -1634,9 +1760,11 @@ int IsStringInBiggerString(const char* longstring, const char* shortstring)
lptr = lnext;
sptr = shortstring;
}
return 0;
}
/*-----------------------------------------------*/
int UnityStringArgumentMatches(const char* str)
{
int retval;
@@ -1649,7 +1777,9 @@ int UnityStringArgumentMatches(const char* str)
while (ptr1[0] != 0)
{
if ((ptr1[0] == '"') || (ptr1[0] == '\''))
{
ptr1++;
}
/* look for the start of the next partial */
ptr2 = ptr1;
@@ -1658,26 +1788,37 @@ int UnityStringArgumentMatches(const char* str)
{
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;
}
@@ -1686,6 +1827,7 @@ int UnityStringArgumentMatches(const char* str)
return 0;
}
/*-----------------------------------------------*/
int UnityTestMatches(void)
{
/* Check if this test name matches the included test pattern */
@@ -1695,14 +1837,19 @@ int UnityTestMatches(void)
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;
}