1 /* Unit tests for gprintf
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
29 test_retval_and_trunc (void)
34 res = g_snprintf (buf, 0, "abc");
35 g_assert_cmpint (res, ==, 3);
37 res = g_snprintf (NULL, 0, "abc");
38 g_assert_cmpint (res, ==, 3);
40 res = g_snprintf (buf, 5, "abc");
41 g_assert_cmpint (res, ==, 3);
43 res = g_snprintf (buf, 1, "abc");
44 g_assert_cmpint (res, ==, 3);
45 g_assert (buf[0] == '\0');
46 g_assert_cmpstr (buf, ==, "");
48 res = g_snprintf (buf, 2, "abc");
49 g_assert_cmpint (res, ==, 3);
50 g_assert (buf[1] == '\0');
51 g_assert_cmpstr (buf, ==, "a");
53 res = g_snprintf (buf, 3, "abc");
54 g_assert_cmpint (res, ==, 3);
55 g_assert (buf[2] == '\0');
56 g_assert_cmpstr (buf, ==, "ab");
58 res = g_snprintf (buf, 4, "abc");
59 g_assert_cmpint (res, ==, 3);
60 g_assert (buf[3] == '\0');
61 g_assert_cmpstr (buf, ==, "abc");
63 res = g_snprintf (buf, 5, "abc");
64 g_assert_cmpint (res, ==, 3);
65 g_assert (buf[3] == '\0');
66 g_assert_cmpstr (buf, ==, "abc");
76 /* %d basic formatting */
78 res = g_snprintf (buf, 128, "%d", 5);
79 g_assert_cmpint (res, ==, 1);
80 g_assert_cmpstr (buf, ==, "5");
82 res = g_snprintf (buf, 128, "%d", 0);
83 g_assert_cmpint (res, ==, 1);
84 g_assert_cmpstr (buf, ==, "0");
86 res = g_snprintf (buf, 128, "%.0d", 0);
87 g_assert_cmpint (res, ==, 0);
88 g_assert_cmpstr (buf, ==, "");
90 res = g_snprintf (buf, 128, "%.0d", 1);
91 g_assert_cmpint (res, ==, 1);
92 g_assert_cmpstr (buf, ==, "1");
94 res = g_snprintf (buf, 128, "%.d", 2);
95 g_assert_cmpint (res, ==, 1);
96 g_assert_cmpstr (buf, ==, "2");
98 res = g_snprintf (buf, 128, "%d", -1);
99 g_assert_cmpint (res, ==, 2);
100 g_assert_cmpstr (buf, ==, "-1");
102 res = g_snprintf (buf, 128, "%.3d", 5);
103 g_assert_cmpint (res, ==, 3);
104 g_assert_cmpstr (buf, ==, "005");
106 res = g_snprintf (buf, 128, "%.3d", -5);
107 g_assert_cmpint (res, ==, 4);
108 g_assert_cmpstr (buf, ==, "-005");
110 res = g_snprintf (buf, 128, "%5.3d", 5);
111 g_assert_cmpint (res, ==, 5);
112 g_assert_cmpstr (buf, ==, " 005");
114 res = g_snprintf (buf, 128, "%-5.3d", -5);
115 g_assert_cmpint (res, ==, 5);
116 g_assert_cmpstr (buf, ==, "-005 ");
118 /* %d, length modifiers */
120 res = g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5);
121 g_assert_cmpint (res, ==, 2);
122 g_assert_cmpstr (buf, ==, "-5");
124 res = g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5);
125 g_assert_cmpint (res, ==, 1);
126 g_assert_cmpstr (buf, ==, "5");
128 res = g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5);
129 g_assert_cmpint (res, ==, 2);
130 g_assert_cmpstr (buf, ==, "-5");
132 res = g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5);
133 g_assert_cmpint (res, ==, 1);
134 g_assert_cmpstr (buf, ==, "5");
136 res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5);
137 g_assert_cmpint (res, ==, 2);
138 g_assert_cmpstr (buf, ==, "-5");
140 res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5);
141 g_assert_cmpint (res, ==, 1);
142 g_assert_cmpstr (buf, ==, "5");
144 res = g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5);
145 g_assert_cmpint (res, ==, 2);
146 g_assert_cmpstr (buf, ==, "-5");
148 res = g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5);
149 g_assert_cmpint (res, ==, 1);
150 g_assert_cmpstr (buf, ==, "5");
154 res = g_snprintf (buf, 128, "%-d", 5);
155 g_assert_cmpint (res, ==, 1);
156 g_assert_cmpstr (buf, ==, "5");
158 res = g_snprintf (buf, 128, "%-+d", 5);
159 g_assert_cmpint (res, ==, 2);
160 g_assert_cmpstr (buf, ==, "+5");
162 res = g_snprintf (buf, 128, "%+-d", 5);
163 g_assert_cmpint (res, ==, 2);
164 g_assert_cmpstr (buf, ==, "+5");
166 res = g_snprintf (buf, 128, "%+d", -5);
167 g_assert_cmpint (res, ==, 2);
168 g_assert_cmpstr (buf, ==, "-5");
170 res = g_snprintf (buf, 128, "% d", 5);
171 g_assert_cmpint (res, ==, 2);
172 g_assert_cmpstr (buf, ==, " 5");
174 res = g_snprintf (buf, 128, "% .0d", 0);
175 g_assert_cmpint (res, ==, 1);
176 g_assert_cmpstr (buf, ==, " ");
178 res = g_snprintf (buf, 128, "%03d", 5);
179 g_assert_cmpint (res, ==, 3);
180 g_assert_cmpstr (buf, ==, "005");
182 res = g_snprintf (buf, 128, "%03d", -5);
183 g_assert_cmpint (res, ==, 3);
184 g_assert_cmpstr (buf, ==, "-05");
186 /* gcc emits warnings for the following formats, since the C spec
187 * says some of the flags must be ignored. (The " " in "% +d" and
188 * the "0" in "%-03d".) But we need to test that our printf gets
189 * those rules right. So we fool gcc into not warning.
192 res = g_snprintf (buf, 128, fmt, 5);
193 g_assert_cmpint (res, ==, 2);
194 g_assert_cmpstr (buf, ==, "+5");
197 res = g_snprintf (buf, 128, fmt, -5);
198 g_assert_cmpint (res, ==, 3);
199 g_assert_cmpstr (buf, ==, "-5 ");
208 /* %o basic formatting */
210 res = g_snprintf (buf, 128, "%o", 5);
211 g_assert_cmpint (res, ==, 1);
212 g_assert_cmpstr (buf, ==, "5");
214 res = g_snprintf (buf, 128, "%o", 8);
215 g_assert_cmpint (res, ==, 2);
216 g_assert_cmpstr (buf, ==, "10");
218 res = g_snprintf (buf, 128, "%o", 0);
219 g_assert_cmpint (res, ==, 1);
220 g_assert_cmpstr (buf, ==, "0");
222 res = g_snprintf (buf, 128, "%.0o", 0);
223 g_assert_cmpint (res, ==, 0);
224 g_assert_cmpstr (buf, ==, "");
226 res = g_snprintf (buf, 128, "%.0o", 1);
227 g_assert_cmpint (res, ==, 1);
228 g_assert_cmpstr (buf, ==, "1");
230 res = g_snprintf (buf, 128, "%.3o", 5);
231 g_assert_cmpint (res, ==, 3);
232 g_assert_cmpstr (buf, ==, "005");
234 res = g_snprintf (buf, 128, "%.3o", 8);
235 g_assert_cmpint (res, ==, 3);
236 g_assert_cmpstr (buf, ==, "010");
238 res = g_snprintf (buf, 128, "%5.3o", 5);
239 g_assert_cmpint (res, ==, 5);
240 g_assert_cmpstr (buf, ==, " 005");
249 /* %u, basic formatting */
251 res = g_snprintf (buf, 128, "%u", 5);
252 g_assert_cmpint (res, ==, 1);
253 g_assert_cmpstr (buf, ==, "5");
255 res = g_snprintf (buf, 128, "%u", 0);
256 g_assert_cmpint (res, ==, 1);
257 g_assert_cmpstr (buf, ==, "0");
259 res = g_snprintf (buf, 128, "%.0u", 0);
260 g_assert_cmpint (res, ==, 0);
261 g_assert_cmpstr (buf, ==, "");
263 res = g_snprintf (buf, 128, "%.0u", 1);
264 g_assert_cmpint (res, ==, 1);
265 g_assert_cmpstr (buf, ==, "1");
267 res = g_snprintf (buf, 128, "%.3u", 5);
268 g_assert_cmpint (res, ==, 3);
269 g_assert_cmpstr (buf, ==, "005");
271 res = g_snprintf (buf, 128, "%5.3u", 5);
272 g_assert_cmpint (res, ==, 5);
273 g_assert_cmpstr (buf, ==, " 005");
282 /* %x, basic formatting */
284 res = g_snprintf (buf, 128, "%x", 5);
285 g_assert_cmpint (res, ==, 1);
286 g_assert_cmpstr (buf, ==, "5");
288 res = g_snprintf (buf, 128, "%x", 31);
289 g_assert_cmpint (res, ==, 2);
290 g_assert_cmpstr (buf, ==, "1f");
292 res = g_snprintf (buf, 128, "%x", 0);
293 g_assert_cmpint (res, ==, 1);
294 g_assert_cmpstr (buf, ==, "0");
296 res = g_snprintf (buf, 128, "%.0x", 0);
297 g_assert_cmpint (res, ==, 0);
298 g_assert_cmpstr (buf, ==, "");
300 res = g_snprintf (buf, 128, "%.0x", 1);
301 g_assert_cmpint (res, ==, 1);
302 g_assert_cmpstr (buf, ==, "1");
304 res = g_snprintf (buf, 128, "%.3x", 5);
305 g_assert_cmpint (res, ==, 3);
306 g_assert_cmpstr (buf, ==, "005");
308 res = g_snprintf (buf, 128, "%.3x", 31);
309 g_assert_cmpint (res, ==, 3);
310 g_assert_cmpstr (buf, ==, "01f");
312 res = g_snprintf (buf, 128, "%5.3x", 5);
313 g_assert_cmpint (res, ==, 5);
314 g_assert_cmpstr (buf, ==, " 005");
318 res = g_snprintf (buf, 128, "%-x", 5);
319 g_assert_cmpint (res, ==, 1);
320 g_assert_cmpstr (buf, ==, "5");
322 res = g_snprintf (buf, 128, "%03x", 5);
323 g_assert_cmpint (res, ==, 3);
324 g_assert_cmpstr (buf, ==, "005");
326 res = g_snprintf (buf, 128, "%#x", 31);
327 g_assert_cmpint (res, ==, 4);
328 g_assert_cmpstr (buf, ==, "0x1f");
330 res = g_snprintf (buf, 128, "%#x", 0);
331 g_assert_cmpint (res, ==, 1);
332 g_assert_cmpstr (buf, ==, "0");
341 /* %X, basic formatting */
343 res = g_snprintf (buf, 128, "%X", 5);
344 g_assert_cmpint (res, ==, 1);
345 g_assert_cmpstr (buf, ==, "5");
347 res = g_snprintf (buf, 128, "%X", 31);
348 g_assert_cmpint (res, ==, 2);
349 g_assert_cmpstr (buf, ==, "1F");
351 res = g_snprintf (buf, 128, "%X", 0);
352 g_assert_cmpint (res, ==, 1);
353 g_assert_cmpstr (buf, ==, "0");
355 res = g_snprintf (buf, 128, "%.0X", 0);
356 g_assert_cmpint (res, ==, 0);
357 g_assert_cmpstr (buf, ==, "");
359 res = g_snprintf (buf, 128, "%.0X", 1);
360 g_assert_cmpint (res, ==, 1);
361 g_assert_cmpstr (buf, ==, "1");
363 res = g_snprintf (buf, 128, "%.3X", 5);
364 g_assert_cmpint (res, ==, 3);
365 g_assert_cmpstr (buf, ==, "005");
367 res = g_snprintf (buf, 128, "%.3X", 31);
368 g_assert_cmpint (res, ==, 3);
369 g_assert_cmpstr (buf, ==, "01F");
371 res = g_snprintf (buf, 128, "%5.3X", 5);
372 g_assert_cmpint (res, ==, 5);
373 g_assert_cmpstr (buf, ==, " 005");
377 res = g_snprintf (buf, 128, "%-X", 5);
378 g_assert_cmpint (res, ==, 1);
379 g_assert_cmpstr (buf, ==, "5");
381 res = g_snprintf (buf, 128, "%03X", 5);
382 g_assert_cmpint (res, ==, 3);
383 g_assert_cmpstr (buf, ==, "005");
385 res = g_snprintf (buf, 128, "%#X", 31);
386 g_assert_cmpint (res, ==, 4);
387 g_assert_cmpstr (buf, ==, "0X1F");
389 res = g_snprintf (buf, 128, "%#X", 0);
390 g_assert_cmpint (res, ==, 1);
391 g_assert_cmpstr (buf, ==, "0");
400 /* %f, basic formattting */
402 res = g_snprintf (buf, 128, "%f", G_PI);
403 g_assert_cmpint (res, ==, 8);
404 g_assert (0 == strncmp (buf, "3.14159", 7));
406 res = g_snprintf (buf, 128, "%.8f", G_PI);
407 g_assert_cmpint (res, ==, 10);
408 g_assert (0 == strncmp (buf, "3.1415926", 9));
410 res = g_snprintf (buf, 128, "%.0f", G_PI);
411 g_assert_cmpint (res, ==, 1);
412 g_assert_cmpstr (buf, ==, "3");
414 res = g_snprintf (buf, 128, "%1.f", G_PI);
415 g_assert_cmpint (res, ==, 1);
416 g_assert_cmpstr (buf, ==, "3");
418 res = g_snprintf (buf, 128, "%3.f", G_PI);
419 g_assert_cmpint (res, ==, 3);
420 g_assert_cmpstr (buf, ==, " 3");
424 res = g_snprintf (buf, 128, "%+f", G_PI);
425 g_assert_cmpint (res, ==, 9);
426 g_assert (0 == strncmp (buf, "+3.14159", 8));
428 res = g_snprintf (buf, 128, "% f", G_PI);
429 g_assert_cmpint (res, ==, 9);
430 g_assert (0 == strncmp (buf, " 3.14159", 8));
432 res = g_snprintf (buf, 128, "%#.0f", G_PI);
433 g_assert_cmpint (res, ==, 2);
434 g_assert_cmpstr (buf, ==, "3.");
436 res = g_snprintf (buf, 128, "%05.2f", G_PI);
437 g_assert_cmpint (res, ==, 5);
438 g_assert_cmpstr (buf, ==, "03.14");
442 same_value (const gchar *actual,
443 const gchar *expected)
445 gdouble actual_value, expected_value;
447 actual_value = g_ascii_strtod (actual, NULL);
448 expected_value = g_ascii_strtod (expected, NULL);
450 return actual_value == expected_value;
459 /* %e, basic formatting */
460 /* for %e we can't expect to reproduce exact strings and lengths, since SUS
461 * only guarantees that the exponent shall always contain at least two
462 * digits. On Windows, it seems to be at least three digits long.
463 * Therefore, we compare the results of parsing the expected result and the
467 res = g_snprintf (buf, 128, "%e", G_PI);
468 g_assert_cmpint (res, >=, 12);
469 g_assert (same_value (buf, "3.141593e+00"));
471 res = g_snprintf (buf, 128, "%.8e", G_PI);
472 g_assert_cmpint (res, >=, 14);
473 g_assert (same_value (buf, "3.14159265e+00"));
475 res = g_snprintf (buf, 128, "%.0e", G_PI);
476 g_assert_cmpint (res, >=, 5);
477 g_assert (same_value (buf, "3e+00"));
479 res = g_snprintf (buf, 128, "%.1e", 0.0);
480 g_assert_cmpint (res, >=, 7);
481 g_assert (same_value (buf, "0.0e+00"));
483 res = g_snprintf (buf, 128, "%.1e", 0.00001);
484 g_assert_cmpint (res, >=, 7);
485 g_assert (same_value (buf, "1.0e-05"));
487 res = g_snprintf (buf, 128, "%.1e", 10000.0);
488 g_assert_cmpint (res, >=, 7);
489 g_assert (same_value (buf, "1.0e+04"));
493 res = g_snprintf (buf, 128, "%+e", G_PI);
494 g_assert_cmpint (res, >=, 13);
495 g_assert (same_value (buf, "+3.141593e+00"));
497 res = g_snprintf (buf, 128, "% e", G_PI);
498 g_assert_cmpint (res, >=, 13);
499 g_assert (same_value (buf, " 3.141593e+00"));
501 res = g_snprintf (buf, 128, "%#.0e", G_PI);
502 g_assert_cmpint (res, >=, 6);
503 g_assert (same_value (buf, "3.e+00"));
505 res = g_snprintf (buf, 128, "%09.2e", G_PI);
506 g_assert_cmpint (res, >=, 9);
507 g_assert (same_value (buf, "03.14e+00"));
516 res = g_snprintf (buf, 128, "%c", 'a');
517 g_assert_cmpint (res, ==, 1);
518 g_assert_cmpstr (buf, ==, "a");
527 res = g_snprintf (buf, 128, "%.2s", "abc");
528 g_assert_cmpint (res, ==, 2);
529 g_assert_cmpstr (buf, ==, "ab");
531 res = g_snprintf (buf, 128, "%.6s", "abc");
532 g_assert_cmpint (res, ==, 3);
533 g_assert_cmpstr (buf, ==, "abc");
535 res = g_snprintf (buf, 128, "%5s", "abc");
536 g_assert_cmpint (res, ==, 5);
537 g_assert_cmpstr (buf, ==, " abc");
539 res = g_snprintf (buf, 128, "%-5s", "abc");
540 g_assert_cmpint (res, ==, 5);
541 g_assert_cmpstr (buf, ==, "abc ");
543 res = g_snprintf (buf, 128, "%5.2s", "abc");
544 g_assert_cmpint (res, ==, 5);
545 g_assert_cmpstr (buf, ==, " ab");
547 res = g_snprintf (buf, 128, "%*s", 5, "abc");
548 g_assert_cmpint (res, ==, 5);
549 g_assert_cmpstr (buf, ==, " abc");
551 #if 0 /* HP-UX doesn't get this right */
552 res = g_snprintf (buf, 128, "%*s", -5, "abc");
553 g_assert_cmpint (res, ==, 5);
554 g_assert_cmpstr (buf, ==, "abc ");
557 res = g_snprintf (buf, 128, "%*.*s", 5, 2, "abc");
558 g_assert_cmpint (res, ==, 5);
559 g_assert_cmpstr (buf, ==, " ab");
570 res = g_snprintf (buf, 128, "abc%n", &i);
571 g_assert_cmpint (res, ==, 3);
572 g_assert_cmpstr (buf, ==, "abc");
573 g_assert_cmpint (i, ==, 3);
575 res = g_snprintf (buf, 128, "abc%ln", &l);
576 g_assert_cmpint (res, ==, 3);
577 g_assert_cmpstr (buf, ==, "abc");
578 g_assert_cmpint (l, ==, 3);
587 res = g_snprintf (buf, 128, "%%");
588 g_assert_cmpint (res, ==, 1);
589 g_assert_cmpstr (buf, ==, "%");
593 test_positional_params (void)
598 res = g_snprintf (buf, 128, "%2$c %1$c", 'b', 'a');
599 g_assert_cmpint (res, ==, 3);
600 g_assert_cmpstr (buf, ==, "a b");
602 res = g_snprintf (buf, 128, "%1$*2$.*3$s", "abc", 5, 2);
603 g_assert_cmpint (res, ==, 5);
604 g_assert_cmpstr (buf, ==, " ab");
606 res = g_snprintf (buf, 128, "%1$s%1$s", "abc");
607 g_assert_cmpint (res, ==, 6);
608 g_assert_cmpstr (buf, ==, "abcabc");
612 test_positional_params2_child (void)
616 res = g_printf ("%2$c %1$c\n", 'b', 'a');
617 g_assert_cmpint (res, ==, 4);
619 res = g_printf ("%1$*2$.*3$s\n", "abc", 5, 2);
620 g_assert_cmpint (res, ==, 6);
622 res = g_printf ("%1$s%1$s\n", "abc");
623 g_assert_cmpint (res, ==, 7);
627 test_positional_params2 (void)
629 g_test_trap_subprocess ("/printf/test-positional-params:child",
630 0, G_TEST_TRAP_SILENCE_STDOUT);
631 g_test_trap_assert_passed ();
632 g_test_trap_assert_stdout ("a b\n ab\nabcabc\n");
636 test_positional_params3 (void)
641 res = g_sprintf (buf, "%2$c %1$c", 'b', 'a');
642 g_assert_cmpint (res, ==, 3);
643 g_assert_cmpstr (buf, ==, "a b");
645 res = g_sprintf (buf, "%1$*2$.*3$s", "abc", 5, 2);
646 g_assert_cmpint (res, ==, 5);
647 g_assert_cmpstr (buf, ==, " ab");
649 res = g_sprintf (buf, "%1$s%1$s", "abc");
650 g_assert_cmpint (res, ==, 6);
651 g_assert_cmpstr (buf, ==, "abcabc");
655 test_percent2_child (void)
659 res = g_printf ("%%");
660 g_assert_cmpint (res, ==, 1);
666 g_test_trap_subprocess ("/printf/test-percent:child",
667 0, G_TEST_TRAP_SILENCE_STDOUT);
668 g_test_trap_assert_passed ();
669 g_test_trap_assert_stdout ("*%*");
678 res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)123456);
679 g_assert_cmpint (res, ==, 6);
680 g_assert_cmpstr (buf, ==, "123456");
682 res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-123456);
683 g_assert_cmpint (res, ==, 7);
684 g_assert_cmpstr (buf, ==, "-123456");
686 res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)123456);
687 g_assert_cmpint (res, ==, 6);
688 g_assert_cmpstr (buf, ==, "123456");
690 res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "o", (gint64)123456);
691 g_assert_cmpint (res, ==, 6);
692 g_assert_cmpstr (buf, ==, "361100");
694 res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "o", (gint64)123456);
695 g_assert_cmpint (res, ==, 7);
696 g_assert_cmpstr (buf, ==, "0361100");
698 res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "x", (gint64)123456);
699 g_assert_cmpint (res, ==, 5);
700 g_assert_cmpstr (buf, ==, "1e240");
702 res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "x", (gint64)123456);
703 g_assert_cmpint (res, ==, 7);
704 g_assert_cmpstr (buf, ==, "0x1e240");
706 res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "X", (gint64)123456);
707 g_assert_cmpint (res, ==, 5);
708 g_assert_cmpstr (buf, ==, "1E240");
711 /* On Win32, test that the "ll" modifier also works, for backward
712 * compatibility. One really should use the G_GINT64_MODIFIER (which
713 * on Win32 is the "I64" that the (msvcrt) C library's printf uses),
714 * but "ll" used to work with the "trio" g_printf implementation in
715 * GLib 2.2, so it's best if it continues to work.
718 /* However, gcc doesn't know about this, so we need to disable printf
721 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
722 _Pragma ("GCC diagnostic push")
723 _Pragma ("GCC diagnostic ignored \"-Wformat\"")
724 _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
727 res = g_snprintf (buf, 128, "%" "lli", (gint64)123456);
728 g_assert_cmpint (res, ==, 6);
729 g_assert_cmpstr (buf, ==, "123456");
731 res = g_snprintf (buf, 128, "%" "lli", (gint64)-123456);
732 g_assert_cmpint (res, ==, 7);
733 g_assert_cmpstr (buf, ==, "-123456");
735 res = g_snprintf (buf, 128, "%" "llu", (guint64)123456);
736 g_assert_cmpint (res, ==, 6);
737 g_assert_cmpstr (buf, ==, "123456");
739 res = g_snprintf (buf, 128, "%" "ll" "o", (gint64)123456);
740 g_assert_cmpint (res, ==, 6);
741 g_assert_cmpstr (buf, ==, "361100");
743 res = g_snprintf (buf, 128, "%#" "ll" "o", (gint64)123456);
744 g_assert_cmpint (res, ==, 7);
745 g_assert_cmpstr (buf, ==, "0361100");
747 res = g_snprintf (buf, 128, "%" "ll" "x", (gint64)123456);
748 g_assert_cmpint (res, ==, 5);
749 g_assert_cmpstr (buf, ==, "1e240");
751 res = g_snprintf (buf, 128, "%#" "ll" "x", (gint64)123456);
752 g_assert_cmpint (res, ==, 7);
753 g_assert_cmpstr (buf, ==, "0x1e240");
755 res = g_snprintf (buf, 128, "%" "ll" "X", (gint64)123456);
756 g_assert_cmpint (res, ==, 5);
757 g_assert_cmpstr (buf, ==, "1E240");
759 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
760 _Pragma ("GCC diagnostic pop")
767 test_64bit2_child (void)
771 res = g_printf ("%" G_GINT64_FORMAT "\n", (gint64)123456);
772 g_assert_cmpint (res, ==, 7);
774 res = g_printf ("%" G_GINT64_FORMAT "\n", (gint64)-123456);
775 g_assert_cmpint (res, ==, 8);
777 res = g_printf ("%" G_GUINT64_FORMAT "\n", (guint64)123456);
778 g_assert_cmpint (res, ==, 7);
780 res = g_printf ("%" G_GINT64_MODIFIER "o\n", (gint64)123456);
781 g_assert_cmpint (res, ==, 7);
783 res = g_printf ("%#" G_GINT64_MODIFIER "o\n", (gint64)123456);
784 g_assert_cmpint (res, ==, 8);
786 res = g_printf ("%" G_GINT64_MODIFIER "x\n", (gint64)123456);
787 g_assert_cmpint (res, ==, 6);
789 res = g_printf ("%#" G_GINT64_MODIFIER "x\n", (gint64)123456);
790 g_assert_cmpint (res, ==, 8);
792 res = g_printf ("%" G_GINT64_MODIFIER "X\n", (gint64)123456);
793 g_assert_cmpint (res, ==, 6);
798 test_64bit2_child_win32 (void)
802 /* On Win32, test that the "ll" modifier also works, for backward
803 * compatibility. One really should use the G_GINT64_MODIFIER (which
804 * on Win32 is the "I64" that the (msvcrt) C library's printf uses),
805 * but "ll" used to work with the "trio" g_printf implementation in
806 * GLib 2.2, so it's best if it continues to work.
809 /* However, gcc doesn't know about this, so we need to disable printf
812 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
813 _Pragma ("GCC diagnostic push")
814 _Pragma ("GCC diagnostic ignored \"-Wformat\"")
815 _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
818 res = g_printf ("%" "lli\n", (gint64)123456);
819 g_assert_cmpint (res, ==, 7);
821 res = g_printf ("%" "lli\n", (gint64)-123456);
822 g_assert_cmpint (res, ==, 8);
824 res = g_printf ("%" "llu\n", (guint64)123456);
825 g_assert_cmpint (res, ==, 7);
827 res = g_printf ("%" "ll" "o\n", (gint64)123456);
828 g_assert_cmpint (res, ==, 7);
830 res = g_printf ("%#" "ll" "o\n", (gint64)123456);
831 g_assert_cmpint (res, ==, 8);
833 res = g_printf ("%" "ll" "x\n", (gint64)123456);
834 g_assert_cmpint (res, ==, 6);
836 res = g_printf ("%#" "ll" "x\n", (gint64)123456);
837 g_assert_cmpint (res, ==, 8);
839 res = g_printf ("%" "ll" "X\n", (gint64)123456);
840 g_assert_cmpint (res, ==, 6);
842 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
843 _Pragma ("GCC diagnostic pop")
851 g_test_trap_subprocess ("/printf/test-64bit:child",
852 0, G_TEST_TRAP_SILENCE_STDOUT);
853 g_test_trap_assert_passed ();
854 g_test_trap_assert_stdout ("123456\n-123456\n123456\n"
855 "361100\n0361100\n1e240\n"
859 g_test_trap_subprocess ("/printf/test-64bit:child-win32",
860 0, G_TEST_TRAP_SILENCE_STDOUT);
861 g_test_trap_assert_passed ();
862 g_test_trap_assert_stdout ("123456\n-123456\n123456\n"
863 "361100\n0361100\n1e240\n"
869 upper_bound (const gchar *format, ...)
874 va_start (args, format);
875 res = g_printf_string_upper_bound (format, args);
882 test_upper_bound (void)
886 res = upper_bound ("bla %s %d: %g\n", "bla", 123, 0.123);
887 g_assert_cmpint (res, ==, 20);
894 g_test_init (&argc, &argv, NULL);
896 g_test_add_func ("/snprintf/retval-and-trunc", test_retval_and_trunc);
897 g_test_add_func ("/snprintf/%d", test_d);
898 g_test_add_func ("/snprintf/%o", test_o);
899 g_test_add_func ("/snprintf/%u", test_u);
900 g_test_add_func ("/snprintf/%x", test_x);
901 g_test_add_func ("/snprintf/%X", test_X);
902 g_test_add_func ("/snprintf/%f", test_f);
903 g_test_add_func ("/snprintf/%e", test_e);
904 g_test_add_func ("/snprintf/%c", test_c);
905 g_test_add_func ("/snprintf/%s", test_s);
906 g_test_add_func ("/snprintf/%n", test_n);
907 g_test_add_func ("/snprintf/test-percent", test_percent);
908 g_test_add_func ("/snprintf/test-positional-params", test_positional_params);
909 g_test_add_func ("/snprintf/test-64bit", test_64bit);
911 g_test_add_func ("/printf/test-percent", test_percent2);
912 g_test_add_func ("/printf/test-percent:child", test_percent2_child);
913 g_test_add_func ("/printf/test-positional-params", test_positional_params2);
914 g_test_add_func ("/printf/test-positional-params:child", test_positional_params2_child);
915 g_test_add_func ("/printf/test-64bit", test_64bit2);
916 g_test_add_func ("/printf/test-64bit:child", test_64bit2_child);
918 g_test_add_func ("/printf/test-64bit:child-win32", test_64bit2_child_win32);
921 g_test_add_func ("/sprintf/test-positional-params", test_positional_params3);
922 g_test_add_func ("/sprintf/upper-bound", test_upper_bound);