Better support for infs and NaNs in JSON writer

As the types and values sent into the writer are determined by Catch2,
I do not expect to actually need this support, but it is better to have
it and not be surprised in the future.
This commit is contained in:
Martin Hořeňovský
2026-07-23 14:26:26 +02:00
parent 0cc833ea95
commit c267251e70
20 changed files with 441 additions and 18 deletions
@@ -7,11 +7,13 @@
// SPDX-License-Identifier: BSL-1.0
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/internal/catch_jsonwriter.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <limits>
#include <locale>
#include <sstream>
@@ -199,6 +201,29 @@ TEST_CASE( "JsonWriter serializes numbers independently of the global locale",
"\"array\": [\n 2.5,\n 1234567\n ]\n}" ) );
}
TEMPLATE_TEST_CASE( "JsonWriter serializes non-finite FP using Python spelling",
"[JsonWriter][floating-point]",
float,
double ) {
std::stringstream sstream;
SECTION( "NaN" ) {
Catch::JsonValueWriter{ sstream }.write(
std::numeric_limits<TestType>::quiet_NaN() );
REQUIRE( sstream.str() == "NaN" );
}
SECTION( "Pos nf" ) {
Catch::JsonValueWriter{ sstream }.write(
std::numeric_limits<TestType>::infinity() );
REQUIRE( sstream.str() == "Infinity" );
}
SECTION( "Neg inf" ) {
Catch::JsonValueWriter{ sstream }.write(
-std::numeric_limits<TestType>::infinity() );
REQUIRE( sstream.str() == "-Infinity" );
}
}
TEST_CASE( "JsonWriter benchmarks", "[JsonWriter][!benchmark]" ) {
const auto input_length = GENERATE( as<size_t>{}, 10, 100, 10'000 );
std::string test_input( input_length, 'a' );