iPXE
string_test.c
Go to the documentation of this file.
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
24FILE_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 <wchar.h>
42#include <ipxe/string.h>
43#include <ipxe/test.h>
44
45/**
46 * Perform string self-tests
47 *
48 */
49static void string_test_exec ( void ) {
50
51 /* Test strlen() */
52 ok ( strlen ( "" ) == 0 );
53 ok ( strlen ( "Hello" ) == 5 );
54 ok ( strlen ( "Hello world!" ) == 12 );
55 ok ( strlen ( "Hello\0world!" ) == 5 );
56
57 /* Test strnlen() */
58 ok ( strnlen ( "", 0 ) == 0 );
59 ok ( strnlen ( "", 10 ) == 0 );
60 ok ( strnlen ( "Hello", 0 ) == 0 );
61 ok ( strnlen ( "Hello", 3 ) == 3 );
62 ok ( strnlen ( "Hello", 5 ) == 5 );
63 ok ( strnlen ( "Hello", 16 ) == 5 );
64 ok ( strnlen ( "Hello world!", 5 ) == 5 );
65 ok ( strnlen ( "Hello world!", 11 ) == 11 );
66 ok ( strnlen ( "Hello world!", 16 ) == 12 );
67
68 /* Test strchr() */
69 ok ( strchr ( "", 'a' ) == NULL );
70 ok ( *(strchr ( "Testing", 'e' )) == 'e' );
71 ok ( *(strchr ( "Testing", 'g' )) == 'g' );
72 ok ( strchr ( "Testing", 'x' ) == NULL );
73 ok ( *(strchr ( "Testing", '\0' )) == '\0' );
74
75 /* Test strrchr() */
76 ok ( strrchr ( "", 'a' ) == NULL );
77 ok ( *(strrchr ( "Haystack", 'a' )) == 'a' );
78 ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
79 ok ( strrchr ( "Haystack", 'x' ) == NULL );
80
81 /* Test strchrnul() */
82 ok ( *(strchrnul ( "", 'x' )) == '\0' );
83 ok ( *(strchrnul ( "Nonstandard", 's' )) == 's' );
84 ok ( *(strchrnul ( "Nonstandard", 't' )) == 't' );
85 ok ( *(strchrnul ( "Nonstandard", 'x' )) == '\0' );
86 ok ( *(strchrnul ( "Nonstandard", '\0' )) == '\0' );
87
88 /* Test memchr() */
89 ok ( memchr ( "", '\0', 0 ) == NULL );
90 ok ( *((uint8_t *)memchr ( "post\0null", 'l', 9 )) == 'l' );
91 ok ( *((uint8_t *)memchr ( "post\0null", '\0', 9 )) == '\0' );
92 ok ( memchr ( "thingy", 'z', 6 ) == NULL );
93
94 /* Test strcmp() */
95 ok ( strcmp ( "", "" ) == 0 );
96 ok ( strcmp ( "Hello", "Hello" ) == 0 );
97 ok ( strcmp ( "Hello", "hello" ) != 0 );
98 ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
99 ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
100 ok ( strcmp ( "abc", "def" ) < 0 );
101
102 /* Test strncmp() */
103 ok ( strncmp ( "", "", 0 ) == 0 );
104 ok ( strncmp ( "", "", 15 ) == 0 );
105 ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
106 ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
107 ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
108 ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
109
110 /* Test strcasecmp() */
111 ok ( strcasecmp ( "", "" ) == 0 );
112 ok ( strcasecmp ( "Uncle Jack", "Uncle jack" ) == 0 );
113 ok ( strcasecmp ( "Uncle Jack", "Uncle" ) != 0 );
114 ok ( strcasecmp ( "Uncle", "Uncle Jack" ) != 0 );
115 ok ( strcasecmp ( "not", "equal" ) != 0 );
116
117 /* Test strncasecmp() */
118 ok ( strncasecmp ( "", "", 0 ) == 0 );
119 ok ( strncasecmp ( "", "", 73 ) == 0 );
120 ok ( strncasecmp ( "Uncle Jack", "Uncle jack", 47 ) == 0 );
121 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 47 ) != 0 );
122 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 9 ) != 0 );
123 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 8 ) == 0 );
124
125 /* Test memcmp() */
126 ok ( memcmp ( "", "", 0 ) == 0 );
127 ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
128 ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
129 ok ( memcmp ( "abc", "def", 3 ) < 0 );
130
131 /* Test strstr() */
132 {
133 const char haystack[] = "find me!";
134 char *found;
135
136 found = strstr ( haystack, "find" );
137 ok ( found == &haystack[0] );
138 found = strstr ( haystack, "me" );
139 ok ( found == &haystack[5] );
140 found = strstr ( haystack, "me." );
141 ok ( found == NULL );
142 }
143
144 /* Test strcasestr() */
145 {
146 const char haystack[] = "Find ME!";
147 char *found;
148
149 found = strcasestr ( haystack, "FIND" );
150 ok ( found == &haystack[0] );
151 found = strcasestr ( haystack, "me" );
152 ok ( found == &haystack[5] );
153 found = strcasestr ( haystack, "me." );
154 ok ( found == NULL );
155 }
156
157 /* Test memset() */
158 {
159 static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
160 static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
161 memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
162 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
163 }
164 {
165 static uint8_t test[4] = { '>', 0, 0, '<' };
166 static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
167 memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
168 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
169 }
170
171 /* Test memmove() */
172 {
173 static uint8_t test[11] =
174 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
175 static const uint8_t expected[11] =
176 { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
177 memmove ( ( test + 1 ), ( test + 3 ), 6 );
178 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
179 }
180 {
181 static uint8_t test[12] =
182 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
183 static const uint8_t expected[12] =
184 { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
185 memmove ( ( test + 6 ), ( test + 1 ), 5 );
186 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
187 }
188
189 /* Test memswap() */
190 {
191 static uint8_t test[8] =
192 { '>', 1, 2, 3, 7, 8, 9, '<' };
193 static const uint8_t expected[8] =
194 { '>', 7, 8, 9, 1, 2, 3, '<' };
195 memswap ( ( test + 1 ), ( test + 4 ), 3 );
196 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
197 }
198
199 /* Test strdup() */
200 {
201 const char *orig = "testing testing";
202 char *dup = strdup ( orig );
203 ok ( dup != NULL );
204 ok ( dup != orig );
205 ok ( strcmp ( dup, orig ) == 0 );
206 free ( dup );
207 }
208
209 /* Test strndup() */
210 {
211 const char *normal = "testing testing";
212 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
213 char *dup;
214 dup = strndup ( normal, 32 );
215 ok ( dup != NULL );
216 ok ( dup != normal );
217 ok ( strcmp ( dup, normal ) == 0 );
218 free ( dup );
219 dup = strndup ( normal, 4 );
220 ok ( dup != NULL );
221 ok ( strcmp ( dup, "test" ) == 0 );
222 free ( dup );
223 dup = strndup ( unterminated, 5 );
224 ok ( dup != NULL );
225 ok ( strcmp ( dup, "hello" ) == 0 );
226 free ( dup );
227 }
228
229 /* Test stpcpy() */
230 {
231 const char longer[12] = "duplicateme";
232 const char shorter[6] = "hello";
233 char dest[12];
234 char *dnul;
235
236 dnul = stpcpy ( dest, longer );
237 ok ( *dnul == '\0' );
238 ok ( dnul == &dest[11] );
239 ok ( memcmp ( dest, longer, 12 ) == 0 );
240 dnul = stpcpy ( dest, shorter );
241 ok ( *dnul == '\0' );
242 ok ( dnul == &dest[5] );
243 ok ( memcmp ( dest, shorter, 6 ) == 0 );
244 ok ( memcmp ( ( dest + 6 ), ( longer + 6 ), 6 ) == 0 );
245 }
246
247 /* Test strcpy() */
248 {
249 const char longer[7] = "copyme";
250 const char shorter[3] = "hi";
251 char dest[7];
252 char *copy;
253
254 copy = strcpy ( dest, longer );
255 ok ( copy == dest );
256 ok ( memcmp ( dest, longer, 7 ) == 0 );
257 copy = strcpy ( dest, shorter );
258 ok ( copy == dest );
259 ok ( memcmp ( dest, shorter, 3 ) == 0 );
260 ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
261 }
262
263 /* Test strncpy() */
264 {
265 const char src[5] = "copy";
266 const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
267 const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
268 char dest[8];
269 char *copy;
270
271 memcpy ( dest, orig, sizeof ( dest ) );
272 copy = strncpy ( dest, src, 5 );
273 ok ( copy == dest );
274 ok ( memcmp ( dest, src, 5 ) == 0 );
275 ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
276 memcpy ( dest, orig, sizeof ( dest ) );
277 copy = strncpy ( dest, src, 4 );
278 ok ( copy == dest );
279 ok ( memcmp ( dest, src, 4 ) == 0 );
280 ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
281 memcpy ( dest, orig, sizeof ( dest ) );
282 copy = strncpy ( dest, src, 8 );
283 ok ( copy == dest );
284 ok ( memcmp ( dest, src, 5 ) == 0 );
285 ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
286 memcpy ( dest, orig, sizeof ( dest ) );
287 copy = strncpy ( dest, "", 8 );
288 ok ( copy == dest );
289 ok ( memcmp ( dest, zero, 8 ) == 0 );
290 }
291
292 /* Test strcat() */
293 {
294 char buf[16] = "append";
295 char *dest;
296
297 dest = strcat ( buf, " this" );
298 ok ( dest == buf );
299 ok ( strcmp ( buf, "append this" ) == 0 );
300 }
301
302 /* Test digit_value() */
303 {
304 unsigned int i;
305 char buf[2];
306 for ( i = 0 ; i < 16 ; i++ ) {
307 snprintf ( buf, sizeof ( buf ), "%x", i );
308 ok ( digit_value ( buf[0] ) == i );
309 snprintf ( buf, sizeof ( buf ), "%X", i );
310 ok ( digit_value ( buf[0] ) == i );
311 }
312 ok ( digit_value ( 0 ) >= 16 );
313 ok ( digit_value ( 9 ) >= 16 );
314 ok ( digit_value ( '0' - 1 ) >= 16 );
315 ok ( digit_value ( '9' + 1 ) >= 16 );
316 ok ( digit_value ( 'A' - 1 ) >= 16 );
317 ok ( digit_value ( 'F' + 1 ) >= 16 );
318 ok ( digit_value ( 'a' - 1 ) >= 16 );
319 ok ( digit_value ( 'f' + 1 ) >= 16 );
320 }
321
322 /* Test strtoul() */
323 ok ( strtoul ( "12345", NULL, 0 ) == 12345UL );
324 ok ( strtoul ( " 741", NULL, 10 ) == 741UL );
325 ok ( strtoul ( " 555a", NULL, 0 ) == 555UL );
326 ok ( strtoul ( " 555a", NULL, 16 ) == 0x555aUL );
327 ok ( strtoul ( "-12", NULL, 0 ) == -12UL );
328 ok ( strtoul ( "+3", NULL, 0 ) == 3UL );
329 ok ( strtoul ( "721", NULL, 0 ) == 721UL );
330 ok ( strtoul ( "721", NULL, 8 ) == 0721UL );
331 ok ( strtoul ( "0721", NULL, 0 ) == 0721UL );
332 ok ( strtoul ( "", NULL, 0 ) == 0UL );
333 ok ( strtoul ( "\t0xcAfe", NULL, 0 ) == 0xcafeUL );
334 ok ( strtoul ( "0xffffffff", NULL, 0 ) == 0xffffffffUL );
335 {
336 static const char string[] = "123aHa.world";
337 char *endp;
338 ok ( strtoul ( string, &endp, 0 ) == 123UL );
339 ok ( endp == &string[3] );
340 ok ( strtoul ( string, &endp, 16 ) == 0x123aUL );
341 ok ( endp == &string[4] );
342 ok ( strtoul ( string, &endp, 26 ) ==
343 ( ( ( ( ( 1 * 26 + 2 ) * 26 + 3 ) * 26 + 10 ) * 26
344 + 17 ) * 26 + 10 ) );
345 ok ( endp == &string[6] );
346 }
347
348 /* Test wcslen() */
349 ok ( wcslen ( L"" ) == 0 );
350 ok ( wcslen ( L"Helloo" ) == 6 );
351 ok ( wcslen ( L"Helloo woorld!" ) == 14 );
352 ok ( wcslen ( L"Helloo\0woorld!" ) == 6 );
353
354 /* Test wcsnlen() */
355 ok ( wcsnlen ( L"", 0 ) == 0 );
356 ok ( wcsnlen ( L"", 10 ) == 0 );
357 ok ( wcsnlen ( L"Helloo", 0 ) == 0 );
358 ok ( wcsnlen ( L"Helloo", 3 ) == 3 );
359 ok ( wcsnlen ( L"Helloo", 5 ) == 5 );
360 ok ( wcsnlen ( L"Helloo", 16 ) == 6 );
361 ok ( wcsnlen ( L"Helloo woorld!", 5 ) == 5 );
362 ok ( wcsnlen ( L"Helloo woorld!", 11 ) == 11 );
363 ok ( wcsnlen ( L"Helloo woorld!", 16 ) == 14 );
364}
365
366/** String self-test */
367struct self_test string_test __self_test = {
368 .name = "string",
369 .exec = string_test_exec,
370};
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct arbelprm_completion_queue_entry normal
Definition arbel.h:0
unsigned char uint8_t
Definition stdint.h:10
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" retur dest)
Definition string.h:151
static const void * src
Definition string.h:48
static int test
Definition epic100.c:73
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
String functions.
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
void * memmove(void *dest, const void *src, size_t len) __nonnull
String functions.
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
char * strcat(char *dest, const char *src)
Concatenate string.
Definition string.c:413
void * memswap(void *first, void *second, size_t len)
Swap memory regions.
Definition string.c:154
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition string.c:516
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
char * strchr(const char *src, int character)
Find character within a string.
Definition string.c:288
int strncasecmp(const char *first, const char *second, size_t max)
Compare case-insensitive strings.
Definition string.c:222
char * strdup(const char *src)
Duplicate string.
Definition string.c:425
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition string.c:136
char * stpcpy(char *dest, const char *src)
Copy string.
Definition string.c:358
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition string.c:187
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition string.c:256
unsigned int digit_value(unsigned int character)
Calculate digit value.
Definition string.c:458
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition string.c:209
char * strndup(const char *src, size_t max)
Duplicate string.
Definition string.c:437
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition string.c:324
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition string.c:304
char * strcpy(char *dest, const char *src)
Copy string.
Definition string.c:378
char * strcasestr(const char *haystack, const char *needle)
Find case-insensitive substring.
Definition string.c:341
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
char * strchrnul(const char *src, int character)
Find character within a string, or NUL terminator.
Definition string.c:272
char * strncpy(char *dest, const char *src, size_t max)
Copy string.
Definition string.c:392
static void string_test_exec(void)
Perform string self-tests.
Definition string_test.c:49
A self-test set.
Definition test.h:15
Self-test infrastructure.
#define ok(success)
Definition test.h:46
#define __self_test
Declare a self-test.
Definition test.h:32
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition vsprintf.c:383
size_t wcsnlen(const wchar_t *string, size_t max)
Calculate length of wide-character string.
Definition wchar.c:43
size_t wcslen(const wchar_t *string)
Calculate length of wide-character string.
Definition wchar.c:57