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

source code of boost/libs/spirit/test/karma/binary3.cpp