diff --git a/test/conversion_test.cpp b/test/conversion_test.cpp index b184343..f9bb7bd 100644 --- a/test/conversion_test.cpp +++ b/test/conversion_test.cpp @@ -7,13 +7,24 @@ //--------------------------------------------------------------------------------------// +#if defined(_MSC_VER) +# pragma warning( disable: 4127 ) // conditional expression is constant +# if _MSC_VER < 1500 +# pragma warning( disable: 4267 ) // '+=': possible loss of data +# endif +#endif + #include #include #include +#include +#include +#include #include #include #include +#include namespace be = boost::endian; using std::cout; @@ -37,63 +48,43 @@ template inline T std_endian_reverse(T x) BOOST_NOEXCEPT namespace { - // values for tests + // values for tests - void native_value(int8_t& x) {x = static_cast(0xF0U);} - void native_value(uint8_t& x) {x = static_cast(0xF0U);} -# if BOOST_ENDIAN_BIG_BYTE - void big_value(int8_t& x) {x = static_cast(0xF0U);} - void big_value(uint8_t& x) {x = static_cast(0xF0U);} - void little_value(int8_t& x) {x = static_cast(0xF0U);} - void little_value(uint8_t& x) {x = static_cast(0xF0U);} -# else - void big_value(int8_t& x) {x = static_cast(0xF0U);} - void big_value(uint8_t& x) {x = static_cast(0xF0U);} - void little_value(int8_t& x) {x = static_cast(0xF0U);} - void little_value(uint8_t& x) {x = static_cast(0xF0U);} -# endif + static unsigned char const test_value_bytes[] = { 0xF1, 0x02, 0xE3, 0x04, 0xD5, 0x06, 0xC7, 0x08 }; - void native_value(int16_t& x) {x = static_cast(0xF102U);} - void native_value(uint16_t& x) {x = static_cast(0xF102U);} -# if BOOST_ENDIAN_BIG_BYTE - void big_value(int16_t& x) {x = static_cast(0xF102U);} - void big_value(uint16_t& x) {x = static_cast(0xF102U);} - void little_value(int16_t& x) {x = static_cast(0x02F1U);} - void little_value(uint16_t& x) {x = static_cast(0x02F1U);} -# else - void big_value(int16_t& x) {x = static_cast(0x02F1U);} - void big_value(uint16_t& x) {x = static_cast(0x02F1U);} - void little_value(int16_t& x) {x = static_cast(0xF102U);} - void little_value(uint16_t& x) {x = static_cast(0xF102U);} -# endif + template void native_value( T& x ) + { + BOOST_STATIC_ASSERT( boost::is_integral::value && sizeof( T ) <= 8 ); + std::memcpy( &x, test_value_bytes, sizeof( x ) ); + } - void native_value(int32_t& x) {x = static_cast(0xF1E21304UL);} - void native_value(uint32_t& x) {x = static_cast(0xF1E21304UL);} -# if BOOST_ENDIAN_BIG_BYTE - void big_value(int32_t& x) {x = static_cast(0xF1E21304UL);} - void big_value(uint32_t& x) {x = static_cast(0xF1E21304UL);} - void little_value(int32_t& x) {x = static_cast(0x0413E2F1UL);} - void little_value(uint32_t& x) {x = static_cast(0x0413E2F1UL);} -# else - void big_value(int32_t& x) {x = static_cast(0x0413E2F1UL);} - void big_value(uint32_t& x) {x = static_cast(0x0413E2F1UL);} - void little_value(int32_t& x) {x = static_cast(0xF1E21304UL);} - void little_value(uint32_t& x) {x = static_cast(0xF1E21304UL);} -# endif + template void little_value( T& x ) + { + BOOST_STATIC_ASSERT( boost::is_integral::value && sizeof( T ) <= 8 ); - void native_value(int64_t& x) {x = static_cast(0xF1E2D3C444231201ULL);} - void native_value(uint64_t& x) {x = static_cast(0xF1E2D3C444231201ULL);} -# if BOOST_ENDIAN_BIG_BYTE - void big_value(int64_t& x) {x = static_cast(0xF1E2D3C444231201ULL);} - void big_value(uint64_t& x) {x = static_cast(0xF1E2D3C444231201ULL);} - void little_value(int64_t& x) {x = static_cast(0x01122344C4D3E2F1ULL);} - void little_value(uint64_t& x) {x = static_cast(0x01122344C4D3E2F1ULL);} -# else - void big_value(int64_t& x) {x = static_cast(0x01122344C4D3E2F1ULL);} - void big_value(uint64_t& x) {x = static_cast(0x01122344C4D3E2F1ULL);} - void little_value(int64_t& x) {x = static_cast(0xF1E2D3C444231201ULL);} - void little_value(uint64_t& x) {x = static_cast(0xF1E2D3C444231201ULL);} -# endif + typedef typename boost::make_unsigned::type U; + + x = 0; + + for( std::size_t i = 0; i < sizeof( x ); ++i ) + { + x += static_cast( test_value_bytes[ i ] ) << ( 8 * i ); + } + } + + template void big_value( T& x ) + { + BOOST_STATIC_ASSERT( boost::is_integral::value && sizeof( T ) <= 8 ); + + typedef typename boost::make_unsigned::type U; + + x = 0; + + for( std::size_t i = 0; i < sizeof( x ); ++i ) + { + x += static_cast( test_value_bytes[ i ] ) << ( 8 * ( sizeof( x ) - i - 1 ) ); + } + } template void test() @@ -107,13 +98,16 @@ namespace // validate the values used by the tests below -# if BOOST_ENDIAN_BIG_BYTE - BOOST_TEST_EQ(native, big); - BOOST_TEST_EQ(::std_endian_reverse(native), little); -# else - BOOST_TEST_EQ(::std_endian_reverse(native), big); - BOOST_TEST_EQ(native, little); -# endif + if( be::order::native == be::order::big ) + { + BOOST_TEST_EQ(native, big); + BOOST_TEST_EQ(::std_endian_reverse(native), little); + } + else + { + BOOST_TEST_EQ(::std_endian_reverse(native), big); + BOOST_TEST_EQ(native, little); + } // value-by-value tests @@ -337,6 +331,19 @@ namespace int cpp_main(int, char * []) { + if( be::order::native == be::order::little ) + { + cout << "Little endian" << endl; + } + else if( be::order::native == be::order::big ) + { + cout << "Big endian" << endl; + } + else + { + cout << "Unknown endian" << endl; + } + cout << "byte swap intrinsics: " BOOST_ENDIAN_INTRINSIC_MSG << endl; //std::cerr << std::hex;