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