Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
de8253fc
Unverified
Commit
de8253fc
authored
Feb 15, 2020
by
Anton Onishchenko
Committed by
GitHub
Feb 14, 2020
Browse files
Fix storing inf and NaN (#817)
parent
29dcf92f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
1 deletion
+71
-1
include/yaml-cpp/node/convert.h
include/yaml-cpp/node/convert.h
+17
-1
test/node/node_test.cpp
test/node/node_test.cpp
+54
-0
No files found.
include/yaml-cpp/node/convert.h
View file @
de8253fc
...
@@ -8,10 +8,12 @@
...
@@ -8,10 +8,12 @@
#endif
#endif
#include <array>
#include <array>
#include <cmath>
#include <limits>
#include <limits>
#include <list>
#include <list>
#include <map>
#include <map>
#include <sstream>
#include <sstream>
#include <type_traits>
#include <vector>
#include <vector>
#include "yaml-cpp/binary.h"
#include "yaml-cpp/binary.h"
...
@@ -94,7 +96,21 @@ struct convert<_Null> {
...
@@ -94,7 +96,21 @@ struct convert<_Null> {
static Node encode(const type& rhs) { \
static Node encode(const type& rhs) { \
std::stringstream stream; \
std::stringstream stream; \
stream.precision(std::numeric_limits<type>::max_digits10); \
stream.precision(std::numeric_limits<type>::max_digits10); \
if (std::is_floating_point<type>::value) { \
if (std::isnan(rhs)) { \
stream << ".nan"; \
} else if (std::isinf(rhs)) { \
if (std::signbit(rhs)) { \
stream << "-.inf"; \
} else { \
stream << ".inf"; \
} \
} else { \
stream << rhs; \
} \
} else { \
stream << rhs; \
stream << rhs; \
} \
return Node(stream.str()); \
return Node(stream.str()); \
} \
} \
\
\
...
...
test/node/node_test.cpp
View file @
de8253fc
...
@@ -461,12 +461,66 @@ TEST(NodeTest, FloatingPrecisionFloat) {
...
@@ -461,12 +461,66 @@ TEST(NodeTest, FloatingPrecisionFloat) {
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
}
}
TEST
(
NodeTest
,
FloatingPrecisionPositiveInfinityFloat
)
{
if
(
!
std
::
numeric_limits
<
float
>::
has_infinity
)
{
return
;
}
const
float
x
=
std
::
numeric_limits
<
float
>::
infinity
();
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionNegativeInfinityFloat
)
{
if
(
!
std
::
numeric_limits
<
float
>::
has_infinity
)
{
return
;
}
const
float
x
=
-
std
::
numeric_limits
<
float
>::
infinity
();
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionNanFloat
)
{
if
(
!
std
::
numeric_limits
<
float
>::
has_quiet_NaN
)
{
return
;
}
const
float
x
=
std
::
numeric_limits
<
float
>::
quiet_NaN
();
Node
node
=
Node
(
x
);
EXPECT_TRUE
(
std
::
isnan
(
node
.
as
<
float
>
()));
}
TEST
(
NodeTest
,
FloatingPrecisionDouble
)
{
TEST
(
NodeTest
,
FloatingPrecisionDouble
)
{
const
double
x
=
0.123456789
;
const
double
x
=
0.123456789
;
Node
node
=
Node
(
x
);
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
double
>
());
EXPECT_EQ
(
x
,
node
.
as
<
double
>
());
}
}
TEST
(
NodeTest
,
FloatingPrecisionPositiveInfinityDouble
)
{
if
(
!
std
::
numeric_limits
<
double
>::
has_infinity
)
{
return
;
}
const
double
x
=
std
::
numeric_limits
<
double
>::
infinity
();
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionNegativeInfinityDouble
)
{
if
(
!
std
::
numeric_limits
<
double
>::
has_infinity
)
{
return
;
}
const
double
x
=
-
std
::
numeric_limits
<
double
>::
infinity
();
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
double
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionNanDouble
)
{
if
(
!
std
::
numeric_limits
<
double
>::
has_quiet_NaN
)
{
return
;
}
const
double
x
=
std
::
numeric_limits
<
double
>::
quiet_NaN
();
Node
node
=
Node
(
x
);
EXPECT_TRUE
(
std
::
isnan
(
node
.
as
<
double
>
()));
}
TEST
(
NodeTest
,
SpaceChar
)
{
TEST
(
NodeTest
,
SpaceChar
)
{
Node
node
=
Node
(
' '
);
Node
node
=
Node
(
' '
);
EXPECT_EQ
(
' '
,
node
.
as
<
char
>
());
EXPECT_EQ
(
' '
,
node
.
as
<
char
>
());
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment