1 // Copyright 2020 Peter Dimov
2 // Distributed under the Boost Software License, Version 1.0.
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #include <boost/endian/conversion.hpp>
6 #include <boost/core/lightweight_test.hpp>
7 #include <boost/config.hpp>
8 #include <cstddef>
9 
test_reverse_inplace(T x)10 template<class T> void test_reverse_inplace( T x )
11 {
12     using boost::endian::endian_reverse_inplace;
13 
14     T x2( x );
15 
16     endian_reverse_inplace( x2 );
17     endian_reverse_inplace( x2 );
18     BOOST_TEST( x == x2 );
19 
20     boost::endian::native_to_little_inplace( x2 );
21     boost::endian::little_to_native_inplace( x2 );
22     BOOST_TEST( x == x2 );
23 
24     boost::endian::native_to_big_inplace( x2 );
25     boost::endian::big_to_native_inplace( x2 );
26     BOOST_TEST( x == x2 );
27 }
28 
29 struct X
30 {
31     int v1_;
32     int v2_;
33 };
34 
operator ==(X const & x1,X const & x2)35 inline bool operator==( X const& x1, X const& x2 )
36 {
37     return x1.v1_ == x2.v1_ && x1.v2_ == x2.v2_;
38 }
39 
endian_reverse_inplace(X & x)40 inline void endian_reverse_inplace( X & x )
41 {
42     using boost::endian::endian_reverse_inplace;
43 
44     endian_reverse_inplace( x.v1_ );
45     endian_reverse_inplace( x.v2_ );
46 }
47 
48 struct Y
49 {
50     X x1_;
51     X x2_[ 2 ];
52 };
53 
operator ==(Y const & y1,Y const & y2)54 inline bool operator==( Y const& y1, Y const& y2 )
55 {
56     return y1.x1_ == y2.x1_ && y1.x2_[0] == y2.x2_[0] && y1.x2_[1] == y2.x2_[1];
57 }
58 
endian_reverse_inplace(Y & y)59 inline void endian_reverse_inplace( Y & y )
60 {
61     using boost::endian::endian_reverse_inplace;
62 
63     endian_reverse_inplace( y.x1_ );
64     endian_reverse_inplace( y.x2_ );
65 }
66 
main()67 int main()
68 {
69     Y y = { { 1, 2 }, { { 3, 4 }, { 5, 6 } } };
70 
71     test_reverse_inplace( y );
72 
73     return boost::report_errors();
74 }
75