File: functional.cpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (253 lines) | stat: -rw-r--r-- 8,055 bytes parent folder | download | duplicates (4)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Range v3 library
//
//  Copyright Eric Niebler 2014-present
//  Copyright Casey Carter 2015
//
//  Use, modification and distribution is subject to 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)
//
// Project home: https://github.com/ericniebler/range-v3

#include <memory>
#include <range/v3/functional/invoke.hpp>
#include <range/v3/functional/not_fn.hpp>
#include <range/v3/view/filter.hpp>
#include "../simple_test.hpp"
#include "../test_utils.hpp"

CPP_assert(ranges::constructible_from<ranges::reference_wrapper<int>, int&>);
CPP_assert(!ranges::constructible_from<ranges::reference_wrapper<int>, int&&>);
CPP_assert(!ranges::constructible_from<ranges::reference_wrapper<int &&>, int&>);
CPP_assert(ranges::constructible_from<ranges::reference_wrapper<int &&>, int&&>);

namespace
{
    struct Integer
    {
        int i;
        operator int() const { return i; }
        bool odd() const { return (i % 2) != 0; }
    };

    enum class kind { lvalue, const_lvalue, rvalue, const_rvalue };

    std::ostream &operator<<(std::ostream &os, kind k)
    {
        const char* message = nullptr;
        switch (k) {
            case kind::lvalue:
                message = "lvalue";
                break;
            case kind::const_lvalue:
                message = "const_lvalue";
                break;
            case kind::rvalue:
                message = "rvalue";
                break;
            case kind::const_rvalue:
                message = "const_rvalue";
                break;
        }
        return os << message;
    }

    kind last_call;

    template<kind DisableKind>
    struct fn
    {
        bool operator()() &
        {
            last_call = kind::lvalue;
            return DisableKind != kind::lvalue;
        }
        bool operator()() const &
        {
            last_call = kind::const_lvalue;
            return DisableKind != kind::const_lvalue;
        }
        bool operator()() &&
        {
            last_call = kind::rvalue;
            return DisableKind != kind::rvalue;
        }
        bool operator()() const &&
        {
            last_call = kind::const_rvalue;
            return DisableKind != kind::const_rvalue;
        }
    };

    constexpr struct {
        template<typename T>
        constexpr T&& operator()(T&& arg) const noexcept {
            return (T&&)arg;
        }
    } h = {};

    struct A {
        int i = 13;
        constexpr int f() const noexcept { return 42; }
        constexpr /*c++14*/ int g(int j) { return 2 * j; }
    };

    constexpr int f() noexcept { return 13; }
    constexpr int g(int i) { return 2 * i + 1; }

