summaryrefslogtreecommitdiff
path: root/libs/fusion/test/algorithm/transform.cpp
blob: 8609f61cd30f3c489e66ca7893f7cfeeb5a29d60 (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
156
157
/*=============================================================================
    Copyright (c) 2001-2011 Joel de Guzman
    Copyright (c) 2007 Dan Marsden

    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)
==============================================================================*/
#include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/sequence/comparison/equal_to.hpp>
#include <boost/fusion/algorithm/transformation/transform.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/type_traits/is_reference.hpp>

struct square
{
    template<typename Sig>
    struct result;

    template <typename T>
    struct result<square(T)>
    {
        typedef int type;
    };

    template <typename T>
    int operator()(T x) const
    {
        return x * x;
    }
};

struct add
{
    template<typename Sig>
    struct result;

    template <typename A, typename B>
    struct result<add(A, B)>
    {
        typedef int type;
    };

    template <typename A, typename B>
    int operator()(A a, B b) const
    {
        return a + b;
    }
};

struct unary_lvalue_transform
{
    template<typename Sig>
    struct result;

    template<typename T>
    struct result<unary_lvalue_transform(T&)>
    {
        typedef T* type;
    };

    template<typename T>
    T* operator()(T& t) const
    {
        return &t;
    }
};

int twice(int v)
{
    return v*2;
}

struct binary_lvalue_transform
{
    template<typename Sig>
    struct result;

    template<typename T0, typename T1>
    struct result<binary_lvalue_transform(T0&,T1&)>
    {
        typedef T0* type;
    };

    template<typename T0, typename T1>
    T0* operator()(T0& t0, T1&) const
    {
        return &t0;
    }
};

int
main()
{
    using namespace boost::fusion;
    using boost::mpl::range_c;

    std::cout << tuple_open('[');
    std::cout << tuple_close(']');
    std::cout << tuple_delimiter(", ");

/// Testing the transform

    {
        typedef range_c<int, 5, 9> sequence_type;
        sequence_type sequence;
        std::cout << boost::fusion::transform(sequence, square()) << std::endl;
        BOOST_TEST((boost::fusion::transform(sequence, square()) == make_vector(25, 36, 49, 64)));
    }

    {
        typedef range_c<int, 5, 9> mpl_list1;
        std::cout << boost::fusion::transform(mpl_list1(), square()) << std::endl;
        BOOST_TEST((boost::fusion::transform(mpl_list1(), square()) == make_vector(25, 36, 49, 64)));
    }
    
    {
        vector<int, int, int> tup(1, 2, 3);
        std::cout << boost::fusion::transform(tup, square()) << std::endl;
        BOOST_TEST((boost::fusion::transform(tup, square()) == make_vector(1, 4, 9)));
    }

    {
        vector<int, int, int> tup1(1, 2, 3);
        vector<int, int, int> tup2(4, 5, 6);
        std::cout << boost::fusion::transform(tup1, tup2, add()) << std::endl;
        BOOST_TEST((boost::fusion::transform(tup1, tup2, add()) == make_vector(5, 7, 9)));
    }

    {
        // Unary transform that requires lvalues, just check compilation
        vector<int, int, int> tup1(1, 2, 3);
        BOOST_TEST(at_c<0>(boost::fusion::transform(tup1, unary_lvalue_transform())) == &at_c<0>(tup1));
        BOOST_TEST(*begin(boost::fusion::transform(tup1, unary_lvalue_transform())) == &at_c<0>(tup1));
    }

    {
        vector<int, int, int> tup1(1, 2, 3);
        vector<int, int, int> tup2(4, 5, 6);
        BOOST_TEST(at_c<0>(boost::fusion::transform(tup1, tup2, binary_lvalue_transform())) == &at_c<0>(tup1));
        BOOST_TEST(*begin(boost::fusion::transform(tup1, tup2, binary_lvalue_transform())) == &at_c<0>(tup1));
    }

    {
        vector<int, int, int> tup1(1, 2, 3);
        BOOST_TEST(boost::fusion::transform(tup1, twice) == make_vector(2,4,6));
    }


    return boost::report_errors();
}