mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-07-15 00:52:46 +08:00
Make UnityPrintFloat on by default
Remove UNITY_FLOAT_VERBOSE entirely, add option UNITY_EXCLUDE_FLOAT_PRINT Remove some questionable float casts from doubles Default to Round Ties to Even behavior, add option to Round Ties Away from Zero
This commit is contained in:
27
src/unity.c
27
src/unity.c
@ -250,7 +250,7 @@ void UnityPrintMask(const _U_UINT mask, const _U_UINT number)
|
||||
}
|
||||
|
||||
/*-----------------------------------------------*/
|
||||
#ifdef UNITY_FLOAT_VERBOSE
|
||||
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||
static void UnityPrintDecimalAndNumberWithLeadingZeros(_US32 fraction_part, _US32 divisor)
|
||||
{
|
||||
UNITY_OUTPUT_CHAR('.');
|
||||
@ -262,9 +262,13 @@ static void UnityPrintDecimalAndNumberWithLeadingZeros(_US32 fraction_part, _US3
|
||||
if (fraction_part == 0) break; /* Truncate trailing 0's */
|
||||
}
|
||||
}
|
||||
#ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO
|
||||
#define ROUND_TIES_TO_EVEN(num_int, num) \
|
||||
if ((num_int & 1) == 1 && num_int > (num)) /* Odd and was rounded up */ \
|
||||
if ((num) - (_US32)(num) <= 0.5) num_int -= 1 /* and remainder was 0.5, a tie */
|
||||
#else
|
||||
#define ROUND_TIES_TO_EVEN(num_int, num)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* char buffer[19];
|
||||
@ -326,7 +330,7 @@ void UnityPrintFloat(_UD number)
|
||||
UnityPrintNumber(exponent);
|
||||
}
|
||||
}
|
||||
#endif /* UNITY_FLOAT_VERBOSE */
|
||||
#endif /* ! UNITY_EXCLUDE_FLOAT_PRINT */
|
||||
|
||||
/*-----------------------------------------------*/
|
||||
|
||||
@ -691,6 +695,7 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
|
||||
#endif
|
||||
|
||||
#ifndef UNITY_EXCLUDE_FLOAT
|
||||
|
||||
static int UnityFloatsWithin(_UF delta, _UF expected, _UF actual)
|
||||
{
|
||||
_UF diff;
|
||||
@ -753,7 +758,7 @@ void UnityAssertFloatsWithin(const _UF delta,
|
||||
if (!UnityFloatsWithin(delta, expected, actual))
|
||||
{
|
||||
UnityTestResultsFailBegin(lineNumber);
|
||||
#ifdef UNITY_FLOAT_VERBOSE
|
||||
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||
UnityPrint(UnityStrExpected);
|
||||
UnityPrintFloat(expected);
|
||||
UnityPrint(UnityStrWas);
|
||||
@ -818,7 +823,7 @@ void UnityAssertFloatSpecial(const _UF actual,
|
||||
UnityPrint(UnityStrNot);
|
||||
UnityPrint(trait_names[trait_index]);
|
||||
UnityPrint(UnityStrWas);
|
||||
#ifdef UNITY_FLOAT_VERBOSE
|
||||
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||
UnityPrintFloat(actual);
|
||||
#else
|
||||
if (should_be_trait)
|
||||
@ -867,11 +872,11 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
|
||||
UnityTestResultsFailBegin(lineNumber);
|
||||
UnityPrint(UnityStrElement);
|
||||
UnityPrintNumberUnsigned(num_elements - elements - 1);
|
||||
#ifdef UNITY_DOUBLE_VERBOSE
|
||||
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||
UnityPrint(UnityStrExpected);
|
||||
UnityPrintFloat((float)(*ptr_expected));
|
||||
UnityPrintFloat(*ptr_expected);
|
||||
UnityPrint(UnityStrWas);
|
||||
UnityPrintFloat((float)(*ptr_actual));
|
||||
UnityPrintFloat(*ptr_actual);
|
||||
#else
|
||||
UnityPrint(UnityStrDelta);
|
||||
#endif
|
||||
@ -895,11 +900,11 @@ void UnityAssertDoublesWithin(const _UD delta,
|
||||
if (!UnityDoublesWithin(delta, expected, actual))
|
||||
{
|
||||
UnityTestResultsFailBegin(lineNumber);
|
||||
#ifdef UNITY_DOUBLE_VERBOSE
|
||||
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||
UnityPrint(UnityStrExpected);
|
||||
UnityPrintFloat((float)expected);
|
||||
UnityPrintFloat(expected);
|
||||
UnityPrint(UnityStrWas);
|
||||
UnityPrintFloat((float)actual);
|
||||
UnityPrintFloat(actual);
|
||||
#else
|
||||
UnityPrint(UnityStrDelta);
|
||||
#endif
|
||||
@ -961,7 +966,7 @@ void UnityAssertDoubleSpecial(const _UD actual,
|
||||
UnityPrint(UnityStrNot);
|
||||
UnityPrint(trait_names[trait_index]);
|
||||
UnityPrint(UnityStrWas);
|
||||
#ifdef UNITY_DOUBLE_VERBOSE
|
||||
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||
UnityPrintFloat(actual);
|
||||
#else
|
||||
if (should_be_trait)
|
||||
|
@ -37,13 +37,11 @@ void tearDown(void);
|
||||
* - define UNITY_EXCLUDE_FLOAT to disallow floating point comparisons
|
||||
* - define UNITY_FLOAT_PRECISION to specify the precision to use when doing TEST_ASSERT_EQUAL_FLOAT
|
||||
* - define UNITY_FLOAT_TYPE to specify doubles instead of single precision floats
|
||||
* - define UNITY_FLOAT_VERBOSE to print floating point values in errors (uses sprintf)
|
||||
* - define UNITY_INCLUDE_DOUBLE to allow double floating point comparisons
|
||||
* - define UNITY_EXCLUDE_DOUBLE to disallow double floating point comparisons (default)
|
||||
* - define UNITY_DOUBLE_PRECISION to specify the precision to use when doing TEST_ASSERT_EQUAL_DOUBLE
|
||||
* - define UNITY_DOUBLE_TYPE to specify something other than double
|
||||
* - define UNITY_DOUBLE_VERBOSE to print floating point values in errors (uses sprintf)
|
||||
* - define UNITY_VERBOSE_NUMBER_MAX_LENGTH to change maximum length of printed numbers (used by sprintf)
|
||||
* - define UNITY_EXCLUDE_FLOAT_PRINT to trim binary size, won't print floating point values in errors
|
||||
|
||||
* Output
|
||||
* - by default, Unity prints to standard out with putchar. define UNITY_OUTPUT_CHAR(a) with a different function if desired
|
||||
|
@ -171,7 +171,6 @@
|
||||
#undef UNITY_INCLUDE_FLOAT
|
||||
#undef UNITY_FLOAT_PRECISION
|
||||
#undef UNITY_FLOAT_TYPE
|
||||
#undef UNITY_FLOAT_VERBOSE
|
||||
|
||||
#else
|
||||
|
||||
@ -225,16 +224,13 @@ typedef UNITY_FLOAT_TYPE _UF;
|
||||
/* No Floating Point Support */
|
||||
#undef UNITY_DOUBLE_PRECISION
|
||||
#undef UNITY_DOUBLE_TYPE
|
||||
#undef UNITY_DOUBLE_VERBOSE
|
||||
|
||||
#ifdef UNITY_INCLUDE_DOUBLE
|
||||
#undef UNITY_INCLUDE_DOUBLE
|
||||
#endif
|
||||
|
||||
#ifdef UNITY_FLOAT_VERBOSE
|
||||
typedef _UF _UD;
|
||||
/* For parameter in UnityPrintFloat, double promotion required */
|
||||
#endif
|
||||
/* For parameter in UnityPrintFloat(_UD), which aliases to double or float */
|
||||
|
||||
#else
|
||||
|
||||
@ -250,12 +246,6 @@ typedef UNITY_FLOAT_TYPE _UF;
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef UNITY_DOUBLE_VERBOSE
|
||||
#ifndef UNITY_FLOAT_VERBOSE
|
||||
#define UNITY_FLOAT_VERBOSE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------
|
||||
* Output Method: stdout (DEFAULT)
|
||||
*-------------------------------------------------------*/
|
||||
@ -443,7 +433,7 @@ void UnityPrintNumber(const _U_SINT number);
|
||||
void UnityPrintNumberUnsigned(const _U_UINT number);
|
||||
void UnityPrintNumberHex(const _U_UINT number, const char nibbles);
|
||||
|
||||
#ifdef UNITY_FLOAT_VERBOSE
|
||||
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||
void UnityPrintFloat(const _UD number);
|
||||
#endif
|
||||
|
||||
|
@ -2285,10 +2285,14 @@ void testFailureCountIncrementsAndIsReturnedAtEnd(void)
|
||||
|
||||
void testCstringsEscapeSequence(void)
|
||||
{
|
||||
#ifndef USING_OUTPUT_SPY
|
||||
TEST_IGNORE();
|
||||
#else
|
||||
startPutcharSpy();
|
||||
UnityPrint("\x16\x10");
|
||||
endPutcharSpy();
|
||||
TEST_ASSERT_EQUAL_STRING("\\x16\\x10", getBufferPutcharSpy());
|
||||
#endif
|
||||
}
|
||||
|
||||
#define TEST_ASSERT_EQUAL_PRINT_NUMBERS(expected, actual) { \
|
||||
@ -3228,16 +3232,14 @@ void testNotEqualFloatArraysLengthZero(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef UNITY_FLOAT_VERBOSE
|
||||
#define TEST_ASSERT_EQUAL_PRINT_FLOATING(expected, actual) { \
|
||||
startPutcharSpy(); UnityPrintFloat((actual)); endPutcharSpy(); \
|
||||
TEST_ASSERT_EQUAL_STRING((expected), getBufferPutcharSpy()); \
|
||||
}
|
||||
#endif
|
||||
|
||||
void testFloatVerbosePrinting(void)
|
||||
{
|
||||
#ifdef UNITY_FLOAT_VERBOSE
|
||||
#if !defined(UNITY_EXCLUDE_FLOAT_PRINT) && defined(USING_OUTPUT_SPY)
|
||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.0", 0.0f);
|
||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000000...", 0.000000499f);
|
||||
float smallest = 0.0000005f;
|
||||
|
Reference in New Issue
Block a user