[libc] Rewrite strtoul()
[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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23 *
24 * String self-tests
25 *
26 * memcpy() tests are handled separately
27 */
28
29 /* Forcibly enable assertions */
30 #undef NDEBUG
31
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <ipxe/string.h>
38 #include <ipxe/test.h>
39
40 /**
41 * Perform string self-tests
42 *
43 */
44 static void string_test_exec ( void ) {
45
46 /* Test strlen() */
47 ok ( strlen ( "" ) == 0 );
48 ok ( strlen ( "Hello" ) == 5 );
49 ok ( strlen ( "Hello world!" ) == 12 );
50 ok ( strlen ( "Hello\0world!" ) == 5 );
51
52 /* Test strnlen() */
53 ok ( strnlen ( "", 0 ) == 0 );
54 ok ( strnlen ( "", 10 ) == 0 );
55 ok ( strnlen ( "Hello", 0 ) == 0 );
56 ok ( strnlen ( "Hello", 3 ) == 3 );
57 ok ( strnlen ( "Hello", 5 ) == 5 );
58 ok ( strnlen ( "Hello", 16 ) == 5 );
59 ok ( strnlen ( "Hello world!", 5 ) == 5 );
60 ok ( strnlen ( "Hello world!", 11 ) == 11 );
61 ok ( strnlen ( "Hello world!", 16 ) == 12 );
62
63 /* Test strchr() */
64 ok ( strchr ( "", 'a' ) == NULL );
65 ok ( *(strchr ( "Testing", 'e' )) == 'e' );
66 ok ( *(strchr ( "Testing", 'g' )) == 'g' );
67 ok ( strchr ( "Testing", 'x' ) == NULL );
68
69 /* Test strrchr() */
70 ok ( strrchr ( "", 'a' ) == NULL );
71 ok ( *(strrchr ( "Haystack", 'a' )) == 'a' );
72 ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
73 ok ( strrchr ( "Haystack", 'x' ) == NULL );
74
75 /* Test memchr() */
76 ok ( memchr ( "", '\0', 0 ) == NULL );
77 ok ( *((uint8_t *)memchr ( "post\0null", 'l', 9 )) == 'l' );
78 ok ( *((uint8_t *)memchr ( "post\0null", '\0', 9 )) == '\0' );
79 ok ( memchr ( "thingy", 'z', 6 ) == NULL );
80
81 /* Test strcmp() */
82 ok ( strcmp ( "", "" ) == 0 );
83 ok ( strcmp ( "Hello", "Hello" ) == 0 );
84 ok ( strcmp ( "Hello", "hello" ) != 0 );
85 ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
86 ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
87
88 /* Test strncmp() */
89 ok ( strncmp ( "", "", 0 ) == 0 );
90 ok ( strncmp ( "", "", 15 ) == 0 );
91 ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
92 ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
93 ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
94 ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
95
96 /* Test strcasecmp() */
97 ok ( strcasecmp ( "", "" ) == 0 );
98 ok ( strcasecmp ( "Uncle Jack", "Uncle jack" ) == 0 );
99 ok ( strcasecmp ( "Uncle Jack", "Uncle" ) != 0 );
100 ok ( strcasecmp ( "Uncle", "Uncle Jack" ) != 0 );
101 ok ( strcasecmp ( "not", "equal" ) != 0 );
102
103 /* Test memcmp() */
104 ok ( memcmp ( "", "", 0 ) == 0 );
105 ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
106 ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
107
108 /* Test strstr() */
109 {
110 const char haystack[] = "find me!";
111 char *found;
112
113 found = strstr ( haystack, "find" );
114 ok ( found == &haystack[0] );
115 found = strstr ( haystack, "me" );
116 ok ( found == &haystack[5] );
117 found = strstr ( haystack, "me." );
118 ok ( found == NULL );
119 }
120
121 /* Test memset() */
122 {
123 static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
124 static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
125 memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
126 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
127 }
128 {
129 static uint8_t test[4] = { '>', 0, 0, '<' };
130 static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
131 memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
132 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
133 }
134
135 /* Test memmove() */
136 {
137 static uint8_t test[11] =
138 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
139 static const uint8_t expected[11] =
140 { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
141 memmove ( ( test + 1 ), ( test + 3 ), 6 );
142 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
143 }
144 {
145 static uint8_t test[12] =
146 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
147 static const uint8_t expected[12] =
148 { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
149 memmove ( ( test + 6 ), ( test + 1 ), 5 );
150 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
151 }
152
153 /* Test memswap() */
154 {
155 static uint8_t test[8] =
156 { '>', 1, 2, 3, 7, 8, 9, '<' };
157 static const uint8_t expected[8] =
158 { '>', 7, 8, 9, 1, 2, 3, '<' };
159 memswap ( ( test + 1 ), ( test + 4 ), 3 );
160 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
161 }
162
163 /* Test strdup() */
164 {
165 const char *orig = "testing testing";
166 char *dup = strdup ( orig );
167 ok ( dup != NULL );
168 ok ( dup != orig );
169 ok ( strcmp ( dup, orig ) == 0 );
170 free ( dup );
171 }
172
173 /* Test strndup() */
174 {
175 const char *normal = "testing testing";
176 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
177 char *dup;
178 dup = strndup ( normal, 32 );
179 ok ( dup != NULL );
180 ok ( dup != normal );
181 ok ( strcmp ( dup, normal ) == 0 );
182 free ( dup );
183 dup = strndup ( normal, 4 );
184 ok ( dup != NULL );
185 ok ( strcmp ( dup, "test" ) == 0 );
186 free ( dup );
187 dup = strndup ( unterminated, 5 );
188 ok ( dup != NULL );
189 ok ( strcmp ( dup, "hello" ) == 0 );
190 free ( dup );
191 }
192
193 /* Test strcpy() */
194 {
195 const char longer[7] = "copyme";
196 const char shorter[3] = "hi";
197 char dest[7];
198 char *copy;
199
200 copy = strcpy ( dest, longer );
201 ok ( copy == dest );
202 ok ( memcmp ( dest, longer, 7 ) == 0 );
203 copy = strcpy ( dest, shorter );
204 ok ( copy == dest );
205 ok ( memcmp ( dest, shorter, 3 ) == 0 );
206 ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
207 }
208
209 /* Test strncpy() */
210 {
211 const char src[5] = "copy";
212 const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
213 const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
214 char dest[8];
215 char *copy;
216
217 memcpy ( dest, orig, sizeof ( dest ) );
218 copy = strncpy ( dest, src, 5 );
219 ok ( copy == dest );
220 ok ( memcmp ( dest, src, 5 ) == 0 );
221 ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
222 memcpy ( dest, orig, sizeof ( dest ) );
223 copy = strncpy ( dest, src, 4 );
224 ok ( copy == dest );
225 ok ( memcmp ( dest, src, 4 ) == 0 );
226 ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
227 memcpy ( dest, orig, sizeof ( dest ) );
228 copy = strncpy ( dest, src, 8 );
229 ok ( copy == dest );
230 ok ( memcmp ( dest, src, 5 ) == 0 );
231 ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
232 memcpy ( dest, orig, sizeof ( dest ) );
233 copy = strncpy ( dest, "", 8 );
234 ok ( copy == dest );
235 ok ( memcmp ( dest, zero, 8 ) == 0 );
236 }
237
238 /* Test strcat() */
239 {
240 char buf[16] = "append";
241 char *dest;
242
243 dest = strcat ( buf, " this" );
244 ok ( dest == buf );
245 ok ( strcmp ( buf, "append this" ) == 0 );
246 }
247
248 /* Test digit_value() */
249 {
250 unsigned int i;
251 char buf[2];
252 for ( i = 0 ; i < 16 ; i++ ) {
253 snprintf ( buf, sizeof ( buf ), "%x", i );
254 ok ( digit_value ( buf[0] ) == i );
255 snprintf ( buf, sizeof ( buf ), "%X", i );
256 ok ( digit_value ( buf[0] ) == i );
257 }
258 ok ( digit_value ( 0 ) >= 16 );
259 ok ( digit_value ( 9 ) >= 16 );
260 ok ( digit_value ( '0' - 1 ) >= 16 );
261 ok ( digit_value ( '9' + 1 ) >= 16 );
262 ok ( digit_value ( 'A' - 1 ) >= 16 );
263 ok ( digit_value ( 'F' + 1 ) >= 16 );
264 ok ( digit_value ( 'a' - 1 ) >= 16 );
265 ok ( digit_value ( 'f' + 1 ) >= 16 );
266 }
267
268 /* Test strtoul() */
269 ok ( strtoul ( "12345", NULL, 0 ) == 12345UL );
270 ok ( strtoul ( " 741", NULL, 10 ) == 741UL );
271 ok ( strtoul ( " 555a", NULL, 0 ) == 555UL );
272 ok ( strtoul ( " 555a", NULL, 16 ) == 0x555aUL );
273 ok ( strtoul ( "-12", NULL, 0 ) == -12UL );
274 ok ( strtoul ( "+3", NULL, 0 ) == 3UL );
275 ok ( strtoul ( "721", NULL, 0 ) == 721UL );
276 ok ( strtoul ( "721", NULL, 8 ) == 0721UL );
277 ok ( strtoul ( "0721", NULL, 0 ) == 0721UL );
278 ok ( strtoul ( "", NULL, 0 ) == 0UL );
279 ok ( strtoul ( "\t0xcAfe", NULL, 0 ) == 0xcafeUL );
280 ok ( strtoul ( "0xffffffff", NULL, 0 ) == 0xffffffffUL );
281 {
282 static const char string[] = "123aHa.world";
283 char *endp;
284 ok ( strtoul ( string, &endp, 0 ) == 123UL );
285 ok ( endp == &string[3] );
286 ok ( strtoul ( string, &endp, 16 ) == 0x123aUL );
287 ok ( endp == &string[4] );
288 ok ( strtoul ( string, &endp, 26 ) ==
289 ( ( ( ( ( 1 * 26 + 2 ) * 26 + 3 ) * 26 + 10 ) * 26
290 + 17 ) * 26 + 10 ) );
291 ok ( endp == &string[6] );
292 }
293 }
294
295 /** String self-test */
296 struct self_test string_test __self_test = {
297 .name = "string",
298 .exec = string_test_exec,
299 };