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