summary refs log tree commit diff
path: root/stdio-common/tstdiomisc.c
blob: c2fc73deb1eb865f70cf171d4c6974943a6acf60 (plain) (blame)
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
#include <array_length.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <libc-diag.h>

static int
t1 (void)
{
  int n = -1;
  sscanf ("abc  ", "abc %n", &n);
  printf ("t1: count=%d\n", n);

  return n != 5;
}

static int
t2 (void)
{
  int result = 0;
  int n;
  long N;
  int retval;
#define SCAN(INPUT, FORMAT, VAR, EXP_RES, EXP_VAL) \
  VAR = -1; \
  retval = sscanf (INPUT, FORMAT, &VAR); \
  printf ("sscanf (\"%s\", \"%s\", &x) => %d, x = %ld\n", \
	  INPUT, FORMAT, retval, (long int) VAR); \
  result |= retval != EXP_RES || VAR != EXP_VAL

  /* This function is testing corner cases of the scanf format string,
     so they do not all conform to -Wformat's expectations.  */
  DIAG_PUSH_NEEDS_COMMENT;
  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-extra-args");

  SCAN ("12345", "%ld", N, 1, 12345);
  SCAN ("12345", "%llllld", N, 0, -1);
  SCAN ("12345", "%LLLLLd", N, 0, -1);
  SCAN ("test ", "%*s%n",  n, 0, 4);
  SCAN ("test ", "%2*s%n",  n, 0, -1);
  SCAN ("12 ",   "%l2d",  n, 0, -1);
  SCAN ("12 ",   "%2ld",  N, 1, 12);

  n = -1;
  N = -1;
  retval = sscanf ("1 1", "%d %Z", &n, &N);
  printf ("sscanf (\"1 1\", \"%%d %%Z\", &n, &N) => %d, n = %d, N = %ld\n", \
	  retval, n, N); \
  result |= retval != 1 || n != 1 || N != -1;

  DIAG_POP_NEEDS_COMMENT;

  return result;
}

static int
t3 (void)
{
  char buf[80];
  wchar_t wbuf[80];
  int result = 0;
  int retval;

  retval = sprintf (buf, "%p", (char *) NULL);
  result |= retval != 5 || strcmp (buf, "(nil)") != 0;

  retval = swprintf (wbuf, array_length (wbuf), L"%p", (char *) NULL);
  result |= retval != 5 || wcscmp (wbuf, L"(nil)") != 0;

  return result;
}

volatile double qnanval;
volatile long double lqnanval;
/* A sNaN is only guaranteed to be representable in variables with static (or
   thread-local) storage duration.  */
static volatile double snanval = __builtin_nans ("");
static volatile double msnanval = -__builtin_nans ("");
static volatile long double lsnanval = __builtin_nansl ("");
static volatile long double lmsnanval = -__builtin_nansl ("");
volatile double infval;
volatile long double linfval;


