Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/utility/operators_test.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 29.6 KB
Line 
1//  Demonstrate and test boost/operators.hpp  -------------------------------//
2
3//  Copyright Beman Dawes 1999.  Distributed under the Boost
4//  Software License, Version 1.0. (See accompanying file
5//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7//  See http://www.boost.org/libs/utility for documentation.
8
9//  Revision History
10//  01 Oct 01 Added tests for "left" operators
11//            and new grouped operators. (Helmut Zeisel)
12//  20 May 01 Output progress messages.  Added tests for new operator
13//            templates.  Updated random number generator.  Changed tests to
14//            use Boost Test Tools library.  (Daryle Walker)
15//  04 Jun 00 Added regression test for a bug I found (David Abrahams)
16//  17 Jun 00 Fix for broken compilers (Aleksey Gurtovoy)
17//  ?? ??? 00 Major update to randomly test all one- and two- argument forms by
18//            wrapping integral types and comparing the results of operations
19//            to the results for the raw types (David Abrahams)
20//  12 Dec 99 Minor update, output confirmation message.
21//  15 Nov 99 Initial version
22
23#define BOOST_INCLUDE_MAIN
24
25#include <boost/config.hpp>                      // for BOOST_MSVC
26#include <boost/cstdlib.hpp>                     // for boost::exit_success
27#include <boost/operators.hpp>                   // for the tested items
28#include <boost/random/linear_congruential.hpp>  // for boost::minstd_rand
29#include <boost/test/test_tools.hpp>             // for main
30
31#include <iostream>  // for std::cout (std::endl indirectly)
32
33
34namespace
35{
36    // avoiding a template version of true_value so as to not confuse VC++
37    int true_value(int x) { return x; }
38    long true_value(long x) { return x; }
39    signed char true_value(signed char x) { return x; }
40    short true_value(short x) { return x; }
41    unsigned int true_value(unsigned int x) { return x; }
42    unsigned long true_value(unsigned long x) { return x; }
43    unsigned char true_value(unsigned char x) { return x; }
44    unsigned short true_value(unsigned short x) { return x; }
45
46    // The use of operators<> here tended to obscure
47    // interactions with certain compiler bugs
48    template <class T>
49    class Wrapped1
50        : boost::operators<Wrapped1<T> >
51        , boost::shiftable<Wrapped1<T> >
52    {
53    public:
54        explicit Wrapped1( T v = T() ) : _value(v) {}
55        T value() const { return _value; }
56
57        bool operator<(const Wrapped1& x) const { return _value < x._value; }
58        bool operator==(const Wrapped1& x) const { return _value == x._value; }
59       
60        Wrapped1& operator+=(const Wrapped1& x)
61          { _value += x._value; return *this; }
62        Wrapped1& operator-=(const Wrapped1& x)
63          { _value -= x._value; return *this; }
64        Wrapped1& operator*=(const Wrapped1& x)
65          { _value *= x._value; return *this; }
66        Wrapped1& operator/=(const Wrapped1& x)
67          { _value /= x._value; return *this; }
68        Wrapped1& operator%=(const Wrapped1& x)
69          { _value %= x._value; return *this; }
70        Wrapped1& operator|=(const Wrapped1& x)
71          { _value |= x._value; return *this; }
72        Wrapped1& operator&=(const Wrapped1& x)
73          { _value &= x._value; return *this; }
74        Wrapped1& operator^=(const Wrapped1& x)
75          { _value ^= x._value; return *this; }
76        Wrapped1& operator<<=(const Wrapped1& x)
77          { _value <<= x._value; return *this; }
78        Wrapped1& operator>>=(const Wrapped1& x)
79          { _value >>= x._value; return *this; }
80        Wrapped1& operator++()               { ++_value; return *this; }
81        Wrapped1& operator--()               { --_value; return *this; }
82       
83    private:
84        T _value;
85    };
86    template <class T>
87    T true_value(Wrapped1<T> x) { return x.value(); }   
88
89    template <class T, class U>
90    class Wrapped2
91        : boost::operators<Wrapped2<T, U> >
92        , boost::operators2<Wrapped2<T, U>, U>
93        , boost::shiftable1<Wrapped2<T, U>
94        , boost::shiftable2<Wrapped2<T, U>, U > >
95    {
96    public:
97        explicit Wrapped2( T v = T() ) : _value(v) {}
98        T value() const { return _value; }
99
100        bool operator<(const Wrapped2& x) const { return _value < x._value; }
101        bool operator==(const Wrapped2& x) const { return _value == x._value; }
102       
103        Wrapped2& operator+=(const Wrapped2& x)
104          { _value += x._value; return *this; }
105        Wrapped2& operator-=(const Wrapped2& x)
106          { _value -= x._value; return *this; }
107        Wrapped2& operator*=(const Wrapped2& x)
108          { _value *= x._value; return *this; }
109        Wrapped2& operator/=(const Wrapped2& x)
110          { _value /= x._value; return *this; }
111        Wrapped2& operator%=(const Wrapped2& x)
112          { _value %= x._value; return *this; }
113        Wrapped2& operator|=(const Wrapped2& x)
114          { _value |= x._value; return *this; }
115        Wrapped2& operator&=(const Wrapped2& x)
116          { _value &= x._value; return *this; }
117        Wrapped2& operator^=(const Wrapped2& x)
118          { _value ^= x._value; return *this; }
119        Wrapped2& operator<<=(const Wrapped2& x)
120          { _value <<= x._value; return *this; }
121        Wrapped2& operator>>=(const Wrapped2& x)
122          { _value >>= x._value; return *this; }
123        Wrapped2& operator++()                { ++_value; return *this; }
124        Wrapped2& operator--()                { --_value; return *this; }
125         
126        bool operator<(U u) const { return _value < u; }
127        bool operator>(U u) const { return _value > u; }
128        bool operator==(U u) const { return _value == u; }
129        Wrapped2& operator+=(U u) { _value += u; return *this; }
130        Wrapped2& operator-=(U u) { _value -= u; return *this; }
131        Wrapped2& operator*=(U u) { _value *= u; return *this; }
132        Wrapped2& operator/=(U u) { _value /= u; return *this; }
133        Wrapped2& operator%=(U u) { _value %= u; return *this; }
134        Wrapped2& operator|=(U u) { _value |= u; return *this; }
135        Wrapped2& operator&=(U u) { _value &= u; return *this; }
136        Wrapped2& operator^=(U u) { _value ^= u; return *this; }
137        Wrapped2& operator<<=(U u) { _value <<= u; return *this; }
138        Wrapped2& operator>>=(U u) { _value >>= u; return *this; }
139
140    private:
141        T _value;
142    };
143    template <class T, class U>
144    T true_value(Wrapped2<T,U> x) { return x.value(); }
145   
146    template <class T>
147    class Wrapped3
148        : boost::equivalent<Wrapped3<T> >
149        , boost::partially_ordered<Wrapped3<T> >
150        , boost::equality_comparable<Wrapped3<T> >
151    {
152    public:
153        explicit Wrapped3( T v = T() ) : _value(v) {}
154        T value() const { return _value; }
155
156        bool operator<(const Wrapped3& x) const { return _value < x._value; }
157       
158    private:
159        T _value;
160    };
161    template <class T>
162    T true_value(Wrapped3<T> x) { return x.value(); }   
163
164    template <class T, class U>
165    class Wrapped4
166        : boost::equality_comparable1<Wrapped4<T, U>
167        , boost::equivalent1<Wrapped4<T, U>
168        , boost::partially_ordered1<Wrapped4<T, U> > > >
169        , boost::partially_ordered2<Wrapped4<T, U>, U
170        , boost::equivalent2<Wrapped4<T, U>, U
171        , boost::equality_comparable2<Wrapped4<T, U>, U> > >
172    {
173    public:
174        explicit Wrapped4( T v = T() ) : _value(v) {}
175        T value() const { return _value; }
176
177        bool operator<(const Wrapped4& x) const { return _value < x._value; }
178         
179        bool operator<(U u) const { return _value < u; }
180        bool operator>(U u) const { return _value > u; }
181
182    private:
183        T _value;
184    };
185    template <class T, class U>
186    T true_value(Wrapped4<T,U> x) { return x.value(); }
187   
188    // U must be convertible to T
189    template <class T, class U>
190    class Wrapped5
191        : boost::ordered_field_operators2<Wrapped5<T, U>, U>
192        , boost::ordered_field_operators1<Wrapped5<T, U> >
193    {
194    public:
195        explicit Wrapped5( T v = T() ) : _value(v) {}
196
197        // Conversion from U to Wrapped5<T,U>
198        Wrapped5(U u) : _value(u) {}
199
200        T value() const { return _value; }
201        bool operator<(const Wrapped5& x) const { return _value < x._value; }
202        bool operator<(U u) const { return _value < u; }
203        bool operator>(U u) const { return _value > u; }
204        bool operator==(const Wrapped5& u) const { return _value == u._value; }
205        bool operator==(U u) const { return _value == u; }
206        Wrapped5& operator/=(const Wrapped5& u) { _value /= u._value; return *this;}
207        Wrapped5& operator/=(U u) { _value /= u; return *this;}
208        Wrapped5& operator*=(const Wrapped5& u) { _value *= u._value; return *this;}
209        Wrapped5& operator*=(U u) { _value *= u; return *this;}
210        Wrapped5& operator-=(const Wrapped5& u) { _value -= u._value; return *this;}
211        Wrapped5& operator-=(U u) { _value -= u; return *this;}
212        Wrapped5& operator+=(const Wrapped5& u) { _value += u._value; return *this;}
213        Wrapped5& operator+=(U u) { _value += u; return *this;}
214
215    private:
216        T _value;
217    };
218    template <class T, class U>
219    T true_value(Wrapped5<T,U> x) { return x.value(); }
220   
221    // U must be convertible to T
222    template <class T, class U>
223    class Wrapped6
224        : boost::ordered_euclidian_ring_operators2<Wrapped6<T, U>, U>
225        , boost::ordered_euclidian_ring_operators1<Wrapped6<T, U> >
226    {
227    public:
228        explicit Wrapped6( T v = T() ) : _value(v) {}
229
230        // Conversion from U to Wrapped6<T,U>
231        Wrapped6(U u) : _value(u) {}
232
233        T value() const { return _value; }
234        bool operator<(const Wrapped6& x) const { return _value < x._value; }
235        bool operator<(U u) const { return _value < u; }
236        bool operator>(U u) const { return _value > u; }
237        bool operator==(const Wrapped6& u) const { return _value == u._value; }
238        bool operator==(U u) const { return _value == u; }
239        Wrapped6& operator%=(const Wrapped6& u) { _value %= u._value; return *this;}
240        Wrapped6& operator%=(U u) { _value %= u; return *this;}
241        Wrapped6& operator/=(const Wrapped6& u) { _value /= u._value; return *this;}
242        Wrapped6& operator/=(U u) { _value /= u; return *this;}
243        Wrapped6& operator*=(const Wrapped6& u) { _value *= u._value; return *this;}
244        Wrapped6& operator*=(U u) { _value *= u; return *this;}
245        Wrapped6& operator-=(const Wrapped6& u) { _value -= u._value; return *this;}
246        Wrapped6& operator-=(U u) { _value -= u; return *this;}
247        Wrapped6& operator+=(const Wrapped6& u) { _value += u._value; return *this;}
248        Wrapped6& operator+=(U u) { _value += u; return *this;}
249
250    private:
251        T _value;
252    };
253    template <class T, class U>
254    T true_value(Wrapped6<T,U> x) { return x.value(); }
255   
256    //  MyInt uses only the single template-argument form of all_operators<>
257    typedef Wrapped1<int> MyInt;
258
259    typedef Wrapped2<long, long> MyLong;
260
261    typedef Wrapped3<signed char> MyChar;
262
263    typedef Wrapped4<short, short> MyShort;
264
265    typedef Wrapped5<double, int> MyDoubleInt;
266
267    typedef Wrapped6<long, int> MyLongInt;
268
269    template <class X1, class Y1, class X2, class Y2>
270    void sanity_check(X1 x1, Y1 y1, X2 x2, Y2 y2)
271    {
272        BOOST_CHECK( true_value(y1) == true_value(y2) );
273        BOOST_CHECK( true_value(x1) == true_value(x2) );
274    }
275
276    template <class X1, class Y1, class X2, class Y2>
277    void test_less_than_comparable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
278    {
279        BOOST_CHECK( (x1 < y1) == (x2 < y2) );
280        BOOST_CHECK( (x1 <= y1) == (x2 <= y2) );
281        BOOST_CHECK( (x1 >= y1) == (x2 >= y2) );
282        BOOST_CHECK( (x1 > y1) == (x2 > y2) );
283    }
284   
285    template <class X1, class Y1, class X2, class Y2>
286    void test_less_than_comparable(X1 x1, Y1 y1, X2 x2, Y2 y2)
287    {
288        sanity_check( x1, y1, x2, y2 );
289        test_less_than_comparable_aux( x1, y1, x2, y2 );
290        test_less_than_comparable_aux( y1, x1, y2, x2 );
291    }
292
293    template <class X1, class Y1, class X2, class Y2>
294    void test_equality_comparable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
295    {
296        BOOST_CHECK( (x1 == y1) == (x2 == y2) );
297        BOOST_CHECK( (x1 != y1) == (x2 != y2) );
298    }
299   
300    template <class X1, class Y1, class X2, class Y2>
301    void test_equality_comparable(X1 x1, Y1 y1, X2 x2, Y2 y2)
302    {
303        sanity_check( x1, y1, x2, y2 );
304        test_equality_comparable_aux( x1, y1, x2, y2 );
305        test_equality_comparable_aux( y1, x1, y2, x2 );
306    }
307
308    template <class X1, class Y1, class X2, class Y2>
309    void test_multipliable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
310    {
311        BOOST_CHECK( (x1 * y1).value() == (x2 * y2) );
312    }
313   
314    template <class X1, class Y1, class X2, class Y2>
315    void test_multipliable(X1 x1, Y1 y1, X2 x2, Y2 y2)
316    {
317        sanity_check( x1, y1, x2, y2 );
318        test_multipliable_aux( x1, y1, x2, y2 );
319        test_multipliable_aux( y1, x1, y2, x2 );
320    }
321
322  template <class A, class B>
323  void test_value_equality(A a, B b)
324  {
325      BOOST_CHECK(a.value() == b);
326  }
327 
328#define TEST_OP_R(op) test_value_equality(x1 op y1, x2 op y2)
329#define TEST_OP_L(op) test_value_equality(y1 op x1, y2 op x2)
330 
331    template <class X1, class Y1, class X2, class Y2>
332    void test_addable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
333    {
334        TEST_OP_R(+);
335    }
336   
337    template <class X1, class Y1, class X2, class Y2>
338    void test_addable(X1 x1, Y1 y1, X2 x2, Y2 y2)
339    {
340        sanity_check( x1, y1, x2, y2 );
341        test_addable_aux( x1, y1, x2, y2 );
342        test_addable_aux( y1, x1, y2, x2 );
343    }
344
345    template <class X1, class Y1, class X2, class Y2>
346    void test_subtractable(X1 x1, Y1 y1, X2 x2, Y2 y2)
347    {
348        sanity_check( x1, y1, x2, y2 );
349        TEST_OP_R(-);
350    }
351
352    template <class X1, class Y1, class X2, class Y2>
353    void test_subtractable_left(X1 x1, Y1 y1, X2 x2, Y2 y2)
354    {
355        sanity_check( x1, y1, x2, y2 );
356        TEST_OP_L(-);
357    }
358
359    template <class X1, class Y1, class X2, class Y2>
360    void test_dividable(X1 x1, Y1 y1, X2 x2, Y2 y2)
361    {
362        sanity_check( x1, y1, x2, y2 );
363        if ( y2 != 0 )
364            TEST_OP_R(/);
365    }
366   
367    template <class X1, class Y1, class X2, class Y2>
368    void test_dividable_left(X1 x1, Y1 y1, X2 x2, Y2 y2)
369    {
370        sanity_check( x1, y1, x2, y2 );
371        if ( x2 != 0 )
372            TEST_OP_L(/);
373    }
374
375    template <class X1, class Y1, class X2, class Y2>
376    void test_modable(X1 x1, Y1 y1, X2 x2, Y2 y2)
377    {
378        sanity_check( x1, y1, x2, y2 );
379        if ( y2 != 0 )
380            TEST_OP_R(%);
381    }
382   
383    template <class X1, class Y1, class X2, class Y2>
384    void test_modable_left(X1 x1, Y1 y1, X2 x2, Y2 y2)
385    {
386        sanity_check( x1, y1, x2, y2 );
387        if ( x2 != 0 )
388            TEST_OP_L(%);
389    }
390
391    template <class X1, class Y1, class X2, class Y2>
392    void test_xorable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
393    {
394        TEST_OP_R(^);
395    }
396   
397    template <class X1, class Y1, class X2, class Y2>
398    void test_xorable(X1 x1, Y1 y1, X2 x2, Y2 y2)
399    {
400        sanity_check( x1, y1, x2, y2 );
401        test_xorable_aux( x1, y1, x2, y2 );
402        test_xorable_aux( y1, x1, y2, x2 );
403    }
404   
405    template <class X1, class Y1, class X2, class Y2>
406    void test_andable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
407    {
408        TEST_OP_R(&);
409    }
410   
411    template <class X1, class Y1, class X2, class Y2>
412    void test_andable(X1 x1, Y1 y1, X2 x2, Y2 y2)
413    {
414        sanity_check( x1, y1, x2, y2 );
415        test_andable_aux( x1, y1, x2, y2 );
416        test_andable_aux( y1, x1, y2, x2 );
417    }
418   
419    template <class X1, class Y1, class X2, class Y2>
420    void test_orable_aux(X1 x1, Y1 y1, X2 x2, Y2 y2)
421    {
422        TEST_OP_R(|);
423    }
424   
425    template <class X1, class Y1, class X2, class Y2>
426    void test_orable(X1 x1, Y1 y1, X2 x2, Y2 y2)
427    {
428        sanity_check( x1, y1, x2, y2 );
429        test_orable_aux( x1, y1, x2, y2 );
430        test_orable_aux( y1, x1, y2, x2 );
431    }
432   
433    template <class X1, class Y1, class X2, class Y2>
434    void test_left_shiftable(X1 x1, Y1 y1, X2 x2, Y2 y2)
435    {
436        sanity_check( x1, y1, x2, y2 );
437        TEST_OP_R(<<);
438    }
439   
440    template <class X1, class Y1, class X2, class Y2>
441    void test_right_shiftable(X1 x1, Y1 y1, X2 x2, Y2 y2)
442    {
443        sanity_check( x1, y1, x2, y2 );
444        TEST_OP_R(>>);
445    }
446   
447    template <class X1, class X2>
448    void test_incrementable(X1 x1, X2 x2)
449    {
450        sanity_check( x1, x1, x2, x2 );
451        BOOST_CHECK( (x1++).value() == x2++ );
452        BOOST_CHECK( x1.value() == x2 );
453    }
454   
455    template <class X1, class X2>
456    void test_decrementable(X1 x1, X2 x2)
457    {
458        sanity_check( x1, x1, x2, x2 );
459        BOOST_CHECK( (x1--).value() == x2-- );
460        BOOST_CHECK( x1.value() == x2 );
461    }
462   
463    template <class X1, class Y1, class X2, class Y2>
464    void test_all(X1 x1, Y1 y1, X2 x2, Y2 y2)
465    {
466        test_less_than_comparable( x1, y1, x2, y2 );
467        test_equality_comparable( x1, y1, x2, y2 );
468        test_multipliable( x1, y1, x2, y2 );
469        test_addable( x1, y1, x2, y2 );
470        test_subtractable( x1, y1, x2, y2 );
471        test_dividable( x1, y1, x2, y2 );
472        test_modable( x1, y1, x2, y2 );
473        test_xorable( x1, y1, x2, y2 );
474        test_andable( x1, y1, x2, y2 );
475        test_orable( x1, y1, x2, y2 );
476        test_left_shiftable( x1, y1, x2, y2 );
477        test_right_shiftable( x1, y1, x2, y2 );
478        test_incrementable( x1, x2 );
479        test_decrementable( x1, x2 );
480    }
481   
482    template <class X1, class Y1, class X2, class Y2>
483    void test_left(X1 x1, Y1 y1, X2 x2, Y2 y2)
484    {
485        test_subtractable_left( x1, y1, x2, y2 );
486        test_dividable_left( x1, y1, x2, y2 );
487        test_modable_left( x1, y1, x2, y2 );
488     }
489
490    template <class Big, class Small>
491    struct tester
492    {
493        void operator()(boost::minstd_rand& randomizer) const
494        {
495            Big    b1 = Big( randomizer() );
496            Big    b2 = Big( randomizer() );
497            Small  s = Small( randomizer() );
498           
499            test_all( Wrapped1<Big>(b1), Wrapped1<Big>(b2), b1, b2 );
500            test_all( Wrapped2<Big, Small>(b1), s, b1, s );
501        }
502    };
503
504    template <class Big, class Small>
505    struct tester_left
506    {
507        void operator()(boost::minstd_rand& randomizer) const
508        {
509            Big    b1 = Big( randomizer() );
510            Small  s = Small( randomizer() );
511           
512            test_left( Wrapped6<Big, Small>(b1), s, b1, s );
513         }
514    };
515
516    // added as a regression test. We had a bug which this uncovered.
517    struct Point
518        : boost::addable<Point
519        , boost::subtractable<Point> >
520    {
521        Point( int h, int v ) : h(h), v(v) {}
522        Point() :h(0), v(0) {}
523        const Point& operator+=( const Point& rhs )
524            { h += rhs.h; v += rhs.v; return *this; }
525        const Point& operator-=( const Point& rhs )
526            { h -= rhs.h; v -= rhs.v; return *this; }
527
528        int h;
529        int v;
530    };
531
532} // unnamed namespace
533
534
535// workaround for MSVC bug; for some reasons the compiler doesn't instantiate
536// inherited operator templates at the moment it must, so the following
537// explicit instantiations force it to do that.
538
539#if defined(BOOST_MSVC) && (_MSC_VER <= 1200)
540template Wrapped1<int>;
541template Wrapped1<long>;
542template Wrapped1<unsigned int>;
543template Wrapped1<unsigned long>;
544
545template Wrapped2<int, int>;
546template Wrapped2<int, signed char>;
547template Wrapped2<long, signed char>;
548template Wrapped2<long, int>;
549template Wrapped2<long, long>;
550template Wrapped2<unsigned int, unsigned int>;
551template Wrapped2<unsigned int, unsigned char>;
552template Wrapped2<unsigned long, unsigned int>;
553template Wrapped2<unsigned long, unsigned char>;
554template Wrapped2<unsigned long, unsigned long>;
555
556template Wrapped6<long, int>;
557template Wrapped6<long, signed char>;
558template Wrapped6<int, signed char>;
559template Wrapped6<unsigned long, unsigned int>;
560template Wrapped6<unsigned long, unsigned char>;
561template Wrapped6<unsigned int, unsigned char>;
562#endif
563
564#define PRIVATE_EXPR_TEST(e, t)  BOOST_CHECK( ((e), (t)) )
565
566int
567test_main( int , char * [] )
568{
569    using std::cout;
570    using std::endl;
571
572    // Regression test.
573    Point x;
574    x = x + Point(3, 4);
575    x = x - Point(3, 4);
576   
577    cout << "Created point, and operated on it." << endl;
578   
579    for (int n = 0; n < 1000; ++n)  // was 10,000 but took too long (Beman)
580    {
581        boost::minstd_rand r;
582        tester<long, int>()(r);
583        tester<long, signed char>()(r);
584        tester<long, long>()(r);
585        tester<int, int>()(r);
586        tester<int, signed char>()(r);
587       
588        tester<unsigned long, unsigned int>()(r);
589        tester<unsigned long, unsigned char>()(r);
590        tester<unsigned long, unsigned long>()(r);
591        tester<unsigned int, unsigned int>()(r);
592        tester<unsigned int, unsigned char>()(r);
593
594        tester_left<long, int>()(r);
595        tester_left<long, signed char>()(r);
596        tester_left<int, signed char>()(r);
597
598        tester_left<unsigned long, unsigned int>()(r);
599        tester_left<unsigned long, unsigned char>()(r);
600        tester_left<unsigned int, unsigned char>()(r);
601    }
602   
603    cout << "Did random tester loop." << endl;
604
605    MyInt i1(1);
606    MyInt i2(2);
607    MyInt i;
608
609    BOOST_CHECK( i1.value() == 1 );
610    BOOST_CHECK( i2.value() == 2 );
611    BOOST_CHECK( i.value() == 0 );
612
613    cout << "Created MyInt objects.\n";
614
615    PRIVATE_EXPR_TEST( (i = i2), (i.value() == 2) );
616
617    BOOST_CHECK( i2 == i );
618    BOOST_CHECK( i1 != i2 );
619    BOOST_CHECK( i1 <  i2 );
620    BOOST_CHECK( i1 <= i2 );
621    BOOST_CHECK( i <= i2 );
622    BOOST_CHECK( i2 >  i1 );
623    BOOST_CHECK( i2 >= i1 );
624    BOOST_CHECK( i2 >= i );
625
626    PRIVATE_EXPR_TEST( (i = i1 + i2), (i.value() == 3) );
627    PRIVATE_EXPR_TEST( (i = i + i2), (i.value() == 5) );
628    PRIVATE_EXPR_TEST( (i = i - i1), (i.value() == 4) );
629    PRIVATE_EXPR_TEST( (i = i * i2), (i.value() == 8) );
630    PRIVATE_EXPR_TEST( (i = i / i2), (i.value() == 4) );
631    PRIVATE_EXPR_TEST( (i = i % ( i - i1 )), (i.value() == 1) );
632    PRIVATE_EXPR_TEST( (i = i2 + i2), (i.value() == 4) );
633    PRIVATE_EXPR_TEST( (i = i1 | i2 | i), (i.value() == 7) );
634    PRIVATE_EXPR_TEST( (i = i & i2), (i.value() == 2) );
635    PRIVATE_EXPR_TEST( (i = i + i1), (i.value() == 3) );
636    PRIVATE_EXPR_TEST( (i = i ^ i1), (i.value() == 2) );
637    PRIVATE_EXPR_TEST( (i = ( i + i1 ) * ( i2 | i1 )), (i.value() == 9) );
638
639    PRIVATE_EXPR_TEST( (i = i1 << i2), (i.value() == 4) );
640    PRIVATE_EXPR_TEST( (i = i2 >> i1), (i.value() == 1) );
641
642    cout << "Performed tests on MyInt objects.\n";
643
644    MyLong j1(1);
645    MyLong j2(2);
646    MyLong j;
647
648    BOOST_CHECK( j1.value() == 1 );
649    BOOST_CHECK( j2.value() == 2 );
650    BOOST_CHECK( j.value() == 0 );
651
652    cout << "Created MyLong objects.\n";
653
654    PRIVATE_EXPR_TEST( (j = j2), (j.value() == 2) );
655   
656    BOOST_CHECK( j2 == j );
657    BOOST_CHECK( 2 == j );
658    BOOST_CHECK( j2 == 2 );   
659    BOOST_CHECK( j == j2 );
660    BOOST_CHECK( j1 != j2 );
661    BOOST_CHECK( j1 != 2 );
662    BOOST_CHECK( 1 != j2 );
663    BOOST_CHECK( j1 <  j2 );
664    BOOST_CHECK( 1 <  j2 );
665    BOOST_CHECK( j1 <  2 );
666    BOOST_CHECK( j1 <= j2 );
667    BOOST_CHECK( 1 <= j2 );
668    BOOST_CHECK( j1 <= j );
669    BOOST_CHECK( j <= j2 );
670    BOOST_CHECK( 2 <= j2 );
671    BOOST_CHECK( j <= 2 );
672    BOOST_CHECK( j2 >  j1 );
673    BOOST_CHECK( 2 >  j1 );
674    BOOST_CHECK( j2 >  1 );
675    BOOST_CHECK( j2 >= j1 );
676    BOOST_CHECK( 2 >= j1 );
677    BOOST_CHECK( j2 >= 1 );
678    BOOST_CHECK( j2 >= j );
679    BOOST_CHECK( 2 >= j );
680    BOOST_CHECK( j2 >= 2 );
681
682    BOOST_CHECK( (j1 + 2) == 3 );
683    BOOST_CHECK( (1 + j2) == 3 );
684    PRIVATE_EXPR_TEST( (j = j1 + j2), (j.value() == 3) );
685   
686    BOOST_CHECK( (j + 2) == 5 );
687    BOOST_CHECK( (3 + j2) == 5 );
688    PRIVATE_EXPR_TEST( (j = j + j2), (j.value() == 5) );
689   
690    BOOST_CHECK( (j - 1) == 4 );
691    PRIVATE_EXPR_TEST( (j = j - j1), (j.value() == 4) );
692   
693    BOOST_CHECK( (j * 2) == 8 );
694    BOOST_CHECK( (4 * j2) == 8 );
695    PRIVATE_EXPR_TEST( (j = j * j2), (j.value() == 8) );
696   
697    BOOST_CHECK( (j / 2) == 4 );
698    PRIVATE_EXPR_TEST( (j = j / j2), (j.value() == 4) );
699   
700    BOOST_CHECK( (j % 3) == 1 );
701    PRIVATE_EXPR_TEST( (j = j % ( j - j1 )), (j.value() == 1) );
702   
703    PRIVATE_EXPR_TEST( (j = j2 + j2), (j.value() == 4) );
704   
705    BOOST_CHECK( (1 | j2 | j) == 7 );
706    BOOST_CHECK( (j1 | 2 | j) == 7 );
707    BOOST_CHECK( (j1 | j2 | 4) == 7 );
708    PRIVATE_EXPR_TEST( (j = j1 | j2 | j), (j.value() == 7) );
709   
710    BOOST_CHECK( (7 & j2) == 2 );
711    BOOST_CHECK( (j & 2) == 2 );
712    PRIVATE_EXPR_TEST( (j = j & j2), (j.value() == 2) );
713   
714    PRIVATE_EXPR_TEST( (j = j | j1), (j.value() == 3) );
715   
716    BOOST_CHECK( (3 ^ j1) == 2 );
717    BOOST_CHECK( (j ^ 1) == 2 );
718    PRIVATE_EXPR_TEST( (j = j ^ j1), (j.value() == 2) );
719   
720    PRIVATE_EXPR_TEST( (j = ( j + j1 ) * ( j2 | j1 )), (j.value() == 9) );
721
722    BOOST_CHECK( (j1 << 2) == 4 );
723    BOOST_CHECK( (j2 << 1) == 4 );
724    PRIVATE_EXPR_TEST( (j = j1 << j2), (j.value() == 4) );
725
726    BOOST_CHECK( (j >> 2) == 1 );
727    BOOST_CHECK( (j2 >> 1) == 1 );
728    PRIVATE_EXPR_TEST( (j = j2 >> j1), (j.value() == 1) );
729   
730    cout << "Performed tests on MyLong objects.\n";
731
732    MyChar k1(1);
733    MyChar k2(2);
734    MyChar k;
735
736    BOOST_CHECK( k1.value() == 1 );
737    BOOST_CHECK( k2.value() == 2 );
738    BOOST_CHECK( k.value() == 0 );
739
740    cout << "Created MyChar objects.\n";
741
742    PRIVATE_EXPR_TEST( (k = k2), (k.value() == 2) );
743
744    BOOST_CHECK( k2 == k );
745    BOOST_CHECK( k1 != k2 );
746    BOOST_CHECK( k1 <  k2 );
747    BOOST_CHECK( k1 <= k2 );
748    BOOST_CHECK( k <= k2 );
749    BOOST_CHECK( k2 >  k1 );
750    BOOST_CHECK( k2 >= k1 );
751    BOOST_CHECK( k2 >= k );
752   
753    cout << "Performed tests on MyChar objects.\n";
754
755    MyShort l1(1);
756    MyShort l2(2);
757    MyShort l;
758
759    BOOST_CHECK( l1.value() == 1 );
760    BOOST_CHECK( l2.value() == 2 );
761    BOOST_CHECK( l.value() == 0 );
762
763    cout << "Created MyShort objects.\n";
764
765    PRIVATE_EXPR_TEST( (l = l2), (l.value() == 2) );
766   
767    BOOST_CHECK( l2 == l );
768    BOOST_CHECK( 2 == l );
769    BOOST_CHECK( l2 == 2 );   
770    BOOST_CHECK( l == l2 );
771    BOOST_CHECK( l1 != l2 );
772    BOOST_CHECK( l1 != 2 );
773    BOOST_CHECK( 1 != l2 );
774    BOOST_CHECK( l1 <  l2 );
775    BOOST_CHECK( 1 <  l2 );
776    BOOST_CHECK( l1 <  2 );
777    BOOST_CHECK( l1 <= l2 );
778    BOOST_CHECK( 1 <= l2 );
779    BOOST_CHECK( l1 <= l );
780    BOOST_CHECK( l <= l2 );
781    BOOST_CHECK( 2 <= l2 );
782    BOOST_CHECK( l <= 2 );
783    BOOST_CHECK( l2 >  l1 );
784    BOOST_CHECK( 2 >  l1 );
785    BOOST_CHECK( l2 >  1 );
786    BOOST_CHECK( l2 >= l1 );
787    BOOST_CHECK( 2 >= l1 );
788    BOOST_CHECK( l2 >= 1 );
789    BOOST_CHECK( l2 >= l );
790    BOOST_CHECK( 2 >= l );
791    BOOST_CHECK( l2 >= 2 );
792   
793    cout << "Performed tests on MyShort objects.\n";
794   
795    MyDoubleInt di1(1);
796    MyDoubleInt di2(2.);
797    MyDoubleInt half(0.5);
798    MyDoubleInt di;
799    MyDoubleInt tmp;
800
801    BOOST_CHECK( di1.value() == 1 );
802    BOOST_CHECK( di2.value() == 2 );
803    BOOST_CHECK( di2.value() == 2 );
804    BOOST_CHECK( di.value() == 0 );
805
806    cout << "Created MyDoubleInt objects.\n";
807
808    PRIVATE_EXPR_TEST( (di = di2), (di.value() == 2) );
809   
810    BOOST_CHECK( di2 == di );
811    BOOST_CHECK( 2 == di );
812    BOOST_CHECK( di == 2 );
813    BOOST_CHECK( di1 < di2 );
814    BOOST_CHECK( 1 < di2 );
815    BOOST_CHECK( di1 <= di2 );
816    BOOST_CHECK( 1 <= di2 );
817    BOOST_CHECK( di2 > di1 );
818    BOOST_CHECK( di2 > 1 );
819    BOOST_CHECK( di2 >= di1 );
820    BOOST_CHECK( di2 >= 1 );
821    BOOST_CHECK( di1 / di2 == half );
822    BOOST_CHECK( di1 / 2 == half );
823    BOOST_CHECK( 1 / di2 == half );
824    PRIVATE_EXPR_TEST( (tmp=di1), ((tmp/=2) == half) );
825    PRIVATE_EXPR_TEST( (tmp=di1), ((tmp/=di2) == half) );
826    BOOST_CHECK( di1 * di2 == di2 );
827    BOOST_CHECK( di1 * 2 == di2 );
828    BOOST_CHECK( 1 * di2 == di2 );
829    PRIVATE_EXPR_TEST( (tmp=di1), ((tmp*=2) == di2) );
830    PRIVATE_EXPR_TEST( (tmp=di1), ((tmp*=di2) == di2) );
831    BOOST_CHECK( di2 - di1 == di1 );
832    BOOST_CHECK( di2 - 1 == di1 );
833    BOOST_CHECK( 2 - di1 == di1 );
834    PRIVATE_EXPR_TEST( (tmp=di2), ((tmp-=1) == di1) );
835    PRIVATE_EXPR_TEST( (tmp=di2), ((tmp-=di1) == di1) );
836    BOOST_CHECK( di1 + di1 == di2 );
837    BOOST_CHECK( di1 + 1 == di2 );
838    BOOST_CHECK( 1 + di1 == di2 );
839    PRIVATE_EXPR_TEST( (tmp=di1), ((tmp+=1) == di2) );
840    PRIVATE_EXPR_TEST( (tmp=di1), ((tmp+=di1) == di2) );
841
842    cout << "Performed tests on MyDoubleInt objects.\n";
843
844    MyLongInt li1(1);
845    MyLongInt li2(2);
846    MyLongInt li;
847    MyLongInt tmp2;
848
849    BOOST_CHECK( li1.value() == 1 );
850    BOOST_CHECK( li2.value() == 2 );
851    BOOST_CHECK( li.value() == 0 );
852
853    cout << "Created MyLongInt objects.\n";
854
855    PRIVATE_EXPR_TEST( (li = li2), (li.value() == 2) );
856   
857    BOOST_CHECK( li2 == li );
858    BOOST_CHECK( 2 == li );
859    BOOST_CHECK( li == 2 );
860    BOOST_CHECK( li1 < li2 );
861    BOOST_CHECK( 1 < li2 );
862    BOOST_CHECK( li1 <= li2 );
863    BOOST_CHECK( 1 <= li2 );
864    BOOST_CHECK( li2 > li1 );
865    BOOST_CHECK( li2 > 1 );
866    BOOST_CHECK( li2 >= li1 );
867    BOOST_CHECK( li2 >= 1 );
868    BOOST_CHECK( li1 % li2 == li1 );
869    BOOST_CHECK( li1 % 2 == li1 );
870    BOOST_CHECK( 1 % li2 == li1 );
871    PRIVATE_EXPR_TEST( (tmp2=li1), ((tmp2%=2) == li1) );
872    PRIVATE_EXPR_TEST( (tmp2=li1), ((tmp2%=li2) == li1) );
873    BOOST_CHECK( li1 / li2 == 0 );
874    BOOST_CHECK( li1 / 2 == 0 );
875    BOOST_CHECK( 1 / li2 == 0 );
876    PRIVATE_EXPR_TEST( (tmp2=li1), ((tmp2/=2) == 0) );
877    PRIVATE_EXPR_TEST( (tmp2=li1), ((tmp2/=li2) == 0) );
878    BOOST_CHECK( li1 * li2 == li2 );
879    BOOST_CHECK( li1 * 2 == li2 );
880    BOOST_CHECK( 1 * li2 == li2 );
881    PRIVATE_EXPR_TEST( (tmp2=li1), ((tmp2*=2) == li2) );
882    PRIVATE_EXPR_TEST( (tmp2=li1), ((tmp2*=li2) == li2) );
883    BOOST_CHECK( li2 - li1 == li1 );
884    BOOST_CHECK( li2 - 1 == li1 );
885    BOOST_CHECK( 2 - li1 == li1 );
886    PRIVATE_EXPR_TEST( (tmp2=li2), ((tmp2-=1) == li1) );
887    PRIVATE_EXPR_TEST( (tmp2=li2), ((tmp2-=li1) == li1) );
888    BOOST_CHECK( li1 + li1 == li2 );
889    BOOST_CHECK( li1 + 1 == li2 );
890    BOOST_CHECK( 1 + li1 == li2 );
891    PRIVATE_EXPR_TEST( (tmp2=li1), ((tmp2+=1) == li2) );
892    PRIVATE_EXPR_TEST( (tmp2=li1), ((tmp2+=li1) == li2) );
893
894    cout << "Performed tests on MyLongInt objects.\n";
895
896    return boost::exit_success;
897}
Note: See TracBrowser for help on using the repository browser.