]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/test/x3/binary.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
8 #include <boost/spirit/home/x3/binary.hpp>
9 #include <boost/cstdint.hpp>
10 #include <boost/predef/other/endian.h>
13 ///////////////////////////////////////////////////////////////////////////////
16 using spirit_test::test_attr
;
17 using spirit_test::test
;
18 using spirit_test::binary_test
;
19 using spirit_test::binary_test_attr
;
21 using boost::spirit::x3::byte_
;
22 using boost::spirit::x3::word
;
23 using boost::spirit::x3::dword
;
24 using boost::spirit::x3::big_word
;
25 using boost::spirit::x3::big_dword
;
26 using boost::spirit::x3::little_word
;
27 using boost::spirit::x3::little_dword
;
28 #ifdef BOOST_HAS_LONG_LONG
29 using boost::spirit::x3::qword
;
30 using boost::spirit::x3::big_qword
;
31 using boost::spirit::x3::little_qword
;
33 using boost::spirit::x3::bin_float
;
34 using boost::spirit::x3::big_bin_float
;
35 using boost::spirit::x3::little_bin_float
;
36 using boost::spirit::x3::bin_double
;
37 using boost::spirit::x3::big_bin_double
;
38 using boost::spirit::x3::little_bin_double
;
43 #ifdef BOOST_HAS_LONG_LONG
49 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(byte_
);
50 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(word
);
51 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(dword
);
52 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_word
);
53 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_dword
);
54 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_word
);
55 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_dword
);
56 #ifdef BOOST_HAS_LONG_LONG
57 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(qword
);
58 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_qword
);
59 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_qword
);
60 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(bin_float
);
61 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_bin_float
);
62 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_bin_float
);
63 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(bin_double
);
64 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_bin_double
);
65 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_bin_double
);
68 // TODO: boost::endian::endian_arithmetic value constructor is not constexpr
70 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(byte_(1));
71 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(word(1));
72 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(dword(1));
73 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_word(1));
74 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_dword(1));
75 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_word(1));
76 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_dword(1));
77 #ifdef BOOST_HAS_LONG_LONG
78 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(qword(1));
79 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_qword(1));
80 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_qword(1));
82 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(bin_float(1.0f
));
83 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_bin_float(1.0f
));
84 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_bin_float(1.0f
));
85 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(bin_double(1.0));
86 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(big_bin_double(1.0));
87 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(little_bin_double(1.0));
90 { // test native endian binaries
91 #if BOOST_ENDIAN_LITTLE_BYTE
92 BOOST_TEST(test_attr("\x01", byte_
, uc
) && uc
== 0x01);
93 BOOST_TEST(test_attr("\x01\x02", word
, us
) && us
== 0x0201);
94 BOOST_TEST(test_attr("\x01\x02\x03\x04", dword
, ui
) && ui
== 0x04030201);
95 #ifdef BOOST_HAS_LONG_LONG
96 BOOST_TEST(test_attr("\x01\x02\x03\x04\x05\x06\x07\x08", qword
, ul
) &&
97 ul
== 0x0807060504030201LL
);
99 BOOST_TEST(binary_test_attr("\x00\x00\x80\x3f", 4, bin_float
, f
) &&
101 BOOST_TEST(binary_test_attr("\x00\x00\x00\x00\x00\x00\xf0\x3f",
102 8, bin_double
, d
) && f
== 1.0);
104 BOOST_TEST(test_attr("\x01", byte_
, uc
) && uc
== 0x01);
105 BOOST_TEST(test_attr("\x01\x02", word
, us
) && us
== 0x0102);
106 BOOST_TEST(test_attr("\x01\x02\x03\x04", dword
, ui
) && ui
== 0x01020304);
107 #ifdef BOOST_HAS_LONG_LONG
108 BOOST_TEST(test_attr("\x01\x02\x03\x04\x05\x06\x07\x08", qword
, ul
) &&
109 ul
== 0x0102030405060708LL
);
111 BOOST_TEST(binary_test_attr("\x3f\x80\x00\x00", 4, bin_float
, f
) &&
113 BOOST_TEST(binary_test_attr("\x3f\xf0\x00\x00\x00\x00\x00\x00",
114 8, bin_double
, d
) && f
== 1.0);
118 { // test native endian binaries
119 #if BOOST_ENDIAN_LITTLE_BYTE
120 BOOST_TEST(test("\x01", byte_(0x01)));
121 BOOST_TEST(test("\x01\x02", word(0x0201)));
122 BOOST_TEST(test("\x01\x02\x03\x04", dword(0x04030201)));
123 #ifdef BOOST_HAS_LONG_LONG
124 BOOST_TEST(test("\x01\x02\x03\x04\x05\x06\x07\x08",
125 qword(0x0807060504030201LL
)));
127 BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, bin_float(1.0f
)));
128 BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
131 BOOST_TEST(test("\x01", byte_(0x01)));
132 BOOST_TEST(test("\x01\x02", word(0x0102)));
133 BOOST_TEST(test("\x01\x02\x03\x04", dword(0x01020304)));
134 #ifdef BOOST_HAS_LONG_LONG
135 BOOST_TEST(test("\x01\x02\x03\x04\x05\x06\x07\x08",
136 qword(0x0102030405060708LL
)));
138 BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, bin_float(1.0f
)));
139 BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
144 { // test big endian binaries
145 BOOST_TEST(test_attr("\x01\x02", big_word
, us
) && us
== 0x0102);
146 BOOST_TEST(test_attr("\x01\x02\x03\x04", big_dword
, ui
) && ui
== 0x01020304);
147 #ifdef BOOST_HAS_LONG_LONG
148 BOOST_TEST(test_attr("\x01\x02\x03\x04\x05\x06\x07\x08", big_qword
, ul
)
149 && ul
== 0x0102030405060708LL
);
151 BOOST_TEST(binary_test_attr("\x3f\x80\x00\x00", 4, big_bin_float
, f
) &&
153 BOOST_TEST(binary_test_attr("\x3f\xf0\x00\x00\x00\x00\x00\x00",
154 8, big_bin_double
, d
) && f
== 1.0);
158 BOOST_TEST(test("\x01\x02", big_word(0x0102)));
159 BOOST_TEST(test("\x01\x02\x03\x04", big_dword(0x01020304)));
160 #ifdef BOOST_HAS_LONG_LONG
161 BOOST_TEST(test("\x01\x02\x03\x04\x05\x06\x07\x08",
162 big_qword(0x0102030405060708LL
)));
164 BOOST_TEST(binary_test("\x3f\x80\x00\x00", 4, big_bin_float(1.0f
)));
165 BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00", 8,
166 big_bin_double(1.0)));
169 { // test little endian binaries
170 BOOST_TEST(test_attr("\x01\x02", little_word
, us
) && us
== 0x0201);
171 BOOST_TEST(test_attr("\x01\x02\x03\x04", little_dword
, ui
) && ui
== 0x04030201);
172 #ifdef BOOST_HAS_LONG_LONG
173 BOOST_TEST(test_attr("\x01\x02\x03\x04\x05\x06\x07\x08", little_qword
, ul
)
174 && ul
== 0x0807060504030201LL
);
176 BOOST_TEST(binary_test_attr("\x00\x00\x80\x3f", 4,
177 little_bin_float
, f
) && f
== 1.0f
);
178 BOOST_TEST(binary_test_attr("\x00\x00\x00\x00\x00\x00\xf0\x3f",
179 8, little_bin_double
, d
) && f
== 1.0);
183 BOOST_TEST(test("\x01\x02", little_word(0x0201)));
184 BOOST_TEST(test("\x01\x02\x03\x04", little_dword(0x04030201)));
185 #ifdef BOOST_HAS_LONG_LONG
186 BOOST_TEST(test("\x01\x02\x03\x04\x05\x06\x07\x08",
187 little_qword(0x0807060504030201LL
)));
189 BOOST_TEST(binary_test("\x00\x00\x80\x3f", 4, little_bin_float(1.0f
)));
190 BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f", 8,
191 little_bin_double(1.0)));
194 return boost::report_errors();