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:
29
src/unity.c
29
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)
|
static void UnityPrintDecimalAndNumberWithLeadingZeros(_US32 fraction_part, _US32 divisor)
|
||||||
{
|
{
|
||||||
UNITY_OUTPUT_CHAR('.');
|
UNITY_OUTPUT_CHAR('.');
|
||||||
@ -262,9 +262,13 @@ static void UnityPrintDecimalAndNumberWithLeadingZeros(_US32 fraction_part, _US3
|
|||||||
if (fraction_part == 0) break; /* Truncate trailing 0's */
|
if (fraction_part == 0) break; /* Truncate trailing 0's */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#define ROUND_TIES_TO_EVEN(num_int, num) \
|
#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_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 */
|
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];
|
* char buffer[19];
|
||||||
@ -326,7 +330,7 @@ void UnityPrintFloat(_UD number)
|
|||||||
UnityPrintNumber(exponent);
|
UnityPrintNumber(exponent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif /* UNITY_FLOAT_VERBOSE */
|
#endif /* ! UNITY_EXCLUDE_FLOAT_PRINT */
|
||||||
|
|
||||||
/*-----------------------------------------------*/
|
/*-----------------------------------------------*/
|
||||||
|
|
||||||
@ -691,6 +695,7 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef UNITY_EXCLUDE_FLOAT
|
#ifndef UNITY_EXCLUDE_FLOAT
|
||||||
|
|
||||||
static int UnityFloatsWithin(_UF delta, _UF expected, _UF actual)
|
static int UnityFloatsWithin(_UF delta, _UF expected, _UF actual)
|
||||||
{
|
{
|
||||||
_UF diff;
|
_UF diff;
|
||||||
@ -753,7 +758,7 @@ void UnityAssertFloatsWithin(const _UF delta,
|
|||||||
if (!UnityFloatsWithin(delta, expected, actual))
|
if (!UnityFloatsWithin(delta, expected, actual))
|
||||||
{
|
{
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
UnityTestResultsFailBegin(lineNumber);
|
||||||
#ifdef UNITY_FLOAT_VERBOSE
|
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||||
UnityPrint(UnityStrExpected);
|
UnityPrint(UnityStrExpected);
|
||||||
UnityPrintFloat(expected);
|
UnityPrintFloat(expected);
|
||||||
UnityPrint(UnityStrWas);
|
UnityPrint(UnityStrWas);
|
||||||
@ -818,7 +823,7 @@ void UnityAssertFloatSpecial(const _UF actual,
|
|||||||
UnityPrint(UnityStrNot);
|
UnityPrint(UnityStrNot);
|
||||||
UnityPrint(trait_names[trait_index]);
|
UnityPrint(trait_names[trait_index]);
|
||||||
UnityPrint(UnityStrWas);
|
UnityPrint(UnityStrWas);
|
||||||
#ifdef UNITY_FLOAT_VERBOSE
|
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||||
UnityPrintFloat(actual);
|
UnityPrintFloat(actual);
|
||||||
#else
|
#else
|
||||||
if (should_be_trait)
|
if (should_be_trait)
|
||||||
@ -867,11 +872,11 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const _UD* expected,
|
|||||||
UnityTestResultsFailBegin(lineNumber);
|
UnityTestResultsFailBegin(lineNumber);
|
||||||
UnityPrint(UnityStrElement);
|
UnityPrint(UnityStrElement);
|
||||||
UnityPrintNumberUnsigned(num_elements - elements - 1);
|
UnityPrintNumberUnsigned(num_elements - elements - 1);
|
||||||
#ifdef UNITY_DOUBLE_VERBOSE
|
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||||
UnityPrint(UnityStrExpected);
|
UnityPrint(UnityStrExpected);
|
||||||
UnityPrintFloat((float)(*ptr_expected));
|
UnityPrintFloat(*ptr_expected);
|
||||||
UnityPrint(UnityStrWas);
|
UnityPrint(UnityStrWas);
|
||||||
UnityPrintFloat((float)(*ptr_actual));
|
UnityPrintFloat(*ptr_actual);
|
||||||
#else
|
#else
|
||||||
UnityPrint(UnityStrDelta);
|
UnityPrint(UnityStrDelta);
|
||||||
#endif
|
#endif
|
||||||
@ -895,11 +900,11 @@ void UnityAssertDoublesWithin(const _UD delta,
|
|||||||
if (!UnityDoublesWithin(delta, expected, actual))
|
if (!UnityDoublesWithin(delta, expected, actual))
|
||||||
{
|
{
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
UnityTestResultsFailBegin(lineNumber);
|
||||||
#ifdef UNITY_DOUBLE_VERBOSE
|
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||||
UnityPrint(UnityStrExpected);
|
UnityPrint(UnityStrExpected);
|
||||||
UnityPrintFloat((float)expected);
|
UnityPrintFloat(expected);
|
||||||
UnityPrint(UnityStrWas);
|
UnityPrint(UnityStrWas);
|
||||||
UnityPrintFloat((float)actual);
|
UnityPrintFloat(actual);
|
||||||
#else
|
#else
|
||||||
UnityPrint(UnityStrDelta);
|
UnityPrint(UnityStrDelta);
|
||||||
#endif
|
#endif
|
||||||
@ -961,7 +966,7 @@ void UnityAssertDoubleSpecial(const _UD actual,
|
|||||||
UnityPrint(UnityStrNot);
|
UnityPrint(UnityStrNot);
|
||||||
UnityPrint(trait_names[trait_index]);
|
UnityPrint(trait_names[trait_index]);
|
||||||
UnityPrint(UnityStrWas);
|
UnityPrint(UnityStrWas);
|
||||||
#ifdef UNITY_DOUBLE_VERBOSE
|
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||||
UnityPrintFloat(actual);
|
UnityPrintFloat(actual);
|
||||||
#else
|
#else
|
||||||
if (should_be_trait)
|
if (should_be_trait)
|
||||||
|
@ -37,13 +37,11 @@ void tearDown(void);
|
|||||||
* - define UNITY_EXCLUDE_FLOAT to disallow floating point comparisons
|
* - 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_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_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_INCLUDE_DOUBLE to allow double floating point comparisons
|
||||||
* - define UNITY_EXCLUDE_DOUBLE to disallow double floating point comparisons (default)
|
* - 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_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_TYPE to specify something other than double
|
||||||
* - define UNITY_DOUBLE_VERBOSE to print floating point values in errors (uses sprintf)
|
* - define UNITY_EXCLUDE_FLOAT_PRINT to trim binary size, won't print floating point values in errors
|
||||||
* - define UNITY_VERBOSE_NUMBER_MAX_LENGTH to change maximum length of printed numbers (used by sprintf)
|
|
||||||
|
|
||||||
* Output
|
* Output
|
||||||
* - by default, Unity prints to standard out with putchar. define UNITY_OUTPUT_CHAR(a) with a different function if desired
|
* - 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_INCLUDE_FLOAT
|
||||||
#undef UNITY_FLOAT_PRECISION
|
#undef UNITY_FLOAT_PRECISION
|
||||||
#undef UNITY_FLOAT_TYPE
|
#undef UNITY_FLOAT_TYPE
|
||||||
#undef UNITY_FLOAT_VERBOSE
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
@ -225,16 +224,13 @@ typedef UNITY_FLOAT_TYPE _UF;
|
|||||||
/* No Floating Point Support */
|
/* No Floating Point Support */
|
||||||
#undef UNITY_DOUBLE_PRECISION
|
#undef UNITY_DOUBLE_PRECISION
|
||||||
#undef UNITY_DOUBLE_TYPE
|
#undef UNITY_DOUBLE_TYPE
|
||||||
#undef UNITY_DOUBLE_VERBOSE
|
|
||||||
|
|
||||||
#ifdef UNITY_INCLUDE_DOUBLE
|
#ifdef UNITY_INCLUDE_DOUBLE
|
||||||
#undef UNITY_INCLUDE_DOUBLE
|
#undef UNITY_INCLUDE_DOUBLE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef UNITY_FLOAT_VERBOSE
|
typedef _UF _UD;
|
||||||
typedef _UF _UD;
|
/* For parameter in UnityPrintFloat(_UD), which aliases to double or float */
|
||||||
/* For parameter in UnityPrintFloat, double promotion required */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
@ -250,12 +246,6 @@ typedef UNITY_FLOAT_TYPE _UF;
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef UNITY_DOUBLE_VERBOSE
|
|
||||||
#ifndef UNITY_FLOAT_VERBOSE
|
|
||||||
#define UNITY_FLOAT_VERBOSE
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*-------------------------------------------------------
|
/*-------------------------------------------------------
|
||||||
* Output Method: stdout (DEFAULT)
|
* Output Method: stdout (DEFAULT)
|
||||||
*-------------------------------------------------------*/
|
*-------------------------------------------------------*/
|
||||||
@ -443,7 +433,7 @@ void UnityPrintNumber(const _U_SINT number);
|
|||||||
void UnityPrintNumberUnsigned(const _U_UINT number);
|
void UnityPrintNumberUnsigned(const _U_UINT number);
|
||||||
void UnityPrintNumberHex(const _U_UINT number, const char nibbles);
|
void UnityPrintNumberHex(const _U_UINT number, const char nibbles);
|
||||||
|
|
||||||
#ifdef UNITY_FLOAT_VERBOSE
|
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
|
||||||
void UnityPrintFloat(const _UD number);
|
void UnityPrintFloat(const _UD number);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -2285,10 +2285,14 @@ void testFailureCountIncrementsAndIsReturnedAtEnd(void)
|
|||||||
|
|
||||||
void testCstringsEscapeSequence(void)
|
void testCstringsEscapeSequence(void)
|
||||||
{
|
{
|
||||||
|
#ifndef USING_OUTPUT_SPY
|
||||||
|
TEST_IGNORE();
|
||||||
|
#else
|
||||||
startPutcharSpy();
|
startPutcharSpy();
|
||||||
UnityPrint("\x16\x10");
|
UnityPrint("\x16\x10");
|
||||||
endPutcharSpy();
|
endPutcharSpy();
|
||||||
TEST_ASSERT_EQUAL_STRING("\\x16\\x10", getBufferPutcharSpy());
|
TEST_ASSERT_EQUAL_STRING("\\x16\\x10", getBufferPutcharSpy());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#define TEST_ASSERT_EQUAL_PRINT_NUMBERS(expected, actual) { \
|
#define TEST_ASSERT_EQUAL_PRINT_NUMBERS(expected, actual) { \
|
||||||
@ -3228,16 +3232,14 @@ void testNotEqualFloatArraysLengthZero(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef UNITY_FLOAT_VERBOSE
|
|
||||||
#define TEST_ASSERT_EQUAL_PRINT_FLOATING(expected, actual) { \
|
#define TEST_ASSERT_EQUAL_PRINT_FLOATING(expected, actual) { \
|
||||||
startPutcharSpy(); UnityPrintFloat((actual)); endPutcharSpy(); \
|
startPutcharSpy(); UnityPrintFloat((actual)); endPutcharSpy(); \
|
||||||
TEST_ASSERT_EQUAL_STRING((expected), getBufferPutcharSpy()); \
|
TEST_ASSERT_EQUAL_STRING((expected), getBufferPutcharSpy()); \
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
void testFloatVerbosePrinting(void)
|
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.0", 0.0f);
|
||||||
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000000...", 0.000000499f);
|
TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000000...", 0.000000499f);
|
||||||
float smallest = 0.0000005f;
|
float smallest = 0.0000005f;
|
||||||
|
Reference in New Issue
Block a user