From e48fe0a07c087a313ed3baf2eeb47951cf3b32d1 Mon Sep 17 00:00:00 2001 From: jsalling Date: Wed, 12 Oct 2016 21:58:28 -0500 Subject: [PATCH] Reorganize NaN and Inf printing into if-else blocks --- src/unity.c | 58 +++++++++++++++++++----------------------- test/tests/testunity.c | 12 +++++++++ 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/unity.c b/src/unity.c index 0cfcbea..44c870f 100644 --- a/src/unity.c +++ b/src/unity.c @@ -251,58 +251,50 @@ void UnityPrintMask(const _U_UINT mask, const _U_UINT number) /*-----------------------------------------------*/ #ifdef UNITY_FLOAT_VERBOSE -#include - -#ifndef UNITY_VERBOSE_NUMBER_MAX_LENGTH -# ifdef UNITY_DOUBLE_VERBOSE -# define UNITY_VERBOSE_NUMBER_MAX_LENGTH 317 -# else -# define UNITY_VERBOSE_NUMBER_MAX_LENGTH 47 -# endif -#endif +/* + * char buffer[19]; + * if (number > 4294967296.0 || -number > 4294967296.0) + * snprintf(buffer, sizeof buffer, "%.6e", number); + * else + * snprintf(buffer, sizeof buffer, "%.6f", number); + * UnityPrint(buffer); + */ void UnityPrintFloat(_UD number) { - // char TempBuffer[UNITY_VERBOSE_NUMBER_MAX_LENGTH + 1]; - // snprintf(TempBuffer, sizeof(TempBuffer), "%.6f", number); - // UnityPrint(TempBuffer); - if (isnan(number)) - { - UnityPrint(UnityStrNaN); - return; - } - if (number < 0) { UNITY_OUTPUT_CHAR('-'); number = -number; } - if (isinf(number)) UnityPrintLen(UnityStrInf, 3); + + if (isnan(number)) UnityPrint(UnityStrNaN); + else if (isinf(number)) UnityPrintLen(UnityStrInf, 3); + else if (number < 0.0000005 && number > 0) UnityPrint("0.000000..."); /* Small number format */ else if (number < 4294967296.0f) /* Fits in an integer */ { _UU32 integer_part = (_UU32)number; _UD fraction_part = number - integer_part; _U_UINT fraction_bits = (_U_UINT)(fraction_part * 1000000.0f + 0.5f); + if (fraction_bits == 1000000) { fraction_bits = 0; integer_part += 1; } - _U_UINT divisor_int = 100000; + _U_UINT divisor = 100000; UnityPrintNumberUnsigned(integer_part); UNITY_OUTPUT_CHAR('.'); /* now mod and print, then divide divisor */ do { - UNITY_OUTPUT_CHAR((char)('0' + (fraction_bits / divisor_int))); - fraction_bits %= divisor_int; + UNITY_OUTPUT_CHAR((char)('0' + (fraction_bits / divisor))); + fraction_bits %= divisor; if (fraction_bits == 0) break; // Truncate trailing 0's - divisor_int /= 10; - } while (divisor_int > 0); - - + divisor /= 10; + } while (divisor > 0); } else /* Won't fit in an integer type */ { @@ -316,17 +308,19 @@ void UnityPrintFloat(_UD number) exponent++; } integer_part = (_UU32)(number / divide + 0.5f); - _UU32 divisor_int = 1000000; + + _UU32 divisor = 1000000; do { - UNITY_OUTPUT_CHAR((char)('0' + (integer_part / divisor_int))); + UNITY_OUTPUT_CHAR((char)('0' + (integer_part / divisor))); - integer_part %= divisor_int; - divisor_int /= 10; - if (divisor_int == 100000) UNITY_OUTPUT_CHAR('.'); - } while (divisor_int > 0); + integer_part %= divisor; + divisor /= 10; + if (divisor == 100000) UNITY_OUTPUT_CHAR('.'); + } while (divisor > 0); UNITY_OUTPUT_CHAR('e'); UNITY_OUTPUT_CHAR('+'); + if (exponent < 10) UNITY_OUTPUT_CHAR('0'); UnityPrintNumberUnsigned(exponent); } } diff --git a/test/tests/testunity.c b/test/tests/testunity.c index e66de4d..75c3552 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -3228,6 +3228,18 @@ void testNotEqualFloatArraysLengthZero(void) #endif } +void testFloatVerbosePrinting(void) +{ +#ifdef UNITY_FLOAT_VERBOSE + UnityPrintFloat(123456789.0f); + UnityPrintFloat(100000000.0f); + UnityPrintFloat(65536.0f*65536.0f); + UnityPrintFloat(1000000000.0f); + UnityPrintFloat(10000000000.0f); + UnityPrintFloat(9999999000.0f); +#endif +} + // ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DOUBLE SUPPORT ================== void testDoublesWithinDelta(void)