WP issues
[glibc.git] / stdio-common / tst-sscanf.c
bloba987e8779778098f7044c94da15db0801e51fc6f
1 /* Copyright (C) 2000, 2002, 2004, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2000.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <locale.h>
24 #ifndef CHAR
25 # define CHAR char
26 # define L(str) str
27 # define SSCANF sscanf
28 #endif
30 const CHAR *str_double[] =
32 L("-.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
33 L("0.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"),
34 L("-1234567E0198765432E0912345678901987654321091234567890198765432109"),
35 L("-0.1000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01")
38 const double val_double[] =
40 -.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
41 0.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
42 -1234567E01, 98765432E09, 12345678901.0, 98765432109.0, 12345678901.0,
43 98765432109.0,
44 -0.1000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01
47 const CHAR *str_long[] =
49 L("-12345678987654321123456789987654321123456789987654321"),
50 L("-12345678987654321123456789987654321123456789987654321"),
51 L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321"),
52 L("-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321")
55 const CHAR *fmt_long[] =
57 L("%9ld%9ld%9ld%9ld%9ld%9ld"),
58 L("%I9ld%I9ld%I9ld%I9ld%I9ld%I9ld"),
59 L("%'11ld%'11ld%'11ld%'11ld%'11ld%'11ld"),
60 L("%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld")
63 const long int val_long[] =
65 -12345678, 987654321, 123456789, 987654321, 123456789, 987654321
68 struct test
70 const CHAR *str;
71 const CHAR *fmt;
72 int retval;
73 } int_tests[] =
75 { L("foo\n"), L("foo\nbar"), -1 },
76 { L("foo\n"), L("foo bar"), -1 },
77 { L("foo\n"), L("foo %d"), -1 },
78 { L("foo\n"), L("foo\n%d"), -1 },
79 { L("foon"), L("foonbar"), -1 },
80 { L("foon"), L("foon%d"), -1 },
81 { L("foo "), L("foo bar"), -1 },
82 { L("foo "), L("foo %d"), -1 },
83 { L("foo\t"), L("foo\tbar"), -1 },
84 { L("foo\t"), L("foo bar"), -1 },
85 { L("foo\t"), L("foo %d"), -1 },
86 { L("foo\t"), L("foo\t%d"), -1 },
87 { L("foo"), L("foo"), 0 },
88 { L("foon"), L("foo bar"), 0 },
89 { L("foon"), L("foo %d"), 0 },
90 { L("foo "), L("fooxbar"), 0 },
91 { L("foo "), L("foox%d"), 0 },
92 { L("foo bar"), L("foon"), 0 },
93 { L("foo bar"), L("foo bar"), 0 },
94 { L("foo bar"), L("foo %d"), 0 },
95 { L("foo bar"), L("foon%d"), 0 },
96 { L("foo "), L("foo %n"), 0 },
97 { L("foo%bar1"), L("foo%%bar%d"), 1 },
98 /* Some OSes skip whitespace here while others don't. */
99 { L("foo \t %bar1"), L("foo%%bar%d"), 1 }
102 struct test double_tests[] =
104 { L("-1"), L("%1g"), 0 },
105 { L("-.1"), L("%2g"), 0 },
106 { L("-inf"), L("%3g"), 0 },
107 { L("+0"), L("%1g"), },
108 { L("-0x1p0"), L("%2g"), 1 },
109 { L("-..1"), L("%g"), 0 },
110 { L("-inf"), L("%g"), 1 }
114 main (void)
116 double d[6];
117 long l[6];
118 int i, j;
119 int tst_locale;
120 int result = 0;
122 tst_locale = 1;
123 if (tst_locale)
124 if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
126 puts ("Failed to set en_US locale, skipping locale related tests");
127 tst_locale = 0;
130 for (i = 0; i < 4; ++i)
132 if (SSCANF (str_double[i], L("%11lf%11lf%11lf%11lf%11lf%11lf"),
133 &d[0], &d[1], &d[2], &d[3], &d[4], &d[5]) != 6)
135 printf ("Double sscanf test %d wrong number of "
136 "assigned inputs\n", i);
137 result = 1;
139 else
140 for (j = 0; j < 6; ++j)
141 if (d[j] != val_double[6 * i + j])
143 printf ("Double sscanf test %d failed (%g instead of %g)\n",
144 i, d[j], val_double[6 * i + j]);
145 result = 1;
146 break;
150 for (i = 0; i < 4; ++i)
152 if (SSCANF (str_long[i], fmt_long[i],
153 &l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
155 printf ("Integer sscanf test %d wrong number of "
156 "assigned inputs\n", i);
157 result = 1;
159 else
160 for (j = 0; j < 6; ++j)
161 if (l[j] != val_long[j])
163 printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
164 i, l[j], val_long[j]);
165 result = 1;
166 break;
169 if (! tst_locale)
170 break;
173 for (i = 0; i < sizeof (int_tests) / sizeof (int_tests[0]); ++i)
175 int dummy, ret;
177 if ((ret = SSCANF (int_tests[i].str, int_tests[i].fmt,
178 &dummy)) != int_tests[i].retval)
180 printf ("int_tests[%d] returned %d != %d\n",
181 i, ret, int_tests[i].retval);
182 result = 1;
186 for (i = 0; i < sizeof (double_tests) / sizeof (double_tests[0]); ++i)
188 double dummy;
189 int ret;
191 if ((ret = SSCANF (double_tests[i].str, double_tests[i].fmt,
192 &dummy)) != double_tests[i].retval)
194 printf ("double_tests[%d] returned %d != %d\n",
195 i, ret, double_tests[i].retval);
196 result = 1;
200 return result;