2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 * memcpy() tests are handled separately
29 /* Forcibly enable assertions */
35 #include <ipxe/test.h>
38 * Perform string self-tests
41 static void string_test_exec ( void ) {
44 ok ( strlen ( "" ) == 0 );
45 ok ( strlen ( "Hello" ) == 5 );
46 ok ( strlen ( "Hello world!" ) == 12 );
47 ok ( strlen ( "Hello\0world!" ) == 5 );
50 ok ( strnlen ( "", 0 ) == 0 );
51 ok ( strnlen ( "", 10 ) == 0 );
52 ok ( strnlen ( "Hello", 0 ) == 0 );
53 ok ( strnlen ( "Hello", 3 ) == 3 );
54 ok ( strnlen ( "Hello", 5 ) == 5 );
55 ok ( strnlen ( "Hello", 16 ) == 5 );
56 ok ( strnlen ( "Hello world!", 5 ) == 5 );
57 ok ( strnlen ( "Hello world!", 11 ) == 11 );
58 ok ( strnlen ( "Hello world!", 16 ) == 12 );
61 ok ( strchr ( "", 'a' ) == NULL );
62 ok ( *(strchr ( "Testing", 'e' )) == 'e' );
63 ok ( *(strchr ( "Testing", 'g' )) == 'g' );
64 ok ( strchr ( "Testing", 'x' ) == NULL );
67 ok ( strcmp ( "", "" ) == 0 );
68 ok ( strcmp ( "Hello", "Hello" ) == 0 );
69 ok ( strcmp ( "Hello", "hello" ) != 0 );
70 ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
71 ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
74 ok ( strncmp ( "", "", 0 ) == 0 );
75 ok ( strncmp ( "", "", 15 ) == 0 );
76 ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
77 ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
78 ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
79 ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
82 ok ( memcmp ( "", "", 0 ) == 0 );
83 ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
84 ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
88 static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
89 static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
90 memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
91 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
94 static uint8_t test[4] = { '>', 0, 0, '<' };
95 static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
96 memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
97 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
102 static uint8_t test[11] =
103 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
104 static const uint8_t expected[11] =
105 { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
106 memmove ( ( test + 1 ), ( test + 3 ), 6 );
107 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
110 static uint8_t test[12] =
111 { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
112 static const uint8_t expected[12] =
113 { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
114 memmove ( ( test + 6 ), ( test + 1 ), 5 );
115 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
120 static uint8_t test[8] =
121 { '>', 1, 2, 3, 7, 8, 9, '<' };
122 static const uint8_t expected[8] =
123 { '>', 7, 8, 9, 1, 2, 3, '<' };
124 memswap ( ( test + 1 ), ( test + 4 ), 3 );
125 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
130 const char *orig = "testing testing";
131 char *dup = strdup ( orig );
134 ok ( strcmp ( dup, orig ) == 0 );
140 const char *normal = "testing testing";
141 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
143 dup = strndup ( normal, 32 );
145 ok ( dup != normal );
146 ok ( strcmp ( dup, normal ) == 0 );
148 dup = strndup ( normal, 4 );
150 ok ( strcmp ( dup, "test" ) == 0 );
152 dup = strndup ( unterminated, 5 );
154 ok ( strcmp ( dup, "hello" ) == 0 );
159 /** String self-test */
160 struct self_test string_test __self_test = {
162 .exec = string_test_exec,