Fix parsing of 0e+0 as float
[platform/upstream/glibc.git] / stdio-common / tst-sscanf.c
1 /* Copyright (C) 2000-2013 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Jakub Jelinek <jakub@redhat.com>, 2000.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <locale.h>
22
23 #ifndef CHAR
24 # define CHAR char
25 # define L(str) str
26 # define SSCANF sscanf
27 #endif
28
29 const CHAR *str_double[] =
30 {
31   L("-.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
32   L("0.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
33   L("-1234567E0198765432E0912345678901987654321091234567890198765432109"),
34   L("-0.1000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01")
35 };
36
37 const double val_double[] =
38 {
39   -.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
40   0.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
41   -1234567E01, 98765432E09, 12345678901.0, 98765432109.0, 12345678901.0,
42     98765432109.0,
43   -0.1000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01
44 };
45
46 const CHAR *str_long[] =
47 {
48   L("-12345678987654321123456789987654321123456789987654321"),
49   L("-12345678987654321123456789987654321123456789987654321"),
50   L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321"),
51   L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321")
52 };
53
54 const CHAR *fmt_long[] =
55 {
56   L("%9ld%9ld%9ld%9ld%9ld%9ld"),
57   L("%I9ld%I9ld%I9ld%I9ld%I9ld%I9ld"),
58   L("%'11ld%'11ld%'11ld%'11ld%'11ld%'11ld"),
59   L("%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld")
60 };
61
62 const long int val_long[] =
63 {
64   -12345678, 987654321, 123456789, 987654321, 123456789, 987654321
65 };
66
67 struct test
68 {
69   const CHAR *str;
70   const CHAR *fmt;
71   int retval;
72 } int_tests[] =
73 {
74   { L("foo\n"), L("foo\nbar"), -1 },
75   { L("foo\n"), L("foo bar"), -1 },
76   { L("foo\n"), L("foo %d"), -1 },
77   { L("foo\n"), L("foo\n%d"), -1 },
78   { L("foon"), L("foonbar"), -1 },
79   { L("foon"), L("foon%d"), -1 },
80   { L("foo "), L("foo bar"), -1 },
81   { L("foo "), L("foo %d"), -1 },
82   { L("foo\t"), L("foo\tbar"), -1 },
83   { L("foo\t"), L("foo bar"), -1 },
84   { L("foo\t"), L("foo %d"), -1 },
85   { L("foo\t"), L("foo\t%d"), -1 },
86   { L("foo"), L("foo"), 0 },
87   { L("foon"), L("foo bar"), 0 },
88   { L("foon"), L("foo %d"), 0 },
89   { L("foo "), L("fooxbar"), 0 },
90   { L("foo "), L("foox%d"), 0 },
91   { L("foo bar"), L("foon"), 0 },
92   { L("foo bar"), L("foo bar"), 0 },
93   { L("foo bar"), L("foo %d"), 0 },
94   { L("foo bar"), L("foon%d"), 0 },
95   { L("foo "), L("foo %n"), 0 },
96   { L("foo%bar1"), L("foo%%bar%d"), 1 },
97   /* Some OSes skip whitespace here while others don't.  */
98   { L("foo \t %bar1"), L("foo%%bar%d"), 1 }
99 };
100
101 struct test double_tests[] =
102 {
103   { L("-1"), L("%1g"), 0 },
104   { L("-.1"), L("%2g"), 0 },
105   { L("-inf"), L("%3g"), 0 },
106   { L("+0"), L("%1g"),  },
107   { L("-0x1p0"), L("%2g"), 1 },
108   { L("-..1"), L("%g"), 0 },
109   { L("-inf"), L("%g"), 1 }
110 };
111
112 struct test2
113 {
114   const CHAR *str;
115   const CHAR *fmt;
116   int retval;
117   char residual;
118 } double_tests2[] =
119 {
120   { L("0e+0"), L("%g%c"), 1, 0 },
121   { L("0xe+0"), L("%g%c"), 2, '+' },
122   { L("0x.e+0"), L("%g%c"), 2, '+' },
123 };
124
125 int
126 main (void)
127 {
128   double d[6];
129   long l[6];
130   int i, j;
131   int tst_locale;
132   int result = 0;
133
134   tst_locale = 1;
135   if (tst_locale)
136     if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
137       {
138         puts ("Failed to set en_US locale, skipping locale related tests");
139         tst_locale = 0;
140       }
141
142   for (i = 0; i < 4; ++i)
143     {
144       if (SSCANF (str_double[i], L("%11lf%11lf%11lf%11lf%11lf%11lf"),
145                   &d[0], &d[1], &d[2], &d[3], &d[4], &d[5]) != 6)
146         {
147           printf ("Double sscanf test %d wrong number of "
148                   "assigned inputs\n", i);
149           result = 1;
150         }
151       else
152         for (j = 0; j < 6; ++j)
153           if (d[j] != val_double[6 * i + j])
154             {
155               printf ("Double sscanf test %d failed (%g instead of %g)\n",
156                       i, d[j], val_double[6 * i + j]);
157               result = 1;
158               break;
159             }
160     }
161
162   for (i = 0; i < 4; ++i)
163     {
164       if (SSCANF (str_long[i], fmt_long[i],
165                   &l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
166         {
167           printf ("Integer sscanf test %d wrong number of "
168                   "assigned inputs\n", i);
169           result = 1;
170         }
171       else
172         for (j = 0; j < 6; ++j)
173           if (l[j] != val_long[j])
174             {
175               printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
176                       i, l[j], val_long[j]);
177               result = 1;
178               break;
179             }
180
181       if (! tst_locale)
182         break;
183     }
184
185   for (i = 0; i < sizeof (int_tests) / sizeof (int_tests[0]); ++i)
186     {
187       int dummy, ret;
188
189       if ((ret = SSCANF (int_tests[i].str, int_tests[i].fmt,
190                          &dummy)) != int_tests[i].retval)
191         {
192           printf ("int_tests[%d] returned %d != %d\n",
193                   i, ret, int_tests[i].retval);
194           result = 1;
195         }
196     }
197
198   for (i = 0; i < sizeof (double_tests) / sizeof (double_tests[0]); ++i)
199     {
200       double dummy;
201       int ret;
202
203       if ((ret = SSCANF (double_tests[i].str, double_tests[i].fmt,
204                          &dummy)) != double_tests[i].retval)
205         {
206           printf ("double_tests[%d] returned %d != %d\n",
207                   i, ret, double_tests[i].retval);
208           result = 1;
209         }
210     }
211
212   for (i = 0; i < sizeof (double_tests2) / sizeof (double_tests2[0]); ++i)
213     {
214       double dummy;
215       int ret;
216       char c = 0;
217
218       if ((ret = SSCANF (double_tests2[i].str, double_tests2[i].fmt,
219                          &dummy, &c)) != double_tests2[i].retval)
220         {
221           printf ("double_tests2[%d] returned %d != %d\n",
222                   i, ret, double_tests2[i].retval);
223           result = 1;
224         }
225       else if (ret == 2 && c != double_tests2[i].residual)
226         {
227           printf ("double_tests2[%d] stopped at '%c' != '%c'\n",
228                   i, c, double_tests2[i].residual);
229           result = 1;
230         }
231     }
232
233   return result;
234 }