boost_endian 0.1.0

Boost C++ library boost_endian packaged using Zanbil
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//  endian_test.cpp  ---------------------------------------------------------//

//  Copyright Beman Dawes 1999-2008

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  See library home page at http://www.boost.org/libs/endian

//----------------------------------------------------------------------------//

//  This test probes for correct endianness, size, and value.

//  See endian_operations_test for tests of operator correctness and interaction
//  between operand types.

//----------------------------------------------------------------------------//

#include <boost/endian/detail/disable_warnings.hpp>

#include <boost/endian/arithmetic.hpp>
#include <boost/cstdint.hpp>

#include <iostream>
#include <limits>
#include <climits>
#include <cstdlib>    // for atoi(), exit()
#include <cstring>    // for memcmp()

#if defined(_MSC_VER)
# pragma warning(disable: 4127)  // conditional expression is constant
#endif

using namespace std;             // Not the best programming practice, but I
using namespace boost;           //   want to verify this combination of using
using namespace boost::endian;   //   namespaces works. See endian_operations_test
//                               //   for tests that don't do "using namespace".

#define VERIFY(predicate) verify( predicate, __LINE__ )
#define VERIFY_SIZE(actual, expected) verify_size( actual, expected, __LINE__ )
#define VERIFY_VALUE_AND_OPS(endian_t,expected_t,expected) verify_value_and_ops<endian_t, expected_t>( expected, __LINE__ )
#define VERIFY_BIG_REPRESENTATION(t) verify_representation<t>( true, __LINE__ )
#define VERIFY_LITTLE_REPRESENTATION(t) verify_representation<t>( false, __LINE__ )
#define VERIFY_NATIVE_REPRESENTATION(t) verify_native_representation<t>( __LINE__ )

namespace
{
  int err_count;

  void verify( bool x, int line )
  {
    if ( x ) return;
    ++err_count;
    cout << "Error: verify failed on line " << line << endl;
  }

  void verify_size( size_t actual, size_t expected, int line )
  {
    if ( actual == expected ) return;
    ++err_count;
    cout << "Error: verify size failed on line " << line << endl;
    cout << " A structure with an expected sizeof() " << expected
         << " had an actual sizeof() " << actual
         << "\n This will cause uses of endian types to fail\n";
  }

  template <class Endian, class Base>
  void verify_value_and_ops( const Base & expected, int line )
  {
    Endian v( expected );
    verify( v == expected, line );

    Endian v2;
    v2.operator=( expected );
    verify( v2 == expected, line );

    ++v; // verify integer_cover_operators being applied to this type -
         // will fail to compile if no endian<> specialization is present

    Endian v3( static_cast<Base>( 1 ) );

    Endian x(static_cast<typename Endian::value_type>(v2+v3));
    if ( x == x ) // silence warning
      return;
  }

  const char * big_rep    = "\x12\x34\x56\x78\x9A\xBC\xDE\xF0";
  const char * little_rep = "\xF0\xDE\xBC\x9A\x78\x56\x34\x12";

  template <class Endian>
  void verify_representation( bool is_big, int line )
  {
    int silence = 0;
    Endian x ( static_cast<typename Endian::value_type>
      (0x123456789abcdef0LL + silence) ); // will truncate

    if ( is_big )
      verify( memcmp( &x,
        reinterpret_cast<const char*>(big_rep)+8-sizeof(Endian),
          sizeof(Endian) ) == 0, line );
    else
      verify( memcmp( &x, little_rep, sizeof(Endian) ) == 0, line );
  }

  template <class Endian>
  inline void verify_native_representation( int line )
  {
    verify_representation<Endian>( order::native == order::big, line );
  }

  //  detect_order  -----------------------------------------------------//

  void detect_order()
  {
    union View
    {
      long long i;
      unsigned char c[8];
    };

    View v = { 0x0102030405060708LL };  // initialize v.i

    if ( memcmp( v.c, "\x8\7\6\5\4\3\2\1", 8) == 0 )
    {
      cout << "This machine is little-endian.\n";
      if( order::native != order::little )
      {
        cout << "yet boost::endian::order::native does not equal boost::endian::order::little.\n"
          "The Boost Endian library must be revised to work correctly on this system.\n"
          "Please report this problem to the Boost mailing list.\n";
        exit(1);
      }
    }
    else if ( memcmp( v.c, "\1\2\3\4\5\6\7\x8", 8) == 0 )
    {
      cout << "This machine is big-endian.\n";
      if( order::native != order::big )
      {
        cout << "yet boost::endian::order::native does not equal boost::endian::order::big.\n"
          "The Boost Endian library must be revised to work correctly on this system.\n"
          "Please report this problem to the Boost mailing list.\n";
        exit(1);
      }
    }
    else
    {
      cout << "This machine is neither strict big-endian nor strict little-endian\n"
        "The Boost Endian library must be revised to work correctly on this system.\n"
          "Please report this problem to the Boost mailing list.\n";
      exit(1);
    }
    cout << "That should not matter and is presented for your information only.\n";
  } // detect_order

