iPXE
bitops_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2016 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  * Bit operations self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stdint.h>
00036 #include <string.h>
00037 #include <assert.h>
00038 #include <ipxe/bitops.h>
00039 #include <ipxe/test.h>
00040 
00041 /**
00042  * Perform bit operations self-tests
00043  *
00044  */
00045 static void bitops_test_exec ( void ) {
00046         uint8_t bits[32];
00047 
00048         /* Initialise bits */
00049         memset ( bits, 0, sizeof ( bits ) );
00050 
00051         /* Test set_bit() */
00052         set_bit ( 0, bits );
00053         ok ( bits[0] == 0x01 );
00054         set_bit ( 17, bits );
00055         ok ( bits[2] == 0x02 );
00056         set_bit ( 22, bits );
00057         ok ( bits[2] == 0x42 );
00058         set_bit ( 22, bits );
00059         ok ( bits[2] == 0x42 );
00060 
00061         /* Test clear_bit() */
00062         clear_bit ( 0, bits );
00063         ok ( bits[0] == 0x00 );
00064         bits[5] = 0xff;
00065         clear_bit ( 42, bits );
00066         ok ( bits[5] == 0xfb );
00067         clear_bit ( 42, bits );
00068         ok ( bits[5] == 0xfb );
00069         clear_bit ( 44, bits );
00070         ok ( bits[5] == 0xeb );
00071 
00072         /* Test test_and_set_bit() */
00073         ok ( test_and_set_bit ( 0, bits ) == 0 );
00074         ok ( bits[0] == 0x01 );
00075         ok ( test_and_set_bit ( 0, bits ) != 0 );
00076         ok ( bits[0] == 0x01 );
00077         ok ( test_and_set_bit ( 69, bits ) == 0 );
00078         ok ( bits[8] == 0x20 );
00079         ok ( test_and_set_bit ( 69, bits ) != 0 );
00080         ok ( bits[8] == 0x20 );
00081         ok ( test_and_set_bit ( 69, bits ) != 0 );
00082         ok ( bits[8] == 0x20 );
00083 
00084         /* Test test_and_clear_bit() */
00085         ok ( test_and_clear_bit ( 0, bits ) != 0 );
00086         ok ( bits[0] == 0x00 );
00087         ok ( test_and_clear_bit ( 0, bits ) == 0 );
00088         ok ( bits[0] == 0x00 );
00089         bits[31] = 0xeb;
00090         ok ( test_and_clear_bit ( 255, bits ) != 0 );
00091         ok ( bits[31] == 0x6b );
00092         ok ( test_and_clear_bit ( 255, bits ) == 0 );
00093         ok ( bits[31] == 0x6b );
00094         ok ( test_and_clear_bit ( 255, bits ) == 0 );
00095         ok ( bits[31] == 0x6b );
00096 }
00097 
00098 /** Bit operations self-test */
00099 struct self_test bitops_test __self_test = {
00100         .name = "bitops",
00101         .exec = bitops_test_exec,
00102 };