forked from boostorg/utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators_test.cpp
941 lines (790 loc) · 33.6 KB
/
operators_test.cpp
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
// Demonstrate and test boost/operators.hpp -------------------------------//
// Copyright Beman Dawes 1999. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org/libs/utility for documentation.
// Revision History
// 03 Apr 08 Added convertible_to_bool (Daniel Frey)
// 01 Oct 01 Added tests for "left" operators
// and new grouped operators. (Helmut Zeisel)
// 20 May 01 Output progress messages. Added tests for new operator
// templates. Updated random number generator. Changed tests to
// use Boost Test Tools library. (Daryle Walker)
// 04 Jun 00 Added regression test for a bug I found (David Abrahams)
// 17 Jun 00 Fix for broken compilers (Aleksey Gurtovoy)
// ?? ??? 00 Major update to randomly test all one- and two- argument forms by
// wrapping integral types and comparing the results of operations
// to the results for the raw types (David Abrahams)
// 12 Dec 99 Minor update, output confirmation message.
// 15 Nov 99 Initial version
#define BOOST_INCLUDE_MAIN
#include <boost/config.hpp> // for BOOST_MSVC
#include <boost/cstdlib.hpp> // for boost::exit_success
#include <boost/operators.hpp> // for the tested items
#include <boost/random/linear_congruential.hpp> // for boost::minstd_rand
#include <boost/test/test_tools.hpp> // for main
#include <iostream> // for std::cout (std::endl indirectly)
namespace
{
// avoiding a template version of true_value so as to not confuse VC++
int true_value(int x) { return x; }
long true_value(long x) { return x; }
signed char true_value(signed char x) { return x; }
short true_value(short x) { return x; }
unsigned int true_value(unsigned int x) { return x; }
unsigned long true_value(unsigned long x) { return x; }
unsigned char true_value(unsigned char x) { return x; }
unsigned short true_value(unsigned short x) { return x; }
// verify the minimum requirements for some operators
class convertible_to_bool
{
private:
bool _value;
typedef bool convertible_to_bool::*unspecified_bool_type;
void operator!() const;
public:
convertible_to_bool( const bool value ) : _value( value ) {}
operator unspecified_bool_type() const
{ return _value ? &convertible_to_bool::_value : 0; }
};
// The use of operators<> here tended to obscure
// interactions with certain compiler bugs
template <class T>
class Wrapped1
: boost::operators<Wrapped1<T> >
, boost::shiftable<Wrapped1<T> >
{
public:
explicit Wrapped1( T v = T() ) : _value(v) {}
T value() const { return _value; }
convertible_to_bool operator<(const Wrapped1& x) const
{ return _value < x._value; }
convertible_to_bool operator==(const Wrapped1& x) const
{ return _value == x._value; }
Wrapped1& operator+=(const Wrapped1& x)
{ _value += x._value; return *this; }
Wrapped1& operator-=(const Wrapped1& x)
{ _value -= x._value; return *this; }
Wrapped1& operator*=(const Wrapped1& x)
{ _value *= x._value; return *this; }
Wrapped1& operator/=(const Wrapped1& x)
{ _value /= x._value; return *this; }
Wrapped1& operator%=(const Wrapped1& x)
{ _value %= x._value; return *this; }
Wrapped1& operator|=(const Wrapped1& x)
{ _value |= x._value; return *this; }
Wrapped1& operator&=(const Wrapped1& x)
{ _value &= x._value; return *this; }
Wrapped1& operator^=(const Wrapped1& x)
{ _value ^= x._value; return *this; }
Wrapped1& operator<<=(const Wrapped1& x)
{ _value <<= x._value; return *this; }
Wrapped1& operator>>=(const Wrapped1& x)
{ _value >>= x._value; return *this; }
Wrapped1& operator++() { ++_value; return *this; }
Wrapped1& operator--() { --_value; return *this; }
private:
T _value;
};
template <class T>
T true_value(Wrapped1<T> x) { return x.value(); }
template <class T, class U>
class Wrapped2
: boost::operators<Wrapped2<T, U> >
, boost::operators2<Wrapped2<T, U>, U>
, boost::shiftable1<Wrapped2<T, U>
, boost::shiftable2<Wrapped2<T, U>, U > >
{
public:
explicit Wrapped2( T v = T() ) : _value(v) {}
T value() const { return _value; }
convertible_to_bool operator<(const Wrapped2& x) const
{ return _value < x._value; }
convertible_to_bool operator==(const Wrapped2& x) const
{ return _value == x._value; }
Wrapped2& operator+=(const Wrapped2& x)
{ _value += x._value; return *this; }
Wrapped2& operator-=(const Wrapped2& x)
{ _value -= x._value; return *this; }
Wrapped2& operator*=(const Wrapped2& x)
{ _value *= x._value; return *this; }
Wrapped2& operator/=(const Wrapped2& x)
{ _value /= x._value; return *this; }
Wrapped2& operator%=(const Wrapped2& x)
{ _value %= x._value; return *this; }
Wrapped2& operator|=(const Wrapped2& x)
{ _value |= x._value; return *this; }
Wrapped2& operator&=(const Wrapped2& x)
{ _value &= x._value; return *this; }
Wrapped2& operator^=(const Wrapped2& x)
{ _value ^= x._value; return *this; }
Wrapped2& operator<<=(const Wrapped2& x)
{ _value <<= x._value; return *this; }
Wrapped2& operator>>=(const Wrapped2& x)
{ _value >>= x._value; return *this; }
Wrapped2& operator++() { ++_value; return *this; }
Wrapped2& operator--() { --_value; return *this; }
convertible_to_bool operator<(U u) const
{ return _value < u; }
convertible_to_bool operator>(U u) const
{ return _value > u; }
convertible_to_bool operator==(U u) const
{ return _value == u; }
Wrapped2& operator+=(U u) { _value += u; return *this; }
Wrapped2& operator-=(U u) { _value -= u; return *this; }
Wrapped2& operator*=(U u) { _value *= u; return *this; }
Wrapped2& operator/=(U u) { _value /= u; return *this; }
Wrapped2& operator%=(U u) { _value %= u; return *this; }
Wrapped2& operator|=(U u) { _value |= u; return *this; }
Wrapped2& operator&=(U u) { _value &= u; return *this; }
Wrapped2& operator^=(U u) { _value ^= u; return *this; }
Wrapped2& operator<<=(U u) { _value <<= u; return *this; }
Wrapped2& operator>>=(U u) { _value >>= u; return *this; }
private:
T _value;
};
template <class T, class U>
T true_value(Wrapped2<T,U> x) { return x.value(); }
template <class T>
class Wrapped3
: boost::equivalent<Wrapped3<T> >
, boost::partially_ordered<Wrapped3<T> >
, boost::equality_comparable<Wrapped3<T> >
{
public:
explicit Wrapped3( T v = T() ) : _value(v) {}
T value() const { return _value; }
convertible_to_bool operator<(const Wrapped3& x) const
{ return _value < x._value; }
private:
T _value;
};
template <class T>
T true_value(Wrapped3<T> x) { return x.value(); }
template <class T, class U>
class Wrapped4
: boost::equality_comparable1<Wrapped4<T, U>
, boost::equivalent1<Wrapped4<T, U>
, boost::partially_ordered1<Wrapped4<T, U> > > >
, boost::partially_ordered2<Wrapped4<T, U>, U
, boost::equivalent2<Wrapped4<T, U>, U
, boost::equality_comparable2<Wrapped4<T, U>, U> > >
{
public:
explicit Wrapped4( T v = T() ) : _value(v) {}
T value() const { return _value; }
convertible_to_bool operator<(const Wrapped4& x) const
{ return _value < x._value; }
convertible_to_bool operator<(U u) const
{ return _value < u; }
convertible_to_bool operator>(U u) const
{ return _value > u; }
private:
T _value;
};
template <class T, class U>
T true_value(Wrapped4<T,U> x) { return x.value(); }
// U must be convertible to T
template <class T, class U>
class Wrapped5
: boost::ordered_field_operators2<Wrapped5<T, U>, U>
, boost::ordered_field_operators1<Wrapped5<T, U> >
{
public:
explicit Wrapped5( T v = T() ) : _value(v) {}
// Conversion from U to Wrapped5<T,U>
Wrapped5(U u) : _value(u) {}
T value() const { return _value; }
convertible_to_bool operator<(const Wrapped5& x) const
{ return _value < x._value; }
convertible_to_bool operator<(U u) const
{ return _value < u; }
convertible_to_bool operator>(U u) const
{ return _value > u; }
convertible_to_bool operator==(const Wrapped5& u) const
{ return _value == u._value; }
convertible_to_bool operator==(U u) const
{ return _value == u; }
Wrapped5& operator/=(const Wrapped5& u) { _value /= u._value; return *this;}
Wrapped5& operator/=(U u) { _value /= u; return *this;}
Wrapped5& operator*=(const Wrapped5& u) { _value *= u._value; return *this;}
Wrapped5& operator*=(U u) { _value *= u; return *this;}
Wrapped5& operator-=(const Wrapped5& u) { _value -= u._value; return *this;}
Wrapped5& operator-=(U u) { _value -= u; return *this;}
Wrapped5& operator+=(const Wrapped5& u) { _value += u._value; return *this;}
Wrapped5& operator+=(U u) { _value += u; return *this;}
private:
T _value;
};
template <class T, class U>
T true_value(Wrapped5<T,U> x) { return x.value(); }
// U must be convertible to T
template <class T, class U>
class Wrapped6
: boost::ordered_euclidean_ring_operators2<Wrapped6<T, U>, U>
, boost::ordered_euclidean_ring_operators1<Wrapped6<T, U> >
{
public:
explicit Wrapped6( T v = T() ) : _value(v) {}
// Conversion from U to Wrapped6<T,U>
Wrapped6(U u) : _value(u) {}
T value() const { return _value; }
convertible_to_bool operator<(const Wrapped6& x) const
{ return _value < x._value; }
convertible_to_bool operator<(U u) const
{ return _value < u; }
convertible_to_bool operator>(U u) const
{ return _value > u; }
convertible_to_bool operator==(const Wrapped6& u) const
{ return _value == u._value; }
convertible_to_bool operator==(U u) const
{ return _value == u; }
Wrapped6& operator%=(const Wrapped6& u) { _value %= u._value; return *this;}
Wrapped6& operator%=(U u) { _value %= u; return *this;}
Wrapped6& operator/=(const Wrapped6& u) { _value /= u._value; return *this;}
Wrapped6& operator/=(U u) { _value /= u; return *this;}
Wrapped6& operator*=(const Wrapped6& u) { _value *= u._value; return *this;}
Wrapped6& operator*=(U u) { _value *= u; return *this;}
Wrapped6& operator-=(const Wrapped6& u) { _value -= u._value; return *this;}
Wrapped6& operator-=(U u) { _value -= u; return *this;}
Wrapped6& operator+=(const Wrapped6& u) { _value += u._value; return *this;}
Wrapped6& operator+=(U u) { _value += u; return *this;}
private:
T _value;
};
template <class T, class U>
T true_value(Wrapped6<T,U> x) { return x.value(); }
// MyInt uses only the single template-argument form of all_operators<>
typedef Wrapped1<int> MyInt;
typedef Wrapped2<long, long> MyLong;
typedef Wrapped3<signed char> MyChar;
typedef Wrapped4<short, short> MyShort;
typedef Wrapped5<double, int> MyDoubleInt;
typedef Wrapped6<long, int> MyLongInt;
template <class X1, class Y1, class X2, class Y2>
void sanity_check(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
BOOST_CHECK( true_value(y1) == true_value(y2) );
BOOST_CHECK( true_value(x1) == true_value(x2) );
}
template <class X1, class Y1, class X2, class Y2>
void test_less_than_comparable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
BOOST_CHECK( static_cast<bool>(x1 < y1) == static_cast<bool>(x2 < y2) );
BOOST_CHECK( static_cast<bool>(x1 <= y1) == static_cast<bool>(x2 <= y2) );
BOOST_CHECK( static_cast<bool>(x1 >= y1) == static_cast<bool>(x2 >= y2) );
BOOST_CHECK( static_cast<bool>(x1 > y1) == static_cast<bool>(x2 > y2) );
}
template <class X1, class Y1, class X2, class Y2>
void test_less_than_comparable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
test_less_than_comparable_aux( x1, y1, x2, y2 );
test_less_than_comparable_aux( y1, x1, y2, x2 );
}
template <class X1, class Y1, class X2, class Y2>
void test_equality_comparable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
BOOST_CHECK( static_cast<bool>(x1 == y1) == static_cast<bool>(x2 == y2) );
BOOST_CHECK( static_cast<bool>(x1 != y1) == static_cast<bool>(x2 != y2) );
}
template <class X1, class Y1, class X2, class Y2>
void test_equality_comparable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
test_equality_comparable_aux( x1, y1, x2, y2 );
test_equality_comparable_aux( y1, x1, y2, x2 );
}
template <class X1, class Y1, class X2, class Y2>
void test_multipliable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
BOOST_CHECK( (x1 * y1).value() == (x2 * y2) );
}
template <class X1, class Y1, class X2, class Y2>
void test_multipliable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
test_multipliable_aux( x1, y1, x2, y2 );
test_multipliable_aux( y1, x1, y2, x2 );
}
template <class A, class B>
void test_value_equality(A a, B b)
{
BOOST_CHECK(a.value() == b);
}
#define TEST_OP_R(op) test_value_equality(x1 op y1, x2 op y2)
#define TEST_OP_L(op) test_value_equality(y1 op x1, y2 op x2)
template <class X1, class Y1, class X2, class Y2>
void test_addable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
TEST_OP_R(+);
}
template <class X1, class Y1, class X2, class Y2>
void test_addable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
test_addable_aux( x1, y1, x2, y2 );
test_addable_aux( y1, x1, y2, x2 );
}
template <class X1, class Y1, class X2, class Y2>
void test_subtractable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
TEST_OP_R(-);
}
template <class X1, class Y1, class X2, class Y2>
void test_subtractable_left(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
TEST_OP_L(-);
}
template <class X1, class Y1, class X2, class Y2>
void test_dividable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
if ( y2 != 0 )
TEST_OP_R(/);
}
template <class X1, class Y1, class X2, class Y2>
void test_dividable_left(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
if ( x2 != 0 )
TEST_OP_L(/);
}
template <class X1, class Y1, class X2, class Y2>
void test_modable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
if ( y2 != 0 )
TEST_OP_R(%);
}
template <class X1, class Y1, class X2, class Y2>
void test_modable_left(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
if ( x2 != 0 )
TEST_OP_L(%);
}
template <class X1, class Y1, class X2, class Y2>
void test_xorable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
TEST_OP_R(^);
}
template <class X1, class Y1, class X2, class Y2>
void test_xorable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
test_xorable_aux( x1, y1, x2, y2 );
test_xorable_aux( y1, x1, y2, x2 );
}
template <class X1, class Y1, class X2, class Y2>
void test_andable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
TEST_OP_R(&);
}
template <class X1, class Y1, class X2, class Y2>
void test_andable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
test_andable_aux( x1, y1, x2, y2 );
test_andable_aux( y1, x1, y2, x2 );
}
template <class X1, class Y1, class X2, class Y2>
void test_orable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
TEST_OP_R(|);
}
template <class X1, class Y1, class X2, class Y2>
void test_orable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
test_orable_aux( x1, y1, x2, y2 );
test_orable_aux( y1, x1, y2, x2 );
}
template <class X1, class Y1, class X2, class Y2>
void test_left_shiftable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
TEST_OP_R(<<);
}
template <class X1, class Y1, class X2, class Y2>
void test_right_shiftable(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
sanity_check( x1, y1, x2, y2 );
TEST_OP_R(>>);
}
template <class X1, class X2>
void test_incrementable(X1 x1, X2 x2)
{
sanity_check( x1, x1, x2, x2 );
BOOST_CHECK( (x1++).value() == x2++ );
BOOST_CHECK( x1.value() == x2 );
}
template <class X1, class X2>
void test_decrementable(X1 x1, X2 x2)
{
sanity_check( x1, x1, x2, x2 );
BOOST_CHECK( (x1--).value() == x2-- );
BOOST_CHECK( x1.value() == x2 );
}
template <class X1, class Y1, class X2, class Y2>
void test_all(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
test_less_than_comparable( x1, y1, x2, y2 );
test_equality_comparable( x1, y1, x2, y2 );
test_multipliable( x1, y1, x2, y2 );
test_addable( x1, y1, x2, y2 );
test_subtractable( x1, y1, x2, y2 );
test_dividable( x1, y1, x2, y2 );
test_modable( x1, y1, x2, y2 );
test_xorable( x1, y1, x2, y2 );
test_andable( x1, y1, x2, y2 );
test_orable( x1, y1, x2, y2 );
test_left_shiftable( x1, y1, x2, y2 );
test_right_shiftable( x1, y1, x2, y2 );
test_incrementable( x1, x2 );
test_decrementable( x1, x2 );
}
template <class X1, class Y1, class X2, class Y2>
void test_left(X1 x1, Y1 y1, X2 x2, Y2 y2)
{
test_subtractable_left( x1, y1, x2, y2 );
test_dividable_left( x1, y1, x2, y2 );
test_modable_left( x1, y1, x2, y2 );
}
template <class Big, class Small>
struct tester
{
void operator()(boost::minstd_rand& randomizer) const
{
Big b1 = Big( randomizer() );
Big b2 = Big( randomizer() );
Small s = Small( randomizer() );
test_all( Wrapped1<Big>(b1), Wrapped1<Big>(b2), b1, b2 );
test_all( Wrapped2<Big, Small>(b1), s, b1, s );
}
};
template <class Big, class Small>
struct tester_left
{
void operator()(boost::minstd_rand& randomizer) const
{
Big b1 = Big( randomizer() );
Small s = Small( randomizer() );
test_left( Wrapped6<Big, Small>(b1), s, b1, s );
}
};
// added as a regression test. We had a bug which this uncovered.
struct Point
: boost::addable<Point
, boost::subtractable<Point> >
{
Point( int h, int v ) : h(h), v(v) {}
Point() :h(0), v(0) {}
const Point& operator+=( const Point& rhs )
{ h += rhs.h; v += rhs.v; return *this; }
const Point& operator-=( const Point& rhs )
{ h -= rhs.h; v -= rhs.v; return *this; }
int h;
int v;
};
} // unnamed namespace
// workaround for MSVC bug; for some reasons the compiler doesn't instantiate
// inherited operator templates at the moment it must, so the following
// explicit instantiations force it to do that.
#if defined(BOOST_MSVC) && (_MSC_VER < 1300)
template Wrapped1<int>;
template Wrapped1<long>;
template Wrapped1<unsigned int>;
template Wrapped1<unsigned long>;
template Wrapped2<int, int>;
template Wrapped2<int, signed char>;
template Wrapped2<long, signed char>;
template Wrapped2<long, int>;
template Wrapped2<long, long>;
template Wrapped2<unsigned int, unsigned int>;
template Wrapped2<unsigned int, unsigned char>;
template Wrapped2<unsigned long, unsigned int>;
template Wrapped2<unsigned long, unsigned char>;
template Wrapped2<unsigned long, unsigned long>;
template Wrapped6<long, int>;
template Wrapped6<long, signed char>;
template Wrapped6<int, signed char>;
template Wrapped6<unsigned long, unsigned int>;
template Wrapped6<unsigned long, unsigned char>;
template Wrapped6<unsigned int, unsigned char>;
#endif
#define PRIVATE_EXPR_TEST(e, t) BOOST_CHECK( ((e), (t)) )
int
test_main( int , char * [] )
{
using std::cout;
using std::endl;
// Regression test.
Point x;
x = x + Point(3, 4);
x = x - Point(3, 4);
cout << "Created point, and operated on it." << endl;
for (int n = 0; n < 1000; ++n) // was 10,000 but took too long (Beman)
{
boost::minstd_rand r;
tester<long, int>()(r);
tester<long, signed char>()(r);
tester<long, long>()(r);
tester<int, int>()(r);
tester<int, signed char>()(r);
tester<unsigned long, unsigned int>()(r);
tester<unsigned long, unsigned char>()(r);
tester<unsigned long, unsigned long>()(r);
tester<unsigned int, unsigned int>()(r);
tester<unsigned int, unsigned char>()(r);
tester_left<long, int>()(r);
tester_left<long, signed char>()(r);
tester_left<int, signed char>()(r);
tester_left<unsigned long, unsigned int>()(r);
tester_left<unsigned long, unsigned char>()(r);
tester_left<unsigned int, unsigned char>()(r);
}
cout << "Did random tester loop." << endl;
MyInt i1(1);
MyInt i2(2);
MyInt i;
BOOST_CHECK( i1.value() == 1 );
BOOST_CHECK( i2.value() == 2 );
BOOST_CHECK( i.value() == 0 );
cout << "Created MyInt objects.\n";
PRIVATE_EXPR_TEST( (i = i2), (i.value() == 2) );
BOOST_CHECK( static_cast<bool>(i2 == i) );
BOOST_CHECK( static_cast<bool>(i1 != i2) );
BOOST_CHECK( static_cast<bool>(i1 < i2) );
BOOST_CHECK( static_cast<bool>(i1 <= i2) );
BOOST_CHECK( static_cast<bool>(i <= i2) );
BOOST_CHECK( static_cast<bool>(i2 > i1) );
BOOST_CHECK( static_cast<bool>(i2 >= i1) );
BOOST_CHECK( static_cast<bool>(i2 >= i) );
PRIVATE_EXPR_TEST( (i = i1 + i2), (i.value() == 3) );
PRIVATE_EXPR_TEST( (i = i + i2), (i.value() == 5) );
PRIVATE_EXPR_TEST( (i = i - i1), (i.value() == 4) );
PRIVATE_EXPR_TEST( (i = i * i2), (i.value() == 8) );
PRIVATE_EXPR_TEST( (i = i / i2), (i.value() == 4) );
PRIVATE_EXPR_TEST( (i = i % ( i - i1 )), (i.value() == 1) );
PRIVATE_EXPR_TEST( (i = i2 + i2), (i.value() == 4) );
PRIVATE_EXPR_TEST( (i = i1 | i2 | i), (i.value() == 7) );
PRIVATE_EXPR_TEST( (i = i & i2), (i.value() == 2) );
PRIVATE_EXPR_TEST( (i = i + i1), (i.value() == 3) );
PRIVATE_EXPR_TEST( (i = i ^ i1), (i.value() == 2) );
PRIVATE_EXPR_TEST( (i = ( i + i1 ) * ( i2 | i1 )), (i.value() == 9) );
PRIVATE_EXPR_TEST( (i = i1 << i2), (i.value() == 4) );
PRIVATE_EXPR_TEST( (i = i2 >> i1), (i.value() == 1) );
cout << "Performed tests on MyInt objects.\n";
MyLong j1(1);
MyLong j2(2);
MyLong j;
BOOST_CHECK( j1.value() == 1 );
BOOST_CHECK( j2.value() == 2 );
BOOST_CHECK( j.value() == 0 );
cout << "Created MyLong objects.\n";
PRIVATE_EXPR_TEST( (j = j2), (j.value() == 2) );
BOOST_CHECK( static_cast<bool>(j2 == j) );
BOOST_CHECK( static_cast<bool>(2 == j) );
BOOST_CHECK( static_cast<bool>(j2 == 2) );
BOOST_CHECK( static_cast<bool>(j == j2) );
BOOST_CHECK( static_cast<bool>(j1 != j2) );
BOOST_CHECK( static_cast<bool>(j1 != 2) );
BOOST_CHECK( static_cast<bool>(1 != j2) );
BOOST_CHECK( static_cast<bool>(j1 < j2) );
BOOST_CHECK( static_cast<bool>(1 < j2) );
BOOST_CHECK( static_cast<bool>(j1 < 2) );
BOOST_CHECK( static_cast<bool>(j1 <= j2) );
BOOST_CHECK( static_cast<bool>(1 <= j2) );
BOOST_CHECK( static_cast<bool>(j1 <= j) );
BOOST_CHECK( static_cast<bool>(j <= j2) );
BOOST_CHECK( static_cast<bool>(2 <= j2) );
BOOST_CHECK( static_cast<bool>(j <= 2) );
BOOST_CHECK( static_cast<bool>(j2 > j1) );
BOOST_CHECK( static_cast<bool>(2 > j1) );
BOOST_CHECK( static_cast<bool>(j2 > 1) );
BOOST_CHECK( static_cast<bool>(j2 >= j1) );
BOOST_CHECK( static_cast<bool>(2 >= j1) );
BOOST_CHECK( static_cast<bool>(j2 >= 1) );
BOOST_CHECK( static_cast<bool>(j2 >= j) );
BOOST_CHECK( static_cast<bool>(2 >= j) );
BOOST_CHECK( static_cast<bool>(j2 >= 2) );
BOOST_CHECK( static_cast<bool>((j1 + 2) == 3) );
BOOST_CHECK( static_cast<bool>((1 + j2) == 3) );
PRIVATE_EXPR_TEST( (j = j1 + j2), (j.value() == 3) );
BOOST_CHECK( static_cast<bool>((j + 2) == 5) );
BOOST_CHECK( static_cast<bool>((3 + j2) == 5) );
PRIVATE_EXPR_TEST( (j = j + j2), (j.value() == 5) );
BOOST_CHECK( static_cast<bool>((j - 1) == 4) );
PRIVATE_EXPR_TEST( (j = j - j1), (j.value() == 4) );
BOOST_CHECK( static_cast<bool>((j * 2) == 8) );
BOOST_CHECK( static_cast<bool>((4 * j2) == 8) );
PRIVATE_EXPR_TEST( (j = j * j2), (j.value() == 8) );
BOOST_CHECK( static_cast<bool>((j / 2) == 4) );
PRIVATE_EXPR_TEST( (j = j / j2), (j.value() == 4) );
BOOST_CHECK( static_cast<bool>((j % 3) == 1) );
PRIVATE_EXPR_TEST( (j = j % ( j - j1 )), (j.value() == 1) );
PRIVATE_EXPR_TEST( (j = j2 + j2), (j.value() == 4) );
BOOST_CHECK( static_cast<bool>((1 | j2 | j) == 7) );
BOOST_CHECK( static_cast<bool>((j1 | 2 | j) == 7) );
BOOST_CHECK( static_cast<bool>((j1 | j2 | 4) == 7) );
PRIVATE_EXPR_TEST( (j = j1 | j2 | j), (j.value() == 7) );
BOOST_CHECK( static_cast<bool>((7 & j2) == 2) );
BOOST_CHECK( static_cast<bool>((j & 2) == 2) );
PRIVATE_EXPR_TEST( (j = j & j2), (j.value() == 2) );
PRIVATE_EXPR_TEST( (j = j | j1), (j.value() == 3) );
BOOST_CHECK( static_cast<bool>((3 ^ j1) == 2) );
BOOST_CHECK( static_cast<bool>((j ^ 1) == 2) );
PRIVATE_EXPR_TEST( (j = j ^ j1), (j.value() == 2) );
PRIVATE_EXPR_TEST( (j = ( j + j1 ) * ( j2 | j1 )), (j.value() == 9) );
BOOST_CHECK( static_cast<bool>((j1 << 2) == 4) );
BOOST_CHECK( static_cast<bool>((j2 << 1) == 4) );
PRIVATE_EXPR_TEST( (j = j1 << j2), (j.value() == 4) );
BOOST_CHECK( static_cast<bool>((j >> 2) == 1) );
BOOST_CHECK( static_cast<bool>((j2 >> 1) == 1) );
PRIVATE_EXPR_TEST( (j = j2 >> j1), (j.value() == 1) );
cout << "Performed tests on MyLong objects.\n";
MyChar k1(1);
MyChar k2(2);
MyChar k;
BOOST_CHECK( k1.value() == 1 );
BOOST_CHECK( k2.value() == 2 );
BOOST_CHECK( k.value() == 0 );
cout << "Created MyChar objects.\n";
PRIVATE_EXPR_TEST( (k = k2), (k.value() == 2) );
BOOST_CHECK( static_cast<bool>(k2 == k) );
BOOST_CHECK( static_cast<bool>(k1 != k2) );
BOOST_CHECK( static_cast<bool>(k1 < k2) );
BOOST_CHECK( static_cast<bool>(k1 <= k2) );
BOOST_CHECK( static_cast<bool>(k <= k2) );
BOOST_CHECK( static_cast<bool>(k2 > k1) );
BOOST_CHECK( static_cast<bool>(k2 >= k1) );
BOOST_CHECK( static_cast<bool>(k2 >= k) );
cout << "Performed tests on MyChar objects.\n";
MyShort l1(1);
MyShort l2(2);
MyShort l;
BOOST_CHECK( l1.value() == 1 );
BOOST_CHECK( l2.value() == 2 );
BOOST_CHECK( l.value() == 0 );
cout << "Created MyShort objects.\n";
PRIVATE_EXPR_TEST( (l = l2), (l.value() == 2) );
BOOST_CHECK( static_cast<bool>(l2 == l) );
BOOST_CHECK( static_cast<bool>(2 == l) );
BOOST_CHECK( static_cast<bool>(l2 == 2) );
BOOST_CHECK( static_cast<bool>(l == l2) );
BOOST_CHECK( static_cast<bool>(l1 != l2) );
BOOST_CHECK( static_cast<bool>(l1 != 2) );
BOOST_CHECK( static_cast<bool>(1 != l2) );
BOOST_CHECK( static_cast<bool>(l1 < l2) );
BOOST_CHECK( static_cast<bool>(1 < l2) );
BOOST_CHECK( static_cast<bool>(l1 < 2) );
BOOST_CHECK( static_cast<bool>(l1 <= l2) );
BOOST_CHECK( static_cast<bool>(1 <= l2) );
BOOST_CHECK( static_cast<bool>(l1 <= l) );
BOOST_CHECK( static_cast<bool>(l <= l2) );
BOOST_CHECK( static_cast<bool>(2 <= l2) );
BOOST_CHECK( static_cast<bool>(l <= 2) );
BOOST_CHECK( static_cast<bool>(l2 > l1) );
BOOST_CHECK( static_cast<bool>(2 > l1) );
BOOST_CHECK( static_cast<bool>(l2 > 1) );
BOOST_CHECK( static_cast<bool>(l2 >= l1) );
BOOST_CHECK( static_cast<bool>(2 >= l1) );
BOOST_CHECK( static_cast<bool>(l2 >= 1) );
BOOST_CHECK( static_cast<bool>(l2 >= l) );
BOOST_CHECK( static_cast<bool>(2 >= l) );
BOOST_CHECK( static_cast<bool>(l2 >= 2) );
cout << "Performed tests on MyShort objects.\n";
MyDoubleInt di1(1);
MyDoubleInt di2(2.);
MyDoubleInt half(0.5);
MyDoubleInt di;
MyDoubleInt tmp;
BOOST_CHECK( di1.value() == 1 );
BOOST_CHECK( di2.value() == 2 );
BOOST_CHECK( di2.value() == 2 );
BOOST_CHECK( di.value() == 0 );
cout << "Created MyDoubleInt objects.\n";
PRIVATE_EXPR_TEST( (di = di2), (di.value() == 2) );
BOOST_CHECK( static_cast<bool>(di2 == di) );
BOOST_CHECK( static_cast<bool>(2 == di) );
BOOST_CHECK( static_cast<bool>(di == 2) );
BOOST_CHECK( static_cast<bool>(di1 < di2) );
BOOST_CHECK( static_cast<bool>(1 < di2) );
BOOST_CHECK( static_cast<bool>(di1 <= di2) );
BOOST_CHECK( static_cast<bool>(1 <= di2) );
BOOST_CHECK( static_cast<bool>(di2 > di1) );
BOOST_CHECK( static_cast<bool>(di2 > 1) );
BOOST_CHECK( static_cast<bool>(di2 >= di1) );
BOOST_CHECK( static_cast<bool>(di2 >= 1) );
BOOST_CHECK( static_cast<bool>(di1 / di2 == half) );
BOOST_CHECK( static_cast<bool>(di1 / 2 == half) );
BOOST_CHECK( static_cast<bool>(1 / di2 == half) );
PRIVATE_EXPR_TEST( (tmp=di1), static_cast<bool>((tmp/=2) == half) );
PRIVATE_EXPR_TEST( (tmp=di1), static_cast<bool>((tmp/=di2) == half) );
BOOST_CHECK( static_cast<bool>(di1 * di2 == di2) );
BOOST_CHECK( static_cast<bool>(di1 * 2 == di2) );
BOOST_CHECK( static_cast<bool>(1 * di2 == di2) );
PRIVATE_EXPR_TEST( (tmp=di1), static_cast<bool>((tmp*=2) == di2) );
PRIVATE_EXPR_TEST( (tmp=di1), static_cast<bool>((tmp*=di2) == di2) );
BOOST_CHECK( static_cast<bool>(di2 - di1 == di1) );
BOOST_CHECK( static_cast<bool>(di2 - 1 == di1) );
BOOST_CHECK( static_cast<bool>(2 - di1 == di1) );
PRIVATE_EXPR_TEST( (tmp=di2), static_cast<bool>((tmp-=1) == di1) );
PRIVATE_EXPR_TEST( (tmp=di2), static_cast<bool>((tmp-=di1) == di1) );
BOOST_CHECK( static_cast<bool>(di1 + di1 == di2) );
BOOST_CHECK( static_cast<bool>(di1 + 1 == di2) );
BOOST_CHECK( static_cast<bool>(1 + di1 == di2) );
PRIVATE_EXPR_TEST( (tmp=di1), static_cast<bool>((tmp+=1) == di2) );
PRIVATE_EXPR_TEST( (tmp=di1), static_cast<bool>((tmp+=di1) == di2) );
cout << "Performed tests on MyDoubleInt objects.\n";
MyLongInt li1(1);
MyLongInt li2(2);
MyLongInt li;
MyLongInt tmp2;
BOOST_CHECK( li1.value() == 1 );
BOOST_CHECK( li2.value() == 2 );
BOOST_CHECK( li.value() == 0 );
cout << "Created MyLongInt objects.\n";
PRIVATE_EXPR_TEST( (li = li2), (li.value() == 2) );
BOOST_CHECK( static_cast<bool>(li2 == li) );
BOOST_CHECK( static_cast<bool>(2 == li) );
BOOST_CHECK( static_cast<bool>(li == 2) );
BOOST_CHECK( static_cast<bool>(li1 < li2) );
BOOST_CHECK( static_cast<bool>(1 < li2) );
BOOST_CHECK( static_cast<bool>(li1 <= li2) );
BOOST_CHECK( static_cast<bool>(1 <= li2) );
BOOST_CHECK( static_cast<bool>(li2 > li1) );
BOOST_CHECK( static_cast<bool>(li2 > 1) );
BOOST_CHECK( static_cast<bool>(li2 >= li1) );
BOOST_CHECK( static_cast<bool>(li2 >= 1) );
BOOST_CHECK( static_cast<bool>(li1 % li2 == li1) );
BOOST_CHECK( static_cast<bool>(li1 % 2 == li1) );
BOOST_CHECK( static_cast<bool>(1 % li2 == li1) );
PRIVATE_EXPR_TEST( (tmp2=li1), static_cast<bool>((tmp2%=2) == li1) );
PRIVATE_EXPR_TEST( (tmp2=li1), static_cast<bool>((tmp2%=li2) == li1) );
BOOST_CHECK( static_cast<bool>(li1 / li2 == 0) );
BOOST_CHECK( static_cast<bool>(li1 / 2 == 0) );
BOOST_CHECK( static_cast<bool>(1 / li2 == 0) );
PRIVATE_EXPR_TEST( (tmp2=li1), static_cast<bool>((tmp2/=2) == 0) );
PRIVATE_EXPR_TEST( (tmp2=li1), static_cast<bool>((tmp2/=li2) == 0) );
BOOST_CHECK( static_cast<bool>(li1 * li2 == li2) );
BOOST_CHECK( static_cast<bool>(li1 * 2 == li2) );
BOOST_CHECK( static_cast<bool>(1 * li2 == li2) );
PRIVATE_EXPR_TEST( (tmp2=li1), static_cast<bool>((tmp2*=2) == li2) );
PRIVATE_EXPR_TEST( (tmp2=li1), static_cast<bool>((tmp2*=li2) == li2) );
BOOST_CHECK( static_cast<bool>(li2 - li1 == li1) );
BOOST_CHECK( static_cast<bool>(li2 - 1 == li1) );
BOOST_CHECK( static_cast<bool>(2 - li1 == li1) );
PRIVATE_EXPR_TEST( (tmp2=li2), static_cast<bool>((tmp2-=1) == li1) );
PRIVATE_EXPR_TEST( (tmp2=li2), static_cast<bool>((tmp2-=li1) == li1) );
BOOST_CHECK( static_cast<bool>(li1 + li1 == li2) );
BOOST_CHECK( static_cast<bool>(li1 + 1 == li2) );
BOOST_CHECK( static_cast<bool>(1 + li1 == li2) );
PRIVATE_EXPR_TEST( (tmp2=li1), static_cast<bool>((tmp2+=1) == li2) );
PRIVATE_EXPR_TEST( (tmp2=li1), static_cast<bool>((tmp2+=li1) == li2) );
cout << "Performed tests on MyLongInt objects.\n";
return boost::exit_success;
}