1 // endian_test.cpp ---------------------------------------------------------//
3 // Copyright Beman Dawes 1999-2008
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
8 // See library home page at http://www.boost.org/libs/endian
10 //----------------------------------------------------------------------------//
12 // This test probes for correct endianness, size, and value.
14 // See endian_operations_test for tests of operator correctness and interaction
15 // between operand types.
17 //----------------------------------------------------------------------------//
19 #include <boost/endian/detail/disable_warnings.hpp>
21 #include <boost/endian/arithmetic.hpp>
22 #include <boost/cstdint.hpp>
23 #include <boost/detail/lightweight_main.hpp>
28 #include <cstdlib> // for atoi(), exit()
29 #include <cstring> // for memcmp()
31 using namespace std; // Not the best programming practice, but I
32 using namespace boost; // want to verify this combination of using
33 using namespace boost::endian; // namespaces works. See endian_operations_test
34 // // for tests that don't do "using namespace".
36 #define VERIFY(predicate) verify( predicate, __LINE__ )
37 #define VERIFY_SIZE(actual, expected) verify_size( actual, expected, __LINE__ )
38 #define VERIFY_VALUE_AND_OPS(endian_t,expected_t,expected) verify_value_and_ops<endian_t, expected_t>( expected, __LINE__ )
39 #define VERIFY_BIG_REPRESENTATION(t) verify_representation<t>( true, __LINE__ )
40 #define VERIFY_LITTLE_REPRESENTATION(t) verify_representation<t>( false, __LINE__ )
41 #define VERIFY_NATIVE_REPRESENTATION(t) verify_native_representation<t>( __LINE__ )
47 void verify( bool x, int line )
51 cout << "Error: verify failed on line " << line << endl;
54 void verify_size( size_t actual, size_t expected, int line )
56 if ( actual == expected ) return;
58 cout << "Error: verify size failed on line " << line << endl;
59 cout << " A structure with an expected sizeof() " << expected
60 << " had an actual sizeof() " << actual
61 << "\n This will cause uses of endian types to fail\n";
64 template <class Endian, class Base>
65 void verify_value_and_ops( const Base & expected, int line )
68 verify( v == expected, line );
71 v2.operator=( expected );
72 verify( v2 == expected, line );
74 ++v; // verify integer_cover_operators being applied to this type -
75 // will fail to compile if no endian<> specialization is present
77 Endian v3( static_cast<Base>( 1 ) );
79 Endian x(static_cast<typename Endian::value_type>(v2+v3));
80 if ( x == x ) // silence warning
84 const char * big_rep = "\x12\x34\x56\x78\x9A\xBC\xDE\xF0";
85 const char * little_rep = "\xF0\xDE\xBC\x9A\x78\x56\x34\x12";
87 template <class Endian>
88 void verify_representation( bool is_big, int line )
91 Endian x ( static_cast<typename Endian::value_type>
92 (0x123456789abcdef0LL + silence) ); // will truncate
96 reinterpret_cast<const char*>(big_rep)+8-sizeof(Endian),
97 sizeof(Endian) ) == 0, line );
99 verify( memcmp( &x, little_rep, sizeof(Endian) ) == 0, line );
102 template <class Endian>
103 inline void verify_native_representation( int line )
105 # if BOOST_ENDIAN_BIG_BYTE
106 verify_representation<Endian>( true, line );
108 verify_representation<Endian>( false, line );
112 // detect_order -----------------------------------------------------//
122 View v = { 0x0102030405060708LL }; // initialize v.i
124 if ( memcmp( v.c, "\x8\7\6\5\4\3\2\1", 8) == 0 )
126 cout << "This machine is little-endian.\n";
127 # if !BOOST_ENDIAN_LITTLE_BYTE
128 cout << "yet boost/predef/other/endian.h does not define BOOST_ENDIAN_LITTLE_BYTE.\n"
129 "The Boost Endian library must be revised to work correctly on this system.\n"
130 "Please report this problem to the Boost mailing list.\n";
134 else if ( memcmp( v.c, "\1\2\3\4\5\6\7\x8", 8) == 0 )
136 cout << "This machine is big-endian.\n";
137 # if !BOOST_ENDIAN_BIG_BYTE
138 cout << "yet boost/predef/other/endian.h does not define BOOST_ENDIAN_BIG_BYTE.\n"
139 "The Boost Endian library must be revised to work correctly on this system.\n"
140 "Please report this problem to the Boost mailing list.\n";
146 cout << "This machine is neither strict big-endian nor strict little-endian\n"
147 "The Boost Endian library must be revised to work correctly on this system.\n"
148 "Please report this problem to the Boost mailing list.\n";
151 cout << "That should not matter and is presented for your information only.\n";
154 // check_data ------------------------------------------------------------//
168 big_uint16_t big_u16;
169 big_uint24_t big_u24;
170 big_uint32_t big_u32;
171 big_uint40_t big_u40;
172 big_uint48_t big_u48;
173 big_uint56_t big_u56;
174 big_uint64_t big_u64;
176 little_int8_t little_8;
177 little_int16_t little_16;
178 little_int24_t little_24;
179 little_int32_t little_32;
180 little_int40_t little_40;
181 little_int48_t little_48;
182 little_int56_t little_56;
183 little_int64_t little_64;
185 little_uint8_t little_u8;
186 little_uint16_t little_u16;
187 little_uint24_t little_u24;
188 little_uint32_t little_u32;
189 little_uint40_t little_u40;
190 little_uint48_t little_u48;
191 little_uint56_t little_u56;
192 little_uint64_t little_u64;
194 native_int8_t native_8;
195 native_int16_t native_16;
196 native_int24_t native_24;
197 native_int32_t native_32;
198 native_int40_t native_40;
199 native_int48_t native_48;
200 native_int56_t native_56;
201 native_int64_t native_64;
203 native_uint8_t native_u8;
204 native_uint16_t native_u16;
205 native_uint24_t native_u24;
206 native_uint32_t native_u32;
207 native_uint40_t native_u40;
208 native_uint48_t native_u48;
209 native_uint56_t native_u56;
210 native_uint64_t native_u64;
212 big_int16_at big_align_int16;
213 big_int32_at big_align_int32;
214 big_int64_at big_align_int64;
216 big_uint16_at big_align_uint16;
217 big_uint32_at big_align_uint32;
218 big_uint64_at big_align_uint64;
220 little_int16_at little_align_int16;
221 little_int32_at little_align_int32;
222 little_int64_at little_align_int64;
224 little_uint16_at little_align_uint16;
225 little_uint32_at little_align_uint32;
226 little_uint64_at little_align_uint64;
228 VERIFY(big_8.data() == reinterpret_cast<const unsigned char *>(&big_8));
229 VERIFY(big_16.data() == reinterpret_cast<const unsigned char *>(&big_16));
230 VERIFY(big_24.data() == reinterpret_cast<const unsigned char *>(&big_24));
231 VERIFY(big_32.data() == reinterpret_cast<const unsigned char *>(&big_32));
232 VERIFY(big_40.data() == reinterpret_cast<const unsigned char *>(&big_40));
233 VERIFY(big_48.data() == reinterpret_cast<const unsigned char *>(&big_48));
234 VERIFY(big_56.data() == reinterpret_cast<const unsigned char *>(&big_56));
235 VERIFY(big_64.data() == reinterpret_cast<const unsigned char *>(&big_64));
237 VERIFY(big_u8.data() == reinterpret_cast<const unsigned char *>(&big_u8));
238 VERIFY(big_u16.data() == reinterpret_cast<const unsigned char *>(&big_u16));
239 VERIFY(big_u24.data() == reinterpret_cast<const unsigned char *>(&big_u24));
240 VERIFY(big_u32.data() == reinterpret_cast<const unsigned char *>(&big_u32));
241 VERIFY(big_u40.data() == reinterpret_cast<const unsigned char *>(&big_u40));
242 VERIFY(big_u48.data() == reinterpret_cast<const unsigned char *>(&big_u48));
243 VERIFY(big_u56.data() == reinterpret_cast<const unsigned char *>(&big_u56));
244 VERIFY(big_u64.data() == reinterpret_cast<const unsigned char *>(&big_u64));
246 VERIFY(little_8.data() == reinterpret_cast<const unsigned char *>(&little_8));
247 VERIFY(little_16.data() == reinterpret_cast<const unsigned char *>(&little_16));
248 VERIFY(little_24.data() == reinterpret_cast<const unsigned char *>(&little_24));
249 VERIFY(little_32.data() == reinterpret_cast<const unsigned char *>(&little_32));
250 VERIFY(little_40.data() == reinterpret_cast<const unsigned char *>(&little_40));
251 VERIFY(little_48.data() == reinterpret_cast<const unsigned char *>(&little_48));
252 VERIFY(little_56.data() == reinterpret_cast<const unsigned char *>(&little_56));
253 VERIFY(little_64.data() == reinterpret_cast<const unsigned char *>(&little_64));
255 VERIFY(little_u8.data() == reinterpret_cast<const unsigned char *>(&little_u8));
256 VERIFY(little_u16.data() == reinterpret_cast<const unsigned char *>(&little_u16));
257 VERIFY(little_u24.data() == reinterpret_cast<const unsigned char *>(&little_u24));
258 VERIFY(little_u32.data() == reinterpret_cast<const unsigned char *>(&little_u32));
259 VERIFY(little_u40.data() == reinterpret_cast<const unsigned char *>(&little_u40));
260 VERIFY(little_u48.data() == reinterpret_cast<const unsigned char *>(&little_u48));
261 VERIFY(little_u56.data() == reinterpret_cast<const unsigned char *>(&little_u56));
262 VERIFY(little_u64.data() == reinterpret_cast<const unsigned char *>(&little_u64));
264 VERIFY(native_8.data() == reinterpret_cast<const unsigned char *>(&native_8));
265 VERIFY(native_16.data() == reinterpret_cast<const unsigned char *>(&native_16));
266 VERIFY(native_24.data() == reinterpret_cast<const unsigned char *>(&native_24));
267 VERIFY(native_32.data() == reinterpret_cast<const unsigned char *>(&native_32));
268 VERIFY(native_40.data() == reinterpret_cast<const unsigned char *>(&native_40));
269 VERIFY(native_48.data() == reinterpret_cast<const unsigned char *>(&native_48));
270 VERIFY(native_56.data() == reinterpret_cast<const unsigned char *>(&native_56));
271 VERIFY(native_64.data() == reinterpret_cast<const unsigned char *>(&native_64));
273 VERIFY(native_u8.data() == reinterpret_cast<const unsigned char *>(&native_u8));
274 VERIFY(native_u16.data() == reinterpret_cast<const unsigned char *>(&native_u16));
275 VERIFY(native_u24.data() == reinterpret_cast<const unsigned char *>(&native_u24));
276 VERIFY(native_u32.data() == reinterpret_cast<const unsigned char *>(&native_u32));
277 VERIFY(native_u40.data() == reinterpret_cast<const unsigned char *>(&native_u40));
278 VERIFY(native_u48.data() == reinterpret_cast<const unsigned char *>(&native_u48));
279 VERIFY(native_u56.data() == reinterpret_cast<const unsigned char *>(&native_u56));
280 VERIFY(native_u64.data() == reinterpret_cast<const unsigned char *>(&native_u64));
282 VERIFY(big_align_int16.data() == reinterpret_cast<const unsigned char *>(&big_align_int16));
283 VERIFY(big_align_int32.data() == reinterpret_cast<const unsigned char *>(&big_align_int32));
284 VERIFY(big_align_int64.data() == reinterpret_cast<const unsigned char *>(&big_align_int64));
286 VERIFY(big_align_uint16.data() == reinterpret_cast<const unsigned char *>(&big_align_uint16));
287 VERIFY(big_align_uint32.data() == reinterpret_cast<const unsigned char *>(&big_align_uint32));
288 VERIFY(big_align_uint64.data() == reinterpret_cast<const unsigned char *>(&big_align_uint64));
290 VERIFY(little_align_int16.data() == reinterpret_cast<const unsigned char *>(&little_align_int16));
291 VERIFY(little_align_int32.data() == reinterpret_cast<const unsigned char *>(&little_align_int32));
292 VERIFY(little_align_int64.data() == reinterpret_cast<const unsigned char *>(&little_align_int64));
294 VERIFY(little_align_uint16.data() == reinterpret_cast<const unsigned char *>(&little_align_uint16));
295 VERIFY(little_align_uint32.data() == reinterpret_cast<const unsigned char *>(&little_align_uint32));
296 VERIFY(little_align_uint64.data() == reinterpret_cast<const unsigned char *>(&little_align_uint64));
300 // check_size ------------------------------------------------------------//
304 VERIFY( numeric_limits<signed char>::digits == 7 );
305 VERIFY( numeric_limits<unsigned char>::digits == 8 );
307 VERIFY_SIZE( sizeof( big_int8_t ), 1 );
308 VERIFY_SIZE( sizeof( big_int16_t ), 2 );
309 VERIFY_SIZE( sizeof( big_int24_t ), 3 );
310 VERIFY_SIZE( sizeof( big_int32_t ), 4 );
311 VERIFY_SIZE( sizeof( big_int40_t ), 5 );
312 VERIFY_SIZE( sizeof( big_int48_t ), 6 );
313 VERIFY_SIZE( sizeof( big_int56_t ), 7 );
314 VERIFY_SIZE( sizeof( big_int64_t ), 8 );
316 VERIFY_SIZE( sizeof( big_uint8_t ), 1 );
317 VERIFY_SIZE( sizeof( big_uint16_t ), 2 );
318 VERIFY_SIZE( sizeof( big_uint24_t ), 3 );
319 VERIFY_SIZE( sizeof( big_uint32_t ), 4 );
320 VERIFY_SIZE( sizeof( big_uint40_t ), 5 );
321 VERIFY_SIZE( sizeof( big_uint48_t ), 6 );
322 VERIFY_SIZE( sizeof( big_uint56_t ), 7 );
323 VERIFY_SIZE( sizeof( big_uint64_t ), 8 );
325 VERIFY_SIZE( sizeof( little_int8_t ), 1 );
326 VERIFY_SIZE( sizeof( little_int16_t ), 2 );
327 VERIFY_SIZE( sizeof( little_int24_t ), 3 );
328 VERIFY_SIZE( sizeof( little_int32_t ), 4 );
329 VERIFY_SIZE( sizeof( little_int40_t ), 5 );
330 VERIFY_SIZE( sizeof( little_int48_t ), 6 );
331 VERIFY_SIZE( sizeof( little_int56_t ), 7 );
332 VERIFY_SIZE( sizeof( little_int64_t ), 8 );
334 VERIFY_SIZE( sizeof( little_uint8_t ), 1 );
335 VERIFY_SIZE( sizeof( little_uint16_t ), 2 );
336 VERIFY_SIZE( sizeof( little_uint24_t ), 3 );
337 VERIFY_SIZE( sizeof( little_uint32_t ), 4 );
338 VERIFY_SIZE( sizeof( little_uint40_t ), 5 );
339 VERIFY_SIZE( sizeof( little_uint48_t ), 6 );
340 VERIFY_SIZE( sizeof( little_uint56_t ), 7 );
341 VERIFY_SIZE( sizeof( little_uint64_t ), 8 );
343 VERIFY_SIZE( sizeof( native_int8_t ), 1 );
344 VERIFY_SIZE( sizeof( native_int16_t ), 2 );
345 VERIFY_SIZE( sizeof( native_int24_t ), 3 );
346 VERIFY_SIZE( sizeof( native_int32_t ), 4 );
347 VERIFY_SIZE( sizeof( native_int40_t ), 5 );
348 VERIFY_SIZE( sizeof( native_int48_t ), 6 );
349 VERIFY_SIZE( sizeof( native_int56_t ), 7 );
350 VERIFY_SIZE( sizeof( native_int64_t ), 8 );
352 VERIFY_SIZE( sizeof( native_uint8_t ), 1 );
353 VERIFY_SIZE( sizeof( native_uint16_t ), 2 );
354 VERIFY_SIZE( sizeof( native_uint24_t ), 3 );
355 VERIFY_SIZE( sizeof( native_uint32_t ), 4 );
356 VERIFY_SIZE( sizeof( native_uint40_t ), 5 );
357 VERIFY_SIZE( sizeof( native_uint48_t ), 6 );
358 VERIFY_SIZE( sizeof( native_uint56_t ), 7 );
359 VERIFY_SIZE( sizeof( native_uint64_t ), 8 );
361 VERIFY_SIZE(sizeof(big_int8_at), 1);
362 VERIFY_SIZE(sizeof(big_int16_at), 2);
363 VERIFY_SIZE( sizeof( big_int32_at ), 4 );
364 VERIFY_SIZE( sizeof( big_int64_at ), 8 );
366 VERIFY_SIZE(sizeof(big_uint8_at), 1);
367 VERIFY_SIZE(sizeof(big_uint16_at), 2);
368 VERIFY_SIZE( sizeof( big_uint32_at ), 4 );
369 VERIFY_SIZE( sizeof( big_uint64_at ), 8 );
371 VERIFY_SIZE(sizeof(little_int8_at), 1);
372 VERIFY_SIZE(sizeof(little_int16_at), 2);
373 VERIFY_SIZE( sizeof( little_int32_at ), 4 );
374 VERIFY_SIZE( sizeof( little_int64_at ), 8 );
376 VERIFY_SIZE(sizeof(little_uint8_at), 1);
377 VERIFY_SIZE(sizeof(little_uint16_at), 2);
378 VERIFY_SIZE( sizeof( little_uint32_at ), 4 );
379 VERIFY_SIZE( sizeof( little_uint64_at ), 8 );
382 // check_alignment -------------------------------------------------------//
384 void check_alignment()
386 // structs with offsets % 2 == 1 for type of size > 1 to ensure no alignment
387 // bytes added for any size > 1
434 struct little_u_struct
464 struct native_u_struct
479 // aligned test cases
481 struct big_aligned_struct
486 // on a 32-bit system, the padding here may be 3 rather than 7 bytes
490 struct little_aligned_struct
495 // on a 32-bit system, the padding here may be 3 rather than 7 bytes
499 int saved_err_count = err_count;
501 VERIFY_SIZE( sizeof(big_struct), 39 );
502 VERIFY_SIZE( sizeof(big_u_struct), 39 );
503 VERIFY_SIZE( sizeof(little_struct), 39 );
504 VERIFY_SIZE( sizeof(little_u_struct), 39 );
505 VERIFY_SIZE( sizeof(native_struct), 39 );
506 VERIFY_SIZE( sizeof(native_u_struct), 39 );
507 VERIFY( sizeof(big_aligned_struct) <= 24 );
508 VERIFY( sizeof(little_aligned_struct) <= 24 );
510 if ( saved_err_count == err_count )
513 "Size and alignment for structures of endian types are as expected.\n";
517 // check_representation_and_range_and_ops --------------------------------//
519 void check_representation_and_range_and_ops()
521 // unaligned integer types
522 VERIFY_BIG_REPRESENTATION( big_int8_t );
523 VERIFY_VALUE_AND_OPS( big_int8_t, int_least8_t, 0x7e );
524 VERIFY_VALUE_AND_OPS( big_int8_t, int_least8_t, -0x80 );
526 VERIFY_BIG_REPRESENTATION( big_int16_t );
527 VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t, 0x7ffe );
528 VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t, -0x8000 );
530 VERIFY_BIG_REPRESENTATION( big_int24_t );
531 VERIFY_VALUE_AND_OPS( big_int24_t, int_least32_t, 0x7ffffe );
532 VERIFY_VALUE_AND_OPS( big_int24_t, int_least32_t, -0x800000 );
534 VERIFY_BIG_REPRESENTATION( big_int32_t );
535 VERIFY_VALUE_AND_OPS( big_int32_t, int_least32_t, 0x7ffffffe );
536 VERIFY_VALUE_AND_OPS( big_int32_t, int_least32_t, -0x7fffffff-1 );
538 VERIFY_BIG_REPRESENTATION( big_int40_t );
539 VERIFY_VALUE_AND_OPS( big_int40_t, int_least64_t, 0x7ffffffffeLL );
540 VERIFY_VALUE_AND_OPS( big_int40_t, int_least64_t, -0x8000000000LL );
542 VERIFY_BIG_REPRESENTATION( big_int48_t );
543 VERIFY_VALUE_AND_OPS( big_int48_t, int_least64_t, 0x7ffffffffffeLL );
544 VERIFY_VALUE_AND_OPS( big_int48_t, int_least64_t, -0x800000000000LL );
546 VERIFY_BIG_REPRESENTATION( big_int56_t );
547 VERIFY_VALUE_AND_OPS( big_int56_t, int_least64_t, 0x7ffffffffffffeLL );
548 VERIFY_VALUE_AND_OPS( big_int56_t, int_least64_t, -0x80000000000000LL );
550 VERIFY_BIG_REPRESENTATION( big_int64_t );
551 VERIFY_VALUE_AND_OPS( big_int64_t, int_least64_t, 0x7ffffffffffffffeLL );
552 VERIFY_VALUE_AND_OPS( big_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );
554 VERIFY_BIG_REPRESENTATION( big_uint8_t );
555 VERIFY_VALUE_AND_OPS( big_uint8_t, uint_least8_t, 0xff );
557 VERIFY_BIG_REPRESENTATION( big_uint16_t );
558 VERIFY_VALUE_AND_OPS( big_uint16_t, uint_least16_t, 0xffff );
560 VERIFY_BIG_REPRESENTATION( big_uint24_t );
561 VERIFY_VALUE_AND_OPS( big_uint24_t, uint_least32_t, 0xffffff );
563 VERIFY_BIG_REPRESENTATION( big_uint32_t );
564 VERIFY_VALUE_AND_OPS( big_uint32_t, uint_least32_t, 0xffffffff );
566 VERIFY_BIG_REPRESENTATION( big_uint40_t );
567 VERIFY_VALUE_AND_OPS( big_uint40_t, uint_least64_t, 0xffffffffffLL );
569 VERIFY_BIG_REPRESENTATION( big_uint48_t );
570 VERIFY_VALUE_AND_OPS( big_uint48_t, uint_least64_t, 0xffffffffffffLL );
572 VERIFY_BIG_REPRESENTATION( big_uint56_t );
573 VERIFY_VALUE_AND_OPS( big_uint56_t, uint_least64_t, 0xffffffffffffffLL );
575 VERIFY_BIG_REPRESENTATION( big_uint64_t );
576 VERIFY_VALUE_AND_OPS( big_uint64_t, uint_least64_t, 0xffffffffffffffffULL );
578 VERIFY_LITTLE_REPRESENTATION( little_int8_t );
579 VERIFY_VALUE_AND_OPS( little_int8_t, int_least8_t, 0x7e );
580 VERIFY_VALUE_AND_OPS( little_int8_t, int_least8_t, -0x80 );
582 VERIFY_LITTLE_REPRESENTATION( little_int16_t );
583 VERIFY_VALUE_AND_OPS( little_int16_t, int_least16_t, 0x7ffe );
584 VERIFY_VALUE_AND_OPS( little_int16_t, int_least16_t, -0x8000 );
586 VERIFY_LITTLE_REPRESENTATION( little_int24_t );
587 VERIFY_VALUE_AND_OPS( little_int24_t, int_least32_t, 0x7ffffe );
588 VERIFY_VALUE_AND_OPS( little_int24_t, int_least32_t, -0x800000 );
590 VERIFY_LITTLE_REPRESENTATION( little_int32_t );
591 VERIFY_VALUE_AND_OPS( little_int32_t, int_least32_t, 0x7ffffffe );
592 VERIFY_VALUE_AND_OPS( little_int32_t, int_least32_t, -0x7fffffff-1 );
594 VERIFY_LITTLE_REPRESENTATION( little_int40_t );
595 VERIFY_VALUE_AND_OPS( little_int40_t, int_least64_t, 0x7ffffffffeLL );
596 VERIFY_VALUE_AND_OPS( little_int40_t, int_least64_t, -0x8000000000LL );
598 VERIFY_LITTLE_REPRESENTATION( little_int48_t );
599 VERIFY_VALUE_AND_OPS( little_int48_t, int_least64_t, 0x7ffffffffffeLL );
600 VERIFY_VALUE_AND_OPS( little_int48_t, int_least64_t, -0x800000000000LL );
602 VERIFY_LITTLE_REPRESENTATION( little_int56_t );
603 VERIFY_VALUE_AND_OPS( little_int56_t, int_least64_t, 0x7ffffffffffffeLL );
604 VERIFY_VALUE_AND_OPS( little_int56_t, int_least64_t, -0x80000000000000LL );
606 VERIFY_LITTLE_REPRESENTATION( little_int64_t );
607 VERIFY_VALUE_AND_OPS( little_int64_t, int_least64_t, 0x7ffffffffffffffeLL );
608 VERIFY_VALUE_AND_OPS( little_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );
610 VERIFY_LITTLE_REPRESENTATION( little_uint8_t );
611 VERIFY_VALUE_AND_OPS( little_uint8_t, uint_least8_t, 0xff );
613 VERIFY_LITTLE_REPRESENTATION( little_uint16_t );
614 VERIFY_VALUE_AND_OPS( little_uint16_t, uint_least16_t, 0xffff );
616 VERIFY_LITTLE_REPRESENTATION( little_uint24_t );
617 VERIFY_VALUE_AND_OPS( little_uint24_t, uint_least32_t, 0xffffff );
619 VERIFY_LITTLE_REPRESENTATION( little_uint32_t );
620 VERIFY_VALUE_AND_OPS( little_uint32_t, uint_least32_t, 0xffffffff );
622 VERIFY_LITTLE_REPRESENTATION( little_uint40_t );
623 VERIFY_VALUE_AND_OPS( little_uint40_t, uint_least64_t, 0xffffffffffLL );
625 VERIFY_LITTLE_REPRESENTATION( little_uint48_t );
626 VERIFY_VALUE_AND_OPS( little_uint48_t, uint_least64_t, 0xffffffffffffLL );
628 VERIFY_LITTLE_REPRESENTATION( little_uint56_t );
629 VERIFY_VALUE_AND_OPS( little_uint56_t, uint_least64_t, 0xffffffffffffffLL );
631 VERIFY_LITTLE_REPRESENTATION( little_uint64_t );
632 VERIFY_VALUE_AND_OPS( little_uint64_t, uint_least64_t, 0xffffffffffffffffULL );
634 VERIFY_NATIVE_REPRESENTATION( native_int8_t );
635 VERIFY_VALUE_AND_OPS( native_int8_t, int_least8_t, 0x7e );
636 VERIFY_VALUE_AND_OPS( native_int8_t, int_least8_t, -0x80 );
638 VERIFY_NATIVE_REPRESENTATION( native_int16_t );
639 VERIFY_VALUE_AND_OPS( native_int16_t, int_least16_t, 0x7ffe );
640 VERIFY_VALUE_AND_OPS( native_int16_t, int_least16_t, -0x8000 );
642 VERIFY_NATIVE_REPRESENTATION( native_int24_t );
643 VERIFY_VALUE_AND_OPS( native_int24_t, int_least32_t, 0x7ffffe );
644 VERIFY_VALUE_AND_OPS( native_int24_t, int_least32_t, -0x800000 );
646 VERIFY_NATIVE_REPRESENTATION( native_int32_t );
647 VERIFY_VALUE_AND_OPS( native_int32_t, int_least32_t, 0x7ffffffe );
648 VERIFY_VALUE_AND_OPS( native_int32_t, int_least32_t, -0x7fffffff-1 );
650 VERIFY_NATIVE_REPRESENTATION( native_int40_t );
651 VERIFY_VALUE_AND_OPS( native_int40_t, int_least64_t, 0x7ffffffffeLL );
652 VERIFY_VALUE_AND_OPS( native_int40_t, int_least64_t, -0x8000000000LL );
654 VERIFY_NATIVE_REPRESENTATION( native_int48_t );
655 VERIFY_VALUE_AND_OPS( native_int48_t, int_least64_t, 0x7ffffffffffeLL );
656 VERIFY_VALUE_AND_OPS( native_int48_t, int_least64_t, -0x800000000000LL );
658 VERIFY_NATIVE_REPRESENTATION( native_int56_t );
659 VERIFY_VALUE_AND_OPS( native_int56_t, int_least64_t, 0x7ffffffffffffeLL );
660 VERIFY_VALUE_AND_OPS( native_int56_t, int_least64_t, -0x80000000000000LL );
662 VERIFY_NATIVE_REPRESENTATION( native_int64_t );
663 VERIFY_VALUE_AND_OPS( native_int64_t, int_least64_t, 0x7ffffffffffffffeLL );
664 VERIFY_VALUE_AND_OPS( native_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );
666 VERIFY_NATIVE_REPRESENTATION( native_uint8_t );
667 VERIFY_VALUE_AND_OPS( native_uint8_t, uint_least8_t, 0xff );
669 VERIFY_NATIVE_REPRESENTATION( native_uint16_t );
670 VERIFY_VALUE_AND_OPS( native_uint16_t, uint_least16_t, 0xffff );
672 VERIFY_NATIVE_REPRESENTATION( native_uint24_t );
673 VERIFY_VALUE_AND_OPS( native_uint24_t, uint_least32_t, 0xffffff );
675 VERIFY_NATIVE_REPRESENTATION( native_uint32_t );
676 VERIFY_VALUE_AND_OPS( native_uint32_t, uint_least32_t, 0xffffffff );
678 VERIFY_NATIVE_REPRESENTATION( native_uint40_t );
679 VERIFY_VALUE_AND_OPS( native_uint40_t, uint_least64_t, 0xffffffffffLL );
681 VERIFY_NATIVE_REPRESENTATION( native_uint48_t );
682 VERIFY_VALUE_AND_OPS( native_uint48_t, uint_least64_t, 0xffffffffffffLL );
684 VERIFY_NATIVE_REPRESENTATION( native_uint56_t );
685 VERIFY_VALUE_AND_OPS( native_uint56_t, uint_least64_t, 0xffffffffffffffLL );
687 VERIFY_NATIVE_REPRESENTATION( native_uint64_t );
688 VERIFY_VALUE_AND_OPS( native_uint64_t, uint_least64_t, 0xffffffffffffffffULL );
690 // aligned integer types
691 VERIFY_BIG_REPRESENTATION( big_int16_at );
692 VERIFY_VALUE_AND_OPS( big_int16_at, int_least16_t, 0x7ffe );
693 VERIFY_VALUE_AND_OPS( big_int16_at, int_least16_t, -0x8000 );
695 VERIFY_BIG_REPRESENTATION( big_int32_at );
696 VERIFY_VALUE_AND_OPS( big_int32_at, int_least32_t, 0x7ffffffe );
697 VERIFY_VALUE_AND_OPS( big_int32_at, int_least32_t, -0x7fffffff-1 );
699 VERIFY_BIG_REPRESENTATION( big_int64_at );
700 VERIFY_VALUE_AND_OPS( big_int64_at, int_least64_t, 0x7ffffffffffffffeLL );
701 VERIFY_VALUE_AND_OPS( big_int64_at, int_least64_t, -0x7fffffffffffffffLL-1 );
703 VERIFY_BIG_REPRESENTATION( big_uint16_at );
704 VERIFY_VALUE_AND_OPS( big_uint16_at, uint_least16_t, 0xffff );
706 VERIFY_BIG_REPRESENTATION( big_uint32_at );
707 VERIFY_VALUE_AND_OPS( big_uint32_at, uint_least32_t, 0xffffffff );
709 VERIFY_BIG_REPRESENTATION( big_uint64_at );
710 VERIFY_VALUE_AND_OPS( big_uint64_at, uint_least64_t, 0xffffffffffffffffULL );
712 VERIFY_LITTLE_REPRESENTATION( little_int16_at );
713 VERIFY_VALUE_AND_OPS( little_int16_at, int_least16_t, 0x7ffe );
714 VERIFY_VALUE_AND_OPS( little_int16_at, int_least16_t, -0x8000 );
716 VERIFY_LITTLE_REPRESENTATION( little_int32_at );
717 VERIFY_VALUE_AND_OPS( little_int32_at, int_least32_t, 0x7ffffffe );
718 VERIFY_VALUE_AND_OPS( little_int32_at, int_least32_t, -0x7fffffff-1 );
720 VERIFY_LITTLE_REPRESENTATION( little_int64_at );
721 VERIFY_VALUE_AND_OPS( little_int64_at, int_least64_t, 0x7ffffffffffffffeLL );
722 VERIFY_VALUE_AND_OPS( little_int64_at, int_least64_t, -0x7fffffffffffffffLL-1 );
724 VERIFY_LITTLE_REPRESENTATION( little_uint16_at );
725 VERIFY_VALUE_AND_OPS( little_uint16_at, uint_least16_t, 0xffff );
727 VERIFY_LITTLE_REPRESENTATION( little_uint32_at );
728 VERIFY_VALUE_AND_OPS( little_uint32_at, uint_least32_t, 0xffffffff );
730 VERIFY_LITTLE_REPRESENTATION( little_uint64_at );
731 VERIFY_VALUE_AND_OPS( little_uint64_at, uint_least64_t, 0xffffffffffffffffULL );
733 } // check_representation_and_range
741 MyInt(int32_t x = 0) : mx(x) {}
742 operator int32_t() const {return mx;}
744 //friend int32_t operator+(const MyInt& x) {return x;}
749 typedef boost::endian::endian_arithmetic< order::big, MyInt, 32 > mybig_int32_ut;
751 mybig_int32_ut v(10);
752 cout << "+v is " << +v << endl;
754 cout << "v is " << +v << endl;
756 cout << "v is " << +v << endl;
758 cout << "v is " << +v << endl;
760 cout << "v is " << +v << endl;
762 cout << "v is " << +v << endl;
763 // cout << "v+v is " << +(v+v) << endl;
768 typedef boost::endian::endian_arithmetic< order::little, MyInt, 32 > mylittle_int32_ut;
770 mylittle_int32_ut v(10);
771 cout << "+v is " << +v << endl;
773 cout << "v is " << +v << endl;
775 cout << "v is " << +v << endl;
777 cout << "v is " << +v << endl;
779 cout << "v is " << +v << endl;
781 cout << "v is " << +v << endl;
782 // cout << "v+v is " << +(v+v) << endl;
787 long iterations = 10000;
789 template< class Endian >
790 Endian timing_test( const char * s)
792 cout << s << " timing test, " << iterations << " iterations: ";
796 for ( long i = 0; i < iterations; ++i )
802 if ( i == 0 ) VERIFY_VALUE_AND_OPS( Endian, typename Endian::value_type, 21 );
807 } // unnamed namespace
809 // main ------------------------------------------------------------------------------//
811 int cpp_main( int argc, char * argv[] )
814 << argv[0] << " [#],\n where # specifies iteration count\n"
815 " default iteration count is " << iterations << endl;
818 iterations = atol( argv[1] );
819 if ( iterations < 1 ) iterations = 1;
824 check_representation_and_range_and_ops();
829 //timing_test<big_int32_t> ( "big_int32_t" );
830 //timing_test<big_int32_at>( "big_int32_at" );
831 //timing_test<little_int32_t> ( "little_int32_t" );
832 //timing_test<little_int32_at>( "little_int32_at" );
834 cout << "\n" << err_count << " errors detected\nTest "
835 << (err_count==0 ? "passed\n\n" : "failed\n\n");
837 return err_count ? 1 : 0;