  //  check_data  ------------------------------------------------------------//

  void check_data()
  {
    big_int8_t big_8;
    big_int16_t big_16;
    big_int24_t big_24;
    big_int32_t big_32;
    big_int40_t big_40;
    big_int48_t big_48;
    big_int56_t big_56;
    big_int64_t big_64;

    big_uint8_t big_u8;
    big_uint16_t big_u16;
    big_uint24_t big_u24;
    big_uint32_t big_u32;
    big_uint40_t big_u40;
    big_uint48_t big_u48;
    big_uint56_t big_u56;
    big_uint64_t big_u64;

    little_int8_t little_8;
    little_int16_t little_16;
    little_int24_t little_24;
    little_int32_t little_32;
    little_int40_t little_40;
    little_int48_t little_48;
    little_int56_t little_56;
    little_int64_t little_64;

    little_uint8_t little_u8;
    little_uint16_t little_u16;
    little_uint24_t little_u24;
    little_uint32_t little_u32;
    little_uint40_t little_u40;
    little_uint48_t little_u48;
    little_uint56_t little_u56;
    little_uint64_t little_u64;

    native_int8_t native_8;
    native_int16_t native_16;
    native_int24_t native_24;
    native_int32_t native_32;
    native_int40_t native_40;
    native_int48_t native_48;
    native_int56_t native_56;
    native_int64_t native_64;

    native_uint8_t native_u8;
    native_uint16_t native_u16;
    native_uint24_t native_u24;
    native_uint32_t native_u32;
    native_uint40_t native_u40;
    native_uint48_t native_u48;
    native_uint56_t native_u56;
    native_uint64_t native_u64;

    big_int16_at  big_align_int16;
    big_int32_at  big_align_int32;
    big_int64_at  big_align_int64;

    big_uint16_at big_align_uint16;
    big_uint32_at big_align_uint32;
    big_uint64_at big_align_uint64;

    little_int16_at  little_align_int16;
    little_int32_at  little_align_int32;
    little_int64_at  little_align_int64;

    little_uint16_at little_align_uint16;
    little_uint32_at little_align_uint32;
    little_uint64_at little_align_uint64;

    VERIFY(big_8.data() == reinterpret_cast<const unsigned char *>(&big_8));
    VERIFY(big_16.data() == reinterpret_cast<const unsigned char *>(&big_16));
    VERIFY(big_24.data() == reinterpret_cast<const unsigned char *>(&big_24));
    VERIFY(big_32.data() == reinterpret_cast<const unsigned char *>(&big_32));
    VERIFY(big_40.data() == reinterpret_cast<const unsigned char *>(&big_40));
    VERIFY(big_48.data() == reinterpret_cast<const unsigned char *>(&big_48));
    VERIFY(big_56.data() == reinterpret_cast<const unsigned char *>(&big_56));
    VERIFY(big_64.data() == reinterpret_cast<const unsigned char *>(&big_64));

    VERIFY(big_u8.data() == reinterpret_cast<const unsigned char *>(&big_u8));
    VERIFY(big_u16.data() == reinterpret_cast<const unsigned char *>(&big_u16));
    VERIFY(big_u24.data() == reinterpret_cast<const unsigned char *>(&big_u24));
    VERIFY(big_u32.data() == reinterpret_cast<const unsigned char *>(&big_u32));
    VERIFY(big_u40.data() == reinterpret_cast<const unsigned char *>(&big_u40));
    VERIFY(big_u48.data() == reinterpret_cast<const unsigned char *>(&big_u48));
    VERIFY(big_u56.data() == reinterpret_cast<const unsigned char *>(&big_u56));
    VERIFY(big_u64.data() == reinterpret_cast<const unsigned char *>(&big_u64));

    VERIFY(little_8.data() == reinterpret_cast<const unsigned char *>(&little_8));
    VERIFY(little_16.data() == reinterpret_cast<const unsigned char *>(&little_16));
    VERIFY(little_24.data() == reinterpret_cast<const unsigned char *>(&little_24));
    VERIFY(little_32.data() == reinterpret_cast<const unsigned char *>(&little_32));
    VERIFY(little_40.data() == reinterpret_cast<const unsigned char *>(&little_40));
    VERIFY(little_48.data() == reinterpret_cast<const unsigned char *>(&little_48));
    VERIFY(little_56.data() == reinterpret_cast<const unsigned char *>(&little_56));
    VERIFY(little_64.data() == reinterpret_cast<const unsigned char *>(&little_64));

    VERIFY(little_u8.data() == reinterpret_cast<const unsigned char *>(&little_u8));
    VERIFY(little_u16.data() == reinterpret_cast<const unsigned char *>(&little_u16));
    VERIFY(little_u24.data() == reinterpret_cast<const unsigned char *>(&little_u24));
    VERIFY(little_u32.data() == reinterpret_cast<const unsigned char *>(&little_u32));
    VERIFY(little_u40.data() == reinterpret_cast<const unsigned char *>(&little_u40));
    VERIFY(little_u48.data() == reinterpret_cast<const unsigned char *>(&little_u48));
    VERIFY(little_u56.data() == reinterpret_cast<const unsigned char *>(&little_u56));
    VERIFY(little_u64.data() == reinterpret_cast<const unsigned char *>(&little_u64));

    VERIFY(native_8.data() == reinterpret_cast<const unsigned char *>(&native_8));
    VERIFY(native_16.data() == reinterpret_cast<const unsigned char *>(&native_16));
    VERIFY(native_24.data() == reinterpret_cast<const unsigned char *>(&native_24));
    VERIFY(native_32.data() == reinterpret_cast<const unsigned char *>(&native_32));
    VERIFY(native_40.data() == reinterpret_cast<const unsigned char *>(&native_40));
    VERIFY(native_48.data() == reinterpret_cast<const unsigned char *>(&native_48));
    VERIFY(native_56.data() == reinterpret_cast<const unsigned char *>(&native_56));
    VERIFY(native_64.data() == reinterpret_cast<const unsigned char *>(&native_64));

    VERIFY(native_u8.data() == reinterpret_cast<const unsigned char *>(&native_u8));
    VERIFY(native_u16.data() == reinterpret_cast<const unsigned char *>(&native_u16));
    VERIFY(native_u24.data() == reinterpret_cast<const unsigned char *>(&native_u24));
    VERIFY(native_u32.data() == reinterpret_cast<const unsigned char *>(&native_u32));
    VERIFY(native_u40.data() == reinterpret_cast<const unsigned char *>(&native_u40));
    VERIFY(native_u48.data() == reinterpret_cast<const unsigned char *>(&native_u48));
    VERIFY(native_u56.data() == reinterpret_cast<const unsigned char *>(&native_u56));
    VERIFY(native_u64.data() == reinterpret_cast<const unsigned char *>(&native_u64));

    VERIFY(big_align_int16.data() == reinterpret_cast<const unsigned char *>(&big_align_int16));
    VERIFY(big_align_int32.data() == reinterpret_cast<const unsigned char *>(&big_align_int32));
    VERIFY(big_align_int64.data() == reinterpret_cast<const unsigned char *>(&big_align_int64));

    VERIFY(big_align_uint16.data() == reinterpret_cast<const unsigned char *>(&big_align_uint16));
    VERIFY(big_align_uint32.data() == reinterpret_cast<const unsigned char *>(&big_align_uint32));
    VERIFY(big_align_uint64.data() == reinterpret_cast<const unsigned char *>(&big_align_uint64));

    VERIFY(little_align_int16.data() == reinterpret_cast<const unsigned char *>(&little_align_int16));
    VERIFY(little_align_int32.data() == reinterpret_cast<const unsigned char *>(&little_align_int32));
    VERIFY(little_align_int64.data() == reinterpret_cast<const unsigned char *>(&little_align_int64));

    VERIFY(little_align_uint16.data() == reinterpret_cast<const unsigned char *>(&little_align_uint16));
    VERIFY(little_align_uint32.data() == reinterpret_cast<const unsigned char *>(&little_align_uint32));
    VERIFY(little_align_uint64.data() == reinterpret_cast<const unsigned char *>(&little_align_uint64));

  }

