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. */
20 #include </usr/include/stdio.h>
29 main (int argc
, char **argv
)
32 FILE *in
= stdin
, *out
= stdout
;
36 if (sscanf ("0", "%d", &x
) != 1)
38 fputs ("test failed!\n", stdout
);
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
);
49 if (argc
== 2 && !strcmp (argv
[1], "-opipe"))
51 out
= popen ("/bin/cat", "w");
54 perror ("popen: /bin/cat");
58 else if (argc
== 3 && !strcmp (argv
[1], "-ipipe"))
60 sprintf (buf
, "/bin/cat %s", argv
[2]);
61 in
= popen (buf
, "r");
64 perror ("popen: /bin/cat");
72 "sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n",
73 sscanf ("thompson", "%s", name
),
75 if (strcmp (name
, "thompson") != 0)
77 fputs ("test failed!\n", stdout
);
82 fputs ("Testing scanf (vfscanf)\n", out
);
84 fputs ("Test 1:\n", out
);
89 n
= fscanf (in
, "%d%f%s", &i
, &x
, name
);
90 fprintf (out
, "n = %d, i = %d, x = %f, name = \"%.50s\"\n",
92 if (n
!= 3 || i
!= 25 || x
!= 5.432F
|| strcmp (name
, "thompson"))
94 fputs ("test failed!\n", stdout
);
98 fprintf (out
, "Residual: \"%s\"\n", fgets (buf
, sizeof (buf
), in
));
99 if (strcmp (buf
, "\n"))
101 fputs ("test failed!\n", stdout
);
104 fputs ("Test 2:\n", out
);
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
);
117 fprintf (out
, "Residual: \"%s\"\n", fgets (buf
, sizeof (buf
), in
));
118 if (strcmp (buf
, "a72\n"))
120 fputs ("test failed!\n", stdout
);
123 fputs ("Test 3:\n", out
);
131 { 3, 2.0F
, "quarts", "oil" },
132 { 2, -12.8F
, "degrees", "" },
134 { 3, 10.0F
, "LBS", "fertilizer" },
135 { 3, 100.0F
, "rgs", "energy" },
136 { -1, 0.0F
, "", "" }};
139 char units
[21], item
[21];
140 while (!feof (in
) && !ferror (in
))
144 if (rounds
++ >= sizeof (ok
) / sizeof (ok
[0]))
146 fputs ("test failed!\n", stdout
);
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
);
166 fprintf (out
, "Residual: \"%s\"\n", fgets (buf
, sizeof (buf
), in
));
167 if (strcmp (buf
, ""))
169 fputs ("test failed!\n", stdout
);
176 fputs ("Test 4:\n", out
);
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
);
189 fputs ("Test 5:\n", out
);
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
);
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
);
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
);