提交 1dfcb544 编写于 作者: J jsalling

Start adding tests. Add const and simplify code.

上级 393f2cb5
...@@ -284,7 +284,7 @@ void UnityPrintFloat(_UD number) ...@@ -284,7 +284,7 @@ void UnityPrintFloat(_UD number)
else if (number < 0.0000005 && number > 0) UnityPrint("0.000000..."); /* Small numbers */ 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 */ 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; _UU32 integer_part = (_UU32)number;
_US32 fraction_part = (_US32)((number - integer_part)*1000000.0 + 0.5); _US32 fraction_part = (_US32)((number - integer_part)*1000000.0 + 0.5);
/* Double precision calculation gives best performance for six rounded decimal places */ /* Double precision calculation gives best performance for six rounded decimal places */
...@@ -300,7 +300,7 @@ void UnityPrintFloat(_UD number) ...@@ -300,7 +300,7 @@ void UnityPrintFloat(_UD number)
} }
else /* Number is larger, use exponential format of 9 digits, "%.8e" */ else /* Number is larger, use exponential format of 9 digits, "%.8e" */
{ {
_US32 divisor = (1000000000/10); const _US32 divisor = (1000000000/10);
_US32 integer_part; _US32 integer_part;
double divide = 10.0; double divide = 10.0;
int exponent = 9; int exponent = 9;
...@@ -314,9 +314,7 @@ void UnityPrintFloat(_UD number) ...@@ -314,9 +314,7 @@ void UnityPrintFloat(_UD number)
/* Double precision calculation required for float, to produce 9 rounded digits */ /* Double precision calculation required for float, to produce 9 rounded digits */
UNITY_OUTPUT_CHAR('0' + integer_part / divisor); UNITY_OUTPUT_CHAR('0' + integer_part / divisor);
integer_part %= divisor; UnityPrintDecimalAndNumberWithLeadingZeros(integer_part % divisor, divisor / 10);
divisor /= 10;
UnityPrintDecimalAndNumberWithLeadingZeros(integer_part, divisor);
UNITY_OUTPUT_CHAR('e'); UNITY_OUTPUT_CHAR('e');
UNITY_OUTPUT_CHAR('+'); UNITY_OUTPUT_CHAR('+');
if (exponent < 10) UNITY_OUTPUT_CHAR('0'); if (exponent < 10) UNITY_OUTPUT_CHAR('0');
......
...@@ -3228,15 +3228,57 @@ void testNotEqualFloatArraysLengthZero(void) ...@@ -3228,15 +3228,57 @@ void testNotEqualFloatArraysLengthZero(void)
#endif #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) void testFloatVerbosePrinting(void)
{ {
#ifdef UNITY_FLOAT_VERBOSE #ifdef UNITY_FLOAT_VERBOSE
UnityPrintFloat(123456789.0f); TEST_ASSERT_EQUAL_PRINT_FLOATING("0.0", 0.0f);
UnityPrintFloat(100000000.0f); TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000000...", 0.000000499f);
UnityPrintFloat(65536.0f*65536.0f); float smallest = 0.0000005f;
UnityPrintFloat(1000000000.0f); *(int*)&smallest += 1;
UnityPrintFloat(10000000000.0f); TEST_ASSERT_EQUAL_PRINT_FLOATING("0.000001", smallest);
UnityPrintFloat(9999999000.0f); 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 #endif
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册