diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 88963c8..dc3e1c0 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -79,3 +79,5 @@ endian-run float_typedef_test.cpp ; endian-run data_test.cpp ; endian-run endian_hpp_test.cpp ; + +run order_test.cpp ; diff --git a/test/order_test.cpp b/test/order_test.cpp new file mode 100644 index 0000000..f98b07c --- /dev/null +++ b/test/order_test.cpp @@ -0,0 +1,87 @@ +// Copyright 2019 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) +# pragma warning(disable: 4127) // conditional expression is constant +#endif + +class byte_span +{ +private: + + unsigned char const * p_; + std::size_t n_; + +public: + + byte_span( unsigned char const * p, std::size_t n ): p_( p ), n_( n ) + { + } + + template explicit byte_span( unsigned char const (&a)[ N ] ): p_( a ), n_( N ) + { + } + + bool operator==( byte_span const& r ) const + { + if( n_ != r.n_ ) return false; + + for( std::size_t i = 0; i < n_; ++i ) + { + if( p_[ i ] != r.p_[ i ] ) return false; + } + + return true; + } + + friend std::ostream& operator<<( std::ostream& os, byte_span s ) + { + if( s.n_ == 0 ) return os; + + os << std::hex << std::setfill( '0' ) << std::uppercase; + + os << std::setw( 2 ) << +s.p_[ 0 ]; + + for( std::size_t i = 1; i < s.n_; ++i ) + { + os << ':' << std::setw( 2 ) << +s.p_[ i ]; + } + + os << std::dec << std::setfill( ' ' ) << std::nouppercase; + + return os; + } +}; + +int main() +{ + boost::uint64_t v = static_cast( 0x0102030405060708ull ); + byte_span v2( reinterpret_cast( &v ), sizeof(v) ); + + if( boost::endian::order::native == boost::endian::order::little ) + { + unsigned char w[] = { 8, 7, 6, 5, 4, 3, 2, 1 }; + BOOST_TEST_EQ( v2, byte_span( w ) ); + } + else if( boost::endian::order::native == boost::endian::order::big ) + { + unsigned char w[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + BOOST_TEST_EQ( v2, byte_span( w ) ); + } + else + { + BOOST_ERROR( "boost::endian::order::native is neither big nor little" ); + } + + return boost::report_errors(); +}