Browse Source

[test] Add self-tests for more string functions

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
cd8273b308
1 changed files with 88 additions and 0 deletions
  1. 88
    0
      src/tests/string_test.c

+ 88
- 0
src/tests/string_test.c View File

@@ -32,6 +32,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
32 32
 #include <stdint.h>
33 33
 #include <stdlib.h>
34 34
 #include <string.h>
35
+#include <strings.h>
35 36
 #include <ipxe/test.h>
36 37
 
37 38
 /**
@@ -63,6 +64,18 @@ static void string_test_exec ( void ) {
63 64
 	ok ( *(strchr ( "Testing", 'g' )) == 'g' );
64 65
 	ok ( strchr ( "Testing", 'x' ) == NULL );
65 66
 
67
+	/* Test strrchr() */
68
+	ok ( strrchr ( "", 'a' ) == NULL );
69
+	ok ( *(strrchr ( "Haystack", 'a' )) == 'a' );
70
+	ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
71
+	ok ( strrchr ( "Haystack", 'x' ) == NULL );
72
+
73
+	/* Test memchr() */
74
+	ok ( memchr ( "", '\0', 0 ) == NULL );
75
+	ok ( *((uint8_t *)memchr ( "post\0null", 'l', 9 )) == 'l' );
76
+	ok ( *((uint8_t *)memchr ( "post\0null", '\0', 9 )) == '\0' );
77
+	ok ( memchr ( "thingy", 'z', 6 ) == NULL );
78
+
66 79
 	/* Test strcmp() */
67 80
 	ok ( strcmp ( "", "" ) == 0 );
68 81
 	ok ( strcmp ( "Hello", "Hello" ) == 0 );
@@ -78,11 +91,31 @@ static void string_test_exec ( void ) {
78 91
 	ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
79 92
 	ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
80 93
 
94
+	/* Test strcasecmp() */
95
+	ok ( strcasecmp ( "", "" ) == 0 );
96
+	ok ( strcasecmp ( "Uncle Jack", "Uncle jack" ) == 0 );
97
+	ok ( strcasecmp ( "Uncle Jack", "Uncle" ) != 0 );
98
+	ok ( strcasecmp ( "Uncle", "Uncle Jack" ) != 0 );
99
+	ok ( strcasecmp ( "not", "equal" ) != 0 );
100
+
81 101
 	/* Test memcmp() */
82 102
 	ok ( memcmp ( "", "", 0 ) == 0 );
83 103
 	ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
84 104
 	ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
85 105
 
106
+	/* Test strstr() */
107
+	{
108
+		const char haystack[] = "find me!";
109
+		char *found;
110
+
111
+		found = strstr ( haystack, "find" );
112
+		ok ( found == &haystack[0] );
113
+		found = strstr ( haystack, "me" );
114
+		ok ( found == &haystack[5] );
115
+		found = strstr ( haystack, "me." );
116
+		ok ( found == NULL );
117
+	}
118
+
86 119
 	/* Test memset() */
87 120
 	{
88 121
 		static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
@@ -154,6 +187,61 @@ static void string_test_exec ( void ) {
154 187
 		ok ( strcmp ( dup, "hello" ) == 0 );
155 188
 		free ( dup );
156 189
 	}
190
+
191
+	/* Test strcpy() */
192
+	{
193
+		const char longer[7] = "copyme";
194
+		const char shorter[3] = "hi";
195
+		char dest[7];
196
+		char *copy;
197
+
198
+		copy = strcpy ( dest, longer );
199
+		ok ( copy == dest );
200
+		ok ( memcmp ( dest, longer, 7 ) == 0 );
201
+		copy = strcpy ( dest, shorter );
202
+		ok ( copy == dest );
203
+		ok ( memcmp ( dest, shorter, 3 ) == 0 );
204
+		ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
205
+	}
206
+
207
+	/* Test strncpy() */
208
+	{
209
+		const char src[5] = "copy";
210
+		const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
211
+		const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
212
+		char dest[8];
213
+		char *copy;
214
+
215
+		memcpy ( dest, orig, sizeof ( dest ) );
216
+		copy = strncpy ( dest, src, 5 );
217
+		ok ( copy == dest );
218
+		ok ( memcmp ( dest, src, 5 ) == 0 );
219
+		ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
220
+		memcpy ( dest, orig, sizeof ( dest ) );
221
+		copy = strncpy ( dest, src, 4 );
222
+		ok ( copy == dest );
223
+		ok ( memcmp ( dest, src, 4 ) == 0 );
224
+		ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
225
+		memcpy ( dest, orig, sizeof ( dest ) );
226
+		copy = strncpy ( dest, src, 8 );
227
+		ok ( copy == dest );
228
+		ok ( memcmp ( dest, src, 5 ) == 0 );
229
+		ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
230
+		memcpy ( dest, orig, sizeof ( dest ) );
231
+		copy = strncpy ( dest, "", 8 );
232
+		ok ( copy == dest );
233
+		ok ( memcmp ( dest, zero, 8 ) == 0 );
234
+	}
235
+
236
+	/* Test strcat() */
237
+	{
238
+		char buf[16] = "append";
239
+		char *dest;
240
+
241
+		dest = strcat ( buf, " this" );
242
+		ok ( dest == buf );
243
+		ok ( strcmp ( buf, "append this" ) == 0 );
244
+	}
157 245
 }
158 246
 
159 247
 /** String self-test */

Loading…
Cancel
Save