Require C90 compliance
[platform/upstream/glib.git] / glib / tests / test-printf.c
1 /* Unit tests for gprintf
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
7  *
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.
11  *
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.
20  */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "glib.h"
26 #include "gstdio.h"
27
28 static void
29 test_retval_and_trunc (void)
30 {
31   gchar buf[128];
32   gint res;
33
34   res = g_snprintf (buf, 0, "abc");
35   g_assert_cmpint (res, ==, 3);
36
37   res = g_snprintf (NULL, 0, "abc");
38   g_assert_cmpint (res, ==, 3);
39
40   res = g_snprintf (buf, 5, "abc");
41   g_assert_cmpint (res, ==, 3);
42
43   res = g_snprintf (buf, 1, "abc");
44   g_assert_cmpint (res, ==, 3);
45   g_assert (buf[0] == '\0');
46   g_assert_cmpstr (buf, ==, "");
47
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");
52
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");
57
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");
62
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");
67 }
68
69 static void
70 test_d (void)
71 {
72   gchar buf[128];
73   gint res;
74   const gchar *fmt;
75
76   /* %d basic formatting */
77
78   res = g_snprintf (buf, 128, "%d", 5);
79   g_assert_cmpint (res, ==, 1);
80   g_assert_cmpstr (buf, ==, "5");
81
82   res = g_snprintf (buf, 128, "%d", 0);
83   g_assert_cmpint (res, ==, 1);
84   g_assert_cmpstr (buf, ==, "0");
85
86   res = g_snprintf (buf, 128, "%.0d", 0);
87   g_assert_cmpint (res, ==, 0);
88   g_assert_cmpstr (buf, ==, "");
89
90   res = g_snprintf (buf, 128, "%.0d", 1);
91   g_assert_cmpint (res, ==, 1);
92   g_assert_cmpstr (buf, ==, "1");
93
94   res = g_snprintf (buf, 128, "%.d", 2);
95   g_assert_cmpint (res, ==, 1);
96   g_assert_cmpstr (buf, ==, "2");
97
98   res = g_snprintf (buf, 128, "%d", -1);
99   g_assert_cmpint (res, ==, 2);
100   g_assert_cmpstr (buf, ==, "-1");
101
102   res = g_snprintf (buf, 128, "%.3d", 5);
103   g_assert_cmpint (res, ==, 3);
104   g_assert_cmpstr (buf, ==, "005");
105
106   res = g_snprintf (buf, 128, "%.3d", -5);
107   g_assert_cmpint (res, ==, 4);
108   g_assert_cmpstr (buf, ==, "-005");
109
110   res = g_snprintf (buf, 128, "%5.3d", 5);
111   g_assert_cmpint (res, ==, 5);
112   g_assert_cmpstr (buf, ==, "  005");
113
114   res = g_snprintf (buf, 128, "%-5.3d", -5);
115   g_assert_cmpint (res, ==, 5);
116   g_assert_cmpstr (buf, ==, "-005 ");
117
118   /* %d, length modifiers */
119
120   res = g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5);
121   g_assert_cmpint (res, ==, 2);
122   g_assert_cmpstr (buf, ==, "-5");
123
124   res = g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5);
125   g_assert_cmpint (res, ==, 1);
126   g_assert_cmpstr (buf, ==, "5");
127
128   res = g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5);
129   g_assert_cmpint (res, ==, 2);
130   g_assert_cmpstr (buf, ==, "-5");
131
132   res = g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5);
133   g_assert_cmpint (res, ==, 1);
134   g_assert_cmpstr (buf, ==, "5");
135
136   res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5);
137   g_assert_cmpint (res, ==, 2);
138   g_assert_cmpstr (buf, ==, "-5");
139
140   res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5);
141   g_assert_cmpint (res, ==, 1);
142   g_assert_cmpstr (buf, ==, "5");
143
144   res = g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5);
145   g_assert_cmpint (res, ==, 2);
146   g_assert_cmpstr (buf, ==, "-5");
147
148   res = g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5);
149   g_assert_cmpint (res, ==, 1);
150   g_assert_cmpstr (buf, ==, "5");
151
152   /* %d, flags */
153
154   res = g_snprintf (buf, 128, "%-d", 5);
155   g_assert_cmpint (res, ==, 1);
156   g_assert_cmpstr (buf, ==, "5");
157
158   res = g_snprintf (buf, 128, "%-+d", 5);
159   g_assert_cmpint (res, ==, 2);
160   g_assert_cmpstr (buf, ==, "+5");
161
162   res = g_snprintf (buf, 128, "%+-d", 5);
163   g_assert_cmpint (res, ==, 2);
164   g_assert_cmpstr (buf, ==, "+5");
165
166   res = g_snprintf (buf, 128, "%+d", -5);
167   g_assert_cmpint (res, ==, 2);
168   g_assert_cmpstr (buf, ==, "-5");
169
170   res = g_snprintf (buf, 128, "% d", 5);
171   g_assert_cmpint (res, ==, 2);
172   g_assert_cmpstr (buf, ==, " 5");
173
174   res = g_snprintf (buf, 128, "% .0d", 0);
175   g_assert_cmpint (res, ==, 1);
176   g_assert_cmpstr (buf, ==, " ");
177
178   res = g_snprintf (buf, 128, "%03d", 5);
179   g_assert_cmpint (res, ==, 3);
180   g_assert_cmpstr (buf, ==, "005");
181
182   res = g_snprintf (buf, 128, "%03d", -5);
183   g_assert_cmpint (res, ==, 3);
184   g_assert_cmpstr (buf, ==, "-05");
185
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.
190    */
191   fmt = "% +d";
192   res = g_snprintf (buf, 128, fmt, 5);
193   g_assert_cmpint (res, ==, 2);
194   g_assert_cmpstr (buf, ==, "+5");
195
196   fmt = "%-03d";
197   res = g_snprintf (buf, 128, fmt, -5);
198   g_assert_cmpint (res, ==, 3);
199   g_assert_cmpstr (buf, ==, "-5 ");
200 }
201
202 static void
203 test_o (void)
204 {
205   gchar buf[128];
206   gint res;
207
208   /* %o basic formatting */
209
210   res = g_snprintf (buf, 128, "%o", 5);
211   g_assert_cmpint (res, ==, 1);
212   g_assert_cmpstr (buf, ==, "5");
213
214   res = g_snprintf (buf, 128, "%o", 8);
215   g_assert_cmpint (res, ==, 2);
216   g_assert_cmpstr (buf, ==, "10");
217
218   res = g_snprintf (buf, 128, "%o", 0);
219   g_assert_cmpint (res, ==, 1);
220   g_assert_cmpstr (buf, ==, "0");
221
222   res = g_snprintf (buf, 128, "%.0o", 0);
223   g_assert_cmpint (res, ==, 0);
224   g_assert_cmpstr (buf, ==, "");
225
226   res = g_snprintf (buf, 128, "%.0o", 1);
227   g_assert_cmpint (res, ==, 1);
228   g_assert_cmpstr (buf, ==, "1");
229
230   res = g_snprintf (buf, 128, "%.3o", 5);
231   g_assert_cmpint (res, ==, 3);
232   g_assert_cmpstr (buf, ==, "005");
233
234   res = g_snprintf (buf, 128, "%.3o", 8);
235   g_assert_cmpint (res, ==, 3);
236   g_assert_cmpstr (buf, ==, "010");
237
238   res = g_snprintf (buf, 128, "%5.3o", 5);
239   g_assert_cmpint (res, ==, 5);
240   g_assert_cmpstr (buf, ==, "  005");
241 }
242
243 static void
244 test_u (void)
245 {
246   gchar buf[128];
247   gint res;
248
249   /* %u, basic formatting */
250
251   res = g_snprintf (buf, 128, "%u", 5);
252   g_assert_cmpint (res, ==, 1);
253   g_assert_cmpstr (buf, ==, "5");
254
255   res = g_snprintf (buf, 128, "%u", 0);
256   g_assert_cmpint (res, ==, 1);
257   g_assert_cmpstr (buf, ==, "0");
258
259   res = g_snprintf (buf, 128, "%.0u", 0);
260   g_assert_cmpint (res, ==, 0);
261   g_assert_cmpstr (buf, ==, "");
262
263   res = g_snprintf (buf, 128, "%.0u", 1);
264   g_assert_cmpint (res, ==, 1);
265   g_assert_cmpstr (buf, ==, "1");
266
267   res = g_snprintf (buf, 128, "%.3u", 5);
268   g_assert_cmpint (res, ==, 3);
269   g_assert_cmpstr (buf, ==, "005");
270
271   res = g_snprintf (buf, 128, "%5.3u", 5);
272   g_assert_cmpint (res, ==, 5);
273   g_assert_cmpstr (buf, ==, "  005");
274 }
275
276 static void
277 test_x (void)
278 {
279   gchar buf[128];
280   gint res;
281
282   /* %x, basic formatting */
283
284   res = g_snprintf (buf, 128, "%x", 5);
285   g_assert_cmpint (res, ==, 1);
286   g_assert_cmpstr (buf, ==, "5");
287
288   res = g_snprintf (buf, 128, "%x", 31);
289   g_assert_cmpint (res, ==, 2);
290   g_assert_cmpstr (buf, ==, "1f");
291
292   res = g_snprintf (buf, 128, "%x", 0);
293   g_assert_cmpint (res, ==, 1);
294   g_assert_cmpstr (buf, ==, "0");
295
296   res = g_snprintf (buf, 128, "%.0x", 0);
297   g_assert_cmpint (res, ==, 0);
298   g_assert_cmpstr (buf, ==, "");
299
300   res = g_snprintf (buf, 128, "%.0x", 1);
301   g_assert_cmpint (res, ==, 1);
302   g_assert_cmpstr (buf, ==, "1");
303
304   res = g_snprintf (buf, 128, "%.3x", 5);
305   g_assert_cmpint (res, ==, 3);
306   g_assert_cmpstr (buf, ==, "005");
307
308   res = g_snprintf (buf, 128, "%.3x", 31);
309   g_assert_cmpint (res, ==, 3);
310   g_assert_cmpstr (buf, ==, "01f");
311
312   res = g_snprintf (buf, 128, "%5.3x", 5);
313   g_assert_cmpint (res, ==, 5);
314   g_assert_cmpstr (buf, ==, "  005");
315
316   /* %x, flags */
317
318   res = g_snprintf (buf, 128, "%-x", 5);
319   g_assert_cmpint (res, ==, 1);
320   g_assert_cmpstr (buf, ==, "5");
321
322   res = g_snprintf (buf, 128, "%03x", 5);
323   g_assert_cmpint (res, ==, 3);
324   g_assert_cmpstr (buf, ==, "005");
325
326   res = g_snprintf (buf, 128, "%#x", 31);
327   g_assert_cmpint (res, ==, 4);
328   g_assert_cmpstr (buf, ==, "0x1f");
329
330   res = g_snprintf (buf, 128, "%#x", 0);
331   g_assert_cmpint (res, ==, 1);
332   g_assert_cmpstr (buf, ==, "0");
333 }
334
335 static void
336 test_X (void)
337 {
338   gchar buf[128];
339   gint res;
340
341   /* %X, basic formatting */
342
343   res = g_snprintf (buf, 128, "%X", 5);
344   g_assert_cmpint (res, ==, 1);
345   g_assert_cmpstr (buf, ==, "5");
346
347   res = g_snprintf (buf, 128, "%X", 31);
348   g_assert_cmpint (res, ==, 2);
349   g_assert_cmpstr (buf, ==, "1F");
350
351   res = g_snprintf (buf, 128, "%X", 0);
352   g_assert_cmpint (res, ==, 1);
353   g_assert_cmpstr (buf, ==, "0");
354
355   res = g_snprintf (buf, 128, "%.0X", 0);
356   g_assert_cmpint (res, ==, 0);
357   g_assert_cmpstr (buf, ==, "");
358
359   res = g_snprintf (buf, 128, "%.0X", 1);
360   g_assert_cmpint (res, ==, 1);
361   g_assert_cmpstr (buf, ==, "1");
362
363   res = g_snprintf (buf, 128, "%.3X", 5);
364   g_assert_cmpint (res, ==, 3);
365   g_assert_cmpstr (buf, ==, "005");
366
367   res = g_snprintf (buf, 128, "%.3X", 31);
368   g_assert_cmpint (res, ==, 3);
369   g_assert_cmpstr (buf, ==, "01F");
370
371   res = g_snprintf (buf, 128, "%5.3X", 5);
372   g_assert_cmpint (res, ==, 5);
373   g_assert_cmpstr (buf, ==, "  005");
374
375   /* %X, flags */
376
377   res = g_snprintf (buf, 128, "%-X", 5);
378   g_assert_cmpint (res, ==, 1);
379   g_assert_cmpstr (buf, ==, "5");
380
381   res = g_snprintf (buf, 128, "%03X", 5);
382   g_assert_cmpint (res, ==, 3);
383   g_assert_cmpstr (buf, ==, "005");
384
385   res = g_snprintf (buf, 128, "%#X", 31);
386   g_assert_cmpint (res, ==, 4);
387   g_assert_cmpstr (buf, ==, "0X1F");
388
389   res = g_snprintf (buf, 128, "%#X", 0);
390   g_assert_cmpint (res, ==, 1);
391   g_assert_cmpstr (buf, ==, "0");
392 }
393
394 static void
395 test_f (void)
396 {
397   gchar buf[128];
398   gint res;
399
400   /* %f, basic formattting */
401
402   res = g_snprintf (buf, 128, "%f", G_PI);
403   g_assert_cmpint (res, ==, 8);
404   g_assert (0 == strncmp (buf, "3.14159", 7));
405
406   res = g_snprintf (buf, 128, "%.8f", G_PI);
407   g_assert_cmpint (res, ==, 10);
408   g_assert (0 == strncmp (buf, "3.1415926", 9));
409
410   res = g_snprintf (buf, 128, "%.0f", G_PI);
411   g_assert_cmpint (res, ==, 1);
412   g_assert_cmpstr (buf, ==, "3");
413
414   res = g_snprintf (buf, 128, "%1.f", G_PI);
415   g_assert_cmpint (res, ==, 1);
416   g_assert_cmpstr (buf, ==, "3");
417
418   res = g_snprintf (buf, 128, "%3.f", G_PI);
419   g_assert_cmpint (res, ==, 3);
420   g_assert_cmpstr (buf, ==, "  3");
421
422   /* %f, flags */
423
424   res = g_snprintf (buf, 128, "%+f", G_PI);
425   g_assert_cmpint (res, ==, 9);
426   g_assert (0 == strncmp (buf, "+3.14159", 8));
427
428   res = g_snprintf (buf, 128, "% f", G_PI);
429   g_assert_cmpint (res, ==, 9);
430   g_assert (0 == strncmp (buf, " 3.14159", 8));
431
432   res = g_snprintf (buf, 128, "%#.0f", G_PI);
433   g_assert_cmpint (res, ==, 2);
434   g_assert_cmpstr (buf, ==, "3.");
435
436   res = g_snprintf (buf, 128, "%05.2f", G_PI);
437   g_assert_cmpint (res, ==, 5);
438   g_assert_cmpstr (buf, ==, "03.14");
439 }
440
441 static gboolean
442 same_value (const gchar *actual, 
443             const gchar *expected)
444 {
445   gdouble actual_value, expected_value;
446
447   actual_value = g_ascii_strtod (actual, NULL);
448   expected_value = g_ascii_strtod (expected, NULL);
449
450   return actual_value == expected_value;
451 }
452
453 static void
454 test_e (void)
455 {
456   gchar buf[128];
457   gint res;
458
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
464    * actual result.
465    */
466
467   res = g_snprintf (buf, 128, "%e", G_PI);
468   g_assert_cmpint (res, >=, 12);
469   g_assert (same_value (buf, "3.141593e+00"));
470
471   res = g_snprintf (buf, 128, "%.8e", G_PI);
472   g_assert_cmpint (res, >=, 14);
473   g_assert (same_value (buf, "3.14159265e+00"));
474
475   res = g_snprintf (buf, 128, "%.0e", G_PI);
476   g_assert_cmpint (res, >=, 5);
477   g_assert (same_value (buf, "3e+00"));
478
479   res = g_snprintf (buf, 128, "%.1e", 0.0);
480   g_assert_cmpint (res, >=, 7);
481   g_assert (same_value (buf, "0.0e+00"));
482
483   res = g_snprintf (buf, 128, "%.1e", 0.00001);
484   g_assert_cmpint (res, >=, 7);
485   g_assert (same_value (buf, "1.0e-05"));
486
487   res = g_snprintf (buf, 128, "%.1e", 10000.0);
488   g_assert_cmpint (res, >=, 7);
489   g_assert (same_value (buf, "1.0e+04"));
490
491   /* %e, flags */
492
493   res = g_snprintf (buf, 128, "%+e", G_PI);
494   g_assert_cmpint (res, >=, 13);
495   g_assert (same_value (buf, "+3.141593e+00"));
496
497   res = g_snprintf (buf, 128, "% e", G_PI);
498   g_assert_cmpint (res, >=, 13);
499   g_assert (same_value (buf, " 3.141593e+00"));
500
501   res = g_snprintf (buf, 128, "%#.0e", G_PI);
502   g_assert_cmpint (res, >=, 6);
503   g_assert (same_value (buf, "3.e+00"));
504
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"));
508 }
509
510 static void
511 test_c (void)
512 {
513   gchar buf[128];
514   gint res;
515
516   res = g_snprintf (buf, 128, "%c", 'a');
517   g_assert_cmpint (res, ==, 1);
518   g_assert_cmpstr (buf, ==, "a");
519 }
520
521 static void
522 test_s (void)
523 {
524   gchar buf[128];
525   gint res;
526
527   res = g_snprintf (buf, 128, "%.2s", "abc");
528   g_assert_cmpint (res, ==, 2);
529   g_assert_cmpstr (buf, ==, "ab");
530
531   res = g_snprintf (buf, 128, "%.6s", "abc");
532   g_assert_cmpint (res, ==, 3);
533   g_assert_cmpstr (buf, ==, "abc");
534
535   res = g_snprintf (buf, 128, "%5s", "abc");
536   g_assert_cmpint (res, ==, 5);
537   g_assert_cmpstr (buf, ==, "  abc");
538
539   res = g_snprintf (buf, 128, "%-5s", "abc");
540   g_assert_cmpint (res, ==, 5);
541   g_assert_cmpstr (buf, ==, "abc  ");
542
543   res = g_snprintf (buf, 128, "%5.2s", "abc");
544   g_assert_cmpint (res, ==, 5);
545   g_assert_cmpstr (buf, ==, "   ab");
546
547   res = g_snprintf (buf, 128, "%*s", 5, "abc");
548   g_assert_cmpint (res, ==, 5);
549   g_assert_cmpstr (buf, ==, "  abc");
550
551   res = g_snprintf (buf, 128, "%*s", -5, "abc");
552   g_assert_cmpint (res, ==, 5);
553   g_assert_cmpstr (buf, ==, "abc  ");
554
555   res = g_snprintf (buf, 128, "%*.*s", 5, 2, "abc");
556   g_assert_cmpint (res, ==, 5);
557   g_assert_cmpstr (buf, ==, "   ab");
558 }
559
560 static void
561 test_n (void)
562 {
563   gchar buf[128];
564   gint res;
565   gint i;
566   glong l;
567
568   res = g_snprintf (buf, 128, "abc%n", &i);
569   g_assert_cmpint (res, ==, 3);
570   g_assert_cmpstr (buf, ==, "abc");
571   g_assert_cmpint (i, ==, 3);
572
573   res = g_snprintf (buf, 128, "abc%ln", &l);
574   g_assert_cmpint (res, ==, 3);
575   g_assert_cmpstr (buf, ==, "abc");
576   g_assert_cmpint (l, ==, 3);
577 }
578
579 static void
580 test_percent (void)
581 {
582   gchar buf[128];
583   gint res;
584
585   res = g_snprintf (buf, 128, "%%");
586   g_assert_cmpint (res, ==, 1);
587   g_assert_cmpstr (buf, ==, "%");
588 }
589
590 static void
591 test_positional_params (void)
592 {
593   gchar buf[128];
594   gint res;
595
596   res = g_snprintf (buf, 128, "%2$c %1$c", 'b', 'a');
597   g_assert_cmpint (res, ==, 3);
598   g_assert_cmpstr (buf, ==, "a b");
599
600   res = g_snprintf (buf, 128, "%1$*2$.*3$s", "abc", 5, 2);
601   g_assert_cmpint (res, ==, 5);
602   g_assert_cmpstr (buf, ==, "   ab");
603
604   res = g_snprintf (buf, 128, "%1$s%1$s", "abc");
605   g_assert_cmpint (res, ==, 6);
606   g_assert_cmpstr (buf, ==, "abcabc");
607 }
608
609 static void
610 test_positional_params2_subprocess (void)
611 {
612   gint res;
613
614   res = g_printf ("%2$c %1$c\n", 'b', 'a');
615   g_assert_cmpint (res, ==, 4);
616
617   res = g_printf ("%1$*2$.*3$s\n", "abc", 5, 2);
618   g_assert_cmpint (res, ==, 6);
619
620   res = g_printf ("%1$s%1$s\n", "abc");
621   g_assert_cmpint (res, ==, 7);
622 }
623
624 static void
625 test_positional_params2 (void)
626 {
627   g_test_trap_subprocess ("/printf/test-positional-params/subprocess", 0, 0);
628   g_test_trap_assert_passed ();
629   g_test_trap_assert_stdout ("a b\n   ab\nabcabc\n");
630 }
631
632 static void
633 test_positional_params3 (void)
634 {
635   gchar buf[128];
636   gint res;
637
638   res = g_sprintf (buf, "%2$c %1$c", 'b', 'a');
639   g_assert_cmpint (res, ==, 3);
640   g_assert_cmpstr (buf, ==, "a b");
641
642   res = g_sprintf (buf, "%1$*2$.*3$s", "abc", 5, 2);
643   g_assert_cmpint (res, ==, 5);
644   g_assert_cmpstr (buf, ==, "   ab");
645
646   res = g_sprintf (buf, "%1$s%1$s", "abc");
647   g_assert_cmpint (res, ==, 6);
648   g_assert_cmpstr (buf, ==, "abcabc");
649 }
650
651 static void
652 test_percent2_subprocess (void)
653 {
654   gint res;
655
656   res = g_printf ("%%");
657   g_assert_cmpint (res, ==, 1);
658 }
659
660 static void
661 test_percent2 (void)
662 {
663   g_test_trap_subprocess ("/printf/test-percent/subprocess", 0, 0);
664   g_test_trap_assert_passed ();
665   g_test_trap_assert_stdout ("*%*");
666 }
667
668 static void
669 test_64bit (void)
670 {
671   gchar buf[128];
672   gint res;
673
674   res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)123456);
675   g_assert_cmpint (res, ==, 6);
676   g_assert_cmpstr (buf, ==, "123456");
677
678   res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-123456);
679   g_assert_cmpint (res, ==, 7);
680   g_assert_cmpstr (buf, ==, "-123456");
681
682   res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)123456);
683   g_assert_cmpint (res, ==, 6);
684   g_assert_cmpstr (buf, ==, "123456");
685
686   res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "o", (gint64)123456);
687   g_assert_cmpint (res, ==, 6);
688   g_assert_cmpstr (buf, ==, "361100");
689
690   res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "o", (gint64)123456);
691   g_assert_cmpint (res, ==, 7);
692   g_assert_cmpstr (buf, ==, "0361100");
693
694   res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "x", (gint64)123456);
695   g_assert_cmpint (res, ==, 5);
696   g_assert_cmpstr (buf, ==, "1e240");
697
698   res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "x", (gint64)123456);
699   g_assert_cmpint (res, ==, 7);
700   g_assert_cmpstr (buf, ==, "0x1e240");
701
702   res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "X", (gint64)123456);
703   g_assert_cmpint (res, ==, 5);
704   g_assert_cmpstr (buf, ==, "1E240");
705
706 #ifdef G_OS_WIN32
707   /* On Win32, test that the "ll" modifier also works, for backward
708    * compatibility. One really should use the G_GINT64_MODIFIER (which
709    * on Win32 is the "I64" that the (msvcrt) C library's printf uses),
710    * but "ll" used to work with the "trio" g_printf implementation in
711    * GLib 2.2, so it's best if it continues to work.
712    */
713
714   /* However, gcc doesn't know about this, so we need to disable printf
715    * format warnings...
716    */
717 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
718 _Pragma ("GCC diagnostic push")
719 _Pragma ("GCC diagnostic ignored \"-Wformat\"")
720 _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
721 #endif
722
723   res = g_snprintf (buf, 128, "%" "lli", (gint64)123456);
724   g_assert_cmpint (res, ==, 6);
725   g_assert_cmpstr (buf, ==, "123456");
726
727   res = g_snprintf (buf, 128, "%" "lli", (gint64)-123456);
728   g_assert_cmpint (res, ==, 7);
729   g_assert_cmpstr (buf, ==, "-123456");
730
731   res = g_snprintf (buf, 128, "%" "llu", (guint64)123456);
732   g_assert_cmpint (res, ==, 6);
733   g_assert_cmpstr (buf, ==, "123456");
734
735   res = g_snprintf (buf, 128, "%" "ll" "o", (gint64)123456);
736   g_assert_cmpint (res, ==, 6);
737   g_assert_cmpstr (buf, ==, "361100");
738
739   res = g_snprintf (buf, 128, "%#" "ll" "o", (gint64)123456);
740   g_assert_cmpint (res, ==, 7);
741   g_assert_cmpstr (buf, ==, "0361100");
742
743   res = g_snprintf (buf, 128, "%" "ll" "x", (gint64)123456);
744   g_assert_cmpint (res, ==, 5);
745   g_assert_cmpstr (buf, ==, "1e240");
746
747   res = g_snprintf (buf, 128, "%#" "ll" "x", (gint64)123456);
748   g_assert_cmpint (res, ==, 7);
749   g_assert_cmpstr (buf, ==, "0x1e240");
750
751   res = g_snprintf (buf, 128, "%" "ll" "X", (gint64)123456);
752   g_assert_cmpint (res, ==, 5);
753   g_assert_cmpstr (buf, ==, "1E240");
754
755 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
756 _Pragma ("GCC diagnostic pop")
757 #endif
758
759 #endif
760 }
761
762 static void
763 test_64bit2_base (void)
764 {
765   gint res;
766
767   res = g_printf ("%" G_GINT64_FORMAT "\n", (gint64)123456);
768   g_assert_cmpint (res, ==, 7);
769
770   res = g_printf ("%" G_GINT64_FORMAT "\n", (gint64)-123456);
771   g_assert_cmpint (res, ==, 8);
772
773   res = g_printf ("%" G_GUINT64_FORMAT "\n", (guint64)123456);
774   g_assert_cmpint (res, ==, 7);
775
776   res = g_printf ("%" G_GINT64_MODIFIER "o\n", (gint64)123456);
777   g_assert_cmpint (res, ==, 7);
778
779   res = g_printf ("%#" G_GINT64_MODIFIER "o\n", (gint64)123456);
780   g_assert_cmpint (res, ==, 8);
781
782   res = g_printf ("%" G_GINT64_MODIFIER "x\n", (gint64)123456);
783   g_assert_cmpint (res, ==, 6);
784
785   res = g_printf ("%#" G_GINT64_MODIFIER "x\n", (gint64)123456);
786   g_assert_cmpint (res, ==, 8);
787
788   res = g_printf ("%" G_GINT64_MODIFIER "X\n", (gint64)123456);
789   g_assert_cmpint (res, ==, 6);
790 }
791
792 #ifdef G_OS_WIN32
793 static void
794 test_64bit2_win32 (void)
795 {
796   gint res;
797
798   /* On Win32, test that the "ll" modifier also works, for backward
799    * compatibility. One really should use the G_GINT64_MODIFIER (which
800    * on Win32 is the "I64" that the (msvcrt) C library's printf uses),
801    * but "ll" used to work with the "trio" g_printf implementation in
802    * GLib 2.2, so it's best if it continues to work.
803    */
804
805   /* However, gcc doesn't know about this, so we need to disable printf
806    * format warnings...
807    */
808 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
809 _Pragma ("GCC diagnostic push")
810 _Pragma ("GCC diagnostic ignored \"-Wformat\"")
811 _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
812 #endif
813
814   res = g_printf ("%" "lli\n", (gint64)123456);
815   g_assert_cmpint (res, ==, 7);
816
817   res = g_printf ("%" "lli\n", (gint64)-123456);
818   g_assert_cmpint (res, ==, 8);
819
820   res = g_printf ("%" "llu\n", (guint64)123456);
821   g_assert_cmpint (res, ==, 7);
822
823   res = g_printf ("%" "ll" "o\n", (gint64)123456);
824   g_assert_cmpint (res, ==, 7);
825
826   res = g_printf ("%#" "ll" "o\n", (gint64)123456);
827   g_assert_cmpint (res, ==, 8);
828
829   res = g_printf ("%" "ll" "x\n", (gint64)123456);
830   g_assert_cmpint (res, ==, 6);
831
832   res = g_printf ("%#" "ll" "x\n", (gint64)123456);
833   g_assert_cmpint (res, ==, 8);
834
835   res = g_printf ("%" "ll" "X\n", (gint64)123456);
836   g_assert_cmpint (res, ==, 6);
837
838 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
839 _Pragma ("GCC diagnostic pop")
840 #endif
841 }
842 #endif
843
844 static void
845 test_64bit2 (void)
846 {
847   g_test_trap_subprocess ("/printf/test-64bit/subprocess/base", 0, 0);
848   g_test_trap_assert_passed ();
849   g_test_trap_assert_stdout ("123456\n-123456\n123456\n"
850                              "361100\n0361100\n1e240\n"
851                              "0x1e240\n1E240\n");
852
853 #ifdef G_OS_WIN32
854   g_test_trap_subprocess ("/printf/test-64bit/subprocess/win32", 0, 0);
855   g_test_trap_assert_passed ();
856   g_test_trap_assert_stdout ("123456\n-123456\n123456\n"
857                              "361100\n0361100\n1e240\n"
858                              "0x1e240\n1E240\n");
859 #endif
860 }
861
862 G_GNUC_PRINTF(1, 2)
863 static gsize
864 upper_bound (const gchar *format, ...)
865 {
866   va_list args;
867   gsize res;
868
869   va_start (args, format);
870   res = g_printf_string_upper_bound (format, args);
871   va_end (args);
872
873   return res;
874 }
875
876 static void
877 test_upper_bound (void)
878 {
879   gsize res;
880
881   res = upper_bound ("bla %s %d: %g\n", "bla", 123, 0.123);
882   g_assert_cmpint (res, ==, 20);
883 }
884
885 int
886 main (int   argc,
887       char *argv[])
888 {
889   g_test_init (&argc, &argv, NULL);
890
891   g_test_add_func ("/snprintf/retval-and-trunc", test_retval_and_trunc);
892   g_test_add_func ("/snprintf/%d", test_d);
893   g_test_add_func ("/snprintf/%o", test_o);
894   g_test_add_func ("/snprintf/%u", test_u);
895   g_test_add_func ("/snprintf/%x", test_x);
896   g_test_add_func ("/snprintf/%X", test_X);
897   g_test_add_func ("/snprintf/%f", test_f);
898   g_test_add_func ("/snprintf/%e", test_e);
899   g_test_add_func ("/snprintf/%c", test_c);
900   g_test_add_func ("/snprintf/%s", test_s);
901   g_test_add_func ("/snprintf/%n", test_n);
902   g_test_add_func ("/snprintf/test-percent", test_percent);
903   g_test_add_func ("/snprintf/test-positional-params", test_positional_params);
904   g_test_add_func ("/snprintf/test-64bit", test_64bit);
905
906   g_test_add_func ("/printf/test-percent", test_percent2);
907   g_test_add_func ("/printf/test-percent/subprocess", test_percent2_subprocess);
908   g_test_add_func ("/printf/test-positional-params", test_positional_params2);
909   g_test_add_func ("/printf/test-positional-params/subprocess", test_positional_params2_subprocess);
910   g_test_add_func ("/printf/test-64bit", test_64bit2);
911   g_test_add_func ("/printf/test-64bit/subprocess/base", test_64bit2_base);
912 #ifdef G_OS_WIN32
913   g_test_add_func ("/printf/test-64bit/subprocess/win32", test_64bit2_win32);
914 #endif
915
916   g_test_add_func ("/sprintf/test-positional-params", test_positional_params3);
917   g_test_add_func ("/sprintf/upper-bound", test_upper_bound);
918
919   return g_test_run();
920 }