binary2.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/config/warning_disable.hpp>
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/karma_binary.hpp>
  8. #include <boost/spirit/include/karma_generate.hpp>
  9. #include <boost/predef/other/endian.h>
  10. #include "test.hpp"
  11. using namespace spirit_test;
  12. ///////////////////////////////////////////////////////////////////////////////
  13. int
  14. main()
  15. {
  16. using namespace boost::spirit;
  17. { // test big endian binaries
  18. BOOST_TEST(binary_test("\x01\x82", 2, big_word, 0x0182));
  19. BOOST_TEST(binary_test("\x81\x02", 2, big_word, 0x8102));
  20. BOOST_TEST(binary_test("\x01\x02\x03\x84", 4, big_dword, 0x01020384));
  21. BOOST_TEST(binary_test("\x81\x02\x03\x04", 4, big_dword, 0x81020304));
  22. #ifdef BOOST_HAS_LONG_LONG
  23. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x88", 8, big_qword,
  24. 0x0102030405060788LL));
  25. BOOST_TEST(binary_test("\x81\x02\x03\x04\x05\x06\x07\x08", 8, big_qword,
  26. 0x8102030405060708LL));
  27. BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00",
  28. 10, big_qword, 0x0102030405060708LL, pad(10)));
  29. #endif
  30. BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, big_bin_float, 1.0f));
  31. BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
  32. big_bin_double, 1.0));
  33. BOOST_TEST(binary_test_delimited("\x3f\xf0\x00\x00\x00\x00\x00\x00\x00\x00",
  34. 10, big_bin_double, 1.0, pad(10)));
  35. }
  36. {
  37. BOOST_TEST(binary_test("\x01\x02", 2, big_word(0x0102)));
  38. BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, big_dword(0x01020304)));
  39. #ifdef BOOST_HAS_LONG_LONG
  40. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8,
  41. big_qword(0x0102030405060708LL)));
  42. BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00",
  43. 10, big_qword(0x0102030405060708LL), pad(10)));
  44. #endif
  45. BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, big_bin_float(1.0f)));
  46. BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
  47. big_bin_double(1.0)));
  48. BOOST_TEST(binary_test_delimited("\x3f\xf0\x00\x00\x00\x00\x00\x00\x00\x00",
  49. 10, big_bin_double(1.0), pad(10)));
  50. }
  51. { // test little endian binaries
  52. BOOST_TEST(binary_test("\x01\x82", 2, little_word, 0x8201));
  53. BOOST_TEST(binary_test("\x81\x02", 2, little_word, 0x0281));
  54. BOOST_TEST(binary_test("\x01\x02\x03\x84", 4, little_dword, 0x84030201));
  55. BOOST_TEST(binary_test("\x81\x02\x03\x04", 4, little_dword, 0x04030281));
  56. #ifdef BOOST_HAS_LONG_LONG
  57. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x88", 8, little_qword,
  58. 0x8807060504030201LL));
  59. BOOST_TEST(binary_test("\x81\x02\x03\x04\x05\x06\x07\x08", 8, little_qword,
  60. 0x0807060504030281LL));
  61. BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00",
  62. 10, little_qword, 0x0807060504030201LL, pad(10)));
  63. #endif
  64. BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, little_bin_float, 1.0f));
  65. BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
  66. little_bin_double, 1.0));
  67. BOOST_TEST(binary_test_delimited("\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00",
  68. 10, little_bin_double, 1.0, pad(10)));
  69. }
  70. {
  71. BOOST_TEST(binary_test("\x01\x02", 2, little_word(0x0201)));
  72. BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, little_dword(0x04030201)));
  73. #ifdef BOOST_HAS_LONG_LONG
  74. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8,
  75. little_qword(0x0807060504030201LL)));
  76. BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00",
  77. 10, little_qword(0x0807060504030201LL), pad(10)));
  78. #endif
  79. BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, little_bin_float(1.0f)));
  80. BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
  81. little_bin_double(1.0)));
  82. BOOST_TEST(binary_test_delimited("\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00",
  83. 10, little_bin_double(1.0), pad(10)));
  84. }
  85. { // test native endian binaries
  86. boost::optional<boost::uint8_t> v8;
  87. boost::optional<boost::uint16_t> v16;
  88. boost::optional<boost::uint32_t> v32;
  89. boost::optional<float> vf;
  90. boost::optional<double> vd;
  91. #if BOOST_ENDIAN_LITTLE_BYTE
  92. BOOST_TEST(!binary_test("", 0, byte_, v8));
  93. BOOST_TEST(!binary_test("", 0, word, v16));
  94. BOOST_TEST(!binary_test("", 0, dword, v32));
  95. #ifdef BOOST_HAS_LONG_LONG
  96. boost::optional<boost::uint64_t> v64;
  97. BOOST_TEST(!binary_test("", 0, qword, v64));
  98. #endif
  99. BOOST_TEST(!binary_test("", 0, bin_float, vf));
  100. BOOST_TEST(!binary_test("", 0, bin_double, vd));
  101. #else // BOOST_ENDIAN_LITTLE_BYTE
  102. BOOST_TEST(!binary_test("", 0, byte_, v8));
  103. BOOST_TEST(!binary_test("", 0, word, v16));
  104. BOOST_TEST(!binary_test("", 0, dword, v32));
  105. #ifdef BOOST_HAS_LONG_LONG
  106. boost::optional<boost::uint64_t> v64;
  107. BOOST_TEST(!binary_test("", 0, qword, v64));
  108. #endif
  109. BOOST_TEST(!binary_test("", 0, bin_float, vf));
  110. BOOST_TEST(!binary_test("", 0, bin_double, vd));
  111. #endif
  112. }
  113. return boost::report_errors();
  114. }