diff options
| author | Martin Hořeňovský | 2026-07-23 14:26:17 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2026-07-23 14:26:26 +0200 |
| commit | c267251e709c52cda421e9490c5cbaf62914fa52 (patch) | |
| tree | a1330c2158cd39ac376d05fca0960a58cda679d7 /tests/SelfTest/IntrospectiveTests | |
| parent | 0cc833ea95bb7136c99be1b5b105780cae5766ff (diff) | |
| download | catch3-c267251e709c52cda421e9490c5cbaf62914fa52.tar.gz | |
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.
Diffstat (limited to 'tests/SelfTest/IntrospectiveTests')
| -rw-r--r-- | tests/SelfTest/IntrospectiveTests/Json.tests.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/SelfTest/IntrospectiveTests/Json.tests.cpp b/tests/SelfTest/IntrospectiveTests/Json.tests.cpp index 4f6789ec..d026c76c 100644 --- a/tests/SelfTest/IntrospectiveTests/Json.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Json.tests.cpp @@ -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' ); |