summaryrefslogtreecommitdiff
path: root/libs/mpl/test/numeric_ops.cpp
blob: c598908d48e481aea75e45a2e0fc8c78984d814c (plain)
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

// Copyright Aleksey Gurtovoy 2003-2004
//
// 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/mpl for documentation.

// $Id: numeric_ops.cpp 49268 2008-10-11 06:26:17Z agurtovoy $
// $Date: 2008-10-10 23:26:17 -0700 (Fri, 10 Oct 2008) $
// $Revision: 49268 $

#include <boost/mpl/arithmetic.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/aux_/test.hpp>

struct complex_tag : int_<10> {};

template< typename Re, typename Im > struct complex
{
    typedef complex_tag tag;
    typedef complex type;
    typedef Re real;
    typedef Im imag;
};

template< typename C > struct real : C::real {};
template< typename C > struct imag : C::imag {};

namespace boost { namespace mpl {

template<> struct BOOST_MPL_AUX_NUMERIC_CAST< integral_c_tag,complex_tag >
{
    template< typename N > struct apply
        : complex< N, integral_c< typename N::value_type, 0 > >
    {
    };
};

template<>
struct plus_impl< complex_tag,complex_tag >
{
    template< typename N1, typename N2 > struct apply
        : complex<
              plus< typename N1::real, typename N2::real >
            , plus< typename N1::imag, typename N2::imag >
            >
    {
    };
};

template<>
struct times_impl< complex_tag,complex_tag >
{
    template< typename N1, typename N2 > struct apply
        : complex<
              minus< 
                  times< typename N1::real, typename N2::real >
                , times< typename N1::imag, typename N2::imag >
                >
            , plus<
                  times< typename N1::real, typename N2::imag >
                , times< typename N1::imag, typename N2::real >
                >
            >
    {
    };
};

template<>
struct equal_to_impl< complex_tag,complex_tag >
{
    template< typename N1, typename N2 > struct apply
        : and_<
              equal_to< typename N1::real, typename N2::real >
            , equal_to< typename N1::imag, typename N2::imag >
            >
    {
    };
};

}}


typedef int_<2> i;
typedef complex< int_<5>, int_<-1> > c1;
typedef complex< int_<-5>, int_<1> > c2;

MPL_TEST_CASE()
{
    typedef plus<c1,c2>::type r1;
    MPL_ASSERT_RELATION( real<r1>::value, ==, 0 );
    MPL_ASSERT_RELATION( imag<r1>::value, ==, 0 );

    typedef plus<c1,c1>::type r2;
    MPL_ASSERT_RELATION( real<r2>::value, ==, 10 );
    MPL_ASSERT_RELATION( imag<r2>::value, ==, -2 );

    typedef plus<c2,c2>::type r3;
    MPL_ASSERT_RELATION( real<r3>::value, ==, -10 );
    MPL_ASSERT_RELATION( imag<r3>::value, ==, 2 );

#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
    typedef plus<c1,i>::type r4;
    MPL_ASSERT_RELATION( real<r4>::value, ==, 7 );
    MPL_ASSERT_RELATION( imag<r4>::value, ==, -1 );

    typedef plus<i,c2>::type r5;
    MPL_ASSERT_RELATION( real<r5>::value, ==, -3 );
    MPL_ASSERT_RELATION( imag<r5>::value, ==, 1 );
#endif
}

MPL_TEST_CASE()
{
    typedef times<c1,c2>::type r1;
    MPL_ASSERT_RELATION( real<r1>::value, ==, -24 );
    MPL_ASSERT_RELATION( imag<r1>::value, ==, 10 );

    typedef times<c1,c1>::type r2;
    MPL_ASSERT_RELATION( real<r2>::value, ==, 24 );
    MPL_ASSERT_RELATION( imag<r2>::value, ==, -10 );

    typedef times<c2,c2>::type r3;
    MPL_ASSERT_RELATION( real<r3>::value, ==, 24 );
    MPL_ASSERT_RELATION( imag<r3>::value, ==, -10 );

#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
    typedef times<c1,i>::type r4;
    MPL_ASSERT_RELATION( real<r4>::value, ==, 10 );
    MPL_ASSERT_RELATION( imag<r4>::value, ==, -2 );

    typedef times<i,c2>::type r5;
    MPL_ASSERT_RELATION( real<r5>::value, ==, -10 );
    MPL_ASSERT_RELATION( imag<r5>::value, ==, 2 );
#endif
}

MPL_TEST_CASE()
{
    MPL_ASSERT(( equal_to<c1,c1> ));
    MPL_ASSERT(( equal_to<c2,c2> ));
    MPL_ASSERT_NOT(( equal_to<c1,c2> ));

    MPL_ASSERT(( equal_to<c1, complex< long_<5>, long_<-1> > > ));

#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
    MPL_ASSERT_NOT(( equal_to<c1,i> ));
    MPL_ASSERT_NOT(( equal_to<i,c2> ));
#endif
}