2015-02-17 Steve Ellcey <sellcey@imgtec.com>
[glibc.git] / stdio-common / tstdiomisc.c
blob5548a71e5242ec475a306472ddfe21ed0b8b28ce
1 #include <float.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <wchar.h>
6 #include <libc-internal.h>
8 static int
9 t1 (void)
11 int n = -1;
12 sscanf ("abc ", "abc %n", &n);
13 printf ("t1: count=%d\n", n);
15 return n != 5;
18 static int
19 t2 (void)
21 int result = 0;
22 int n;
23 long N;
24 int retval;
25 #define SCAN(INPUT, FORMAT, VAR, EXP_RES, EXP_VAL) \
26 VAR = -1; \
27 retval = sscanf (INPUT, FORMAT, &VAR); \
28 printf ("sscanf (\"%s\", \"%s\", &x) => %d, x = %ld\n", \
29 INPUT, FORMAT, retval, (long int) VAR); \
30 result |= retval != EXP_RES || VAR != EXP_VAL
32 /* This function is testing corner cases of the scanf format string,
33 so they do not all conform to -Wformat's expectations. */
34 DIAG_PUSH_NEEDS_COMMENT;
35 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
36 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-extra-args");
38 SCAN ("12345", "%ld", N, 1, 12345);
39 SCAN ("12345", "%llllld", N, 0, -1);
40 SCAN ("12345", "%LLLLLd", N, 0, -1);
41 SCAN ("test ", "%*s%n", n, 0, 4);
42 SCAN ("test ", "%2*s%n", n, 0, -1);
43 SCAN ("12 ", "%l2d", n, 0, -1);
44 SCAN ("12 ", "%2ld", N, 1, 12);
46 n = -1;
47 N = -1;
48 retval = sscanf ("1 1", "%d %Z", &n, &N);
49 printf ("sscanf (\"1 1\", \"%%d %%Z\", &n, &N) => %d, n = %d, N = %ld\n", \
50 retval, n, N); \
51 result |= retval != 1 || n != 1 || N != -1;
53 DIAG_POP_NEEDS_COMMENT;
55 return result;
58 static int
59 t3 (void)
61 char buf[80];
62 wchar_t wbuf[80];
63 int result = 0;
64 int retval;
66 retval = sprintf (buf, "%p", (char *) NULL);
67 result |= retval != 5 || strcmp (buf, "(nil)") != 0;
69 retval = swprintf (wbuf, sizeof (wbuf) / sizeof (wbuf[0]),
70 L"%p", (char *) NULL);
71 result |= retval != 5 || wcscmp (wbuf, L"(nil)") != 0;
73 return result;
76 volatile double qnanval;
77 volatile long double lqnanval;
78 /* A sNaN is only guaranteed to be representable in variables with static (or
79 thread-local) storage duration. */
80 static volatile double snanval = __builtin_nans ("");
81 static volatile double msnanval = -__builtin_nans ("");
82 static volatile long double lsnanval = __builtin_nansl ("");
83 static volatile long double lmsnanval = -__builtin_nansl ("");
84 volatile double infval;
85 volatile long double linfval;
88 static int
89 F (void)
91 char buf[80];
92 wchar_t wbuf[40];
93 int result = 0;
95 qnanval = NAN;
97 snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
98 qnanval, qnanval, qnanval, qnanval,
99 qnanval, qnanval, qnanval, qnanval);
100 result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
101 printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);
103 snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
104 -qnanval, -qnanval, -qnanval, -qnanval,
105 -qnanval, -qnanval, -qnanval, -qnanval);
106 result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
107 printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
108 buf);
110 snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
111 snanval, snanval, snanval, snanval,
112 snanval, snanval, snanval, snanval);
113 result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
114 printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);
116 snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
117 msnanval, msnanval, msnanval, msnanval,
118 msnanval, msnanval, msnanval, msnanval);
119 result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
120 printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
121 buf);
123 infval = DBL_MAX * DBL_MAX;
125 snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
126 infval, infval, infval, infval, infval, infval, infval, infval);
127 result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0;
128 printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf);
130 snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
131 -infval, -infval, -infval, -infval,
132 -infval, -infval, -infval, -infval);
133 result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
134 printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n",
135 buf);
137 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
138 qnanval, qnanval, qnanval, qnanval,
139 qnanval, qnanval, qnanval, qnanval);
140 result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
141 printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);
143 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
144 -qnanval, -qnanval, -qnanval, -qnanval,
145 -qnanval, -qnanval, -qnanval, -qnanval);
146 result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
147 printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
148 wbuf);
150 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
151 snanval, snanval, snanval, snanval,
152 snanval, snanval, snanval, snanval);
153 result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
154 printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);
156 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
157 msnanval, msnanval, msnanval, msnanval,
158 msnanval, msnanval, msnanval, msnanval);
159 result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
160 printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
161 wbuf);
163 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
164 infval, infval, infval, infval, infval, infval, infval, infval);
165 result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0;
166 printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf);
168 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
169 -infval, -infval, -infval, -infval,
170 -infval, -infval, -infval, -infval);
171 result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
172 printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n",
173 wbuf);
175 lqnanval = NAN;
177 snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
178 lqnanval, lqnanval, lqnanval, lqnanval,
179 lqnanval, lqnanval, lqnanval, lqnanval);
180 result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
181 printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);
183 snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
184 -lqnanval, -lqnanval, -lqnanval, -lqnanval,
185 -lqnanval, -lqnanval, -lqnanval, -lqnanval);
186 result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
187 printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
188 buf);
190 snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
191 lsnanval, lsnanval, lsnanval, lsnanval,
192 lsnanval, lsnanval, lsnanval, lsnanval);
193 result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
194 printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);
196 snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
197 lmsnanval, lmsnanval, lmsnanval, lmsnanval,
198 lmsnanval, lmsnanval, lmsnanval, lmsnanval);
199 result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
200 printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
201 buf);
203 linfval = LDBL_MAX * LDBL_MAX;
205 snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
206 linfval, linfval, linfval, linfval,
207 linfval, linfval, linfval, linfval);
208 result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0;
209 printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf);
211 snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
212 -linfval, -linfval, -linfval, -linfval,
213 -linfval, -linfval, -linfval, -linfval);
214 result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
215 printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n",
216 buf);
218 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
219 L"%La %LA %Le %LE %Lf %LF %Lg %LG",
220 lqnanval, lqnanval, lqnanval, lqnanval,
221 lqnanval, lqnanval, lqnanval, lqnanval);
222 result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
223 printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);
225 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
226 L"%La %LA %Le %LE %Lf %LF %Lg %LG",
227 -lqnanval, -lqnanval, -lqnanval, -lqnanval,
228 -lqnanval, -lqnanval, -lqnanval, -lqnanval);
229 result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
230 printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
231 wbuf);
233 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
234 L"%La %LA %Le %LE %Lf %LF %Lg %LG",
235 lsnanval, lsnanval, lsnanval, lsnanval,
236 lsnanval, lsnanval, lsnanval, lsnanval);
237 result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
238 printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);
240 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
241 L"%La %LA %Le %LE %Lf %LF %Lg %LG",
242 lmsnanval, lmsnanval, lmsnanval, lmsnanval,
243 lmsnanval, lmsnanval, lmsnanval, lmsnanval);
244 result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
245 printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
246 wbuf);
248 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
249 L"%La %LA %Le %LE %Lf %LF %Lg %LG",
250 linfval, linfval, linfval, linfval,
251 linfval, linfval, linfval, linfval);
252 result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0;
253 printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf);
255 swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
256 L"%La %LA %Le %LE %Lf %LF %Lg %LG",
257 -linfval, -linfval, -linfval, -linfval,
258 -linfval, -linfval, -linfval, -linfval);
259 result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
260 printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n",
261 wbuf);
263 return result;
267 main (int argc, char *argv[])
269 int result = 0;
271 result |= t1 ();
272 result |= t2 ();
273 result |= t3 ();
274 result |= F ();
276 result |= fflush (stdout) == EOF;
278 return result;