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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
//=================================================================
//
// sscanf.c
//
// Testcase for C library sscanf()
//
//=================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later
// version.
//
// eCos 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 General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eCos; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// As a special exception, if other files instantiate templates or use
// macros or inline functions from this file, or you compile this file
// and link it with other works to produce a work based on this file,
// this file does not by itself cause the resulting work to be covered by
// the GNU General Public License. However the source code for this file
// must still be made available in accordance with section (3) of the GNU
// General Public License v2.
//
// This exception does not invalidate any other reasons why a work based
// on this file might be covered by the GNU General Public License.
// -------------------------------------------
// ####ECOSGPLCOPYRIGHTEND####
//=================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): ctarpy, jlarmour
// Contributors:
// Date: 2000-04-20
// Description: Contains testcode for C library sscanf() function
//
//
//####DESCRIPTIONEND####
// CONFIGURATION
#include <pkgconf/libc_stdio.h> // Configuration header
// INCLUDES
#include <stdio.h>
#include <cyg/infra/testcase.h>
// HOW TO START TESTS
#if defined(CYGFUN_LIBC_STDIO_ungetc)
# define START_TEST( test ) test(0)
#else
# define START_TEST( test ) CYG_EMPTY_STATEMENT
#endif
// FUNCTIONS
#if defined(CYGFUN_LIBC_STDIO_ungetc)
// Functions to avoid having to use libc strings
static int
my_strcmp(const char *s1, const char *s2)
{
for ( ; *s1 == *s2 ; s1++,s2++ )
{
if ( *s1 == '\0')
break;
} // for
return (*s1 - *s2);
} // my_strcmp()
static char *
my_strcpy(char *s1, const char *s2)
{
while (*s2 != '\0') {
*(s1++) = *(s2++);
}
*s1 = '\0';
return s1;
} // my_strcpy()
static void
test( CYG_ADDRWORD data )
{
static char x[300];
static char y[300];
char z[20];
int ret;
int int_test, int_test2, int_test3, int_test4;
// Check 1
my_strcpy(x, "20");
ret = sscanf(x, "%d", &int_test);
CYG_TEST_PASS_FAIL(int_test==20, "%d test");
CYG_TEST_PASS_FAIL(ret == 1, "%d test return code");
// Check 2
my_strcpy(y, "PigsNoses");
ret = sscanf(y, "%s", x);
CYG_TEST_PASS_FAIL(my_strcmp(x, y)==0, "%s test");
CYG_TEST_PASS_FAIL(ret == 1, "%s test return code");
// Check 3
my_strcpy(x, "FE8C");
ret = sscanf(x, "%X", &int_test);
CYG_TEST_PASS_FAIL(int_test == 65164, "hex read leading zeroes");
CYG_TEST_PASS_FAIL(ret == 1, "hex read leading zeroes return code");
// Check 4
my_strcpy(x, "000053Ba");
ret = sscanf(x, "%x", &int_test);
CYG_TEST_PASS_FAIL(int_test == 21434, "hex read leading zeroes");
CYG_TEST_PASS_FAIL(ret == 1, "hex read leading zeros return code");
// Check 5
my_strcpy(x, "53Ba");
ret = sscanf(x, "%3x", &int_test);
CYG_TEST_PASS_FAIL(int_test == 1339, "hex read constrained width");
CYG_TEST_PASS_FAIL(ret == 1, "hex read constrained width return code");
// Check 6
my_strcpy(x, "get the first char test");
ret = sscanf(x, "%c", &y[0]);
CYG_TEST_PASS_FAIL(y[0] == 'g', "%c test");
CYG_TEST_PASS_FAIL(ret == 1, "%c test return code");
// Check 7
my_strcpy(x, "-5");
ret = sscanf(x, "%d", &int_test);
CYG_TEST_PASS_FAIL(int_test == -5, "negative integer");
CYG_TEST_PASS_FAIL(ret == 1, "negative integer return code");
// Check 8
my_strcpy(x, "53");
ret = sscanf(x, "%o", &int_test);
CYG_TEST_PASS_FAIL(int_test == 43, "%o test");
CYG_TEST_PASS_FAIL(ret == 1, "%o test return code");
// Check 9
int_test=10;
my_strcpy(x, "-5 0");
ret = sscanf(x, "%*d %d", &int_test);
CYG_TEST_PASS_FAIL(int_test == 0, "assignment suppression");
CYG_TEST_PASS_FAIL(ret == 1, "assignment suppression return code");
// Check 10
int_test=0;
my_strcpy(x, "052");
ret = sscanf(x, "%i", &int_test);
CYG_TEST_PASS_FAIL(int_test == 42, "octal with %i");
CYG_TEST_PASS_FAIL(ret == 1, "octal with %i return code");
// Check 10
int_test=0;
my_strcpy(x, "0x05f");
ret = sscanf(x, "%i", &int_test);
CYG_TEST_PASS_FAIL(int_test == 95, "hex with %i");
CYG_TEST_PASS_FAIL(ret == 1, "hex with %i return code");
// Check 11
int_test=0;
my_strcpy(x, "+023456");
ret = sscanf(x, "%u", &int_test);
CYG_TEST_PASS_FAIL(int_test == 23456, "unsigned int with %u");
CYG_TEST_PASS_FAIL(ret == 1, "unsigned int with %u return code");
// Check 12
my_strcpy(x, "aString 2747 D");
ret = sscanf(x, "%s %d %c", y, &int_test, z);
CYG_TEST_PASS_FAIL( (my_strcmp(y, "aString")==0) &&
(int_test == 2747) &&
(z[0] == 'D'), "A few combinations #1");
CYG_TEST_PASS_FAIL(ret == 3, "Combinations #1 return code");
// Check 13
my_strcpy(x, "-6 52 awurble 2747 xxx");
ret = sscanf(x, "%d %u %s %2d %n %c",
&int_test, &int_test2, y, &int_test3, &int_test4, z);
CYG_TEST_PASS_FAIL( (int_test == -6) && (int_test2 == 52) &&
(my_strcmp(y, "awurble")==0) &&
(int_test3 == 27) &&
(int_test4 == 16) &&
(z[0] == '4'), "A few combinations #2");
CYG_TEST_PASS_FAIL(ret == 5, "Combinations #2 return code");
#ifdef CYGSEM_LIBC_STDIO_SCANF_FLOATING_POINT
// How much _relative_ error do we allow?
#define FLT_TOLERANCE 1.0e-3
#define MY_FABS(x) ((x) < 0.0 ? -(x) : (x))
CYG_TEST_INFO("Starting floating point specific tests");
{
float flt_test, wanted;
// Check 14
my_strcpy(x, "2.5");
wanted = 2.5;
ret = sscanf(x, "%f", &flt_test);
CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) <
MY_FABS(wanted * FLT_TOLERANCE),
"Simple %f test #1" );
CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test #1 return code" );
// Check 15
my_strcpy(x, "-23.472345");
wanted = -23.472345;
ret = sscanf(x, "%f", &flt_test);
CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) <
MY_FABS(wanted * FLT_TOLERANCE),
"Simple %f test #2" );
CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test #2 return code" );
// Check 16
my_strcpy(x, "0.0");
ret = sscanf(x, "%f", &flt_test);
CYG_TEST_PASS_FAIL( flt_test==0.0, "Simple %f test 0.0" );
CYG_TEST_PASS_FAIL( ret == 1, "Simple %f test 0.0 return code" );
// Check 17
my_strcpy(x, "-2.6334e00");
wanted = -2.6334;
ret = sscanf(x, "%e", &flt_test);
CYG_TEST_PASS_FAIL( MY_FABS(flt_test - wanted) <
MY_FABS(wanted * FLT_TOLERANCE),
"Simple %e test #1" );
CYG_TEST_PASS_FAIL( ret == 1, "Simple %e test #1 return code" );
// Check 18
my_strcpy(x, "-2.56789e-06");
wanted = -2.56789e-06;
ret = sscanf(x, "%e", &flt_test);
CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) <
MY_FABS(wanted * FLT_TOLERANCE),
"Simple %e test #2" );
CYG_TEST_PASS_FAIL( ret == 1, "Simple %e test #2 return code" );
// Check 19
my_strcpy(x, "-2.12389e-06");
wanted = -2.12389e-06;
ret = sscanf(x, "%g", &flt_test);
CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) <
MY_FABS(wanted * FLT_TOLERANCE),
"Simple %g test #1" );
CYG_TEST_PASS_FAIL( ret == 1, "Simple %g test #1 return code" );
// Check 20
my_strcpy(x, "1.3423345");
wanted = 1.342335;
ret = sscanf(x, "%g", &flt_test);
CYG_TEST_PASS_FAIL( MY_FABS(flt_test-wanted) <
MY_FABS(wanted * FLT_TOLERANCE),
"Simple %g test #2" );
CYG_TEST_PASS_FAIL( ret == 1, "Simple %g test #2 return code" );
} // compound
#else
CYG_TEST_PASS("Floating point tests skipped - not configured");
#endif // ifdef CYGSEM_LIBC_STDIO_SCANF_FLOATING_POINT
CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
"sscanf() function");
} // test()
#endif // defined(CYGFUN_LIBC_STDIO_ungetc)
int
main(int argc, char *argv[])
{
CYG_TEST_INIT();
CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
"sscanf() function");
START_TEST( test );
CYG_TEST_NA("Testing is not applicable to this configuration");
} // main()
// EOF sscanf.c
|