summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hof/test/match.cpp
blob: 0d09dfdf66a893a10b1eb8d7af32028390b6ec11 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*=============================================================================
    Copyright (c) 2017 Paul Fultz II
    match.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/match.hpp>
#include <boost/hof/static.hpp>
#include <boost/hof/lambda.hpp>
#include <boost/hof/placeholders.hpp>
#include <boost/hof/limit.hpp>
#include "test.hpp"

#include <memory>

namespace test1
{
    struct int_class
    {
        int operator()(int) const
        {
            return 1;
        }
    };

    struct foo
    {};

    struct foo_class
    {
        foo operator()(foo) const
        {
            return foo();
        }
    };

    static constexpr boost::hof::static_<boost::hof::match_adaptor<int_class, foo_class> > fun = {};

    static_assert(std::is_same<int, decltype(fun(1))>::value, "Failed match");
    static_assert(std::is_same<foo, decltype(fun(foo()))>::value, "Failed match");
}

struct int_class
{
    constexpr int operator()(int) const
    {
        return 1;
    }
};

struct foo
{};

struct foo_class
{
    constexpr int operator()(foo) const
    {
        return 2;
    }
};

static constexpr boost::hof::match_adaptor<int_class, foo_class> fun = {};

BOOST_HOF_TEST_CASE()
{
    
    BOOST_HOF_TEST_CHECK(fun(1) == 1);
    BOOST_HOF_TEST_CHECK(fun(foo()) == 2);

    BOOST_HOF_STATIC_TEST_CHECK(fun(1) == 1);
    BOOST_HOF_STATIC_TEST_CHECK(fun(foo()) == 2);
};

BOOST_HOF_TEST_CASE()
{
    
    BOOST_HOF_TEST_CHECK(boost::hof::reveal(fun)(1) == 1);
    BOOST_HOF_TEST_CHECK(boost::hof::reveal(fun)(foo()) == 2);

    BOOST_HOF_STATIC_TEST_CHECK(boost::hof::reveal(fun)(1) == 1);
    BOOST_HOF_STATIC_TEST_CHECK(boost::hof::reveal(fun)(foo()) == 2);
};

BOOST_HOF_TEST_CASE()
{
    
    constexpr auto lam = boost::hof::match(
        BOOST_HOF_STATIC_LAMBDA(int) { return 1; },
        BOOST_HOF_STATIC_LAMBDA(foo) { return 2; }
    );
    
    BOOST_HOF_TEST_CHECK(lam(1) == 1);
    BOOST_HOF_TEST_CHECK(lam(foo()) == 2);
};

BOOST_HOF_TEST_CASE()
{
    int i = 0;
    auto lam = boost::hof::match(
        [&](int) { return i+1; },
        [&](foo) { return i+2; }
    );
// Disable this check on msvc, since lambdas might be default constructible
#ifndef _MSC_VER
    STATIC_ASSERT_NOT_DEFAULT_CONSTRUCTIBLE(decltype(lam));
#endif
    BOOST_HOF_TEST_CHECK(lam(1) == 1);
    BOOST_HOF_TEST_CHECK(lam(foo()) == 2);
};


BOOST_HOF_TEST_CASE()
{
    struct not_default_constructible
    {
        int i;
        not_default_constructible(int x) : i(x)
        {}
    };
    STATIC_ASSERT_NOT_DEFAULT_CONSTRUCTIBLE(not_default_constructible);
    not_default_constructible ndc = not_default_constructible(0);
    auto lam = boost::hof::match(
        [&](int) { return ndc.i+1; },
        [&](foo) { return ndc.i+2; }
    );
// Disable this check on msvc, since lambdas might be default constructible
#ifndef _MSC_VER
    STATIC_ASSERT_NOT_DEFAULT_CONSTRUCTIBLE(decltype(lam));
#endif
    
    BOOST_HOF_TEST_CHECK(lam(1) == 1);
    BOOST_HOF_TEST_CHECK(lam(foo()) == 2);
};


struct int_move_class
{
    std::unique_ptr<int> i;
    int_move_class() : i(new int(1))
    {}
    int operator()(int) const
    {
        return *i;
    }
};

struct foo_move_class
{
    std::unique_ptr<int> i;
    foo_move_class() : i(new int(2))
    {}
    int operator()(foo) const
    {
        return *i;
    }
};

BOOST_HOF_TEST_CASE()
{
    auto fun_move = boost::hof::match(int_move_class(), foo_move_class());
    BOOST_HOF_TEST_CHECK(fun_move(1) == 1);
    BOOST_HOF_TEST_CHECK(fun_move(foo()) == 2);

};

BOOST_HOF_TEST_CASE()
{
    const auto negate = (!boost::hof::_);
    const auto sub = (boost::hof::_ - boost::hof::_);
    BOOST_HOF_TEST_CHECK(boost::hof::match(negate, sub)(0) == negate(0));
    BOOST_HOF_TEST_CHECK(boost::hof::match(negate, sub)(0, 1) == sub(0, 1));
}

BOOST_HOF_TEST_CASE()
{
    const auto negate = (!boost::hof::_1);
    const auto sub = (boost::hof::_1 - boost::hof::_2);
    BOOST_HOF_TEST_CHECK(boost::hof::match(negate, sub)(0) == negate(0));
    // This is test is disabled because its invalid. It is valid to give more
    // parameters than what are used by the placeholders. So negate(0, 1) is a
    // valid call, which makes the following test fail with ambigous overload.
    // BOOST_HOF_TEST_CHECK(boost::hof::match(negate, sub)(0, 1) == sub(0, 1));
}

BOOST_HOF_TEST_CASE()
{
    const auto negate = boost::hof::limit_c<1>(!boost::hof::_1);
    const auto sub = boost::hof::limit_c<2>(boost::hof::_1 - boost::hof::_2);
    BOOST_HOF_TEST_CHECK(boost::hof::match(negate, sub)(0) == negate(0));
    // This test will pass because we have limited the number of parameters.
    BOOST_HOF_TEST_CHECK(boost::hof::match(negate, sub)(0, 1) == sub(0, 1));
}