test/zserio/StringConvertUtilTest.cpp
Line | Count | Source |
1 | | #include <array> |
2 | | #include <limits> |
3 | | |
4 | | #include "gtest/gtest.h" |
5 | | #include "zserio/StringConvertUtil.h" |
6 | | #include "zserio/StringView.h" |
7 | | |
8 | | namespace zserio |
9 | | { |
10 | | |
11 | | using allocator_type = std::allocator<uint8_t>; |
12 | | |
13 | | TEST(StringConvertUtilTest, convertInt8) |
14 | 1 | { |
15 | 1 | using type = int8_t; |
16 | 1 | const type value = std::numeric_limits<type>::min(); |
17 | 1 | std::array<char, 24> buffer = {}; |
18 | 1 | const char* valueInString = convertIntToString(buffer, value); |
19 | 1 | EXPECT_EQ(std::to_string(value), valueInString); |
20 | | |
21 | 1 | EXPECT_EQ(std::to_string(value), toString(value)); |
22 | 1 | } |
23 | | |
24 | | TEST(StringConvertUtilTest, convertUInt8) |
25 | 1 | { |
26 | 1 | using type = uint8_t; |
27 | 1 | const type value = std::numeric_limits<type>::max(); |
28 | 1 | std::array<char, 24> buffer = {}; |
29 | 1 | const char* valueInString = convertIntToString(buffer, value); |
30 | 1 | EXPECT_EQ(std::to_string(value), valueInString); |
31 | | |
32 | 1 | EXPECT_EQ(std::to_string(value), toString(value)); |
33 | 1 | } |
34 | | |
35 | | TEST(StringConvertUtilTest, convertInt16) |
36 | 1 | { |
37 | 1 | using type = int16_t; |
38 | 1 | const type value = std::numeric_limits<type>::min(); |
39 | 1 | std::array<char, 24> buffer = {}; |
40 | 1 | const char* valueInString = convertIntToString(buffer, value); |
41 | 1 | EXPECT_EQ(std::to_string(value), valueInString); |
42 | | |
43 | 1 | EXPECT_EQ(std::to_string(value), toString(value)); |
44 | 1 | } |
45 | | |
46 | | TEST(StringConvertUtilTest, convertUInt16) |
47 | 1 | { |
48 | 1 | using type = uint16_t; |
49 | 1 | const type value = std::numeric_limits<type>::max(); |
50 | 1 | std::array<char, 24> buffer = {}; |
51 | 1 | const char* valueInString = convertIntToString(buffer, value); |
52 | 1 | EXPECT_EQ(std::to_string(value), valueInString); |
53 | | |
54 | 1 | EXPECT_EQ(std::to_string(value), toString(value)); |
55 | 1 | } |
56 | | |
57 | | TEST(StringConvertUtilTest, convertInt32) |
58 | 1 | { |
59 | 1 | using type = int32_t; |
60 | 1 | const type value = std::numeric_limits<type>::min(); |
61 | 1 | std::array<char, 24> buffer = {}; |
62 | 1 | const char* valueInString = convertIntToString(buffer, value); |
63 | 1 | EXPECT_EQ(std::to_string(value), valueInString); |
64 | | |
65 | 1 | EXPECT_EQ(std::to_string(value), toString(value)); |
66 | 1 | } |
67 | | |
68 | | TEST(StringConvertUtilTest, convertUInt32) |
69 | 1 | { |
70 | 1 | using type = uint32_t; |
71 | 1 | const type value = std::numeric_limits<type>::max(); |
72 | 1 | std::array<char, 24> buffer = {}; |
73 | 1 | const char* valueInString = convertIntToString(buffer, value); |
74 | 1 | EXPECT_EQ(std::to_string(value), valueInString); |
75 | | |
76 | 1 | EXPECT_EQ(std::to_string(value), toString(value)); |
77 | 1 | } |
78 | | |
79 | | TEST(StringConvertUtilTest, convertInt64) |
80 | 1 | { |
81 | 1 | using type = int64_t; |
82 | 1 | const type value = std::numeric_limits<type>::min(); |
83 | 1 | std::array<char, 24> buffer = {}; |
84 | 1 | const char* valueInString = convertIntToString(buffer, value); |
85 | 1 | EXPECT_EQ(std::to_string(value), valueInString); |
86 | | |
87 | 1 | EXPECT_EQ(std::to_string(value), toString(value)); |
88 | 1 | } |
89 | | |
90 | | TEST(StringConvertUtilTest, convertUInt64) |
91 | 1 | { |
92 | 1 | using type = uint64_t; |
93 | 1 | const type value = std::numeric_limits<type>::max(); |
94 | 1 | std::array<char, 24> buffer = {}; |
95 | 1 | const char* valueInString = convertIntToString(buffer, value); |
96 | 1 | EXPECT_EQ(std::to_string(value), valueInString); |
97 | | |
98 | 1 | EXPECT_EQ(std::to_string(value), toString(value)); |
99 | 1 | } |
100 | | |
101 | | TEST(StringConvertUtilTest, convertFloat) |
102 | 1 | { |
103 | 1 | std::array<char, 24> integerBuffer = {}; |
104 | 1 | std::array<char, 24> floatingBuffer = {}; |
105 | 1 | const char* integerString = ""; |
106 | 1 | const char* floatingString = ""; |
107 | 1 | convertFloatToString(integerBuffer, floatingBuffer, 13579.2468F, integerString, floatingString); |
108 | 1 | EXPECT_EQ(std::string("13579"), integerString); |
109 | 1 | EXPECT_EQ(std::string("247"), floatingString); |
110 | | |
111 | 1 | convertFloatToString(integerBuffer, floatingBuffer, -2468.123456789F, integerString, floatingString); |
112 | 1 | EXPECT_EQ(std::string("-2468"), integerString); |
113 | 1 | EXPECT_EQ(std::string("123"), floatingString); |
114 | | |
115 | 1 | convertFloatToString(integerBuffer, floatingBuffer, 1.0F, integerString, floatingString); |
116 | 1 | EXPECT_EQ(std::string("1"), integerString); |
117 | 1 | EXPECT_EQ(std::string("0"), floatingString); |
118 | | |
119 | 1 | convertFloatToString(integerBuffer, floatingBuffer, 1E10F, integerString, floatingString); |
120 | 1 | EXPECT_EQ(std::string("10000000000"), integerString); |
121 | 1 | EXPECT_EQ(std::string("0"), floatingString); |
122 | | |
123 | 1 | convertFloatToString(integerBuffer, floatingBuffer, -1E10F, integerString, floatingString); |
124 | 1 | EXPECT_EQ(std::string("-10000000000"), integerString); |
125 | 1 | EXPECT_EQ(std::string("0"), floatingString); |
126 | | |
127 | 1 | convertFloatToString(integerBuffer, floatingBuffer, 1E20F, integerString, floatingString); |
128 | 1 | EXPECT_EQ(std::string("+Inf"), integerString); |
129 | 1 | EXPECT_EQ(nullptr, floatingString); |
130 | | |
131 | 1 | convertFloatToString(integerBuffer, floatingBuffer, -1E20F, integerString, floatingString); |
132 | 1 | EXPECT_EQ(std::string("-Inf"), integerString); |
133 | 1 | EXPECT_EQ(nullptr, floatingString); |
134 | 1 | } |
135 | | |
136 | | TEST(StringConvertUtilTest, convertBool) |
137 | 1 | { |
138 | 1 | EXPECT_EQ(std::string("true"), convertBoolToString(true)); |
139 | 1 | EXPECT_EQ(std::string("false"), convertBoolToString(false)); |
140 | | |
141 | 1 | EXPECT_EQ("true", toString(true)); |
142 | 1 | EXPECT_EQ("false", toString(false)); |
143 | 1 | } |
144 | | |
145 | | TEST(StringConvertUtilTest, convertStringView) |
146 | 1 | { |
147 | 1 | EXPECT_EQ(std::string(), toString(StringView())); |
148 | 1 | EXPECT_EQ("test", toString(StringView("test"))); |
149 | 1 | const std::array<char, 4> buffer = {'t', 'e', 's', 't'}; |
150 | 1 | EXPECT_EQ("test", toString(StringView(buffer.data(), buffer.size()))); |
151 | 1 | } |
152 | | |
153 | | } // namespace zserio |