]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/tst-sprintf.c
Eliminate -Wno-format from printf/scanf tests.
[thirdparty/glibc.git] / stdio-common / tst-sprintf.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <locale.h>
4 #include <string.h>
5
6
7 static int
8 do_test (void)
9 {
10 char buf[100];
11 int result = 0;
12
13 if (sprintf (buf, "%.0ls", L"foo") != 0
14 || strlen (buf) != 0)
15 {
16 puts ("sprintf (buf, \"%.0ls\", L\"foo\") produced some output");
17 result = 1;
18 }
19
20 #define SIZE (1024*70000)
21 #define STR(x) #x
22
23 char *dst = malloc (SIZE + 1);
24
25 if (dst == NULL)
26 {
27 puts ("memory allocation failure");
28 result = 1;
29 }
30 else
31 {
32 sprintf (dst, "%*s", SIZE, "");
33 if (strnlen (dst, SIZE + 1) != SIZE)
34 {
35 puts ("sprintf (dst, \"%*s\", " STR(SIZE) ", \"\") did not produce enough output");
36 result = 1;
37 }
38 free (dst);
39 }
40
41 if (sprintf (buf, "%1$d%3$.*2$s%4$d", 7, 67108863, "x", 8) != 3
42 || strcmp (buf, "7x8") != 0)
43 {
44 printf ("sprintf (buf, \"%%1$d%%3$.*2$s%%4$d\", 7, 67108863, \"x\", 8) produced `%s' output", buf);
45 result = 1;
46 }
47
48 /* We are testing a corner case of the sprintf format string here. */
49 DIAG_PUSH_NEEDS_COMMENT;
50 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
51 int n = sprintf (buf, "%67108863.16\"%d", 7);
52 DIAG_POP_NEEDS_COMMENT;
53
54 if (n != 14 || strcmp (buf, "%67108863.16\"7") != 0)
55 {
56 printf ("sprintf (buf, \"%%67108863.16\\\"%%d\", 7) produced `%s' output",
57 buf);
58 result = 1;
59 }
60
61 /* We are testing a corner case of the sprintf format string here. */
62 DIAG_PUSH_NEEDS_COMMENT;
63 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
64 n = sprintf (buf, "%*\"%d", 0x3ffffff, 7);
65 DIAG_POP_NEEDS_COMMENT;
66
67 if (n != 11 || strcmp (buf, "%67108863\"7") != 0)
68 {
69 printf ("sprintf (buf, \"%%*\\\"%%d\", 0x3ffffff, 7) produced `%s' output", buf);
70 result = 1;
71 }
72
73 if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
74 {
75 puts ("cannot set locale");
76 result = 1;
77 }
78 else if (sprintf (buf, "%.8s\n", "Foo: \277") != 7
79 || strcmp (buf, "Foo: \277\n") != 0)
80 {
81 printf ("sprintf (buf, \"%%.8s\\n\", \"Foo: \\277\") produced '%s' output\n", buf);
82 result = 1;
83 }
84
85 return result;
86 }
87
88 #define TEST_FUNCTION do_test ()
89 #include "../test-skeleton.c"