File: test-printf.c

package info (click to toggle)
glib2.0 2.58.3-2%2Bdeb10u2
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 48,956 kB
  • sloc: ansic: 452,656; xml: 16,781; python: 6,149; makefile: 3,776; sh: 1,499; perl: 1,140; cpp: 9
file content (939 lines) | stat: -rw-r--r-- 25,035 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
/* Unit tests for gprintf
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "glib.h"
#include "gstdio.h"
#ifdef G_OS_WIN32
#include <io.h>
#include <fcntl.h>
#endif

static void
test_retval_and_trunc (void)
{
  gchar buf[128];
  gint res;

  res = g_snprintf (buf, 0, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (NULL, 0, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (buf, 5, "abc");
  g_assert_cmpint (res, ==, 3);

  res = g_snprintf (buf, 1, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[0] == '\0');
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 2, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[1] == '\0');
  g_assert_cmpstr (buf, ==, "a");

  res = g_snprintf (buf, 3, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[2] == '\0');
  g_assert_cmpstr (buf, ==, "ab");

  res = g_snprintf (buf, 4, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[3] == '\0');
  g_assert_cmpstr (buf, ==, "abc");

  res = g_snprintf (buf, 5, "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert (buf[3] == '\0');
  g_assert_cmpstr (buf, ==, "abc");
}

static void
test_d (void)
{
  gchar buf[128];
  gint res;

  /* %d basic formatting */

  res = g_snprintf (buf, 128, "%d", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%d", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");

  res = g_snprintf (buf, 128, "%.0d", 0);
  g_assert_cmpint (res, ==, 0);
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 128, "%.0d", 1);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "1");

  res = g_snprintf (buf, 128, "%.d", 2);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "2");

  res = g_snprintf (buf, 128, "%d", -1);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-1");

  res = g_snprintf (buf, 128, "%.3d", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%.3d", -5);
  g_assert_cmpint (res, ==, 4);
  g_assert_cmpstr (buf, ==, "-005");

  res = g_snprintf (buf, 128, "%5.3d", 5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  005");

  res = g_snprintf (buf, 128, "%-5.3d", -5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "-005 ");

  /* %d, length modifiers */

  res = g_snprintf (buf, 128, "%" G_GINT16_FORMAT, (gint16)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT16_FORMAT, (guint16)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GINT32_FORMAT, (gint32)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT32_FORMAT, (guint32)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%" G_GSSIZE_FORMAT, (gssize)-5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "%" G_GSIZE_FORMAT, (gsize)5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  /* %d, flags */

  res = g_snprintf (buf, 128, "%-d", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%-+d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");

  res = g_snprintf (buf, 128, "%+-d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");

  res = g_snprintf (buf, 128, "%+d", -5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "-5");

  res = g_snprintf (buf, 128, "% d", 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, " 5");

  res = g_snprintf (buf, 128, "% .0d", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, " ");

  res = g_snprintf (buf, 128, "%03d", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%03d", -5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "-05");
}

/* gcc emits warnings for the following formats, since the C spec
 * says some of the flags must be ignored. (The " " in "% +d" and
 * the "0" in "%-03d".) But we need to test that our printf gets
 * those rules right. So we fool gcc into not warning.
 *
 * These have to be in a separate function in order to use #pragma.
 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
static void
test_d_invalid (void)
{
  const gchar *fmt;
  gchar buf[128];
  gint res;

  fmt = "% +d";
  res = g_snprintf (buf, 128, fmt, 5);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "+5");

  fmt = "%-03d";
  res = g_snprintf (buf, 128, fmt, -5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "-5 ");
}
#pragma GCC diagnostic pop

static void
test_o (void)
{
  gchar buf[128];
  gint res;

  /* %o basic formatting */

  res = g_snprintf (buf, 128, "%o", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%o", 8);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "10");

  res = g_snprintf (buf, 128, "%o", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");

  res = g_snprintf (buf, 128, "%.0o", 0);
  g_assert_cmpint (res, ==, 0);
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 128, "%.0o", 1);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "1");

  res = g_snprintf (buf, 128, "%.3o", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%.3o", 8);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "010");

  res = g_snprintf (buf, 128, "%5.3o", 5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  005");
}

static void
test_u (void)
{
  gchar buf[128];
  gint res;

  /* %u, basic formatting */

  res = g_snprintf (buf, 128, "%u", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%u", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");

  res = g_snprintf (buf, 128, "%.0u", 0);
  g_assert_cmpint (res, ==, 0);
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 128, "%.0u", 1);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "1");

  res = g_snprintf (buf, 128, "%.3u", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%5.3u", 5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  005");
}

static void
test_x (void)
{
  gchar buf[128];
  gint res;

  /* %x, basic formatting */

  res = g_snprintf (buf, 128, "%x", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%x", 31);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "1f");

  res = g_snprintf (buf, 128, "%x", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");

  res = g_snprintf (buf, 128, "%.0x", 0);
  g_assert_cmpint (res, ==, 0);
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 128, "%.0x", 1);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "1");

  res = g_snprintf (buf, 128, "%.3x", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%.3x", 31);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "01f");

  res = g_snprintf (buf, 128, "%5.3x", 5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  005");

  /* %x, flags */

  res = g_snprintf (buf, 128, "%-x", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%03x", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%#x", 31);
  g_assert_cmpint (res, ==, 4);
  g_assert_cmpstr (buf, ==, "0x1f");

  res = g_snprintf (buf, 128, "%#x", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");
}

static void
test_X (void)
{
  gchar buf[128];
  gint res;

  /* %X, basic formatting */

  res = g_snprintf (buf, 128, "%X", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%X", 31);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "1F");

  res = g_snprintf (buf, 128, "%X", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");

  res = g_snprintf (buf, 128, "%.0X", 0);
  g_assert_cmpint (res, ==, 0);
  g_assert_cmpstr (buf, ==, "");

  res = g_snprintf (buf, 128, "%.0X", 1);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "1");

  res = g_snprintf (buf, 128, "%.3X", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%.3X", 31);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "01F");

  res = g_snprintf (buf, 128, "%5.3X", 5);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  005");

  /* %X, flags */

  res = g_snprintf (buf, 128, "%-X", 5);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "5");

  res = g_snprintf (buf, 128, "%03X", 5);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "005");

  res = g_snprintf (buf, 128, "%#X", 31);
  g_assert_cmpint (res, ==, 4);
  g_assert_cmpstr (buf, ==, "0X1F");

  res = g_snprintf (buf, 128, "%#X", 0);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "0");
}

static void
test_f (void)
{
  gchar buf[128];
  gint res;

  /* %f, basic formattting */

  res = g_snprintf (buf, 128, "%f", G_PI);
  g_assert_cmpint (res, ==, 8);
  g_assert (0 == strncmp (buf, "3.14159", 7));

  res = g_snprintf (buf, 128, "%.8f", G_PI);
  g_assert_cmpint (res, ==, 10);
  g_assert (0 == strncmp (buf, "3.1415926", 9));

  res = g_snprintf (buf, 128, "%.0f", G_PI);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "3");

  res = g_snprintf (buf, 128, "%1.f", G_PI);
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "3");

  res = g_snprintf (buf, 128, "%3.f", G_PI);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "  3");

  /* %f, flags */

  res = g_snprintf (buf, 128, "%+f", G_PI);
  g_assert_cmpint (res, ==, 9);
  g_assert (0 == strncmp (buf, "+3.14159", 8));

  res = g_snprintf (buf, 128, "% f", G_PI);
  g_assert_cmpint (res, ==, 9);
  g_assert (0 == strncmp (buf, " 3.14159", 8));

  res = g_snprintf (buf, 128, "%#.0f", G_PI);
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "3.");

  res = g_snprintf (buf, 128, "%05.2f", G_PI);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "03.14");
}

