iPXE
memset_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 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  * memset() self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <string.h>
00036 #include <ipxe/test.h>
00037 
00038 /* Provide global functions to allow inspection of generated code */
00039 
00040 void memset_zero_0 ( void *dest ) { memset ( dest, 0, 0 ); }
00041 void memset_zero_1 ( void *dest ) { memset ( dest, 0, 1 ); }
00042 void memset_zero_2 ( void *dest ) { memset ( dest, 0, 2 ); }
00043 void memset_zero_3 ( void *dest ) { memset ( dest, 0, 3 ); }
00044 void memset_zero_4 ( void *dest ) { memset ( dest, 0, 4 ); }
00045 void memset_zero_5 ( void *dest ) { memset ( dest, 0, 5 ); }
00046 void memset_zero_6 ( void *dest ) { memset ( dest, 0, 6 ); }
00047 void memset_zero_7 ( void *dest ) { memset ( dest, 0, 7 ); }
00048 void memset_zero_8 ( void *dest ) { memset ( dest, 0, 8 ); }
00049 void memset_zero_9 ( void *dest ) { memset ( dest, 0, 9 ); }
00050 void memset_zero_10 ( void *dest ) { memset ( dest, 0, 10 ); }
00051 void memset_zero_11 ( void *dest ) { memset ( dest, 0, 11 ); }
00052 void memset_zero_12 ( void *dest ) { memset ( dest, 0, 12 ); }
00053 void memset_zero_13 ( void *dest ) { memset ( dest, 0, 13 ); }
00054 void memset_zero_14 ( void *dest ) { memset ( dest, 0, 14 ); }
00055 void memset_zero_15 ( void *dest ) { memset ( dest, 0, 15 ); }
00056 void memset_zero_16 ( void *dest ) { memset ( dest, 0, 16 ); }
00057 void memset_zero_17 ( void *dest ) { memset ( dest, 0, 17 ); }
00058 void memset_zero_18 ( void *dest ) { memset ( dest, 0, 18 ); }
00059 void memset_zero_19 ( void *dest ) { memset ( dest, 0, 19 ); }
00060 void memset_zero_20 ( void *dest ) { memset ( dest, 0, 20 ); }
00061 void memset_zero_21 ( void *dest ) { memset ( dest, 0, 21 ); }
00062 void memset_zero_22 ( void *dest ) { memset ( dest, 0, 22 ); }
00063 void memset_zero_23 ( void *dest ) { memset ( dest, 0, 23 ); }
00064 void memset_zero_24 ( void *dest ) { memset ( dest, 0, 24 ); }
00065 void memset_zero_25 ( void *dest ) { memset ( dest, 0, 25 ); }
00066 void memset_zero_26 ( void *dest ) { memset ( dest, 0, 26 ); }
00067 void memset_zero_27 ( void *dest ) { memset ( dest, 0, 27 ); }
00068 void memset_zero_28 ( void *dest ) { memset ( dest, 0, 28 ); }
00069 void memset_zero_29 ( void *dest ) { memset ( dest, 0, 29 ); }
00070 void memset_zero_30 ( void *dest ) { memset ( dest, 0, 30 ); }
00071 void memset_zero_31 ( void *dest ) { memset ( dest, 0, 31 ); }
00072 
00073 /**
00074  * Force a call to the variable-length implementation of memset()
00075  *
00076  * @v dest              Destination address
00077  * @v fill              Fill pattern
00078  * @v len               Length of data
00079  * @ret dest            Destination address
00080  */
00081 __attribute__ (( noinline )) void * memset_var ( void *dest, unsigned int fill,
00082                                                  size_t len ) {
00083         return memset ( dest, fill, len );
00084 }
00085 
00086 /**
00087  * Perform a constant-length memset() test
00088  *
00089  * @v len               Length of data
00090  */
00091 #define MEMSET_TEST_CONSTANT( len ) do {                                \
00092                 uint8_t dest_const[ 1 + len + 1 ];                      \
00093                 uint8_t dest_var[ 1 + len + 1 ];                        \
00094                 static uint8_t zero[len];                               \
00095                 unsigned int i;                                         \
00096                                                                         \
00097                 for ( i = 0 ; i < sizeof ( dest_const ) ; i++ )         \
00098                         dest_const[i] = 0xaa;                           \
00099                 memset ( ( dest_const + 1 ), 0, len );                  \
00100                 ok ( dest_const[0] == 0xaa );                           \
00101                 ok ( dest_const[ sizeof ( dest_const ) - 1 ] == 0xaa ); \
00102                 ok ( memcmp ( ( dest_const + 1 ), zero, len ) == 0 );   \
00103                                                                         \
00104                 for ( i = 0 ; i < sizeof ( dest_var ) ; i++ )           \
00105                         dest_var[i] = 0xbb;                             \
00106                 memset_var ( ( dest_var + 1 ), 0, len );                \
00107                 ok ( dest_var[0] == 0xbb );                             \
00108                 ok ( dest_var[ sizeof ( dest_var ) - 1 ] == 0xbb );     \
00109                 ok ( memcmp ( ( dest_var + 1 ), zero, len ) == 0 );     \
00110         } while ( 0 )
00111 
00112 /**
00113  * Perform memset() self-tests
00114  *
00115  */
00116 static void memset_test_exec ( void ) {
00117 
00118         /* Constant-length tests */
00119         MEMSET_TEST_CONSTANT ( 0 );
00120         MEMSET_TEST_CONSTANT ( 1 );
00121         MEMSET_TEST_CONSTANT ( 2 );
00122         MEMSET_TEST_CONSTANT ( 3 );
00123         MEMSET_TEST_CONSTANT ( 4 );
00124         MEMSET_TEST_CONSTANT ( 5 );
00125         MEMSET_TEST_CONSTANT ( 6 );
00126         MEMSET_TEST_CONSTANT ( 7 );
00127         MEMSET_TEST_CONSTANT ( 8 );
00128         MEMSET_TEST_CONSTANT ( 9 );
00129         MEMSET_TEST_CONSTANT ( 10 );
00130         MEMSET_TEST_CONSTANT ( 11 );
00131         MEMSET_TEST_CONSTANT ( 12 );
00132         MEMSET_TEST_CONSTANT ( 13 );
00133         MEMSET_TEST_CONSTANT ( 14 );
00134         MEMSET_TEST_CONSTANT ( 15 );
00135         MEMSET_TEST_CONSTANT ( 16 );
00136         MEMSET_TEST_CONSTANT ( 17 );
00137         MEMSET_TEST_CONSTANT ( 18 );
00138         MEMSET_TEST_CONSTANT ( 19 );
00139         MEMSET_TEST_CONSTANT ( 20 );
00140         MEMSET_TEST_CONSTANT ( 21 );
00141         MEMSET_TEST_CONSTANT ( 22 );
00142         MEMSET_TEST_CONSTANT ( 23 );
00143         MEMSET_TEST_CONSTANT ( 24 );
00144         MEMSET_TEST_CONSTANT ( 25 );
00145         MEMSET_TEST_CONSTANT ( 26 );
00146         MEMSET_TEST_CONSTANT ( 27 );
00147         MEMSET_TEST_CONSTANT ( 28 );
00148         MEMSET_TEST_CONSTANT ( 29 );
00149         MEMSET_TEST_CONSTANT ( 30 );
00150         MEMSET_TEST_CONSTANT ( 31 );
00151 }
00152 
00153 /** memset() self-test */
00154 struct self_test memset_test __self_test = {
00155         .name = "memset",
00156         .exec = memset_test_exec,
00157 };