From 13160d93b25d2491bf511de9eeb21bebf05cb7ad Mon Sep 17 00:00:00 2001 From: abolz Date: Fri, 8 Dec 2017 15:18:32 +0100 Subject: [PATCH] Add tests for #360 --- test/src/unit-regression.cpp | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 8eff9d764..556db4950 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -593,6 +593,64 @@ TEST_CASE("regression tests") "[json.exception.out_of_range.406] number overflow parsing '22e2222'"); } + SECTION("issue #360 - Loss of precision when serializing ") + { + auto CheckRoundtrip = [](double number) + { + CAPTURE(number); + + json j = number; + CHECK(j.is_number_float()); + + std::stringstream ss; + ss << j; + + CHECK_NOTHROW(ss >> j); + CHECK(j.is_number_float()); + CHECK(j.get() == number); + }; + + CheckRoundtrip(100000000000.1236); + CheckRoundtrip(std::numeric_limits::max()); + // Some more numbers which fail to roundtrip when serialized with + // digits10 significand digits (instead of max_digits10) + CheckRoundtrip(1.541888611948064e-17); + CheckRoundtrip(5.418771028591015e-16); + CheckRoundtrip(9.398685592608595e-15); + CheckRoundtrip(8.826843952762347e-14); + CheckRoundtrip(8.143291313475335e-13); + CheckRoundtrip(4.851328172762508e-12); + CheckRoundtrip(6.677850998084358e-11); + CheckRoundtrip(3.995398518174525e-10); + CheckRoundtrip(1.960452605645124e-9); + CheckRoundtrip(3.551812586302883e-8); + CheckRoundtrip(2.947988411689261e-7); + CheckRoundtrip(8.210166748056192e-6); + CheckRoundtrip(6.104889704266753e-5); + CheckRoundtrip(0.0008629954631330876); + CheckRoundtrip(0.004936993881051611); + CheckRoundtrip(0.08309725102608073); + CheckRoundtrip(0.5210494268499783); + CheckRoundtrip(6.382927930939767); + CheckRoundtrip(59.94947245358671); + CheckRoundtrip(361.0838651266122); + CheckRoundtrip(4678.354596181877); + CheckRoundtrip(61412.17658956043); + CheckRoundtrip(725696.0799057782); + CheckRoundtrip(2811732.583399828); + CheckRoundtrip(30178351.07533605); + CheckRoundtrip(689684880.3235844); + CheckRoundtrip(5714887673.555147); + CheckRoundtrip(84652038821.18808); + CheckRoundtrip(156510583431.7721); + CheckRoundtrip(5938450569021.732); + CheckRoundtrip(83623297654460.33); + CheckRoundtrip(701466573254773.6); + CheckRoundtrip(1369013370304513); + CheckRoundtrip(96963648023094720); + CheckRoundtrip(3.478237409280108e+17); + } + SECTION("issue #366 - json::parse on failed stream gets stuck") { std::ifstream f("file_not_found.json");