summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/global/qxp/function_ref/tst_qxp_function_ref.cpp
blob: 6264e6a5ae9f74abac0e03d13da7ea330a6a8500 (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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtCore/qxpfunctional.h>

#include <QTest>

#include <type_traits>

//  checking dependency q20::remove_cvref_t:
#define CHECK(in, out) \
    static_assert(std::is_same_v<q20::remove_cvref_t< in >, out >)
CHECK(int, int);
CHECK(const int, int);
CHECK(int &, int);
CHECK(const int &, int);
CHECK(int &&, int);
CHECK(const int &&, int);
CHECK(int *, int *);
CHECK(const int *, const int *);
CHECK(int[4], int[4]);
CHECK(const int (&)[4], int[4]);
#undef CHECK

template <typename T> constexpr inline bool
is_noexcept_function_ref_helper_v = false;
template <typename R, typename...Args> constexpr inline bool
is_noexcept_function_ref_helper_v<qxp::function_ref<R(Args...) noexcept(true)>> = true;
template <typename R, typename...Args> constexpr inline bool
is_noexcept_function_ref_helper_v<qxp::function_ref<R(Args...) const noexcept(true)>> = true;

template <typename T> constexpr inline bool
is_noexcept_function_ref_v = is_noexcept_function_ref_helper_v<q20::remove_cvref_t<T>>;

class tst_qxp_function_ref : public QObject
{
    Q_OBJECT
public:
    using QObject::QObject;

private Q_SLOTS:
    void basics();
    void constOverloads();
    void constExpr();
    void voidReturning();
    void ctad();
};

void tst_qxp_function_ref::basics()
{
    static_assert(std::is_trivially_copyable_v<qxp::function_ref<int(int)>>);
    static_assert(std::is_trivially_copyable_v<qxp::function_ref<int()>>);
    static_assert(std::is_trivially_copyable_v<qxp::function_ref<void()>>);

    {
        Q_CONSTINIT static int invoked = 0;
        auto lambda = [](int i) noexcept { ++invoked; return i; };
        const qxp::function_ref<int(int)> f = lambda;
        QCOMPARE(invoked, 0);
        QCOMPARE(f(42), 42);
        QCOMPARE(invoked, 1);

        const int fourtyTwo = 42;

        const qxp::function_ref<int(int) noexcept> f2 = std::move(lambda);
        QCOMPARE(invoked, 1);
        QCOMPARE(f2(fourtyTwo), 42);
        QCOMPARE(invoked, 2);

        int (*fpr)(int) = lambda;

        const qxp::function_ref f3 = fpr;
        static_assert(!is_noexcept_function_ref_v<decltype(f3)>);
        QCOMPARE(invoked, 2);
        QCOMPARE(f3(42), 42);
        QCOMPARE(invoked, 3);

        int (*fpr2)(int) noexcept = lambda;

        const qxp::function_ref f4 = fpr2;
        static_assert(is_noexcept_function_ref_v<decltype(f4)>);
        QCOMPARE(invoked, 3);
        QCOMPARE(f4(42), 42);
        QCOMPARE(invoked, 4);
    }
    {
        Q_CONSTINIT static int invoked = 0;
        auto lambda = [] { ++invoked; return 42; };
        const qxp::function_ref<int()> f = lambda;
        QCOMPARE(invoked, 0);
        QCOMPARE(f(), 42);
        QCOMPARE(invoked, 1);

        const qxp::function_ref<int()> f2 = std::move(lambda);
        QCOMPARE(invoked, 1);
        QCOMPARE(f2(), 42);
        QCOMPARE(invoked, 2);

        int (*fpr)() = lambda;

        const qxp::function_ref f3 = fpr;
        static_assert(!is_noexcept_function_ref_v<decltype(f3)>);
        QCOMPARE(invoked, 2);
        QCOMPARE(f3(), 42);
        QCOMPARE(invoked, 3);
    }
    {
        Q_CONSTINIT static int invoked = 0;
        auto lambda = [] { ++invoked; };
        const qxp::function_ref<void()> f = lambda;
        QCOMPARE(invoked, 0);
        f();
        QCOMPARE(invoked, 1);

        const qxp::function_ref<void()> f2 = std::move(lambda);
        QCOMPARE(invoked, 1);
        f2();
        QCOMPARE(invoked, 2);

        void (*fpr)() = lambda;

        const qxp::function_ref f3 = fpr;
        QCOMPARE(invoked, 2);
        f3();
        QCOMPARE(invoked, 3);
    }
}

void tst_qxp_function_ref::constOverloads()
{
    auto func_c = [](qxp::function_ref<int() const> callable)
    {
        return callable();
    };
    auto func_m = [](qxp::function_ref<int() /*mutable*/> callable)
    {
        return callable();
    };

    struct S
    {
        int operator()() { return 1; }
        int operator()() const { return 2; }
    };
    S s;
    QCOMPARE(func_c(s), 2);
    QCOMPARE(func_m(s), 1);
    const S cs;
    QCOMPARE(func_c(cs), 2);
#if 0
    // this should not compile (and doesn't, but currently fails with an error in the impl,
    // not by failing a constructor constaint → spec issue?).
    QCOMPARE(func_m(cs), 2);
#endif
}

void tst_qxp_function_ref::constExpr()
{
    Q_CONSTINIT static int invoked = 0;
    {
        Q_CONSTINIT static auto lambda = [] (int i) { ++invoked; return i; };
        // the function object constructor is constexpr, so this should be constinit:
        Q_CONSTINIT static qxp::function_ref<int(int)> f = lambda;

        QCOMPARE(invoked, 0);
        QCOMPARE(f(15), 15);
        QCOMPARE(invoked, 1);
    }
    {
        constexpr static auto lambda = [] (int i) { ++invoked; return i; };
        // the function object constructor is constexpr, so this should be constinit:
        Q_CONSTINIT static qxp::function_ref<int(int) const> f = lambda;

        QCOMPARE(invoked, 1);
        QCOMPARE(f(51), 51);
        QCOMPARE(invoked, 2);

#if 0 // ### should this work?:
        Q_CONSTINIT static qxp::function_ref<int(int)> f2 = lambda;

        QCOMPARE(invoked, 2);
        QCOMPARE(f(150), 150);
        QCOMPARE(invoked, 3);
#endif

    }
}

int  i_f_i_nx(int i) noexcept { return i; }
void v_f_i_nx(int)   noexcept {}
int  i_f_v_nx()      noexcept { return 42; }
void v_f_v_nx()      noexcept {}

int  i_f_i_ex(int i) { return i; }
void v_f_i_ex(int)   {}
int  i_f_v_ex()      { return 42; }
void v_f_v_ex()      {}

void tst_qxp_function_ref::voidReturning()
{
    // check that "casting" int to void returns works:

    using Fi = qxp::function_ref<void(int)>;
    using Fv = qxp::function_ref<void()>;

    {
        Fi fi = i_f_i_nx;
        fi(42);
        Fv fv = i_f_v_nx;
        fv();
    }

    {
        Fi fi = i_f_i_ex;
        fi(42);
        Fv fv = i_f_v_ex;
        fv();
    }

    // now with lambdas

    bool ok = false; // prevent lambdas from decaying to function pointers
    {
        auto lambda1 = [&](int i) noexcept { return i + int(ok); };
        Fi fi = lambda1;
        fi(42);
        auto lambda2 = [&]() noexcept { return int(ok); };
        Fv fv = lambda2;
        fv();
    }

    {
        auto lambda1 = [&](int i) { return i + int(ok); };
        Fi fi = lambda1;
        fi(42);
        auto lambda2 = [&]() { return int(ok); };
        Fv fv = lambda2;
        fv();
    }
}

void tst_qxp_function_ref::ctad()
{
#define CHECK(fun, sig) \
    do { \
        qxp::function_ref f = fun; \
        static_assert(std::is_same_v<decltype(f), \
                                    qxp::function_ref<sig>>); \
        qxp::function_ref f2 = &fun; \
        static_assert(std::is_same_v<decltype(f2), \
                                    qxp::function_ref<sig>>); \
    } while (false)

    CHECK(i_f_i_nx, int (int) noexcept);
    CHECK(v_f_i_nx, void(int) noexcept);
    CHECK(i_f_v_nx, int (   ) noexcept);
    CHECK(v_f_v_nx, void(   ) noexcept);

    CHECK(i_f_i_ex, int (int));
    CHECK(v_f_i_ex, void(int));
    CHECK(i_f_v_ex, int (   ));
    CHECK(v_f_v_ex, void(   ));

#undef CHECK

#if 0 // no deduction guides for the non-function-pointer case, so no CTAD for lambdas
    {
        auto lambda = [](int i) -> int { return i; };
        qxp::function_ref f = lambda;
        static_assert(std::is_same_v<decltype(f),
                                     qxp::function_ref<int(int)>>);
    }
#endif
}


QTEST_APPLESS_MAIN(tst_qxp_function_ref);

#include "tst_qxp_function_ref.moc"