static gboolean
same_value (const gchar *actual, 
            const gchar *expected)
{
  gdouble actual_value, expected_value;

  actual_value = g_ascii_strtod (actual, NULL);
  expected_value = g_ascii_strtod (expected, NULL);

  return actual_value == expected_value;
}

static void
test_e (void)
{
  gchar buf[128];
  gint res;

  /* %e, basic formatting */
  /* for %e we can't expect to reproduce exact strings and lengths, since SUS
   * only guarantees that the exponent shall always contain at least two 
   * digits. On Windows, it seems to be at least three digits long.
   * Therefore, we compare the results of parsing the expected result and the
   * actual result.
   */

  res = g_snprintf (buf, 128, "%e", G_PI);
  g_assert_cmpint (res, >=, 12);
  g_assert (same_value (buf, "3.141593e+00"));

  res = g_snprintf (buf, 128, "%.8e", G_PI);
  g_assert_cmpint (res, >=, 14);
  g_assert (same_value (buf, "3.14159265e+00"));

  res = g_snprintf (buf, 128, "%.0e", G_PI);
  g_assert_cmpint (res, >=, 5);
  g_assert (same_value (buf, "3e+00"));

  res = g_snprintf (buf, 128, "%.1e", 0.0);
  g_assert_cmpint (res, >=, 7);
  g_assert (same_value (buf, "0.0e+00"));

  res = g_snprintf (buf, 128, "%.1e", 0.00001);
  g_assert_cmpint (res, >=, 7);
  g_assert (same_value (buf, "1.0e-05"));

  res = g_snprintf (buf, 128, "%.1e", 10000.0);
  g_assert_cmpint (res, >=, 7);
  g_assert (same_value (buf, "1.0e+04"));

  /* %e, flags */

  res = g_snprintf (buf, 128, "%+e", G_PI);
  g_assert_cmpint (res, >=, 13);
  g_assert (same_value (buf, "+3.141593e+00"));

  res = g_snprintf (buf, 128, "% e", G_PI);
  g_assert_cmpint (res, >=, 13);
  g_assert (same_value (buf, " 3.141593e+00"));

  res = g_snprintf (buf, 128, "%#.0e", G_PI);
  g_assert_cmpint (res, >=, 6);
  g_assert (same_value (buf, "3.e+00"));

  res = g_snprintf (buf, 128, "%09.2e", G_PI);
  g_assert_cmpint (res, >=, 9);
  g_assert (same_value (buf, "03.14e+00"));
}

