From c69ff41fc26731bd5b83ce5f242184eea0358bc7 Mon Sep 17 00:00:00 2001 From: miloyip Date: Wed, 15 Apr 2015 14:23:00 +0800 Subject: [PATCH] Add tests for parsing number with exhaustive exponents and random signifcant --- test/unittest/readertest.cpp | 42 +++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index bccfbd6..7c70bc9 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -302,28 +302,40 @@ static void TestParseDouble() { "e-308", 2.2250738585072014e-308); -#if 0 // Very slow - static const unsigned count = 10000000; - // Random test for double { + static const unsigned count = 1000; // Tested with 1000000 locally Random r; + Reader reader; // Reusing reader to prevent heap allocation - for (unsigned i = 0; i < count; i++) { - internal::Double d; - do { + // Exhaustively test different exponents with random significant + for (uint64_t exp = 0; exp < 2047; exp++) { + ; + for (unsigned i = 0; i < count; i++) { // Need to call r() in two statements for cross-platform coherent sequence. - uint64_t u = uint64_t(r()) << 32; + uint64_t u = (exp << 52) | uint64_t(r() & 0x000FFFFF) << 32; u |= uint64_t(r()); - d = internal::Double(u); - } while (d.IsNan() || d.IsInf()/* || !d.IsNormal()*/); // Also work for subnormal now + internal::Double d = internal::Double(u); - char buffer[32]; - *internal::dtoa(d.Value(), buffer) = '\0'; - TEST_DOUBLE(fullPrecision, buffer, d.Value()); + char buffer[32]; + *internal::dtoa(d.Value(), buffer) = '\0'; + + StringStream s(buffer); + ParseDoubleHandler h; + ASSERT_EQ(kParseErrorNone, reader.Parse(s, h).Code()); + EXPECT_EQ(1u, h.step_); + internal::Double a(h.actual_); + if (fullPrecision) { + EXPECT_EQ(d.Uint64Value(), a.Uint64Value()); + if (d.Uint64Value() != a.Uint64Value()) + printf(" String: %sn Actual: %.17gnExpected: %.17gn", buffer, h.actual_, d.Value()); + } + else { + EXPECT_EQ(d.Sign(), a.Sign()); /* for 0.0 != -0.0 */ + EXPECT_DOUBLE_EQ(d.Value(), h.actual_); + } + } } } -#endif - #undef TEST_DOUBLE } @@ -651,7 +663,7 @@ TEST(Reader, ParseString_Error) { TEST_STRINGENCODING_ERROR(UTF32<>, UTF8<>, unsigned, ARRAY('[', '\"', 0x110000, '\"', ']', '\0')); // Malform ASCII sequence - TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', 0x80, '\"', ']', '\0')); + TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80), '\"', ']', '\0')); #undef ARRAY #undef TEST_STRINGARRAY_ERROR