  //  check_size  ------------------------------------------------------------//

  void check_size()
  {
    VERIFY( numeric_limits<signed char>::digits == 7 );
    VERIFY( numeric_limits<unsigned char>::digits == 8 );

    VERIFY_SIZE( sizeof( big_int8_t ), 1 );
    VERIFY_SIZE( sizeof( big_int16_t ), 2 );
    VERIFY_SIZE( sizeof( big_int24_t ), 3 );
    VERIFY_SIZE( sizeof( big_int32_t ), 4 );
    VERIFY_SIZE( sizeof( big_int40_t ), 5 );
    VERIFY_SIZE( sizeof( big_int48_t ), 6 );
    VERIFY_SIZE( sizeof( big_int56_t ), 7 );
    VERIFY_SIZE( sizeof( big_int64_t ), 8 );

    VERIFY_SIZE( sizeof( big_uint8_t ), 1 );
    VERIFY_SIZE( sizeof( big_uint16_t ), 2 );
    VERIFY_SIZE( sizeof( big_uint24_t ), 3 );
    VERIFY_SIZE( sizeof( big_uint32_t ), 4 );
    VERIFY_SIZE( sizeof( big_uint40_t ), 5 );
    VERIFY_SIZE( sizeof( big_uint48_t ), 6 );
    VERIFY_SIZE( sizeof( big_uint56_t ), 7 );
    VERIFY_SIZE( sizeof( big_uint64_t ), 8 );

    VERIFY_SIZE( sizeof( little_int8_t ), 1 );
    VERIFY_SIZE( sizeof( little_int16_t ), 2 );
    VERIFY_SIZE( sizeof( little_int24_t ), 3 );
    VERIFY_SIZE( sizeof( little_int32_t ), 4 );
    VERIFY_SIZE( sizeof( little_int40_t ), 5 );
    VERIFY_SIZE( sizeof( little_int48_t ), 6 );
    VERIFY_SIZE( sizeof( little_int56_t ), 7 );
    VERIFY_SIZE( sizeof( little_int64_t ), 8 );

    VERIFY_SIZE( sizeof( little_uint8_t ), 1 );
    VERIFY_SIZE( sizeof( little_uint16_t ), 2 );
    VERIFY_SIZE( sizeof( little_uint24_t ), 3 );
    VERIFY_SIZE( sizeof( little_uint32_t ), 4 );
    VERIFY_SIZE( sizeof( little_uint40_t ), 5 );
    VERIFY_SIZE( sizeof( little_uint48_t ), 6 );
    VERIFY_SIZE( sizeof( little_uint56_t ), 7 );
    VERIFY_SIZE( sizeof( little_uint64_t ), 8 );

    VERIFY_SIZE( sizeof( native_int8_t ), 1 );
    VERIFY_SIZE( sizeof( native_int16_t ), 2 );
    VERIFY_SIZE( sizeof( native_int24_t ), 3 );
    VERIFY_SIZE( sizeof( native_int32_t ), 4 );
    VERIFY_SIZE( sizeof( native_int40_t ), 5 );
    VERIFY_SIZE( sizeof( native_int48_t ), 6 );
    VERIFY_SIZE( sizeof( native_int56_t ), 7 );
    VERIFY_SIZE( sizeof( native_int64_t ), 8 );

    VERIFY_SIZE( sizeof( native_uint8_t ), 1 );
    VERIFY_SIZE( sizeof( native_uint16_t ), 2 );
    VERIFY_SIZE( sizeof( native_uint24_t ), 3 );
    VERIFY_SIZE( sizeof( native_uint32_t ), 4 );
    VERIFY_SIZE( sizeof( native_uint40_t ), 5 );
    VERIFY_SIZE( sizeof( native_uint48_t ), 6 );
    VERIFY_SIZE( sizeof( native_uint56_t ), 7 );
    VERIFY_SIZE( sizeof( native_uint64_t ), 8 );

    VERIFY_SIZE(sizeof(big_int8_at), 1);
    VERIFY_SIZE(sizeof(big_int16_at), 2);
    VERIFY_SIZE( sizeof( big_int32_at ), 4 );
    VERIFY_SIZE( sizeof( big_int64_at ), 8 );

    VERIFY_SIZE(sizeof(big_uint8_at), 1);
    VERIFY_SIZE(sizeof(big_uint16_at), 2);
    VERIFY_SIZE( sizeof( big_uint32_at ), 4 );
    VERIFY_SIZE( sizeof( big_uint64_at ), 8 );

    VERIFY_SIZE(sizeof(little_int8_at), 1);
    VERIFY_SIZE(sizeof(little_int16_at), 2);
    VERIFY_SIZE( sizeof( little_int32_at ), 4 );
    VERIFY_SIZE( sizeof( little_int64_at ), 8 );

    VERIFY_SIZE(sizeof(little_uint8_at), 1);
    VERIFY_SIZE(sizeof(little_uint16_at), 2);
    VERIFY_SIZE( sizeof( little_uint32_at ), 4 );
    VERIFY_SIZE( sizeof( little_uint64_at ), 8 );
  } // check_size

