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 <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 48 of file string_test.c.

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

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:48

String self-test.

Definition at line 328 of file string_test.c.