float_typedef_test.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <boost/endian/arithmetic.hpp>
  6. #include <boost/endian/buffers.hpp>
  7. #include <boost/core/lightweight_test.hpp>
  8. template<class T> struct align
  9. {
  10. char _;
  11. T v;
  12. explicit align( typename T::value_type y ): _(), v( y )
  13. {
  14. }
  15. };
  16. template<class T, class U> void test_buffer( U const & y, bool aligned )
  17. {
  18. align<T> x( y );
  19. BOOST_TEST_EQ( sizeof(x), aligned? 2 * sizeof(U): 1 + sizeof(U) );
  20. BOOST_TEST_EQ( x.v.value(), y );
  21. }
  22. template<class T, class U> void test_arithmetic( U const & y, bool aligned )
  23. {
  24. test_buffer<T>( y, aligned );
  25. align<T> x( y );
  26. BOOST_TEST_EQ( x.v + 7, y + 7 );
  27. }
  28. int main()
  29. {
  30. using namespace boost::endian;
  31. // buffers
  32. test_buffer<big_float32_buf_t>( 3.1416f, false );
  33. test_buffer<big_float64_buf_t>( 3.14159, false );
  34. test_buffer<little_float32_buf_t>( 3.1416f, false );
  35. test_buffer<little_float64_buf_t>( 3.14159, false );
  36. test_buffer<native_float32_buf_t>( 3.1416f, false );
  37. test_buffer<native_float64_buf_t>( 3.14159, false );
  38. test_buffer<big_float32_buf_at>( 3.1416f, true );
  39. test_buffer<big_float64_buf_at>( 3.14159, true );
  40. test_buffer<little_float32_buf_at>( 3.1416f, true );
  41. test_buffer<little_float64_buf_at>( 3.14159, true );
  42. // arithmetic
  43. test_arithmetic<big_float32_t>( 3.1416f, false );
  44. test_arithmetic<big_float64_t>( 3.14159, false );
  45. test_arithmetic<little_float32_t>( 3.1416f, false );
  46. test_arithmetic<little_float64_t>( 3.14159, false );
  47. test_arithmetic<native_float32_t>( 3.1416f, false );
  48. test_arithmetic<native_float64_t>( 3.14159, false );
  49. test_arithmetic<big_float32_at>( 3.1416f, true );
  50. test_arithmetic<big_float64_at>( 3.14159, true );
  51. test_arithmetic<little_float32_at>( 3.1416f, true );
  52. test_arithmetic<little_float64_at>( 3.14159, true );
  53. return boost::report_errors();
  54. }