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 memcmp() */
109  ok ( memcmp ( "", "", 0 ) == 0 );
110  ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
111  ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
112  ok ( memcmp ( "abc", "def", 3 ) < 0 );
113 
114  /* Test strstr() */
115  {
116  const char haystack[] = "find me!";
117  char *found;
118 
119  found = strstr ( haystack, "find" );
120  ok ( found == &haystack[0] );
121  found = strstr ( haystack, "me" );
122  ok ( found == &haystack[5] );
123  found = strstr ( haystack, "me." );
124  ok ( found == NULL );
125  }
126 
127  /* Test memset() */
128  {
129  static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
130  static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
131  memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
132  ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
133  }
134  {
135  static uint8_t test[4] = { '>', 0, 0, '<' };
136  static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
137  memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
138  ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
139  }
140 
141  /* Test memmove() */
142  {
143  static uint8_t test[11] =
144  { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
145  static const uint8_t expected[11] =
146  { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
147  memmove ( ( test + 1 ), ( test + 3 ), 6 );
148  ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
149  }
150  {
151  static uint8_t test[12] =
152  { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
153  static const uint8_t expected[12] =
154  { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
155  memmove ( ( test + 6 ), ( test + 1 ), 5 );
156  ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
157  }
158 
159  /* Test memswap() */
160  {
161  static uint8_t test[8] =
162  { '>', 1, 2, 3, 7, 8, 9, '<' };
163  static const uint8_t expected[8] =
164  { '>', 7, 8, 9, 1, 2, 3, '<' };
165  memswap ( ( test + 1 ), ( test + 4 ), 3 );
166  ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
167  }
168 
169  /* Test strdup() */
170  {
171  const char *orig = "testing testing";
172  char *dup = strdup ( orig );
173  ok ( dup != NULL );
174  ok ( dup != orig );
175  ok ( strcmp ( dup, orig ) == 0 );
176  free ( dup );
177  }
178 
179  /* Test strndup() */
180  {
181  const char *normal = "testing testing";
182  const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
183  char *dup;
184  dup = strndup ( normal, 32 );
185  ok ( dup != NULL );
186  ok ( dup != normal );
187  ok ( strcmp ( dup, normal ) == 0 );
188  free ( dup );
189  dup = strndup ( normal, 4 );
190  ok ( dup != NULL );
191  ok ( strcmp ( dup, "test" ) == 0 );
192  free ( dup );
193  dup = strndup ( unterminated, 5 );
194  ok ( dup != NULL );
195  ok ( strcmp ( dup, "hello" ) == 0 );
196  free ( dup );
197  }
198 
199  /* Test strcpy() */
200  {
201  const char longer[7] = "copyme";
202  const char shorter[3] = "hi";
203  char dest[7];
204  char *copy;
205 
206  copy = strcpy ( dest, longer );
207  ok ( copy == dest );
208  ok ( memcmp ( dest, longer, 7 ) == 0 );
209  copy = strcpy ( dest, shorter );
210  ok ( copy == dest );
211  ok ( memcmp ( dest, shorter, 3 ) == 0 );
212  ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
213  }
214 
215  /* Test strncpy() */
216  {
217  const char src[5] = "copy";
218  const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
219  const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
220  char dest[8];
221  char *copy;
222 
223  memcpy ( dest, orig, sizeof ( dest ) );
224  copy = strncpy ( dest, src, 5 );
225  ok ( copy == dest );
226  ok ( memcmp ( dest, src, 5 ) == 0 );
227  ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
228  memcpy ( dest, orig, sizeof ( dest ) );
229  copy = strncpy ( dest, src, 4 );
230  ok ( copy == dest );
231  ok ( memcmp ( dest, src, 4 ) == 0 );
232  ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
233  memcpy ( dest, orig, sizeof ( dest ) );
234  copy = strncpy ( dest, src, 8 );
235  ok ( copy == dest );
236  ok ( memcmp ( dest, src, 5 ) == 0 );
237  ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
238  memcpy ( dest, orig, sizeof ( dest ) );
239  copy = strncpy ( dest, "", 8 );
240  ok ( copy == dest );
241  ok ( memcmp ( dest, zero, 8 ) == 0 );
242  }
243 
244  /* Test strcat() */
245  {
246  char buf[16] = "append";
247  char *dest;
248 
249  dest = strcat ( buf, " this" );
250  ok ( dest == buf );
251  ok ( strcmp ( buf, "append this" ) == 0 );
252  }
253 
254  /* Test digit_value() */
255  {
256  unsigned int i;
257  char buf[2];
258  for ( i = 0 ; i < 16 ; i++ ) {
259  snprintf ( buf, sizeof ( buf ), "%x", i );
260  ok ( digit_value ( buf[0] ) == i );
261  snprintf ( buf, sizeof ( buf ), "%X", i );
262  ok ( digit_value ( buf[0] ) == i );
263  }
264  ok ( digit_value ( 0 ) >= 16 );
265  ok ( digit_value ( 9 ) >= 16 );
266  ok ( digit_value ( '0' - 1 ) >= 16 );
267  ok ( digit_value ( '9' + 1 ) >= 16 );
268  ok ( digit_value ( 'A' - 1 ) >= 16 );
269  ok ( digit_value ( 'F' + 1 ) >= 16 );
270  ok ( digit_value ( 'a' - 1 ) >= 16 );
271  ok ( digit_value ( 'f' + 1 ) >= 16 );
272  }
273 
274  /* Test strtoul() */
275  ok ( strtoul ( "12345", NULL, 0 ) == 12345UL );
276  ok ( strtoul ( " 741", NULL, 10 ) == 741UL );
277  ok ( strtoul ( " 555a", NULL, 0 ) == 555UL );
278  ok ( strtoul ( " 555a", NULL, 16 ) == 0x555aUL );
279  ok ( strtoul ( "-12", NULL, 0 ) == -12UL );
280  ok ( strtoul ( "+3", NULL, 0 ) == 3UL );
281  ok ( strtoul ( "721", NULL, 0 ) == 721UL );
282  ok ( strtoul ( "721", NULL, 8 ) == 0721UL );
283  ok ( strtoul ( "0721", NULL, 0 ) == 0721UL );
284  ok ( strtoul ( "", NULL, 0 ) == 0UL );
285  ok ( strtoul ( "\t0xcAfe", NULL, 0 ) == 0xcafeUL );
286  ok ( strtoul ( "0xffffffff", NULL, 0 ) == 0xffffffffUL );
287  {
288  static const char string[] = "123aHa.world";
289  char *endp;
290  ok ( strtoul ( string, &endp, 0 ) == 123UL );
291  ok ( endp == &string[3] );
292  ok ( strtoul ( string, &endp, 16 ) == 0x123aUL );
293  ok ( endp == &string[4] );
294  ok ( strtoul ( string, &endp, 26 ) ==
295  ( ( ( ( ( 1 * 26 + 2 ) * 26 + 3 ) * 26 + 10 ) * 26
296  + 17 ) * 26 + 10 ) );
297  ok ( endp == &string[6] );
298  }
299 }
int expected
Expected value.
Definition: setjmp_test.c:45
void * memswap(void *first, void *second, size_t len)
Swap memory regions.
Definition: string.c:152
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:456
char * strrchr(const char *src, int character)
Find rightmost character within a string.
Definition: string.c:274
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:207
uint32_t zero
Must be zero.
Definition: ntlm.h:24
unsigned int digit_value(unsigned int character)
Calculate digit value.
Definition: string.c:398
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition: string.c:185
void * memchr(const void *src, int character, size_t len)
Find character within a memory region.
Definition: string.c:134
char * strncpy(char *dest, const char *src, size_t max)
Copy string.
Definition: string.c:332
char * strstr(const char *haystack, const char *needle)
Find substring.
Definition: string.c:294
void * memcpy(void *dest, const void *src, size_t len) __nonnull
char * strcpy(char *dest, const char *src)
Copy string.
Definition: string.c:311
static void * dest
Definition: strings.h:176
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
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:256
char * strdup(const char *src)
Duplicate string.
Definition: string.c:365
size_t strlen(const char *src)
Get length of string.
Definition: string.c:228
unsigned char uint8_t
Definition: stdint.h:10
size_t strnlen(const char *src, size_t max)
Get length of string.
Definition: string.c:240
void * memmove(void *dest, const void *src, size_t len) __nonnull
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:172
char * strndup(const char *src, size_t max)
Duplicate string.
Definition: string.c:377
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:353
#define ok(success)
Definition: test.h:46
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:113
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
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, strcasecmp(), strcat(), strchr(), strcmp(), strcpy(), strdup(), strlen(), 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 302 of file string_test.c.