endian_ld_st_roundtrip_test.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. template<class T> void test( T const& x )
  11. {
  12. {
  13. unsigned char buffer[ sizeof(T) ];
  14. boost::endian::endian_store<T, sizeof(T), boost::endian::order::little>( buffer, x );
  15. T x2 = boost::endian::endian_load<T, sizeof(T), boost::endian::order::little>( buffer );
  16. BOOST_TEST_EQ( x, x2 );
  17. }
  18. {
  19. unsigned char buffer[ sizeof(T) ];
  20. boost::endian::endian_store<T, sizeof(T), boost::endian::order::big>( buffer, x );
  21. T x2 = boost::endian::endian_load<T, sizeof(T), boost::endian::order::big>( buffer );
  22. BOOST_TEST_EQ( x, x2 );
  23. }
  24. }
  25. enum E
  26. {
  27. e = 0xF1F2F3
  28. };
  29. int main()
  30. {
  31. test( 1.2e+34f );
  32. test( -1.234e+56 );
  33. test( e );
  34. return boost::report_errors();
  35. }