Update.
[glibc.git] / stdio-common / tstscanf.c
blob3e6e496e4f680580eb2f746dab076f29a1157640
1 /* Copyright (C) 1991, 1992, 1996, 1997, 1998 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 sscanf ("conversion] Zero flag Ze]ro#\n", "%*[^]] %[^#]\n", buf);
43 if (strcmp (buf, "] Zero flag Ze]ro") != 0)
45 fputs ("test failed!\n", stdout);
46 result = 1;
49 if (argc == 2 && !strcmp (argv[1], "-opipe"))
51 out = popen ("/bin/cat", "w");
52 if (out == NULL)
54 perror ("popen: /bin/cat");
55 result = 1;
58 else if (argc == 3 && !strcmp (argv[1], "-ipipe"))
60 sprintf (buf, "/bin/cat %s", argv[2]);
61 in = popen (buf, "r");
62 if (in == NULL)
64 perror ("popen: /bin/cat");
65 result = 1;
70 char name[50];
71 fprintf (out,
72 "sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n",
73 sscanf ("thompson", "%s", name),
74 name);
75 if (strcmp (name, "thompson") != 0)
77 fputs ("test failed!\n", stdout);
78 result = 1;
82 fputs ("Testing scanf (vfscanf)\n", out);
84 fputs ("Test 1:\n", out);
86 int n, i;
87 float x;
88 char name[50];
89 n = fscanf (in, "%d%f%s", &i, &x, name);
90 fprintf (out, "n = %d, i = %d, x = %f, name = \"%.50s\"\n",
91 n, i, x, name);
92 if (n != 3 || i != 25 || x != 5.432F || strcmp (name, "thompson"))
94 fputs ("test failed!\n", stdout);
95 result = 1;
98 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
99 if (strcmp (buf, "\n"))
101 fputs ("test failed!\n", stdout);
102 result = 1;
104 fputs ("Test 2:\n", out);
106 int i;
107 float x;
108 char name[50];
109 (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
110 fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
111 if (i != 56 || x != 789.0F || strcmp (name, "56"))
113 fputs ("test failed!\n", stdout);
114 result = 1;
117 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
118 if (strcmp (buf, "a72\n"))
120 fputs ("test failed!\n", stdout);
121 result = 1;
123 fputs ("Test 3:\n", out);
125 static struct {
126 int count;
127 float quant;
128 const char *units;
129 const char *item;
130 } ok[] = {
131 { 3, 2.0F, "quarts", "oil" },
132 { 2, -12.8F, "degrees", "" },
133 { 0, 0.0F, "", "" },
134 { 3, 10.0F, "LBS", "fertilizer" },
135 { 3, 100.0F, "rgs", "energy" },
136 { -1, 0.0F, "", "" }};
137 size_t rounds = 0;
138 float quant;
139 char units[21], item[21];
140 while (!feof (in) && !ferror (in))
142 int count;
144 if (rounds++ >= sizeof (ok) / sizeof (ok[0]))
146 fputs ("test failed!\n", stdout);
147 result = 1;
150 quant = 0.0;
151 units[0] = item[0] = '\0';
152 count = fscanf (in, "%f%20s of %20s", &quant, units, item);
153 (void) fscanf (in, "%*[^\n]");
154 fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
155 count, quant, item, units);
156 if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
157 || strcmp (item, ok[rounds-1].item)
158 || strcmp (units, ok[rounds-1].units))
160 fputs ("test failed!\n", stdout);
161 result = 1;
165 buf[0] = '\0';
166 fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
167 if (strcmp (buf, ""))
169 fputs ("test failed!\n", stdout);
170 result = 1;
173 if (out != stdout)
174 pclose (out);
176 fputs ("Test 4:\n", out);
178 int res, val, n;
180 res = sscanf ("-242", "%3o%n", &val, &n);
181 printf ("res = %d, val = %d, n = %d\n", res, val, n);
182 if (res != 1 || val != -20 || n != 3)
184 fputs ("test failed!\n", stdout);
185 result = 1;
189 fputs ("Test 5:\n", out);
191 double a = 0, b = 0;
192 int res, n;
194 res = sscanf ("1234567", "%3lg%3lg%n", &a, &b, &n);
195 printf ("res = %d, a = %g, b = %g, n = %d\n", res, a, b, n);
197 if (res != 2 || a != 123 || b != 456 || n != 6)
199 fputs ("test failed!\n", stdout);
200 result = 1;
203 res = sscanf ("0", "%lg", &a);
204 printf ("res = %d, a = %g\n", res, a);
206 if (res != 1 || a != 0)
208 fputs ("test failed!\n", stdout);
209 result = 1;
212 res = sscanf ("1e3", "%lg%n", &a, &n);
213 printf ("res = %d, a = %g, n = %d\n", res, a, n);
215 if (res != 1 || a != 1000 || n != 3)
217 fputs ("test failed!\n", stdout);
218 result = 1;
222 exit (result);