Fix NaN check and add unit tests
This commit is contained in:
parent
92693d6e60
commit
8739e21e6a
@ -7,6 +7,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -158,10 +159,9 @@ inline Emitter& Emitter::WriteStreamable(T value) {
|
|||||||
|
|
||||||
bool special = false;
|
bool special = false;
|
||||||
if (std::is_floating_point_v<T>) {
|
if (std::is_floating_point_v<T>) {
|
||||||
if ((std::numeric_limits<T>::has_quiet_NaN &&
|
if ((std::numeric_limits<T>::has_quiet_NaN ||
|
||||||
value == std::numeric_limits<T>::quiet_NaN()) ||
|
std::numeric_limits<T>::has_signaling_NaN) &&
|
||||||
(std::numeric_limits<T>::has_signaling_NaN &&
|
value != value) {
|
||||||
value == std::numeric_limits<T>::signaling_NaN())) {
|
|
||||||
special = true;
|
special = true;
|
||||||
stream << ".nan";
|
stream << ".nan";
|
||||||
} else if (std::numeric_limits<T>::has_infinity) {
|
} else if (std::numeric_limits<T>::has_infinity) {
|
||||||
|
|||||||
@ -253,8 +253,9 @@ TEST_F(EmitterTest, ScalarFormat) {
|
|||||||
out << DoubleQuoted << "explicit double-quoted scalar";
|
out << DoubleQuoted << "explicit double-quoted scalar";
|
||||||
out << "auto-detected\ndouble-quoted scalar";
|
out << "auto-detected\ndouble-quoted scalar";
|
||||||
out << "a non-\"auto-detected\" double-quoted scalar";
|
out << "a non-\"auto-detected\" double-quoted scalar";
|
||||||
out << Literal << "literal scalar\nthat may span\nmany, many\nlines "
|
out << Literal
|
||||||
"and have \"whatever\" crazy\tsymbols that we like";
|
<< "literal scalar\nthat may span\nmany, many\nlines "
|
||||||
|
"and have \"whatever\" crazy\tsymbols that we like";
|
||||||
out << EndSeq;
|
out << EndSeq;
|
||||||
|
|
||||||
ExpectEmit(
|
ExpectEmit(
|
||||||
@ -526,9 +527,10 @@ TEST_F(EmitterTest, SimpleComment) {
|
|||||||
|
|
||||||
TEST_F(EmitterTest, MultiLineComment) {
|
TEST_F(EmitterTest, MultiLineComment) {
|
||||||
out << BeginSeq;
|
out << BeginSeq;
|
||||||
out << "item 1" << Comment(
|
out << "item 1"
|
||||||
"really really long\ncomment that couldn't "
|
<< Comment(
|
||||||
"possibly\nfit on one line");
|
"really really long\ncomment that couldn't "
|
||||||
|
"possibly\nfit on one line");
|
||||||
out << "item 2";
|
out << "item 2";
|
||||||
out << EndSeq;
|
out << EndSeq;
|
||||||
|
|
||||||
@ -984,6 +986,33 @@ TEST_F(EmitterTest, ValueOfBackslash) {
|
|||||||
ExpectEmit("foo: \"\\\\\"");
|
ExpectEmit("foo: \"\\\\\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(EmitterTest, Infinity) {
|
||||||
|
out << YAML::BeginMap;
|
||||||
|
out << YAML::Key << "foo" << YAML::Value
|
||||||
|
<< std::numeric_limits<float>::infinity();
|
||||||
|
out << YAML::EndMap;
|
||||||
|
|
||||||
|
ExpectEmit("foo: .inf");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(EmitterTest, NegInfinity) {
|
||||||
|
out << YAML::BeginMap;
|
||||||
|
out << YAML::Key << "foo" << YAML::Value
|
||||||
|
<< -std::numeric_limits<float>::infinity();
|
||||||
|
out << YAML::EndMap;
|
||||||
|
|
||||||
|
ExpectEmit("foo: -.inf");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(EmitterTest, NaN) {
|
||||||
|
out << YAML::BeginMap;
|
||||||
|
out << YAML::Key << "foo" << YAML::Value
|
||||||
|
<< -std::numeric_limits<float>::quiet_NaN();
|
||||||
|
out << YAML::EndMap;
|
||||||
|
|
||||||
|
ExpectEmit("foo: .nan");
|
||||||
|
}
|
||||||
|
|
||||||
class EmitterErrorTest : public ::testing::Test {
|
class EmitterErrorTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
void ExpectEmitError(const std::string& expectedError) {
|
void ExpectEmitError(const std::string& expectedError) {
|
||||||
@ -1034,5 +1063,5 @@ TEST_F(EmitterErrorTest, InvalidAlias) {
|
|||||||
|
|
||||||
ExpectEmitError(ErrorMsg::INVALID_ALIAS);
|
ExpectEmitError(ErrorMsg::INVALID_ALIAS);
|
||||||
}
|
}
|
||||||
}
|
} // namespace
|
||||||
}
|
} // namespace YAML
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user