1 | // |
2 | // FormatTest.cpp |
3 | // |
4 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
5 | // All rights reserved. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "FormatTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Any.h" |
15 | #include "Poco/Format.h" |
16 | #include "Poco/Exception.h" |
17 | |
18 | |
19 | using Poco::format; |
20 | using Poco::BadCastException; |
21 | using Poco::Int64; |
22 | using Poco::UInt64; |
23 | |
24 | |
25 | FormatTest::FormatTest(const std::string& rName): CppUnit::TestCase(rName) |
26 | { |
27 | } |
28 | |
29 | |
30 | FormatTest::~FormatTest() |
31 | { |
32 | } |
33 | |
34 | |
35 | void FormatTest::testChar() |
36 | { |
37 | char c = 'a'; |
38 | std::string s(format("%c" , c)); |
39 | assertTrue (s == "a" ); |
40 | s = format("%2c" , c); |
41 | assertTrue (s == " a" ); |
42 | s = format("%-2c" , c); |
43 | assertTrue (s == "a " ); |
44 | |
45 | s = format("%*c" , 2, c); |
46 | assertTrue (s == " a" ); |
47 | s = format("%-*c" , 2, c); |
48 | assertTrue (s == "a " ); |
49 | |
50 | s = format("%c" , std::string("foo" )); |
51 | assertTrue (s == "[ERRFMT]" ); |
52 | } |
53 | |
54 | |
55 | void FormatTest::testInt() |
56 | { |
57 | int i = 42; |
58 | std::string s(format("%d" , i)); |
59 | assertTrue (s == "42" ); |
60 | s = format("%4d" , i); |
61 | assertTrue (s == " 42" ); |
62 | s = format("%04d" , i); |
63 | assertTrue (s == "0042" ); |
64 | |
65 | s = format("%*d" , 4, i); |
66 | assertTrue (s == " 42" ); |
67 | s = format("%0*d" , 4, i); |
68 | assertTrue (s == "0042" ); |
69 | |
70 | short h = 42; |
71 | s = format("%hd" , h); |
72 | assertTrue (s == "42" ); |
73 | s = format("%4hd" , h); |
74 | assertTrue (s == " 42" ); |
75 | s = format("%04hd" , h); |
76 | assertTrue (s == "0042" ); |
77 | |
78 | s = format("%*hd" , 4, h); |
79 | assertTrue (s == " 42" ); |
80 | s = format("%0*hd" , 4, h); |
81 | assertTrue (s == "0042" ); |
82 | |
83 | unsigned short hu = 42; |
84 | s = format("%hu" , hu); |
85 | assertTrue (s == "42" ); |
86 | s = format("%4hu" , hu); |
87 | assertTrue (s == " 42" ); |
88 | s = format("%04hu" , hu); |
89 | assertTrue (s == "0042" ); |
90 | |
91 | s = format("%*hu" , 4, hu); |
92 | assertTrue (s == " 42" ); |
93 | s = format("%0*hu" , 4, hu); |
94 | assertTrue (s == "0042" ); |
95 | |
96 | unsigned x = 0x42; |
97 | s = format("%x" , x); |
98 | assertTrue (s == "42" ); |
99 | s = format("%4x" , x); |
100 | assertTrue (s == " 42" ); |
101 | s = format("%04x" , x); |
102 | assertTrue (s == "0042" ); |
103 | |
104 | s = format("%*x" , 4, x); |
105 | assertTrue (s == " 42" ); |
106 | s = format("%0*x" , 4, x); |
107 | assertTrue (s == "0042" ); |
108 | |
109 | unsigned o = 042; |
110 | s = format("%o" , o); |
111 | assertTrue (s == "42" ); |
112 | s = format("%4o" , o); |
113 | assertTrue (s == " 42" ); |
114 | s = format("%04o" , o); |
115 | assertTrue (s == "0042" ); |
116 | |
117 | s = format("%*o" , 4, o); |
118 | assertTrue (s == " 42" ); |
119 | s = format("%0*o" , 4, o); |
120 | assertTrue (s == "0042" ); |
121 | |
122 | unsigned u = 42; |
123 | s = format("%u" , u); |
124 | assertTrue (s == "42" ); |
125 | s = format("%4u" , u); |
126 | assertTrue (s == " 42" ); |
127 | s = format("%04u" , u); |
128 | assertTrue (s == "0042" ); |
129 | |
130 | s = format("%*u" , 4, u); |
131 | assertTrue (s == " 42" ); |
132 | s = format("%0*u" , 4, u); |
133 | assertTrue (s == "0042" ); |
134 | |
135 | long l = 42; |
136 | s = format("%ld" , l); |
137 | assertTrue (s == "42" ); |
138 | s = format("%4ld" , l); |
139 | assertTrue (s == " 42" ); |
140 | s = format("%04ld" , l); |
141 | assertTrue (s == "0042" ); |
142 | |
143 | s = format("%*ld" , 4, l); |
144 | assertTrue (s == " 42" ); |
145 | s = format("%0*ld" , 4, l); |
146 | assertTrue (s == "0042" ); |
147 | |
148 | unsigned long ul = 42; |
149 | s = format("%lu" , ul); |
150 | assertTrue (s == "42" ); |
151 | s = format("%4lu" , ul); |
152 | assertTrue (s == " 42" ); |
153 | s = format("%04lu" , ul); |
154 | assertTrue (s == "0042" ); |
155 | |
156 | s = format("%*lu" , 4, ul); |
157 | assertTrue (s == " 42" ); |
158 | s = format("%0*lu" , 4, ul); |
159 | assertTrue (s == "0042" ); |
160 | |
161 | unsigned long xl = 0x42; |
162 | s = format("%lx" , xl); |
163 | assertTrue (s == "42" ); |
164 | s = format("%4lx" , xl); |
165 | assertTrue (s == " 42" ); |
166 | s = format("%04lx" , xl); |
167 | assertTrue (s == "0042" ); |
168 | |
169 | s = format("%*lx" , 4, xl); |
170 | assertTrue (s == " 42" ); |
171 | s = format("%0*lx" , 4, xl); |
172 | assertTrue (s == "0042" ); |
173 | |
174 | Int64 i64 = 42; |
175 | s = format("%Ld" , i64); |
176 | assertTrue (s == "42" ); |
177 | s = format("%4Ld" , i64); |
178 | assertTrue (s == " 42" ); |
179 | s = format("%04Ld" , i64); |
180 | assertTrue (s == "0042" ); |
181 | |
182 | s = format("%*Ld" , 4, i64); |
183 | assertTrue (s == " 42" ); |
184 | s = format("%0*Ld" , 4, i64); |
185 | assertTrue (s == "0042" ); |
186 | |
187 | UInt64 ui64 = 42; |
188 | s = format("%Lu" , ui64); |
189 | assertTrue (s == "42" ); |
190 | s = format("%4Lu" , ui64); |
191 | assertTrue (s == " 42" ); |
192 | s = format("%04Lu" , ui64); |
193 | assertTrue (s == "0042" ); |
194 | |
195 | s = format("%*Lu" , 4, ui64); |
196 | assertTrue (s == " 42" ); |
197 | s = format("%0*Lu" , 4, ui64); |
198 | assertTrue (s == "0042" ); |
199 | |
200 | x = 0xaa; |
201 | s = format("%x" , x); |
202 | assertTrue (s == "aa" ); |
203 | s = format("%X" , x); |
204 | assertTrue (s == "AA" ); |
205 | |
206 | i = 42; |
207 | s = format("%+d" , i); |
208 | assertTrue (s == "+42" ); |
209 | |
210 | i = -42; |
211 | s = format("%+d" , i); |
212 | assertTrue (s == "-42" ); |
213 | s = format("%+04d" , i); |
214 | assertTrue (s == "-042" ); |
215 | |
216 | s = format("%+0*d" , 4, i); |
217 | assertTrue (s == "-042" ); |
218 | |
219 | s = format("%d" , i); |
220 | assertTrue (s == "-42" ); |
221 | |
222 | s = format("%d" , i); |
223 | assertTrue (s == "-42" ); |
224 | |
225 | x = 0x42; |
226 | s = format("%#x" , x); |
227 | assertTrue (s == "0x42" ); |
228 | |
229 | s = format("%d" , l); |
230 | assertTrue (s == "[ERRFMT]" ); |
231 | } |
232 | |
233 | |
234 | void FormatTest::testBool() |
235 | { |
236 | bool b = true; |
237 | std::string s = format("%b" , b); |
238 | assertTrue (s == "1" ); |
239 | |
240 | b = false; |
241 | s = format("%b" , b); |
242 | assertTrue (s == "0" ); |
243 | |
244 | std::vector<Poco::Any> bv; |
245 | bv.push_back(false); |
246 | bv.push_back(true); |
247 | bv.push_back(false); |
248 | bv.push_back(true); |
249 | bv.push_back(false); |
250 | bv.push_back(true); |
251 | bv.push_back(false); |
252 | bv.push_back(true); |
253 | bv.push_back(false); |
254 | bv.push_back(true); |
255 | |
256 | s.clear(); |
257 | format(s, "%b%b%b%b%b%b%b%b%b%b" , bv); |
258 | assertTrue (s == "0101010101" ); |
259 | } |
260 | |
261 | |
262 | void FormatTest::testAnyInt() |
263 | { |
264 | char c = 42; |
265 | std::string s(format("%?i" , c)); |
266 | assertTrue (s == "42" ); |
267 | |
268 | c = 43; |
269 | s.clear(); |
270 | format(s, "%?i" , c); |
271 | assertTrue (s == "43" ); |
272 | |
273 | bool b = true; |
274 | s = format("%?i" , b); |
275 | assertTrue (s == "1" ); |
276 | |
277 | signed char sc = -42; |
278 | s = format("%?i" , sc); |
279 | assertTrue (s == "-42" ); |
280 | |
281 | unsigned char uc = 65; |
282 | s = format("%?i" , uc); |
283 | assertTrue (s == "65" ); |
284 | |
285 | short ss = -134; |
286 | s = format("%?i" , ss); |
287 | assertTrue (s == "-134" ); |
288 | |
289 | unsigned short us = 200; |
290 | s = format("%?i" , us); |
291 | assertTrue (s == "200" ); |
292 | |
293 | int i = -12345; |
294 | s = format("%?i" , i); |
295 | assertTrue (s == "-12345" ); |
296 | |
297 | unsigned ui = 12345; |
298 | s = format("%?i" , ui); |
299 | assertTrue (s == "12345" ); |
300 | |
301 | long l = -54321; |
302 | s = format("%?i" , l); |
303 | assertTrue (s == "-54321" ); |
304 | |
305 | unsigned long ul = 54321; |
306 | s = format("%?i" , ul); |
307 | assertTrue (s == "54321" ); |
308 | |
309 | Int64 i64 = -12345678; |
310 | s = format("%?i" , i64); |
311 | assertTrue (s == "-12345678" ); |
312 | |
313 | UInt64 ui64 = 12345678; |
314 | s = format("%?i" , ui64); |
315 | assertTrue (s == "12345678" ); |
316 | |
317 | ss = 0x42; |
318 | s = format("%?x" , ss); |
319 | assertTrue (s == "42" ); |
320 | |
321 | ss = 042; |
322 | s = format("%?o" , ss); |
323 | assertTrue (s == "42" ); |
324 | } |
325 | |
326 | |
327 | void FormatTest::testFloatFix() |
328 | { |
329 | double d = 1.5; |
330 | std::string s(format("%f" , d)); |
331 | assertTrue (s.find("1.50" ) == 0); |
332 | |
333 | s = format("%10f" , d); |
334 | assertTrue (s.find(" 1.50" ) != std::string::npos); |
335 | |
336 | s = format("%*f" , 10, d); |
337 | assertTrue (s.find(" 1.50" ) != std::string::npos); |
338 | |
339 | s = format("%6.2f" , d); |
340 | assertTrue (s == " 1.50" ); |
341 | s = format("%-6.2f" , d); |
342 | assertTrue (s == "1.50 " ); |
343 | |
344 | s = format("%*.*f" , 6, 2, d); |
345 | assertTrue (s == " 1.50" ); |
346 | s = format("%-*.*f" , 6,2, d); |
347 | assertTrue (s == "1.50 " ); |
348 | |
349 | float f = 1.5; |
350 | s = format("%hf" , f); |
351 | assertTrue (s.find("1.50" ) == 0); |
352 | |
353 | s = format("%.0f" , 1.0); |
354 | assertTrue (s == "1" ); |
355 | |
356 | s = format("%.*f" , 0, 1.0); |
357 | assertTrue (s == "1" ); |
358 | } |
359 | |
360 | |
361 | void FormatTest::testFloatSci() |
362 | { |
363 | double d = 1.5; |
364 | std::string s(format("%e" , d)); |
365 | assertTrue (s.find("1.50" ) == 0); |
366 | assertTrue (s.find("0e+0" ) != std::string::npos); |
367 | |
368 | s = format("%20e" , d); |
369 | assertTrue (s.find(" 1.50" ) != std::string::npos); |
370 | assertTrue (s.find("0e+0" ) != std::string::npos); |
371 | |
372 | s = format("%*e" , 20, d); |
373 | assertTrue (s.find(" 1.50" ) != std::string::npos); |
374 | assertTrue (s.find("0e+0" ) != std::string::npos); |
375 | |
376 | s = format("%10.2e" , d); |
377 | assertTrue (s == " 1.50e+000" || s == " 1.50e+00" ); |
378 | s = format("%-10.2e" , d); |
379 | assertTrue (s == "1.50e+000 " || s == "1.50e+00 " ); |
380 | s = format("%-10.2E" , d); |
381 | assertTrue (s == "1.50E+000 " || s == "1.50E+00 " ); |
382 | |
383 | s = format("%*.*e" , 10, 2, d); |
384 | assertTrue (s == " 1.50e+000" || s == " 1.50e+00" ); |
385 | s = format("%-*.*e" , 10, 2, d); |
386 | assertTrue (s == "1.50e+000 " || s == "1.50e+00 " ); |
387 | s = format("%-*.*E" , 10, 2, d); |
388 | assertTrue (s == "1.50E+000 " || s == "1.50E+00 " ); |
389 | } |
390 | |
391 | |
392 | void FormatTest::testString() |
393 | { |
394 | std::string foo("foo" ); |
395 | std::string s(format("%s" , foo)); |
396 | assertTrue (s == "foo" ); |
397 | |
398 | s = format("%5s" , foo); |
399 | assertTrue (s == " foo" ); |
400 | |
401 | s = format("%-5s" , foo); |
402 | assertTrue (s == "foo " ); |
403 | |
404 | s = format("%*s" , 5, foo); |
405 | assertTrue (s == " foo" ); |
406 | |
407 | s = format("%-*s" , 5, foo); |
408 | assertTrue (s == "foo " ); |
409 | |
410 | s = format("%s%%a" , foo); |
411 | assertTrue (s == "foo%a" ); |
412 | |
413 | s = format("'%s%%''%s%%'" , foo, foo); |
414 | assertTrue (s == "'foo%''foo%'" ); |
415 | } |
416 | |
417 | |
418 | void FormatTest::testMultiple() |
419 | { |
420 | std::string s(format("aaa%dbbb%4dccc" , 1, 2)); |
421 | assertTrue (s == "aaa1bbb 2ccc" ); |
422 | |
423 | s = format("%%%d%%%d%%%d" , 1, 2, 3); |
424 | assertTrue (s == "%1%2%3" ); |
425 | |
426 | s = format("%d%d%d%d" , 1, 2, 3, 4); |
427 | assertTrue (s == "1234" ); |
428 | |
429 | s = format("%d%d%d%d%d" , 1, 2, 3, 4, 5); |
430 | assertTrue (s == "12345" ); |
431 | |
432 | s = format("%d%d%d%d%d%d" , 1, 2, 3, 4, 5, 6); |
433 | assertTrue (s == "123456" ); |
434 | } |
435 | |
436 | |
437 | void FormatTest::testIndex() |
438 | { |
439 | std::string s(format("%[1]d%[0]d" , 1, 2)); |
440 | assertTrue (s == "21" ); |
441 | |
442 | s = format("%[5]d%[4]d%[3]d%[2]d%[1]d%[0]d" , 1, 2, 3, 4, 5, 6); |
443 | assertTrue (s == "654321" ); |
444 | |
445 | s = format("%%%[1]d%%%[2]d%%%d" , 1, 2, 3); |
446 | assertTrue (s == "%2%3%1" ); |
447 | |
448 | s = format("%%%d%%%d%%%[0]d" , 1, 2); |
449 | assertTrue (s == "%1%2%1" ); |
450 | } |
451 | |
452 | |
453 | void FormatTest::setUp() |
454 | { |
455 | } |
456 | |
457 | |
458 | void FormatTest::tearDown() |
459 | { |
460 | } |
461 | |
462 | |
463 | CppUnit::Test* FormatTest::suite() |
464 | { |
465 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("FormatTest" ); |
466 | |
467 | CppUnit_addTest(pSuite, FormatTest, testChar); |
468 | CppUnit_addTest(pSuite, FormatTest, testBool); |
469 | CppUnit_addTest(pSuite, FormatTest, testInt); |
470 | CppUnit_addTest(pSuite, FormatTest, testAnyInt); |
471 | CppUnit_addTest(pSuite, FormatTest, testFloatFix); |
472 | CppUnit_addTest(pSuite, FormatTest, testFloatSci); |
473 | CppUnit_addTest(pSuite, FormatTest, testString); |
474 | CppUnit_addTest(pSuite, FormatTest, testMultiple); |
475 | CppUnit_addTest(pSuite, FormatTest, testIndex); |
476 | |
477 | return pSuite; |
478 | } |
479 | |