Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / endian / test / endian_test.cpp
1 //  endian_test.cpp  ---------------------------------------------------------//
2
3 //  Copyright Beman Dawes 1999-2008
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 //  This test probes for correct endianness, size, and value.
13
14 //  See endian_operations_test for tests of operator correctness and interaction
15 //  between operand types.
16
17 //----------------------------------------------------------------------------//
18
19 #include <boost/endian/detail/disable_warnings.hpp>
20
21 #include <boost/endian/arithmetic.hpp>
22 #include <boost/cstdint.hpp>
23 #include <boost/detail/lightweight_main.hpp>
24
25 #include <iostream>
26 #include <limits>
27 #include <climits>
28 #include <cstdlib>    // for atoi(), exit()
29 #include <cstring>    // for memcmp()
30
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".
35
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__ )
42
43 namespace
44 {
45   int err_count;
46
47   void verify( bool x, int line )
48   {
49     if ( x ) return;
50     ++err_count;
51     cout << "Error: verify failed on line " << line << endl;
52   }
53
54   void verify_size( size_t actual, size_t expected, int line )
55   {
56     if ( actual == expected ) return;
57     ++err_count;
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";
62   }
63
64   template <class Endian, class Base>
65   void verify_value_and_ops( const Base & expected, int line )
66   {
67     Endian v( expected );
68     verify( v == expected, line );
69
70     Endian v2;
71     v2.operator=( expected );
72     verify( v2 == expected, line );
73
74     ++v; // verify integer_cover_operators being applied to this type -
75          // will fail to compile if no endian<> specialization is present
76
77     Endian v3( static_cast<Base>( 1 ) );
78
79     Endian x(static_cast<typename Endian::value_type>(v2+v3));
80     if ( x == x ) // silence warning
81       return;
82   }
83
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";
86
87   template <class Endian>
88   void verify_representation( bool is_big, int line )
89   {
90     int silence = 0;
91     Endian x ( static_cast<typename Endian::value_type>
92       (0x123456789abcdef0LL + silence) ); // will truncate
93
94     if ( is_big )
95       verify( memcmp( &x,
96         reinterpret_cast<const char*>(big_rep)+8-sizeof(Endian),
97           sizeof(Endian) ) == 0, line );
98     else
99       verify( memcmp( &x, little_rep, sizeof(Endian) ) == 0, line );
100   }
101
102   template <class Endian>
103   inline void verify_native_representation( int line )
104   {
105 #   if BOOST_ENDIAN_BIG_BYTE
106       verify_representation<Endian>( true, line );
107 #   else
108       verify_representation<Endian>( false, line );
109 #   endif
110   }
111
112   //  detect_order  -----------------------------------------------------//
113
114   void detect_order()
115   {
116     union View
117     {
118       long long i;
119       unsigned char c[8];
120     };
121
122     View v = { 0x0102030405060708LL };  // initialize v.i
123
124     if ( memcmp( v.c, "\x8\7\6\5\4\3\2\1", 8) == 0 )
125     {
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";
131         exit(1);
132   #   endif
133     }
134     else if ( memcmp( v.c, "\1\2\3\4\5\6\7\x8", 8) == 0 )
135     {
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";
141         exit(1);
142   #   endif
143     }
144     else
145     {
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";
149       exit(1);
150     }
151     cout << "That should not matter and is presented for your information only.\n";
152   } // detect_order
153
154   //  check_data  ------------------------------------------------------------//
155
156   void check_data()
157   {
158     big_int8_t big_8;
159     big_int16_t big_16;
160     big_int24_t big_24;
161     big_int32_t big_32;
162     big_int40_t big_40;
163     big_int48_t big_48;
164     big_int56_t big_56;
165     big_int64_t big_64;
166
167     big_uint8_t big_u8;
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;
175
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;
184
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;
193
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;
202
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;
211
212     big_int16_at  big_align_int16;
213     big_int32_at  big_align_int32;
214     big_int64_at  big_align_int64;
215
216     big_uint16_at big_align_uint16;
217     big_uint32_at big_align_uint32;
218     big_uint64_at big_align_uint64;
219
220     little_int16_at  little_align_int16;
221     little_int32_at  little_align_int32;
222     little_int64_at  little_align_int64;
223
224     little_uint16_at little_align_uint16;
225     little_uint32_at little_align_uint32;
226     little_uint64_at little_align_uint64;
227
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));
236
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));
245
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));
254
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));
263
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));
272
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));
281
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));
285
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));
289
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));
293
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));
297
298   }
299
300   //  check_size  ------------------------------------------------------------//
301
302   void check_size()
303   {
304     VERIFY( numeric_limits<signed char>::digits == 7 );
305     VERIFY( numeric_limits<unsigned char>::digits == 8 );
306
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 );
315
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 );
324
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 );
333
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 );
342
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 );
351
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 );
360
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 );
365
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 );
370
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 );
375
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 );
380   } // check_size
381
382   //  check_alignment  -------------------------------------------------------//
383
384   void check_alignment()
385   {
386     // structs with offsets % 2 == 1 for type of size > 1 to ensure no alignment
387     // bytes added for any size > 1
388
389     struct big_struct
390     {
391       big_int8_t    v0;
392       big_int16_t    v1;
393       big_int24_t    v3;
394       char      v6;
395       big_int32_t    v7;
396       big_int40_t    v11;
397       char      v16;
398       big_int48_t    v17;
399       big_int56_t    v23;
400       char      v30;
401       big_int64_t    v31;
402     };
403
404     struct big_u_struct
405     {
406       big_uint8_t    v0;
407       big_uint16_t    v1;
408       big_uint24_t    v3;
409       char       v6;
410       big_uint32_t    v7;
411       big_uint40_t    v11;
412       char       v16;
413       big_uint48_t    v17;
414       big_uint56_t    v23;
415       char       v30;
416       big_uint64_t    v31;
417     };
418
419     struct little_struct
420     {
421       little_int8_t    v0;
422       little_int16_t    v1;
423       little_int24_t    v3;
424       char         v6;
425       little_int32_t    v7;
426       little_int40_t    v11;
427       char         v16;
428       little_int48_t    v17;
429       little_int56_t    v23;
430       char         v30;
431       little_int64_t    v31;
432     };
433
434     struct little_u_struct
435     {
436       little_uint8_t    v0;
437       little_uint16_t    v1;
438       little_uint24_t    v3;
439       char          v6;
440       little_uint32_t    v7;
441       little_uint40_t    v11;
442       char          v16;
443       little_uint48_t    v17;
444       little_uint56_t    v23;
445       char          v30;
446       little_uint64_t    v31;
447     };
448
449     struct native_struct
450     {
451       native_int8_t    v0;
452       native_int16_t    v1;
453       native_int24_t    v3;
454       char         v6;
455       native_int32_t    v7;
456       native_int40_t    v11;
457       char         v16;
458       native_int48_t    v17;
459       native_int56_t    v23;
460       char         v30;
461       native_int64_t    v31;
462     };
463
464     struct native_u_struct
465     {
466       native_uint8_t    v0;
467       native_uint16_t    v1;
468       native_uint24_t    v3;
469       char          v6;
470       native_uint32_t    v7;
471       native_uint40_t    v11;
472       char          v16;
473       native_uint48_t    v17;
474       native_uint56_t    v23;
475       char          v30;
476       native_uint64_t    v31;
477     };
478
479     //  aligned test cases
480
481     struct big_aligned_struct
482     {
483       big_int16_at    v0;
484       big_int32_at    v1;
485       char          v3;
486       // on a 32-bit system, the padding here may be 3 rather than 7 bytes
487       big_int64_at    v4;
488     };
489
490     struct little_aligned_struct
491     {
492       little_int16_at    v0;
493       little_int32_at    v1;
494       char          v3;
495       // on a 32-bit system, the padding here may be 3 rather than 7 bytes
496       little_int64_at    v4;
497     };
498
499     int saved_err_count = err_count;
500
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 );
509
510     if ( saved_err_count == err_count )
511     {
512       cout <<
513         "Size and alignment for structures of endian types are as expected.\n";
514     }
515   } // check_alignment
516
517   //  check_representation_and_range_and_ops  --------------------------------//
518
519   void check_representation_and_range_and_ops()
520   {
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 );
525
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 );
529
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 );
533
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 );
537
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 );
541
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 );
545
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 );
549
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 );
553
554     VERIFY_BIG_REPRESENTATION( big_uint8_t );
555     VERIFY_VALUE_AND_OPS( big_uint8_t, uint_least8_t,  0xff );
556
557     VERIFY_BIG_REPRESENTATION( big_uint16_t );
558     VERIFY_VALUE_AND_OPS( big_uint16_t, uint_least16_t, 0xffff );
559
560     VERIFY_BIG_REPRESENTATION( big_uint24_t );
561     VERIFY_VALUE_AND_OPS( big_uint24_t, uint_least32_t, 0xffffff );
562
563     VERIFY_BIG_REPRESENTATION( big_uint32_t );
564     VERIFY_VALUE_AND_OPS( big_uint32_t, uint_least32_t, 0xffffffff );
565
566     VERIFY_BIG_REPRESENTATION( big_uint40_t );
567     VERIFY_VALUE_AND_OPS( big_uint40_t, uint_least64_t, 0xffffffffffLL );
568
569     VERIFY_BIG_REPRESENTATION( big_uint48_t );
570     VERIFY_VALUE_AND_OPS( big_uint48_t, uint_least64_t, 0xffffffffffffLL );
571
572     VERIFY_BIG_REPRESENTATION( big_uint56_t );
573     VERIFY_VALUE_AND_OPS( big_uint56_t, uint_least64_t, 0xffffffffffffffLL );
574
575     VERIFY_BIG_REPRESENTATION( big_uint64_t );
576     VERIFY_VALUE_AND_OPS( big_uint64_t, uint_least64_t, 0xffffffffffffffffULL );
577
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 );
581
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 );
585
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 );
589
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 );
593
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 );
597
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 );
601
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 );
605
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 );
609
610     VERIFY_LITTLE_REPRESENTATION( little_uint8_t );
611     VERIFY_VALUE_AND_OPS( little_uint8_t, uint_least8_t, 0xff );
612
613     VERIFY_LITTLE_REPRESENTATION( little_uint16_t );
614     VERIFY_VALUE_AND_OPS( little_uint16_t, uint_least16_t, 0xffff );
615
616     VERIFY_LITTLE_REPRESENTATION( little_uint24_t );
617     VERIFY_VALUE_AND_OPS( little_uint24_t, uint_least32_t, 0xffffff );
618
619     VERIFY_LITTLE_REPRESENTATION( little_uint32_t );
620     VERIFY_VALUE_AND_OPS( little_uint32_t, uint_least32_t, 0xffffffff );
621
622     VERIFY_LITTLE_REPRESENTATION( little_uint40_t );
623     VERIFY_VALUE_AND_OPS( little_uint40_t, uint_least64_t, 0xffffffffffLL );
624
625     VERIFY_LITTLE_REPRESENTATION( little_uint48_t );
626     VERIFY_VALUE_AND_OPS( little_uint48_t, uint_least64_t, 0xffffffffffffLL );
627
628     VERIFY_LITTLE_REPRESENTATION( little_uint56_t );
629     VERIFY_VALUE_AND_OPS( little_uint56_t, uint_least64_t, 0xffffffffffffffLL );
630
631     VERIFY_LITTLE_REPRESENTATION( little_uint64_t );
632     VERIFY_VALUE_AND_OPS( little_uint64_t, uint_least64_t, 0xffffffffffffffffULL );
633
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 );
637
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 );
641
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 );
645
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 );
649
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 );
653
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 );
657
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 );
661
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 );
665
666     VERIFY_NATIVE_REPRESENTATION( native_uint8_t );
667     VERIFY_VALUE_AND_OPS( native_uint8_t, uint_least8_t, 0xff );
668
669     VERIFY_NATIVE_REPRESENTATION( native_uint16_t );
670     VERIFY_VALUE_AND_OPS( native_uint16_t, uint_least16_t, 0xffff );
671
672     VERIFY_NATIVE_REPRESENTATION( native_uint24_t );
673     VERIFY_VALUE_AND_OPS( native_uint24_t, uint_least32_t, 0xffffff );
674
675     VERIFY_NATIVE_REPRESENTATION( native_uint32_t );
676     VERIFY_VALUE_AND_OPS( native_uint32_t, uint_least32_t, 0xffffffff );
677
678     VERIFY_NATIVE_REPRESENTATION( native_uint40_t );
679     VERIFY_VALUE_AND_OPS( native_uint40_t, uint_least64_t, 0xffffffffffLL );
680
681     VERIFY_NATIVE_REPRESENTATION( native_uint48_t );
682     VERIFY_VALUE_AND_OPS( native_uint48_t, uint_least64_t, 0xffffffffffffLL );
683
684     VERIFY_NATIVE_REPRESENTATION( native_uint56_t );
685     VERIFY_VALUE_AND_OPS( native_uint56_t, uint_least64_t, 0xffffffffffffffLL );
686
687     VERIFY_NATIVE_REPRESENTATION( native_uint64_t );
688     VERIFY_VALUE_AND_OPS( native_uint64_t, uint_least64_t, 0xffffffffffffffffULL );
689
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 );
694
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 );
698
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 );
702
703     VERIFY_BIG_REPRESENTATION( big_uint16_at );
704     VERIFY_VALUE_AND_OPS( big_uint16_at, uint_least16_t, 0xffff );
705
706     VERIFY_BIG_REPRESENTATION( big_uint32_at );
707     VERIFY_VALUE_AND_OPS( big_uint32_at, uint_least32_t, 0xffffffff );
708
709     VERIFY_BIG_REPRESENTATION( big_uint64_at );
710     VERIFY_VALUE_AND_OPS( big_uint64_at, uint_least64_t, 0xffffffffffffffffULL );
711
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 );
715
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 );
719
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 );
723
724     VERIFY_LITTLE_REPRESENTATION( little_uint16_at );
725     VERIFY_VALUE_AND_OPS( little_uint16_at, uint_least16_t, 0xffff );
726
727     VERIFY_LITTLE_REPRESENTATION( little_uint32_at );
728     VERIFY_VALUE_AND_OPS( little_uint32_at, uint_least32_t, 0xffffffff );
729
730     VERIFY_LITTLE_REPRESENTATION( little_uint64_at );
731     VERIFY_VALUE_AND_OPS( little_uint64_at, uint_least64_t, 0xffffffffffffffffULL );
732
733   } // check_representation_and_range
734
735 /*
736
737   class MyInt
738   {
739     int32_t mx;
740   public:
741     MyInt(int32_t x = 0) : mx(x) {}
742     operator int32_t() const {return mx;}
743
744     //friend int32_t operator+(const MyInt& x) {return x;}
745   };
746
747   void check_udt()
748   {
749     typedef boost::endian::endian_arithmetic< order::big, MyInt, 32 >  mybig_int32_ut;
750
751     mybig_int32_ut v(10);
752     cout << "+v is " << +v << endl;
753     v += 1;
754     cout << "v is " << +v << endl;
755     v -= 2;
756     cout << "v is " << +v << endl;
757     v *= 2;
758     cout << "v is " << +v << endl;
759     ++v;
760     cout << "v is " << +v << endl;
761     --v;
762     cout << "v is " << +v << endl;
763 //    cout << "v+v is " << +(v+v) << endl;
764   }
765
766   void check_udt_le()
767   {
768     typedef boost::endian::endian_arithmetic< order::little, MyInt, 32 >  mylittle_int32_ut;
769
770     mylittle_int32_ut v(10);
771     cout << "+v is " << +v << endl;
772     v += 1;
773     cout << "v is " << +v << endl;
774     v -= 2;
775     cout << "v is " << +v << endl;
776     v *= 2;
777     cout << "v is " << +v << endl;
778     ++v;
779     cout << "v is " << +v << endl;
780     --v;
781     cout << "v is " << +v << endl;
782 //    cout << "v+v is " << +(v+v) << endl;
783   }
784
785 */
786
787   long iterations = 10000;
788
789   template< class Endian >
790   Endian timing_test( const char * s)
791   {
792     cout << s << " timing test, " << iterations << " iterations: ";
793 //    progress_timer t;
794
795     Endian v = 1;
796     for ( long i = 0; i < iterations; ++i )
797     {
798       v += 1;
799       v *= 3;
800       ++v;
801       v *= i;
802       if ( i == 0 ) VERIFY_VALUE_AND_OPS( Endian, typename Endian::value_type, 21 );
803     }
804     return v;
805   }
806
807 } // unnamed namespace
808
809 //  main  ------------------------------------------------------------------------------//
810
811 int cpp_main( int argc, char * argv[] )
812 {
813   cout << "Usage: "
814        << argv[0] << " [#],\n where # specifies iteration count\n"
815           " default iteration count is " << iterations << endl;
816
817   if ( argc > 1 )
818     iterations = atol( argv[1] );
819   if ( iterations < 1 ) iterations = 1;
820
821   detect_order();
822   check_size();
823   check_alignment();
824   check_representation_and_range_and_ops();
825   check_data();
826   //check_udt();
827   //check_udt_le();
828
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" );
833
834   cout << "\n" << err_count << " errors detected\nTest "
835        << (err_count==0 ? "passed\n\n" : "failed\n\n");
836
837   return err_count ? 1 : 0;
838 } // main