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
74 /* Test strrchr() */
75 ok ( strrchr ( "", 'a' ) == NULL );
76 ok ( *(strrchr ( "Haystack", 'a' )) == 'a' );
77 ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
78 ok ( strrchr ( "Haystack", 'x' ) == NULL );
79
80 /* Test memchr() */
81 ok ( memchr ( "", '\0', 0 ) == NULL );
82 ok ( *((uint8_t *)memchr ( "post\0null", 'l', 9 )) == 'l' );
83 ok ( *((uint8_t *)memchr ( "post\0null", '\0', 9 )) == '\0' );
84 ok ( memchr ( "thingy", 'z', 6 ) == NULL );
85
86 /* Test strcmp() */
87 ok ( strcmp ( "", "" ) == 0 );
88 ok ( strcmp ( "Hello", "Hello" ) == 0 );
89 ok ( strcmp ( "Hello", "hello" ) != 0 );
90 ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
91 ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
92 ok ( strcmp ( "abc", "def" ) < 0 );
93
94 /* Test strncmp() */
95 ok ( strncmp ( "", "", 0 ) == 0 );
96 ok ( strncmp ( "", "", 15 ) == 0 );
97 ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
98 ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
99 ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
100 ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
101
102 /* Test strcasecmp() */
103 ok ( strcasecmp ( "", "" ) == 0 );
104 ok ( strcasecmp ( "Uncle Jack", "Uncle jack" ) == 0 );
105 ok ( strcasecmp ( "Uncle Jack", "Uncle" ) != 0 );
106 ok ( strcasecmp ( "Uncle", "Uncle Jack" ) != 0 );
107 ok ( strcasecmp ( "not", "equal" ) != 0 );
108
109 /* Test strncasecmp() */
110 ok ( strncasecmp ( "", "", 0 ) == 0 );
111 ok ( strncasecmp ( "", "", 73 ) == 0 );
112 ok ( strncasecmp ( "Uncle Jack", "Uncle jack", 47 ) == 0 );
113 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 47 ) != 0 );
114 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 9 ) != 0 );
115 ok ( strncasecmp ( "Uncle Jack", "Uncle jake", 8 ) == 0 );
116
117 /* Test memcmp() */
118 ok ( memcmp ( "", "", 0 ) == 0 );
119 ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
120 ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
121 ok ( memcmp ( "abc", "def", 3 ) < 0 );
122
123 /* Test strstr() */
124 {
125 const char haystack[] = "find me!";
126 char *found;
127
128 found = strstr ( haystack, "find" );
129 ok ( found == &haystack[0] );
130 found = strstr ( haystack, "me" );
131 ok ( found == &haystack[5] );
132 found = strstr ( haystack, "me." );
133 ok ( found == NULL );
134 }
135
136 /* Test memset() */
137 {
138 static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
139 static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
140 memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
141 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
142 }
143 {
144 static uint8_t test[4] = { '>', 0, 0, '<' };
145 static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
146 memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
147 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
148 }
149
150 /* Test memmove() */
151 {
152 static uint8_t test[11] =
153 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
154 static const uint8_t expected[11] =
155 { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
156 memmove ( ( test + 1 ), ( test + 3 ), 6 );
157 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
158 }
159 {
160 static uint8_t test[12] =
161 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
162 static const uint8_t expected[12] =
163 { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
164 memmove ( ( test + 6 ), ( test + 1 ), 5 );
165 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
166 }
167
168 /* Test memswap() */
169 {
170 static uint8_t test[8] =
171 { '>', 1, 2, 3, 7, 8, 9, '<' };
172 static const uint8_t expected[8] =
173 { '>', 7, 8, 9, 1, 2, 3, '<' };
174 memswap ( ( test + 1 ), ( test + 4 ), 3 );
175 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
176 }
177
178 /* Test strdup() */
179 {
180 const char *orig = "testing testing";
181 char *dup = strdup ( orig );
182 ok ( dup != NULL );
183 ok ( dup != orig );
184 ok ( strcmp ( dup, orig ) == 0 );
185 free ( dup );
186 }
187
188 /* Test strndup() */
189 {
190 const char *normal = "testing testing";
191 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
192 char *dup;
193 dup = strndup ( normal, 32 );
194 ok ( dup != NULL );
195 ok ( dup != normal );
196 ok ( strcmp ( dup, normal ) == 0 );
197 free ( dup );
198 dup = strndup ( normal, 4 );
199 ok ( dup != NULL );
200 ok ( strcmp ( dup, "test" ) == 0 );
201 free ( dup );
202 dup = strndup ( unterminated, 5 );
203 ok ( dup != NULL );
204 ok ( strcmp ( dup, "hello" ) == 0 );
205 free ( dup );
206 }
207
208 /* Test stpcpy() */
209 {
210 const char longer[12] = "duplicateme";
211 const char shorter[6] = "hello";
212 char dest[12];
213 char *dnul;
214
215 dnul = stpcpy ( dest, longer );
216 ok ( *dnul == '\0' );
217 ok ( dnul == &dest[11] );
218 ok ( memcmp ( dest, longer, 12 ) == 0 );
219 dnul = stpcpy ( dest, shorter );
220 ok ( *dnul == '\0' );
221 ok ( dnul == &dest[5] );
222 ok ( memcmp ( dest, shorter, 6 ) == 0 );
223 ok ( memcmp ( ( dest + 6 ), ( longer + 6 ), 6 ) == 0 );
224 }
225
226 /* Test strcpy() */
227 {
228 const char longer[7] = "copyme";
229 const char shorter[3] = "hi";
230 char dest[7];
231 char *copy;
232
233 copy = strcpy ( dest, longer );
234 ok ( copy == dest );
235 ok ( memcmp ( dest, longer, 7 ) == 0 );
236 copy = strcpy ( dest, shorter );
237 ok ( copy == dest );
238 ok ( memcmp ( dest, shorter, 3 ) == 0 );
239 ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
240 }
241
242 /* Test strncpy() */
243 {
244 const char src[5] = "copy";
245 const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
246 const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
247 char dest[8];
248 char *copy;
249
250 memcpy ( dest, orig, sizeof ( dest ) );
251 copy = strncpy ( dest, src, 5 );
252 ok ( copy == dest );
253 ok ( memcmp ( dest, src, 5 ) == 0 );
254 ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
255 memcpy ( dest, orig, sizeof ( dest ) );
256 copy = strncpy ( dest, src, 4 );
257 ok ( copy == dest );
258 ok ( memcmp ( dest, src, 4 ) == 0 );
259 ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
260 memcpy ( dest, orig, sizeof ( dest ) );
261 copy = strncpy ( dest, src, 8 );
262 ok ( copy == dest );
263 ok ( memcmp ( dest, src, 5 ) == 0 );
264 ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
265 memcpy ( dest, orig, sizeof ( dest ) );
266 copy = strncpy ( dest, "", 8 );
267 ok ( copy == dest );
268 ok ( memcmp ( dest, zero, 8 ) == 0 );
269 }
270
271 /* Test strcat() */
272 {
273 char buf[16] = "append";
274 char *dest;
275
276 dest = strcat ( buf, " this" );
277 ok ( dest == buf );
278 ok ( strcmp ( buf, "append this" ) == 0 );
279 }
280
281 /* Test digit_value() */
282 {
283 unsigned int i;
284 char buf[2];
285 for ( i = 0 ; i < 16 ; i++ ) {
286 snprintf ( buf, sizeof ( buf ), "%x", i );
287 ok ( digit_value ( buf[0] ) == i );
288 snprintf ( buf, sizeof ( buf ), "%X", i );
289 ok ( digit_value ( buf[0] ) == i );
290 }
291 ok ( digit_value ( 0 ) >= 16 );
292 ok ( digit_value ( 9 ) >= 16 );
293 ok ( digit_value ( '0' - 1 ) >= 16 );
294 ok ( digit_value ( '9' + 1 ) >= 16 );
295 ok ( digit_value ( 'A' - 1 ) >= 16 );
296 ok ( digit_value ( 'F' + 1 ) >= 16 );
297 ok ( digit_value ( 'a' - 1 ) >= 16 );
298 ok ( digit_value ( 'f' + 1 ) >= 16 );
299 }
300
301 /* Test strtoul() */
302 ok ( strtoul ( "12345", NULL, 0 ) == 12345UL );
303 ok ( strtoul ( " 741", NULL, 10 ) == 741UL );
304 ok ( strtoul ( " 555a", NULL, 0 ) == 555UL );
305 ok ( strtoul ( " 555a", NULL, 16 ) == 0x555aUL );
306 ok ( strtoul ( "-12", NULL, 0 ) == -12UL );
307 ok ( strtoul ( "+3", NULL, 0 ) == 3UL );
308 ok ( strtoul ( "721", NULL, 0 ) == 721UL );
309 ok ( strtoul ( "721", NULL, 8 ) == 0721UL );
310 ok ( strtoul ( "0721", NULL, 0 ) == 0721UL );
311 ok ( strtoul ( "", NULL, 0 ) == 0UL );
312 ok ( strtoul ( "\t0xcAfe", NULL, 0 ) == 0xcafeUL );
313 ok ( strtoul ( "0xffffffff", NULL, 0 ) == 0xffffffffUL );
314 {
315 static const char string[] = "123aHa.world";
316 char *endp;
317 ok ( strtoul ( string, &endp, 0 ) == 123UL );
318 ok ( endp == &string[3] );
319 ok ( strtoul ( string, &endp, 16 ) == 0x123aUL );
320 ok ( endp == &string[4] );
321 ok ( strtoul ( string, &endp, 26 ) ==
322 ( ( ( ( ( 1 * 26 + 2 ) * 26 + 3 ) * 26 + 10 ) * 26
323 + 17 ) * 26 + 10 ) );
324 ok ( endp == &string[6] );
325 }
326
327 /* Test wcslen() */
328 ok ( wcslen ( L"" ) == 0 );
329 ok ( wcslen ( L"Helloo" ) == 6 );
330 ok ( wcslen ( L"Helloo woorld!" ) == 14 );
331 ok ( wcslen ( L"Helloo\0woorld!" ) == 6 );
332
333 /* Test wcsnlen() */
334 ok ( wcsnlen ( L"", 0 ) == 0 );
335 ok ( wcsnlen ( L"", 10 ) == 0 );
336 ok ( wcsnlen ( L"Helloo", 0 ) == 0 );
337 ok ( wcsnlen ( L"Helloo", 3 ) == 3 );
338 ok ( wcsnlen ( L"Helloo", 5 ) == 5 );
339 ok ( wcsnlen ( L"Helloo", 16 ) == 6 );
340 ok ( wcsnlen ( L"Helloo woorld!", 5 ) == 5 );
341 ok ( wcsnlen ( L"Helloo woorld!", 11 ) == 11 );
342 ok ( wcsnlen ( L"Helloo woorld!", 16 ) == 14 );
343}
344
345/** String self-test */
346struct self_test string_test __self_test = {
347 .name = "string",
348 .exec = string_test_exec,
349};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
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:896
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:382
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:485
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:272
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:394
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:327
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:427
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:406
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition string.c:310
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition string.c:290
char * strcpy(char *dest, const char *src)
Copy string.
Definition string.c:347
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
char * strncpy(char *dest, const char *src, size_t max)
Copy string.
Definition string.c:361
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