mirror of
https://github.com/nlohmann/json.git
synced 2026-05-20 05:05:24 +00:00
* Fix for printing long doubles bug in dump_float When you use long double as a floating point type with the current version of this file and try to dump json it prints trash instead of actual number. This if-else fixes the problem. On using long double you just need to add an 'L' modifier before 'g' in format string. Signed-off-by: Kirill Lokotkov <klokotkov@ya.ru> * C++11 compatibility Signed-off-by: Kirill Lokotkov <klokotkov@ya.ru> * Shorter solution Signed-off-by: Kirill Lokotkov <klokotkov@ya.ru> * Applied amalgamate Signed-off-by: rusloker <klokotkov@ya.ru> * Add unit tests for `dump()` with `long double` in custom `basic_json` Signed-off-by: rusloker <klokotkov@ya.ru> * Fix UB in `snprintf_float` by using `%.*Lg` for `long double` Signed-off-by: rusloker <klokotkov@ya.ru> * Use `std::array` for `values` in serialization unit tests to improve type safety Signed-off-by: rusloker <klokotkov@ya.ru> * Fix brace initialization for `std::array` in serialization unit tests Signed-off-by: rusloker <klokotkov@ya.ru> * Remove comments in `snprintf_float` regarding `%Lg` usage Signed-off-by: rusloker <klokotkov@ya.ru> * Skip `long double` infinity dump assertions under Valgrind Signed-off-by: rusloker <klokotkov@ya.ru> * Clarify Valgrind bug-tracker reference in `long double` test Signed-off-by: rusloker <klokotkov@ya.ru> * Satisfy clang-tidy in `long double` infinity probe Signed-off-by: rusloker <klokotkov@ya.ru> --------- Signed-off-by: Kirill Lokotkov <klokotkov@ya.ru> Signed-off-by: rusloker <klokotkov@ya.ru>
This commit is contained in:
@@ -344,8 +344,22 @@ TEST_CASE("dump for basic_json with long double number_float_t")
|
|||||||
SECTION("NaN and infinity dump as null")
|
SECTION("NaN and infinity dump as null")
|
||||||
{
|
{
|
||||||
CHECK(long_double_json(std::numeric_limits<long double>::quiet_NaN()).dump() == "null");
|
CHECK(long_double_json(std::numeric_limits<long double>::quiet_NaN()).dump() == "null");
|
||||||
CHECK(long_double_json(std::numeric_limits<long double>::infinity()).dump() == "null");
|
|
||||||
CHECK(long_double_json(-std::numeric_limits<long double>::infinity()).dump() == "null");
|
// Probe the platform's runtime behavior — `volatile` forces a runtime
|
||||||
|
// call rather than constexpr-folding to a known answer at compile time.
|
||||||
|
// Skip the infinity assertions if std::isfinite() doesn't actually
|
||||||
|
// recognize long double infinity on this platform (notably, Valgrind
|
||||||
|
// 3.22's x87 80-bit emulation reports +/-inf as a large finite value).
|
||||||
|
// TODO(rusloker): remove this guard once Valgrind's 80-bit long double
|
||||||
|
// support ships (Valgrind bug https://bugs.kde.org/show_bug.cgi?id=197915,
|
||||||
|
// ASSIGNED since 2009 — the Valgrind project tracks its bugs on
|
||||||
|
// bugs.kde.org) and the minimum supported Valgrind version contains it.
|
||||||
|
const volatile long double inf_probe = std::numeric_limits<long double>::infinity();
|
||||||
|
if (!std::isfinite(inf_probe))
|
||||||
|
{
|
||||||
|
CHECK(long_double_json(std::numeric_limits<long double>::infinity()).dump() == "null");
|
||||||
|
CHECK(long_double_json(-std::numeric_limits<long double>::infinity()).dump() == "null");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("dump output matches double for exactly-representable values")
|
SECTION("dump output matches double for exactly-representable values")
|
||||||
|
|||||||
Reference in New Issue
Block a user