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