1 // Copyright 2019 Peter Dimov
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
6 #include <boost/endian/conversion.hpp>
7 #include <boost/core/lightweight_test.hpp>
8 #include <boost/config.hpp>
9 #include <boost/cstdint.hpp>
18 unsigned char const * p_;
23 byte_span( unsigned char const * p, std::size_t n ): p_( p ), n_( n )
27 template<std::size_t N> explicit byte_span( unsigned char const (&a)[ N ] ): p_( a ), n_( N )
31 bool operator==( byte_span const& r ) const
33 if( n_ != r.n_ ) return false;
35 for( std::size_t i = 0; i < n_; ++i )
37 if( p_[ i ] != r.p_[ i ] ) return false;
43 friend std::ostream& operator<<( std::ostream& os, byte_span s )
45 if( s.n_ == 0 ) return os;
47 os << std::hex << std::setfill( '0' ) << std::uppercase;
49 os << std::setw( 2 ) << +s.p_[ 0 ];
51 for( std::size_t i = 1; i < s.n_; ++i )
53 os << ':' << std::setw( 2 ) << +s.p_[ i ];
56 os << std::dec << std::setfill( ' ' ) << std::nouppercase;;
62 template<class T> void test_1()
65 unsigned char v[] = { 0xAA, 0xAA };
67 boost::endian::endian_store<T, 1, boost::endian::order::little>( v, 0x01 );
69 unsigned char w[] = { 0x01, 0xAA };
71 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
75 unsigned char v[] = { 0xAA, 0xAA };
77 boost::endian::endian_store<T, 1, boost::endian::order::big>( v, 0x01 );
79 unsigned char w[] = { 0x01, 0xAA };
81 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
85 template<class T> void test_2()
88 unsigned char v[] = { 0xAA, 0xAA, 0xAA };
90 boost::endian::endian_store<T, 2, boost::endian::order::little>( v, 0x0102 );
92 unsigned char w[] = { 0x02, 0x01, 0xAA };
94 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
98 unsigned char v[] = { 0xAA, 0xAA, 0xAA };
100 boost::endian::endian_store<T, 2, boost::endian::order::big>( v, 0x0102 );
102 unsigned char w[] = { 0x01, 0x02, 0xAA };
104 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
108 template<class T> void test_3()
111 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA };
113 boost::endian::endian_store<T, 3, boost::endian::order::little>( v, 0x010203 );
115 unsigned char w[] = { 0x03, 0x02, 0x01, 0xAA };
117 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
121 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA };
123 boost::endian::endian_store<T, 3, boost::endian::order::big>( v, 0x010203 );
125 unsigned char w[] = { 0x01, 0x02, 0x03, 0xAA };
127 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
131 template<class T> void test_4()
134 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
136 boost::endian::endian_store<T, 4, boost::endian::order::little>( v, 0x01020304 );
138 unsigned char w[] = { 0x04, 0x03, 0x02, 0x01, 0xAA };
140 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
144 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
146 boost::endian::endian_store<T, 4, boost::endian::order::big>( v, 0x01020304 );
148 unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0xAA };
150 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
154 template<class T> void test_5()
157 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
159 boost::endian::endian_store<T, 5, boost::endian::order::little>( v, 0x0102030405 );
161 unsigned char w[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
163 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
167 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
169 boost::endian::endian_store<T, 5, boost::endian::order::big>( v, 0x0102030405 );
171 unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA };
173 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
177 template<class T> void test_6()
180 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
182 boost::endian::endian_store<T, 6, boost::endian::order::little>( v, 0x010203040506 );
184 unsigned char w[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
186 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
190 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
192 boost::endian::endian_store<T, 6, boost::endian::order::big>( v, 0x010203040506 );
194 unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA };
196 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
200 template<class T> void test_7()
203 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
205 boost::endian::endian_store<T, 7, boost::endian::order::little>( v, 0x01020304050607 );
207 unsigned char w[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
209 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
213 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
215 boost::endian::endian_store<T, 7, boost::endian::order::big>( v, 0x01020304050607 );
217 unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA };
219 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
223 template<class T> void test_8()
226 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
228 boost::endian::endian_store<T, 8, boost::endian::order::little>( v, 0x0102030405060708 );
230 unsigned char w[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
232 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
236 unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
238 boost::endian::endian_store<T, 8, boost::endian::order::big>( v, 0x0102030405060708 );
240 unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA };
242 BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
250 test_1<boost::int8_t>();
251 test_1<boost::uint8_t>();
253 test_1<boost::int16_t>();
254 test_1<boost::uint16_t>();
256 test_1<boost::int32_t>();
257 test_1<boost::uint32_t>();
259 test_1<boost::int64_t>();
260 test_1<boost::uint64_t>();
264 test_2<boost::int16_t>();
265 test_2<boost::uint16_t>();
267 test_2<boost::int32_t>();
268 test_2<boost::uint32_t>();
270 test_2<boost::int64_t>();
271 test_2<boost::uint64_t>();
275 test_3<boost::int32_t>();
276 test_3<boost::uint32_t>();
278 test_3<boost::int64_t>();
279 test_3<boost::uint64_t>();
283 test_4<boost::int32_t>();
284 test_4<boost::uint32_t>();
286 test_4<boost::int64_t>();
287 test_4<boost::uint64_t>();
291 test_5<boost::int64_t>();
292 test_5<boost::uint64_t>();
296 test_6<boost::int64_t>();
297 test_6<boost::uint64_t>();
301 test_7<boost::int64_t>();
302 test_7<boost::uint64_t>();
306 test_8<boost::int64_t>();
307 test_8<boost::uint64_t>();
309 return boost::report_errors();