iPXE
string_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * String self-tests
00029  *
00030  * memcpy() tests are handled separately
00031  */
00032 
00033 /* Forcibly enable assertions */
00034 #undef NDEBUG
00035 
00036 #include <stdint.h>
00037 #include <stdlib.h>
00038 #include <stdio.h>
00039 #include <string.h>
00040 #include <strings.h>
00041 #include <ipxe/string.h>
00042 #include <ipxe/test.h>
00043 
00044 /**
00045  * Perform string self-tests
00046  *
00047  */
00048 static void string_test_exec ( void ) {
00049 
00050         /* Test strlen() */
00051         ok ( strlen ( "" ) == 0 );
00052         ok ( strlen ( "Hello" ) == 5 );
00053         ok ( strlen ( "Hello world!" ) == 12 );
00054         ok ( strlen ( "Hello\0world!" ) == 5 );
00055 
00056         /* Test strnlen() */
00057         ok ( strnlen ( "", 0 ) == 0 );
00058         ok ( strnlen ( "", 10 ) == 0 );
00059         ok ( strnlen ( "Hello", 0 ) == 0 );
00060         ok ( strnlen ( "Hello", 3 ) == 3 );
00061         ok ( strnlen ( "Hello", 5 ) == 5 );
00062         ok ( strnlen ( "Hello", 16 ) == 5 );
00063         ok ( strnlen ( "Hello world!", 5 ) == 5 );
00064         ok ( strnlen ( "Hello world!", 11 ) == 11 );
00065         ok ( strnlen ( "Hello world!", 16 ) == 12 );
00066 
00067         /* Test strchr() */
00068         ok ( strchr ( "", 'a' ) == NULL );
00069         ok ( *(strchr ( "Testing", 'e' )) == 'e' );
00070         ok ( *(strchr ( "Testing", 'g' )) == 'g' );
00071         ok ( strchr ( "Testing", 'x' ) == NULL );
00072 
00073         /* Test strrchr() */
00074         ok ( strrchr ( "", 'a' ) == NULL );
00075         ok ( *(strrchr ( "Haystack", 'a' )) == 'a' );
00076         ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
00077         ok ( strrchr ( "Haystack", 'x' ) == NULL );
00078 
00079         /* Test memchr() */
00080         ok ( memchr ( "", '\0', 0 ) == NULL );
00081         ok ( *((uint8_t *)memchr ( "post\0null", 'l', 9 )) == 'l' );
00082         ok ( *((uint8_t *)memchr ( "post\0null", '\0', 9 )) == '\0' );
00083         ok ( memchr ( "thingy", 'z', 6 ) == NULL );
00084 
00085         /* Test strcmp() */
00086         ok ( strcmp ( "", "" ) == 0 );
00087         ok ( strcmp ( "Hello", "Hello" ) == 0 );
00088         ok ( strcmp ( "Hello", "hello" ) != 0 );
00089         ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
00090         ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
00091 
00092         /* Test strncmp() */
00093         ok ( strncmp ( "", "", 0 ) == 0 );
00094         ok ( strncmp ( "", "", 15 ) == 0 );
00095         ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
00096         ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
00097         ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
00098         ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
00099 
00100         /* Test strcasecmp() */
00101         ok ( strcasecmp ( "", "" ) == 0 );
00102         ok ( strcasecmp ( "Uncle Jack", "Uncle jack" ) == 0 );
00103         ok ( strcasecmp ( "Uncle Jack", "Uncle" ) != 0 );
00104         ok ( strcasecmp ( "Uncle", "Uncle Jack" ) != 0 );
00105         ok ( strcasecmp ( "not", "equal" ) != 0 );
00106 
00107         /* Test memcmp() */
00108         ok ( memcmp ( "", "", 0 ) == 0 );
00109         ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
00110         ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
00111 
00112         /* Test strstr() */
00113         {
00114                 const char haystack[] = "find me!";
00115                 char *found;
00116 
00117                 found = strstr ( haystack, "find" );
00118                 ok ( found == &haystack[0] );
00119                 found = strstr ( haystack, "me" );
00120                 ok ( found == &haystack[5] );
00121                 found = strstr ( haystack, "me." );
00122                 ok ( found == NULL );
00123         }
00124 
00125         /* Test memset() */
00126         {
00127                 static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
00128                 static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
00129                 memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
00130                 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
00131         }
00132         {
00133                 static uint8_t test[4] = { '>', 0, 0, '<' };
00134                 static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
00135                 memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
00136                 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
00137         }
00138 
00139         /* Test memmove() */
00140         {
00141                 static uint8_t test[11] =
00142                         { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
00143                 static const uint8_t expected[11] =
00144                         { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
00145                 memmove ( ( test + 1 ), ( test + 3 ), 6 );
00146                 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
00147         }
00148         {
00149                 static uint8_t test[12] =
00150                         { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
00151                 static const uint8_t expected[12] =
00152                         { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
00153                 memmove ( ( test + 6 ), ( test + 1 ), 5 );
00154                 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
00155         }
00156 
00157         /* Test memswap() */
00158         {
00159                 static uint8_t test[8] =
00160                         { '>', 1, 2, 3, 7, 8, 9, '<' };
00161                 static const uint8_t expected[8] =
00162                         { '>', 7, 8, 9, 1, 2, 3, '<' };
00163                 memswap ( ( test + 1 ), ( test + 4 ), 3 );
00164                 ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
00165         }
00166 
00167         /* Test strdup() */
00168         {
00169                 const char *orig = "testing testing";
00170                 char *dup = strdup ( orig );
00171                 ok ( dup != NULL );
00172                 ok ( dup != orig );
00173                 ok ( strcmp ( dup, orig ) == 0 );
00174                 free ( dup );
00175         }
00176 
00177         /* Test strndup() */
00178         {
00179                 const char *normal = "testing testing";
00180                 const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
00181                 char *dup;
00182                 dup = strndup ( normal, 32 );
00183                 ok ( dup != NULL );
00184                 ok ( dup != normal );
00185                 ok ( strcmp ( dup, normal ) == 0 );
00186                 free ( dup );
00187                 dup = strndup ( normal, 4 );
00188                 ok ( dup != NULL );
00189                 ok ( strcmp ( dup, "test" ) == 0 );
00190                 free ( dup );
00191                 dup = strndup ( unterminated, 5 );
00192                 ok ( dup != NULL );
00193                 ok ( strcmp ( dup, "hello" ) == 0 );
00194                 free ( dup );
00195         }
00196 
00197         /* Test strcpy() */
00198         {
00199                 const char longer[7] = "copyme";
00200                 const char shorter[3] = "hi";
00201                 char dest[7];
00202                 char *copy;
00203 
00204                 copy = strcpy ( dest, longer );
00205                 ok ( copy == dest );
00206                 ok ( memcmp ( dest, longer, 7 ) == 0 );
00207                 copy = strcpy ( dest, shorter );
00208                 ok ( copy == dest );
00209                 ok ( memcmp ( dest, shorter, 3 ) == 0 );
00210                 ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
00211         }
00212 
00213         /* Test strncpy() */
00214         {
00215                 const char src[5] = "copy";
00216                 const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
00217                 const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
00218                 char dest[8];
00219                 char *copy;
00220 
00221                 memcpy ( dest, orig, sizeof ( dest ) );
00222                 copy = strncpy ( dest, src, 5 );
00223                 ok ( copy == dest );
00224                 ok ( memcmp ( dest, src, 5 ) == 0 );
00225                 ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
00226                 memcpy ( dest, orig, sizeof ( dest ) );
00227                 copy = strncpy ( dest, src, 4 );
00228                 ok ( copy == dest );
00229                 ok ( memcmp ( dest, src, 4 ) == 0 );
00230                 ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
00231                 memcpy ( dest, orig, sizeof ( dest ) );
00232                 copy = strncpy ( dest, src, 8 );
00233                 ok ( copy == dest );
00234                 ok ( memcmp ( dest, src, 5 ) == 0 );
00235                 ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
00236                 memcpy ( dest, orig, sizeof ( dest ) );
00237                 copy = strncpy ( dest, "", 8 );
00238                 ok ( copy == dest );
00239                 ok ( memcmp ( dest, zero, 8 ) == 0 );
00240         }
00241 
00242         /* Test strcat() */
00243         {
00244                 char buf[16] = "append";
00245                 char *dest;
00246 
00247                 dest = strcat ( buf, " this" );
00248                 ok ( dest == buf );
00249                 ok ( strcmp ( buf, "append this" ) == 0 );
00250         }
00251 
00252         /* Test digit_value() */
00253         {
00254                 unsigned int i;
00255                 char buf[2];
00256                 for ( i = 0 ; i < 16 ; i++ ) {
00257                         snprintf ( buf, sizeof ( buf ), "%x", i );
00258                         ok ( digit_value ( buf[0] ) == i );
00259                         snprintf ( buf, sizeof ( buf ), "%X", i );
00260                         ok ( digit_value ( buf[0] ) == i );
00261                 }
00262                 ok ( digit_value ( 0 ) >= 16 );
00263                 ok ( digit_value ( 9 ) >= 16 );
00264                 ok ( digit_value ( '0' - 1 ) >= 16 );
00265                 ok ( digit_value ( '9' + 1 ) >= 16 );
00266                 ok ( digit_value ( 'A' - 1 ) >= 16 );
00267                 ok ( digit_value ( 'F' + 1 ) >= 16 );
00268                 ok ( digit_value ( 'a' - 1 ) >= 16 );
00269                 ok ( digit_value ( 'f' + 1 ) >= 16 );
00270         }
00271 
00272         /* Test strtoul() */
00273         ok ( strtoul ( "12345", NULL, 0 ) == 12345UL );
00274         ok ( strtoul ( "  741", NULL, 10 ) == 741UL );
00275         ok ( strtoul ( " 555a", NULL, 0 ) == 555UL );
00276         ok ( strtoul ( " 555a", NULL, 16 ) == 0x555aUL );
00277         ok ( strtoul ( "-12", NULL, 0 ) == -12UL );
00278         ok ( strtoul ( "+3", NULL, 0 ) == 3UL );
00279         ok ( strtoul ( "721", NULL, 0 ) == 721UL );
00280         ok ( strtoul ( "721", NULL, 8 ) == 0721UL );
00281         ok ( strtoul ( "0721", NULL, 0 ) == 0721UL );
00282         ok ( strtoul ( "", NULL, 0 ) == 0UL );
00283         ok ( strtoul ( "\t0xcAfe", NULL, 0 ) == 0xcafeUL );
00284         ok ( strtoul ( "0xffffffff", NULL, 0 ) == 0xffffffffUL );
00285         {
00286                 static const char string[] = "123aHa.world";
00287                 char *endp;
00288                 ok ( strtoul ( string, &endp, 0 ) == 123UL );
00289                 ok ( endp == &string[3] );
00290                 ok ( strtoul ( string, &endp, 16 ) == 0x123aUL );
00291                 ok ( endp == &string[4] );
00292                 ok ( strtoul ( string, &endp, 26 ) ==
00293                      ( ( ( ( ( 1 * 26 + 2 ) * 26 + 3 ) * 26 + 10 ) * 26
00294                          + 17 ) * 26 + 10 ) );
00295                 ok ( endp == &string[6] );
00296         }
00297 }
00298 
00299 /** String self-test */
00300 struct self_test string_test __self_test = {
00301         .name = "string",
00302         .exec = string_test_exec,
00303 };