From 1dfcb54491f6c53087dc7e16874bb6f3efb5c550 Mon Sep 17 00:00:00 2001 From: jsalling Date: Sun, 6 Nov 2016 22:22:11 -0600 Subject: [PATCH] Start adding tests. Add const and simplify code. --- src/unity.c | 8 +++---- test/tests/testunity.c | 54 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/unity.c b/src/unity.c index 8ed1064..ecc9559 100644 --- a/src/unity.c +++ b/src/unity.c @@ -284,7 +284,7 @@ void UnityPrintFloat(_UD number) else if (number < 0.0000005 && number > 0) UnityPrint("0.000000..."); /* Small numbers */ else if (number < 4294967296.0f) /* Rounded result fits in 32 bits, "%.6f" format */ { - _US32 divisor = (1000000/10); + const _US32 divisor = (1000000/10); _UU32 integer_part = (_UU32)number; _US32 fraction_part = (_US32)((number - integer_part)*1000000.0 + 0.5); /* Double precision calculation gives best performance for six rounded decimal places */ @@ -300,7 +300,7 @@ void UnityPrintFloat(_UD number) } else /* Number is larger, use exponential format of 9 digits, "%.8e" */ { - _US32 divisor = (1000000000/10); + const _US32 divisor = (1000000000/10); _US32 integer_part; double divide = 10.0; int exponent = 9; @@ -314,9 +314,7 @@ void UnityPrintFloat(_UD number) /* Double precision calculation required for float, to produce 9 rounded digits */ UNITY_OUTPUT_CHAR('0' + integer_part / divisor); - integer_part %= divisor; - divisor /= 10; - UnityPrintDecimalAndNumberWithLeadingZeros(integer_part, divisor); + UnityPrintDecimalAndNumberWithLeadingZeros(integer_part % divisor, divisor / 10); UNITY_OUTPUT_CHAR('e'); UNITY_OUTPUT_CHAR('+'); if (exponent < 10) UNITY_OUTPUT_CHAR('0'); diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 75c3552..76babfc 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -3228,15 +3228,57 @@ 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 - UnityPrintFloat(123456789.0f); - UnityPrintFloat(100000000.0f); - UnityPrintFloat(65536.0f*65536.0f); - UnityPrintFloat(1000000000.0f); - UnityPrintFloat(10000000000.0f); - UnityPrintFloat(9999999000.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.0", 0.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000000...", 0.000000499f); + float smallest = 0.0000005f; + *(int*)&smallest += 1; + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000001", smallest); + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.100469", 0.100469499f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0", 0.9999995f); /*Rounding to int place*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0", 1.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.25", 1.25f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("7.999999", 7.999999f); /*Not rounding*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.000002", 16.000002f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.000004", 16.000004f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.000006", 16.000006f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("4294967040.0", 4294967040.0f); /*Last full print integer*/ + + TEST_ASSERT_EQUAL_PRINT_FLOATING("4.2949673e+09", 4294967296.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("5.0e+09", 5000000000.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("8.0e+09", 8.0e+09f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("8.3099991e+09", 8309999104.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 1.0e+10f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.0e+10", 10000000000.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00005499e+10", 1.000055e+10f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.10000006e+38", 1.10000005e+38f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.63529943e+10", 1.63529943e+10f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("3.40282347e+38", 3.40282346638e38f); + + TEST_ASSERT_EQUAL_PRINT_FLOATING("Inf", 3.40282346638e38f*2.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("Inf", 1.0f / f_zero); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-Inf", -3.40282346638e38f*2.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("NaN", -3.40282346638e38f*2.0f * f_zero); + + //Double + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.100469", 0.10046949999999999); + TEST_ASSERT_EQUAL_PRINT_FLOATING("4294967295.999999", 4294967295.999999); + TEST_ASSERT_EQUAL_PRINT_FLOATING("4.2949673e+09", 4294967296.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("7.0e+100", 7.0e+100); + + TEST_ASSERT_EQUAL_PRINT_FLOATING("Inf", 1.7976931348623157e308*10.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("Inf", 1.0 / d_zero); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-Inf", -1.7976931348623157e308*10.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("NaN", -1.7976931348623157e308*10.0 * d_zero); #endif } -- GitLab