From c51c359b7601f4f071444a4579d1678b5aac7d06 Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周一, 09 11月 2015 23:30:07 +0800 Subject: [PATCH] Custom double to string conversion routine --- libscpi/test/test_scpi_utils.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 44 insertions(+), 2 deletions(-) diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c index d25b132..f13f63b 100644 --- a/libscpi/test/test_scpi_utils.c +++ b/libscpi/test/test_scpi_utils.c @@ -225,6 +225,47 @@ CU_ASSERT_STRING_EQUAL(str, "1111111011011100101110101001100001110110010101000011001000010000"); } +static void test_scpi_dtostre() { + const size_t strsize = 49 + 1; + double val[] = {NAN, INFINITY, -INFINITY, 0, + 1, 1.1, 1.01, 1.001, 1.0001, 1.00001, 1.000001, 1.0000001, 1.00000001, + 1.000000001, 1.0000000001, 1.00000000001, 1.000000000001, + 1.0000000000001, 1e-5, 1.1e-5, 1.01e-5, 1.001e-5, 1.0001e-5, 1.00001e-5, + 1.000001e-5, 1.0000001e-5, 1.00000001e-5, 1.000000001e-5, + 1.0000000001e-5, 1.00000000001e-5, 1.000000000001e-5, + 1.0000000000001e-5, 1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, + 123456789, 1234567890, 12345678901, 123456789012, 1234567890123, + 12345678901234, 123456789012345, 1234567890123456, 12345678901234567, + 123456789012345678, 1234567890123456789, 1234567890123456789e1, 1.1, + 10.1, 100.1, 1000.1, 10000.1, 100000.1, 1000000.1, 10000000.1, + 100000000.1, 1000000000.1, 0.1234567890123456789, + 0.01234567890123456789, 0.001234567890123456789, + 0.0001234567890123456789, 0.00001234567890123456789, + 0.000001234567890123456789, 0.0000001234567890123456789, + 0.00000001234567890123456789, 0.00000000123456789, 0.000000000123456789, + 0.0000000000123456789, 0.00000000000123456789, 0.000000000000123456789, + 0.0000000000000123456789, 0.00000000000000123456789, + 0.000000000000000123456789, 0.0000000000000000123456789, + -1e-5, -1.1e-5, -1.01e-5, -1.001e-5, -1.0001e-5, -1.00001e-5, + -1.00001e-6, -1.00001e-10, -1.00001e-20, -1.00001e-50, -1.00001e-100, + -1.00001e-200, -1.00001e-300, -1.00001e6, -1.00001e10, -1.00001e20, + -1.00001e50, -1.00001e100, -1.00001e150, -1.00001e200, -1.00001e300, + 1.7976931348623157e308, 2.2250738585072014e-308, + -1.7976931348623157e308, -2.2250738585072014e-308}; + int N = sizeof (val) / sizeof (*val); + int i; + char str[strsize]; + char ref[strsize]; + size_t len; + + for (i = 0; i < N; i++) { + len = strlen(SCPI_dtostre(val[i], str, strsize, 15, 0)); + snprintf(ref, strsize, "%.15lg", val[i]); + CU_ASSERT(len == strlen(ref)); + CU_ASSERT_STRING_EQUAL(str, ref); + } +} + static void test_floatToStr() { const size_t max = 49 + 1; float val[] = {1, -1, 1.1, -1.1, 1e3, 1e30, -1.3e30, -1.3e-30}; @@ -245,7 +286,7 @@ static void test_doubleToStr() { const size_t max = 49 + 1; double val[] = {1, -1, 1.1, -1.1, 1e3, 1e30, -1.3e30, -1.3e-30}; - int N = sizeof (val) / sizeof (double); + int N = sizeof (val) / sizeof (*val); int i; char str[max]; char ref[max]; @@ -253,7 +294,7 @@ for (i = 0; i < N; i++) { len = SCPI_DoubleToStr(val[i], str, max); - snprintf(ref, max, "%lg", val[i]); + snprintf(ref, max, "%.15lg", val[i]); CU_ASSERT(len == strlen(ref)); CU_ASSERT_STRING_EQUAL(str, ref); } @@ -707,6 +748,7 @@ || (NULL == CU_add_test(pSuite, "UInt32ToStrBase", test_UInt32ToStrBase)) || (NULL == CU_add_test(pSuite, "Int64ToStr", test_Int64ToStr)) || (NULL == CU_add_test(pSuite, "UInt64ToStrBase", test_UInt64ToStrBase)) + || (NULL == CU_add_test(pSuite, "SCPI_dtostre", test_scpi_dtostre)) || (NULL == CU_add_test(pSuite, "floatToStr", test_floatToStr)) || (NULL == CU_add_test(pSuite, "doubleToStr", test_doubleToStr)) || (NULL == CU_add_test(pSuite, "strBaseToInt32", test_strBaseToInt32)) -- Gitblit v1.9.1