chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / stdio-common / tst-sprintf.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <gnu/option-groups.h>
5
6
7 int
8 main (void)
9 {
10   char buf[100];
11   int result = 0;
12
13 #if __OPTION_POSIX_C_LANG_WIDE_CHAR
14   if (sprintf (buf, "%.0ls", L"foo") != 0
15       || strlen (buf) != 0)
16     {
17       puts ("sprintf (buf, \"%.0ls\", L\"foo\") produced some output");
18       result = 1;
19     }
20 #endif /* __OPTION_POSIX_C_LANG_WIDE_CHAR */
21
22 #define SIZE (1024*70000)
23 #define STR(x) #x
24
25   char *dst = malloc (SIZE + 1);
26
27   if (dst == NULL)
28     {
29       puts ("memory allocation failure");
30       result = 1;
31     }
32   else
33     {
34       sprintf (dst, "%*s", SIZE, "");
35       if (strnlen (dst, SIZE + 1) != SIZE)
36         {
37           puts ("sprintf (dst, \"%*s\", " STR(SIZE) ", \"\") did not produce enough output");
38           result = 1;
39         }
40       free (dst);
41     }
42
43   if (sprintf (buf, "%1$d%3$.*2$s%4$d", 7, 67108863, "x", 8) != 3
44       || strcmp (buf, "7x8") != 0)
45     {
46       printf ("sprintf (buf, \"%%1$d%%3$.*2$s%%4$d\", 7, 67108863, \"x\", 8) produced `%s' output", buf);
47       result = 1;
48     }
49
50   if (sprintf (buf, "%67108863.16\"%d", 7) != 14
51       || strcmp (buf, "%67108863.16\"7") != 0)
52     {
53       printf ("sprintf (buf, \"%%67108863.16\\\"%%d\", 7) produced `%s' output", buf);
54       result = 1;
55     }
56
57   if (sprintf (buf, "%*\"%d", 0x3ffffff, 7) != 11
58       || strcmp (buf, "%67108863\"7") != 0)
59     {
60       printf ("sprintf (buf, \"%%*\\\"%%d\", 0x3ffffff, 7) produced `%s' output", buf);
61       result = 1;
62     }
63
64   return result;
65 }