| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1991-2016 Free Software Foundation, Inc. | 
 | 2 |    This file is part of the GNU C Library. | 
 | 3 |  | 
 | 4 |    The GNU C Library is free software; you can redistribute it and/or | 
 | 5 |    modify it under the terms of the GNU Lesser General Public | 
 | 6 |    License as published by the Free Software Foundation; either | 
 | 7 |    version 2.1 of the License, or (at your option) any later version. | 
 | 8 |  | 
 | 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 |    Lesser General Public License for more details. | 
 | 13 |  | 
 | 14 |    You should have received a copy of the GNU Lesser General Public | 
 | 15 |    License along with the GNU C Library; if not, see | 
 | 16 |    <http://www.gnu.org/licenses/>.  */ | 
 | 17 |  | 
 | 18 | #ifdef	BSD | 
 | 19 | #include </usr/include/stdio.h> | 
 | 20 | #else | 
 | 21 | #include <stdio.h> | 
 | 22 | #endif | 
 | 23 | #include <math.h> | 
 | 24 | #include <stdlib.h> | 
 | 25 | #include <string.h> | 
 | 26 |  | 
 | 27 |  | 
 | 28 | int | 
 | 29 | main (int argc, char **argv) | 
 | 30 | { | 
 | 31 |   char buf[BUFSIZ]; | 
 | 32 |   FILE *in = stdin, *out = stdout; | 
 | 33 |   int x; | 
 | 34 |   int result = 0; | 
 | 35 |  | 
 | 36 |   if (sscanf ("0", "%d", &x) != 1) | 
 | 37 |     { | 
 | 38 |       fputs ("test failed!\n", stdout); | 
 | 39 |       result = 1; | 
 | 40 |     } | 
 | 41 |  | 
 | 42 |   if (sscanf ("08905x", "%9[0-9]", buf) != 1 | 
 | 43 |       || strcmp (buf, "08905") != 0) | 
 | 44 |     { | 
 | 45 |       fputs ("test failed!\n", stdout); | 
 | 46 |       result = 1; | 
 | 47 |     } | 
 | 48 |  | 
 | 49 |   if (sscanf ("", "%10[a-z]", buf) != EOF) | 
 | 50 |     { | 
 | 51 |       fputs ("test failed!\n", stdout); | 
 | 52 |       result = 1; | 
 | 53 |     } | 
 | 54 |  | 
 | 55 |   sscanf ("conversion] Zero flag Ze]ro#\n", "%*[^]] %[^#]\n", buf); | 
 | 56 |   if (strcmp (buf, "] Zero flag Ze]ro") != 0) | 
 | 57 |     { | 
 | 58 |       fputs ("test failed!\n", stdout); | 
 | 59 |       result = 1; | 
 | 60 |     } | 
 | 61 |  | 
 | 62 |   if (argc == 2 && !strcmp (argv[1], "-opipe")) | 
 | 63 |     { | 
 | 64 |       out = popen ("/bin/cat", "w"); | 
 | 65 |       if (out == NULL) | 
 | 66 | 	{ | 
 | 67 | 	  perror ("popen: /bin/cat"); | 
 | 68 | 	  result = 1; | 
 | 69 | 	} | 
 | 70 |     } | 
 | 71 |   else if (argc == 3 && !strcmp (argv[1], "-ipipe")) | 
 | 72 |     { | 
 | 73 |       sprintf (buf, "/bin/cat %s", argv[2]); | 
 | 74 |       in = popen (buf, "r"); | 
 | 75 |       if (in == NULL) | 
 | 76 | 	{ | 
 | 77 | 	  perror ("popen: /bin/cat"); | 
 | 78 | 	  result = 1; | 
 | 79 | 	} | 
 | 80 |     } | 
 | 81 |  | 
 | 82 |   { | 
 | 83 |     char name[50]; | 
 | 84 |     fprintf (out, | 
 | 85 | 	     "sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n", | 
 | 86 | 	     sscanf ("thompson", "%s", name), | 
 | 87 | 	     name); | 
 | 88 |     if (strcmp (name, "thompson") != 0) | 
 | 89 |       { | 
 | 90 | 	fputs ("test failed!\n", stdout); | 
 | 91 | 	result = 1; | 
 | 92 |       } | 
 | 93 |   } | 
 | 94 |  | 
 | 95 |   fputs ("Testing scanf (vfscanf)\n", out); | 
 | 96 |  | 
 | 97 |   fputs ("Test 1:\n", out); | 
 | 98 |   { | 
 | 99 |     int n, i; | 
 | 100 |     float x; | 
 | 101 |     char name[50]; | 
 | 102 |     n = fscanf (in, "%d%f%s", &i, &x, name); | 
 | 103 |     fprintf (out, "n = %d, i = %d, x = %f, name = \"%.50s\"\n", | 
 | 104 | 	     n, i, x, name); | 
 | 105 |     if (n != 3 || i != 25 || x != 5.432F || strcmp (name, "thompson")) | 
 | 106 |       { | 
 | 107 | 	fputs ("test failed!\n", stdout); | 
 | 108 | 	result = 1; | 
 | 109 |       } | 
 | 110 |   } | 
 | 111 |   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in)); | 
 | 112 |   if (strcmp (buf, "\n")) | 
 | 113 |     { | 
 | 114 |       fputs ("test failed!\n", stdout); | 
 | 115 |       result = 1; | 
 | 116 |     } | 
 | 117 |   fputs ("Test 2:\n", out); | 
 | 118 |   { | 
 | 119 |     int i; | 
 | 120 |     float x; | 
 | 121 |     char name[50]; | 
 | 122 |     (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name); | 
 | 123 |     fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name); | 
 | 124 |     if (i != 56 || x != 789.0F || strcmp (name, "56")) | 
 | 125 |       { | 
 | 126 | 	fputs ("test failed!\n", stdout); | 
 | 127 | 	result = 1; | 
 | 128 |       } | 
 | 129 |   } | 
 | 130 |   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in)); | 
 | 131 |   if (strcmp (buf, "a72\n")) | 
 | 132 |     { | 
 | 133 |       fputs ("test failed!\n", stdout); | 
 | 134 |       result = 1; | 
 | 135 |     } | 
 | 136 |   fputs ("Test 3:\n", out); | 
 | 137 |   { | 
 | 138 |     static struct { | 
 | 139 |       int count; | 
 | 140 |       float quant; | 
 | 141 |       const char *units; | 
 | 142 |       const char *item; | 
 | 143 |     } ok[] = { | 
 | 144 |       { 3, 2.0F, "quarts", "oil" }, | 
 | 145 |       { 2, -12.8F, "degrees", "" }, | 
 | 146 |       { 0, 0.0F, "", "" }, | 
 | 147 |       { 3, 10.0F, "LBS", "fertilizer" }, | 
 | 148 |       { 3, 100.0F, "rgs", "energy" }, | 
 | 149 |       { -1, 0.0F, "", "" }}; | 
 | 150 |     size_t rounds = 0; | 
 | 151 |     float quant; | 
 | 152 |     char units[21], item[21]; | 
 | 153 |     while (!feof (in) && !ferror (in)) | 
 | 154 |       { | 
 | 155 | 	int count; | 
 | 156 |  | 
 | 157 | 	if (rounds++ >= sizeof (ok) / sizeof (ok[0])) | 
 | 158 | 	  { | 
 | 159 | 	    fputs ("test failed!\n", stdout); | 
 | 160 | 	    result = 1; | 
 | 161 | 	  } | 
 | 162 |  | 
 | 163 | 	quant = 0.0; | 
 | 164 | 	units[0] = item[0] = '\0'; | 
 | 165 | 	count = fscanf (in, "%f%20s of %20s", &quant, units, item); | 
 | 166 | 	(void) fscanf (in, "%*[^\n]"); | 
 | 167 | 	fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n", | 
 | 168 | 		 count, quant, item, units); | 
 | 169 | 	if (count != ok[rounds-1].count || quant != ok[rounds-1].quant | 
 | 170 | 	    || strcmp (item, ok[rounds-1].item) | 
 | 171 | 	    || strcmp (units, ok[rounds-1].units)) | 
 | 172 | 	  { | 
 | 173 | 	    fputs ("test failed!\n", stdout); | 
 | 174 | 	    result = 1; | 
 | 175 | 	  } | 
 | 176 |       } | 
 | 177 |   } | 
 | 178 |   buf[0] = '\0'; | 
 | 179 |   fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in)); | 
 | 180 |   if (strcmp (buf, "")) | 
 | 181 |     { | 
 | 182 |       fputs ("test failed!\n", stdout); | 
 | 183 |       result = 1; | 
 | 184 |     } | 
 | 185 |  | 
 | 186 |   if (out != stdout) | 
 | 187 |     pclose (out); | 
 | 188 |  | 
 | 189 |   fputs ("Test 4:\n", out); | 
 | 190 |   { | 
 | 191 |     int res, val, n; | 
 | 192 |  | 
 | 193 |     res = sscanf ("-242", "%3o%n", &val, &n); | 
 | 194 |     printf ("res = %d, val = %d, n = %d\n", res, val, n); | 
 | 195 |     if (res != 1 || val != -20 || n != 3) | 
 | 196 |       { | 
 | 197 | 	fputs ("test failed!\n", stdout); | 
 | 198 | 	result = 1; | 
 | 199 |       } | 
 | 200 |   } | 
 | 201 |  | 
 | 202 |   fputs ("Test 5:\n", out); | 
 | 203 |   { | 
 | 204 |     double a = 0, b = 0; | 
 | 205 |     int res, n; | 
 | 206 |  | 
 | 207 |     res = sscanf ("1234567", "%3lg%3lg%n", &a, &b, &n); | 
 | 208 |     printf ("res = %d, a = %g, b = %g, n = %d\n", res, a, b, n); | 
 | 209 |  | 
 | 210 |     if (res != 2 || a != 123 || b != 456 || n != 6) | 
 | 211 |       { | 
 | 212 | 	fputs ("test failed!\n", stdout); | 
 | 213 | 	result = 1; | 
 | 214 |       } | 
 | 215 |  | 
 | 216 |     res = sscanf ("0", "%lg", &a); | 
 | 217 |     printf ("res = %d, a = %g\n", res, a); | 
 | 218 |  | 
 | 219 |     if (res != 1 || a != 0) | 
 | 220 |       { | 
 | 221 | 	fputs ("test failed!\n", stdout); | 
 | 222 | 	result = 1; | 
 | 223 |       } | 
 | 224 |  | 
 | 225 |     res = sscanf ("1e3", "%lg%n", &a, &n); | 
 | 226 |     printf ("res = %d, a = %g, n = %d\n", res, a, n); | 
 | 227 |  | 
 | 228 |     if (res != 1 || a != 1000 || n != 3) | 
 | 229 |       { | 
 | 230 | 	fputs ("test failed!\n", stdout); | 
 | 231 | 	result = 1; | 
 | 232 |       } | 
 | 233 |   } | 
 | 234 |  | 
 | 235 |   fputs ("Test 6:\n", stdout); | 
 | 236 |   { | 
 | 237 |     char *p = (char *) -1; | 
 | 238 |     int res; | 
 | 239 |  | 
 | 240 |     sprintf (buf, "%p", NULL); | 
 | 241 |     res = sscanf (buf, "%p", &p); | 
 | 242 |     printf ("sscanf (\"%s\", \"%%p\", &p) = %d, p == %p\n", buf, res, p); | 
 | 243 |  | 
 | 244 |     if (res != 1 || p != NULL) | 
 | 245 |       { | 
 | 246 | 	fputs ("test failed!\n", stdout); | 
 | 247 | 	result = 1; | 
 | 248 |       } | 
 | 249 |   } | 
 | 250 |  | 
 | 251 |   fputs ("Test 7:\n", stdout); | 
 | 252 |   { | 
 | 253 |     short a[2] = { -1, -1 }; | 
 | 254 |     int res; | 
 | 255 |  | 
 | 256 |     res = sscanf ("32767 1234", "%hd %hd", &a[0], &a[1]); | 
 | 257 |     printf ("res = %d, a[0] = %d, a[1] = %d\n", res, a[0], a[1]); | 
 | 258 |  | 
 | 259 |     if (res != 2 || a[0] != 32767 || a[1] != 1234) | 
 | 260 |       { | 
 | 261 | 	fputs ("test failed!\n", stdout); | 
 | 262 | 	result = 1; | 
 | 263 |       } | 
 | 264 |   } | 
 | 265 |  | 
 | 266 |   fputs ("Test 8:\n", stdout); | 
 | 267 |   { | 
 | 268 |     double d = 123456.789; | 
 | 269 |     int res; | 
 | 270 |  | 
 | 271 |     res = sscanf ("0x1234", "%lf", &d); | 
 | 272 |     printf ("res = %d, d = %f\n", res, d); | 
 | 273 |  | 
 | 274 |     if (res != 1 || d != 4660) | 
 | 275 |       { | 
 | 276 | 	fputs ("test failed!\n", stdout); | 
 | 277 | 	result = 1; | 
 | 278 |       } | 
 | 279 |   } | 
 | 280 |  | 
 | 281 |   fputs ("Test 9:\n", stdout); | 
 | 282 |   { | 
 | 283 |     /* From PR libc/1313 reported by Ben Caradoc-Davies <bmcd@physics.otago.ac.nz>.  */ | 
 | 284 |     float value; | 
 | 285 |     int res; | 
 | 286 |  | 
 | 287 |     res = sscanf ("0123", "%2f", &value); | 
 | 288 |     if (res != 1 || value != 1.0) | 
 | 289 |       { | 
 | 290 | 	fputs ("test failed!\n", stdout); | 
 | 291 | 	result = 1; | 
 | 292 |       } | 
 | 293 |   } | 
 | 294 |  | 
 | 295 |   fputs ("Test 10:\n", stdout); | 
 | 296 |   { | 
 | 297 |     float value; | 
 | 298 |     int res; | 
 | 299 |  | 
 | 300 |     res = sscanf ("--", "%f", &value); | 
 | 301 |     if (res != 0) | 
 | 302 |       { | 
 | 303 | 	fputs ("test failed!\n", stdout); | 
 | 304 | 	result = 1; | 
 | 305 |       } | 
 | 306 |   } | 
 | 307 |  | 
 | 308 |   fputs ("Test 11:\n", stdout); | 
 | 309 |   { | 
 | 310 |     char uart[50]; | 
 | 311 |     int res; | 
 | 312 |  | 
 | 313 |     res = sscanf ("uart:16550A tx:0", "uart:%31s tx:%*u", uart); | 
 | 314 |     if (res != 1) | 
 | 315 |       { | 
 | 316 | 	fputs ("test failed!\n", stdout); | 
 | 317 | 	result = 1; | 
 | 318 |       } | 
 | 319 |   } | 
 | 320 |  | 
 | 321 |   fputs ("Test 12:\n", stdout); | 
 | 322 |   { | 
 | 323 |     char uart[50]; | 
 | 324 |     int res; | 
 | 325 |  | 
 | 326 |     res = sscanf ("uart:16550A", "uart:%31s tx:%*u", uart); | 
 | 327 |     if (res != 1) | 
 | 328 |       { | 
 | 329 | 	fputs ("test failed!\n", stdout); | 
 | 330 | 	result = 1; | 
 | 331 |       } | 
 | 332 |   } | 
 | 333 |  | 
 | 334 |   fputs ("Test 13:\n", stdout); | 
 | 335 |   { | 
 | 336 |     float value; | 
 | 337 |     int res; | 
 | 338 |  | 
 | 339 |     res = sscanf ("-InF", "%f", &value); | 
 | 340 |     if (res != 1 || isinf (value) != -1) | 
 | 341 |       { | 
 | 342 | 	fputs ("test failed!\n", stdout); | 
 | 343 | 	result = 1; | 
 | 344 |       } | 
 | 345 |  | 
 | 346 |     res = sscanf ("+InfiNiTY", "%f", &value); | 
 | 347 |     if (res != 1 || isinf (value) != 1) | 
 | 348 |       { | 
 | 349 | 	fputs ("test failed!\n", stdout); | 
 | 350 | 	result = 1; | 
 | 351 |       } | 
 | 352 |   } | 
 | 353 |  | 
 | 354 |   return result; | 
 | 355 | } |