userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
float_special_to_json_test.cpp
1
#
include
<
gtest
/
gtest
.
h
>
2
3
#
include
<
cmath
>
4
#
include
<
string
>
5
6
#
include
<
userver
/
protobuf
/
json
/
convert
.
hpp
>
7
#
include
<
userver
/
utest
/
assert_macros
.
hpp
>
8
9
#
include
"utils.hpp"
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
protobuf::json::tests {
14
15
template
<
typename
T>
16
std::string GetFloatSpecialValue(
const
T val) {
17
if
(std::isnan(val)) {
18
return
"NaN"
;
19
}
else
if
(std::isinf(val)) {
20
return
val > 0 ?
"Infinity"
:
"-Infinity"
;
21
}
else
{
22
throw
std::runtime_error(
"not a special value"
);
23
}
24
}
25
26
template
<
typename
T>
27
class
FloatSpecialToJsonTest :
public
::
testing
::Test {
28
public
:
29
using
Param = T;
30
};
31
32
using
FpTypes = ::testing::Types<
33
FloatQuietNan,
34
FloatSignalingNan,
35
FloatPositiveInfinity,
36
FloatNegativeInfinity,
37
DoubleQuietNan,
38
DoubleSignalingNan,
39
DoublePositiveInfinity,
40
DoubleNegativeInfinity>;
41
42
TYPED_TEST_SUITE(FloatSpecialToJsonTest, FpTypes);
43
44
TYPED_TEST(FloatSpecialToJsonTest, Test) {
45
using
Param =
typename
TestFixture::Param;
46
47
if
constexpr
(Param::kSkip) {
48
return
;
49
}
50
51
const
auto
& value = Param::kValue;
52
auto
input = PrepareTestData(value);
53
formats::
json
::
Value
json;
54
formats::
json
::
Value
sample_json;
55
56
UASSERT_NO_THROW
((json = MessageToJson(input, {})));
57
UASSERT_NO_THROW
((sample_json = CreateSampleJson(input)));
58
59
EXPECT_EQ(json
[
"field1"
]
.As<std::string>(), GetFloatSpecialValue(value.field1));
60
EXPECT_EQ(json
[
"field1"
]
.As<std::string>(), sample_json
[
"field1"
]
.As<std::string>());
61
}
62
63
}
// namespace protobuf::json::tests
64
65
USERVER_NAMESPACE_END
protobuf
tests
json
float_special_to_json_test.cpp
Generated on
for userver by
Doxygen
1.17.0