[ena] Assign memory BAR if left empty by BIOS
[ipxe.git] / src / tests / string_test.c
1 /*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27 *
28 * String self-tests
29 *
30 * memcpy() tests are handled separately
31 */
32
33 /* Forcibly enable assertions */
34 #undef NDEBUG
35
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <ipxe/string.h>
42 #include <ipxe/test.h>
43
44 /**
45 * Perform string self-tests
46 *
47 */
48 static void string_test_exec ( void ) {
49
50 /* Test strlen() */
51 ok ( strlen ( "" ) == 0 );
52 ok ( strlen ( "Hello" ) == 5 );
53 ok ( strlen ( "Hello world!" ) == 12 );
54 ok ( strlen ( "Hello\0world!" ) == 5 );
55
56 /* Test strnlen() */
57 ok ( strnlen ( "", 0 ) == 0 );
58 ok ( strnlen ( "", 10 ) == 0 );
59 ok ( strnlen ( "Hello", 0 ) == 0 );
60 ok ( strnlen ( "Hello", 3 ) == 3 );
61 ok ( strnlen ( "Hello", 5 ) == 5 );
62 ok ( strnlen ( "Hello", 16 ) == 5 );
63 ok ( strnlen ( "Hello world!", 5 ) == 5 );
64 ok ( strnlen ( "Hello world!", 11 ) == 11 );
65 ok ( strnlen ( "Hello world!", 16 ) == 12 );
66
67 /* Test strchr() */
68 ok ( strchr ( "", 'a' ) == NULL );
69 ok ( *(strchr ( "Testing", 'e' )) == 'e' );
70 ok ( *(strchr ( "Testing", 'g' )) == 'g' );
71 ok ( strchr ( "Testing", 'x' ) == NULL );
72
73 /* Test strrchr() */
74 ok ( strrchr ( "", 'a' ) == NULL );
75 ok ( *(strrchr ( "Haystack", 'a' )) == 'a' );
76 ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
77 ok ( strrchr ( "Haystack", 'x' ) == NULL );
78
79 /* Test memchr() */
80 ok ( memchr ( "", '\0', 0 ) == NULL );
81 ok ( *((uint8_t *)memchr ( "post\0null", 'l', 9 )) == 'l' );
82 ok ( *((uint8_t *)memchr ( "post\0null", '\0', 9 )) == '\0' );
83 ok ( memchr ( "thingy", 'z', 6 ) == NULL );
84
85 /* Test strcmp() */
86 ok ( strcmp ( "", "" ) == 0 );
87 ok ( strcmp ( "Hello", "Hello" ) == 0 );
88 ok ( strcmp ( "Hello", "hello" ) != 0 );
89 ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
90 ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
91 ok ( strcmp ( "abc", "def" ) < 0 );
92
93 /* Test strncmp() */
94 ok ( strncmp ( "", "", 0 ) == 0 );
95 ok ( strncmp ( "", "", 15 ) == 0 );
96 ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
97 ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
98 ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
99 ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
100
101 /* Test strcasecmp() */
102 ok ( strcasecmp ( "", "" ) == 0 );
103 ok ( strcasecmp ( "Uncle Jack", "Uncle jack" ) == 0 );
104 ok ( strcasecmp ( "Uncle Jack", "Uncle" ) != 0 );
105 ok ( strcasecmp ( "Uncle", "Uncle Jack" ) != 0 );
106 ok ( strcasecmp ( "not", "equal" ) != 0 );
107
108 /* Test strncasecmp() */
109 ok ( strncasecmp ( "", "", 0 ) == 0 );
110 ok ( strncasecmp ( "", "", 73 ) == 0 );
111 ok ( strncasecmp ( "Uncle Jack", "Uncle jack", 47 ) == 0 );
112 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 47 ) != 0 );
113 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 9 ) != 0 );
114 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 8 ) == 0 );
115
116 /* Test memcmp() */
117 ok ( memcmp ( "", "", 0 ) == 0 );
118 ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
119 ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
120 ok ( memcmp ( "abc", "def", 3 ) < 0 );
121
122 /* Test strstr() */
123 {
124 const char haystack[] = "find me!";
125 char *found;
126
127 found = strstr ( haystack, "find" );
128 ok ( found == &haystack[0] );
129 found = strstr ( haystack, "me" );
130 ok ( found == &haystack[5] );
131 found = strstr ( haystack, "me." );
132 ok ( found == NULL );
133 }
134
135 /* Test memset() */
136 {
137 static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
138 static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
139 memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
140 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
141 }
142 {
143 static uint8_t test[4] = { '>', 0, 0, '<' };
144 static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
145 memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
146 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
147 }
148
149 /* Test memmove() */
150 {
151 static uint8_t test[11] =
152 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
153 static const uint8_t expected[11] =
154 { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
155 memmove ( ( test + 1 ), ( test + 3 ), 6 );
156 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
157 }
158 {
159 static uint8_t test[12] =
160 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
161 static const uint8_t expected[12] =
162 { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
163 memmove ( ( test + 6 ), ( test + 1 ), 5 );
164 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
165 }
166
167 /* Test memswap() */
168 {
169 static uint8_t test[8] =
170 { '>', 1, 2, 3, 7, 8, 9, '<' };
171 static const uint8_t expected[8] =
172 { '>', 7, 8, 9, 1, 2, 3, '<' };
173 memswap ( ( test + 1 ), ( test + 4 ), 3 );
174 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
175 }
176
177 /* Test strdup() */
178 {
179 const char *orig = "testing testing";
180 char *dup = strdup ( orig );
181 ok ( dup != NULL );
182 ok ( dup != orig );
183 ok ( strcmp ( dup, orig ) == 0 );
184 free ( dup );
185 }
186
187 /* Test strndup() */
188 {
189 const char *normal = "testing testing";
190 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
191 char *dup;
192 dup = strndup ( normal, 32 );
193 ok ( dup != NULL );
194 ok ( dup != normal );
195 ok ( strcmp ( dup, normal ) == 0 );
196 free ( dup );
197 dup = strndup ( normal, 4 );
198 ok ( dup != NULL );
199 ok ( strcmp ( dup, "test" ) == 0 );
200 free ( dup );
201 dup = strndup ( unterminated, 5 );
202 ok ( dup != NULL );
203 ok ( strcmp ( dup, "hello" ) == 0 );
204 free ( dup );
205 }
206
207 /* Test strcpy() */
208 {
209 const char longer[7] = "copyme";
210 const char shorter[3] = "hi";
211 char dest[7];
212 char *copy;
213
214 copy = strcpy ( dest, longer );
215 ok ( copy == dest );
216 ok ( memcmp ( dest, longer, 7 ) == 0 );
217 copy = strcpy ( dest, shorter );
218 ok ( copy == dest );
219 ok ( memcmp ( dest, shorter, 3 ) == 0 );
220 ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
221 }
222
223 /* Test strncpy() */
224 {
225 const char src[5] = "copy";
226 const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
227 const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
228 char dest[8];
229 char *copy;
230
231 memcpy ( dest, orig, sizeof ( dest ) );
232 copy = strncpy ( dest, src, 5 );
233 ok ( copy == dest );
234 ok ( memcmp ( dest, src, 5 ) == 0 );
235 ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
236 memcpy ( dest, orig, sizeof ( dest ) );
237 copy = strncpy ( dest, src, 4 );
238 ok ( copy == dest );
239 ok ( memcmp ( dest, src, 4 ) == 0 );
240 ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
241 memcpy ( dest, orig, sizeof ( dest ) );
242 copy = strncpy ( dest, src, 8 );
243 ok ( copy == dest );
244 ok ( memcmp ( dest, src, 5 ) == 0 );
245 ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
246 memcpy ( dest, orig, sizeof ( dest ) );
247 copy = strncpy ( dest, "", 8 );
248 ok ( copy == dest );
249 ok ( memcmp ( dest, zero, 8 ) == 0 );
250 }
251
252 /* Test strcat() */
253 {
254 char buf[16] = "append";
255 char *dest;
256
257 dest = strcat ( buf, " this" );
258 ok ( dest == buf );
259 ok ( strcmp ( buf, "append this" ) == 0 );
260 }
261
262 /* Test digit_value() */
263 {
264 unsigned int i;
265 char buf[2];
266 for ( i = 0 ; i < 16 ; i++ ) {
267 snprintf ( buf, sizeof ( buf ), "%x", i );
268 ok ( digit_value ( buf[0] ) == i );
269 snprintf ( buf, sizeof ( buf ), "%X", i );
270 ok ( digit_value ( buf[0] ) == i );
271 }
272 ok ( digit_value ( 0 ) >= 16 );
273 ok ( digit_value ( 9 ) >= 16 );
274 ok ( digit_value ( '0' - 1 ) >= 16 );
275 ok ( digit_value ( '9' + 1 ) >= 16 );
276 ok ( digit_value ( 'A' - 1 ) >= 16 );
277 ok ( digit_value ( 'F' + 1 ) >= 16 );
278 ok ( digit_value ( 'a' - 1 ) >= 16 );
279 ok ( digit_value ( 'f' + 1 ) >= 16 );
280 }
281
282 /* Test strtoul() */
283 ok ( strtoul ( "12345", NULL, 0 ) == 12345UL );
284 ok ( strtoul ( " 741", NULL, 10 ) == 741UL );
285 ok ( strtoul ( " 555a", NULL, 0 ) == 555UL );
286 ok ( strtoul ( " 555a", NULL, 16 ) == 0x555aUL );
287 ok ( strtoul ( "-12", NULL, 0 ) == -12UL );
288 ok ( strtoul ( "+3", NULL, 0 ) == 3UL );
289 ok ( strtoul ( "721", NULL, 0 ) == 721UL );
290 ok ( strtoul ( "721", NULL, 8 ) == 0721UL );
291 ok ( strtoul ( "0721", NULL, 0 ) == 0721UL );
292 ok ( strtoul ( "", NULL, 0 ) == 0UL );
293 ok ( strtoul ( "\t0xcAfe", NULL, 0 ) == 0xcafeUL );
294 ok ( strtoul ( "0xffffffff", NULL, 0 ) == 0xffffffffUL );
295 {
296 static const char string[] = "123aHa.world";
297 char *endp;
298 ok ( strtoul ( string, &endp, 0 ) == 123UL );
299 ok ( endp == &string[3] );
300 ok ( strtoul ( string, &endp, 16 ) == 0x123aUL );
301 ok ( endp == &string[4] );
302 ok ( strtoul ( string, &endp, 26 ) ==
303 ( ( ( ( ( 1 * 26 + 2 ) * 26 + 3 ) * 26 + 10 ) * 26
304 + 17 ) * 26 + 10 ) );
305 ok ( endp == &string[6] );
306 }
307 }
308
309 /** String self-test */
310 struct self_test string_test __self_test = {
311 .name = "string",
312 .exec = string_test_exec,
313 };