endian_store_test.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/conversion.hpp>
  6. #include <boost/core/lightweight_test.hpp>
  7. #include <boost/config.hpp>
  8. #include <boost/cstdint.hpp>
  9. #include <cstddef>
  10. #include <ostream>
  11. #include <iomanip>
  12. class byte_span
  13. {
  14. private:
  15. unsigned char const * p_;
  16. std::size_t n_;
  17. public:
  18. byte_span( unsigned char const * p, std::size_t n ): p_( p ), n_( n )
  19. {
  20. }
  21. template<std::size_t N> explicit byte_span( unsigned char const (&a)[ N ] ): p_( a ), n_( N )
  22. {
  23. }
  24. bool operator==( byte_span const& r ) const
  25. {
  26. if( n_ != r.n_ ) return false;
  27. for( std::size_t i = 0; i < n_; ++i )
  28. {
  29. if( p_[ i ] != r.p_[ i ] ) return false;
  30. }
  31. return true;
  32. }
  33. friend std::ostream& operator<<( std::ostream& os, byte_span s )
  34. {
  35. if( s.n_ == 0 ) return os;
  36. os << std::hex << std::setfill( '0' ) << std::uppercase;
  37. os << std::setw( 2 ) << +s.p_[ 0 ];
  38. for( std::size_t i = 1; i < s.n_; ++i )
  39. {
  40. os << ':' << std::setw( 2 ) << +s.p_[ i ];
  41. }
  42. os << std::dec << std::setfill( ' ' ) << std::nouppercase;;
  43. return os;
  44. }
  45. };
  46. template<class T> void test_1()
  47. {
  48. {
  49. unsigned char v[] = { 0xAA, 0xAA };
  50. boost::endian::endian_store<T, 1, boost::endian::order::little>( v, 0x01 );
  51. unsigned char w[] = { 0x01, 0xAA };
  52. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  53. }
  54. {
  55. unsigned char v[] = { 0xAA, 0xAA };
  56. boost::endian::endian_store<T, 1, boost::endian::order::big>( v, 0x01 );
  57. unsigned char w[] = { 0x01, 0xAA };
  58. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  59. }
  60. }
  61. template<class T> void test_2()
  62. {
  63. {
  64. unsigned char v[] = { 0xAA, 0xAA, 0xAA };
  65. boost::endian::endian_store<T, 2, boost::endian::order::little>( v, 0x0102 );
  66. unsigned char w[] = { 0x02, 0x01, 0xAA };
  67. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  68. }
  69. {
  70. unsigned char v[] = { 0xAA, 0xAA, 0xAA };
  71. boost::endian::endian_store<T, 2, boost::endian::order::big>( v, 0x0102 );
  72. unsigned char w[] = { 0x01, 0x02, 0xAA };
  73. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  74. }
  75. }
  76. template<class T> void test_3()
  77. {
  78. {
  79. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA };
  80. boost::endian::endian_store<T, 3, boost::endian::order::little>( v, 0x010203 );
  81. unsigned char w[] = { 0x03, 0x02, 0x01, 0xAA };
  82. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  83. }
  84. {
  85. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA };
  86. boost::endian::endian_store<T, 3, boost::endian::order::big>( v, 0x010203 );
  87. unsigned char w[] = { 0x01, 0x02, 0x03, 0xAA };
  88. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  89. }
  90. }
  91. template<class T> void test_4()
  92. {
  93. {
  94. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  95. boost::endian::endian_store<T, 4, boost::endian::order::little>( v, 0x01020304 );
  96. unsigned char w[] = { 0x04, 0x03, 0x02, 0x01, 0xAA };
  97. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  98. }
  99. {
  100. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  101. boost::endian::endian_store<T, 4, boost::endian::order::big>( v, 0x01020304 );
  102. unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0xAA };
  103. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  104. }
  105. }
  106. template<class T> void test_5()
  107. {
  108. {
  109. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  110. boost::endian::endian_store<T, 5, boost::endian::order::little>( v, 0x0102030405 );
  111. unsigned char w[] = { 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
  112. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  113. }
  114. {
  115. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  116. boost::endian::endian_store<T, 5, boost::endian::order::big>( v, 0x0102030405 );
  117. unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0xAA };
  118. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  119. }
  120. }
  121. template<class T> void test_6()
  122. {
  123. {
  124. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  125. boost::endian::endian_store<T, 6, boost::endian::order::little>( v, 0x010203040506 );
  126. unsigned char w[] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
  127. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  128. }
  129. {
  130. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  131. boost::endian::endian_store<T, 6, boost::endian::order::big>( v, 0x010203040506 );
  132. unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xAA };
  133. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  134. }
  135. }
  136. template<class T> void test_7()
  137. {
  138. {
  139. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  140. boost::endian::endian_store<T, 7, boost::endian::order::little>( v, 0x01020304050607 );
  141. unsigned char w[] = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
  142. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  143. }
  144. {
  145. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  146. boost::endian::endian_store<T, 7, boost::endian::order::big>( v, 0x01020304050607 );
  147. unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xAA };
  148. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  149. }
  150. }
  151. template<class T> void test_8()
  152. {
  153. {
  154. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  155. boost::endian::endian_store<T, 8, boost::endian::order::little>( v, 0x0102030405060708 );
  156. unsigned char w[] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0xAA };
  157. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  158. }
  159. {
  160. unsigned char v[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
  161. boost::endian::endian_store<T, 8, boost::endian::order::big>( v, 0x0102030405060708 );
  162. unsigned char w[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xAA };
  163. BOOST_TEST_EQ( byte_span( v ), byte_span( w ) );
  164. }
  165. }
  166. int main()
  167. {
  168. // 1
  169. test_1<boost::int8_t>();
  170. test_1<boost::uint8_t>();
  171. test_1<boost::int16_t>();
  172. test_1<boost::uint16_t>();
  173. test_1<boost::int32_t>();
  174. test_1<boost::uint32_t>();
  175. test_1<boost::int64_t>();
  176. test_1<boost::uint64_t>();
  177. // 2
  178. test_2<boost::int16_t>();
  179. test_2<boost::uint16_t>();
  180. test_2<boost::int32_t>();
  181. test_2<boost::uint32_t>();
  182. test_2<boost::int64_t>();
  183. test_2<boost::uint64_t>();
  184. // 3
  185. test_3<boost::int32_t>();
  186. test_3<boost::uint32_t>();
  187. test_3<boost::int64_t>();
  188. test_3<boost::uint64_t>();
  189. // 4
  190. test_4<boost::int32_t>();
  191. test_4<boost::uint32_t>();
  192. test_4<boost::int64_t>();
  193. test_4<boost::uint64_t>();
  194. // 5
  195. test_5<boost::int64_t>();
  196. test_5<boost::uint64_t>();
  197. // 6
  198. test_6<boost::int64_t>();
  199. test_6<boost::uint64_t>();
  200. // 7
  201. test_7<boost::int64_t>();
  202. test_7<boost::uint64_t>();
  203. // 8
  204. test_8<boost::int64_t>();
  205. test_8<boost::uint64_t>();
  206. return boost::report_errors();
  207. }