static void
test_c (void)
{
  gchar buf[128];
  gint res;

  res = g_snprintf (buf, 128, "%c", 'a');
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "a");
}

static void
test_s (void)
{
  gchar buf[128];
  gint res;

  res = g_snprintf (buf, 128, "%.2s", "abc");
  g_assert_cmpint (res, ==, 2);
  g_assert_cmpstr (buf, ==, "ab");

  res = g_snprintf (buf, 128, "%.6s", "abc");
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "abc");

  res = g_snprintf (buf, 128, "%5s", "abc");
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  abc");

  res = g_snprintf (buf, 128, "%-5s", "abc");
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "abc  ");

  res = g_snprintf (buf, 128, "%5.2s", "abc");
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "   ab");

  res = g_snprintf (buf, 128, "%*s", 5, "abc");
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "  abc");

  res = g_snprintf (buf, 128, "%*s", -5, "abc");
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "abc  ");

  res = g_snprintf (buf, 128, "%*.*s", 5, 2, "abc");
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "   ab");
}

static void
test_n (void)
{
  gchar buf[128];
  gint res;
  gint i;
  glong l;

  res = g_snprintf (buf, 128, "abc%n", &i);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "abc");
  g_assert_cmpint (i, ==, 3);

  res = g_snprintf (buf, 128, "abc%ln", &l);
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "abc");
  g_assert_cmpint (l, ==, 3);
}

static void
test_percent (void)
{
  gchar buf[128];
  gint res;

  res = g_snprintf (buf, 128, "%%");
  g_assert_cmpint (res, ==, 1);
  g_assert_cmpstr (buf, ==, "%");
}

static void
test_positional_params (void)
{
  gchar buf[128];
  gint res;

  res = g_snprintf (buf, 128, "%2$c %1$c", 'b', 'a');
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "a b");

  res = g_snprintf (buf, 128, "%1$*2$.*3$s", "abc", 5, 2);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "   ab");

  res = g_snprintf (buf, 128, "%1$s%1$s", "abc");
  g_assert_cmpint (res, ==, 6);
  g_assert_cmpstr (buf, ==, "abcabc");
}

static void
test_positional_params2 (void)
{
  if (g_test_subprocess ())
    {
      gint res;

      res = g_printf ("%2$c %1$c\n", 'b', 'a');
      g_assert_cmpint (res, ==, 4);

      res = g_printf ("%1$*2$.*3$s\n", "abc", 5, 2);
      g_assert_cmpint (res, ==, 6);

      res = g_printf ("%1$s%1$s\n", "abc");
      g_assert_cmpint (res, ==, 7);
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout ("a b\n   ab\nabcabc\n");
}

static void
test_positional_params3 (void)
{
  gchar buf[128];
  gint res;

  res = g_sprintf (buf, "%2$c %1$c", 'b', 'a');
  g_assert_cmpint (res, ==, 3);
  g_assert_cmpstr (buf, ==, "a b");

  res = g_sprintf (buf, "%1$*2$.*3$s", "abc", 5, 2);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "   ab");

  res = g_sprintf (buf, "%1$s%1$s", "abc");
  g_assert_cmpint (res, ==, 6);
  g_assert_cmpstr (buf, ==, "abcabc");
}

