endian_arithmetic_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/core/lightweight_test.hpp>
  7. #include <boost/config.hpp>
  8. #include <boost/cstdint.hpp>
  9. #include <cstddef>
  10. template<BOOST_SCOPED_ENUM(boost::endian::order) Order, BOOST_SCOPED_ENUM(boost::endian::align) Align, class T> void test_arithmetic_( T const& x )
  11. {
  12. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y( x );
  13. BOOST_TEST_EQ( +x, +y );
  14. BOOST_TEST_EQ( x + x, y + y );
  15. BOOST_TEST_EQ( x - x, y - y );
  16. BOOST_TEST_EQ( x * x, y * y );
  17. BOOST_TEST_EQ( x / x, y / y );
  18. {
  19. T x2( x );
  20. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  21. BOOST_TEST_EQ( x2 += x, y2 += y );
  22. }
  23. {
  24. T x2( x );
  25. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  26. BOOST_TEST_EQ( x2 -= x, y2 -= y );
  27. }
  28. {
  29. T x2( x );
  30. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  31. BOOST_TEST_EQ( x2 *= x, y2 *= y );
  32. }
  33. {
  34. T x2( x );
  35. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  36. BOOST_TEST_EQ( x2 /= x, y2 /= y );
  37. }
  38. {
  39. T x2( x );
  40. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  41. BOOST_TEST_EQ( ++x2, ++y2 );
  42. }
  43. {
  44. T x2( x );
  45. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  46. BOOST_TEST_EQ( --x2, --y2 );
  47. }
  48. {
  49. T x2( x );
  50. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  51. BOOST_TEST_EQ( x2++, y2++ );
  52. }
  53. {
  54. T x2( x );
  55. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  56. BOOST_TEST_EQ( x2--, y2-- );
  57. }
  58. }
  59. template<BOOST_SCOPED_ENUM(boost::endian::order) Order, BOOST_SCOPED_ENUM(boost::endian::align) Align, class T> void test_integral_( T const& x )
  60. {
  61. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y( x );
  62. BOOST_TEST_EQ( x % x, y % y );
  63. BOOST_TEST_EQ( x & x, y & y );
  64. BOOST_TEST_EQ( x | x, y | y );
  65. BOOST_TEST_EQ( x ^ x, y ^ y );
  66. BOOST_TEST_EQ( x << 1, y << 1 );
  67. BOOST_TEST_EQ( x >> 1, y >> 1 );
  68. {
  69. T x2( x );
  70. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  71. BOOST_TEST_EQ( x2 %= x, y2 %= y );
  72. }
  73. {
  74. T x2( x );
  75. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  76. BOOST_TEST_EQ( x2 &= x, y2 &= y );
  77. }
  78. {
  79. T x2( x );
  80. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  81. BOOST_TEST_EQ( x2 |= x, y2 |= y );
  82. }
  83. {
  84. T x2( x );
  85. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  86. BOOST_TEST_EQ( x2 ^= x, y2 ^= y );
  87. }
  88. {
  89. T x2( x );
  90. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  91. BOOST_TEST_EQ( x2 <<= 1, y2 <<= 1 );
  92. }
  93. {
  94. T x2( x );
  95. boost::endian::endian_arithmetic<Order, T, sizeof(T) * 8, Align> y2( y );
  96. BOOST_TEST_EQ( x2 >>= 1, y2 >>= 1 );
  97. }
  98. }
  99. template<class T> void test_arithmetic( T const& x )
  100. {
  101. test_arithmetic_<boost::endian::order::little, boost::endian::align::no>( x );
  102. test_arithmetic_<boost::endian::order::little, boost::endian::align::yes>( x );
  103. test_arithmetic_<boost::endian::order::big, boost::endian::align::no>( x );
  104. test_arithmetic_<boost::endian::order::big, boost::endian::align::yes>( x );
  105. }
  106. template<class T> void test_integral( T const& x )
  107. {
  108. test_arithmetic( x );
  109. test_integral_<boost::endian::order::little, boost::endian::align::no>( x );
  110. test_integral_<boost::endian::order::little, boost::endian::align::yes>( x );
  111. test_integral_<boost::endian::order::big, boost::endian::align::no>( x );
  112. test_integral_<boost::endian::order::big, boost::endian::align::yes>( x );
  113. }
  114. int main()
  115. {
  116. test_integral( 0x7EF2 );
  117. test_integral( 0x01020304u );
  118. test_arithmetic( 3.1416f );
  119. test_arithmetic( 3.14159 );
  120. return boost::report_errors();
  121. }