2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
30 * memcpy() tests are handled separately
33 /* Forcibly enable assertions */
41 #include <ipxe/string.h>
42 #include <ipxe/test.h>
45 * Perform string self-tests
48 static void string_test_exec ( void ) {
51 ok ( strlen ( "" ) == 0 );
52 ok ( strlen ( "Hello" ) == 5 );
53 ok ( strlen ( "Hello world!" ) == 12 );
54 ok ( strlen ( "Hello\0world!" ) == 5 );
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 );
68 ok ( strchr ( "", 'a' ) == NULL );
69 ok ( *(strchr ( "Testing", 'e' )) == 'e' );
70 ok ( *(strchr ( "Testing", 'g' )) == 'g' );
71 ok ( strchr ( "Testing", 'x' ) == NULL );
74 ok ( strrchr ( "", 'a' ) == NULL );
75 ok ( *(strrchr ( "Haystack", 'a' )) == 'a' );
76 ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
77 ok ( strrchr ( "Haystack", 'x' ) == NULL );
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 );
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 );
93 ok ( strncmp ( "", "", 0 ) == 0 );
94 ok ( strncmp ( "", "", 15 ) == 0 );
95 ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
96 ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
97 ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
98 ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
100 /* Test strcasecmp() */
101 ok ( strcasecmp ( "", "" ) == 0 );
102 ok ( strcasecmp ( "Uncle Jack", "Uncle jack" ) == 0 );
103 ok ( strcasecmp ( "Uncle Jack", "Uncle" ) != 0 );
104 ok ( strcasecmp ( "Uncle", "Uncle Jack" ) != 0 );
105 ok ( strcasecmp ( "not", "equal" ) != 0 );
108 ok ( memcmp ( "", "", 0 ) == 0 );
109 ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
110 ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
114 const char haystack[] = "find me!";
117 found = strstr ( haystack, "find" );
118 ok ( found == &haystack[0] );
119 found = strstr ( haystack, "me" );
120 ok ( found == &haystack[5] );
121 found = strstr ( haystack, "me." );
122 ok ( found == NULL );
127 static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
128 static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
129 memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
130 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
133 static uint8_t test[4] = { '>', 0, 0, '<' };
134 static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
135 memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
136 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
141 static uint8_t test[11] =
142 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
143 static const uint8_t expected[11] =
144 { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
145 memmove ( ( test + 1 ), ( test + 3 ), 6 );
146 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
149 static uint8_t test[12] =
150 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
151 static const uint8_t expected[12] =
152 { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
153 memmove ( ( test + 6 ), ( test + 1 ), 5 );
154 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
159 static uint8_t test[8] =
160 { '>', 1, 2, 3, 7, 8, 9, '<' };
161 static const uint8_t expected[8] =
162 { '>', 7, 8, 9, 1, 2, 3, '<' };
163 memswap ( ( test + 1 ), ( test + 4 ), 3 );
164 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
169 const char *orig = "testing testing";
170 char *dup = strdup ( orig );
173 ok ( strcmp ( dup, orig ) == 0 );
179 const char *normal = "testing testing";
180 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
182 dup = strndup ( normal, 32 );
184 ok ( dup != normal );
185 ok ( strcmp ( dup, normal ) == 0 );
187 dup = strndup ( normal, 4 );
189 ok ( strcmp ( dup, "test" ) == 0 );
191 dup = strndup ( unterminated, 5 );
193 ok ( strcmp ( dup, "hello" ) == 0 );
199 const char longer[7] = "copyme";
200 const char shorter[3] = "hi";
204 copy = strcpy ( dest, longer );
206 ok ( memcmp ( dest, longer, 7 ) == 0 );
207 copy = strcpy ( dest, shorter );
209 ok ( memcmp ( dest, shorter, 3 ) == 0 );
210 ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
215 const char src[5] = "copy";
216 const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
217 const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
221 memcpy ( dest, orig, sizeof ( dest ) );
222 copy = strncpy ( dest, src, 5 );
224 ok ( memcmp ( dest, src, 5 ) == 0 );
225 ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
226 memcpy ( dest, orig, sizeof ( dest ) );
227 copy = strncpy ( dest, src, 4 );
229 ok ( memcmp ( dest, src, 4 ) == 0 );
230 ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
231 memcpy ( dest, orig, sizeof ( dest ) );
232 copy = strncpy ( dest, src, 8 );
234 ok ( memcmp ( dest, src, 5 ) == 0 );
235 ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
236 memcpy ( dest, orig, sizeof ( dest ) );
237 copy = strncpy ( dest, "", 8 );
239 ok ( memcmp ( dest, zero, 8 ) == 0 );
244 char buf[16] = "append";
247 dest = strcat ( buf, " this" );
249 ok ( strcmp ( buf, "append this" ) == 0 );
252 /* Test digit_value() */
256 for ( i = 0 ; i < 16 ; i++ ) {
257 snprintf ( buf, sizeof ( buf ), "%x", i );
258 ok ( digit_value ( buf[0] ) == i );
259 snprintf ( buf, sizeof ( buf ), "%X", i );
260 ok ( digit_value ( buf[0] ) == i );
262 ok ( digit_value ( 0 ) >= 16 );
263 ok ( digit_value ( 9 ) >= 16 );
264 ok ( digit_value ( '0' - 1 ) >= 16 );
265 ok ( digit_value ( '9' + 1 ) >= 16 );
266 ok ( digit_value ( 'A' - 1 ) >= 16 );
267 ok ( digit_value ( 'F' + 1 ) >= 16 );
268 ok ( digit_value ( 'a' - 1 ) >= 16 );
269 ok ( digit_value ( 'f' + 1 ) >= 16 );
273 ok ( strtoul ( "12345", NULL, 0 ) == 12345UL );
274 ok ( strtoul ( " 741", NULL, 10 ) == 741UL );
275 ok ( strtoul ( " 555a", NULL, 0 ) == 555UL );
276 ok ( strtoul ( " 555a", NULL, 16 ) == 0x555aUL );
277 ok ( strtoul ( "-12", NULL, 0 ) == -12UL );
278 ok ( strtoul ( "+3", NULL, 0 ) == 3UL );
279 ok ( strtoul ( "721", NULL, 0 ) == 721UL );
280 ok ( strtoul ( "721", NULL, 8 ) == 0721UL );
281 ok ( strtoul ( "0721", NULL, 0 ) == 0721UL );
282 ok ( strtoul ( "", NULL, 0 ) == 0UL );
283 ok ( strtoul ( "\t0xcAfe", NULL, 0 ) == 0xcafeUL );
284 ok ( strtoul ( "0xffffffff", NULL, 0 ) == 0xffffffffUL );
286 static const char string[] = "123aHa.world";
288 ok ( strtoul ( string, &endp, 0 ) == 123UL );
289 ok ( endp == &string[3] );
290 ok ( strtoul ( string, &endp, 16 ) == 0x123aUL );
291 ok ( endp == &string[4] );
292 ok ( strtoul ( string, &endp, 26 ) ==
293 ( ( ( ( ( 1 * 26 + 2 ) * 26 + 3 ) * 26 + 10 ) * 26
294 + 17 ) * 26 + 10 ) );
295 ok ( endp == &string[6] );
299 /** String self-test */
300 struct self_test string_test __self_test = {
302 .exec = string_test_exec,