static int
F (void)
{
  char buf[80];
  wchar_t wbuf[40];
  int result = 0;

  qnanval = NAN;

  /* The %f and %F arguments are in fact constants, but GCC is
     prevented from seeing this (volatile is used) so it cannot tell
     that the output is not truncated.  */
  DIAG_PUSH_NEEDS_COMMENT;
#if __GNUC_PREREQ (7, 0)
  DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-truncation");
#endif

  snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
	    qnanval, qnanval, qnanval, qnanval,
	    qnanval, qnanval, qnanval, qnanval);
  result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
  printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);

  snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
	    -qnanval, -qnanval, -qnanval, -qnanval,
	    -qnanval, -qnanval, -qnanval, -qnanval);
  result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
  printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
	  buf);

  snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
	    snanval, snanval, snanval, snanval,
	    snanval, snanval, snanval, snanval);
  result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
  printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);

  snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
	    msnanval, msnanval, msnanval, msnanval,
	    msnanval, msnanval, msnanval, msnanval);
  result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
  printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
	  buf);

  infval = DBL_MAX * DBL_MAX;

  snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
	    infval, infval, infval, infval, infval, infval, infval, infval);
  result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0;
  printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf);

  snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
	    -infval, -infval, -infval, -infval,
	    -infval, -infval, -infval, -infval);
  result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
  printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n",
	  buf);

  swprintf (wbuf, array_length (wbuf), L"%a %A %e %E %f %F %g %G",
	    qnanval, qnanval, qnanval, qnanval,
	    qnanval, qnanval, qnanval, qnanval);
  result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
  printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);

  swprintf (wbuf, array_length (wbuf), L"%a %A %e %E %f %F %g %G",
	    -qnanval, -qnanval, -qnanval, -qnanval,
	    -qnanval, -qnanval, -qnanval, -qnanval);
  result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
  printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
	  wbuf);

  swprintf (wbuf, array_length (wbuf), L"%a %A %e %E %f %F %g %G",
	    snanval, snanval, snanval, snanval,
	    snanval, snanval, snanval, snanval);
  result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
  printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);

  swprintf (wbuf, array_length (wbuf), L"%a %A %e %E %f %F %g %G",
	    msnanval, msnanval, msnanval, msnanval,
	    msnanval, msnanval, msnanval, msnanval);
  result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
  printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
	  wbuf);

  swprintf (wbuf, array_length (wbuf), L"%a %A %e %E %f %F %g %G",
	    infval, infval, infval, infval, infval, infval, infval, infval);
  result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0;
  printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf);

  swprintf (wbuf, array_length (wbuf), L"%a %A %e %E %f %F %g %G",
	    -infval, -infval, -infval, -infval,
	    -infval, -infval, -infval, -infval);
  result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
  printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n",
	  wbuf);

  lqnanval = NAN;

  snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
	    lqnanval, lqnanval, lqnanval, lqnanval,
	    lqnanval, lqnanval, lqnanval, lqnanval);
  result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
  printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);

  snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
	    -lqnanval, -lqnanval, -lqnanval, -lqnanval,
	    -lqnanval, -lqnanval, -lqnanval, -lqnanval);
  result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
  printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
	  buf);

  snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
	    lsnanval, lsnanval, lsnanval, lsnanval,
	    lsnanval, lsnanval, lsnanval, lsnanval);
  result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
  printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);

  snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
	    lmsnanval, lmsnanval, lmsnanval, lmsnanval,
	    lmsnanval, lmsnanval, lmsnanval, lmsnanval);
  result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
  printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
	  buf);

  linfval = LDBL_MAX * LDBL_MAX;

  snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
	    linfval, linfval, linfval, linfval,
	    linfval, linfval, linfval, linfval);
  result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0;
  printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf);

  snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
	    -linfval, -linfval, -linfval, -linfval,
	    -linfval, -linfval, -linfval, -linfval);
  result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
  printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n",
	  buf);

  swprintf (wbuf, array_length (wbuf),
	    L"%La %LA %Le %LE %Lf %LF %Lg %LG",
	    lqnanval, lqnanval, lqnanval, lqnanval,
	    lqnanval, lqnanval, lqnanval, lqnanval);
  result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
  printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);

  swprintf (wbuf, array_length (wbuf),
	    L"%La %LA %Le %LE %Lf %LF %Lg %LG",
	    -lqnanval, -lqnanval, -lqnanval, -lqnanval,
	    -lqnanval, -lqnanval, -lqnanval, -lqnanval);
  result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
  printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
	  wbuf);

  swprintf (wbuf, array_length (wbuf),
	    L"%La %LA %Le %LE %Lf %LF %Lg %LG",
	    lsnanval, lsnanval, lsnanval, lsnanval,
	    lsnanval, lsnanval, lsnanval, lsnanval);
  result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
  printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);

  swprintf (wbuf, array_length (wbuf),
	    L"%La %LA %Le %LE %Lf %LF %Lg %LG",
	    lmsnanval, lmsnanval, lmsnanval, lmsnanval,
	    lmsnanval, lmsnanval, lmsnanval, lmsnanval);
  result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
  printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
	  wbuf);

  swprintf (wbuf, array_length (wbuf),
	    L"%La %LA %Le %LE %Lf %LF %Lg %LG",
	    linfval, linfval, linfval, linfval,
	    linfval, linfval, linfval, linfval);
  result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0;
  printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf);

  swprintf (wbuf, array_length (wbuf),
	    L"%La %LA %Le %LE %Lf %LF %Lg %LG",
	    -linfval, -linfval, -linfval, -linfval,
	    -linfval, -linfval, -linfval, -linfval);
  result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
  printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n",
	  wbuf);

  DIAG_POP_NEEDS_COMMENT;

  return result;
}

int
main (int argc, char *argv[])
{
  int result = 0;

  result |= t1 ();
  result |= t2 ();
  result |= t3 ();
  result |= F ();

  result |= fflush (stdout) == EOF;

  return result;
}