1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/* Copyright (C) 2000, 2002, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Jakub Jelinek <jakub@redhat.com>, 2000.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
const char *str_double[] =
{
"-.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01",
"0.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01",
"-1234567E0198765432E0912345678901987654321091234567890198765432109",
"-0.1000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"
};
const double val_double[] =
{
-.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
0.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
-1234567E01, 98765432E09, 12345678901.0, 98765432109.0, 12345678901.0,
98765432109.0,
-0.1000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01
};
const char *str_long[] =
{
"-12345678987654321123456789987654321123456789987654321",
"-12345678987654321123456789987654321123456789987654321",
"-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321",
"-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321"
};
const char *fmt_long[] =
{
"%9ld%9ld%9ld%9ld%9ld%9ld",
"%I9ld%I9ld%I9ld%I9ld%I9ld%I9ld",
"%'11ld%'11ld%'11ld%'11ld%'11ld%'11ld",
"%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld"
};
const long int val_long[] =
{
-12345678, 987654321, 123456789, 987654321, 123456789, 987654321
};
struct int_test
{
const char *str;
const char *fmt;
int retval;
} int_tests[] =
{
{ "foo\n", "foo\nbar", -1 },
{ "foo\n", "foo bar", -1 },
{ "foo\n", "foo %d", -1 },
{ "foo\n", "foo\n%d", -1 },
{ "foon", "foonbar", -1 },
{ "foon", "foon%d", -1 },
{ "foo ", "foo bar", -1 },
{ "foo ", "foo %d", -1 },
{ "foo\t", "foo\tbar", -1 },
{ "foo\t", "foo bar", -1 },
{ "foo\t", "foo %d", -1 },
{ "foo\t", "foo\t%d", -1 },
{ "foo", "foo", 0 },
{ "foon", "foo bar", 0 },
{ "foon", "foo %d", 0 },
{ "foo ", "fooxbar", 0 },
{ "foo ", "foox%d", 0 },
{ "foo bar", "foon", 0 },
{ "foo bar", "foo bar", 0 },
{ "foo bar", "foo %d", 0 },
{ "foo bar", "foon%d", 0 },
{ "foo ", "foo %n", 0 },
{ "foo%bar1", "foo%%bar%d", 1 },
/* Some OSes skip whitespace here while others don't. */
{ "foo \t %bar1", "foo%%bar%d", 1 }
};
int
main (void)
{
double d[6];
long l[6];
int i, j;
int tst_locale;
int result = 0;
tst_locale = 1;
if (tst_locale)
if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
{
puts ("Failed to set en_US locale, skipping locale related tests");
tst_locale = 0;
}
for (i = 0; i < 4; ++i)
{
if (sscanf (str_double[i], "%11lf%11lf%11lf%11lf%11lf%11lf",
&d[0], &d[1], &d[2], &d[3], &d[4], &d[5]) != 6)
{
printf ("Double sscanf test %d wrong number of "
"assigned inputs\n", i);
result = 1;
}
else
for (j = 0; j < 6; ++j)
if (d[j] != val_double[6 * i + j])
{
printf ("Double sscanf test %d failed (%g instead of %g)\n",
i, d[j], val_double[6 * i + j]);
result = 1;
break;
}
}
for (i = 0; i < 4; ++i)
{
if (sscanf (str_long[i], fmt_long[i],
&l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
{
printf ("Integer sscanf test %d wrong number of "
"assigned inputs\n", i);
result = 1;
}
else
for (j = 0; j < 6; ++j)
if (l[j] != val_long[j])
{
printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
i, l[j], val_long[j]);
result = 1;
break;
}
if (! tst_locale)
break;
}
for (i = 0; i < sizeof (int_tests) / sizeof (int_tests[0]); ++i)
{
int dummy, ret;
if ((ret = sscanf (int_tests[i].str, int_tests[i].fmt,
&dummy)) != int_tests[i].retval)
{
printf ("int_tests[%d] returned %d != %d\n",
i, ret, int_tests[i].retval);
result = 1;
}
}
return result;
}
|