Skip to content

Commit 4e5fa3b

Browse files
authored
Fix long double inf test under Valgrind. Fixes #5175 (#5176)
* 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>
1 parent cba5dc0 commit 4e5fa3b

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

tests/src/unit-serialization.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,22 @@ TEST_CASE("dump for basic_json with long double number_float_t")
344344
SECTION("NaN and infinity dump as null")
345345
{
346346
CHECK(long_double_json(std::numeric_limits<long double>::quiet_NaN()).dump() == "null");
347-
CHECK(long_double_json(std::numeric_limits<long double>::infinity()).dump() == "null");
348-
CHECK(long_double_json(-std::numeric_limits<long double>::infinity()).dump() == "null");
347+
348+
// Probe the platform's runtime behavior — `volatile` forces a runtime
349+
// call rather than constexpr-folding to a known answer at compile time.
350+
// Skip the infinity assertions if std::isfinite() doesn't actually
351+
// recognize long double infinity on this platform (notably, Valgrind
352+
// 3.22's x87 80-bit emulation reports +/-inf as a large finite value).
353+
// TODO(rusloker): remove this guard once Valgrind's 80-bit long double
354+
// support ships (Valgrind bug https://bugs.kde.org/show_bug.cgi?id=197915,
355+
// ASSIGNED since 2009 — the Valgrind project tracks its bugs on
356+
// bugs.kde.org) and the minimum supported Valgrind version contains it.
357+
const volatile long double inf_probe = std::numeric_limits<long double>::infinity();
358+
if (!std::isfinite(inf_probe))
359+
{
360+
CHECK(long_double_json(std::numeric_limits<long double>::infinity()).dump() == "null");
361+
CHECK(long_double_json(-std::numeric_limits<long double>::infinity()).dump() == "null");
362+
}
349363
}
350364

351365
SECTION("dump output matches double for exactly-representable values")

0 commit comments

Comments
 (0)