endian_reverse_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #if defined(_MSC_VER)
  6. # pragma warning( disable: 4309 ) // static_cast: truncation of constant value
  7. #endif
  8. #include <boost/endian/conversion.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/config.hpp>
  11. #include <cstddef>
  12. template<class T, std::size_t N = sizeof(T)> struct test_value
  13. {
  14. };
  15. template<class T> struct test_value<T, 1>
  16. {
  17. static const T v1 = static_cast<T>( 0x1F );
  18. static const T w1 = static_cast<T>( 0x1F );
  19. static const T v2 = static_cast<T>( 0xF1 );
  20. static const T w2 = static_cast<T>( 0xF1 );
  21. };
  22. template<class T> T const test_value<T, 1>::v1;
  23. template<class T> T const test_value<T, 1>::w1;
  24. template<class T> T const test_value<T, 1>::v2;
  25. template<class T> T const test_value<T, 1>::w2;
  26. template<class T> struct test_value<T, 2>
  27. {
  28. static const T v1 = static_cast<T>( 0x1F2E );
  29. static const T w1 = static_cast<T>( 0x2E1F );
  30. static const T v2 = static_cast<T>( 0xF1E2 );
  31. static const T w2 = static_cast<T>( 0xE2F1 );
  32. };
  33. template<class T> T const test_value<T, 2>::v1;
  34. template<class T> T const test_value<T, 2>::w1;
  35. template<class T> T const test_value<T, 2>::v2;
  36. template<class T> T const test_value<T, 2>::w2;
  37. template<class T> struct test_value<T, 4>
  38. {
  39. static const T v1 = static_cast<T>( 0x1F2E3D4C );
  40. static const T w1 = static_cast<T>( 0x4C3D2E1F );
  41. static const T v2 = static_cast<T>( 0xF1E2D3C4 );
  42. static const T w2 = static_cast<T>( 0xC4D3E2F1 );
  43. };
  44. template<class T> T const test_value<T, 4>::v1;
  45. template<class T> T const test_value<T, 4>::w1;
  46. template<class T> T const test_value<T, 4>::v2;
  47. template<class T> T const test_value<T, 4>::w2;
  48. template<class T> struct test_value<T, 8>
  49. {
  50. static const T v1 = static_cast<T>( 0x1F2E3D4C5B6A7988ull );
  51. static const T w1 = static_cast<T>( 0x88796A5B4C3D2E1Full );
  52. static const T v2 = static_cast<T>( 0xF1E2D3C4B5A69788ull );
  53. static const T w2 = static_cast<T>( 0x8897A6B5C4D3E2F1ull );
  54. };
  55. template<class T> T const test_value<T, 8>::v1;
  56. template<class T> T const test_value<T, 8>::w1;
  57. template<class T> T const test_value<T, 8>::v2;
  58. template<class T> T const test_value<T, 8>::w2;
  59. #if defined(BOOST_HAS_INT128)
  60. template<class T> struct test_value<T, 16>
  61. {
  62. static const T v1 = static_cast<T>( 0x1F2E3D4C5B6A7988ull ) << 64 | static_cast<T>( 0xF1E2D3C4B5A69780ull );
  63. static const T w1 = static_cast<T>( 0x8097A6B5C4D3E2F1ull ) << 64 | static_cast<T>( 0x88796A5B4C3D2E1Full );
  64. static const T v2 = static_cast<T>( 0xF1E2D3C4B5A69788ull ) << 64 | static_cast<T>( 0x1F2E3D4C5B6A7980ull );
  65. static const T w2 = static_cast<T>( 0x80796A5B4C3D2E1Full ) << 64 | static_cast<T>( 0x8897A6B5C4D3E2F1ull );
  66. };
  67. template<class T> T const test_value<T, 16>::v1;
  68. template<class T> T const test_value<T, 16>::w1;
  69. template<class T> T const test_value<T, 16>::v2;
  70. template<class T> T const test_value<T, 16>::w2;
  71. #endif // #if defined(BOOST_HAS_INT128)
  72. template<class T> void test()
  73. {
  74. using boost::endian::endian_reverse;
  75. using boost::endian::endian_reverse_inplace;
  76. {
  77. T t1 = test_value<T>::v1;
  78. T t2 = endian_reverse( t1 );
  79. BOOST_TEST_EQ( t2, test_value<T>::w1 );
  80. T t3 = endian_reverse( t2 );
  81. BOOST_TEST_EQ( t3, t1 );
  82. T t4 = t1;
  83. endian_reverse_inplace( t4 );
  84. BOOST_TEST_EQ( t4, test_value<T>::w1 );
  85. endian_reverse_inplace( t4 );
  86. BOOST_TEST_EQ( t4, t1 );
  87. }
  88. {
  89. T t1 = test_value<T>::v2;
  90. T t2 = endian_reverse( t1 );
  91. BOOST_TEST_EQ( t2, test_value<T>::w2 );
  92. T t3 = endian_reverse( t2 );
  93. BOOST_TEST_EQ( t3, t1 );
  94. T t4 = t1;
  95. endian_reverse_inplace( t4 );
  96. BOOST_TEST_EQ( t4, test_value<T>::w2 );
  97. endian_reverse_inplace( t4 );
  98. BOOST_TEST_EQ( t4, t1 );
  99. }
  100. }
  101. template<class T> void test_np()
  102. {
  103. using boost::endian::endian_reverse;
  104. using boost::endian::endian_reverse_inplace;
  105. {
  106. T t1 = test_value<T>::v1;
  107. T t2 = endian_reverse( t1 );
  108. BOOST_TEST( t2 == test_value<T>::w1 );
  109. T t3 = endian_reverse( t2 );
  110. BOOST_TEST( t3 == t1 );
  111. T t4 = t1;
  112. endian_reverse_inplace( t4 );
  113. BOOST_TEST( t4 == test_value<T>::w1 );
  114. endian_reverse_inplace( t4 );
  115. BOOST_TEST( t4 == t1 );
  116. }
  117. {
  118. T t1 = test_value<T>::v2;
  119. T t2 = endian_reverse( t1 );
  120. BOOST_TEST( t2 == test_value<T>::w2 );
  121. T t3 = endian_reverse( t2 );
  122. BOOST_TEST( t3 == t1 );
  123. T t4 = t1;
  124. endian_reverse_inplace( t4 );
  125. BOOST_TEST( t4 == test_value<T>::w2 );
  126. endian_reverse_inplace( t4 );
  127. BOOST_TEST( t4 == t1 );
  128. }
  129. }
  130. int main()
  131. {
  132. test<boost::int8_t>();
  133. test<boost::uint8_t>();
  134. test<boost::int16_t>();
  135. test<boost::uint16_t>();
  136. test<boost::int32_t>();
  137. test<boost::uint32_t>();
  138. test<boost::int64_t>();
  139. test<boost::uint64_t>();
  140. test<char>();
  141. test<unsigned char>();
  142. test<signed char>();
  143. test<short>();
  144. test<unsigned short>();
  145. test<int>();
  146. test<unsigned int>();
  147. test<long>();
  148. test<unsigned long>();
  149. test<long long>();
  150. test<unsigned long long>();
  151. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  152. test<char16_t>();
  153. #endif
  154. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  155. test<char32_t>();
  156. #endif
  157. #if defined(BOOST_HAS_INT128)
  158. test_np<boost::int128_type>();
  159. test_np<boost::uint128_type>();
  160. #endif
  161. return boost::report_errors();
  162. }