Update.
[glibc.git] / stdio-common / tstscanf.c
blob6db98eef318a016e464699845322784f8e49ea84
1 /* Copyright (C) 1991, 92, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #ifdef BSD
20 #include </usr/include/stdio.h>
21 #else
22 #include <stdio.h>
23 #endif
24 #include <stdlib.h>
25 #include <string.h>
28 int
29 main (int argc, char **argv)
31 char buf[BUFSIZ];
32 FILE *in = stdin, *out = stdout;
33 int x;
34 int result = 0;
36 if (sscanf ("0", "%d", &x) != 1)
38 fputs ("test failed!\n", stdout);
39 result = 1;
42 if (sscanf ("08905x", "%9[0-9]", buf) != 1
43 || strcmp (buf, "08905") != 0)
45 fputs ("test failed!\n", stdout);
46 result = 1;
49 sscanf ("conversion] Zero flag Ze]ro#\n", "%*[^]] %[^#]\n", buf);
50 if (strcmp (buf, "] Zero flag Ze]ro") != 0)
52 fputs ("test failed!\n", stdout);
53 result = 1;
56 if (argc == 2 && !strcmp (argv[1], "-opipe"))
58 out = popen ("/bin/cat", "w");
59 if (out == NULL)
61 perror ("popen: /bin/cat");
62 result = 1;
65 else if (argc == 3 && !strcmp (argv[1], "-ipipe"))
67 sprintf (buf, "/bin/cat %s", argv[2]);
68 in = popen (buf, "r");
69 if (in == NULL)
71 perror ("popen: /bin/cat");
72 result = 1;
77 char name[50];
78 fprintf (out,
79 "sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n",
80 sscanf ("thompson", "%s", name),
81 name);
82 if (strcmp (name, "thompson") != 0)
84 fputs ("test failed!\n", stdout);
85 result = 1;
89 fputs ("Testing scanf (vfscanf)\n", out);
91 fputs ("Test 1:\n", out);
93 int n, i;
94 float x;
95 char name[50];
96 n = fscanf (in, "%d%f%s", &i, &x, name);
97 fprintf (out, "n = %d, i = %d, x = %f, name = \"%.50s\"\n",
98 n, i, x, name);
99 if (n != 3 || i != 25 || x != 5.432F || strcmp (name, "thompson"))
101 fputs ("test failed!\n", stdout);
102 result = 1;
105 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
106 if (strcmp (buf, "\n"))
108 fputs ("test failed!\n", stdout);
109 result = 1;
111 fputs ("Test 2:\n", out);
113 int i;
114 float x;
115 char name[50];
116 (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
117 fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
118 if (i != 56 || x != 789.0F || strcmp (name, "56"))
120 fputs ("test failed!\n", stdout);
121 result = 1;
124 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
125 if (strcmp (buf, "a72\n"))
127 fputs ("test failed!\n", stdout);
128 result = 1;
130 fputs ("Test 3:\n", out);
132 static struct {
133 int count;
134 float quant;
135 const char *units;
136 const char *item;
137 } ok[] = {
138 { 3, 2.0F, "quarts", "oil" },
139 { 2, -12.8F, "degrees", "" },
140 { 0, 0.0F, "", "" },
141 { 3, 10.0F, "LBS", "fertilizer" },
142 { 3, 100.0F, "rgs", "energy" },
143 { -1, 0.0F, "", "" }};
144 size_t rounds = 0;
145 float quant;
146 char units[21], item[21];
147 while (!feof (in) && !ferror (in))
149 int count;
151 if (rounds++ >= sizeof (ok) / sizeof (ok[0]))
153 fputs ("test failed!\n", stdout);
154 result = 1;
157 quant = 0.0;
158 units[0] = item[0] = '\0';
159 count = fscanf (in, "%f%20s of %20s", &quant, units, item);
160 (void) fscanf (in, "%*[^\n]");
161 fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
162 count, quant, item, units);
163 if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
164 || strcmp (item, ok[rounds-1].item)
165 || strcmp (units, ok[rounds-1].units))
167 fputs ("test failed!\n", stdout);
168 result = 1;
172 buf[0] = '\0';
173 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
174 if (strcmp (buf, ""))
176 fputs ("test failed!\n", stdout);
177 result = 1;
180 if (out != stdout)
181 pclose (out);
183 fputs ("Test 4:\n", out);
185 int res, val, n;
187 res = sscanf ("-242", "%3o%n", &val, &n);
188 printf ("res = %d, val = %d, n = %d\n", res, val, n);
189 if (res != 1 || val != -20 || n != 3)
191 fputs ("test failed!\n", stdout);
192 result = 1;
196 fputs ("Test 5:\n", out);
198 double a = 0, b = 0;
199 int res, n;
201 res = sscanf ("1234567", "%3lg%3lg%n", &a, &b, &n);
202 printf ("res = %d, a = %g, b = %g, n = %d\n", res, a, b, n);
204 if (res != 2 || a != 123 || b != 456 || n != 6)
206 fputs ("test failed!\n", stdout);
207 result = 1;
210 res = sscanf ("0", "%lg", &a);
211 printf ("res = %d, a = %g\n", res, a);
213 if (res != 1 || a != 0)
215 fputs ("test failed!\n", stdout);
216 result = 1;
219 res = sscanf ("1e3", "%lg%n", &a, &n);
220 printf ("res = %d, a = %g, n = %d\n", res, a, n);
222 if (res != 1 || a != 1000 || n != 3)
224 fputs ("test failed!\n", stdout);
225 result = 1;
229 fputs ("Test 6:\n", stdout);
231 char *p = (char *) -1;
232 int res;
234 sprintf (buf, "%p", NULL);
235 res = sscanf (buf, "%p", &p);
236 printf ("sscanf (\"%s\", \"%%p\", &p) = %d, p == %p\n", buf, res, p);
238 if (res != 1 || p != NULL)
240 fputs ("test failed!\n", stdout);
241 result = 1;
245 fputs ("Test 7:\n", stdout);
247 short a[2] = { -1, -1 };
248 int res;
250 res = sscanf ("32767 1234", "%hd %hd", &a[0], &a[1]);
251 printf ("res = %d, a[0] = %d, a[1] = %d\n", res, a[0], a[1]);
253 if (res != 2 || a[0] != 32767 || a[1] != 1234)
255 fputs ("test failed!\n", stdout);
256 result = 1;
260 fputs ("Test 8:\n", stdout);
262 double d = 123456.789;
263 int res;
265 res = sscanf ("0x1234", "%lf", &d);
266 printf ("res = %d, d = %f\n", res, d);
268 if (res != 0 || d != 123456.789)
270 fputs ("test failed!\n", stdout);
271 result = 1;
275 fputs ("Test 9:\n", stdout);
277 /* From PR libc/1313 reported by Ben Caradoc-Davies <bmcd@physics.otago.ac.nz>. */
278 float value;
279 int res;
281 res = sscanf ("0123", "%2f", &value);
282 if (res != 1 || value != 1.0)
284 fputs ("test failed!\n", stdout);
285 result = 1;
289 exit (result);