1 |
786 |
skrzyp |
//=================================================================
|
2 |
|
|
//
|
3 |
|
|
// sscanf.c
|
4 |
|
|
//
|
5 |
|
|
// Testcase for C library sscanf()
|
6 |
|
|
//
|
7 |
|
|
//=================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//=================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): ctarpy, jlarmour
|
43 |
|
|
// Contributors:
|
44 |
|
|
// Date: 2000-04-20
|
45 |
|
|
// Description: Contains testcode for C library sscanf() function
|
46 |
|
|
//
|
47 |
|
|
//
|
48 |
|
|
//####DESCRIPTIONEND####
|
49 |
|
|
|
50 |
|
|
// CONFIGURATION
|
51 |
|
|
|
52 |
|
|
#include <pkgconf/libc_stdio.h> // Configuration header
|
53 |
|
|
|
54 |
|
|
// INCLUDES
|
55 |
|
|
|
56 |
|
|
#include <stdio.h>
|
57 |
|
|
#include <cyg/infra/testcase.h>
|
58 |
|
|
|
59 |
|
|
// HOW TO START TESTS
|
60 |
|
|
|
61 |
|
|
#if defined(CYGFUN_LIBC_STDIO_ungetc)
|
62 |
|
|
|
63 |
|
|
# define START_TEST( test ) test(0)
|
64 |
|
|
|
65 |
|
|
#else
|
66 |
|
|
|
67 |
|
|
# define START_TEST( test ) CYG_EMPTY_STATEMENT
|
68 |
|
|
|
69 |
|
|
#endif
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
// FUNCTIONS
|
73 |
|
|
|
74 |
|
|
#if defined(CYGFUN_LIBC_STDIO_ungetc)
|
75 |
|
|
|
76 |
|
|
// Functions to avoid having to use libc strings
|
77 |
|
|
|
78 |
|
|
static int
|
79 |
|
|
my_strcmp(const char *s1, const char *s2)
|
80 |
|
|
{
|
81 |
|
|
for ( ; *s1 == *s2 ; s1++,s2++ )
|
82 |
|
|
{
|
83 |
|
|
if ( *s1 == '\0')
|
84 |
|
|
break;
|
85 |
|
|
} // for
|
86 |
|
|
|
87 |
|
|
return (*s1 - *s2);
|
88 |
|
|
} // my_strcmp()
|
89 |
|
|
|
90 |
|
|
|
91 |
|
|
static char *
|
92 |
|
|
my_strcpy(char *s1, const char *s2)
|
93 |
|
|
{
|
94 |
|
|
while (*s2 != '\0') {
|
95 |
|
|
*(s1++) = *(s2++);
|
96 |
|
|
}
|
97 |
|
|
*s1 = '\0';
|
98 |
|
|
|
99 |
|
|
return s1;
|
100 |
|
|
} // my_strcpy()
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
static void
|
104 |
|
|
test( CYG_ADDRWORD data )
|
105 |
|
|
{
|
106 |
|
|
static char x[300];
|
107 |
|
|
static char y[300];
|
108 |
|
|
char z[20];
|
109 |
|
|
int ret;
|
110 |
|
|
int int_test, int_test2, int_test3, int_test4;
|
111 |
|
|
|
112 |
|
|
// Check 1
|
113 |
|
|
my_strcpy(x, "20");
|
114 |
|
|
ret = sscanf(x, "%d", &int_test);
|
115 |
|
|
CYG_TEST_PASS_FAIL(int_test==20, "%d test");
|
116 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "%d test return code");
|
117 |
|
|
|
118 |
|
|
// Check 2
|
119 |
|
|
my_strcpy(y, "PigsNoses");
|
120 |
|
|
ret = sscanf(y, "%s", x);
|
121 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, y)==0, "%s test");
|
122 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "%s test return code");
|
123 |
|
|
|
124 |
|
|
// Check 3
|
125 |
|
|
my_strcpy(x, "FE8C");
|
126 |
|
|
ret = sscanf(x, "%X", &int_test);
|
127 |
|
|
CYG_TEST_PASS_FAIL(int_test == 65164, "hex read leading zeroes");
|
128 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "hex read leading zeroes return code");
|
129 |
|
|
|
130 |
|
|
// Check 4
|
131 |
|
|
my_strcpy(x, "000053Ba");
|
132 |
|
|
ret = sscanf(x, "%x", &int_test);
|
133 |
|
|
CYG_TEST_PASS_FAIL(int_test == 21434, "hex read leading zeroes");
|
134 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "hex read leading zeros return code");
|
135 |
|
|
|
136 |
|
|
// Check 5
|
137 |
|
|
my_strcpy(x, "53Ba");
|
138 |
|
|
ret = sscanf(x, "%3x", &int_test);
|
139 |
|
|
CYG_TEST_PASS_FAIL(int_test == 1339, "hex read constrained width");
|
140 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "hex read constrained width return code");
|
141 |
|
|
|
142 |
|
|
// Check 6
|
143 |
|
|
my_strcpy(x, "get the first char test");
|
144 |
|
|
ret = sscanf(x, "%c", &y[0]);
|
145 |
|
|
CYG_TEST_PASS_FAIL(y[0] == 'g', "%c test");
|
146 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "%c test return code");
|
147 |
|
|
|
148 |
|
|
// Check 7
|
149 |
|
|
my_strcpy(x, "-5");
|
150 |
|
|
ret = sscanf(x, "%d", &int_test);
|
151 |
|
|
CYG_TEST_PASS_FAIL(int_test == -5, "negative integer");
|
152 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "negative integer return code");
|
153 |
|
|
|
154 |
|
|
// Check 8
|
155 |
|
|
my_strcpy(x, "53");
|
156 |
|
|
ret = sscanf(x, "%o", &int_test);
|
157 |
|
|
CYG_TEST_PASS_FAIL(int_test == 43, "%o test");
|
158 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "%o test return code");
|
159 |
|
|
|
160 |
|
|
// Check 9
|
161 |
|
|
int_test=10;
|
162 |
|
|
my_strcpy(x, "-5 0");
|
163 |
|
|
ret = sscanf(x, "%*d %d", &int_test);
|
164 |
|
|
CYG_TEST_PASS_FAIL(int_test == 0, "assignment suppression");
|
165 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "assignment suppression return code");
|
166 |
|
|
|
167 |
|
|
// Check 10
|
168 |
|
|
int_test=0;
|
169 |
|
|
my_strcpy(x, "052");
|
170 |
|
|
ret = sscanf(x, "%i", &int_test);
|
171 |
|
|
CYG_TEST_PASS_FAIL(int_test == 42, "octal with %i");
|
172 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "octal with %i return code");
|
173 |
|
|
|
174 |
|
|
// Check 10
|
175 |
|
|
int_test=0;
|
176 |
|
|
my_strcpy(x, "0x05f");
|
177 |
|
|
ret = sscanf(x, "%i", &int_test);
|
178 |
|
|
CYG_TEST_PASS_FAIL(int_test == 95, "hex with %i");
|
179 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "hex with %i return code");
|
180 |
|
|
|
181 |
|
|
// Check 11
|
182 |
|
|
int_test=0;
|
183 |
|
|
my_strcpy(x, "+023456");
|
184 |
|
|
ret = sscanf(x, "%u", &int_test);
|
185 |
|
|
CYG_TEST_PASS_FAIL(int_test == 23456, "unsigned int with %u");
|
186 |
|
|
CYG_TEST_PASS_FAIL(ret == 1, "unsigned int with %u return code");
|
187 |
|
|
|
188 |
|
|
// Check 12
|
189 |
|
|
my_strcpy(x, "aString 2747 D");
|
190 |
|
|
ret = sscanf(x, "%s %d %c", y, &int_test, z);
|
191 |
|
|
CYG_TEST_PASS_FAIL( (my_strcmp(y, "aString")==0) &&
|
192 |
|
|
(int_test == 2747) &&
|
193 |
|
|
(z[0] == 'D'), "A few combinations #1");
|
194 |
|
|
CYG_TEST_PASS_FAIL(ret == 3, "Combinations #1 return code");
|
195 |
|
|
|
196 |
|
|
|
197 |
|
|
// Check 13
|
198 |
|
|
my_strcpy(x, "-6 52 awurble 2747 xxx");
|
199 |
|
|
ret = sscanf(x, "%d %u %s %2d %n %c",
|
200 |
|
|
&int_test, &int_test2, y, &int_test3, &int_test4, z);
|
201 |
|
|
CYG_TEST_PASS_FAIL( (int_test == -6) && (int_test2 == 52) &&
|
202 |
|
|
(my_strcmp(y, "awurble")==0) &&
|
203 |
|
|
(int_test3 == 27) &&
|
204 |
|
|
(int_test4 == 16) &&
|
205 |
|
|
(z[0] == '4'), "A few combinations #2");
|
206 |
|
|
CYG_TEST_PASS_FAIL(ret == 5, "Combinations #2 return code");
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
#ifdef CYGSEM_LIBC_STDIO_SCANF_FLOATING_POINT
|
210 |
|
|
|
211 |
|
|
// How much _relative_ error do we allow?
|
212 |
|
|
#define FLT_TOLERANCE 1.0e-3
|
213 |
|
|
|
214 |
|
|
#define MY_FABS(x) ((x) < 0.0 ? -(x) : (x))
|
215 |
|
|
|
216 |
|
|
CYG_TEST_INFO("Starting floating point specific tests");
|
217 |
|
|
|
218 |
|
|
{
|
219 |
|
|
float flt_test, wanted;
|
220 |
|
|
|
221 |
|
|
// Check 14
|
222 |
|
|
my_strcpy(x, "2.5");
|
223 |
|
|
wanted = 2.5;
|
224 |
|
|
ret = sscanf(x, "%f", &flt_test);
|
225 |
|
|
CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) <
|
226 |
|
|
MY_FABS(wanted * FLT_TOLERANCE),
|
227 |
|
|
"Simple %f test #1" );
|
228 |
|
|
CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test #1 return code" );
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
// Check 15
|
232 |
|
|
my_strcpy(x, "-23.472345");
|
233 |
|
|
wanted = -23.472345;
|
234 |
|
|
ret = sscanf(x, "%f", &flt_test);
|
235 |
|
|
CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) <
|
236 |
|
|
MY_FABS(wanted * FLT_TOLERANCE),
|
237 |
|
|
"Simple %f test #2" );
|
238 |
|
|
CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test #2 return code" );
|
239 |
|
|
|
240 |
|
|
// Check 16
|
241 |
|
|
my_strcpy(x, "0.0");
|
242 |
|
|
ret = sscanf(x, "%f", &flt_test);
|
243 |
|
|
CYG_TEST_PASS_FAIL( flt_test==0.0, "Simple %f test 0.0" );
|
244 |
|
|
CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test 0.0 return code" );
|
245 |
|
|
|
246 |
|
|
|
247 |
|
|
// Check 17
|
248 |
|
|
my_strcpy(x, "-2.6334e00");
|
249 |
|
|
wanted = -2.6334;
|
250 |
|
|
ret = sscanf(x, "%e", &flt_test);
|
251 |
|
|
CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) <
|
252 |
|
|
MY_FABS(wanted * FLT_TOLERANCE),
|
253 |
|
|
"Simple %e test #1" );
|
254 |
|
|
CYG_TEST_PASS_FAIL( ret == 1, "Simple %e test #1 return code" );
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
// Check 18
|
258 |
|
|
my_strcpy(x, "-2.56789e-06");
|
259 |
|
|
wanted = -2.56789e-06;
|
260 |
|
|
ret = sscanf(x, "%e", &flt_test);
|
261 |
|
|
CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) <
|
262 |
|
|
MY_FABS(wanted * FLT_TOLERANCE),
|
263 |
|
|
"Simple %e test #2" );
|
264 |
|
|
CYG_TEST_PASS_FAIL( ret == 1, "Simple %e test #2 return code" );
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
// Check 19
|
268 |
|
|
my_strcpy(x, "-2.12389e-06");
|
269 |
|
|
wanted = -2.12389e-06;
|
270 |
|
|
ret = sscanf(x, "%g", &flt_test);
|
271 |
|
|
CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) <
|
272 |
|
|
MY_FABS(wanted * FLT_TOLERANCE),
|
273 |
|
|
"Simple %g test #1" );
|
274 |
|
|
CYG_TEST_PASS_FAIL( ret == 1, "Simple %g test #1 return code" );
|
275 |
|
|
|
276 |
|
|
// Check 20
|
277 |
|
|
my_strcpy(x, "1.3423345");
|
278 |
|
|
wanted = 1.342335;
|
279 |
|
|
ret = sscanf(x, "%g", &flt_test);
|
280 |
|
|
CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) <
|
281 |
|
|
MY_FABS(wanted * FLT_TOLERANCE),
|
282 |
|
|
"Simple %g test #2" );
|
283 |
|
|
CYG_TEST_PASS_FAIL( ret == 1, "Simple %g test #2 return code" );
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
} // compound
|
287 |
|
|
|
288 |
|
|
#else
|
289 |
|
|
CYG_TEST_PASS("Floating point tests skipped - not configured");
|
290 |
|
|
#endif // ifdef CYGSEM_LIBC_STDIO_SCANF_FLOATING_POINT
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
|
294 |
|
|
"sscanf() function");
|
295 |
|
|
|
296 |
|
|
} // test()
|
297 |
|
|
|
298 |
|
|
#endif // defined(CYGFUN_LIBC_STDIO_ungetc)
|
299 |
|
|
|
300 |
|
|
|
301 |
|
|
int
|
302 |
|
|
main(int argc, char *argv[])
|
303 |
|
|
{
|
304 |
|
|
CYG_TEST_INIT();
|
305 |
|
|
|
306 |
|
|
CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
|
307 |
|
|
"sscanf() function");
|
308 |
|
|
|
309 |
|
|
START_TEST( test );
|
310 |
|
|
|
311 |
|
|
CYG_TEST_NA("Testing is not applicable to this configuration");
|
312 |
|
|
} // main()
|
313 |
|
|
|
314 |
|
|
|
315 |
|
|
// EOF sscanf.c
|