[intel] Forcibly skip PHY reset on some models
[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
92 /* Test strncmp() */
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 );
99
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 );
106
107 /* Test memcmp() */
108 ok ( memcmp ( "", "", 0 ) == 0 );
109 ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
110 ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
111
112 /* Test strstr() */
113 {
114 const char haystack[] = "find me!";
115 char *found;
116
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 );
123 }
124
125 /* Test memset() */
126 {
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 );
131 }
132 {
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 );
137 }
138
139 /* Test memmove() */
140 {
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 );
147 }
148 {
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 );
155 }
156
157 /* Test memswap() */
158 {
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 );
165 }
166
167 /* Test strdup() */
168 {
169 const char *orig = "testing testing";
170 char *dup = strdup ( orig );
171 ok ( dup != NULL );
172 ok ( dup != orig );
173 ok ( strcmp ( dup, orig ) == 0 );
174 free ( dup );
175 }
176
177 /* Test strndup() */
178 {
179 const char *normal = "testing testing";
180 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
181 char *dup;
182 dup = strndup ( normal, 32 );
183 ok ( dup != NULL );
184 ok ( dup != normal );
185 ok ( strcmp ( dup, normal ) == 0 );
186 free ( dup );
187 dup = strndup ( normal, 4 );
188 ok ( dup != NULL );
189 ok ( strcmp ( dup, "test" ) == 0 );
190 free ( dup );
191 dup = strndup ( unterminated, 5 );
192 ok ( dup != NULL );
193 ok ( strcmp ( dup, "hello" ) == 0 );
194 free ( dup );
195 }
196
197 /* Test strcpy() */
198 {
199 const char longer[7] = "copyme";
200 const char shorter[3] = "hi";
201 char dest[7];
202 char *copy;
203
204 copy = strcpy ( dest, longer );
205 ok ( copy == dest );
206 ok ( memcmp ( dest, longer, 7 ) == 0 );
207 copy = strcpy ( dest, shorter );
208 ok ( copy == dest );
209 ok ( memcmp ( dest, shorter, 3 ) == 0 );
210 ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
211 }
212
213 /* Test strncpy() */
214 {
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 };
218 char dest[8];
219 char *copy;
220
221 memcpy ( dest, orig, sizeof ( dest ) );
222 copy = strncpy ( dest, src, 5 );
223 ok ( copy == dest );
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 );
228 ok ( copy == dest );
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 );
233 ok ( copy == dest );
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 );
238 ok ( copy == dest );
239 ok ( memcmp ( dest, zero, 8 ) == 0 );
240 }
241
242 /* Test strcat() */
243 {
244 char buf[16] = "append";
245 char *dest;
246
247 dest = strcat ( buf, " this" );
248 ok ( dest == buf );
249 ok ( strcmp ( buf, "append this" ) == 0 );
250 }
251
252 /* Test digit_value() */
253 {
254 unsigned int i;
255 char buf[2];
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 );
261 }
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 );
270 }
271
272 /* Test strtoul() */
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 );
285 {
286 static const char string[] = "123aHa.world";
287 char *endp;
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] );
296 }
297 }
298
299 /** String self-test */
300 struct self_test string_test __self_test = {
301 .name = "string",
302 .exec = string_test_exec,
303 };