binary3.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/spirit/include/phoenix_core.hpp>
  10. #include <boost/spirit/include/phoenix_operator.hpp>
  11. #include <boost/predef/other/endian.h>
  12. #include "test.hpp"
  13. using namespace spirit_test;
  14. ///////////////////////////////////////////////////////////////////////////////
  15. int
  16. main()
  17. {
  18. using namespace boost::spirit;
  19. using namespace boost::phoenix;
  20. { // test optional attributes
  21. #if BOOST_ENDIAN_LITTLE_BYTE
  22. boost::optional<boost::uint8_t> v8 (0x01);
  23. BOOST_TEST(binary_test("\x01", 1, byte_, v8));
  24. boost::optional<boost::uint16_t> v16 (0x0201);
  25. BOOST_TEST(binary_test("\x01\x02", 2, word, v16));
  26. boost::optional<boost::uint32_t> v32 (0x04030201);
  27. BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, v32));
  28. #ifdef BOOST_HAS_LONG_LONG
  29. boost::optional<boost::uint64_t> v64 (0x0807060504030201LL);
  30. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, v64));
  31. #endif
  32. boost::optional<float> vf(1.0f);
  33. BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float, vf));
  34. boost::optional<double> vd(1.0);
  35. BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
  36. bin_double, vd));
  37. #else // BOOST_ENDIAN_LITTLE_BYTE
  38. boost::optional<boost::uint8_t> v8 (0x01);
  39. BOOST_TEST(binary_test("\x01", 1, byte_, v8));
  40. boost::optional<boost::uint16_t> v16 (0x0102);
  41. BOOST_TEST(binary_test("\x01\x02", 2, word, v16));
  42. boost::optional<boost::uint32_t> v32 (0x01020304);
  43. BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, v32));
  44. #ifdef BOOST_HAS_LONG_LONG
  45. boost::optional<boost::uint64_t> v64 (0x0102030405060708LL);
  46. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, v64));
  47. #endif
  48. boost::optional<float> vf(1.0f);
  49. BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float, vf));
  50. boost::optional<double> vd(1.0);
  51. BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
  52. bin_double, vd));
  53. #endif
  54. }
  55. // we support Phoenix attributes only starting with V2.2
  56. #if SPIRIT_VERSION >= 0x2020
  57. { // test Phoenix expression attributes, only supported if
  58. // karma_phoenix_attributes.hpp is included
  59. namespace phoenix = boost::phoenix;
  60. #if BOOST_ENDIAN_LITTLE_BYTE
  61. BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::val(0x01)));
  62. BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::val(0x0201)));
  63. BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword,
  64. phoenix::val(0x04030201)));
  65. boost::uint8_t v8 (0x01);
  66. BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::ref(v8)));
  67. BOOST_TEST(binary_test("\x02", 1, byte_, ++phoenix::ref(v8)));
  68. boost::uint16_t v16 (0x0201);
  69. BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::ref(v16)));
  70. BOOST_TEST(binary_test("\x02\x02", 2, word, ++phoenix::ref(v16)));
  71. boost::uint32_t v32 (0x04030201);
  72. BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, phoenix::ref(v32)));
  73. BOOST_TEST(binary_test("\x02\x02\x03\x04", 4, dword, ++phoenix::ref(v32)));
  74. #ifdef BOOST_HAS_LONG_LONG
  75. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword,
  76. phoenix::val(0x0807060504030201LL)));
  77. boost::uint64_t v64 (0x0807060504030201LL);
  78. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword,
  79. phoenix::ref(v64)));
  80. BOOST_TEST(binary_test("\x02\x02\x03\x04\x05\x06\x07\x08", 8, qword,
  81. ++phoenix::ref(v64)));
  82. #endif
  83. BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float,
  84. phoenix::val(1.0f)));
  85. float vf(1.0f);
  86. BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float,
  87. phoenix::ref(vf)));
  88. BOOST_TEST(binary_test("\x00\x00\x00\x40", 4, bin_float,
  89. ++phoenix::ref(vf)));
  90. BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
  91. bin_double, phoenix::val(1.0)));
  92. double vd(1.0);
  93. BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
  94. bin_double, phoenix::ref(vd)));
  95. BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\x00\x40", 8,
  96. bin_double, ++phoenix::ref(vd)));
  97. #else // BOOST_ENDIAN_LITTLE_BYTE
  98. BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::val(0x01)));
  99. BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::val(0x0102)));
  100. BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword,
  101. phoenix::val(0x01020304)));
  102. boost::uint8_t v8 (0x01);
  103. BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::ref(v8)));
  104. BOOST_TEST(binary_test("\x02", 1, byte_, ++phoenix::ref(v8)));
  105. boost::uint16_t v16 (0x0102);
  106. BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::ref(v16)));
  107. BOOST_TEST(binary_test("\x01\x03", 2, word, ++phoenix::ref(v16)));
  108. boost::uint32_t v32 (0x01020304);
  109. BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, phoenix::ref(v32)));
  110. BOOST_TEST(binary_test("\x01\x02\x03\x05", 4, dword, ++phoenix::ref(v32)));
  111. #ifdef BOOST_HAS_LONG_LONG
  112. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword,
  113. phoenix::val(0x0102030405060708LL)));
  114. boost::uint64_t v64 (0x0102030405060708LL);
  115. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword,
  116. phoenix::ref(v64)));
  117. BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x09", 8, qword,
  118. ++phoenix::ref(v64)));
  119. #endif
  120. BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float,
  121. phoenix::val(1.0f)));
  122. float vf(1.0f);
  123. BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float,
  124. phoenix::ref(vf)));
  125. BOOST_TEST(binary_test("\x40\x00\x00\x00", 4, bin_float,
  126. ++phoenix::ref(vf)));
  127. BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
  128. bin_double, phoenix::val(1.0)));
  129. double vd(1.0);
  130. BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
  131. bin_double, phoenix::ref(vd)));
  132. BOOST_TEST(binary_test("\x40\x00\x00\x00\x00\x00\x00\x00", 8,
  133. bin_double, ++phoenix::ref(vd)));
  134. #endif
  135. }
  136. #endif
  137. return boost::report_errors();
  138. }