Back to index

glibc  2.9
tst-sscanf.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000, 2002, 2004, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2000.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <locale.h>
00023 
00024 #ifndef CHAR
00025 # define CHAR char
00026 # define L(str) str
00027 # define SSCANF sscanf
00028 #endif
00029 
00030 const CHAR *str_double[] =
00031 {
00032   L("-.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
00033   L("0.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
00034   L("-1234567E0198765432E0912345678901987654321091234567890198765432109"),
00035   L("-0.1000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01")
00036 };
00037 
00038 const double val_double[] =
00039 {
00040   -.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
00041   0.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
00042   -1234567E01, 98765432E09, 12345678901.0, 98765432109.0, 12345678901.0,
00043     98765432109.0,
00044   -0.1000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01
00045 };
00046 
00047 const CHAR *str_long[] =
00048 {
00049   L("-12345678987654321123456789987654321123456789987654321"),
00050   L("-12345678987654321123456789987654321123456789987654321"),
00051   L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321"),
00052   L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321")
00053 };
00054 
00055 const CHAR *fmt_long[] =
00056 {
00057   L("%9ld%9ld%9ld%9ld%9ld%9ld"),
00058   L("%I9ld%I9ld%I9ld%I9ld%I9ld%I9ld"),
00059   L("%'11ld%'11ld%'11ld%'11ld%'11ld%'11ld"),
00060   L("%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld")
00061 };
00062 
00063 const long int val_long[] =
00064 {
00065   -12345678, 987654321, 123456789, 987654321, 123456789, 987654321
00066 };
00067 
00068 struct test
00069 {
00070   const CHAR *str;
00071   const CHAR *fmt;
00072   int retval;
00073 } int_tests[] =
00074 {
00075   { L("foo\n"), L("foo\nbar"), -1 },
00076   { L("foo\n"), L("foo bar"), -1 },
00077   { L("foo\n"), L("foo %d"), -1 },
00078   { L("foo\n"), L("foo\n%d"), -1 },
00079   { L("foon"), L("foonbar"), -1 },
00080   { L("foon"), L("foon%d"), -1 },
00081   { L("foo "), L("foo bar"), -1 },
00082   { L("foo "), L("foo %d"), -1 },
00083   { L("foo\t"), L("foo\tbar"), -1 },
00084   { L("foo\t"), L("foo bar"), -1 },
00085   { L("foo\t"), L("foo %d"), -1 },
00086   { L("foo\t"), L("foo\t%d"), -1 },
00087   { L("foo"), L("foo"), 0 },
00088   { L("foon"), L("foo bar"), 0 },
00089   { L("foon"), L("foo %d"), 0 },
00090   { L("foo "), L("fooxbar"), 0 },
00091   { L("foo "), L("foox%d"), 0 },
00092   { L("foo bar"), L("foon"), 0 },
00093   { L("foo bar"), L("foo bar"), 0 },
00094   { L("foo bar"), L("foo %d"), 0 },
00095   { L("foo bar"), L("foon%d"), 0 },
00096   { L("foo "), L("foo %n"), 0 },
00097   { L("foo%bar1"), L("foo%%bar%d"), 1 },
00098   /* Some OSes skip whitespace here while others don't.  */
00099   { L("foo \t %bar1"), L("foo%%bar%d"), 1 }
00100 };
00101 
00102 struct test double_tests[] =
00103 {
00104   { L("-1"), L("%1g"), 0 },
00105   { L("-.1"), L("%2g"), 0 },
00106   { L("-inf"), L("%3g"), 0 },
00107   { L("+0"), L("%1g"),  },
00108   { L("-0x1p0"), L("%2g"), 1 },
00109   { L("-..1"), L("%g"), 0 },
00110   { L("-inf"), L("%g"), 1 }
00111 };
00112 
00113 int
00114 main (void)
00115 {
00116   double d[6];
00117   long l[6];
00118   int i, j;
00119   int tst_locale;
00120   int result = 0;
00121 
00122   tst_locale = 1;
00123   if (tst_locale)
00124     if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
00125       {
00126        puts ("Failed to set en_US locale, skipping locale related tests");
00127        tst_locale = 0;
00128       }
00129 
00130   for (i = 0; i < 4; ++i)
00131     {
00132       if (SSCANF (str_double[i], L("%11lf%11lf%11lf%11lf%11lf%11lf"),
00133                 &d[0], &d[1], &d[2], &d[3], &d[4], &d[5]) != 6)
00134        {
00135          printf ("Double sscanf test %d wrong number of "
00136                 "assigned inputs\n", i);
00137          result = 1;
00138        }
00139       else
00140        for (j = 0; j < 6; ++j)
00141          if (d[j] != val_double[6 * i + j])
00142            {
00143              printf ("Double sscanf test %d failed (%g instead of %g)\n",
00144                     i, d[j], val_double[6 * i + j]);
00145              result = 1;
00146              break;
00147            }
00148     }
00149 
00150   for (i = 0; i < 4; ++i)
00151     {
00152       if (SSCANF (str_long[i], fmt_long[i],
00153                 &l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
00154        {
00155          printf ("Integer sscanf test %d wrong number of "
00156                 "assigned inputs\n", i);
00157          result = 1;
00158        }
00159       else
00160        for (j = 0; j < 6; ++j)
00161          if (l[j] != val_long[j])
00162            {
00163              printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
00164                     i, l[j], val_long[j]);
00165              result = 1;
00166              break;
00167            }
00168 
00169       if (! tst_locale)
00170        break;
00171     }
00172 
00173   for (i = 0; i < sizeof (int_tests) / sizeof (int_tests[0]); ++i)
00174     {
00175       int dummy, ret;
00176 
00177       if ((ret = SSCANF (int_tests[i].str, int_tests[i].fmt,
00178                       &dummy)) != int_tests[i].retval)
00179        {
00180          printf ("int_tests[%d] returned %d != %d\n",
00181                 i, ret, int_tests[i].retval);
00182          result = 1;
00183        }
00184     }
00185 
00186   for (i = 0; i < sizeof (double_tests) / sizeof (double_tests[0]); ++i)
00187     {
00188       double dummy;
00189       int ret;
00190 
00191       if ((ret = SSCANF (double_tests[i].str, double_tests[i].fmt,
00192                       &dummy)) != double_tests[i].retval)
00193        {
00194          printf ("double_tests[%d] returned %d != %d\n",
00195                 i, ret, double_tests[i].retval);
00196          result = 1;
00197        }
00198     }
00199 
00200   return result;
00201 }