test_suite.cpp
Go to the documentation of this file.
1 // \author (c) Marco Paland (info@paland.com)
3 // 2017-2019, PALANDesign Hannover, Germany
4 //
5 // \license The MIT License (MIT)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // THE SOFTWARE.
24 //
25 // \brief printf unit tests
26 //
28 
29 // use the 'catch' test framework
30 #define CATCH_CONFIG_MAIN
31 #include "catch.hpp"
32 
33 #include <string.h>
34 #include <sstream>
35 #include <math.h>
36 
37 
38 namespace test {
39  // use functions in own test namespace to avoid stdio conflicts
40  #include "../printf.h"
41  #include "../printf.c"
42 } // namespace test
43 
44 
45 // dummy putchar
46 static char printf_buffer[100];
47 static size_t printf_idx = 0U;
48 
49 void test::_putchar(char character)
50 {
51  printf_buffer[printf_idx++] = character;
52 }
53 
54 void _out_fct(char character, void* arg)
55 {
56  (void)arg;
57  printf_buffer[printf_idx++] = character;
58 }
59 
60 
61 TEST_CASE("printf", "[]" ) {
62  printf_idx = 0U;
63  memset(printf_buffer, 0xCC, 100U);
64  REQUIRE(test::printf("% d", 4232) == 5);
65  REQUIRE(printf_buffer[5] == (char)0xCC);
66  printf_buffer[5] = 0;
67  REQUIRE(!strcmp(printf_buffer, " 4232"));
68 }
69 
70 
71 TEST_CASE("fctprintf", "[]" ) {
72  printf_idx = 0U;
73  memset(printf_buffer, 0xCC, 100U);
74  test::fctprintf(&_out_fct, nullptr, "This is a test of %X", 0x12EFU);
75  REQUIRE(!strncmp(printf_buffer, "This is a test of 12EF", 22U));
76  REQUIRE(printf_buffer[22] == (char)0xCC);
77 }
78 
79 
80 TEST_CASE("snprintf", "[]" ) {
81  char buffer[100];
82 
83  test::snprintf(buffer, 100U, "%d", -1000);
84  REQUIRE(!strcmp(buffer, "-1000"));
85 
86  test::snprintf(buffer, 3U, "%d", -1000);
87  REQUIRE(!strcmp(buffer, "-1"));
88 }
89 
90 static void vprintf_builder_1(char* buffer, ...)
91 {
92  va_list args;
93  va_start(args, buffer);
94  test::vprintf("%d", args);
95  va_end(args);
96 }
97 
98 static void vsnprintf_builder_1(char* buffer, ...)
99 {
100  va_list args;
101  va_start(args, buffer);
102  test::vsnprintf(buffer, 100U, "%d", args);
103  va_end(args);
104 }
106 static void vsnprintf_builder_3(char* buffer, ...)
107 {
108  va_list args;
109  va_start(args, buffer);
110  test::vsnprintf(buffer, 100U, "%d %d %s", args);
111  va_end(args);
112 }
113 
114 
115 TEST_CASE("vprintf", "[]" ) {
116  char buffer[100];
117  printf_idx = 0U;
118  memset(printf_buffer, 0xCC, 100U);
119  vprintf_builder_1(buffer, 2345);
120  REQUIRE(printf_buffer[4] == (char)0xCC);
121  printf_buffer[4] = 0;
122  REQUIRE(!strcmp(printf_buffer, "2345"));
123 }
124 
125 
126 TEST_CASE("vsnprintf", "[]" ) {
127  char buffer[100];
129  vsnprintf_builder_1(buffer, -1);
130  REQUIRE(!strcmp(buffer, "-1"));
131 
132  vsnprintf_builder_3(buffer, 3, -1000, "test");
133  REQUIRE(!strcmp(buffer, "3 -1000 test"));
134 }
135 
136 
137 TEST_CASE("space flag", "[]" ) {
138  char buffer[100];
139 
140  test::sprintf(buffer, "% d", 42);
141  REQUIRE(!strcmp(buffer, " 42"));
143  test::sprintf(buffer, "% d", -42);
144  REQUIRE(!strcmp(buffer, "-42"));
145 
146  test::sprintf(buffer, "% 5d", 42);
147  REQUIRE(!strcmp(buffer, " 42"));
148 
149  test::sprintf(buffer, "% 5d", -42);
150  REQUIRE(!strcmp(buffer, " -42"));
151 
152  test::sprintf(buffer, "% 15d", 42);
153  REQUIRE(!strcmp(buffer, " 42"));
154 
155  test::sprintf(buffer, "% 15d", -42);
156  REQUIRE(!strcmp(buffer, " -42"));
157 
158  test::sprintf(buffer, "% 15d", -42);
159  REQUIRE(!strcmp(buffer, " -42"));
160 
161  test::sprintf(buffer, "% 15.3f", -42.987);
162  REQUIRE(!strcmp(buffer, " -42.987"));
163 
164  test::sprintf(buffer, "% 15.3f", 42.987);
165  REQUIRE(!strcmp(buffer, " 42.987"));
166 
167  test::sprintf(buffer, "% s", "Hello testing");
168  REQUIRE(!strcmp(buffer, "Hello testing"));
169 
170  test::sprintf(buffer, "% d", 1024);
171  REQUIRE(!strcmp(buffer, " 1024"));
172 
173  test::sprintf(buffer, "% d", -1024);
174  REQUIRE(!strcmp(buffer, "-1024"));
175 
176  test::sprintf(buffer, "% i", 1024);
177  REQUIRE(!strcmp(buffer, " 1024"));
178 
179  test::sprintf(buffer, "% i", -1024);
180  REQUIRE(!strcmp(buffer, "-1024"));
182  test::sprintf(buffer, "% u", 1024);
183  REQUIRE(!strcmp(buffer, "1024"));
184 
185  test::sprintf(buffer, "% u", 4294966272U);
186  REQUIRE(!strcmp(buffer, "4294966272"));
187 
188  test::sprintf(buffer, "% o", 511);
189  REQUIRE(!strcmp(buffer, "777"));
190 
191  test::sprintf(buffer, "% o", 4294966785U);
192  REQUIRE(!strcmp(buffer, "37777777001"));
193 
194  test::sprintf(buffer, "% x", 305441741);
195  REQUIRE(!strcmp(buffer, "1234abcd"));
196 
197  test::sprintf(buffer, "% x", 3989525555U);
198  REQUIRE(!strcmp(buffer, "edcb5433"));
200  test::sprintf(buffer, "% X", 305441741);
201  REQUIRE(!strcmp(buffer, "1234ABCD"));
202 
203  test::sprintf(buffer, "% X", 3989525555U);
204  REQUIRE(!strcmp(buffer, "EDCB5433"));
205 
206  test::sprintf(buffer, "% c", 'x');
207  REQUIRE(!strcmp(buffer, "x"));
208 }
209 
210 
211 TEST_CASE("+ flag", "[]" ) {
212  char buffer[100];
213 
214  test::sprintf(buffer, "%+d", 42);
215  REQUIRE(!strcmp(buffer, "+42"));
216 
217  test::sprintf(buffer, "%+d", -42);
218  REQUIRE(!strcmp(buffer, "-42"));
219 
220  test::sprintf(buffer, "%+5d", 42);
221  REQUIRE(!strcmp(buffer, " +42"));
222 
223  test::sprintf(buffer, "%+5d", -42);
224  REQUIRE(!strcmp(buffer, " -42"));
225 
226  test::sprintf(buffer, "%+15d", 42);
227  REQUIRE(!strcmp(buffer, " +42"));
228 
229  test::sprintf(buffer, "%+15d", -42);
230  REQUIRE(!strcmp(buffer, " -42"));
231 
232  test::sprintf(buffer, "%+s", "Hello testing");
233  REQUIRE(!strcmp(buffer, "Hello testing"));
234 
235  test::sprintf(buffer, "%+d", 1024);
236  REQUIRE(!strcmp(buffer, "+1024"));
237 
238  test::sprintf(buffer, "%+d", -1024);
239  REQUIRE(!strcmp(buffer, "-1024"));
240 
241  test::sprintf(buffer, "%+i", 1024);
242  REQUIRE(!strcmp(buffer, "+1024"));
243 
244  test::sprintf(buffer, "%+i", -1024);
245  REQUIRE(!strcmp(buffer, "-1024"));
246 
247  test::sprintf(buffer, "%+u", 1024);
248  REQUIRE(!strcmp(buffer, "1024"));
249 
250  test::sprintf(buffer, "%+u", 4294966272U);
251  REQUIRE(!strcmp(buffer, "4294966272"));
252 
253  test::sprintf(buffer, "%+o", 511);
254  REQUIRE(!strcmp(buffer, "777"));
255 
256  test::sprintf(buffer, "%+o", 4294966785U);
257  REQUIRE(!strcmp(buffer, "37777777001"));
258 
259  test::sprintf(buffer, "%+x", 305441741);
260  REQUIRE(!strcmp(buffer, "1234abcd"));
261 
262  test::sprintf(buffer, "%+x", 3989525555U);
263  REQUIRE(!strcmp(buffer, "edcb5433"));
264 
265  test::sprintf(buffer, "%+X", 305441741);
266  REQUIRE(!strcmp(buffer, "1234ABCD"));
267 
268  test::sprintf(buffer, "%+X", 3989525555U);
269  REQUIRE(!strcmp(buffer, "EDCB5433"));
270 
271  test::sprintf(buffer, "%+c", 'x');
272  REQUIRE(!strcmp(buffer, "x"));
273 
274  test::sprintf(buffer, "%+.0d", 0);
275  REQUIRE(!strcmp(buffer, "+"));
276 }
277 
278 
279 TEST_CASE("0 flag", "[]" ) {
280  char buffer[100];
282  test::sprintf(buffer, "%0d", 42);
283  REQUIRE(!strcmp(buffer, "42"));
284 
285  test::sprintf(buffer, "%0ld", 42L);
286  REQUIRE(!strcmp(buffer, "42"));
287 
288  test::sprintf(buffer, "%0d", -42);
289  REQUIRE(!strcmp(buffer, "-42"));
290 
291  test::sprintf(buffer, "%05d", 42);
292  REQUIRE(!strcmp(buffer, "00042"));
293 
294  test::sprintf(buffer, "%05d", -42);
295  REQUIRE(!strcmp(buffer, "-0042"));
296 
297  test::sprintf(buffer, "%015d", 42);
298  REQUIRE(!strcmp(buffer, "000000000000042"));
299 
300  test::sprintf(buffer, "%015d", -42);
301  REQUIRE(!strcmp(buffer, "-00000000000042"));
302 
303  test::sprintf(buffer, "%015.2f", 42.1234);
304  REQUIRE(!strcmp(buffer, "000000000042.12"));
305 
306  test::sprintf(buffer, "%015.3f", 42.9876);
307  REQUIRE(!strcmp(buffer, "00000000042.988"));
308 
309  test::sprintf(buffer, "%015.5f", -42.9876);
310  REQUIRE(!strcmp(buffer, "-00000042.98760"));
311 }
312 
313 
314 TEST_CASE("- flag", "[]" ) {
315  char buffer[100];
316 
317  test::sprintf(buffer, "%-d", 42);
318  REQUIRE(!strcmp(buffer, "42"));
319 
320  test::sprintf(buffer, "%-d", -42);
321  REQUIRE(!strcmp(buffer, "-42"));
322 
323  test::sprintf(buffer, "%-5d", 42);
324  REQUIRE(!strcmp(buffer, "42 "));
325 
326  test::sprintf(buffer, "%-5d", -42);
327  REQUIRE(!strcmp(buffer, "-42 "));
328 
329  test::sprintf(buffer, "%-15d", 42);
330  REQUIRE(!strcmp(buffer, "42 "));
331 
332  test::sprintf(buffer, "%-15d", -42);
333  REQUIRE(!strcmp(buffer, "-42 "));
334 
335  test::sprintf(buffer, "%-0d", 42);
336  REQUIRE(!strcmp(buffer, "42"));
337 
338  test::sprintf(buffer, "%-0d", -42);
339  REQUIRE(!strcmp(buffer, "-42"));
340 
341  test::sprintf(buffer, "%-05d", 42);
342  REQUIRE(!strcmp(buffer, "42 "));
343 
344  test::sprintf(buffer, "%-05d", -42);
345  REQUIRE(!strcmp(buffer, "-42 "));
346 
347  test::sprintf(buffer, "%-015d", 42);
348  REQUIRE(!strcmp(buffer, "42 "));
349 
350  test::sprintf(buffer, "%-015d", -42);
351  REQUIRE(!strcmp(buffer, "-42 "));
352 
353  test::sprintf(buffer, "%0-d", 42);
354  REQUIRE(!strcmp(buffer, "42"));
355 
356  test::sprintf(buffer, "%0-d", -42);
357  REQUIRE(!strcmp(buffer, "-42"));
358 
359  test::sprintf(buffer, "%0-5d", 42);
360  REQUIRE(!strcmp(buffer, "42 "));
361 
362  test::sprintf(buffer, "%0-5d", -42);
363  REQUIRE(!strcmp(buffer, "-42 "));
364 
365  test::sprintf(buffer, "%0-15d", 42);
366  REQUIRE(!strcmp(buffer, "42 "));
367 
368  test::sprintf(buffer, "%0-15d", -42);
369  REQUIRE(!strcmp(buffer, "-42 "));
370 
371  test::sprintf(buffer, "%0-15.3e", -42.);
372 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
373  REQUIRE(!strcmp(buffer, "-4.200e+01 "));
374 #else
375  REQUIRE(!strcmp(buffer, "e"));
376 #endif
377 
378  test::sprintf(buffer, "%0-15.3g", -42.);
379 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
380  REQUIRE(!strcmp(buffer, "-42.0 "));
381 #else
382  REQUIRE(!strcmp(buffer, "g"));
383 #endif
384 }
385 
386 
387 TEST_CASE("# flag", "[]" ) {
388  char buffer[100];
389 
390  test::sprintf(buffer, "%#.0x", 0);
391  REQUIRE(!strcmp(buffer, ""));
392  test::sprintf(buffer, "%#.1x", 0);
393  REQUIRE(!strcmp(buffer, "0"));
394  test::sprintf(buffer, "%#.0llx", (long long)0);
395  REQUIRE(!strcmp(buffer, ""));
396  test::sprintf(buffer, "%#.8x", 0x614e);
397  REQUIRE(!strcmp(buffer, "0x0000614e"));
398  test::sprintf(buffer,"%#b", 6);
399  REQUIRE(!strcmp(buffer, "0b110"));
400 }
401 
402 
403 TEST_CASE("specifier", "[]" ) {
404  char buffer[100];
405 
406  test::sprintf(buffer, "Hello testing");
407  REQUIRE(!strcmp(buffer, "Hello testing"));
408 
409  test::sprintf(buffer, "%s", "Hello testing");
410  REQUIRE(!strcmp(buffer, "Hello testing"));
411 
412  test::sprintf(buffer, "%d", 1024);
413  REQUIRE(!strcmp(buffer, "1024"));
414 
415  test::sprintf(buffer, "%d", -1024);
416  REQUIRE(!strcmp(buffer, "-1024"));
417 
418  test::sprintf(buffer, "%i", 1024);
419  REQUIRE(!strcmp(buffer, "1024"));
420 
421  test::sprintf(buffer, "%i", -1024);
422  REQUIRE(!strcmp(buffer, "-1024"));
423 
424  test::sprintf(buffer, "%u", 1024);
425  REQUIRE(!strcmp(buffer, "1024"));
426 
427  test::sprintf(buffer, "%u", 4294966272U);
428  REQUIRE(!strcmp(buffer, "4294966272"));
429 
430  test::sprintf(buffer, "%o", 511);
431  REQUIRE(!strcmp(buffer, "777"));
432 
433  test::sprintf(buffer, "%o", 4294966785U);
434  REQUIRE(!strcmp(buffer, "37777777001"));
435 
436  test::sprintf(buffer, "%x", 305441741);
437  REQUIRE(!strcmp(buffer, "1234abcd"));
438 
439  test::sprintf(buffer, "%x", 3989525555U);
440  REQUIRE(!strcmp(buffer, "edcb5433"));
441 
442  test::sprintf(buffer, "%X", 305441741);
443  REQUIRE(!strcmp(buffer, "1234ABCD"));
444 
445  test::sprintf(buffer, "%X", 3989525555U);
446  REQUIRE(!strcmp(buffer, "EDCB5433"));
447 
448  test::sprintf(buffer, "%%");
449  REQUIRE(!strcmp(buffer, "%"));
450 }
451 
452 
453 TEST_CASE("width", "[]" ) {
454  char buffer[100];
455 
456  test::sprintf(buffer, "%1s", "Hello testing");
457  REQUIRE(!strcmp(buffer, "Hello testing"));
458 
459  test::sprintf(buffer, "%1d", 1024);
460  REQUIRE(!strcmp(buffer, "1024"));
461 
462  test::sprintf(buffer, "%1d", -1024);
463  REQUIRE(!strcmp(buffer, "-1024"));
464 
465  test::sprintf(buffer, "%1i", 1024);
466  REQUIRE(!strcmp(buffer, "1024"));
468  test::sprintf(buffer, "%1i", -1024);
469  REQUIRE(!strcmp(buffer, "-1024"));
470 
471  test::sprintf(buffer, "%1u", 1024);
472  REQUIRE(!strcmp(buffer, "1024"));
473 
474  test::sprintf(buffer, "%1u", 4294966272U);
475  REQUIRE(!strcmp(buffer, "4294966272"));
476 
477  test::sprintf(buffer, "%1o", 511);
478  REQUIRE(!strcmp(buffer, "777"));
479 
480  test::sprintf(buffer, "%1o", 4294966785U);
481  REQUIRE(!strcmp(buffer, "37777777001"));
482 
483  test::sprintf(buffer, "%1x", 305441741);
484  REQUIRE(!strcmp(buffer, "1234abcd"));
485 
486  test::sprintf(buffer, "%1x", 3989525555U);
487  REQUIRE(!strcmp(buffer, "edcb5433"));
488 
489  test::sprintf(buffer, "%1X", 305441741);
490  REQUIRE(!strcmp(buffer, "1234ABCD"));
491 
492  test::sprintf(buffer, "%1X", 3989525555U);
493  REQUIRE(!strcmp(buffer, "EDCB5433"));
494 
495  test::sprintf(buffer, "%1c", 'x');
496  REQUIRE(!strcmp(buffer, "x"));
497 }
498 
499 
500 TEST_CASE("width 20", "[]" ) {
501  char buffer[100];
502 
503  test::sprintf(buffer, "%20s", "Hello");
504  REQUIRE(!strcmp(buffer, " Hello"));
505 
506  test::sprintf(buffer, "%20d", 1024);
507  REQUIRE(!strcmp(buffer, " 1024"));
508 
509  test::sprintf(buffer, "%20d", -1024);
510  REQUIRE(!strcmp(buffer, " -1024"));
511 
512  test::sprintf(buffer, "%20i", 1024);
513  REQUIRE(!strcmp(buffer, " 1024"));
514 
515  test::sprintf(buffer, "%20i", -1024);
516  REQUIRE(!strcmp(buffer, " -1024"));
517 
518  test::sprintf(buffer, "%20u", 1024);
519  REQUIRE(!strcmp(buffer, " 1024"));
520 
521  test::sprintf(buffer, "%20u", 4294966272U);
522  REQUIRE(!strcmp(buffer, " 4294966272"));
523 
524  test::sprintf(buffer, "%20o", 511);
525  REQUIRE(!strcmp(buffer, " 777"));
526 
527  test::sprintf(buffer, "%20o", 4294966785U);
528  REQUIRE(!strcmp(buffer, " 37777777001"));
529 
530  test::sprintf(buffer, "%20x", 305441741);
531  REQUIRE(!strcmp(buffer, " 1234abcd"));
532 
533  test::sprintf(buffer, "%20x", 3989525555U);
534  REQUIRE(!strcmp(buffer, " edcb5433"));
535 
536  test::sprintf(buffer, "%20X", 305441741);
537  REQUIRE(!strcmp(buffer, " 1234ABCD"));
538 
539  test::sprintf(buffer, "%20X", 3989525555U);
540  REQUIRE(!strcmp(buffer, " EDCB5433"));
541 
542  test::sprintf(buffer, "%20c", 'x');
543  REQUIRE(!strcmp(buffer, " x"));
544 }
545 
546 
547 TEST_CASE("width *20", "[]" ) {
548  char buffer[100];
549 
550  test::sprintf(buffer, "%*s", 20, "Hello");
551  REQUIRE(!strcmp(buffer, " Hello"));
552 
553  test::sprintf(buffer, "%*d", 20, 1024);
554  REQUIRE(!strcmp(buffer, " 1024"));
555 
556  test::sprintf(buffer, "%*d", 20, -1024);
557  REQUIRE(!strcmp(buffer, " -1024"));
558 
559  test::sprintf(buffer, "%*i", 20, 1024);
560  REQUIRE(!strcmp(buffer, " 1024"));
561 
562  test::sprintf(buffer, "%*i", 20, -1024);
563  REQUIRE(!strcmp(buffer, " -1024"));
564 
565  test::sprintf(buffer, "%*u", 20, 1024);
566  REQUIRE(!strcmp(buffer, " 1024"));
567 
568  test::sprintf(buffer, "%*u", 20, 4294966272U);
569  REQUIRE(!strcmp(buffer, " 4294966272"));
570 
571  test::sprintf(buffer, "%*o", 20, 511);
572  REQUIRE(!strcmp(buffer, " 777"));
573 
574  test::sprintf(buffer, "%*o", 20, 4294966785U);
575  REQUIRE(!strcmp(buffer, " 37777777001"));
576 
577  test::sprintf(buffer, "%*x", 20, 305441741);
578  REQUIRE(!strcmp(buffer, " 1234abcd"));
579 
580  test::sprintf(buffer, "%*x", 20, 3989525555U);
581  REQUIRE(!strcmp(buffer, " edcb5433"));
582 
583  test::sprintf(buffer, "%*X", 20, 305441741);
584  REQUIRE(!strcmp(buffer, " 1234ABCD"));
585 
586  test::sprintf(buffer, "%*X", 20, 3989525555U);
587  REQUIRE(!strcmp(buffer, " EDCB5433"));
588 
589  test::sprintf(buffer, "%*c", 20,'x');
590  REQUIRE(!strcmp(buffer, " x"));
591 }
592 
593 
594 TEST_CASE("width -20", "[]" ) {
595  char buffer[100];
596 
597  test::sprintf(buffer, "%-20s", "Hello");
598  REQUIRE(!strcmp(buffer, "Hello "));
599 
600  test::sprintf(buffer, "%-20d", 1024);
601  REQUIRE(!strcmp(buffer, "1024 "));
602 
603  test::sprintf(buffer, "%-20d", -1024);
604  REQUIRE(!strcmp(buffer, "-1024 "));
605 
606  test::sprintf(buffer, "%-20i", 1024);
607  REQUIRE(!strcmp(buffer, "1024 "));
608 
609  test::sprintf(buffer, "%-20i", -1024);
610  REQUIRE(!strcmp(buffer, "-1024 "));
611 
612  test::sprintf(buffer, "%-20u", 1024);
613  REQUIRE(!strcmp(buffer, "1024 "));
614 
615  test::sprintf(buffer, "%-20.4f", 1024.1234);
616  REQUIRE(!strcmp(buffer, "1024.1234 "));
617 
618  test::sprintf(buffer, "%-20u", 4294966272U);
619  REQUIRE(!strcmp(buffer, "4294966272 "));
620 
621  test::sprintf(buffer, "%-20o", 511);
622  REQUIRE(!strcmp(buffer, "777 "));
623 
624  test::sprintf(buffer, "%-20o", 4294966785U);
625  REQUIRE(!strcmp(buffer, "37777777001 "));
626 
627  test::sprintf(buffer, "%-20x", 305441741);
628  REQUIRE(!strcmp(buffer, "1234abcd "));
629 
630  test::sprintf(buffer, "%-20x", 3989525555U);
631  REQUIRE(!strcmp(buffer, "edcb5433 "));
632 
633  test::sprintf(buffer, "%-20X", 305441741);
634  REQUIRE(!strcmp(buffer, "1234ABCD "));
635 
636  test::sprintf(buffer, "%-20X", 3989525555U);
637  REQUIRE(!strcmp(buffer, "EDCB5433 "));
638 
639  test::sprintf(buffer, "%-20c", 'x');
640  REQUIRE(!strcmp(buffer, "x "));
641 
642  test::sprintf(buffer, "|%5d| |%-2d| |%5d|", 9, 9, 9);
643  REQUIRE(!strcmp(buffer, "| 9| |9 | | 9|"));
644 
645  test::sprintf(buffer, "|%5d| |%-2d| |%5d|", 10, 10, 10);
646  REQUIRE(!strcmp(buffer, "| 10| |10| | 10|"));
647 
648  test::sprintf(buffer, "|%5d| |%-12d| |%5d|", 9, 9, 9);
649  REQUIRE(!strcmp(buffer, "| 9| |9 | | 9|"));
650 
651  test::sprintf(buffer, "|%5d| |%-12d| |%5d|", 10, 10, 10);
652  REQUIRE(!strcmp(buffer, "| 10| |10 | | 10|"));
653 }
654 
655 
656 TEST_CASE("width 0-20", "[]" ) {
657  char buffer[100];
658 
659  test::sprintf(buffer, "%0-20s", "Hello");
660  REQUIRE(!strcmp(buffer, "Hello "));
661 
662  test::sprintf(buffer, "%0-20d", 1024);
663  REQUIRE(!strcmp(buffer, "1024 "));
664 
665  test::sprintf(buffer, "%0-20d", -1024);
666  REQUIRE(!strcmp(buffer, "-1024 "));
667 
668  test::sprintf(buffer, "%0-20i", 1024);
669  REQUIRE(!strcmp(buffer, "1024 "));
670 
671  test::sprintf(buffer, "%0-20i", -1024);
672  REQUIRE(!strcmp(buffer, "-1024 "));
673 
674  test::sprintf(buffer, "%0-20u", 1024);
675  REQUIRE(!strcmp(buffer, "1024 "));
676 
677  test::sprintf(buffer, "%0-20u", 4294966272U);
678  REQUIRE(!strcmp(buffer, "4294966272 "));
679 
680  test::sprintf(buffer, "%0-20o", 511);
681  REQUIRE(!strcmp(buffer, "777 "));
682 
683  test::sprintf(buffer, "%0-20o", 4294966785U);
684  REQUIRE(!strcmp(buffer, "37777777001 "));
685 
686  test::sprintf(buffer, "%0-20x", 305441741);
687  REQUIRE(!strcmp(buffer, "1234abcd "));
688 
689  test::sprintf(buffer, "%0-20x", 3989525555U);
690  REQUIRE(!strcmp(buffer, "edcb5433 "));
691 
692  test::sprintf(buffer, "%0-20X", 305441741);
693  REQUIRE(!strcmp(buffer, "1234ABCD "));
694 
695  test::sprintf(buffer, "%0-20X", 3989525555U);
696  REQUIRE(!strcmp(buffer, "EDCB5433 "));
697 
698  test::sprintf(buffer, "%0-20c", 'x');
699  REQUIRE(!strcmp(buffer, "x "));
700 }
701 
702 
703 TEST_CASE("padding 20", "[]" ) {
704  char buffer[100];
705 
706  test::sprintf(buffer, "%020d", 1024);
707  REQUIRE(!strcmp(buffer, "00000000000000001024"));
708 
709  test::sprintf(buffer, "%020d", -1024);
710  REQUIRE(!strcmp(buffer, "-0000000000000001024"));
711 
712  test::sprintf(buffer, "%020i", 1024);
713  REQUIRE(!strcmp(buffer, "00000000000000001024"));
714 
715  test::sprintf(buffer, "%020i", -1024);
716  REQUIRE(!strcmp(buffer, "-0000000000000001024"));
717 
718  test::sprintf(buffer, "%020u", 1024);
719  REQUIRE(!strcmp(buffer, "00000000000000001024"));
720 
721  test::sprintf(buffer, "%020u", 4294966272U);
722  REQUIRE(!strcmp(buffer, "00000000004294966272"));
723 
724  test::sprintf(buffer, "%020o", 511);
725  REQUIRE(!strcmp(buffer, "00000000000000000777"));
726 
727  test::sprintf(buffer, "%020o", 4294966785U);
728  REQUIRE(!strcmp(buffer, "00000000037777777001"));
729 
730  test::sprintf(buffer, "%020x", 305441741);
731  REQUIRE(!strcmp(buffer, "0000000000001234abcd"));
732 
733  test::sprintf(buffer, "%020x", 3989525555U);
734  REQUIRE(!strcmp(buffer, "000000000000edcb5433"));
735 
736  test::sprintf(buffer, "%020X", 305441741);
737  REQUIRE(!strcmp(buffer, "0000000000001234ABCD"));
738 
739  test::sprintf(buffer, "%020X", 3989525555U);
740  REQUIRE(!strcmp(buffer, "000000000000EDCB5433"));
741 }
742 
743 
744 TEST_CASE("padding .20", "[]" ) {
745  char buffer[100];
746 
747  test::sprintf(buffer, "%.20d", 1024);
748  REQUIRE(!strcmp(buffer, "00000000000000001024"));
749 
750  test::sprintf(buffer, "%.20d", -1024);
751  REQUIRE(!strcmp(buffer, "-00000000000000001024"));
752 
753  test::sprintf(buffer, "%.20i", 1024);
754  REQUIRE(!strcmp(buffer, "00000000000000001024"));
755 
756  test::sprintf(buffer, "%.20i", -1024);
757  REQUIRE(!strcmp(buffer, "-00000000000000001024"));
758 
759  test::sprintf(buffer, "%.20u", 1024);
760  REQUIRE(!strcmp(buffer, "00000000000000001024"));
761 
762  test::sprintf(buffer, "%.20u", 4294966272U);
763  REQUIRE(!strcmp(buffer, "00000000004294966272"));
764 
765  test::sprintf(buffer, "%.20o", 511);
766  REQUIRE(!strcmp(buffer, "00000000000000000777"));
767 
768  test::sprintf(buffer, "%.20o", 4294966785U);
769  REQUIRE(!strcmp(buffer, "00000000037777777001"));
770 
771  test::sprintf(buffer, "%.20x", 305441741);
772  REQUIRE(!strcmp(buffer, "0000000000001234abcd"));
773 
774  test::sprintf(buffer, "%.20x", 3989525555U);
775  REQUIRE(!strcmp(buffer, "000000000000edcb5433"));
776 
777  test::sprintf(buffer, "%.20X", 305441741);
778  REQUIRE(!strcmp(buffer, "0000000000001234ABCD"));
779 
780  test::sprintf(buffer, "%.20X", 3989525555U);
781  REQUIRE(!strcmp(buffer, "000000000000EDCB5433"));
782 }
783 
784 
785 TEST_CASE("padding #020", "[]" ) {
786  char buffer[100];
787 
788  test::sprintf(buffer, "%#020d", 1024);
789  REQUIRE(!strcmp(buffer, "00000000000000001024"));
790 
791  test::sprintf(buffer, "%#020d", -1024);
792  REQUIRE(!strcmp(buffer, "-0000000000000001024"));
793 
794  test::sprintf(buffer, "%#020i", 1024);
795  REQUIRE(!strcmp(buffer, "00000000000000001024"));
796 
797  test::sprintf(buffer, "%#020i", -1024);
798  REQUIRE(!strcmp(buffer, "-0000000000000001024"));
799 
800  test::sprintf(buffer, "%#020u", 1024);
801  REQUIRE(!strcmp(buffer, "00000000000000001024"));
802 
803  test::sprintf(buffer, "%#020u", 4294966272U);
804  REQUIRE(!strcmp(buffer, "00000000004294966272"));
805 
806  test::sprintf(buffer, "%#020o", 511);
807  REQUIRE(!strcmp(buffer, "00000000000000000777"));
808 
809  test::sprintf(buffer, "%#020o", 4294966785U);
810  REQUIRE(!strcmp(buffer, "00000000037777777001"));
811 
812  test::sprintf(buffer, "%#020x", 305441741);
813  REQUIRE(!strcmp(buffer, "0x00000000001234abcd"));
814 
815  test::sprintf(buffer, "%#020x", 3989525555U);
816  REQUIRE(!strcmp(buffer, "0x0000000000edcb5433"));
817 
818  test::sprintf(buffer, "%#020X", 305441741);
819  REQUIRE(!strcmp(buffer, "0X00000000001234ABCD"));
820 
821  test::sprintf(buffer, "%#020X", 3989525555U);
822  REQUIRE(!strcmp(buffer, "0X0000000000EDCB5433"));
823 }
824 
825 
826 TEST_CASE("padding #20", "[]" ) {
827  char buffer[100];
828 
829  test::sprintf(buffer, "%#20d", 1024);
830  REQUIRE(!strcmp(buffer, " 1024"));
831 
832  test::sprintf(buffer, "%#20d", -1024);
833  REQUIRE(!strcmp(buffer, " -1024"));
834 
835  test::sprintf(buffer, "%#20i", 1024);
836  REQUIRE(!strcmp(buffer, " 1024"));
837 
838  test::sprintf(buffer, "%#20i", -1024);
839  REQUIRE(!strcmp(buffer, " -1024"));
840 
841  test::sprintf(buffer, "%#20u", 1024);
842  REQUIRE(!strcmp(buffer, " 1024"));
843 
844  test::sprintf(buffer, "%#20u", 4294966272U);
845  REQUIRE(!strcmp(buffer, " 4294966272"));
846 
847  test::sprintf(buffer, "%#20o", 511);
848  REQUIRE(!strcmp(buffer, " 0777"));
849 
850  test::sprintf(buffer, "%#20o", 4294966785U);
851  REQUIRE(!strcmp(buffer, " 037777777001"));
852 
853  test::sprintf(buffer, "%#20x", 305441741);
854  REQUIRE(!strcmp(buffer, " 0x1234abcd"));
855 
856  test::sprintf(buffer, "%#20x", 3989525555U);
857  REQUIRE(!strcmp(buffer, " 0xedcb5433"));
858 
859  test::sprintf(buffer, "%#20X", 305441741);
860  REQUIRE(!strcmp(buffer, " 0X1234ABCD"));
861 
862  test::sprintf(buffer, "%#20X", 3989525555U);
863  REQUIRE(!strcmp(buffer, " 0XEDCB5433"));
864 }
865 
866 
867 TEST_CASE("padding 20.5", "[]" ) {
868  char buffer[100];
869 
870  test::sprintf(buffer, "%20.5d", 1024);
871  REQUIRE(!strcmp(buffer, " 01024"));
872 
873  test::sprintf(buffer, "%20.5d", -1024);
874  REQUIRE(!strcmp(buffer, " -01024"));
875 
876  test::sprintf(buffer, "%20.5i", 1024);
877  REQUIRE(!strcmp(buffer, " 01024"));
878 
879  test::sprintf(buffer, "%20.5i", -1024);
880  REQUIRE(!strcmp(buffer, " -01024"));
881 
882  test::sprintf(buffer, "%20.5u", 1024);
883  REQUIRE(!strcmp(buffer, " 01024"));
884 
885  test::sprintf(buffer, "%20.5u", 4294966272U);
886  REQUIRE(!strcmp(buffer, " 4294966272"));
887 
888  test::sprintf(buffer, "%20.5o", 511);
889  REQUIRE(!strcmp(buffer, " 00777"));
890 
891  test::sprintf(buffer, "%20.5o", 4294966785U);
892  REQUIRE(!strcmp(buffer, " 37777777001"));
894  test::sprintf(buffer, "%20.5x", 305441741);
895  REQUIRE(!strcmp(buffer, " 1234abcd"));
896 
897  test::sprintf(buffer, "%20.10x", 3989525555U);
898  REQUIRE(!strcmp(buffer, " 00edcb5433"));
899 
900  test::sprintf(buffer, "%20.5X", 305441741);
901  REQUIRE(!strcmp(buffer, " 1234ABCD"));
902 
903  test::sprintf(buffer, "%20.10X", 3989525555U);
904  REQUIRE(!strcmp(buffer, " 00EDCB5433"));
905 }
907 
908 TEST_CASE("padding neg numbers", "[]" ) {
909  char buffer[100];
910 
911  // space padding
912  test::sprintf(buffer, "% 1d", -5);
913  REQUIRE(!strcmp(buffer, "-5"));
914 
915  test::sprintf(buffer, "% 2d", -5);
916  REQUIRE(!strcmp(buffer, "-5"));
917 
918  test::sprintf(buffer, "% 3d", -5);
919  REQUIRE(!strcmp(buffer, " -5"));
920 
921  test::sprintf(buffer, "% 4d", -5);
922  REQUIRE(!strcmp(buffer, " -5"));
923 
924  // zero padding
925  test::sprintf(buffer, "%01d", -5);
926  REQUIRE(!strcmp(buffer, "-5"));
927 
928  test::sprintf(buffer, "%02d", -5);
929  REQUIRE(!strcmp(buffer, "-5"));
930 
931  test::sprintf(buffer, "%03d", -5);
932  REQUIRE(!strcmp(buffer, "-05"));
933 
934  test::sprintf(buffer, "%04d", -5);
935  REQUIRE(!strcmp(buffer, "-005"));
936 }
937 
938 
939 TEST_CASE("float padding neg numbers", "[]" ) {
940  char buffer[100];
941 
942  // space padding
943  test::sprintf(buffer, "% 3.1f", -5.);
944  REQUIRE(!strcmp(buffer, "-5.0"));
945 
946  test::sprintf(buffer, "% 4.1f", -5.);
947  REQUIRE(!strcmp(buffer, "-5.0"));
948 
949  test::sprintf(buffer, "% 5.1f", -5.);
950  REQUIRE(!strcmp(buffer, " -5.0"));
951 
952 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
953  test::sprintf(buffer, "% 6.1g", -5.);
954  REQUIRE(!strcmp(buffer, " -5"));
955 
956  test::sprintf(buffer, "% 6.1e", -5.);
957  REQUIRE(!strcmp(buffer, "-5.0e+00"));
958 
959  test::sprintf(buffer, "% 10.1e", -5.);
960  REQUIRE(!strcmp(buffer, " -5.0e+00"));
961 #endif
962 
963  // zero padding
964  test::sprintf(buffer, "%03.1f", -5.);
965  REQUIRE(!strcmp(buffer, "-5.0"));
966 
967  test::sprintf(buffer, "%04.1f", -5.);
968  REQUIRE(!strcmp(buffer, "-5.0"));
969 
970  test::sprintf(buffer, "%05.1f", -5.);
971  REQUIRE(!strcmp(buffer, "-05.0"));
972 
973  // zero padding no decimal point
974  test::sprintf(buffer, "%01.0f", -5.);
975  REQUIRE(!strcmp(buffer, "-5"));
976 
977  test::sprintf(buffer, "%02.0f", -5.);
978  REQUIRE(!strcmp(buffer, "-5"));
979 
980  test::sprintf(buffer, "%03.0f", -5.);
981  REQUIRE(!strcmp(buffer, "-05"));
982 
983 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
984  test::sprintf(buffer, "%010.1e", -5.);
985  REQUIRE(!strcmp(buffer, "-005.0e+00"));
986 
987  test::sprintf(buffer, "%07.0E", -5.);
988  REQUIRE(!strcmp(buffer, "-05E+00"));
989 
990  test::sprintf(buffer, "%03.0g", -5.);
991  REQUIRE(!strcmp(buffer, "-05"));
992 #endif
993 }
994 
995 TEST_CASE("length", "[]" ) {
996  char buffer[100];
997 
998  test::sprintf(buffer, "%.0s", "Hello testing");
999  REQUIRE(!strcmp(buffer, ""));
1000 
1001  test::sprintf(buffer, "%20.0s", "Hello testing");
1002  REQUIRE(!strcmp(buffer, " "));
1003 
1004  test::sprintf(buffer, "%.s", "Hello testing");
1005  REQUIRE(!strcmp(buffer, ""));
1006 
1007  test::sprintf(buffer, "%20.s", "Hello testing");
1008  REQUIRE(!strcmp(buffer, " "));
1009 
1010  test::sprintf(buffer, "%20.0d", 1024);
1011  REQUIRE(!strcmp(buffer, " 1024"));
1012 
1013  test::sprintf(buffer, "%20.0d", -1024);
1014  REQUIRE(!strcmp(buffer, " -1024"));
1015 
1016  test::sprintf(buffer, "%20.d", 0);
1017  REQUIRE(!strcmp(buffer, " "));
1018 
1019  test::sprintf(buffer, "%20.0i", 1024);
1020  REQUIRE(!strcmp(buffer, " 1024"));
1021 
1022  test::sprintf(buffer, "%20.i", -1024);
1023  REQUIRE(!strcmp(buffer, " -1024"));
1024 
1025  test::sprintf(buffer, "%20.i", 0);
1026  REQUIRE(!strcmp(buffer, " "));
1027 
1028  test::sprintf(buffer, "%20.u", 1024);
1029  REQUIRE(!strcmp(buffer, " 1024"));
1030 
1031  test::sprintf(buffer, "%20.0u", 4294966272U);
1032  REQUIRE(!strcmp(buffer, " 4294966272"));
1033 
1034  test::sprintf(buffer, "%20.u", 0U);
1035  REQUIRE(!strcmp(buffer, " "));
1036 
1037  test::sprintf(buffer, "%20.o", 511);
1038  REQUIRE(!strcmp(buffer, " 777"));
1039 
1040  test::sprintf(buffer, "%20.0o", 4294966785U);
1041  REQUIRE(!strcmp(buffer, " 37777777001"));
1042 
1043  test::sprintf(buffer, "%20.o", 0U);
1044  REQUIRE(!strcmp(buffer, " "));
1045 
1046  test::sprintf(buffer, "%20.x", 305441741);
1047  REQUIRE(!strcmp(buffer, " 1234abcd"));
1048 
1049  test::sprintf(buffer, "%50.x", 305441741);
1050  REQUIRE(!strcmp(buffer, " 1234abcd"));
1051 
1052  test::sprintf(buffer, "%50.x%10.u", 305441741, 12345);
1053  REQUIRE(!strcmp(buffer, " 1234abcd 12345"));
1054 
1055  test::sprintf(buffer, "%20.0x", 3989525555U);
1056  REQUIRE(!strcmp(buffer, " edcb5433"));
1057 
1058  test::sprintf(buffer, "%20.x", 0U);
1059  REQUIRE(!strcmp(buffer, " "));
1060 
1061  test::sprintf(buffer, "%20.X", 305441741);
1062  REQUIRE(!strcmp(buffer, " 1234ABCD"));
1063 
1064  test::sprintf(buffer, "%20.0X", 3989525555U);
1065  REQUIRE(!strcmp(buffer, " EDCB5433"));
1066 
1067  test::sprintf(buffer, "%20.X", 0U);
1068  REQUIRE(!strcmp(buffer, " "));
1069 
1070  test::sprintf(buffer, "%02.0u", 0U);
1071  REQUIRE(!strcmp(buffer, " "));
1072 
1073  test::sprintf(buffer, "%02.0d", 0);
1074  REQUIRE(!strcmp(buffer, " "));
1075 }
1076 
1077 
1078 TEST_CASE("float", "[]" ) {
1079  char buffer[100];
1080 
1081  // test special-case floats using math.h macros
1082  test::sprintf(buffer, "%8f", NAN);
1083  REQUIRE(!strcmp(buffer, " nan"));
1084 
1085  test::sprintf(buffer, "%8f", INFINITY);
1086  REQUIRE(!strcmp(buffer, " inf"));
1087 
1088  test::sprintf(buffer, "%-8f", -INFINITY);
1089  REQUIRE(!strcmp(buffer, "-inf "));
1090 
1091 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
1092  test::sprintf(buffer, "%+8e", INFINITY);
1093  REQUIRE(!strcmp(buffer, " +inf"));
1094 #endif
1095 
1096  test::sprintf(buffer, "%.4f", 3.1415354);
1097  REQUIRE(!strcmp(buffer, "3.1415"));
1098 
1099  test::sprintf(buffer, "%.3f", 30343.1415354);
1100  REQUIRE(!strcmp(buffer, "30343.142"));
1101 
1102  test::sprintf(buffer, "%.0f", 34.1415354);
1103  REQUIRE(!strcmp(buffer, "34"));
1104 
1105  test::sprintf(buffer, "%.0f", 1.3);
1106  REQUIRE(!strcmp(buffer, "1"));
1107 
1108  test::sprintf(buffer, "%.0f", 1.55);
1109  REQUIRE(!strcmp(buffer, "2"));
1110 
1111  test::sprintf(buffer, "%.1f", 1.64);
1112  REQUIRE(!strcmp(buffer, "1.6"));
1113 
1114  test::sprintf(buffer, "%.2f", 42.8952);
1115  REQUIRE(!strcmp(buffer, "42.90"));
1116 
1117  test::sprintf(buffer, "%.9f", 42.8952);
1118  REQUIRE(!strcmp(buffer, "42.895200000"));
1119 
1120  test::sprintf(buffer, "%.10f", 42.895223);
1121  REQUIRE(!strcmp(buffer, "42.8952230000"));
1122 
1123  // this testcase checks, that the precision is truncated to 9 digits.
1124  // a perfect working float should return the whole number
1125  test::sprintf(buffer, "%.12f", 42.89522312345678);
1126  REQUIRE(!strcmp(buffer, "42.895223123000"));
1127 
1128  // this testcase checks, that the precision is truncated AND rounded to 9 digits.
1129  // a perfect working float should return the whole number
1130  test::sprintf(buffer, "%.12f", 42.89522387654321);
1131  REQUIRE(!strcmp(buffer, "42.895223877000"));
1132 
1133  test::sprintf(buffer, "%6.2f", 42.8952);
1134  REQUIRE(!strcmp(buffer, " 42.90"));
1135 
1136  test::sprintf(buffer, "%+6.2f", 42.8952);
1137  REQUIRE(!strcmp(buffer, "+42.90"));
1138 
1139  test::sprintf(buffer, "%+5.1f", 42.9252);
1140  REQUIRE(!strcmp(buffer, "+42.9"));
1141 
1142  test::sprintf(buffer, "%f", 42.5);
1143  REQUIRE(!strcmp(buffer, "42.500000"));
1144 
1145  test::sprintf(buffer, "%.1f", 42.5);
1146  REQUIRE(!strcmp(buffer, "42.5"));
1147 
1148  test::sprintf(buffer, "%f", 42167.0);
1149  REQUIRE(!strcmp(buffer, "42167.000000"));
1150 
1151  test::sprintf(buffer, "%.9f", -12345.987654321);
1152  REQUIRE(!strcmp(buffer, "-12345.987654321"));
1153 
1154  test::sprintf(buffer, "%.1f", 3.999);
1155  REQUIRE(!strcmp(buffer, "4.0"));
1156 
1157  test::sprintf(buffer, "%.0f", 3.5);
1158  REQUIRE(!strcmp(buffer, "4"));
1159 
1160  test::sprintf(buffer, "%.0f", 4.5);
1161  REQUIRE(!strcmp(buffer, "4"));
1162 
1163  test::sprintf(buffer, "%.0f", 3.49);
1164  REQUIRE(!strcmp(buffer, "3"));
1165 
1166  test::sprintf(buffer, "%.1f", 3.49);
1167  REQUIRE(!strcmp(buffer, "3.5"));
1168 
1169  test::sprintf(buffer, "a%-5.1f", 0.5);
1170  REQUIRE(!strcmp(buffer, "a0.5 "));
1171 
1172  test::sprintf(buffer, "a%-5.1fend", 0.5);
1173  REQUIRE(!strcmp(buffer, "a0.5 end"));
1174 
1175 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
1176  test::sprintf(buffer, "%G", 12345.678);
1177  REQUIRE(!strcmp(buffer, "12345.7"));
1178 
1179  test::sprintf(buffer, "%.7G", 12345.678);
1180  REQUIRE(!strcmp(buffer, "12345.68"));
1181 
1182  test::sprintf(buffer, "%.5G", 123456789.);
1183  REQUIRE(!strcmp(buffer, "1.2346E+08"));
1184 
1185  test::sprintf(buffer, "%.6G", 12345.);
1186  REQUIRE(!strcmp(buffer, "12345.0"));
1187 
1188  test::sprintf(buffer, "%+12.4g", 123456789.);
1189  REQUIRE(!strcmp(buffer, " +1.235e+08"));
1190 
1191  test::sprintf(buffer, "%.2G", 0.001234);
1192  REQUIRE(!strcmp(buffer, "0.0012"));
1193 
1194  test::sprintf(buffer, "%+10.4G", 0.001234);
1195  REQUIRE(!strcmp(buffer, " +0.001234"));
1196 
1197  test::sprintf(buffer, "%+012.4g", 0.00001234);
1198  REQUIRE(!strcmp(buffer, "+001.234e-05"));
1199 
1200  test::sprintf(buffer, "%.3g", -1.2345e-308);
1201  REQUIRE(!strcmp(buffer, "-1.23e-308"));
1202 
1203  test::sprintf(buffer, "%+.3E", 1.23e+308);
1204  REQUIRE(!strcmp(buffer, "+1.230E+308"));
1205 #endif
1206 
1207  // out of range for float: should switch to exp notation if supported, else empty
1208  test::sprintf(buffer, "%.1f", 1E20);
1209 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
1210  REQUIRE(!strcmp(buffer, "1.0e+20"));
1211 #else
1212  REQUIRE(!strcmp(buffer, ""));
1213 #endif
1214 
1215  // brute force float
1216  bool fail = false;
1217  std::stringstream str;
1218  str.precision(5);
1219  for (float i = -100000; i < 100000; i += 1) {
1220  test::sprintf(buffer, "%.5f", i / 10000);
1221  str.str("");
1222  str << std::fixed << i / 10000;
1223  fail = fail || !!strcmp(buffer, str.str().c_str());
1224  }
1225  REQUIRE(!fail);
1226 
1227 
1228 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
1229  // brute force exp
1230  str.setf(std::ios::scientific, std::ios::floatfield);
1231  for (float i = -1e20; i < 1e20; i += 1e15) {
1232  test::sprintf(buffer, "%.5f", i);
1233  str.str("");
1234  str << i;
1235  fail = fail || !!strcmp(buffer, str.str().c_str());
1236  }
1237  REQUIRE(!fail);
1238 #endif
1239 }
1240 
1241 
1242 TEST_CASE("types", "[]" ) {
1243  char buffer[100];
1244 
1245  test::sprintf(buffer, "%i", 0);
1246  REQUIRE(!strcmp(buffer, "0"));
1247 
1248  test::sprintf(buffer, "%i", 1234);
1249  REQUIRE(!strcmp(buffer, "1234"));
1250 
1251  test::sprintf(buffer, "%i", 32767);
1252  REQUIRE(!strcmp(buffer, "32767"));
1253 
1254  test::sprintf(buffer, "%i", -32767);
1255  REQUIRE(!strcmp(buffer, "-32767"));
1256 
1257  test::sprintf(buffer, "%li", 30L);
1258  REQUIRE(!strcmp(buffer, "30"));
1259 
1260  test::sprintf(buffer, "%li", -2147483647L);
1261  REQUIRE(!strcmp(buffer, "-2147483647"));
1262 
1263  test::sprintf(buffer, "%li", 2147483647L);
1264  REQUIRE(!strcmp(buffer, "2147483647"));
1265 
1266  test::sprintf(buffer, "%lli", 30LL);
1267  REQUIRE(!strcmp(buffer, "30"));
1268 
1269  test::sprintf(buffer, "%lli", -9223372036854775807LL);
1270  REQUIRE(!strcmp(buffer, "-9223372036854775807"));
1271 
1272  test::sprintf(buffer, "%lli", 9223372036854775807LL);
1273  REQUIRE(!strcmp(buffer, "9223372036854775807"));
1274 
1275  test::sprintf(buffer, "%lu", 100000L);
1276  REQUIRE(!strcmp(buffer, "100000"));
1277 
1278  test::sprintf(buffer, "%lu", 0xFFFFFFFFL);
1279  REQUIRE(!strcmp(buffer, "4294967295"));
1280 
1281  test::sprintf(buffer, "%llu", 281474976710656LLU);
1282  REQUIRE(!strcmp(buffer, "281474976710656"));
1283 
1284  test::sprintf(buffer, "%llu", 18446744073709551615LLU);
1285  REQUIRE(!strcmp(buffer, "18446744073709551615"));
1286 
1287  test::sprintf(buffer, "%zu", 2147483647UL);
1288  REQUIRE(!strcmp(buffer, "2147483647"));
1289 
1290  test::sprintf(buffer, "%zd", 2147483647UL);
1291  REQUIRE(!strcmp(buffer, "2147483647"));
1292 
1293  if (sizeof(size_t) == sizeof(long)) {
1294  test::sprintf(buffer, "%zi", -2147483647L);
1295  REQUIRE(!strcmp(buffer, "-2147483647"));
1296  }
1297  else {
1298  test::sprintf(buffer, "%zi", -2147483647LL);
1299  REQUIRE(!strcmp(buffer, "-2147483647"));
1300  }
1301 
1302  test::sprintf(buffer, "%b", 60000);
1303  REQUIRE(!strcmp(buffer, "1110101001100000"));
1304 
1305  test::sprintf(buffer, "%lb", 12345678L);
1306  REQUIRE(!strcmp(buffer, "101111000110000101001110"));
1307 
1308  test::sprintf(buffer, "%o", 60000);
1309  REQUIRE(!strcmp(buffer, "165140"));
1310 
1311  test::sprintf(buffer, "%lo", 12345678L);
1312  REQUIRE(!strcmp(buffer, "57060516"));
1313 
1314  test::sprintf(buffer, "%lx", 0x12345678L);
1315  REQUIRE(!strcmp(buffer, "12345678"));
1316 
1317  test::sprintf(buffer, "%llx", 0x1234567891234567LLU);
1318  REQUIRE(!strcmp(buffer, "1234567891234567"));
1319 
1320  test::sprintf(buffer, "%lx", 0xabcdefabL);
1321  REQUIRE(!strcmp(buffer, "abcdefab"));
1322 
1323  test::sprintf(buffer, "%lX", 0xabcdefabL);
1324  REQUIRE(!strcmp(buffer, "ABCDEFAB"));
1325 
1326  test::sprintf(buffer, "%c", 'v');
1327  REQUIRE(!strcmp(buffer, "v"));
1328 
1329  test::sprintf(buffer, "%cv", 'w');
1330  REQUIRE(!strcmp(buffer, "wv"));
1331 
1332  test::sprintf(buffer, "%s", "A Test");
1333  REQUIRE(!strcmp(buffer, "A Test"));
1334 
1335  test::sprintf(buffer, "%hhu", 0xFFFFUL);
1336  REQUIRE(!strcmp(buffer, "255"));
1337 
1338  test::sprintf(buffer, "%hu", 0x123456UL);
1339  REQUIRE(!strcmp(buffer, "13398"));
1340 
1341  test::sprintf(buffer, "%s%hhi %hu", "Test", 10000, 0xFFFFFFFF);
1342  REQUIRE(!strcmp(buffer, "Test16 65535"));
1343 
1344  test::sprintf(buffer, "%tx", &buffer[10] - &buffer[0]);
1345  REQUIRE(!strcmp(buffer, "a"));
1346 
1347 // TBD
1348  if (sizeof(intmax_t) == sizeof(long)) {
1349  test::sprintf(buffer, "%ji", -2147483647L);
1350  REQUIRE(!strcmp(buffer, "-2147483647"));
1351  }
1352  else {
1353  test::sprintf(buffer, "%ji", -2147483647LL);
1354  REQUIRE(!strcmp(buffer, "-2147483647"));
1355  }
1356 }
1357 
1358 
1359 TEST_CASE("pointer", "[]" ) {
1360  char buffer[100];
1361 
1362  test::sprintf(buffer, "%p", (void*)0x1234U);
1363  if (sizeof(void*) == 4U) {
1364  REQUIRE(!strcmp(buffer, "00001234"));
1365  }
1366  else {
1367  REQUIRE(!strcmp(buffer, "0000000000001234"));
1368  }
1369 
1370  test::sprintf(buffer, "%p", (void*)0x12345678U);
1371  if (sizeof(void*) == 4U) {
1372  REQUIRE(!strcmp(buffer, "12345678"));
1373  }
1374  else {
1375  REQUIRE(!strcmp(buffer, "0000000012345678"));
1376  }
1377 
1378  test::sprintf(buffer, "%p-%p", (void*)0x12345678U, (void*)0x7EDCBA98U);
1379  if (sizeof(void*) == 4U) {
1380  REQUIRE(!strcmp(buffer, "12345678-7EDCBA98"));
1381  }
1382  else {
1383  REQUIRE(!strcmp(buffer, "0000000012345678-000000007EDCBA98"));
1384  }
1385 
1386  if (sizeof(uintptr_t) == sizeof(uint64_t)) {
1387  test::sprintf(buffer, "%p", (void*)(uintptr_t)0xFFFFFFFFU);
1388  REQUIRE(!strcmp(buffer, "00000000FFFFFFFF"));
1389  }
1390  else {
1391  test::sprintf(buffer, "%p", (void*)(uintptr_t)0xFFFFFFFFU);
1392  REQUIRE(!strcmp(buffer, "FFFFFFFF"));
1393  }
1394 }
1395 
1396 
1397 TEST_CASE("unknown flag", "[]" ) {
1398  char buffer[100];
1399 
1400  test::sprintf(buffer, "%kmarco", 42, 37);
1401  REQUIRE(!strcmp(buffer, "kmarco"));
1402 }
1403 
1404 
1405 TEST_CASE("string length", "[]" ) {
1406  char buffer[100];
1407 
1408  test::sprintf(buffer, "%.4s", "This is a test");
1409  REQUIRE(!strcmp(buffer, "This"));
1410 
1411  test::sprintf(buffer, "%.4s", "test");
1412  REQUIRE(!strcmp(buffer, "test"));
1413 
1414  test::sprintf(buffer, "%.7s", "123");
1415  REQUIRE(!strcmp(buffer, "123"));
1416 
1417  test::sprintf(buffer, "%.7s", "");
1418  REQUIRE(!strcmp(buffer, ""));
1419 
1420  test::sprintf(buffer, "%.4s%.2s", "123456", "abcdef");
1421  REQUIRE(!strcmp(buffer, "1234ab"));
1422 
1423  test::sprintf(buffer, "%.4.2s", "123456");
1424  REQUIRE(!strcmp(buffer, ".2s"));
1425 
1426  test::sprintf(buffer, "%.*s", 3, "123456");
1427  REQUIRE(!strcmp(buffer, "123"));
1428 }
1429 
1430 
1431 TEST_CASE("buffer length", "[]" ) {
1432  char buffer[100];
1433  int ret;
1434 
1435  ret = test::snprintf(nullptr, 10, "%s", "Test");
1436  REQUIRE(ret == 4);
1437  ret = test::snprintf(nullptr, 0, "%s", "Test");
1438  REQUIRE(ret == 4);
1439 
1440  buffer[0] = (char)0xA5;
1441  ret = test::snprintf(buffer, 0, "%s", "Test");
1442  REQUIRE(buffer[0] == (char)0xA5);
1443  REQUIRE(ret == 4);
1444 
1445  buffer[0] = (char)0xCC;
1446  test::snprintf(buffer, 1, "%s", "Test");
1447  REQUIRE(buffer[0] == '\0');
1448 
1449  test::snprintf(buffer, 2, "%s", "Hello");
1450  REQUIRE(!strcmp(buffer, "H"));
1451 }
1452 
1453 
1454 TEST_CASE("ret value", "[]" ) {
1455  char buffer[100] ;
1456  int ret;
1457 
1458  ret = test::snprintf(buffer, 6, "0%s", "1234");
1459  REQUIRE(!strcmp(buffer, "01234"));
1460  REQUIRE(ret == 5);
1461 
1462  ret = test::snprintf(buffer, 6, "0%s", "12345");
1463  REQUIRE(!strcmp(buffer, "01234"));
1464  REQUIRE(ret == 6); // '5' is truncated
1465 
1466  ret = test::snprintf(buffer, 6, "0%s", "1234567");
1467  REQUIRE(!strcmp(buffer, "01234"));
1468  REQUIRE(ret == 8); // '567' are truncated
1469 
1470  ret = test::snprintf(buffer, 10, "hello, world");
1471  REQUIRE(ret == 12);
1472 
1473  ret = test::snprintf(buffer, 3, "%d", 10000);
1474  REQUIRE(ret == 5);
1475  REQUIRE(strlen(buffer) == 2U);
1476  REQUIRE(buffer[0] == '1');
1477  REQUIRE(buffer[1] == '0');
1478  REQUIRE(buffer[2] == '\0');
1479 }
1480 
1481 
1482 TEST_CASE("misc", "[]" ) {
1483  char buffer[100];
1484 
1485  test::sprintf(buffer, "%u%u%ctest%d %s", 5, 3000, 'a', -20, "bit");
1486  REQUIRE(!strcmp(buffer, "53000atest-20 bit"));
1487 
1488  test::sprintf(buffer, "%.*f", 2, 0.33333333);
1489  REQUIRE(!strcmp(buffer, "0.33"));
1490 
1491  test::sprintf(buffer, "%.*d", -1, 1);
1492  REQUIRE(!strcmp(buffer, "1"));
1493 
1494  test::sprintf(buffer, "%.3s", "foobar");
1495  REQUIRE(!strcmp(buffer, "foo"));
1496 
1497  test::sprintf(buffer, "% .0d", 0);
1498  REQUIRE(!strcmp(buffer, " "));
1499 
1500  test::sprintf(buffer, "%10.5d", 4);
1501  REQUIRE(!strcmp(buffer, " 00004"));
1502 
1503  test::sprintf(buffer, "%*sx", -3, "hi");
1504  REQUIRE(!strcmp(buffer, "hi x"));
1505 
1506 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
1507  test::sprintf(buffer, "%.*g", 2, 0.33333333);
1508  REQUIRE(!strcmp(buffer, "0.33"));
1509 
1510  test::sprintf(buffer, "%.*e", 2, 0.33333333);
1511  REQUIRE(!strcmp(buffer, "3.33e-01"));
1512 #endif
1513 }
static void vsnprintf_builder_1(char *buffer,...)
Definition: test_suite.cpp:98
#define vsnprintf
#define printf
static void vprintf_builder_1(char *buffer,...)
Definition: test_suite.cpp:90
static char printf_buffer[100]
Definition: test_suite.cpp:46
#define REQUIRE(...)
Definition: catch.hpp:14671
TEST_CASE("printf", "[]")
Definition: test_suite.cpp:61
static void vsnprintf_builder_3(char *buffer,...)
Definition: test_suite.cpp:106
#define vprintf
static size_t printf_idx
Definition: test_suite.cpp:47
static void _out_fct(char character, void *buffer, size_t idx, size_t maxlen)
Definition: test_suite.cpp:159
int fctprintf(void(*out)(char character, void *arg), void *arg, const char *format,...)
Definition: test_suite.cpp:906
#define snprintf
void _putchar(char character)
Definition: test_suite.cpp:49
#define sprintf


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58