Increase the timeout for some GLib tests
[glib.git] / glib / tests / test-printf.c
blob377e3b5e77a1d8595fdc09853ba21591214b1d41
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.
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "glib.h"
26 #include "gstdio.h"
28 static void
29 test_retval_and_trunc (void)
31 gchar buf[128];
32 gint res;
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");
69 static void
70 test_d (void)
72 gchar buf[128];
73 gint res;
75 /* %d basic formatting */
77 res = g_snprintf (buf, 128, "%d", 5);
78 g_assert_cmpint (res, ==, 1);
79 g_assert_cmpstr (buf, ==, "5");
81 res = g_snprintf (buf, 128, "%d", 0);
82 g_assert_cmpint (res, ==, 1);
83 g_assert_cmpstr (buf, ==, "0");
85 res = g_snprintf (buf, 128, "%.0d", 0);
86 g_assert_cmpint (res, ==, 0);
87 g_assert_cmpstr (buf, ==, "");
89 res = g_snprintf (buf, 128, "%.0d", 1);
90 g_assert_cmpint (res, ==, 1);
91 g_assert_cmpstr (buf, ==, "1");
93 res = g_snprintf (buf, 128, "%.d", 2);
94 g_assert_cmpint (res, ==, 1);
95 g_assert_cmpstr (buf, ==, "2");
97 res = g_snprintf (buf, 128, "%d", -1);
98 g_assert_cmpint (res, ==, 2);
99 g_assert_cmpstr (buf, ==, "-1");
101 res = g_snprintf (buf, 128, "%.3d", 5);
102 g_assert_cmpint (res, ==, 3);
103 g_assert_cmpstr (buf, ==, "005");
105 res = g_snprintf (buf, 128, "%.3d", -5);
106 g_assert_cmpint (res, ==, 4);
107 g_assert_cmpstr (buf, ==, "-005");
109 res = g_snprintf (buf, 128, "%5.3d", 5);
110 g_assert_cmpint (res, ==, 5);
111 g_assert_cmpstr (buf, ==, " 005");
113 res = g_snprintf (buf, 128, "%-5.3d", -5);
114 g_assert_cmpint (res, ==, 5);
115 g_assert_cmpstr (buf, ==, "-005 ");
117 /* %d, length modifiers */
119 res = g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5);
120 g_assert_cmpint (res, ==, 2);
121 g_assert_cmpstr (buf, ==, "-5");
123 res = g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5);
124 g_assert_cmpint (res, ==, 1);
125 g_assert_cmpstr (buf, ==, "5");
127 res = g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5);
128 g_assert_cmpint (res, ==, 2);
129 g_assert_cmpstr (buf, ==, "-5");
131 res = g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5);
132 g_assert_cmpint (res, ==, 1);
133 g_assert_cmpstr (buf, ==, "5");
135 res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5);
136 g_assert_cmpint (res, ==, 2);
137 g_assert_cmpstr (buf, ==, "-5");
139 res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5);
140 g_assert_cmpint (res, ==, 1);
141 g_assert_cmpstr (buf, ==, "5");
143 res = g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5);
144 g_assert_cmpint (res, ==, 2);
145 g_assert_cmpstr (buf, ==, "-5");
147 res = g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5);
148 g_assert_cmpint (res, ==, 1);
149 g_assert_cmpstr (buf, ==, "5");
151 /* %d, flags */
153 res = g_snprintf (buf, 128, "%-d", 5);
154 g_assert_cmpint (res, ==, 1);
155 g_assert_cmpstr (buf, ==, "5");
157 res = g_snprintf (buf, 128, "%-+d", 5);
158 g_assert_cmpint (res, ==, 2);
159 g_assert_cmpstr (buf, ==, "+5");
161 res = g_snprintf (buf, 128, "%+-d", 5);
162 g_assert_cmpint (res, ==, 2);
163 g_assert_cmpstr (buf, ==, "+5");
165 res = g_snprintf (buf, 128, "%+d", -5);
166 g_assert_cmpint (res, ==, 2);
167 g_assert_cmpstr (buf, ==, "-5");
169 res = g_snprintf (buf, 128, "% d", 5);
170 g_assert_cmpint (res, ==, 2);
171 g_assert_cmpstr (buf, ==, " 5");
173 res = g_snprintf (buf, 128, "% .0d", 0);
174 g_assert_cmpint (res, ==, 1);
175 g_assert_cmpstr (buf, ==, " ");
177 res = g_snprintf (buf, 128, "%03d", 5);
178 g_assert_cmpint (res, ==, 3);
179 g_assert_cmpstr (buf, ==, "005");
181 res = g_snprintf (buf, 128, "%03d", -5);
182 g_assert_cmpint (res, ==, 3);
183 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.
191 * These have to be in a separate function in order to use #pragma.
193 #pragma GCC diagnostic push
194 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
195 static void
196 test_d_invalid (void)
198 const gchar *fmt;
199 gchar buf[128];
200 gint res;
202 fmt = "% +d";
203 res = g_snprintf (buf, 128, fmt, 5);
204 g_assert_cmpint (res, ==, 2);
205 g_assert_cmpstr (buf, ==, "+5");
207 fmt = "%-03d";
208 res = g_snprintf (buf, 128, fmt, -5);
209 g_assert_cmpint (res, ==, 3);
210 g_assert_cmpstr (buf, ==, "-5 ");
212 #pragma GCC diagnostic pop
214 static void
215 test_o (void)
217 gchar buf[128];
218 gint res;
220 /* %o basic formatting */
222 res = g_snprintf (buf, 128, "%o", 5);
223 g_assert_cmpint (res, ==, 1);
224 g_assert_cmpstr (buf, ==, "5");
226 res = g_snprintf (buf, 128, "%o", 8);
227 g_assert_cmpint (res, ==, 2);
228 g_assert_cmpstr (buf, ==, "10");
230 res = g_snprintf (buf, 128, "%o", 0);
231 g_assert_cmpint (res, ==, 1);
232 g_assert_cmpstr (buf, ==, "0");
234 res = g_snprintf (buf, 128, "%.0o", 0);
235 g_assert_cmpint (res, ==, 0);
236 g_assert_cmpstr (buf, ==, "");
238 res = g_snprintf (buf, 128, "%.0o", 1);
239 g_assert_cmpint (res, ==, 1);
240 g_assert_cmpstr (buf, ==, "1");
242 res = g_snprintf (buf, 128, "%.3o", 5);
243 g_assert_cmpint (res, ==, 3);
244 g_assert_cmpstr (buf, ==, "005");
246 res = g_snprintf (buf, 128, "%.3o", 8);
247 g_assert_cmpint (res, ==, 3);
248 g_assert_cmpstr (buf, ==, "010");
250 res = g_snprintf (buf, 128, "%5.3o", 5);
251 g_assert_cmpint (res, ==, 5);
252 g_assert_cmpstr (buf, ==, " 005");
255 static void
256 test_u (void)
258 gchar buf[128];
259 gint res;
261 /* %u, basic formatting */
263 res = g_snprintf (buf, 128, "%u", 5);
264 g_assert_cmpint (res, ==, 1);
265 g_assert_cmpstr (buf, ==, "5");
267 res = g_snprintf (buf, 128, "%u", 0);
268 g_assert_cmpint (res, ==, 1);
269 g_assert_cmpstr (buf, ==, "0");
271 res = g_snprintf (buf, 128, "%.0u", 0);
272 g_assert_cmpint (res, ==, 0);
273 g_assert_cmpstr (buf, ==, "");
275 res = g_snprintf (buf, 128, "%.0u", 1);
276 g_assert_cmpint (res, ==, 1);
277 g_assert_cmpstr (buf, ==, "1");
279 res = g_snprintf (buf, 128, "%.3u", 5);
280 g_assert_cmpint (res, ==, 3);
281 g_assert_cmpstr (buf, ==, "005");
283 res = g_snprintf (buf, 128, "%5.3u", 5);
284 g_assert_cmpint (res, ==, 5);
285 g_assert_cmpstr (buf, ==, " 005");
288 static void
289 test_x (void)
291 gchar buf[128];
292 gint res;
294 /* %x, basic formatting */
296 res = g_snprintf (buf, 128, "%x", 5);
297 g_assert_cmpint (res, ==, 1);
298 g_assert_cmpstr (buf, ==, "5");
300 res = g_snprintf (buf, 128, "%x", 31);
301 g_assert_cmpint (res, ==, 2);
302 g_assert_cmpstr (buf, ==, "1f");
304 res = g_snprintf (buf, 128, "%x", 0);
305 g_assert_cmpint (res, ==, 1);
306 g_assert_cmpstr (buf, ==, "0");
308 res = g_snprintf (buf, 128, "%.0x", 0);
309 g_assert_cmpint (res, ==, 0);
310 g_assert_cmpstr (buf, ==, "");
312 res = g_snprintf (buf, 128, "%.0x", 1);
313 g_assert_cmpint (res, ==, 1);
314 g_assert_cmpstr (buf, ==, "1");
316 res = g_snprintf (buf, 128, "%.3x", 5);
317 g_assert_cmpint (res, ==, 3);
318 g_assert_cmpstr (buf, ==, "005");
320 res = g_snprintf (buf, 128, "%.3x", 31);
321 g_assert_cmpint (res, ==, 3);
322 g_assert_cmpstr (buf, ==, "01f");
324 res = g_snprintf (buf, 128, "%5.3x", 5);
325 g_assert_cmpint (res, ==, 5);
326 g_assert_cmpstr (buf, ==, " 005");
328 /* %x, flags */
330 res = g_snprintf (buf, 128, "%-x", 5);
331 g_assert_cmpint (res, ==, 1);
332 g_assert_cmpstr (buf, ==, "5");
334 res = g_snprintf (buf, 128, "%03x", 5);
335 g_assert_cmpint (res, ==, 3);
336 g_assert_cmpstr (buf, ==, "005");
338 res = g_snprintf (buf, 128, "%#x", 31);
339 g_assert_cmpint (res, ==, 4);
340 g_assert_cmpstr (buf, ==, "0x1f");
342 res = g_snprintf (buf, 128, "%#x", 0);
343 g_assert_cmpint (res, ==, 1);
344 g_assert_cmpstr (buf, ==, "0");
347 static void
348 test_X (void)
350 gchar buf[128];
351 gint res;
353 /* %X, basic formatting */
355 res = g_snprintf (buf, 128, "%X", 5);
356 g_assert_cmpint (res, ==, 1);
357 g_assert_cmpstr (buf, ==, "5");
359 res = g_snprintf (buf, 128, "%X", 31);
360 g_assert_cmpint (res, ==, 2);
361 g_assert_cmpstr (buf, ==, "1F");
363 res = g_snprintf (buf, 128, "%X", 0);
364 g_assert_cmpint (res, ==, 1);
365 g_assert_cmpstr (buf, ==, "0");
367 res = g_snprintf (buf, 128, "%.0X", 0);
368 g_assert_cmpint (res, ==, 0);
369 g_assert_cmpstr (buf, ==, "");
371 res = g_snprintf (buf, 128, "%.0X", 1);
372 g_assert_cmpint (res, ==, 1);
373 g_assert_cmpstr (buf, ==, "1");
375 res = g_snprintf (buf, 128, "%.3X", 5);
376 g_assert_cmpint (res, ==, 3);
377 g_assert_cmpstr (buf, ==, "005");
379 res = g_snprintf (buf, 128, "%.3X", 31);
380 g_assert_cmpint (res, ==, 3);
381 g_assert_cmpstr (buf, ==, "01F");
383 res = g_snprintf (buf, 128, "%5.3X", 5);
384 g_assert_cmpint (res, ==, 5);
385 g_assert_cmpstr (buf, ==, " 005");
387 /* %X, flags */
389 res = g_snprintf (buf, 128, "%-X", 5);
390 g_assert_cmpint (res, ==, 1);
391 g_assert_cmpstr (buf, ==, "5");
393 res = g_snprintf (buf, 128, "%03X", 5);
394 g_assert_cmpint (res, ==, 3);
395 g_assert_cmpstr (buf, ==, "005");
397 res = g_snprintf (buf, 128, "%#X", 31);
398 g_assert_cmpint (res, ==, 4);
399 g_assert_cmpstr (buf, ==, "0X1F");
401 res = g_snprintf (buf, 128, "%#X", 0);
402 g_assert_cmpint (res, ==, 1);
403 g_assert_cmpstr (buf, ==, "0");
406 static void
407 test_f (void)
409 gchar buf[128];
410 gint res;
412 /* %f, basic formattting */
414 res = g_snprintf (buf, 128, "%f", G_PI);
415 g_assert_cmpint (res, ==, 8);
416 g_assert (0 == strncmp (buf, "3.14159", 7));
418 res = g_snprintf (buf, 128, "%.8f", G_PI);
419 g_assert_cmpint (res, ==, 10);
420 g_assert (0 == strncmp (buf, "3.1415926", 9));
422 res = g_snprintf (buf, 128, "%.0f", G_PI);
423 g_assert_cmpint (res, ==, 1);
424 g_assert_cmpstr (buf, ==, "3");
426 res = g_snprintf (buf, 128, "%1.f", G_PI);
427 g_assert_cmpint (res, ==, 1);
428 g_assert_cmpstr (buf, ==, "3");
430 res = g_snprintf (buf, 128, "%3.f", G_PI);
431 g_assert_cmpint (res, ==, 3);
432 g_assert_cmpstr (buf, ==, " 3");
434 /* %f, flags */
436 res = g_snprintf (buf, 128, "%+f", G_PI);
437 g_assert_cmpint (res, ==, 9);
438 g_assert (0 == strncmp (buf, "+3.14159", 8));
440 res = g_snprintf (buf, 128, "% f", G_PI);
441 g_assert_cmpint (res, ==, 9);
442 g_assert (0 == strncmp (buf, " 3.14159", 8));
444 res = g_snprintf (buf, 128, "%#.0f", G_PI);
445 g_assert_cmpint (res, ==, 2);
446 g_assert_cmpstr (buf, ==, "3.");
448 res = g_snprintf (buf, 128, "%05.2f", G_PI);
449 g_assert_cmpint (res, ==, 5);
450 g_assert_cmpstr (buf, ==, "03.14");
453 static gboolean
454 same_value (const gchar *actual,
455 const gchar *expected)
457 gdouble actual_value, expected_value;
459 actual_value = g_ascii_strtod (actual, NULL);
460 expected_value = g_ascii_strtod (expected, NULL);
462 return actual_value == expected_value;
465 static void
466 test_e (void)
468 gchar buf[128];
469 gint res;
471 /* %e, basic formatting */
472 /* for %e we can't expect to reproduce exact strings and lengths, since SUS
473 * only guarantees that the exponent shall always contain at least two
474 * digits. On Windows, it seems to be at least three digits long.
475 * Therefore, we compare the results of parsing the expected result and the
476 * actual result.
479 res = g_snprintf (buf, 128, "%e", G_PI);
480 g_assert_cmpint (res, >=, 12);
481 g_assert (same_value (buf, "3.141593e+00"));
483 res = g_snprintf (buf, 128, "%.8e", G_PI);
484 g_assert_cmpint (res, >=, 14);
485 g_assert (same_value (buf, "3.14159265e+00"));
487 res = g_snprintf (buf, 128, "%.0e", G_PI);
488 g_assert_cmpint (res, >=, 5);
489 g_assert (same_value (buf, "3e+00"));
491 res = g_snprintf (buf, 128, "%.1e", 0.0);
492 g_assert_cmpint (res, >=, 7);
493 g_assert (same_value (buf, "0.0e+00"));
495 res = g_snprintf (buf, 128, "%.1e", 0.00001);
496 g_assert_cmpint (res, >=, 7);
497 g_assert (same_value (buf, "1.0e-05"));
499 res = g_snprintf (buf, 128, "%.1e", 10000.0);
500 g_assert_cmpint (res, >=, 7);
501 g_assert (same_value (buf, "1.0e+04"));
503 /* %e, flags */
505 res = g_snprintf (buf, 128, "%+e", G_PI);
506 g_assert_cmpint (res, >=, 13);
507 g_assert (same_value (buf, "+3.141593e+00"));
509 res = g_snprintf (buf, 128, "% e", G_PI);
510 g_assert_cmpint (res, >=, 13);
511 g_assert (same_value (buf, " 3.141593e+00"));
513 res = g_snprintf (buf, 128, "%#.0e", G_PI);
514 g_assert_cmpint (res, >=, 6);
515 g_assert (same_value (buf, "3.e+00"));
517 res = g_snprintf (buf, 128, "%09.2e", G_PI);
518 g_assert_cmpint (res, >=, 9);
519 g_assert (same_value (buf, "03.14e+00"));
522 static void
523 test_c (void)
525 gchar buf[128];
526 gint res;
528 res = g_snprintf (buf, 128, "%c", 'a');
529 g_assert_cmpint (res, ==, 1);
530 g_assert_cmpstr (buf, ==, "a");
533 static void
534 test_s (void)
536 gchar buf[128];
537 gint res;
539 res = g_snprintf (buf, 128, "%.2s", "abc");
540 g_assert_cmpint (res, ==, 2);
541 g_assert_cmpstr (buf, ==, "ab");
543 res = g_snprintf (buf, 128, "%.6s", "abc");
544 g_assert_cmpint (res, ==, 3);
545 g_assert_cmpstr (buf, ==, "abc");
547 res = g_snprintf (buf, 128, "%5s", "abc");
548 g_assert_cmpint (res, ==, 5);
549 g_assert_cmpstr (buf, ==, " abc");
551 res = g_snprintf (buf, 128, "%-5s", "abc");
552 g_assert_cmpint (res, ==, 5);
553 g_assert_cmpstr (buf, ==, "abc ");
555 res = g_snprintf (buf, 128, "%5.2s", "abc");
556 g_assert_cmpint (res, ==, 5);
557 g_assert_cmpstr (buf, ==, " ab");
559 res = g_snprintf (buf, 128, "%*s", 5, "abc");
560 g_assert_cmpint (res, ==, 5);
561 g_assert_cmpstr (buf, ==, " abc");
563 res = g_snprintf (buf, 128, "%*s", -5, "abc");
564 g_assert_cmpint (res, ==, 5);
565 g_assert_cmpstr (buf, ==, "abc ");
567 res = g_snprintf (buf, 128, "%*.*s", 5, 2, "abc");
568 g_assert_cmpint (res, ==, 5);
569 g_assert_cmpstr (buf, ==, " ab");
572 static void
573 test_n (void)
575 gchar buf[128];
576 gint res;
577 gint i;
578 glong l;
580 res = g_snprintf (buf, 128, "abc%n", &i);
581 g_assert_cmpint (res, ==, 3);
582 g_assert_cmpstr (buf, ==, "abc");
583 g_assert_cmpint (i, ==, 3);
585 res = g_snprintf (buf, 128, "abc%ln", &l);
586 g_assert_cmpint (res, ==, 3);
587 g_assert_cmpstr (buf, ==, "abc");
588 g_assert_cmpint (l, ==, 3);
591 static void
592 test_percent (void)
594 gchar buf[128];
595 gint res;
597 res = g_snprintf (buf, 128, "%%");
598 g_assert_cmpint (res, ==, 1);
599 g_assert_cmpstr (buf, ==, "%");
602 static void
603 test_positional_params (void)
605 gchar buf[128];
606 gint res;
608 res = g_snprintf (buf, 128, "%2$c %1$c", 'b', 'a');
609 g_assert_cmpint (res, ==, 3);
610 g_assert_cmpstr (buf, ==, "a b");
612 res = g_snprintf (buf, 128, "%1$*2$.*3$s", "abc", 5, 2);
613 g_assert_cmpint (res, ==, 5);
614 g_assert_cmpstr (buf, ==, " ab");
616 res = g_snprintf (buf, 128, "%1$s%1$s", "abc");
617 g_assert_cmpint (res, ==, 6);
618 g_assert_cmpstr (buf, ==, "abcabc");
621 static void
622 test_positional_params2 (void)
624 if (g_test_subprocess ())
626 gint res;
628 res = g_printf ("%2$c %1$c\n", 'b', 'a');
629 g_assert_cmpint (res, ==, 4);
631 res = g_printf ("%1$*2$.*3$s\n", "abc", 5, 2);
632 g_assert_cmpint (res, ==, 6);
634 res = g_printf ("%1$s%1$s\n", "abc");
635 g_assert_cmpint (res, ==, 7);
636 return;
638 g_test_trap_subprocess (NULL, 0, 0);
639 g_test_trap_assert_passed ();
640 #ifndef G_OS_WIN32
641 g_test_trap_assert_stdout ("a b\n ab\nabcabc\n");
642 #else
643 g_test_trap_assert_stdout ("a b\r\n ab\r\nabcabc\r\n");
644 #endif
647 static void
648 test_positional_params3 (void)
650 gchar buf[128];
651 gint res;
653 res = g_sprintf (buf, "%2$c %1$c", 'b', 'a');
654 g_assert_cmpint (res, ==, 3);
655 g_assert_cmpstr (buf, ==, "a b");
657 res = g_sprintf (buf, "%1$*2$.*3$s", "abc", 5, 2);
658 g_assert_cmpint (res, ==, 5);
659 g_assert_cmpstr (buf, ==, " ab");
661 res = g_sprintf (buf, "%1$s%1$s", "abc");
662 g_assert_cmpint (res, ==, 6);
663 g_assert_cmpstr (buf, ==, "abcabc");
666 static void
667 test_percent2 (void)
669 if (g_test_subprocess ())
671 gint res;
673 res = g_printf ("%%");
674 g_assert_cmpint (res, ==, 1);
675 return;
677 g_test_trap_subprocess (NULL, 0, 0);
678 g_test_trap_assert_passed ();
679 g_test_trap_assert_stdout ("*%*");
682 static void
683 test_64bit (void)
685 gchar buf[128];
686 gint res;
688 res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)123456);
689 g_assert_cmpint (res, ==, 6);
690 g_assert_cmpstr (buf, ==, "123456");
692 res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-123456);
693 g_assert_cmpint (res, ==, 7);
694 g_assert_cmpstr (buf, ==, "-123456");
696 res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)123456);
697 g_assert_cmpint (res, ==, 6);
698 g_assert_cmpstr (buf, ==, "123456");
700 res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "o", (gint64)123456);
701 g_assert_cmpint (res, ==, 6);
702 g_assert_cmpstr (buf, ==, "361100");
704 res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "o", (gint64)123456);
705 g_assert_cmpint (res, ==, 7);
706 g_assert_cmpstr (buf, ==, "0361100");
708 res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "x", (gint64)123456);
709 g_assert_cmpint (res, ==, 5);
710 g_assert_cmpstr (buf, ==, "1e240");
712 res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "x", (gint64)123456);
713 g_assert_cmpint (res, ==, 7);
714 g_assert_cmpstr (buf, ==, "0x1e240");
716 res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "X", (gint64)123456);
717 g_assert_cmpint (res, ==, 5);
718 g_assert_cmpstr (buf, ==, "1E240");
720 #ifdef G_OS_WIN32
721 /* On Win32, test that the "ll" modifier also works, for backward
722 * compatibility. One really should use the G_GINT64_MODIFIER (which
723 * on Win32 is the "I64" that the (msvcrt) C library's printf uses),
724 * but "ll" used to work with the "trio" g_printf implementation in
725 * GLib 2.2, so it's best if it continues to work.
728 /* However, gcc doesn't know about this, so we need to disable printf
729 * format warnings...
731 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
732 _Pragma ("GCC diagnostic push")
733 _Pragma ("GCC diagnostic ignored \"-Wformat\"")
734 _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
735 #endif
737 res = g_snprintf (buf, 128, "%" "lli", (gint64)123456);
738 g_assert_cmpint (res, ==, 6);
739 g_assert_cmpstr (buf, ==, "123456");
741 res = g_snprintf (buf, 128, "%" "lli", (gint64)-123456);
742 g_assert_cmpint (res, ==, 7);
743 g_assert_cmpstr (buf, ==, "-123456");
745 res = g_snprintf (buf, 128, "%" "llu", (guint64)123456);
746 g_assert_cmpint (res, ==, 6);
747 g_assert_cmpstr (buf, ==, "123456");
749 res = g_snprintf (buf, 128, "%" "ll" "o", (gint64)123456);
750 g_assert_cmpint (res, ==, 6);
751 g_assert_cmpstr (buf, ==, "361100");
753 res = g_snprintf (buf, 128, "%#" "ll" "o", (gint64)123456);
754 g_assert_cmpint (res, ==, 7);
755 g_assert_cmpstr (buf, ==, "0361100");
757 res = g_snprintf (buf, 128, "%" "ll" "x", (gint64)123456);
758 g_assert_cmpint (res, ==, 5);
759 g_assert_cmpstr (buf, ==, "1e240");
761 res = g_snprintf (buf, 128, "%#" "ll" "x", (gint64)123456);
762 g_assert_cmpint (res, ==, 7);
763 g_assert_cmpstr (buf, ==, "0x1e240");
765 res = g_snprintf (buf, 128, "%" "ll" "X", (gint64)123456);
766 g_assert_cmpint (res, ==, 5);
767 g_assert_cmpstr (buf, ==, "1E240");
769 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
770 _Pragma ("GCC diagnostic pop")
771 #endif
773 #endif
776 static void
777 test_64bit2_base (void)
779 gint res;
781 res = g_printf ("%" G_GINT64_FORMAT "\n", (gint64)123456);
782 g_assert_cmpint (res, ==, 7);
784 res = g_printf ("%" G_GINT64_FORMAT "\n", (gint64)-123456);
785 g_assert_cmpint (res, ==, 8);
787 res = g_printf ("%" G_GUINT64_FORMAT "\n", (guint64)123456);
788 g_assert_cmpint (res, ==, 7);
790 res = g_printf ("%" G_GINT64_MODIFIER "o\n", (gint64)123456);
791 g_assert_cmpint (res, ==, 7);
793 res = g_printf ("%#" G_GINT64_MODIFIER "o\n", (gint64)123456);
794 g_assert_cmpint (res, ==, 8);
796 res = g_printf ("%" G_GINT64_MODIFIER "x\n", (gint64)123456);
797 g_assert_cmpint (res, ==, 6);
799 res = g_printf ("%#" G_GINT64_MODIFIER "x\n", (gint64)123456);
800 g_assert_cmpint (res, ==, 8);
802 res = g_printf ("%" G_GINT64_MODIFIER "X\n", (gint64)123456);
803 g_assert_cmpint (res, ==, 6);
806 #ifdef G_OS_WIN32
807 static void
808 test_64bit2_win32 (void)
810 gint res;
812 /* On Win32, test that the "ll" modifier also works, for backward
813 * compatibility. One really should use the G_GINT64_MODIFIER (which
814 * on Win32 is the "I64" that the (msvcrt) C library's printf uses),
815 * but "ll" used to work with the "trio" g_printf implementation in
816 * GLib 2.2, so it's best if it continues to work.
819 /* However, gcc doesn't know about this, so we need to disable printf
820 * format warnings...
822 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
823 _Pragma ("GCC diagnostic push")
824 _Pragma ("GCC diagnostic ignored \"-Wformat\"")
825 _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
826 #endif
828 res = g_printf ("%" "lli\n", (gint64)123456);
829 g_assert_cmpint (res, ==, 7);
831 res = g_printf ("%" "lli\n", (gint64)-123456);
832 g_assert_cmpint (res, ==, 8);
834 res = g_printf ("%" "llu\n", (guint64)123456);
835 g_assert_cmpint (res, ==, 7);
837 res = g_printf ("%" "ll" "o\n", (gint64)123456);
838 g_assert_cmpint (res, ==, 7);
840 res = g_printf ("%#" "ll" "o\n", (gint64)123456);
841 g_assert_cmpint (res, ==, 8);
843 res = g_printf ("%" "ll" "x\n", (gint64)123456);
844 g_assert_cmpint (res, ==, 6);
846 res = g_printf ("%#" "ll" "x\n", (gint64)123456);
847 g_assert_cmpint (res, ==, 8);
849 res = g_printf ("%" "ll" "X\n", (gint64)123456);
850 g_assert_cmpint (res, ==, 6);
852 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
853 _Pragma ("GCC diagnostic pop")
854 #endif
856 #endif
858 static void
859 test_64bit2 (void)
861 #ifndef G_OS_WIN32
862 g_test_trap_subprocess ("/printf/test-64bit/subprocess/base", 0, 0);
863 g_test_trap_assert_passed ();
864 g_test_trap_assert_stdout ("123456\n-123456\n123456\n"
865 "361100\n0361100\n1e240\n"
866 "0x1e240\n1E240\n");
868 #else
869 g_test_trap_subprocess ("/printf/test-64bit/subprocess/base", 0, 0);
870 g_test_trap_assert_passed ();
871 g_test_trap_assert_stdout ("123456\r\n-123456\r\n123456\r\n"
872 "361100\r\n0361100\r\n1e240\r\n"
873 "0x1e240\r\n1E240\r\n");
875 g_test_trap_subprocess ("/printf/test-64bit/subprocess/win32", 0, 0);
876 g_test_trap_assert_passed ();
877 g_test_trap_assert_stdout ("123456\r\n-123456\r\n123456\r\n"
878 "361100\r\n0361100\r\n1e240\r\n"
879 "0x1e240\r\n1E240\r\n");
880 #endif
883 G_GNUC_PRINTF(1, 2)
884 static gsize
885 upper_bound (const gchar *format, ...)
887 va_list args;
888 gsize res;
890 va_start (args, format);
891 res = g_printf_string_upper_bound (format, args);
892 va_end (args);
894 return res;
897 static void
898 test_upper_bound (void)
900 gsize res;
902 res = upper_bound ("bla %s %d: %g\n", "bla", 123, 0.123);
903 g_assert_cmpint (res, ==, 20);
907 main (int argc,
908 char *argv[])
910 g_test_init (&argc, &argv, NULL);
912 g_test_add_func ("/snprintf/retval-and-trunc", test_retval_and_trunc);
913 g_test_add_func ("/snprintf/%d", test_d);
914 g_test_add_func ("/snprintf/%d-invalid", test_d_invalid);
915 g_test_add_func ("/snprintf/%o", test_o);
916 g_test_add_func ("/snprintf/%u", test_u);
917 g_test_add_func ("/snprintf/%x", test_x);
918 g_test_add_func ("/snprintf/%X", test_X);
919 g_test_add_func ("/snprintf/%f", test_f);
920 g_test_add_func ("/snprintf/%e", test_e);
921 g_test_add_func ("/snprintf/%c", test_c);
922 g_test_add_func ("/snprintf/%s", test_s);
923 g_test_add_func ("/snprintf/%n", test_n);
924 g_test_add_func ("/snprintf/test-percent", test_percent);
925 g_test_add_func ("/snprintf/test-positional-params", test_positional_params);
926 g_test_add_func ("/snprintf/test-64bit", test_64bit);
928 g_test_add_func ("/printf/test-percent", test_percent2);
929 g_test_add_func ("/printf/test-positional-params", test_positional_params2);
930 g_test_add_func ("/printf/test-64bit", test_64bit2);
931 g_test_add_func ("/printf/test-64bit/subprocess/base", test_64bit2_base);
932 #ifdef G_OS_WIN32
933 g_test_add_func ("/printf/test-64bit/subprocess/win32", test_64bit2_win32);
934 #endif
936 g_test_add_func ("/sprintf/test-positional-params", test_positional_params3);
937 g_test_add_func ("/sprintf/upper-bound", test_upper_bound);
939 return g_test_run();