]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/infra/v2_0/tests/diag_sprintf2.c
Initial revision
[karo-tx-redboot.git] / packages / infra / v2_0 / tests / diag_sprintf2.c
1 //=================================================================
2 //
3 //        diag_sprintf2.c
4 //
5 //        Testcase for infra library diag_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 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //=================================================================
39 //#####DESCRIPTIONBEGIN####
40 //
41 // Author(s):       ctarpy, jlarmour
42 // Contributors:    andrew lunn
43 // Date:            2005-02-09
44 // Description:     Contains testcode for infra library diag_sprintf() function
45 //
46 //
47 //####DESCRIPTIONEND####
48
49 // CONFIGURATION
50
51 // INCLUDES
52
53 #include <cyg/infra/diag.h>
54 #include <cyg/infra/testcase.h>
55
56 // FUNCTIONS
57
58 // Functions to avoid having to use libc strings
59
60 static int
61 my_strlen(const char *s)
62 {
63     const char *ptr;
64
65     ptr = s;
66     for ( ptr=s ; *ptr != '\0' ; ptr++ )
67         ;
68
69     return (int)(ptr-s);
70 } // my_strlen()
71
72
73 static int
74 my_strcmp(const char *s1, const char *s2)
75 {
76     for ( ; *s1 == *s2 ; s1++,s2++ )
77     {
78         if ( *s1 == '\0' )
79             break;
80     } // for
81
82     return (*s1 - *s2);
83 } // my_strcmp()
84
85
86 static char *
87 my_strcpy(char *s1, const char *s2)
88 {
89     while (*s2 != '\0') {
90         *(s1++) = *(s2++);
91     }
92     *s1 = '\0';
93
94     return s1; 
95 } // my_strcpy()
96
97
98
99 static void
100 test( CYG_ADDRWORD data )
101 {
102     static char x[200];
103     static char y[200];
104     static char z[200];
105     int ret;
106
107     // Check 1
108     my_strcpy(x, "I'm afraid the shield generator");
109     ret = diag_sprintf(y, "%s will be quite operational - %5d%%%c%05X", x,
110                    13, '5', 0x89ab);
111     my_strcpy( z, "I'm afraid the shield generator will be "
112                   "quite operational -    13%5089AB" );
113     CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "%s%n%d%%%c%0X test");
114
115     CYG_TEST_PASS_FAIL(ret == my_strlen(z),
116                        "%s%n%d%%%c%0X test return code" );
117
118     // Check 2
119     ret = diag_sprintf(y, "|%5d|%10s|%03d|%c|", 2, "times", 6, '=');
120     my_strcpy(z, "|    2|     times|006|=|");
121
122     CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "|%5d|%10s|%03d|%c| test");
123
124     CYG_TEST_PASS_FAIL(ret == my_strlen(z),
125                        "|%5d|%10s|%03d|%c|%o| test return code" );
126
127     // Check 3
128     ret = diag_snprintf(y, 19, "print up to here >< and not this bit" );
129     my_strcpy(z, "print up to here >");
130     CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "simple diag_snprintf test #1");
131     CYG_TEST_PASS_FAIL(ret == my_strlen(z),
132                        "simple diag_snprintf test #1 return code" );
133     
134     // Check 4
135     ret = diag_snprintf(y, 31, "print a bit of this number: %05d nyer", 1234);
136     my_strcpy(z, "print a bit of this number: 01");
137     CYG_TEST_PASS_FAIL(my_strcmp(y,z) == 0, "simple diag_snprintf test #2");
138     CYG_TEST_PASS_FAIL(ret == my_strlen(z),
139                        "simple diag_snprintf test #2 return code" );
140     
141     CYG_TEST_FINISH("Finished tests from testcase " __FILE__
142                     " for C library diag_sprintf() function");
143
144 } // test()
145
146 int
147 main(int argc, char *argv[])
148 {
149     CYG_TEST_INIT();
150
151     CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for infra "
152                   "library diag_sprintf() function");
153     CYG_TEST_INFO("These test combinations of diag_sprintf() features");
154
155     test(0);
156
157     return 0;
158 } // main()
159
160 // EOF diag_sprintf2.c