1 | // |
2 | // NumberFormatterTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "NumberFormatterTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/NumberFormatter.h" |
15 | #include <sstream> |
16 | |
17 | |
18 | using Poco::NumberFormatter; |
19 | using Poco::Int64; |
20 | using Poco::UInt64; |
21 | |
22 | |
23 | NumberFormatterTest::NumberFormatterTest(const std::string& rName): CppUnit::TestCase(rName) |
24 | { |
25 | } |
26 | |
27 | |
28 | NumberFormatterTest::~NumberFormatterTest() |
29 | { |
30 | } |
31 | |
32 | |
33 | void NumberFormatterTest::testFormat() |
34 | { |
35 | assertTrue (NumberFormatter::format(123) == "123" ); |
36 | assertTrue (NumberFormatter::format(-123) == "-123" ); |
37 | assertTrue (NumberFormatter::format(-123, 5) == " -123" ); |
38 | |
39 | assertTrue (NumberFormatter::format((unsigned) 123) == "123" ); |
40 | assertTrue (NumberFormatter::format((unsigned) 123, 5) == " 123" ); |
41 | assertTrue (NumberFormatter::format0((unsigned) 123, 5) == "00123" ); |
42 | |
43 | #ifndef POCO_LONG_IS_64_BIT |
44 | assertTrue (NumberFormatter::format((long) 123) == "123" ); |
45 | assertTrue (NumberFormatter::format((long) -123) == "-123" ); |
46 | assertTrue (NumberFormatter::format((long) -123, 5) == " -123" ); |
47 | |
48 | assertTrue (NumberFormatter::format((unsigned long) 123) == "123" ); |
49 | assertTrue (NumberFormatter::format((unsigned long) 123, 5) == " 123" ); |
50 | #endif // POCO_LONG_IS_64_BIT |
51 | |
52 | assertTrue (NumberFormatter::format(123) == "123" ); |
53 | assertTrue (NumberFormatter::format(-123) == "-123" ); |
54 | assertTrue (NumberFormatter::format(-123, 5) == " -123" ); |
55 | |
56 | assertTrue (NumberFormatter::format((Int64) 123) == "123" ); |
57 | assertTrue (NumberFormatter::format((Int64) -123) == "-123" ); |
58 | assertTrue (NumberFormatter::format((Int64) -123, 5) == " -123" ); |
59 | |
60 | assertTrue (NumberFormatter::format((UInt64) 123) == "123" ); |
61 | assertTrue (NumberFormatter::format((UInt64) 123, 5) == " 123" ); |
62 | |
63 | if (sizeof(void*) == 4) |
64 | { |
65 | assertTrue (NumberFormatter::format((void*) 0x12345678) == "12345678" ); |
66 | } |
67 | else |
68 | { |
69 | assertTrue (NumberFormatter::format((void*) 0x12345678) == "0000000012345678" ); |
70 | } |
71 | } |
72 | |
73 | |
74 | void NumberFormatterTest::testFormat0() |
75 | { |
76 | assertTrue (NumberFormatter::format0(123, 5) == "00123" ); |
77 | assertTrue (NumberFormatter::format0(-123, 5) == "-0123" ); |
78 | #ifndef POCO_LONG_IS_64_BIT |
79 | assertTrue (NumberFormatter::format0((long) 123, 5) == "00123" ); |
80 | assertTrue (NumberFormatter::format0((long) -123, 5) == "-0123" ); |
81 | assertTrue (NumberFormatter::format0((unsigned long) 123, 5) == "00123" ); |
82 | #endif // POCO_LONG_IS_64_BIT |
83 | assertTrue (NumberFormatter::format0((Int64) 123, 5) == "00123" ); |
84 | assertTrue (NumberFormatter::format0((Int64) -123, 5) == "-0123" ); |
85 | assertTrue (NumberFormatter::format0((UInt64) 123, 5) == "00123" ); |
86 | } |
87 | |
88 | |
89 | void NumberFormatterTest::testFormatBool() |
90 | { |
91 | assertTrue (NumberFormatter::format(true, NumberFormatter::FMT_TRUE_FALSE) == "true" ); |
92 | assertTrue (NumberFormatter::format(false, NumberFormatter::FMT_TRUE_FALSE) == "false" ); |
93 | assertTrue (NumberFormatter::format(true, NumberFormatter::FMT_YES_NO) == "yes" ); |
94 | assertTrue (NumberFormatter::format(false, NumberFormatter::FMT_YES_NO) == "no" ); |
95 | assertTrue (NumberFormatter::format(true, NumberFormatter::FMT_ON_OFF) == "on" ); |
96 | assertTrue (NumberFormatter::format(false, NumberFormatter::FMT_ON_OFF) == "off" ); |
97 | } |
98 | |
99 | |
100 | void NumberFormatterTest::testFormatHex() |
101 | { |
102 | assertTrue (NumberFormatter::formatHex(0x12) == "12" ); |
103 | assertTrue (NumberFormatter::formatHex(0xab) == "AB" ); |
104 | assertTrue (NumberFormatter::formatHex(0x12, 4) == "0012" ); |
105 | assertTrue (NumberFormatter::formatHex(0xab, 4) == "00AB" ); |
106 | |
107 | assertTrue (NumberFormatter::formatHex((unsigned) 0x12) == "12" ); |
108 | assertTrue (NumberFormatter::formatHex((unsigned) 0xab) == "AB" ); |
109 | assertTrue (NumberFormatter::formatHex((unsigned) 0x12, 4) == "0012" ); |
110 | assertTrue (NumberFormatter::formatHex((unsigned) 0xab, 4) == "00AB" ); |
111 | #ifndef POCO_LONG_IS_64_BIT |
112 | assertTrue (NumberFormatter::formatHex((long) 0x12) == "12" ); |
113 | assertTrue (NumberFormatter::formatHex((long) 0xab) == "AB" ); |
114 | assertTrue (NumberFormatter::formatHex((long) 0x12, 4) == "0012" ); |
115 | assertTrue (NumberFormatter::formatHex((long) 0xab, 4) == "00AB" ); |
116 | |
117 | assertTrue (NumberFormatter::formatHex((unsigned long) 0x12) == "12" ); |
118 | assertTrue (NumberFormatter::formatHex((unsigned long) 0xab) == "AB" ); |
119 | assertTrue (NumberFormatter::formatHex((unsigned long) 0x12, 4) == "0012" ); |
120 | assertTrue (NumberFormatter::formatHex((unsigned long) 0xab, 4) == "00AB" ); |
121 | #endif // POCO_LONG_IS_64_BIT |
122 | assertTrue (NumberFormatter::formatHex((Int64) 0x12) == "12" ); |
123 | assertTrue (NumberFormatter::formatHex((Int64) 0xab) == "AB" ); |
124 | assertTrue (NumberFormatter::formatHex((Int64) 0x12, 4) == "0012" ); |
125 | assertTrue (NumberFormatter::formatHex((Int64) 0xab, 4) == "00AB" ); |
126 | |
127 | assertTrue (NumberFormatter::formatHex((UInt64) 0x12) == "12" ); |
128 | assertTrue (NumberFormatter::formatHex((UInt64) 0xab) == "AB" ); |
129 | assertTrue (NumberFormatter::formatHex((UInt64) 0x12, 4) == "0012" ); |
130 | assertTrue (NumberFormatter::formatHex((UInt64) 0xab, 4) == "00AB" ); |
131 | |
132 | assertTrue (NumberFormatter::formatHex(0x12, true) == "0x12" ); |
133 | assertTrue (NumberFormatter::formatHex(0xab, true) == "0xAB" ); |
134 | assertTrue (NumberFormatter::formatHex(0x12, 4, true) == "0x12" ); |
135 | assertTrue (NumberFormatter::formatHex(0xab, 4, true) == "0xAB" ); |
136 | assertTrue (NumberFormatter::formatHex(0x12, 6, true) == "0x0012" ); |
137 | assertTrue (NumberFormatter::formatHex(0xab, 6, true) == "0x00AB" ); |
138 | |
139 | assertTrue (NumberFormatter::formatHex((unsigned) 0x12, true) == "0x12" ); |
140 | assertTrue (NumberFormatter::formatHex((unsigned) 0xab, true) == "0xAB" ); |
141 | assertTrue (NumberFormatter::formatHex((unsigned) 0x12, 4, true) == "0x12" ); |
142 | assertTrue (NumberFormatter::formatHex((unsigned) 0xab, 4, true) == "0xAB" ); |
143 | assertTrue (NumberFormatter::formatHex((unsigned) 0x12, 6, true) == "0x0012" ); |
144 | assertTrue (NumberFormatter::formatHex((unsigned) 0xab, 6, true) == "0x00AB" ); |
145 | |
146 | #ifndef POCO_LONG_IS_64_BIT |
147 | |
148 | assertTrue (NumberFormatter::formatHex((long) 0x12, true) == "0x12" ); |
149 | assertTrue (NumberFormatter::formatHex((long) 0xab, true) == "0xAB" ); |
150 | assertTrue (NumberFormatter::formatHex((long) 0x12, 4, true) == "0x12" ); |
151 | assertTrue (NumberFormatter::formatHex((long) 0xab, 4, true) == "0xAB" ); |
152 | assertTrue (NumberFormatter::formatHex((long) 0x12, 6, true) == "0x0012" ); |
153 | assertTrue (NumberFormatter::formatHex((long) 0xab, 6, true) == "0x00AB" ); |
154 | |
155 | assertTrue (NumberFormatter::formatHex((unsigned long) 0x12, true) == "0x12" ); |
156 | assertTrue (NumberFormatter::formatHex((unsigned long) 0xab, true) == "0xAB" ); |
157 | assertTrue (NumberFormatter::formatHex((unsigned long) 0x12, 4, true) == "0x12" ); |
158 | assertTrue (NumberFormatter::formatHex((unsigned long) 0xab, 4, true) == "0xAB" ); |
159 | assertTrue (NumberFormatter::formatHex((unsigned long) 0x12, 6, true) == "0x0012" ); |
160 | assertTrue (NumberFormatter::formatHex((unsigned long) 0xab, 6, true) == "0x00AB" ); |
161 | |
162 | #endif // POCO_LONG_IS_64_BIT |
163 | |
164 | assertTrue (NumberFormatter::formatHex((Int64) 0x12, true) == "0x12" ); |
165 | assertTrue (NumberFormatter::formatHex((Int64) 0xab, true) == "0xAB" ); |
166 | assertTrue (NumberFormatter::formatHex((Int64) 0x12, 4, true) == "0x12" ); |
167 | assertTrue (NumberFormatter::formatHex((Int64) 0xab, 4, true) == "0xAB" ); |
168 | assertTrue (NumberFormatter::formatHex((Int64) 0x12, 6, true) == "0x0012" ); |
169 | assertTrue (NumberFormatter::formatHex((Int64) 0xab, 6, true) == "0x00AB" ); |
170 | |
171 | assertTrue (NumberFormatter::formatHex((UInt64) 0x12, true) == "0x12" ); |
172 | assertTrue (NumberFormatter::formatHex((UInt64) 0xab, true) == "0xAB" ); |
173 | assertTrue (NumberFormatter::formatHex((UInt64) 0x12, 4, true) == "0x12" ); |
174 | assertTrue (NumberFormatter::formatHex((UInt64) 0xab, 4, true) == "0xAB" ); |
175 | assertTrue (NumberFormatter::formatHex((UInt64) 0x12, 6, true) == "0x0012" ); |
176 | assertTrue (NumberFormatter::formatHex((UInt64) 0xab, 6, true) == "0x00AB" ); |
177 | |
178 | } |
179 | |
180 | |
181 | void NumberFormatterTest::testFormatFloat() |
182 | { |
183 | assertTrue (NumberFormatter::format(1.0f) == "1" ); |
184 | assertTrue (NumberFormatter::format(1.23f) == "1.23" ); |
185 | assertTrue (NumberFormatter::format(-1.23f) == "-1.23" ); |
186 | assertTrue (NumberFormatter::format(0.1f) == "0.1" ); |
187 | assertTrue (NumberFormatter::format(-0.1f) == "-0.1" ); |
188 | assertTrue (NumberFormatter::format(1.23) == "1.23" ); |
189 | assertTrue (NumberFormatter::format(-1.23) == "-1.23" ); |
190 | assertTrue (NumberFormatter::format(1.0) == "1" ); |
191 | assertTrue (NumberFormatter::format(-1.0) == "-1" ); |
192 | assertTrue (NumberFormatter::format(0.1) == "0.1" ); |
193 | assertTrue (NumberFormatter::format(-0.1) == "-0.1" ); |
194 | |
195 | int decDigits = std::numeric_limits<double>::digits10; |
196 | std::ostringstream ostr; |
197 | ostr << "0." << std::string(decDigits - 1, '0') << '1'; |
198 | assertTrue (NumberFormatter::format(1 / std::pow(10., decDigits)) == ostr.str()); |
199 | |
200 | ostr.str("" ); |
201 | ostr << "1e-" << decDigits + 1; |
202 | std::string str (ostr.str()); |
203 | std::string str1 (NumberFormatter::format(1 / std::pow(10., decDigits + 1))); |
204 | assertTrue (NumberFormatter::format(1 / std::pow(10., decDigits + 1)) == ostr.str()); |
205 | |
206 | assertTrue (NumberFormatter::format(12.25) == "12.25" ); |
207 | assertTrue (NumberFormatter::format(12.25, 4) == "12.2500" ); |
208 | assertTrue (NumberFormatter::format(12.25, 8, 4) == " 12.2500" ); |
209 | |
210 | assertTrue (NumberFormatter::format(12.45f, 2) == "12.45" ); |
211 | |
212 | assertTrue (NumberFormatter::format(-12.25) == "-12.25" ); |
213 | assertTrue (NumberFormatter::format(-12.25, 4) == "-12.2500" ); |
214 | assertTrue (NumberFormatter::format(-12.25, 10, 4) == " -12.2500" ); |
215 | assertTrue (NumberFormatter::format(-12.25, 10, 2) == " -12.25" ); |
216 | assertTrue (NumberFormatter::format(-12.25, 10, 1) == " -12.3" ); |
217 | |
218 | assertTrue (NumberFormatter::format(50.0, 3) == "50.000" ); |
219 | assertTrue (NumberFormatter::format(50.0f, 3) == "50.000" ); |
220 | assertTrue (NumberFormatter::format(50.123, 3) == "50.123" ); |
221 | assertTrue (NumberFormatter::format(50.123f, 3) == "50.123" ); |
222 | assertTrue (NumberFormatter::format(50.123, 0) == "50" ); |
223 | assertTrue (NumberFormatter::format(50.123f, 0) == "50" ); |
224 | assertTrue (NumberFormatter::format(50.546, 0) == "50" ); |
225 | assertTrue (NumberFormatter::format(50.546f, 0) == "50" ); |
226 | assertTrue (NumberFormatter::format(50.546f, 2) == "50.55" ); |
227 | } |
228 | |
229 | |
230 | void NumberFormatterTest::testAppend() |
231 | { |
232 | std::string s; |
233 | NumberFormatter::append(s, 123); |
234 | assertTrue (s == "123" ); |
235 | s.erase(); |
236 | NumberFormatter::append(s, 123, 4); |
237 | assertTrue (s == " 123" ); |
238 | s.erase(); |
239 | NumberFormatter::append0(s, 123, 5); |
240 | assertTrue (s == "00123" ); |
241 | s.erase(); |
242 | NumberFormatter::appendHex(s, 0xDEAD); |
243 | assertTrue (s == "DEAD" ); |
244 | s.erase(); |
245 | NumberFormatter::appendHex(s, 0xDEAD, 6); |
246 | assertTrue (s == "00DEAD" ); |
247 | s.erase(); |
248 | NumberFormatter::append(s, 123u); |
249 | assertTrue (s == "123" ); |
250 | s.erase(); |
251 | NumberFormatter::append(s, 123u, 4); |
252 | assertTrue (s == " 123" ); |
253 | s.erase(); |
254 | NumberFormatter::append0(s, 123u, 5); |
255 | assertTrue (s == "00123" ); |
256 | |
257 | |
258 | s.erase(); |
259 | NumberFormatter::append(s, 123.4); |
260 | assertTrue (s == "123.4" ); |
261 | s.erase(); |
262 | NumberFormatter::append(s, 123.4567, 2); |
263 | assertTrue (s == "123.46" ); |
264 | s.erase(); |
265 | NumberFormatter::append(s, 123.4567, 10, 5); |
266 | assertTrue (s == " 123.45670" ); |
267 | s.erase(); |
268 | NumberFormatter::append(s, 123., 2); |
269 | assertTrue (s == "123.00" ); |
270 | s.erase(); |
271 | NumberFormatter::append(s, static_cast<double>(1234567), 2); |
272 | assertTrue (s == "1234567.00" ); |
273 | s.erase(); |
274 | NumberFormatter::append(s, 1234567.0, 10, 1); |
275 | assertTrue (s == " 1234567.0" ); |
276 | } |
277 | |
278 | |
279 | void NumberFormatterTest::testInfNaN() |
280 | { |
281 | if (std::numeric_limits<double>::has_quiet_NaN) |
282 | { |
283 | assertTrue (Poco::NumberFormatter::format(std::numeric_limits<double>::quiet_NaN()) == "nan" ); |
284 | assertTrue (Poco::NumberFormatter::format(-std::numeric_limits<double>::quiet_NaN()) == "nan" ); |
285 | } |
286 | |
287 | if (std::numeric_limits<double>::has_infinity) |
288 | { |
289 | assertTrue (Poco::NumberFormatter::format(std::numeric_limits<double>::infinity()) == "inf" ); |
290 | assertTrue (Poco::NumberFormatter::format(-std::numeric_limits<double>::infinity()) == "-inf" ); |
291 | } |
292 | |
293 | if (std::numeric_limits<float>::has_quiet_NaN) |
294 | { |
295 | assertTrue (Poco::NumberFormatter::format(std::numeric_limits<float>::quiet_NaN()) == "nan" ); |
296 | assertTrue (Poco::NumberFormatter::format(-std::numeric_limits<float>::quiet_NaN()) == "nan" ); |
297 | } |
298 | |
299 | if (std::numeric_limits<float>::has_infinity) |
300 | { |
301 | assertTrue (Poco::NumberFormatter::format(std::numeric_limits<float>::infinity()) == "inf" ); |
302 | assertTrue (Poco::NumberFormatter::format(-std::numeric_limits<float>::infinity()) == "-inf" ); |
303 | } |
304 | } |
305 | |
306 | |
307 | void NumberFormatterTest::setUp() |
308 | { |
309 | } |
310 | |
311 | |
312 | void NumberFormatterTest::tearDown() |
313 | { |
314 | } |
315 | |
316 | |
317 | CppUnit::Test* NumberFormatterTest::suite() |
318 | { |
319 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NumberFormatterTest" ); |
320 | |
321 | CppUnit_addTest(pSuite, NumberFormatterTest, testFormat); |
322 | CppUnit_addTest(pSuite, NumberFormatterTest, testFormat0); |
323 | CppUnit_addTest(pSuite, NumberFormatterTest, testFormatBool); |
324 | CppUnit_addTest(pSuite, NumberFormatterTest, testFormatHex); |
325 | CppUnit_addTest(pSuite, NumberFormatterTest, testFormatFloat); |
326 | CppUnit_addTest(pSuite, NumberFormatterTest, testAppend); |
327 | CppUnit_addTest(pSuite, NumberFormatterTest, testInfNaN); |
328 | |
329 | return pSuite; |
330 | } |
331 | |