From 604781308152b192d4276cb9958458e0dc705826 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 27 Apr 2019 18:03:43 +0300 Subject: [PATCH] Add endian_store_test --- test/Jamfile.v2 | 3 + test/endian_store_test.cpp | 401 +++++++++++++++++++++++++++++++++++++ 2 files changed, 404 insertions(+) create mode 100644 test/endian_store_test.cpp diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 8222140..9e015b7 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -60,5 +60,8 @@ run endian_reverse_test.cpp : : : BOOST_ENDIAN_NO_INTRINSICS : endian_re run endian_load_test.cpp ; run endian_load_test.cpp : : : BOOST_ENDIAN_NO_INTRINSICS : endian_load_test_ni ; +run endian_store_test.cpp ; +run endian_store_test.cpp : : : BOOST_ENDIAN_NO_INTRINSICS : endian_store_test_ni ; + run endian_ld_st_roundtrip_test.cpp ; run endian_ld_st_roundtrip_test.cpp : : : BOOST_ENDIAN_NO_INTRINSICS : endian_ld_st_roundtrip_test_ni ; diff --git a/test/endian_store_test.cpp b/test/endian_store_test.cpp new file mode 100644 index 0000000..470a187 --- /dev/null +++ b/test/endian_store_test.cpp @@ -0,0 +1,401 @@ +// 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 + +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() +{ + // 1 + + { + unsigned char v[] = { 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01, v ); + + unsigned char w[] = { 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01, v ); + + unsigned char w[] = { 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01, v ); + + unsigned char w[] = { 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01, v ); + + unsigned char w[] = { 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + // 2 + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102, v ); + + unsigned char w[] = { 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102, v ); + + unsigned char w[] = { 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102, v ); + + unsigned char w[] = { 0x01, 0x02, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102, v ); + + unsigned char w[] = { 0x01, 0x02, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + // 3 + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x010203, v ); + + unsigned char w[] = { 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x010203, v ); + + unsigned char w[] = { 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x010203, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x010203, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + // 4 + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01020304, v ); + + unsigned char w[] = { 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01020304, v ); + + unsigned char w[] = { 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01020304, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01020304, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + // 5 + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102030405, v ); + + unsigned char w[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102030405, v ); + + unsigned char w[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102030405, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102030405, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + // 6 + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x010203040506, v ); + + unsigned char w[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x010203040506, v ); + + unsigned char w[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x010203040506, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x010203040506, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + // 7 + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01020304050607, v ); + + unsigned char w[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01020304050607, v ); + + unsigned char w[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01020304050607, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x01020304050607, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + // 8 + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102030405060708, v ); + + unsigned char w[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102030405060708, v ); + + unsigned char w[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102030405060708, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + { + unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; + + boost::endian::endian_store( 0x0102030405060708, v ); + + unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA }; + + BOOST_TEST_EQ( byte_span( v ), byte_span( w ) ); + } + + return boost::report_errors(); +}