  //  check_alignment  -------------------------------------------------------//

  void check_alignment()
  {
    // structs with offsets % 2 == 1 for type of size > 1 to ensure no alignment
    // bytes added for any size > 1

    struct big_struct
    {
      big_int8_t    v0;
      big_int16_t    v1;
      big_int24_t    v3;
      char      v6;
      big_int32_t    v7;
      big_int40_t    v11;
      char      v16;
      big_int48_t    v17;
      big_int56_t    v23;
      char      v30;
      big_int64_t    v31;
    };

    struct big_u_struct
    {
      big_uint8_t    v0;
      big_uint16_t    v1;
      big_uint24_t    v3;
      char       v6;
      big_uint32_t    v7;
      big_uint40_t    v11;
      char       v16;
      big_uint48_t    v17;
      big_uint56_t    v23;
      char       v30;
      big_uint64_t    v31;
    };

    struct little_struct
    {
      little_int8_t    v0;
      little_int16_t    v1;
      little_int24_t    v3;
      char         v6;
      little_int32_t    v7;
      little_int40_t    v11;
      char         v16;
      little_int48_t    v17;
      little_int56_t    v23;
      char         v30;
      little_int64_t    v31;
    };

    struct little_u_struct
    {
      little_uint8_t    v0;
      little_uint16_t    v1;
      little_uint24_t    v3;
      char          v6;
      little_uint32_t    v7;
      little_uint40_t    v11;
      char          v16;
      little_uint48_t    v17;
      little_uint56_t    v23;
      char          v30;
      little_uint64_t    v31;
    };

    struct native_struct
    {
      native_int8_t    v0;
      native_int16_t    v1;
      native_int24_t    v3;
      char         v6;
      native_int32_t    v7;
      native_int40_t    v11;
      char         v16;
      native_int48_t    v17;
      native_int56_t    v23;
      char         v30;
      native_int64_t    v31;
    };

    struct native_u_struct
    {
      native_uint8_t    v0;
      native_uint16_t    v1;
      native_uint24_t    v3;
      char          v6;
      native_uint32_t    v7;
      native_uint40_t    v11;
      char          v16;
      native_uint48_t    v17;
      native_uint56_t    v23;
      char          v30;
      native_uint64_t    v31;
    };

    //  aligned test cases

    struct big_aligned_struct
    {
      big_int16_at    v0;
      big_int32_at    v1;
      char          v3;
      // on a 32-bit system, the padding here may be 3 rather than 7 bytes
      big_int64_at    v4;
    };

    struct little_aligned_struct
    {
      little_int16_at    v0;
      little_int32_at    v1;
      char          v3;
      // on a 32-bit system, the padding here may be 3 rather than 7 bytes
      little_int64_at    v4;
    };

    int saved_err_count = err_count;

    VERIFY_SIZE( sizeof(big_struct), 39 );
    VERIFY_SIZE( sizeof(big_u_struct), 39 );
    VERIFY_SIZE( sizeof(little_struct), 39 );
    VERIFY_SIZE( sizeof(little_u_struct), 39 );
    VERIFY_SIZE( sizeof(native_struct), 39 );
    VERIFY_SIZE( sizeof(native_u_struct), 39 );
    VERIFY( sizeof(big_aligned_struct) <= 24 );
    VERIFY( sizeof(little_aligned_struct) <= 24 );

    if ( saved_err_count == err_count )
    {
      cout <<
        "Size and alignment for structures of endian types are as expected.\n";
    }
  } // check_alignment