static void
test_percent2 (void)
{
  if (g_test_subprocess ())
    {
      gint res;

      res = g_printf ("%%");
      g_assert_cmpint (res, ==, 1);
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout ("*%*");
}

static void
test_64bit (void)
{
  gchar buf[128];
  gint res;

  res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)123456);
  g_assert_cmpint (res, ==, 6);
  g_assert_cmpstr (buf, ==, "123456");

  res = g_snprintf (buf, 128, "%" G_GINT64_FORMAT, (gint64)-123456);
  g_assert_cmpint (res, ==, 7);
  g_assert_cmpstr (buf, ==, "-123456");

  res = g_snprintf (buf, 128, "%" G_GUINT64_FORMAT, (guint64)123456);
  g_assert_cmpint (res, ==, 6);
  g_assert_cmpstr (buf, ==, "123456");

  res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "o", (gint64)123456);
  g_assert_cmpint (res, ==, 6);
  g_assert_cmpstr (buf, ==, "361100");

  res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "o", (gint64)123456);
  g_assert_cmpint (res, ==, 7);
  g_assert_cmpstr (buf, ==, "0361100");

  res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "x", (gint64)123456);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "1e240");

  res = g_snprintf (buf, 128, "%#" G_GINT64_MODIFIER "x", (gint64)123456);
  g_assert_cmpint (res, ==, 7);
  g_assert_cmpstr (buf, ==, "0x1e240");

  res = g_snprintf (buf, 128, "%" G_GINT64_MODIFIER "X", (gint64)123456);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "1E240");

#ifdef G_OS_WIN32
  /* On Win32, test that the "ll" modifier also works, for backward
   * compatibility. One really should use the G_GINT64_MODIFIER (which
   * on Win32 is the "I64" that the (msvcrt) C library's printf uses),
   * but "ll" used to work with the "trio" g_printf implementation in
   * GLib 2.2, so it's best if it continues to work.
   */

  /* However, gcc doesn't know about this, so we need to disable printf
   * format warnings...
   */
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
_Pragma ("GCC diagnostic push")
_Pragma ("GCC diagnostic ignored \"-Wformat\"")
_Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
#endif

  res = g_snprintf (buf, 128, "%" "lli", (gint64)123456);
  g_assert_cmpint (res, ==, 6);
  g_assert_cmpstr (buf, ==, "123456");

  res = g_snprintf (buf, 128, "%" "lli", (gint64)-123456);
  g_assert_cmpint (res, ==, 7);
  g_assert_cmpstr (buf, ==, "-123456");

  res = g_snprintf (buf, 128, "%" "llu", (guint64)123456);
  g_assert_cmpint (res, ==, 6);
  g_assert_cmpstr (buf, ==, "123456");

  res = g_snprintf (buf, 128, "%" "ll" "o", (gint64)123456);
  g_assert_cmpint (res, ==, 6);
  g_assert_cmpstr (buf, ==, "361100");

  res = g_snprintf (buf, 128, "%#" "ll" "o", (gint64)123456);
  g_assert_cmpint (res, ==, 7);
  g_assert_cmpstr (buf, ==, "0361100");

  res = g_snprintf (buf, 128, "%" "ll" "x", (gint64)123456);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "1e240");

  res = g_snprintf (buf, 128, "%#" "ll" "x", (gint64)123456);
  g_assert_cmpint (res, ==, 7);
  g_assert_cmpstr (buf, ==, "0x1e240");

  res = g_snprintf (buf, 128, "%" "ll" "X", (gint64)123456);
  g_assert_cmpint (res, ==, 5);
  g_assert_cmpstr (buf, ==, "1E240");

#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
_Pragma ("GCC diagnostic pop")
#endif

#endif
}

static void
test_64bit2_base (void)
{
  gint res;

  res = g_printf ("%" G_GINT64_FORMAT "\n", (gint64)123456);
  g_assert_cmpint (res, ==, 7);

  res = g_printf ("%" G_GINT64_FORMAT "\n", (gint64)-123456);
  g_assert_cmpint (res, ==, 8);

  res = g_printf ("%" G_GUINT64_FORMAT "\n", (guint64)123456);
  g_assert_cmpint (res, ==, 7);

  res = g_printf ("%" G_GINT64_MODIFIER "o\n", (gint64)123456);
  g_assert_cmpint (res, ==, 7);

  res = g_printf ("%#" G_GINT64_MODIFIER "o\n", (gint64)123456);
  g_assert_cmpint (res, ==, 8);

  res = g_printf ("%" G_GINT64_MODIFIER "x\n", (gint64)123456);
  g_assert_cmpint (res, ==, 6);

  res = g_printf ("%#" G_GINT64_MODIFIER "x\n", (gint64)123456);
  g_assert_cmpint (res, ==, 8);

  res = g_printf ("%" G_GINT64_MODIFIER "X\n", (gint64)123456);
  g_assert_cmpint (res, ==, 6);
}

