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 | |
10 | template<class T> struct align |
11 | { |
12 | char _; |
13 | T v; |
14 | |
15 | explicit align( typename T::value_type y ): _(), v( y ) |
16 | { |
17 | } |
18 | }; |
19 | |
20 | template<class T> struct align2 |
21 | { |
22 | char _; |
23 | T v; |
24 | }; |
25 | |
26 | template<class T, class U> void test_buffer( U const & y, bool aligned ) |
27 | { |
28 | align<T> x( y ); |
29 | |
30 | BOOST_TEST_EQ( sizeof(x), aligned? sizeof( align2<U> ): 1 + sizeof(U) ); |
31 | |
32 | BOOST_TEST_EQ( x.v.value(), y ); |
33 | } |
34 | |
35 | template<class T, class U> void test_arithmetic( U const & y, bool aligned ) |
36 | { |
37 | test_buffer<T>( y, aligned ); |
38 | |
39 | align<T> x( y ); |
40 | |
41 | BOOST_TEST_EQ( x.v + 7, y + 7 ); |
42 | } |
43 | |
44 | int main() |
45 | { |
46 | using namespace boost::endian; |
47 | |
48 | // buffers |
49 | |
50 | test_buffer<big_float32_buf_t>( y: 3.1416f, aligned: false ); |
51 | test_buffer<big_float64_buf_t>( y: 3.14159, aligned: false ); |
52 | |
53 | test_buffer<little_float32_buf_t>( y: 3.1416f, aligned: false ); |
54 | test_buffer<little_float64_buf_t>( y: 3.14159, aligned: false ); |
55 | |
56 | test_buffer<native_float32_buf_t>( y: 3.1416f, aligned: false ); |
57 | test_buffer<native_float64_buf_t>( y: 3.14159, aligned: false ); |
58 | |
59 | test_buffer<big_float32_buf_at>( y: 3.1416f, aligned: true ); |
60 | test_buffer<big_float64_buf_at>( y: 3.14159, aligned: true ); |
61 | |
62 | test_buffer<little_float32_buf_at>( y: 3.1416f, aligned: true ); |
63 | test_buffer<little_float64_buf_at>( y: 3.14159, aligned: true ); |
64 | |
65 | // arithmetic |
66 | |
67 | test_arithmetic<big_float32_t>( y: 3.1416f, aligned: false ); |
68 | test_arithmetic<big_float64_t>( y: 3.14159, aligned: false ); |
69 | |
70 | test_arithmetic<little_float32_t>( y: 3.1416f, aligned: false ); |
71 | test_arithmetic<little_float64_t>( y: 3.14159, aligned: false ); |
72 | |
73 | test_arithmetic<native_float32_t>( y: 3.1416f, aligned: false ); |
74 | test_arithmetic<native_float64_t>( y: 3.14159, aligned: false ); |
75 | |
76 | test_arithmetic<big_float32_at>( y: 3.1416f, aligned: true ); |
77 | test_arithmetic<big_float64_at>( y: 3.14159, aligned: true ); |
78 | |
79 | test_arithmetic<little_float32_at>( y: 3.1416f, aligned: true ); |
80 | test_arithmetic<little_float64_at>( y: 3.14159, aligned: true ); |
81 | |
82 | return boost::report_errors(); |
83 | } |
84 | |