  //  check_representation_and_range_and_ops  --------------------------------//

  void check_representation_and_range_and_ops()
  {
    // unaligned integer types
    VERIFY_BIG_REPRESENTATION( big_int8_t );
    VERIFY_VALUE_AND_OPS( big_int8_t, int_least8_t,  0x7e );
    VERIFY_VALUE_AND_OPS( big_int8_t, int_least8_t, -0x80 );

    VERIFY_BIG_REPRESENTATION( big_int16_t );
    VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t,  0x7ffe );
    VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t, -0x8000 );

    VERIFY_BIG_REPRESENTATION( big_int24_t );
    VERIFY_VALUE_AND_OPS( big_int24_t, int_least32_t,  0x7ffffe );
    VERIFY_VALUE_AND_OPS( big_int24_t, int_least32_t, -0x800000 );

    VERIFY_BIG_REPRESENTATION( big_int32_t );
    VERIFY_VALUE_AND_OPS( big_int32_t, int_least32_t,  0x7ffffffe );
    VERIFY_VALUE_AND_OPS( big_int32_t, int_least32_t, -0x7fffffff-1 );

    VERIFY_BIG_REPRESENTATION( big_int40_t );
    VERIFY_VALUE_AND_OPS( big_int40_t, int_least64_t,  0x7ffffffffeLL );
    VERIFY_VALUE_AND_OPS( big_int40_t, int_least64_t, -0x8000000000LL );

    VERIFY_BIG_REPRESENTATION( big_int48_t );
    VERIFY_VALUE_AND_OPS( big_int48_t, int_least64_t,  0x7ffffffffffeLL );
    VERIFY_VALUE_AND_OPS( big_int48_t, int_least64_t, -0x800000000000LL );

    VERIFY_BIG_REPRESENTATION( big_int56_t );
    VERIFY_VALUE_AND_OPS( big_int56_t, int_least64_t,  0x7ffffffffffffeLL );
    VERIFY_VALUE_AND_OPS( big_int56_t, int_least64_t, -0x80000000000000LL );

    VERIFY_BIG_REPRESENTATION( big_int64_t );
    VERIFY_VALUE_AND_OPS( big_int64_t, int_least64_t,  0x7ffffffffffffffeLL );
    VERIFY_VALUE_AND_OPS( big_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );

    VERIFY_BIG_REPRESENTATION( big_uint8_t );
    VERIFY_VALUE_AND_OPS( big_uint8_t, uint_least8_t,  0xff );

    VERIFY_BIG_REPRESENTATION( big_uint16_t );
    VERIFY_VALUE_AND_OPS( big_uint16_t, uint_least16_t, 0xffff );

    VERIFY_BIG_REPRESENTATION( big_uint24_t );
    VERIFY_VALUE_AND_OPS( big_uint24_t, uint_least32_t, 0xffffff );

    VERIFY_BIG_REPRESENTATION( big_uint32_t );
    VERIFY_VALUE_AND_OPS( big_uint32_t, uint_least32_t, 0xffffffff );

    VERIFY_BIG_REPRESENTATION( big_uint40_t );
    VERIFY_VALUE_AND_OPS( big_uint40_t, uint_least64_t, 0xffffffffffLL );

    VERIFY_BIG_REPRESENTATION( big_uint48_t );
    VERIFY_VALUE_AND_OPS( big_uint48_t, uint_least64_t, 0xffffffffffffLL );

    VERIFY_BIG_REPRESENTATION( big_uint56_t );
    VERIFY_VALUE_AND_OPS( big_uint56_t, uint_least64_t, 0xffffffffffffffLL );

    VERIFY_BIG_REPRESENTATION( big_uint64_t );
    VERIFY_VALUE_AND_OPS( big_uint64_t, uint_least64_t, 0xffffffffffffffffULL );

    VERIFY_LITTLE_REPRESENTATION( little_int8_t );
    VERIFY_VALUE_AND_OPS( little_int8_t, int_least8_t,   0x7e );
    VERIFY_VALUE_AND_OPS( little_int8_t, int_least8_t,  -0x80 );

    VERIFY_LITTLE_REPRESENTATION( little_int16_t );
    VERIFY_VALUE_AND_OPS( little_int16_t, int_least16_t,  0x7ffe );
    VERIFY_VALUE_AND_OPS( little_int16_t, int_least16_t, -0x8000 );

    VERIFY_LITTLE_REPRESENTATION( little_int24_t );
    VERIFY_VALUE_AND_OPS( little_int24_t, int_least32_t,  0x7ffffe );
    VERIFY_VALUE_AND_OPS( little_int24_t, int_least32_t, -0x800000 );

    VERIFY_LITTLE_REPRESENTATION( little_int32_t );
    VERIFY_VALUE_AND_OPS( little_int32_t, int_least32_t,  0x7ffffffe );
    VERIFY_VALUE_AND_OPS( little_int32_t, int_least32_t, -0x7fffffff-1 );

    VERIFY_LITTLE_REPRESENTATION( little_int40_t );
    VERIFY_VALUE_AND_OPS( little_int40_t, int_least64_t,  0x7ffffffffeLL );
    VERIFY_VALUE_AND_OPS( little_int40_t, int_least64_t, -0x8000000000LL );

    VERIFY_LITTLE_REPRESENTATION( little_int48_t );
    VERIFY_VALUE_AND_OPS( little_int48_t, int_least64_t,  0x7ffffffffffeLL );
    VERIFY_VALUE_AND_OPS( little_int48_t, int_least64_t, -0x800000000000LL );

    VERIFY_LITTLE_REPRESENTATION( little_int56_t );
    VERIFY_VALUE_AND_OPS( little_int56_t, int_least64_t,  0x7ffffffffffffeLL );
    VERIFY_VALUE_AND_OPS( little_int56_t, int_least64_t, -0x80000000000000LL );

    VERIFY_LITTLE_REPRESENTATION( little_int64_t );
    VERIFY_VALUE_AND_OPS( little_int64_t, int_least64_t,  0x7ffffffffffffffeLL );
    VERIFY_VALUE_AND_OPS( little_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );

    VERIFY_LITTLE_REPRESENTATION( little_uint8_t );
    VERIFY_VALUE_AND_OPS( little_uint8_t, uint_least8_t, 0xff );

    VERIFY_LITTLE_REPRESENTATION( little_uint16_t );
    VERIFY_VALUE_AND_OPS( little_uint16_t, uint_least16_t, 0xffff );

    VERIFY_LITTLE_REPRESENTATION( little_uint24_t );
    VERIFY_VALUE_AND_OPS( little_uint24_t, uint_least32_t, 0xffffff );

    VERIFY_LITTLE_REPRESENTATION( little_uint32_t );
    VERIFY_VALUE_AND_OPS( little_uint32_t, uint_least32_t, 0xffffffff );

    VERIFY_LITTLE_REPRESENTATION( little_uint40_t );
    VERIFY_VALUE_AND_OPS( little_uint40_t, uint_least64_t, 0xffffffffffLL );

    VERIFY_LITTLE_REPRESENTATION( little_uint48_t );
    VERIFY_VALUE_AND_OPS( little_uint48_t, uint_least64_t, 0xffffffffffffLL );

    VERIFY_LITTLE_REPRESENTATION( little_uint56_t );
    VERIFY_VALUE_AND_OPS( little_uint56_t, uint_least64_t, 0xffffffffffffffLL );

    VERIFY_LITTLE_REPRESENTATION( little_uint64_t );
    VERIFY_VALUE_AND_OPS( little_uint64_t, uint_least64_t, 0xffffffffffffffffULL );

    VERIFY_NATIVE_REPRESENTATION( native_int8_t );
    VERIFY_VALUE_AND_OPS( native_int8_t, int_least8_t,   0x7e );
    VERIFY_VALUE_AND_OPS( native_int8_t, int_least8_t,  -0x80 );

    VERIFY_NATIVE_REPRESENTATION( native_int16_t );
    VERIFY_VALUE_AND_OPS( native_int16_t, int_least16_t,  0x7ffe );
    VERIFY_VALUE_AND_OPS( native_int16_t, int_least16_t, -0x8000 );

    VERIFY_NATIVE_REPRESENTATION( native_int24_t );
    VERIFY_VALUE_AND_OPS( native_int24_t, int_least32_t,  0x7ffffe );
    VERIFY_VALUE_AND_OPS( native_int24_t, int_least32_t, -0x800000 );

    VERIFY_NATIVE_REPRESENTATION( native_int32_t );
    VERIFY_VALUE_AND_OPS( native_int32_t, int_least32_t,  0x7ffffffe );
    VERIFY_VALUE_AND_OPS( native_int32_t, int_least32_t, -0x7fffffff-1 );

    VERIFY_NATIVE_REPRESENTATION( native_int40_t );
    VERIFY_VALUE_AND_OPS( native_int40_t, int_least64_t,  0x7ffffffffeLL );
    VERIFY_VALUE_AND_OPS( native_int40_t, int_least64_t, -0x8000000000LL );

    VERIFY_NATIVE_REPRESENTATION( native_int48_t );
    VERIFY_VALUE_AND_OPS( native_int48_t, int_least64_t,  0x7ffffffffffeLL );
    VERIFY_VALUE_AND_OPS( native_int48_t, int_least64_t, -0x800000000000LL );

    VERIFY_NATIVE_REPRESENTATION( native_int56_t );
    VERIFY_VALUE_AND_OPS( native_int56_t, int_least64_t,  0x7ffffffffffffeLL );
    VERIFY_VALUE_AND_OPS( native_int56_t, int_least64_t, -0x80000000000000LL );

    VERIFY_NATIVE_REPRESENTATION( native_int64_t );
    VERIFY_VALUE_AND_OPS( native_int64_t, int_least64_t,  0x7ffffffffffffffeLL );
    VERIFY_VALUE_AND_OPS( native_int64_t, int_least64_t, -0x7fffffffffffffffLL-1 );

    VERIFY_NATIVE_REPRESENTATION( native_uint8_t );
    VERIFY_VALUE_AND_OPS( native_uint8_t, uint_least8_t, 0xff );

    VERIFY_NATIVE_REPRESENTATION( native_uint16_t );
    VERIFY_VALUE_AND_OPS( native_uint16_t, uint_least16_t, 0xffff );

    VERIFY_NATIVE_REPRESENTATION( native_uint24_t );
    VERIFY_VALUE_AND_OPS( native_uint24_t, uint_least32_t, 0xffffff );

    VERIFY_NATIVE_REPRESENTATION( native_uint32_t );
    VERIFY_VALUE_AND_OPS( native_uint32_t, uint_least32_t, 0xffffffff );

    VERIFY_NATIVE_REPRESENTATION( native_uint40_t );
    VERIFY_VALUE_AND_OPS( native_uint40_t, uint_least64_t, 0xffffffffffLL );

    VERIFY_NATIVE_REPRESENTATION( native_uint48_t );
    VERIFY_VALUE_AND_OPS( native_uint48_t, uint_least64_t, 0xffffffffffffLL );

    VERIFY_NATIVE_REPRESENTATION( native_uint56_t );
    VERIFY_VALUE_AND_OPS( native_uint56_t, uint_least64_t, 0xffffffffffffffLL );

    VERIFY_NATIVE_REPRESENTATION( native_uint64_t );
    VERIFY_VALUE_AND_OPS( native_uint64_t, uint_least64_t, 0xffffffffffffffffULL );

    // aligned integer types
    VERIFY_BIG_REPRESENTATION( big_int16_at );
    VERIFY_VALUE_AND_OPS( big_int16_at, int_least16_t,  0x7ffe );
    VERIFY_VALUE_AND_OPS( big_int16_at, int_least16_t, -0x8000 );

    VERIFY_BIG_REPRESENTATION( big_int32_at );
    VERIFY_VALUE_AND_OPS( big_int32_at, int_least32_t,  0x7ffffffe );
    VERIFY_VALUE_AND_OPS( big_int32_at, int_least32_t, -0x7fffffff-1 );

    VERIFY_BIG_REPRESENTATION( big_int64_at );
    VERIFY_VALUE_AND_OPS( big_int64_at, int_least64_t,  0x7ffffffffffffffeLL );
    VERIFY_VALUE_AND_OPS( big_int64_at, int_least64_t, -0x7fffffffffffffffLL-1 );

    VERIFY_BIG_REPRESENTATION( big_uint16_at );
    VERIFY_VALUE_AND_OPS( big_uint16_at, uint_least16_t, 0xffff );

    VERIFY_BIG_REPRESENTATION( big_uint32_at );
    VERIFY_VALUE_AND_OPS( big_uint32_at, uint_least32_t, 0xffffffff );

    VERIFY_BIG_REPRESENTATION( big_uint64_at );
    VERIFY_VALUE_AND_OPS( big_uint64_at, uint_least64_t, 0xffffffffffffffffULL );

    VERIFY_LITTLE_REPRESENTATION( little_int16_at );
    VERIFY_VALUE_AND_OPS( little_int16_at, int_least16_t,  0x7ffe );
    VERIFY_VALUE_AND_OPS( little_int16_at, int_least16_t, -0x8000 );

    VERIFY_LITTLE_REPRESENTATION( little_int32_at );
    VERIFY_VALUE_AND_OPS( little_int32_at, int_least32_t,  0x7ffffffe );
    VERIFY_VALUE_AND_OPS( little_int32_at, int_least32_t, -0x7fffffff-1 );

    VERIFY_LITTLE_REPRESENTATION( little_int64_at );
    VERIFY_VALUE_AND_OPS( little_int64_at, int_least64_t,  0x7ffffffffffffffeLL );
    VERIFY_VALUE_AND_OPS( little_int64_at, int_least64_t, -0x7fffffffffffffffLL-1 );

    VERIFY_LITTLE_REPRESENTATION( little_uint16_at );
    VERIFY_VALUE_AND_OPS( little_uint16_at, uint_least16_t, 0xffff );

    VERIFY_LITTLE_REPRESENTATION( little_uint32_at );
    VERIFY_VALUE_AND_OPS( little_uint32_at, uint_least32_t, 0xffffffff );

    VERIFY_LITTLE_REPRESENTATION( little_uint64_at );
    VERIFY_VALUE_AND_OPS( little_uint64_at, uint_least64_t, 0xffffffffffffffffULL );

  } // check_representation_and_range

