Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / endian / test / float_typedef_test.cpp
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, class U> void test_buffer( U const & y, bool aligned )
21 {
22     align<T> x( y );
23
24     BOOST_TEST_EQ( sizeof(x), aligned? 2 * sizeof(U): 1 + sizeof(U) );
25
26     BOOST_TEST_EQ( x.v.value(), y );
27 }
28
29 template<class T, class U> void test_arithmetic( U const & y, bool aligned )
30 {
31     test_buffer<T>( y, aligned );
32
33     align<T> x( y );
34
35     BOOST_TEST_EQ( x.v + 7, y + 7 );
36 }
37
38 int main()
39 {
40     using namespace boost::endian;
41
42     // buffers
43
44     test_buffer<big_float32_buf_t>( 3.1416f, false );
45     test_buffer<big_float64_buf_t>( 3.14159, false );
46
47     test_buffer<little_float32_buf_t>( 3.1416f, false );
48     test_buffer<little_float64_buf_t>( 3.14159, false );
49
50     test_buffer<native_float32_buf_t>( 3.1416f, false );
51     test_buffer<native_float64_buf_t>( 3.14159, false );
52
53     test_buffer<big_float32_buf_at>( 3.1416f, true );
54     test_buffer<big_float64_buf_at>( 3.14159, true );
55
56     test_buffer<little_float32_buf_at>( 3.1416f, true );
57     test_buffer<little_float64_buf_at>( 3.14159, true );
58
59     // arithmetic
60
61     test_arithmetic<big_float32_t>( 3.1416f, false );
62     test_arithmetic<big_float64_t>( 3.14159, false );
63
64     test_arithmetic<little_float32_t>( 3.1416f, false );
65     test_arithmetic<little_float64_t>( 3.14159, false );
66
67     test_arithmetic<native_float32_t>( 3.1416f, false );
68     test_arithmetic<native_float64_t>( 3.14159, false );
69
70     test_arithmetic<big_float32_at>( 3.1416f, true );
71     test_arithmetic<big_float64_at>( 3.14159, true );
72
73     test_arithmetic<little_float32_at>( 3.1416f, true );
74     test_arithmetic<little_float64_at>( 3.14159, true );
75
76     return boost::report_errors();
77 }