1 |
786 |
skrzyp |
//=================================================================
|
2 |
|
|
//
|
3 |
|
|
// diag_sprintf1.c
|
4 |
|
|
//
|
5 |
|
|
// Testcase for infra 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, 2006 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: andrew lunn
|
44 |
|
|
// Date: 2005-02-09
|
45 |
|
|
// Description: Contains testcode for infra diag_sprintf() function
|
46 |
|
|
//
|
47 |
|
|
//
|
48 |
|
|
//####DESCRIPTIONEND####
|
49 |
|
|
|
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 my_strlen(const char *s)
|
61 |
|
|
{
|
62 |
|
|
const char *ptr;
|
63 |
|
|
|
64 |
|
|
ptr = s;
|
65 |
|
|
for ( ptr=s ; *ptr != '\0' ; ptr++ )
|
66 |
|
|
;
|
67 |
|
|
|
68 |
|
|
return (int)(ptr-s);
|
69 |
|
|
} // my_strlen()
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
static int my_strcmp(const char *s1, const char *s2)
|
73 |
|
|
{
|
74 |
|
|
for ( ; *s1 == *s2 ; s1++,s2++ )
|
75 |
|
|
{
|
76 |
|
|
if ( *s1 == '\0' )
|
77 |
|
|
break;
|
78 |
|
|
} // for
|
79 |
|
|
|
80 |
|
|
return (*s1 - *s2);
|
81 |
|
|
} // my_strcmp()
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
static char *my_strcpy(char *s1, const char *s2)
|
85 |
|
|
{
|
86 |
|
|
while (*s2 != '\0') {
|
87 |
|
|
*(s1++) = *(s2++);
|
88 |
|
|
}
|
89 |
|
|
*s1 = '\0';
|
90 |
|
|
|
91 |
|
|
return s1;
|
92 |
|
|
} // my_strcpy()
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
static void test(CYG_ADDRWORD data)
|
97 |
|
|
{
|
98 |
|
|
static char x[500];
|
99 |
|
|
static char y[500];
|
100 |
|
|
int ret;
|
101 |
|
|
int tmp;
|
102 |
|
|
int *ptr;
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
// Check 1
|
106 |
|
|
ret = diag_sprintf(x, "%d", 20);
|
107 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "20")==0, "%d test");
|
108 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%d test return code");
|
109 |
|
|
|
110 |
|
|
// Check 2
|
111 |
|
|
my_strcpy(y, "Pigs noses. Get 'em while there 'ot");
|
112 |
|
|
ret = diag_sprintf(x, "%s", y);
|
113 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, y)==0, "%s test");
|
114 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%s test return code");
|
115 |
|
|
|
116 |
|
|
// Check 3
|
117 |
|
|
ret = diag_sprintf(x, "||%7d||", 2378);
|
118 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "|| 2378||")==0, "padding test");
|
119 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "padding test return code");
|
120 |
|
|
|
121 |
|
|
// Check 4
|
122 |
|
|
ret = diag_sprintf(x, "%x", 3573);
|
123 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "df5")==0, "hex conversion (lowercase)");
|
124 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "hex conv (lowercase) return code");
|
125 |
|
|
|
126 |
|
|
// Check 5
|
127 |
|
|
ret = diag_sprintf(x, "%X", 3573);
|
128 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "DF5")==0, "hex conversion (uppercase)");
|
129 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "hex conv (upperbase ) return code");
|
130 |
|
|
|
131 |
|
|
// Check 6
|
132 |
|
|
ret = diag_sprintf(x, "%c", 65);
|
133 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "A")==0, "%c test");
|
134 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%c test return code");
|
135 |
|
|
|
136 |
|
|
// Check 7
|
137 |
|
|
// Check 7 was for octal, but diag_sprintf does not support this.
|
138 |
|
|
|
139 |
|
|
// Check 8
|
140 |
|
|
ret = diag_sprintf(x, "%u", (unsigned int) 4738);
|
141 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "4738")==0, "%u test");
|
142 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%u test return code");
|
143 |
|
|
|
144 |
|
|
// Check 9
|
145 |
|
|
// Check 9 was for the %n conversion specifier which diag_sprintf
|
146 |
|
|
// does not support.
|
147 |
|
|
|
148 |
|
|
// Check 10
|
149 |
|
|
ret = diag_sprintf(x, "%%");
|
150 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "%")==0, "%% test");
|
151 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%% test return code");
|
152 |
|
|
|
153 |
|
|
// Check 11
|
154 |
|
|
ret = diag_sprintf(x, "%ld", (long)1<<30);
|
155 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "1073741824")==0, "%ld test");
|
156 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%ld test return code");
|
157 |
|
|
|
158 |
|
|
// Check 12
|
159 |
|
|
ret = diag_sprintf(x, "%lu", (unsigned long)(1<<31) + 100);
|
160 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "2147483748")==0, "%lu test");
|
161 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%lu test return code");
|
162 |
|
|
|
163 |
|
|
// Check 13
|
164 |
|
|
ret = diag_sprintf(x, "%x", 0x789a);
|
165 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "789a")==0, "%x test");
|
166 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%x test return code");
|
167 |
|
|
|
168 |
|
|
// Check 14
|
169 |
|
|
ret = diag_sprintf(x, "%X", 0x789ab2);
|
170 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "789AB2")==0, "%X test");
|
171 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%x test return code");
|
172 |
|
|
|
173 |
|
|
// Check 15
|
174 |
|
|
ret = diag_sprintf(x, "%08x", 0xdea2f2);
|
175 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "00dea2f2")==0, "%0x test");
|
176 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%0x test return code");
|
177 |
|
|
|
178 |
|
|
// Check 16
|
179 |
|
|
ret = diag_sprintf(x, "%09X", 0x12fa1c);
|
180 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, "00012FA1C")==0, "%0X test");
|
181 |
|
|
CYG_TEST_PASS_FAIL(ret==my_strlen(x), "%0X test return code");
|
182 |
|
|
|
183 |
|
|
// Check 17
|
184 |
|
|
ptr=&tmp;
|
185 |
|
|
ret = diag_sprintf(x, "%p", (void *)ptr);
|
186 |
|
|
// just check _something_ was returned
|
187 |
|
|
CYG_TEST_PASS_FAIL((ret==my_strlen(x)) && (ret > 0),
|
188 |
|
|
"%p test return code");
|
189 |
|
|
|
190 |
|
|
// Long string tests
|
191 |
|
|
ret = diag_sprintf(x, "This is a very long string so I hope this works as "
|
192 |
|
|
"otherwise I would be very, very, sad. The cat sat on the "
|
193 |
|
|
"hat, and the mat sat on the rat. Quick brown fax, etc.etc. "
|
194 |
|
|
"blah, blah and all that jazz. Isn't he finished yet? My "
|
195 |
|
|
"old man's a dustman, why do I have to think up this "
|
196 |
|
|
"drivel, isn't that what summer students are for, if "
|
197 |
|
|
"anything that seems thinking up mindless drivel seems to "
|
198 |
|
|
"be their occupation in life. Yoof of today, eh? What, "
|
199 |
|
|
"what? %s So there.",
|
200 |
|
|
"And this is a middly bit.");
|
201 |
|
|
my_strcpy(y, "This is a very long string so I hope this works as "
|
202 |
|
|
"otherwise I would be very, very, sad. The cat sat on the "
|
203 |
|
|
"hat, and the mat sat on the rat. Quick brown fax, etc.etc. "
|
204 |
|
|
"blah, blah and all that jazz. Isn't he finished yet? My "
|
205 |
|
|
"old man's a dustman, why do I have to think up this "
|
206 |
|
|
"drivel, isn't that what summer students are for, if "
|
207 |
|
|
"anything that seems thinking up mindless drivel seems to "
|
208 |
|
|
"be their occupation in life. Yoof of today, eh? What, "
|
209 |
|
|
"what? And this is a middly bit. So there.");
|
210 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, y)==0, "long (480 char) string output #1");
|
211 |
|
|
CYG_TEST_PASS_FAIL(ret == my_strlen(y),
|
212 |
|
|
"long (480 char) string output #1 return code");
|
213 |
|
|
|
214 |
|
|
ret = diag_sprintf(x, "Boo! This %s So there.",
|
215 |
|
|
"is a very long string so I hope this works as "
|
216 |
|
|
"otherwise I would be very, very, sad. The cat sat on the "
|
217 |
|
|
"hat, and the mat sat on the rat. Quick brown fax, etc.etc. "
|
218 |
|
|
"blah, blah and all that jazz. Isn't he finished yet? My "
|
219 |
|
|
"old man's a dustman, why do I have to think up this "
|
220 |
|
|
"drivel, isn't that what summer students are for, if "
|
221 |
|
|
"anything that seems thinking up mindless drivel seems to "
|
222 |
|
|
"be their occupation in life. Yoof of today, eh? What, "
|
223 |
|
|
"what? And this is a middly bit.");
|
224 |
|
|
my_strcpy(y, "Boo! This is a very long string so I hope this works as "
|
225 |
|
|
"otherwise I would be very, very, sad. The cat sat on the "
|
226 |
|
|
"hat, and the mat sat on the rat. Quick brown fax, etc.etc. "
|
227 |
|
|
"blah, blah and all that jazz. Isn't he finished yet? My "
|
228 |
|
|
"old man's a dustman, why do I have to think up this "
|
229 |
|
|
"drivel, isn't that what summer students are for, if "
|
230 |
|
|
"anything that seems thinking up mindless drivel seems to "
|
231 |
|
|
"be their occupation in life. Yoof of today, eh? What, "
|
232 |
|
|
"what? And this is a middly bit. So there.");
|
233 |
|
|
CYG_TEST_PASS_FAIL(my_strcmp(x, y)==0, "long (485 char) string output #2");
|
234 |
|
|
CYG_TEST_PASS_FAIL(ret == my_strlen(y),
|
235 |
|
|
"long (485 char) string output #2 return code");
|
236 |
|
|
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
|
241 |
|
|
"diag_sprintf() function");
|
242 |
|
|
|
243 |
|
|
} // test()
|
244 |
|
|
|
245 |
|
|
void cyg_user_start(void)
|
246 |
|
|
{
|
247 |
|
|
CYG_TEST_INIT();
|
248 |
|
|
|
249 |
|
|
CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for infra library "
|
250 |
|
|
"diag_sprintf() function");
|
251 |
|
|
CYG_TEST_INFO("These test individual features separately");
|
252 |
|
|
|
253 |
|
|
test(0);
|
254 |
|
|
} // cyg_user_start()
|
255 |
|
|
|
256 |
|
|
// EOF diag_sprintf1.c
|