• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Peter Dimov
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 
6 #include <boost/endian/arithmetic.hpp>
7 #include <boost/endian/buffers.hpp>
8 #include <boost/core/lightweight_test.hpp>
9 #include <boost/config.hpp>
10 #include <boost/cstdint.hpp>
11 #include <cstddef>
12 
test()13 template<class U> void test()
14 {
15     {
16         U u( 0 );
17 
18         unsigned char * p1 = u.data();
19         void * p2 = &u;
20 
21         BOOST_TEST_EQ( p1, p2 );
22     }
23 
24     {
25         U const u( 0 );
26 
27         unsigned char const * p1 = u.data();
28         void const * p2 = &u;
29 
30         BOOST_TEST_EQ( p1, p2 );
31     }
32 }
33 
test_unaligned()34 template<class T, std::size_t Bits> void test_unaligned()
35 {
36     using namespace boost::endian;
37 
38     test< endian_buffer<order::big, T, Bits, align::no> >();
39     test< endian_buffer<order::little, T, Bits, align::no> >();
40     test< endian_buffer<order::native, T, Bits, align::no> >();
41 
42     test< endian_arithmetic<order::big, T, Bits, align::no> >();
43     test< endian_arithmetic<order::little, T, Bits, align::no> >();
44     test< endian_arithmetic<order::native, T, Bits, align::no> >();
45 }
46 
test_aligned()47 template<class T, std::size_t Bits> void test_aligned()
48 {
49     using namespace boost::endian;
50 
51     test< endian_buffer<order::big, T, Bits, align::yes> >();
52     test< endian_buffer<order::little, T, Bits, align::yes> >();
53 
54     test< endian_arithmetic<order::big, T, Bits, align::yes> >();
55     test< endian_arithmetic<order::little, T, Bits, align::yes> >();
56 }
57 
main()58 int main()
59 {
60     test_unaligned<boost::int_least8_t, 8>();
61     test_unaligned<boost::int_least16_t, 16>();
62     test_unaligned<boost::int_least32_t, 24>();
63     test_unaligned<boost::int_least32_t, 32>();
64     test_unaligned<boost::int_least64_t, 40>();
65     test_unaligned<boost::int_least64_t, 48>();
66     test_unaligned<boost::int_least64_t, 56>();
67     test_unaligned<boost::int_least64_t, 64>();
68 
69     test_unaligned<boost::uint_least8_t, 8>();
70     test_unaligned<boost::uint_least16_t, 16>();
71     test_unaligned<boost::uint_least32_t, 24>();
72     test_unaligned<boost::uint_least32_t, 32>();
73     test_unaligned<boost::uint_least64_t, 40>();
74     test_unaligned<boost::uint_least64_t, 48>();
75     test_unaligned<boost::uint_least64_t, 56>();
76     test_unaligned<boost::uint_least64_t, 64>();
77 
78     test_unaligned<float, 32>();
79     test_unaligned<double, 64>();
80 
81     test_aligned<boost::int8_t, 8>();
82     test_aligned<boost::int16_t, 16>();
83     test_aligned<boost::int32_t, 32>();
84     test_aligned<boost::int64_t, 64>();
85 
86     test_aligned<boost::uint8_t, 8>();
87     test_aligned<boost::uint16_t, 16>();
88     test_aligned<boost::uint32_t, 32>();
89     test_aligned<boost::uint64_t, 64>();
90 
91     test_aligned<float, 32>();
92     test_aligned<double, 64>();
93 
94     return boost::report_errors();
95 }
96