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