1 //=================================================================
5 // Testcase for C library sprintf()
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.
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.
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
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.
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.
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.
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####
43 // Author(s): ctarpy, jlarmour
46 // Description: Contains testcode for C library sprintf() function
49 //####DESCRIPTIONEND####
53 #include <pkgconf/libc_stdio.h> // Configuration header
58 #include <cyg/infra/testcase.h>
62 // Functions to avoid having to use libc strings
65 my_strlen(const char *s)
70 for ( ptr=s ; *ptr != '\0' ; ptr++ )
78 my_strcmp(const char *s1, const char *s2)
80 for ( ; *s1 == *s2 ; s1++,s2++ )
91 my_strcpy(char *s1, const char *s2)
104 test( CYG_ADDRWORD data )
114 my_strcpy(x, "I'm afraid the shield generator");
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");
122 CYG_TEST_PASS_FAIL(ret == my_strlen(z),
123 "%s%n%d%%%c%0X test return code" );
125 CYG_TEST_PASS_FAIL(tmp==31, "%n test");
128 ret = sprintf(y, "|%5d|%10s|%03d|%c|%o|", 2, "times", 6, '=', 10 );
129 my_strcpy(z, "| 2| times|006|=|12|");
131 CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "|%5d|%10s|%03d|%c|%o| test");
133 CYG_TEST_PASS_FAIL(ret == my_strlen(z),
134 "|%5d|%10s|%03d|%c|%o| test return code" );
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" );
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" );
150 #ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT
152 CYG_TEST_INFO("Starting floating point specific tests");
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|");
159 CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0,
160 "|%5f|%10s|%03d|%c|%+-5.2G|%010.3G| test");
162 CYG_TEST_PASS_FAIL(ret == my_strlen(z),
163 "|%5f|%10s|%03d|%c|%+-5.2G|%010.3G| test "
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");
172 CYG_TEST_PASS_FAIL(ret == my_strlen(z),
173 "snprintf double test #1 return code");
175 #endif // ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT
177 CYG_TEST_FINISH("Finished tests from testcase " __FILE__
178 " for C library sprintf() function");
183 main(int argc, char *argv[])
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");