    void test_invoke()
    {
        CHECK(ranges::invoke(f) == 13);
        // CHECK(noexcept(ranges::invoke(f) == 13));
        CHECK(ranges::invoke(g, 2) == 5);
        CHECK(ranges::invoke(h, 42) == 42);
        CHECK(noexcept(ranges::invoke(h, 42) == 42));
        {
            int i = 13;
            CHECK(&ranges::invoke(h, i) == &i);
            CHECK(noexcept(&ranges::invoke(h, i) == &i));
        }

        CHECK(ranges::invoke(&A::f, A{}) == 42);
        // CHECK(noexcept(ranges::invoke(&A::f, A{}) == 42));
        CHECK(ranges::invoke(&A::g, A{}, 2) == 4);
        {
            A a;
            const auto& ca = a;
            CHECK(ranges::invoke(&A::f, a) == 42);
            // CHECK(noexcept(ranges::invoke(&A::f, a) == 42));
            CHECK(ranges::invoke(&A::f, ca) == 42);
            // CHECK(noexcept(ranges::invoke(&A::f, ca) == 42));
            CHECK(ranges::invoke(&A::g, a, 2) == 4);
        }

        {
            A a;
            const auto& ca = a;
            CHECK(ranges::invoke(&A::f, &a) == 42);
            // CHECK(noexcept(ranges::invoke(&A::f, &a) == 42));
            CHECK(ranges::invoke(&A::f, &ca) == 42);
            // CHECK(noexcept(ranges::invoke(&A::f, &ca) == 42));
            CHECK(ranges::invoke(&A::g, &a, 2) == 4);
        }
        {
            std::unique_ptr<A> up(new A);
            CHECK(ranges::invoke(&A::f, up) == 42);
            CHECK(ranges::invoke(&A::g, up, 2) == 4);
        }
        {
            auto sp = std::make_shared<A>();
            CHECK(ranges::invoke(&A::f, sp) == 42);
            // CHECK(noexcept(ranges::invoke(&A::f, sp) == 42));
            CHECK(ranges::invoke(&A::g, sp, 2) == 4);
        }

        CHECK(ranges::invoke(&A::i, A{}) == 13);
        // CHECK(noexcept(ranges::invoke(&A::i, A{}) == 13));
        { int&& tmp = ranges::invoke(&A::i, A{}); (void)tmp; }

        {
            A a;
            const auto& ca = a;
            CHECK(ranges::invoke(&A::i, a) == 13);
            // CHECK(noexcept(ranges::invoke(&A::i, a) == 13));
            CHECK(ranges::invoke(&A::i, ca) == 13);
            // CHECK(noexcept(ranges::invoke(&A::i, ca) == 13));
            CHECK(ranges::invoke(&A::i, &a) == 13);
            // CHECK(noexcept(ranges::invoke(&A::i, &a) == 13));
            CHECK(ranges::invoke(&A::i, &ca) == 13);
            // CHECK(noexcept(ranges::invoke(&A::i, &ca) == 13));

            ranges::invoke(&A::i, a) = 0;
            CHECK(a.i == 0);
            ranges::invoke(&A::i, &a) = 1;
            CHECK(a.i == 1);
            CPP_assert(ranges::same_as<decltype(ranges::invoke(&A::i, ca)), const int&>);
            CPP_assert(ranges::same_as<decltype(ranges::invoke(&A::i, &ca)), const int&>);
        }

        {
            std::unique_ptr<A> up(new A);
            CHECK(ranges::invoke(&A::i, up) == 13);
            ranges::invoke(&A::i, up) = 0;
            CHECK(up->i == 0);
        }

        {
            auto sp = std::make_shared<A>();
            CHECK(ranges::invoke(&A::i, sp) == 13);
            ranges::invoke(&A::i, sp) = 0;
            CHECK(sp->i == 0);
        }

        // {
        //     struct B { int i = 42; constexpr int f() const { return i; } };
        //     constexpr B b;
        //     static_assert(b.i == 42, "");
        //     static_assert(b.f() == 42, "");
        //     static_assert(ranges::invoke_detail::impl(&B::i, b) == 42, "");
        //     static_assert(ranges::invoke_detail::impl(&B::i, &b) == 42, "");
        //     static_assert(ranges::invoke_detail::impl(&B::i, B{}) == 42, "");
        //     static_assert(ranges::invoke_detail::impl(&B::f, b) == 42, "");
        //     static_assert(ranges::invoke_detail::impl(&B::f, &b) == 42, "");
        //     static_assert(ranges::invoke_detail::impl(&B::f, B{}) == 42, "");
        // }
    }

} // unnamed namespace



int main()
{
    {
        // Check that not_fn works with callables
        Integer some_ints[] = {{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}};
        ::check_equal(some_ints | ranges::views::filter(ranges::not_fn(&Integer::odd)),
                      {0,2,4,6});
    }

    // Check that not_fn forwards value category
    {
        constexpr auto k = kind::lvalue;
        using F = fn<k>;
        auto f = ranges::not_fn(F{});
        CHECK(f() == true);
        CHECK(last_call == k);
    }
    {
        constexpr auto k = kind::const_lvalue;
        using F = fn<k>;
        auto const f = ranges::not_fn(F{});
        CHECK(f() == true);
        CHECK(last_call == k);
    }
    {
        constexpr auto k = kind::rvalue;
        using F = fn<k>;
        auto f = ranges::not_fn(F{});
        CHECK(std::move(f)() == true); // xvalue
        CHECK(last_call == k);

        CHECK(decltype(f){}() == true); // prvalue
        CHECK(last_call == k);
    }

#ifdef _WIN32
    {
        // Ensure that invocable accepts pointers to functions with non-default calling conventions.
        CPP_assert(ranges::invocable<void(__cdecl*)()>);
        CPP_assert(ranges::invocable<void(__stdcall*)()>);
        CPP_assert(ranges::invocable<void(__fastcall*)()>);
        CPP_assert(ranges::invocable<void(__thiscall*)()>);
#ifndef __MINGW32__
        CPP_assert(ranges::invocable<void(__vectorcall*)()>);
#endif
    }
#endif // _WIN32

    test_invoke();

    return ::test_result();
}