2005-03-15 Jakub Jelinek <jakub@redhat.com>
[glibc/history.git] / stdio-common / tst-sscanf.c
blobe710e190d79f78c8ef2de081002c09c4115de89c
1 /* Copyright (C) 2000, 2002, 2004 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 const char *str_double[] =
26 "-.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01",
27 "0.10000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01",
28 "-1234567E0198765432E0912345678901987654321091234567890198765432109",
29 "-0.1000E+020.20000E+020.25000E+010.40000E+010.50000E+010.12500E+01"
32 const double val_double[] =
34 -.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
35 0.10000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01,
36 -1234567E01, 98765432E09, 12345678901.0, 98765432109.0, 12345678901.0,
37 98765432109.0,
38 -0.1000E+02, 0.20000E+02, 0.25000E+01, 0.40000E+01, 0.50000E+01, 0.12500E+01
41 const char *str_long[] =
43 "-12345678987654321123456789987654321123456789987654321",
44 "-12345678987654321123456789987654321123456789987654321",
45 "-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321",
46 "-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321"
49 const char *fmt_long[] =
51 "%9ld%9ld%9ld%9ld%9ld%9ld",
52 "%I9ld%I9ld%I9ld%I9ld%I9ld%I9ld",
53 "%'11ld%'11ld%'11ld%'11ld%'11ld%'11ld",
54 "%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld"
57 const long int val_long[] =
59 -12345678, 987654321, 123456789, 987654321, 123456789, 987654321
62 struct int_test
64 const char *str;
65 const char *fmt;
66 int retval;
67 } int_tests[] =
69 { "foo\n", "foo\nbar", -1 },
70 { "foo\n", "foo bar", -1 },
71 { "foo\n", "foo %d", -1 },
72 { "foo\n", "foo\n%d", -1 },
73 { "foon", "foonbar", -1 },
74 { "foon", "foon%d", -1 },
75 { "foo ", "foo bar", -1 },
76 { "foo ", "foo %d", -1 },
77 { "foo\t", "foo\tbar", -1 },
78 { "foo\t", "foo bar", -1 },
79 { "foo\t", "foo %d", -1 },
80 { "foo\t", "foo\t%d", -1 },
81 { "foo", "foo", 0 },
82 { "foon", "foo bar", 0 },
83 { "foon", "foo %d", 0 },
84 { "foo ", "fooxbar", 0 },
85 { "foo ", "foox%d", 0 },
86 { "foo bar", "foon", 0 },
87 { "foo bar", "foo bar", 0 },
88 { "foo bar", "foo %d", 0 },
89 { "foo bar", "foon%d", 0 },
90 { "foo ", "foo %n", 0 },
91 { "foo%bar1", "foo%%bar%d", 1 },
92 /* Some OSes skip whitespace here while others don't. */
93 { "foo \t %bar1", "foo%%bar%d", 1 }
96 int
97 main (void)
99 double d[6];
100 long l[6];
101 int i, j;
102 int tst_locale;
103 int result = 0;
105 tst_locale = 1;
106 if (tst_locale)
107 if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
109 puts ("Failed to set en_US locale, skipping locale related tests");
110 tst_locale = 0;
113 for (i = 0; i < 4; ++i)
115 if (sscanf (str_double[i], "%11lf%11lf%11lf%11lf%11lf%11lf",
116 &d[0], &d[1], &d[2], &d[3], &d[4], &d[5]) != 6)
118 printf ("Double sscanf test %d wrong number of "
119 "assigned inputs\n", i);
120 result = 1;
122 else
123 for (j = 0; j < 6; ++j)
124 if (d[j] != val_double[6 * i + j])
126 printf ("Double sscanf test %d failed (%g instead of %g)\n",
127 i, d[j], val_double[6 * i + j]);
128 result = 1;
129 break;
133 for (i = 0; i < 4; ++i)
135 if (sscanf (str_long[i], fmt_long[i],
136 &l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
138 printf ("Integer sscanf test %d wrong number of "
139 "assigned inputs\n", i);
140 result = 1;
142 else
143 for (j = 0; j < 6; ++j)
144 if (l[j] != val_long[j])
146 printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
147 i, l[j], val_long[j]);
148 result = 1;
149 break;
152 if (! tst_locale)
153 break;
156 for (i = 0; i < sizeof (int_tests) / sizeof (int_tests[0]); ++i)
158 int dummy, ret;
160 if ((ret = sscanf (int_tests[i].str, int_tests[i].fmt,
161 &dummy)) != int_tests[i].retval)
163 printf ("int_tests[%d] returned %d != %d\n",
164 i, ret, int_tests[i].retval);
165 result = 1;
169 return result;