iPXE
byteswap_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 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  * Byte-order swapping test functions
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stdint.h>
00036 #include <assert.h>
00037 #include <byteswap.h>
00038 #include <ipxe/test.h>
00039 
00040 /* Provide global functions to allow inspection of generated assembly code */
00041 
00042 uint16_t test_bswap16 ( uint16_t x ) {
00043         return __bswap_16 ( x );
00044 }
00045 
00046 uint32_t test_bswap32 ( uint32_t x ) {
00047         return __bswap_32 ( x );
00048 }
00049 
00050 uint64_t test_bswap64 ( uint64_t x ) {
00051         return __bswap_64 ( x );
00052 }
00053 
00054 void test_bswap16s ( uint16_t *x ) {
00055         __bswap_16s ( x );
00056 }
00057 
00058 void test_bswap32s ( uint32_t *x ) {
00059         __bswap_32s ( x );
00060 }
00061 
00062 void test_bswap64s ( uint64_t *x ) {
00063         __bswap_64s ( x );
00064 }
00065 
00066 /**
00067  * Perform byte-order swapping
00068  *
00069  */
00070 static void byteswap_test_exec ( void ) {
00071         uint16_t test16;
00072         uint32_t test32;
00073         uint64_t test64;
00074 
00075         ok ( test_bswap16 ( 0x1234 ) == 0x3412 );
00076         ok ( test_bswap32 ( 0x12345678UL ) == 0x78563412UL );
00077         ok ( test_bswap64 ( 0x123456789abcdef0ULL ) == 0xf0debc9a78563412ULL );
00078 
00079         test16 = 0xabcd;
00080         test_bswap16s ( &test16 );
00081         ok ( test16 == 0xcdab );
00082 
00083         test32 = 0xabcdef01UL;
00084         test_bswap32s ( &test32 );
00085         ok ( test32 == 0x01efcdabUL );
00086 
00087         test64 = 0xabcdef0123456789ULL;
00088         test_bswap64s ( &test64 );
00089         ok ( test64 == 0x8967452301efcdabULL );
00090 }
00091 
00092 /** Byte-order swapping self-test */
00093 struct self_test byteswap_test __self_test = {
00094         .name = "byteswap",
00095         .exec = byteswap_test_exec,
00096 };