summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hof/test/flow.cpp
blob: 755374e476fd6f24a99e1919e6d012eb1a60b8fe (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
158
159
160
161
162
163
164
165
166
167
168
169
/*=============================================================================
    Copyright (c) 2017 Paul Fultz II
    flow.cpp
    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/hof/flow.hpp>
#include <boost/hof/function.hpp>
#include <boost/hof/lambda.hpp>
#include <boost/hof/placeholders.hpp>
#include <memory>
#include "test.hpp"

namespace flow_test {
struct increment
{
    template<class T>
    constexpr T operator()(T x) const noexcept
    {
        return x + 1;
    }
};

struct decrement
{
    template<class T>
    constexpr T operator()(T x) const noexcept
    {
        return x - 1;
    }
};

struct negate
{
    template<class T>
    constexpr T operator()(T x) const noexcept
    {
        return -x;
    }
};

struct increment_movable
{
    std::unique_ptr<int> n;
    increment_movable()
    : n(new int(1))
    {}
    template<class T>
    T operator()(T x) const
    {
        return x + *n;
    }
};

struct decrement_movable
{
    std::unique_ptr<int> n;
    decrement_movable()
    : n(new int(1))
    {}
    template<class T>
    T operator()(T x) const
    {
        return x - *n;
    }
};
#if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
BOOST_HOF_TEST_CASE()
{
    static_assert(noexcept(boost::hof::flow(increment(), decrement(), increment())(3)), "noexcept flow");
}
#endif

BOOST_HOF_TEST_CASE()
{
    BOOST_HOF_TEST_CHECK(boost::hof::flow(boost::hof::identity)(3) == 3);
    BOOST_HOF_TEST_CHECK(boost::hof::flow(boost::hof::identity, boost::hof::identity)(3) == 3);
    BOOST_HOF_TEST_CHECK(boost::hof::flow(boost::hof::identity, boost::hof::identity, boost::hof::identity)(3) == 3);

    BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(boost::hof::identity)(3) == 3);
    BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(boost::hof::identity, boost::hof::identity)(3) == 3);
    BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(boost::hof::identity, boost::hof::identity, boost::hof::identity)(3) == 3);
}

BOOST_HOF_TEST_CASE()
{
    int r = boost::hof::flow(increment(), decrement(), increment())(3);
    BOOST_HOF_TEST_CHECK(r == 4);
    BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(increment(), decrement(), increment())(3) == 4);
}

BOOST_HOF_TEST_CASE()
{
    int r = boost::hof::flow(increment(), negate(), decrement(), decrement())(3);
    BOOST_HOF_TEST_CHECK(r == -6);
    BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(increment(), negate(), decrement(), decrement())(3) == -6);
}
#ifndef _MSC_VER
BOOST_HOF_TEST_CASE()
{
    constexpr auto f = boost::hof::flow(increment(), decrement());
    static_assert(std::is_empty<decltype(f)>::value, "Compose function not empty");
    int r = f(3);
    BOOST_HOF_TEST_CHECK(r == 3);
    BOOST_HOF_STATIC_TEST_CHECK(f(3) == 3);
}
#endif

BOOST_HOF_TEST_CASE()
{
    STATIC_ASSERT_MOVE_ONLY(increment_movable);
    STATIC_ASSERT_MOVE_ONLY(decrement_movable);
    int r = boost::hof::flow(increment_movable(), decrement_movable(), increment_movable())(3);
    BOOST_HOF_TEST_CHECK(r == 4);
}

BOOST_HOF_TEST_CASE()
{
    const auto f = boost::hof::flow([](int i) { return i+1; }, [](int i) { return i-1; }, [](int i) { return i+1; });
#ifndef _MSC_VER
    static_assert(std::is_empty<decltype(f)>::value, "Compose function not empty");
#endif
    int r = f(3);
    BOOST_HOF_TEST_CHECK(r == 4);
}


BOOST_HOF_STATIC_FUNCTION(f_flow_single_function) = boost::hof::flow(increment());

BOOST_HOF_TEST_CASE()
{
    BOOST_HOF_TEST_CHECK(f_flow_single_function(3) == 4);
    BOOST_HOF_STATIC_TEST_CHECK(f_flow_single_function(3) == 4);
}

BOOST_HOF_STATIC_FUNCTION(f_flow_function) = boost::hof::flow(increment(), decrement(), increment());

BOOST_HOF_TEST_CASE()
{
    BOOST_HOF_TEST_CHECK(f_flow_function(3) == 4);
    BOOST_HOF_STATIC_TEST_CHECK(f_flow_function(3) == 4);
}

BOOST_HOF_STATIC_FUNCTION(f_flow_function_4) = boost::hof::flow(increment(), negate(), decrement(), decrement());

BOOST_HOF_TEST_CASE()
{
    BOOST_HOF_TEST_CHECK(f_flow_function_4(3) == -6);
    BOOST_HOF_STATIC_TEST_CHECK(f_flow_function_4(3) == -6);
}

BOOST_HOF_STATIC_LAMBDA_FUNCTION(f_flow_lambda) = boost::hof::flow(
    [](int i) { return i+1; }, 
    [](int i) { return i-1; }, 
    [](int i) { return i+1; }
);

BOOST_HOF_TEST_CASE()
{
    int r = f_flow_lambda(3);
    BOOST_HOF_TEST_CHECK(r == 4);
}

BOOST_HOF_TEST_CASE()
{
    BOOST_HOF_TEST_CHECK(boost::hof::flow(boost::hof::_1 + boost::hof::_1, boost::hof::_1 * boost::hof::_1)(3) == 36);
    BOOST_HOF_STATIC_TEST_CHECK(boost::hof::flow(boost::hof::_1 + boost::hof::_1, boost::hof::_1 * boost::hof::_1)(3) == 36);
}
}