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 optional attributes
29 
30 #ifdef BOOST_LITTLE_ENDIAN
31         boost::optional<boost::uint8_t> v8 (0x01);
32         BOOST_TEST(binary_test("\x01", 1, byte_, v8));
33         boost::optional<boost::uint16_t> v16 (0x0201);
34         BOOST_TEST(binary_test("\x01\x02", 2, word, v16));
35         boost::optional<boost::uint32_t> v32 (0x04030201);
36         BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, v32));
37 #ifdef BOOST_HAS_LONG_LONG
38         boost::optional<boost::uint64_t> v64 (0x0807060504030201LL);
39         BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, v64));
40 #endif
41         boost::optional<float> vf(1.0f);
42         BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float, vf));
43         boost::optional<double> vd(1.0);
44         BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
45             bin_double, vd));
46 
47 #else // BOOST_LITTLE_ENDIAN
48 
49         boost::optional<boost::uint8_t> v8 (0x01);
50         BOOST_TEST(binary_test("\x01", 1, byte_, v8));
51         boost::optional<boost::uint16_t> v16 (0x0102);
52         BOOST_TEST(binary_test("\x01\x02", 2, word, v16));
53         boost::optional<boost::uint32_t> v32 (0x01020304);
54         BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, v32));
55 #ifdef BOOST_HAS_LONG_LONG
56         boost::optional<boost::uint64_t> v64 (0x0102030405060708LL);
57         BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword, v64));
58 #endif
59         boost::optional<float> vf(1.0f);
60         BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float, vf));
61         boost::optional<double> vd(1.0);
62         BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
63             bin_double, vd));
64 
65 #endif
66     }
67 
68 // we support Phoenix attributes only starting with V2.2
69 #if SPIRIT_VERSION >= 0x2020
70     {   // test Phoenix expression attributes, only supported if
71         // karma_phoenix_attributes.hpp is included
72         namespace phoenix = boost::phoenix;
73 
74 #ifdef BOOST_LITTLE_ENDIAN
75         BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::val(0x01)));
76         BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::val(0x0201)));
77         BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword,
78             phoenix::val(0x04030201)));
79 
80         boost::uint8_t v8 (0x01);
81         BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::ref(v8)));
82         BOOST_TEST(binary_test("\x02", 1, byte_, ++phoenix::ref(v8)));
83 
84         boost::uint16_t v16 (0x0201);
85         BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::ref(v16)));
86         BOOST_TEST(binary_test("\x02\x02", 2, word, ++phoenix::ref(v16)));
87 
88         boost::uint32_t v32 (0x04030201);
89         BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, phoenix::ref(v32)));
90         BOOST_TEST(binary_test("\x02\x02\x03\x04", 4, dword, ++phoenix::ref(v32)));
91 
92 #ifdef BOOST_HAS_LONG_LONG
93         BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword,
94             phoenix::val(0x0807060504030201LL)));
95 
96         boost::uint64_t v64 (0x0807060504030201LL);
97         BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword,
98             phoenix::ref(v64)));
99         BOOST_TEST(binary_test("\x02\x02\x03\x04\x05\x06\x07\x08", 8, qword,
100             ++phoenix::ref(v64)));
101 #endif
102         BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float,
103             phoenix::val(1.0f)));
104         float vf(1.0f);
105         BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float,
106             phoenix::ref(vf)));
107         BOOST_TEST(binary_test("\x00\x00\x00\x40", 4, bin_float,
108             ++phoenix::ref(vf)));
109 
110         BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
111             bin_double, phoenix::val(1.0)));
112         double vd(1.0);
113         BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
114             bin_double, phoenix::ref(vd)));
115         BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\x00\x40", 8,
116             bin_double, ++phoenix::ref(vd)));
117 
118 #else // BOOST_LITTLE_ENDIAN
119 
120         BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::val(0x01)));
121         BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::val(0x0102)));
122         BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword,
123             phoenix::val(0x01020304)));
124 
125         boost::uint8_t v8 (0x01);
126         BOOST_TEST(binary_test("\x01", 1, byte_, phoenix::ref(v8)));
127         BOOST_TEST(binary_test("\x02", 1, byte_, ++phoenix::ref(v8)));
128 
129         boost::uint16_t v16 (0x0102);
130         BOOST_TEST(binary_test("\x01\x02", 2, word, phoenix::ref(v16)));
131         BOOST_TEST(binary_test("\x01\x03", 2, word, ++phoenix::ref(v16)));
132 
133         boost::uint32_t v32 (0x01020304);
134         BOOST_TEST(binary_test("\x01\x02\x03\x04", 4, dword, phoenix::ref(v32)));
135         BOOST_TEST(binary_test("\x01\x02\x03\x05", 4, dword, ++phoenix::ref(v32)));
136 
137 #ifdef BOOST_HAS_LONG_LONG
138         BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword,
139             phoenix::val(0x0102030405060708LL)));
140 
141         boost::uint64_t v64 (0x0102030405060708LL);
142         BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08", 8, qword,
143             phoenix::ref(v64)));
144         BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x09", 8, qword,
145             ++phoenix::ref(v64)));
146 #endif
147         BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float,
148             phoenix::val(1.0f)));
149         float vf(1.0f);
150         BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float,
151             phoenix::ref(vf)));
152         BOOST_TEST(binary_test("\x40\x00\x00\x00", 4, bin_float,
153             ++phoenix::ref(vf)));
154 
155         BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
156             bin_double, phoenix::val(1.0)));
157         double vd(1.0);
158         BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
159             bin_double, phoenix::ref(vd)));
160         BOOST_TEST(binary_test("\x40\x00\x00\x00\x00\x00\x00\x00", 8,
161             bin_double, ++phoenix::ref(vd)));
162 
163 #endif
164     }
165 #endif
166 
167     return boost::report_errors();
168 }
169