OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [language/] [c/] [libc/] [stdio/] [v2_0/] [tests/] [sscanf.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.