From 4197f996ac9ea7650a433bf45480264f71fd5eeb Mon Sep 17 00:00:00 2001 From: Beman Date: Sun, 23 Nov 2014 10:38:21 -0500 Subject: [PATCH] More conversion.hpp fixes. Some, but not all, conversion_test tests passing. --- include/boost/endian/conversion.hpp | 67 +++--- ...converter_test.cpp => conversion_test.cpp} | 196 ++++++++---------- .../converter_test/converter_test.vcxproj | 6 +- 3 files changed, 137 insertions(+), 132 deletions(-) rename test/{converter_test.cpp => conversion_test.cpp} (51%) diff --git a/include/boost/endian/conversion.hpp b/include/boost/endian/conversion.hpp index dac594e..3b18fc6 100644 --- a/include/boost/endian/conversion.hpp +++ b/include/boost/endian/conversion.hpp @@ -262,7 +262,7 @@ namespace endian inline T generic_reverse_endianness(T x) BOOST_NOEXCEPT { T tmp(x); - std::reverse_endianness( + std::reverse( reinterpret_cast(&tmp), reinterpret_cast(&tmp) + sizeof(T)); return tmp; @@ -325,8 +325,8 @@ namespace endian namespace detail { - // Primary template and specializations to support convert_value(). See rationale in - // convert_value() below. + // Primary template and specializations to support reverse_endianness(). + // See rationale in reverse_endianness() below. template class value_converter ; // primary template template class value_converter @@ -334,14 +334,14 @@ namespace endian template class value_converter {public: T operator()(T x) BOOST_NOEXCEPT {return x;}}; template class value_converter - {public: T operator()(T x) BOOST_NOEXCEPT {return reverse_value(x);}}; + {public: T operator()(T x) BOOST_NOEXCEPT {return reverse_endianness(x);}}; template class value_converter - {public: T operator()(T x) BOOST_NOEXCEPT {return reverse_value(x);}}; + {public: T operator()(T x) BOOST_NOEXCEPT {return reverse_endianness(x);}}; } // compile-time generic convert return by value template - Reversible convert_value(Reversible x) BOOST_NOEXCEPT + Reversible reverse_endianness(Reversible x) BOOST_NOEXCEPT { // work around lack of function template partial specialization by instantiating // a function object of a class that is partially specialized on the two order @@ -363,7 +363,7 @@ namespace endian } template - Reversible convert_value(Reversible from, + Reversible reverse_endianness(Reversible from, BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT { return effective_order(from_order) == effective_order(to_order) @@ -375,50 +375,67 @@ namespace endian //--------------------------------------------------------------------------------------// // reverse byte-order in place (i.e. flip endianness) - inline void reverse_endianness_in_place(int8_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(int16_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(int32_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(int64_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(uint8_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(uint16_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(uint32_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(uint64_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(float& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } - inline void reverse_endianness_in_place(double& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(int8_t& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(int16_t& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(int32_t& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(int64_t& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(uint8_t& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(uint16_t& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(uint32_t& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(uint64_t& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(float& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } + inline void reverse_endianness_in_place(double& x) BOOST_NOEXCEPT + { x = reverse_endianness(x); } // reverse in place unless native endianness is big - // Effects: none if native endian order is big, otherwise reverse_endianness_in_place(x) + // Effects: none if native endian order is big, + // otherwise reverse_endianness_in_place(x) template # ifdef BOOST_BIG_ENDIAN inline void big_to_native_in_place(Reversible&) BOOST_NOEXCEPT {} # else - inline void big_to_native_in_place(Reversible& x) BOOST_NOEXCEPT { reverse_endianness_in_place(x); } + inline void big_to_native_in_place(Reversible& x) BOOST_NOEXCEPT + { reverse_endianness_in_place(x); } # endif template # ifdef BOOST_BIG_ENDIAN inline void native_to_big_in_place(Reversible&) BOOST_NOEXCEPT {} # else - inline void native_to_big_in_place(Reversible& x) BOOST_NOEXCEPT { reverse_endianness_in_place(x); } + inline void native_to_big_in_place(Reversible& x) BOOST_NOEXCEPT + { reverse_endianness_in_place(x); } # endif - // reverse in placeunless native endianness is little - // Effects: none if native endian order is little, otherwise reverse_endianness_in_place(x) + // reverse in place unless native endianness is little + // Effects: none if native endian order is little, + // otherwise reverse_endianness_in_place(x) template # ifdef BOOST_LITTLE_ENDIAN inline void little_to_native_in_place(Reversible&) BOOST_NOEXCEPT {} # else - inline void little_to_native_in_place(Reversible& x) BOOST_NOEXCEPT { reverse_endianness_in_place(x); } + inline void little_to_native_in_place(Reversible& x) BOOST_NOEXCEPT + { reverse_endianness_in_place(x); } # endif template # ifdef BOOST_LITTLE_ENDIAN inline void native_to_little_in_place(Reversible&) BOOST_NOEXCEPT {} # else - inline void native_to_little_in_place(Reversible& x) BOOST_NOEXCEPT { reverse_endianness_in_place(x); } + inline void native_to_little_in_place(Reversible& x) BOOST_NOEXCEPT + { reverse_endianness_in_place(x); } # endif namespace detail { - // Primary template and specializations to support generic reverse_endianness_in_place(). + // Primary template and specializations support generic + // reverse_endianness_in_place(). // See rationale in reverse_endianness_in_place() below. template class converter; // primary template diff --git a/test/converter_test.cpp b/test/conversion_test.cpp similarity index 51% rename from test/converter_test.cpp rename to test/conversion_test.cpp index f265285..82bb193 100644 --- a/test/converter_test.cpp +++ b/test/conversion_test.cpp @@ -1,4 +1,4 @@ -// converter_test.cpp ----------------------------------------------------------------// +// conversion_test.cpp ---------------------------------------------------------------// // Copyright Beman Dawes 2010 @@ -134,122 +134,110 @@ namespace big_value(big); little_value(little); - BOOST_TEST_EQ(be::reverse_value(big), little); - BOOST_TEST_EQ(be::reverse_value(little), big); - BOOST_TEST_EQ(be::detail::reverse_value(big), little); - BOOST_TEST_EQ(be::detail::reverse_value(little), big); + // one-way tests - BOOST_TEST_EQ(be::big_endian_value(native), big); - BOOST_TEST_EQ(be::big_endian_value(native), big); - BOOST_TEST_EQ(be::little_endian_value(native), little); - BOOST_TEST_EQ(be::little_endian_value(native), little); - BOOST_TEST_EQ(be::big_endian_value(be::big_endian_value(native)), native); - BOOST_TEST_EQ(be::big_endian_value(be::big_endian_value(big)), big); - BOOST_TEST_EQ(be::big_endian_value(be::big_endian_value(little)), little); - BOOST_TEST_EQ(be::little_endian_value(be::little_endian_value(native)), native); - BOOST_TEST_EQ(be::little_endian_value(be::little_endian_value(big)), big); - BOOST_TEST_EQ(be::little_endian_value(be::little_endian_value(little)), little); + BOOST_TEST_EQ(be::reverse_endianness(big), little); + BOOST_TEST_EQ(be::reverse_endianness(little), big); +// BOOST_TEST_EQ(be::reverse(big), little); +// BOOST_TEST_EQ(be::reverse(little), big); + + BOOST_TEST_EQ(be::native_to_big(native), big); +// BOOST_TEST_EQ(be::native_to_big(native), big); + BOOST_TEST_EQ(be::native_to_little(native), little); + // BOOST_TEST_EQ(be::native_to_little(native), little); + + // round-trip tests + + BOOST_TEST_EQ(be::big_to_native(be::native_to_big(native)), native); + BOOST_TEST_EQ(be::native_to_big(be::big_to_native(big)), big); + BOOST_TEST_EQ(be::big_to_native(be::native_to_big(little)), little); + + BOOST_TEST_EQ(be::little_to_native(be::native_to_little(native)), native); + BOOST_TEST_EQ(be::little_to_native(be::native_to_little(big)), big); + BOOST_TEST_EQ(be::little_to_native(be::native_to_little(little)), little); # ifdef BOOST_BIG_ENDIAN - BOOST_TEST_EQ(be::reverse_value(native), little); - BOOST_TEST_EQ(be::detail::reverse_value(native), little); - BOOST_TEST_EQ(be::big_endian_value(big), big); - BOOST_TEST_EQ(be::big_endian_value(big), big); - BOOST_TEST_EQ(be::big_endian_value(little), little); - BOOST_TEST_EQ(be::big_endian_value(little), little); + BOOST_TEST_EQ(be::reverse_endianness(native), little); +// BOOST_TEST_EQ(be::detail::reverse(native), little); + BOOST_TEST_EQ(be::big_to_native(big), big); +// BOOST_TEST_EQ(be::big_endian(big), big); + BOOST_TEST_EQ(be::little_to_native(little), little); +// BOOST_TEST_EQ(be::big_endian(little), little); # else - BOOST_TEST_EQ(be::reverse_value(native), big); - BOOST_TEST_EQ(be::detail::reverse_value(native), big); - BOOST_TEST_EQ(be::big_endian_value(big), little); - BOOST_TEST_EQ(be::big_endian_value(big), little); - BOOST_TEST_EQ(be::big_endian_value(little), big); - BOOST_TEST_EQ(be::big_endian_value(little), big); + BOOST_TEST_EQ(be::reverse_endianness(native), big); +// BOOST_TEST_EQ(be::detail::reverse(native), big); + BOOST_TEST_EQ(be::big_to_native(big), little); +// BOOST_TEST_EQ(be::big_endian(big), little); + BOOST_TEST_EQ(be::native_to_big(little), big); +// BOOST_TEST_EQ(be::big_endian(little), big); # endif - // compile time order determination + // compile time order determination test - BOOST_TEST_EQ((be::convert_value(big)), big); - BOOST_TEST_EQ((be::convert_value(little)), little); - BOOST_TEST_EQ((be::convert_value(native)), native); + BOOST_TEST_EQ((be::reverse_endianness(big)), big); + BOOST_TEST_EQ((be::reverse_endianness(little)), little); + BOOST_TEST_EQ((be::reverse_endianness(native)), native); - BOOST_TEST_EQ((be::convert_value(big)), little); - BOOST_TEST_EQ((be::convert_value(big)), native); - BOOST_TEST_EQ((be::convert_value(little)), big); - BOOST_TEST_EQ((be::convert_value(little)), native); - BOOST_TEST_EQ((be::convert_value(native)), big); - BOOST_TEST_EQ((be::convert_value(native)), little); + BOOST_TEST_EQ((be::reverse_endianness(big)), little); + BOOST_TEST_EQ((be::reverse_endianness(big)), native); + BOOST_TEST_EQ((be::reverse_endianness(little)), big); + BOOST_TEST_EQ((be::reverse_endianness(little)), native); + BOOST_TEST_EQ((be::reverse_endianness(native)), big); + BOOST_TEST_EQ((be::reverse_endianness(native)), little); - // runtime order determination + // runtime order determination test - BOOST_TEST_EQ((be::convert_value(big, be::order::big, be::order::big)), big); - BOOST_TEST_EQ((be::convert_value(little, be::order::little, be::order::little)), little); - BOOST_TEST_EQ((be::convert_value(native, be::order::native, be::order::native)), native); + BOOST_TEST_EQ((be::reverse_endianness(big, be::order::big, be::order::big)), big); + BOOST_TEST_EQ((be::reverse_endianness(little, be::order::little, + be::order::little)), little); + BOOST_TEST_EQ((be::reverse_endianness(native, be::order::native, + be::order::native)), native); - BOOST_TEST_EQ((be::convert_value(big, be::order::big, be::order::little)), little); - BOOST_TEST_EQ((be::convert_value(big, be::order::big, be::order::native)), native); - BOOST_TEST_EQ((be::convert_value(little, be::order::little, be::order::big)), big); - BOOST_TEST_EQ((be::convert_value(little, be::order::little, be::order::native)), native); - BOOST_TEST_EQ((be::convert_value(native, be::order::native, be::order::big)), big); - BOOST_TEST_EQ((be::convert_value(native, be::order::native, be::order::little)), little); + BOOST_TEST_EQ((be::reverse_endianness(big, be::order::big, be::order::little)), + little); + BOOST_TEST_EQ((be::reverse_endianness(big, be::order::big, be::order::native)), + native); + BOOST_TEST_EQ((be::reverse_endianness(little, be::order::little, be::order::big)), + big); + BOOST_TEST_EQ((be::reverse_endianness(little, be::order::little, be::order::native)), + native); + BOOST_TEST_EQ((be::reverse_endianness(native, be::order::native, be::order::big)), + big); + BOOST_TEST_EQ((be::reverse_endianness(native, be::order::native, be::order::little)), + little); - // light test of modify-in-place functions +// // light test of modify-in-place functions +// +// T x; +// +// x = big; be::reverse_endianness(x); BOOST_TEST_EQ(x, little); +// x = big; be::convert(x); BOOST_TEST_EQ(x, little); +// x = big; be::convert(x, be::order::big, be::order::little); BOOST_TEST_EQ(x, little); +// +//# ifdef BOOST_BIG_ENDIAN +// x = native; be::big_endian(x); BOOST_TEST_EQ(x, big); +// x = big; be::big_endian(x); BOOST_TEST_EQ(x, big); +// x = little; be::big_endian(x); BOOST_TEST_EQ(x, little); +// x = native; be::little_endian(x); BOOST_TEST_EQ(x, little); +// x = big; be::little_endian(x); BOOST_TEST_EQ(x, little); +// x = little; be::little_endian(x); BOOST_TEST_EQ(x, big); +//# else +// x = native; be::big_endian(x); BOOST_TEST_EQ(x, big); +// x = big; be::big_endian(x); BOOST_TEST_EQ(x, little); +// x = little; be::big_endian(x); BOOST_TEST_EQ(x, big); +// x = native; be::little_endian(x); BOOST_TEST_EQ(x, little); +// x = big; be::little_endian(x); BOOST_TEST_EQ(x, big); +// x = little; be::little_endian(x); BOOST_TEST_EQ(x, little); +//# endif - T x; - - x = big; be::reverse(x); BOOST_TEST_EQ(x, little); - x = big; be::convert(x); BOOST_TEST_EQ(x, little); - x = big; be::convert(x, be::order::big, be::order::little); BOOST_TEST_EQ(x, little); - -# ifdef BOOST_BIG_ENDIAN - x = native; be::big_endian(x); BOOST_TEST_EQ(x, big); - x = big; be::big_endian(x); BOOST_TEST_EQ(x, big); - x = little; be::big_endian(x); BOOST_TEST_EQ(x, little); - x = native; be::little_endian(x); BOOST_TEST_EQ(x, little); - x = big; be::little_endian(x); BOOST_TEST_EQ(x, little); - x = little; be::little_endian(x); BOOST_TEST_EQ(x, big); -# else - x = native; be::big_endian(x); BOOST_TEST_EQ(x, big); - x = big; be::big_endian(x); BOOST_TEST_EQ(x, little); - x = little; be::big_endian(x); BOOST_TEST_EQ(x, big); - x = native; be::little_endian(x); BOOST_TEST_EQ(x, little); - x = big; be::little_endian(x); BOOST_TEST_EQ(x, big); - x = little; be::little_endian(x); BOOST_TEST_EQ(x, little); -# endif - - // synonym test - - x = big; x = be::bswap(x); BOOST_TEST_EQ(x, little); - x = big; be::mbswap(x); BOOST_TEST_EQ(x, little); - -# ifdef BOOST_BIG_ENDIAN - BOOST_TEST_EQ(be::htobe(native), big); - BOOST_TEST_EQ(be::htole(native), little); - BOOST_TEST_EQ(be::betoh(big), big); - BOOST_TEST_EQ(be::letoh(big), little); - BOOST_TEST_EQ(be::betoh(little), little); - BOOST_TEST_EQ(be::letoh(little), big); - - x = native; be::mhtobe(x); BOOST_TEST_EQ(x, big); - x = native; be::mhtole(x); BOOST_TEST_EQ(x, little); - x = big; be::mbetoh(x); BOOST_TEST_EQ(x, big); - x = big; be::mletoh(x); BOOST_TEST_EQ(x, little); - x = little; be::mbetoh(x); BOOST_TEST_EQ(x, little); - x = little; be::mletoh(x); BOOST_TEST_EQ(x, big); -# else - BOOST_TEST_EQ(be::htobe(native), big); - BOOST_TEST_EQ(be::htole(native), little); - BOOST_TEST_EQ(be::betoh(big), little); - BOOST_TEST_EQ(be::letoh(big), big); - BOOST_TEST_EQ(be::betoh(little), big); - BOOST_TEST_EQ(be::letoh(little), little); - - x = native; be::mhtobe(x); BOOST_TEST_EQ(x, big); - x = native; be::mhtole(x); BOOST_TEST_EQ(x, little); - x = big; be::mbetoh(x); BOOST_TEST_EQ(x, little); - x = big; be::mletoh(x); BOOST_TEST_EQ(x, big); - x = little; be::mbetoh(x); BOOST_TEST_EQ(x, big); - x = little; be::mletoh(x); BOOST_TEST_EQ(x, little); -# endif } } // unnamed namespace diff --git a/test/msvc/converter_test/converter_test.vcxproj b/test/msvc/converter_test/converter_test.vcxproj index b106d55..a6d7f64 100644 --- a/test/msvc/converter_test/converter_test.vcxproj +++ b/test/msvc/converter_test/converter_test.vcxproj @@ -18,6 +18,9 @@ x64 + + + {EAE18F4D-AAF2-4C19-86FB-1144B5BD5993} Win32Proj @@ -158,9 +161,6 @@ Executing test... - - -