#ifdef G_OS_WIN32
static void
test_64bit2_win32 (void)
{
  gint res;

  /* On Win32, test that the "ll" modifier also works, for backward
   * compatibility. One really should use the G_GINT64_MODIFIER (which
   * on Win32 is the "I64" that the (msvcrt) C library's printf uses),
   * but "ll" used to work with the "trio" g_printf implementation in
   * GLib 2.2, so it's best if it continues to work.
   */

  /* However, gcc doesn't know about this, so we need to disable printf
   * format warnings...
   */
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
_Pragma ("GCC diagnostic push")
_Pragma ("GCC diagnostic ignored \"-Wformat\"")
_Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
#endif

  res = g_printf ("%" "lli\n", (gint64)123456);
  g_assert_cmpint (res, ==, 7);

  res = g_printf ("%" "lli\n", (gint64)-123456);
  g_assert_cmpint (res, ==, 8);

  res = g_printf ("%" "llu\n", (guint64)123456);
  g_assert_cmpint (res, ==, 7);

  res = g_printf ("%" "ll" "o\n", (gint64)123456);
  g_assert_cmpint (res, ==, 7);

  res = g_printf ("%#" "ll" "o\n", (gint64)123456);
  g_assert_cmpint (res, ==, 8);

  res = g_printf ("%" "ll" "x\n", (gint64)123456);
  g_assert_cmpint (res, ==, 6);

  res = g_printf ("%#" "ll" "x\n", (gint64)123456);
  g_assert_cmpint (res, ==, 8);

  res = g_printf ("%" "ll" "X\n", (gint64)123456);
  g_assert_cmpint (res, ==, 6);

#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
_Pragma ("GCC diagnostic pop")
#endif
}
#endif

static void
test_64bit2 (void)
{
  g_test_trap_subprocess ("/printf/test-64bit/subprocess/base", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout ("123456\n-123456\n123456\n"
                             "361100\n0361100\n1e240\n"
                             "0x1e240\n1E240\n");
#ifdef G_OS_WIN32
  g_test_trap_subprocess ("/printf/test-64bit/subprocess/win32", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout ("123456\n-123456\n123456\n"
                             "361100\n0361100\n1e240\n"
                             "0x1e240\n1E240\n");
#endif
}

G_GNUC_PRINTF(1, 2)
static gsize
upper_bound (const gchar *format, ...)
{
  va_list args;
  gsize res;

  va_start (args, format);
  res = g_printf_string_upper_bound (format, args);
  va_end (args);

  return res;
}

static void
test_upper_bound (void)
{
  gsize res;

  res = upper_bound ("bla %s %d: %g\n", "bla", 123, 0.123);
  g_assert_cmpint (res, ==, 20);
}

int
main (int   argc,
      char *argv[])
{
#ifdef G_OS_WIN32
  /* Ensure binary mode for stdout, this way
   * tests produce \n line endings on Windows instead of the
   * default \r\n.
   */
  _setmode (fileno (stdout), _O_BINARY);
#endif
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/snprintf/retval-and-trunc", test_retval_and_trunc);
  g_test_add_func ("/snprintf/%d", test_d);
  g_test_add_func ("/snprintf/%d-invalid", test_d_invalid);
  g_test_add_func ("/snprintf/%o", test_o);
  g_test_add_func ("/snprintf/%u", test_u);
  g_test_add_func ("/snprintf/%x", test_x);
  g_test_add_func ("/snprintf/%X", test_X);
  g_test_add_func ("/snprintf/%f", test_f);
  g_test_add_func ("/snprintf/%e", test_e);
  g_test_add_func ("/snprintf/%c", test_c);
  g_test_add_func ("/snprintf/%s", test_s);
  g_test_add_func ("/snprintf/%n", test_n);
  g_test_add_func ("/snprintf/test-percent", test_percent);
  g_test_add_func ("/snprintf/test-positional-params", test_positional_params);
  g_test_add_func ("/snprintf/test-64bit", test_64bit);

  g_test_add_func ("/printf/test-percent", test_percent2);
  g_test_add_func ("/printf/test-positional-params", test_positional_params2);
  g_test_add_func ("/printf/test-64bit", test_64bit2);
  g_test_add_func ("/printf/test-64bit/subprocess/base", test_64bit2_base);
#ifdef G_OS_WIN32
  g_test_add_func ("/printf/test-64bit/subprocess/win32", test_64bit2_win32);
#endif

  g_test_add_func ("/sprintf/test-positional-params", test_positional_params3);
  g_test_add_func ("/sprintf/upper-bound", test_upper_bound);

  return g_test_run();
}