Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / tests / string_test.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * String self-tests
25  *
26  * memcpy() tests are handled separately
27  */
28
29 /* Forcibly enable assertions */
30 #undef NDEBUG
31
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ipxe/test.h>
36
37 /**
38  * Perform string self-tests
39  *
40  */
41 static void string_test_exec ( void ) {
42
43         /* Test strlen() */
44         ok ( strlen ( "" ) == 0 );
45         ok ( strlen ( "Hello" ) == 5 );
46         ok ( strlen ( "Hello world!" ) == 12 );
47         ok ( strlen ( "Hello\0world!" ) == 5 );
48
49         /* Test strnlen() */
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 );
59
60         /* Test strchr() */
61         ok ( strchr ( "", 'a' ) == NULL );
62         ok ( *(strchr ( "Testing", 'e' )) == 'e' );
63         ok ( *(strchr ( "Testing", 'g' )) == 'g' );
64         ok ( strchr ( "Testing", 'x' ) == NULL );
65
66         /* Test strcmp() */
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 );
72
73         /* Test strncmp() */
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 );
80
81         /* Test memcmp() */
82         ok ( memcmp ( "", "", 0 ) == 0 );
83         ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
84         ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
85
86         /* Test memset() */
87         {
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 );
92         }
93         {
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 );
98         }
99
100         /* Test memmove() */
101         {
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 );
108         }
109         {
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 );
116         }
117
118         /* Test memswap() */
119         {
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 );
126         }
127
128         /* Test strdup() */
129         {
130                 const char *orig = "testing testing";
131                 char *dup = strdup ( orig );
132                 ok ( dup != NULL );
133                 ok ( dup != orig );
134                 ok ( strcmp ( dup, orig ) == 0 );
135                 free ( dup );
136         }
137
138         /* Test strndup() */
139         {
140                 const char *normal = "testing testing";
141                 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
142                 char *dup;
143                 dup = strndup ( normal, 32 );
144                 ok ( dup != NULL );
145                 ok ( dup != normal );
146                 ok ( strcmp ( dup, normal ) == 0 );
147                 free ( dup );
148                 dup = strndup ( normal, 4 );
149                 ok ( dup != NULL );
150                 ok ( strcmp ( dup, "test" ) == 0 );
151                 free ( dup );
152                 dup = strndup ( unterminated, 5 );
153                 ok ( dup != NULL );
154                 ok ( strcmp ( dup, "hello" ) == 0 );
155                 free ( dup );
156         }
157 }
158
159 /** String self-test */
160 struct self_test string_test __self_test = {
161         .name = "string",
162         .exec = string_test_exec,
163 };