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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libc/] [stdio/] [current/] [tests/] [sprintf2.c] - Blame information for rev 810

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=================================================================
2
//
3
//        sprintf2.c
4
//
5
//        Testcase for C library sprintf()
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 sprintf() 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
// FUNCTIONS
60
 
61
// Functions to avoid having to use libc strings
62
 
63
static int
64
my_strlen(const char *s)
65
{
66
    const char *ptr;
67
 
68
    ptr = s;
69
    for ( ptr=s ; *ptr != '\0' ; ptr++ )
70
        ;
71
 
72
    return (int)(ptr-s);
73
} // my_strlen()
74
 
75
 
76
static int
77
my_strcmp(const char *s1, const char *s2)
78
{
79
    for ( ; *s1 == *s2 ; s1++,s2++ )
80
    {
81
        if ( *s1 == '\0' )
82
            break;
83
    } // for
84
 
85
    return (*s1 - *s2);
86
} // my_strcmp()
87
 
88
 
89
static char *
90
my_strcpy(char *s1, const char *s2)
91
{
92
    while (*s2 != '\0') {
93
        *(s1++) = *(s2++);
94
    }
95
    *s1 = '\0';
96
 
97
    return s1;
98
} // my_strcpy()
99
 
100
 
101
 
102
static void
103
test( CYG_ADDRWORD data )
104
{
105
    static char x[200];
106
    static char y[200];
107
    static char z[200];
108
    int ret;
109
    int tmp;
110
    int *ptr;
111
 
112
    // Check 1
113
    my_strcpy(x, "I'm afraid the shield generator");
114
    ptr = &tmp;
115
    ret = sprintf(y, "%s%n will be quite operational - %5d%%%c%05X", x,
116
                  ptr, 13, '5', 0x89ab);
117
    my_strcpy( z, "I'm afraid the shield generator will be "
118
                  "quite operational -    13%5089AB" );
119
    CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "%s%n%d%%%c%0X test");
120
 
121
    CYG_TEST_PASS_FAIL(ret == my_strlen(z),
122
                       "%s%n%d%%%c%0X test return code" );
123
 
124
    CYG_TEST_PASS_FAIL(tmp==31, "%n test");
125
 
126
    // Check 2
127
    ret = sprintf(y, "|%5d|%10s|%03d|%c|%o|", 2, "times", 6, '=', 10 );
128
    my_strcpy(z, "|    2|     times|006|=|12|");
129
 
130
    CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "|%5d|%10s|%03d|%c|%o| test");
131
 
132
    CYG_TEST_PASS_FAIL(ret == my_strlen(z),
133
                       "|%5d|%10s|%03d|%c|%o| test return code" );
134
 
135
    // Check 3
136
    ret = snprintf(y, 19, "print up to here >< and not this bit" );
137
    my_strcpy(z, "print up to here >");
138
    CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "simple snprintf test #1");
139
    CYG_TEST_PASS_FAIL(ret == my_strlen(z),
140
                       "simple snprintf test #1 return code" );
141
 
142
    // Check 4
143
    ret = snprintf(y, 31, "print a bit of this number: %05d nyer", 1234);
144
    my_strcpy(z, "print a bit of this number: 01");
145
    CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "simple snprintf test #2");
146
    CYG_TEST_PASS_FAIL(ret == my_strlen(z),
147
                       "simple snprintf test #2 return code" );
148
 
149
#ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT
150
 
151
    CYG_TEST_INFO("Starting floating point specific tests");
152
 
153
    // Check 5
154
    ret = sprintf(y, "|%5f|%10s|%03d|%c|%+-5.2G|%010.3G|",
155
                  2.0, "times", 6, '=', 12.0, -2.3451e-6 );
156
    my_strcpy(z, "|2.000000|     times|006|=|+12  |-02.35E-06|");
157
 
158
    CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0,
159
                       "|%5f|%10s|%03d|%c|%+-5.2G|%010.3G| test");
160
 
161
    CYG_TEST_PASS_FAIL(ret == my_strlen(z),
162
                       "|%5f|%10s|%03d|%c|%+-5.2G|%010.3G| test "
163
                       "return code" );
164
 
165
    // Check 6
166
    ret = snprintf(y, 20, "bit of this: %g double", 6.431e8);
167
    my_strcpy(z, "bit of this: 6.431e");
168
    CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0,
169
                       "snprintf double test #1");
170
 
171
    CYG_TEST_PASS_FAIL(ret == my_strlen(z),
172
                       "snprintf double test #1 return code");
173
 
174
#endif // ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT
175
 
176
    CYG_TEST_FINISH("Finished tests from testcase " __FILE__
177
                    " for C library sprintf() function");
178
 
179
} // test()
180
 
181
int
182
main(int argc, char *argv[])
183
{
184
    CYG_TEST_INIT();
185
 
186
    CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C "
187
                  "library sprintf() function");
188
    CYG_TEST_INFO("These test combinations of sprintf() features");
189
 
190
    test(0);
191
 
192
    return 0;
193
} // main()
194
 
195
// EOF sprintf2.c

powered by: WebSVN 2.1.0

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