/*

  class MyInt
  {
    int32_t mx;
  public:
    MyInt(int32_t x = 0) : mx(x) {}
    operator int32_t() const {return mx;}

    //friend int32_t operator+(const MyInt& x) {return x;}
  };

  void check_udt()
  {
    typedef boost::endian::endian_arithmetic< order::big, MyInt, 32 >  mybig_int32_ut;

    mybig_int32_ut v(10);
    cout << "+v is " << +v << endl;
    v += 1;
    cout << "v is " << +v << endl;
    v -= 2;
    cout << "v is " << +v << endl;
    v *= 2;
    cout << "v is " << +v << endl;
    ++v;
    cout << "v is " << +v << endl;
    --v;
    cout << "v is " << +v << endl;
//    cout << "v+v is " << +(v+v) << endl;
  }

  void check_udt_le()
  {
    typedef boost::endian::endian_arithmetic< order::little, MyInt, 32 >  mylittle_int32_ut;

    mylittle_int32_ut v(10);
    cout << "+v is " << +v << endl;
    v += 1;
    cout << "v is " << +v << endl;
    v -= 2;
    cout << "v is " << +v << endl;
    v *= 2;
    cout << "v is " << +v << endl;
    ++v;
    cout << "v is " << +v << endl;
    --v;
    cout << "v is " << +v << endl;
//    cout << "v+v is " << +(v+v) << endl;
  }

*/

  long iterations = 10000;

  template< class Endian >
  Endian timing_test( const char * s)
  {
    cout << s << " timing test, " << iterations << " iterations: ";
//    progress_timer t;

    Endian v = 1;
    for ( long i = 0; i < iterations; ++i )
    {
      v += 1;
      v *= 3;
      ++v;
      v *= i;
      if ( i == 0 ) VERIFY_VALUE_AND_OPS( Endian, typename Endian::value_type, 21 );
    }
    return v;
  }

} // unnamed namespace

//  main  ------------------------------------------------------------------------------//

int cpp_main( int argc, char * argv[] )
{
  cout << "Usage: "
       << argv[0] << " [#],\n where # specifies iteration count\n"
          " default iteration count is " << iterations << endl;

  if ( argc > 1 )
    iterations = atol( argv[1] );
  if ( iterations < 1 ) iterations = 1;

  detect_order();
  check_size();
  check_alignment();
  check_representation_and_range_and_ops();
  check_data();
  //check_udt();
  //check_udt_le();

  //timing_test<big_int32_t> ( "big_int32_t" );
  //timing_test<big_int32_at>( "big_int32_at" );
  //timing_test<little_int32_t> ( "little_int32_t" );
  //timing_test<little_int32_at>( "little_int32_at" );

  cout << "\n" << err_count << " errors detected\nTest "
       << (err_count==0 ? "passed\n\n" : "failed\n\n");

  return err_count ? 1 : 0;
} // main

int main( int argc, char* argv[] )
{
    try
    {
        return cpp_main( argc, argv );
    }
    catch( std::exception const & x )
    {
        std::cout << "Exception: " << x.what() << std::endl;
        return 1;
    }
}