source: branches/libc-0.6/src/libctests/glibc/stdio-common/tst-sscanf.c

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

testcase adjustments.

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 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#ifndef __EMX__
45 "-12345678987654321123456789987654321123456789987654321",
46 "-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321",
47 "-12,345,678987,654,321123,456,789987,654,321123,456,789987,654,321"
48#endif
49};
50
51const char *fmt_long[] =
52{
53 "%9ld%9ld%9ld%9ld%9ld%9ld",
54#ifndef __EMX__
55 "%I9ld%I9ld%I9ld%I9ld%I9ld%I9ld",
56 "%'11ld%'11ld%'11ld%'11ld%'11ld%'11ld",
57 "%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld%I'11ld"
58#endif
59};
60
61const long int val_long[] =
62{
63 -12345678, 987654321, 123456789, 987654321, 123456789, 987654321
64};
65
66struct int_test
67{
68 const char *str;
69 const char *fmt;
70 int retval;
71} int_tests[] =
72{
73 { "foo\n", "foo\nbar", -1 },
74 { "foo\n", "foo bar", -1 },
75 { "foo\n", "foo %d", -1 },
76 { "foo\n", "foo\n%d", -1 },
77 { "foon", "foonbar", -1 },
78 { "foon", "foon%d", -1 },
79 { "foo ", "foo bar", -1 },
80 { "foo ", "foo %d", -1 },
81 { "foo\t", "foo\tbar", -1 },
82 { "foo\t", "foo bar", -1 },
83 { "foo\t", "foo %d", -1 },
84 { "foo\t", "foo\t%d", -1 },
85 { "foo", "foo", 0 },
86 { "foon", "foo bar", 0 },
87 { "foon", "foo %d", 0 },
88 { "foo ", "fooxbar", 0 },
89 { "foo ", "foox%d", 0 },
90 { "foo bar", "foon", 0 },
91 { "foo bar", "foo bar", 0 },
92 { "foo bar", "foo %d", 0 },
93 { "foo bar", "foon%d", 0 },
94 { "foo ", "foo %n", 0 },
95 { "foo%bar1", "foo%%bar%d", 1 },
96 /* Some OSes skip whitespace here while others don't. */
97 { "foo \t %bar1", "foo%%bar%d", 1 }
98};
99
100int
101main (void)
102{
103 double d[6];
104 long l[6];
105 int i, j;
106 int tst_locale;
107 int result = 0;
108
109 tst_locale = 1;
110 if (tst_locale)
111 if (setlocale (LC_ALL, "en_US.ISO-8859-1") == NULL)
112 {
113 puts ("Failed to set en_US locale, skipping locale related tests");
114 tst_locale = 0;
115 }
116
117 for (i = 0; i < 4; ++i)
118 {
119 if (sscanf (str_double[i], "%11lf%11lf%11lf%11lf%11lf%11lf",
120 &d[0], &d[1], &d[2], &d[3], &d[4], &d[5]) != 6)
121 {
122 printf ("Double sscanf test %d wrong number of "
123 "assigned inputs\n", i);
124 result = 1;
125 }
126 else
127 for (j = 0; j < 6; ++j)
128 if (d[j] != val_double[6 * i + j])
129 {
130 printf ("Double sscanf test %d failed (%g instead of %g)\n",
131 i, d[j], val_double[6 * i + j]);
132 result = 1;
133 break;
134 }
135 }
136
137#ifdef __EMX__
138 for (i = 0; i < 1; ++i)
139#else
140 for (i = 0; i < 4; ++i)
141#endif
142 {
143 if (sscanf (str_long[i], fmt_long[i],
144 &l[0], &l[1], &l[2], &l[3], &l[4], &l[5]) != 6)
145 {
146 printf ("Integer sscanf test %d wrong number of "
147 "assigned inputs\n", i);
148 result = 1;
149 }
150 else
151 for (j = 0; j < 6; ++j)
152 if (l[j] != val_long[j])
153 {
154 printf ("Integer sscanf test %d failed (%ld instead %ld)\n",
155 i, l[j], val_long[j]);
156 result = 1;
157 break;
158 }
159
160 if (! tst_locale)
161 break;
162 }
163
164 for (i = 0; i < sizeof (int_tests) / sizeof (int_tests[0]); ++i)
165 {
166 int dummy, ret;
167
168 if ((ret = sscanf (int_tests[i].str, int_tests[i].fmt,
169 &dummy)) != int_tests[i].retval)
170 {
171 printf ("int_tests[%d] returned %d != %d\n",
172 i, ret, int_tests[i].retval);
173 result = 1;
174 }
175 }
176
177 return result;
178}
Note: See TracBrowser for help on using the repository browser.