1 // buffer_test.cpp -------------------------------------------------------------------//
2
3 // Copyright Beman Dawes 2014
4
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
7
8 // See library home page at http://www.boost.org/libs/endian
9
10 //--------------------------------------------------------------------------------------//
11
12 #include <boost/endian/detail/disable_warnings.hpp>
13
14 #include <boost/endian/buffers.hpp>
15 #include <boost/core/lightweight_test.hpp>
16 #include <boost/cstdint.hpp>
17 #include <iostream>
18 #include <sstream>
19 #include <limits>
20
21 using namespace boost::endian;
22 using std::cout;
23 using std::endl;
24
25 namespace
26 {
27
28 // check_size ------------------------------------------------------------//
29
check_size()30 void check_size()
31 {
32
33 BOOST_TEST_EQ(sizeof(big_int8_buf_t), 1u);
34 BOOST_TEST_EQ(sizeof(big_int16_buf_t), 2u);
35 BOOST_TEST_EQ(sizeof(big_int24_buf_t), 3u);
36 BOOST_TEST_EQ(sizeof(big_int32_buf_t), 4u);
37 BOOST_TEST_EQ(sizeof(big_int40_buf_t), 5u);
38 BOOST_TEST_EQ(sizeof(big_int48_buf_t), 6u);
39 BOOST_TEST_EQ(sizeof(big_int56_buf_t), 7u);
40 BOOST_TEST_EQ(sizeof(big_int64_buf_t), 8u);
41
42 BOOST_TEST_EQ(sizeof(big_uint8_buf_t), 1u);
43 BOOST_TEST_EQ(sizeof(big_uint16_buf_t), 2u);
44 BOOST_TEST_EQ(sizeof(big_uint24_buf_t), 3u);
45 BOOST_TEST_EQ(sizeof(big_uint32_buf_t), 4u);
46 BOOST_TEST_EQ(sizeof(big_uint40_buf_t), 5u);
47 BOOST_TEST_EQ(sizeof(big_uint48_buf_t), 6u);
48 BOOST_TEST_EQ(sizeof(big_uint56_buf_t), 7u);
49 BOOST_TEST_EQ(sizeof(big_uint64_buf_t), 8u);
50
51 BOOST_TEST_EQ(sizeof(big_float32_buf_t), 4u);
52 BOOST_TEST_EQ(sizeof(big_float64_buf_t), 8u);
53
54 BOOST_TEST_EQ(sizeof(little_int8_buf_t), 1u);
55 BOOST_TEST_EQ(sizeof(little_int16_buf_t), 2u);
56 BOOST_TEST_EQ(sizeof(little_int24_buf_t), 3u);
57 BOOST_TEST_EQ(sizeof(little_int32_buf_t), 4u);
58 BOOST_TEST_EQ(sizeof(little_int40_buf_t), 5u);
59 BOOST_TEST_EQ(sizeof(little_int48_buf_t), 6u);
60 BOOST_TEST_EQ(sizeof(little_int56_buf_t), 7u);
61 BOOST_TEST_EQ(sizeof(little_int64_buf_t), 8u);
62
63 BOOST_TEST_EQ(sizeof(little_uint8_buf_t), 1u);
64 BOOST_TEST_EQ(sizeof(little_uint16_buf_t), 2u);
65 BOOST_TEST_EQ(sizeof(little_uint24_buf_t), 3u);
66 BOOST_TEST_EQ(sizeof(little_uint32_buf_t), 4u);
67 BOOST_TEST_EQ(sizeof(little_uint40_buf_t), 5u);
68 BOOST_TEST_EQ(sizeof(little_uint48_buf_t), 6u);
69 BOOST_TEST_EQ(sizeof(little_uint56_buf_t), 7u);
70 BOOST_TEST_EQ(sizeof(little_uint64_buf_t), 8u);
71
72 BOOST_TEST_EQ(sizeof(little_float32_buf_t), 4u);
73 BOOST_TEST_EQ(sizeof(little_float64_buf_t), 8u);
74
75 BOOST_TEST_EQ(sizeof(native_int8_buf_t), 1u);
76 BOOST_TEST_EQ(sizeof(native_int16_buf_t), 2u);
77 BOOST_TEST_EQ(sizeof(native_int24_buf_t), 3u);
78 BOOST_TEST_EQ(sizeof(native_int32_buf_t), 4u);
79 BOOST_TEST_EQ(sizeof(native_int40_buf_t), 5u);
80 BOOST_TEST_EQ(sizeof(native_int48_buf_t), 6u);
81 BOOST_TEST_EQ(sizeof(native_int56_buf_t), 7u);
82 BOOST_TEST_EQ(sizeof(native_int64_buf_t), 8u);
83
84 BOOST_TEST_EQ(sizeof(native_uint8_buf_t), 1u);
85 BOOST_TEST_EQ(sizeof(native_uint16_buf_t), 2u);
86 BOOST_TEST_EQ(sizeof(native_uint24_buf_t), 3u);
87 BOOST_TEST_EQ(sizeof(native_uint32_buf_t), 4u);
88 BOOST_TEST_EQ(sizeof(native_uint40_buf_t), 5u);
89 BOOST_TEST_EQ(sizeof(native_uint48_buf_t), 6u);
90 BOOST_TEST_EQ(sizeof(native_uint56_buf_t), 7u);
91 BOOST_TEST_EQ(sizeof(native_uint64_buf_t), 8u);
92
93 BOOST_TEST_EQ(sizeof(native_float32_buf_t), 4u);
94 BOOST_TEST_EQ(sizeof(native_float64_buf_t), 8u);
95
96 BOOST_TEST_EQ(sizeof(big_int8_buf_at), 1u);
97 BOOST_TEST_EQ(sizeof(big_int16_buf_at), 2u);
98 BOOST_TEST_EQ(sizeof(big_int32_buf_at), 4u);
99 BOOST_TEST_EQ(sizeof(big_int64_buf_at), 8u);
100
101 BOOST_TEST_EQ(sizeof(big_uint8_buf_at), 1u);
102 BOOST_TEST_EQ(sizeof(big_uint16_buf_at), 2u);
103 BOOST_TEST_EQ(sizeof(big_uint32_buf_at), 4u);
104 BOOST_TEST_EQ(sizeof(big_uint64_buf_at), 8u);
105
106 BOOST_TEST_EQ(sizeof(big_float32_buf_at), 4u);
107 BOOST_TEST_EQ(sizeof(big_float64_buf_at), 8u);
108
109 BOOST_TEST_EQ(sizeof(little_int8_buf_at), 1u);
110 BOOST_TEST_EQ(sizeof(little_int16_buf_at), 2u);
111 BOOST_TEST_EQ(sizeof(little_int32_buf_at), 4u);
112 BOOST_TEST_EQ(sizeof(little_int64_buf_at), 8u);
113
114 BOOST_TEST_EQ(sizeof(little_uint8_buf_at), 1u);
115 BOOST_TEST_EQ(sizeof(little_uint16_buf_at), 2u);
116 BOOST_TEST_EQ(sizeof(little_uint32_buf_at), 4u);
117 BOOST_TEST_EQ(sizeof(little_uint64_buf_at), 8u);
118
119 BOOST_TEST_EQ(sizeof(little_float32_buf_at), 4u);
120 BOOST_TEST_EQ(sizeof(little_float64_buf_at), 8u);
121
122 } // check_size
123
124 // test_inserter_and_extractor -----------------------------------------------------//
125
test_inserter_and_extractor()126 void test_inserter_and_extractor()
127 {
128 std::cout << "test inserter and extractor..." << std::endl;
129
130 big_uint64_buf_t bu64(0x010203040506070ULL);
131 little_uint64_buf_t lu64(0x010203040506070ULL);
132
133 boost::uint64_t x;
134
135 std::stringstream ss;
136
137 ss << bu64;
138 ss >> x;
139 BOOST_TEST_EQ(x, 0x010203040506070ULL);
140
141 ss.clear();
142 ss << lu64;
143 ss >> x;
144 BOOST_TEST_EQ(x, 0x010203040506070ULL);
145
146 ss.clear();
147 ss << 0x010203040506070ULL;
148 big_uint64_buf_t bu64z(0);
149 ss >> bu64z;
150 BOOST_TEST_EQ(bu64z.value(), bu64.value());
151
152 ss.clear();
153 ss << 0x010203040506070ULL;
154 little_uint64_buf_t lu64z(0);
155 ss >> lu64z;
156 BOOST_TEST_EQ(lu64z.value(), lu64.value());
157
158 std::cout << "test inserter and extractor complete" << std::endl;
159
160 }
161
162 template<class T> struct unaligned
163 {
164 char x;
165 T y;
166 };
167
test_buffer_type(typename T::value_type v1,typename T::value_type v2)168 template<class T> void test_buffer_type( typename T::value_type v1, typename T::value_type v2 )
169 {
170 T buffer( v1 );
171 BOOST_TEST_EQ( buffer.value(), v1 );
172
173 buffer = v2;
174 BOOST_TEST_EQ( buffer.value(), v2 );
175
176 unaligned<T> buffer2 = { 0, T( v1 ) };
177 BOOST_TEST_EQ( buffer2.y.value(), v1 );
178
179 buffer2.y = v2;
180 BOOST_TEST_EQ( buffer2.y.value(), v2 );
181 }
182
test_construction_and_assignment()183 void test_construction_and_assignment()
184 {
185 std::cout << "test construction and assignment..." << std::endl;
186
187 test_buffer_type< big_int8_buf_at>( 0x01, -0x01 );
188 test_buffer_type<big_int16_buf_at>( 0x0102, -0x0102 );
189 test_buffer_type<big_int32_buf_at>( 0x01020304, -0x01020304 );
190 test_buffer_type<big_int64_buf_at>( 0x0102030405060708LL, -0x0102030405060708LL );
191
192 test_buffer_type< big_uint8_buf_at>( 0x01, 0xFE );
193 test_buffer_type<big_uint16_buf_at>( 0x0102, 0xFE02 );
194 test_buffer_type<big_uint32_buf_at>( 0x01020304, 0xFE020304 );
195 test_buffer_type<big_uint64_buf_at>( 0x0102030405060708ULL, 0xFE02030405060708ULL );
196
197 test_buffer_type<big_float32_buf_at>( +1.5f, -3.14f );
198 test_buffer_type<big_float64_buf_at>( +1.5, -3.14 );
199
200 test_buffer_type< little_int8_buf_at>( 0x01, -0x01 );
201 test_buffer_type<little_int16_buf_at>( 0x0102, -0x0102 );
202 test_buffer_type<little_int32_buf_at>( 0x01020304, -0x01020304 );
203 test_buffer_type<little_int64_buf_at>( 0x0102030405060708LL, -0x0102030405060708LL );
204
205 test_buffer_type< little_uint8_buf_at>( 0x01, 0xFE );
206 test_buffer_type<little_uint16_buf_at>( 0x0102, 0xFE02 );
207 test_buffer_type<little_uint32_buf_at>( 0x01020304, 0xFE020304 );
208 test_buffer_type<little_uint64_buf_at>( 0x0102030405060708ULL, 0xFE02030405060708ULL );
209
210 test_buffer_type<little_float32_buf_at>( +1.5f, -3.14f );
211 test_buffer_type<little_float64_buf_at>( +1.5, -3.14 );
212
213 test_buffer_type< big_int8_buf_t>( 0x01, -0x01 );
214 test_buffer_type<big_int16_buf_t>( 0x0102, -0x0102 );
215 test_buffer_type<big_int24_buf_t>( 0x010203, -0x010203 );
216 test_buffer_type<big_int32_buf_t>( 0x01020304, -0x01020304 );
217 test_buffer_type<big_int40_buf_t>( 0x0102030405LL, -0x0102030405LL );
218 test_buffer_type<big_int48_buf_t>( 0x010203040506LL, -0x010203040506LL );
219 test_buffer_type<big_int56_buf_t>( 0x01020304050607LL, -0x01020304050607LL );
220 test_buffer_type<big_int64_buf_t>( 0x0102030405060708LL, -0x0102030405060708LL );
221
222 test_buffer_type<big_float32_buf_t>( +1.5f, -3.14f );
223 test_buffer_type<big_float64_buf_t>( +1.5, -3.14 );
224
225 test_buffer_type< little_uint8_buf_t>( 0x01, 0xFE );
226 test_buffer_type<little_uint16_buf_t>( 0x0102, 0xFE02 );
227 test_buffer_type<little_uint24_buf_t>( 0x010203, 0xFE0203 );
228 test_buffer_type<little_uint32_buf_t>( 0x01020304, 0xFE020304 );
229 test_buffer_type<little_uint40_buf_t>( 0x0102030405ULL, 0xFE02030405ULL );
230 test_buffer_type<little_uint48_buf_t>( 0x010203040506ULL, 0xFE0203040506ULL );
231 test_buffer_type<little_uint56_buf_t>( 0x01020304050607ULL, 0xFE020304050607ULL );
232 test_buffer_type<little_uint64_buf_t>( 0x0102030405060708ULL, 0xFE02030405060708ULL );
233
234 test_buffer_type<little_float32_buf_t>( +1.5f, -3.14f );
235 test_buffer_type<little_float64_buf_t>( +1.5, -3.14 );
236
237 std::cout << "test construction and assignment complete" << std::endl;
238 }
239
240 template <typename T>
test_boundary_values_()241 void test_boundary_values_()
242 {
243 test_buffer_type< endian_buffer<order::big, T, sizeof(T) * CHAR_BIT, align::no > >( std::numeric_limits<T>::min(), std::numeric_limits<T>::max() );
244 test_buffer_type< endian_buffer<order::little, T, sizeof(T) * CHAR_BIT, align::no > >( std::numeric_limits<T>::min(), std::numeric_limits<T>::max() );
245 test_buffer_type< endian_buffer<order::big, T, sizeof(T) * CHAR_BIT, align::yes> >( std::numeric_limits<T>::min(), std::numeric_limits<T>::max() );
246 test_buffer_type< endian_buffer<order::little, T, sizeof(T) * CHAR_BIT, align::yes> >( std::numeric_limits<T>::min(), std::numeric_limits<T>::max() );
247 }
248
test_boundary_values()249 void test_boundary_values()
250 {
251 std::cout << "test boundary values..." << std::endl;
252
253 // integer types
254
255 test_boundary_values_<signed char>();
256 test_boundary_values_<unsigned char>();
257 test_boundary_values_<signed short>();
258 test_boundary_values_<unsigned short>();
259 test_boundary_values_<signed int>();
260 test_boundary_values_<unsigned int>();
261 test_boundary_values_<signed long>();
262 test_boundary_values_<unsigned long>();
263 test_boundary_values_<signed long long>();
264 test_boundary_values_<unsigned long long>();
265
266 // character types
267
268 test_boundary_values_<char>();
269
270 #if !defined(BOOST_NO_CXX11_CHAR16_T)
271 test_boundary_values_<char16_t>();
272 #endif
273
274 #if !defined(BOOST_NO_CXX11_CHAR32_T)
275 test_boundary_values_<char32_t>();
276 #endif
277
278 // floating-point types
279
280 test_boundary_values_<float>();
281 test_boundary_values_<double>();
282
283 std::cout << "test boundary values complete" << std::endl;
284 }
285
286 } // unnamed namespace
287
288 //--------------------------------------------------------------------------------------//
289
cpp_main(int,char * [])290 int cpp_main(int, char *[])
291 {
292 cout << "byte swap intrinsics: " BOOST_ENDIAN_INTRINSIC_MSG << endl;
293
294 cout << " construct big endian aligned" << endl;
295 big_int32_buf_at x(1122334455);
296
297 cout << " assign to buffer from built-in integer" << endl;
298 x = 1234567890;
299
300 cout << " operator==(buffer.value(), built-in)" << endl;
301 bool b1(x.value() == 1234567890);
302 BOOST_TEST(b1);
303
304 cout << " construct little endian unaligned" << endl;
305 little_int32_buf_t x2(1122334455);
306
307 cout << " assign to buffer from built-in integer" << endl;
308 x2 = 1234567890;
309
310 cout << " operator==(buffer.value(), built-in)" << endl;
311 bool b2(x2.value() == 1234567890);
312 BOOST_TEST(b2);
313
314 check_size();
315 test_inserter_and_extractor();
316 test_construction_and_assignment();
317 test_boundary_values();
318
319 cout << " done" << endl;
320
321 return ::boost::report_errors();
322 }
323
main(int argc,char * argv[])324 int main( int argc, char* argv[] )
325 {
326 try
327 {
328 return cpp_main( argc, argv );
329 }
330 catch( std::exception const & x )
331 {
332 BOOST_ERROR( x.what() );
333 return boost::report_errors();
334 }
335 }
336
337 #include <boost/endian/detail/disable_warnings_pop.hpp>
338