Refer to C23 in place of C2X in glibc
[glibc.git] / stdio-common / tst-sprintf.c
blobcbd35447a987e9d3c24dd13a2ec2c5f9c9d5cf70
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <locale.h>
4 #include <string.h>
5 #include <libc-diag.h>
8 static int
9 do_test (void)
11 char buf[100];
12 int result = 0;
14 if (sprintf (buf, "%.0ls", L"foo") != 0
15 || strlen (buf) != 0)
17 puts ("sprintf (buf, \"%.0ls\", L\"foo\") produced some output");
18 result = 1;
21 #define SIZE (1024*70000)
22 #define STR(x) #x
24 char *dst = malloc (SIZE + 1);
26 if (dst == NULL)
28 puts ("memory allocation failure");
29 result = 1;
31 else
33 sprintf (dst, "%*s", SIZE, "");
34 if (strnlen (dst, SIZE + 1) != SIZE)
36 puts ("sprintf (dst, \"%*s\", " STR(SIZE) ", \"\") did not produce enough output");
37 result = 1;
39 free (dst);
42 if (sprintf (buf, "%1$d%3$.*2$s%4$d", 7, 67108863, "x", 8) != 3
43 || strcmp (buf, "7x8") != 0)
45 printf ("sprintf (buf, \"%%1$d%%3$.*2$s%%4$d\", 7, 67108863, \"x\", 8) produced `%s' output", buf);
46 result = 1;
49 /* We are testing a corner case of the sprintf format string here. */
50 DIAG_PUSH_NEEDS_COMMENT;
51 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
52 int n = sprintf (buf, "%67108863.16\"%d", 7);
53 DIAG_POP_NEEDS_COMMENT;
55 if (n != 14 || strcmp (buf, "%67108863.16\"7") != 0)
57 printf ("sprintf (buf, \"%%67108863.16\\\"%%d\", 7) produced `%s' output",
58 buf);
59 result = 1;
62 /* We are testing a corner case of the sprintf format string here. */
63 DIAG_PUSH_NEEDS_COMMENT;
64 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
65 n = sprintf (buf, "%*\"%d", 0x3ffffff, 7);
66 DIAG_POP_NEEDS_COMMENT;
68 if (n != 11 || strcmp (buf, "%67108863\"7") != 0)
70 printf ("sprintf (buf, \"%%*\\\"%%d\", 0x3ffffff, 7) produced `%s' output", buf);
71 result = 1;
74 if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
76 puts ("cannot set locale");
77 result = 1;
79 else if (sprintf (buf, "%.8s\n", "Foo: \277") != 7
80 || strcmp (buf, "Foo: \277\n") != 0)
82 printf ("sprintf (buf, \"%%.8s\\n\", \"Foo: \\277\") produced '%s' output\n", buf);
83 result = 1;
86 return result;
89 #define TEST_FUNCTION do_test ()
90 #include "../test-skeleton.c"