source: vendor/glibc-tests/glibc/stdio-common/tst-sscanf.c

Last change on this file was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
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.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <locale.h>
23
24const char *str_double[] =
25{
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"
30};
31
32const double val_double[] =
33{
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
39};
40
41const char *str_long[] =
42{
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"
47};
48
49const char *fmt_long[] =
50{
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"
55};
56
57const long int val_long[] =
58{
59 -12345678, 987654321, 123456789, 987654321, 123456789, 987654321
60};
61
62struct int_test
63{
64 const char *str;
65 const char *fmt;
66 int retval;
67} int_tests[] =
68{
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 }
94};
95
96int
97main (void)
98{
99 double d[6];
100 long l[6];
101 int i, j;
102 int tst_locale;
103 int result = 0;
104
105 tst_locale = 1;
106 if (tst_locale)
107 if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
108 {
109 puts ("Failed to set en_US locale, skipping locale related tests");
110 tst_locale = 0;
111 }
112
113 for (i = 0; i < 4; ++i)
114 {
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)
117 {
118 printf ("Double sscanf test %d wrong number of "
119 "assigned inputs\n", i);
120 result = 1;
121 }
122 else
123 for (j = 0; j < 6; ++j)
124 if (d[j] != val_double[6 * i + j])
125 {
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;
130 }
131 }
132
133 for (i = 0; i < 4; ++i)
134 {
135 if (sscanf (str_long[i], fmt_long[i],
136 &l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
137 {
138 printf ("Integer sscanf test %d wrong number of "
139 "assigned inputs\n", i);
140 result = 1;
141 }
142 else
143 for (j = 0; j < 6; ++j)
144 if (l[j] != val_long[j])
145 {
146 printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
147 i, l[j], val_long[j]);
148 result = 1;
149 break;
150 }
151
152 if (! tst_locale)
153 break;
154 }
155
156 for (i = 0; i < sizeof (int_tests) / sizeof (int_tests[0]); ++i)
157 {
158 int dummy, ret;
159
160 if ((ret = sscanf (int_tests[i].str, int_tests[i].fmt,
161 &dummy)) != int_tests[i].retval)
162 {
163 printf ("int_tests[%d] returned %d != %d\n",
164 i, ret, int_tests[i].retval);
165 result = 1;
166 }
167 }
168
169 return result;
170}
Note: See TracBrowser for help on using the repository browser.