diff --git a/include/yaml-cpp/emitter.h b/include/yaml-cpp/emitter.h index 9f43060..d57392f 100644 --- a/include/yaml-cpp/emitter.h +++ b/include/yaml-cpp/emitter.h @@ -7,6 +7,7 @@ #pragma once #endif +#include #include #include #include @@ -158,10 +159,9 @@ inline Emitter& Emitter::WriteStreamable(T value) { bool special = false; if (std::is_floating_point_v) { - if ((std::numeric_limits::has_quiet_NaN && - value == std::numeric_limits::quiet_NaN()) || - (std::numeric_limits::has_signaling_NaN && - value == std::numeric_limits::signaling_NaN())) { + if ((std::numeric_limits::has_quiet_NaN || + std::numeric_limits::has_signaling_NaN) && + value != value) { special = true; stream << ".nan"; } else if (std::numeric_limits::has_infinity) { diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index dfd4f34..f48178c 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -253,8 +253,9 @@ TEST_F(EmitterTest, ScalarFormat) { out << DoubleQuoted << "explicit double-quoted scalar"; out << "auto-detected\ndouble-quoted scalar"; out << "a non-\"auto-detected\" double-quoted scalar"; - out << Literal << "literal scalar\nthat may span\nmany, many\nlines " - "and have \"whatever\" crazy\tsymbols that we like"; + out << Literal + << "literal scalar\nthat may span\nmany, many\nlines " + "and have \"whatever\" crazy\tsymbols that we like"; out << EndSeq; ExpectEmit( @@ -526,9 +527,10 @@ TEST_F(EmitterTest, SimpleComment) { TEST_F(EmitterTest, MultiLineComment) { out << BeginSeq; - out << "item 1" << Comment( - "really really long\ncomment that couldn't " - "possibly\nfit on one line"); + out << "item 1" + << Comment( + "really really long\ncomment that couldn't " + "possibly\nfit on one line"); out << "item 2"; out << EndSeq; @@ -984,6 +986,33 @@ TEST_F(EmitterTest, ValueOfBackslash) { ExpectEmit("foo: \"\\\\\""); } +TEST_F(EmitterTest, Infinity) { + out << YAML::BeginMap; + out << YAML::Key << "foo" << YAML::Value + << std::numeric_limits::infinity(); + out << YAML::EndMap; + + ExpectEmit("foo: .inf"); +} + +TEST_F(EmitterTest, NegInfinity) { + out << YAML::BeginMap; + out << YAML::Key << "foo" << YAML::Value + << -std::numeric_limits::infinity(); + out << YAML::EndMap; + + ExpectEmit("foo: -.inf"); +} + +TEST_F(EmitterTest, NaN) { + out << YAML::BeginMap; + out << YAML::Key << "foo" << YAML::Value + << -std::numeric_limits::quiet_NaN(); + out << YAML::EndMap; + + ExpectEmit("foo: .nan"); +} + class EmitterErrorTest : public ::testing::Test { protected: void ExpectEmitError(const std::string& expectedError) { @@ -1034,5 +1063,5 @@ TEST_F(EmitterErrorTest, InvalidAlias) { ExpectEmitError(ErrorMsg::INVALID_ALIAS); } -} -} +} // namespace +} // namespace YAML