iPXE
Functions | Variables
string_test.c File Reference

String self-tests. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <wchar.h>
#include <ipxe/string.h>
#include <ipxe/test.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void string_test_exec (void)
 Perform string self-tests. More...
 

Variables

struct self_test string_test __self_test
 String self-test. More...
 

Detailed Description

String self-tests.

memcpy() tests are handled separately

Definition in file string_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ string_test_exec()

static void string_test_exec ( void  )
static

Perform string self-tests.

Definition at line 49 of file string_test.c.

49  {
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 }
int expected
Expected value.
Definition: setjmp_test.c:45
void * memswap(void *first, void *second, size_t len)
Swap memory regions.
Definition: string.c:153
struct arbelprm_completion_queue_entry normal
Definition: arbel.h:11
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:484
size_t wcsnlen(const wchar_t *string, size_t max)
Calculate length of wide-character string.
Definition: wchar.c:42
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:289
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:208
int strncasecmp(const char *first, const char *second, size_t max)
Compare case-insensitive strings.
Definition: string.c:221
unsigned int digit_value(unsigned int character)
Calculate digit value.
Definition: string.c:426
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition: string.c:186
size_t wcslen(const wchar_t *string)
Calculate length of wide-character string.
Definition: wchar.c:56
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition: string.c:135
char * strncpy(char *dest, const char *src, size_t max)
Copy string.
Definition: string.c:360
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition: string.c:309
void * memcpy(void *dest, const void *src, size_t len) __nonnull
char * stpcpy(char *dest, const char *src)
Copy string.
Definition: string.c:326
static const void * src
Definition: string.h:47
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:346
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:271
char * strdup(const char *src)
Duplicate string.
Definition: string.c:393
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
unsigned char uint8_t
Definition: stdint.h:10
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition: string.c:255
void * memmove(void *dest, const void *src, size_t len) __nonnull
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
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" return dest
Definition: string.h:150
char * strndup(const char *src, size_t max)
Duplicate string.
Definition: string.c:405
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
char * strcat(char *dest, const char *src)
Concatenate string.
Definition: string.c:381
#define ok(success)
Definition: test.h:46
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static int test
Definition: epic100.c:73
void * memset(void *dest, int character, size_t len) __nonnull

References dest, digit_value(), free, memchr(), memcmp(), memcpy(), memmove(), memset(), memswap(), normal, NULL, ok, snprintf(), src, stpcpy(), strcasecmp(), strcat(), strchr(), strcmp(), strcpy(), strdup(), strlen(), strncasecmp(), strncmp(), strncpy(), strndup(), strnlen(), strrchr(), strstr(), strtoul(), test, wcslen(), and wcsnlen().

Variable Documentation

◆ __self_test

struct self_test string_test __self_test
Initial value:
= {
.name = "string",
}
static void string_test_exec(void)
Perform string self-tests.
Definition: string_test.c:49

String self-test.

Definition at line 346 of file string_test.c.