summaryrefslogtreecommitdiffstats
path: root/src/testlib/qcomparisontesthelper_p.h
blob: b422fc404956f3016107ff53cb70c7732d0c9681 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QCOMPARISONTESTHELPER_P_H
#define QCOMPARISONTESTHELPER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/q20type_traits.h>
#include <QtCore/qxptype_traits.h>
#include <QtTest/qtest.h>

QT_BEGIN_NAMESPACE

namespace QTestPrivate {

#ifdef __cpp_lib_three_way_comparison
template <typename LT, typename RT>
using HasThreeWayComparisonOp = decltype(std::declval<LT>() <=> std::declval<RT>());

template <typename LT, typename RT>
constexpr bool implementsThreeWayComparisonOp_v = qxp::is_detected_v<HasThreeWayComparisonOp,
                                                                     LT, RT>;
#endif

Q_TESTLIB_EXPORT QByteArray formatTypeWithCRefImpl(QMetaType type, bool isConst,
                                                   bool isRef, bool isRvalueRef);

template <typename T>
QByteArray formatTypeWithCRef()
{
    return formatTypeWithCRefImpl(QMetaType::fromType<q20::remove_cvref_t<T>>(),
                                  std::is_const_v<std::remove_reference_t<T>>,
                                  std::is_reference_v<T>,
                                  std::is_rvalue_reference_v<T>);
}

#define FOR_EACH_CREF(Func, Left, Right, Op, Result) \
    Func(Left &, Right &, Op, Result) \
    Func(Left &, Right const &, Op, Result) \
    Func(Left &, Right &&, Op, Result) \
    Func(Left &, Right const &&, Op, Result) \
    Func(Left const &, Right &, Op, Result) \
    Func(Left const &, Right const &, Op, Result) \
    Func(Left const &, Right &&, Op, Result) \
    Func(Left const &, Right const &&, Op, Result) \
    Func(Left &&, Right &, Op, Result) \
    Func(Left &&, Right const &, Op, Result) \
    Func(Left &&, Right &&, Op, Result) \
    Func(Left &&, Right const &&, Op, Result) \
    Func(Left const &&, Right &, Op, Result) \
    Func(Left const &&, Right const &, Op, Result) \
    Func(Left const &&, Right &&, Op, Result) \
    Func(Left const &&, Right const &&, Op, Result) \
    /* END */

#define CHECK_SINGLE_OPERATOR(Left, Right, Op, Result) \
    do { \
        constexpr bool qtest_op_check_isImplNoexcept \
                        = noexcept(std::declval<Left>() Op std::declval<Right>()); \
        if constexpr (!qtest_op_check_isImplNoexcept) { \
            QEXPECT_FAIL("", QByteArray("(" + formatTypeWithCRef<Left>() \
                                        + " " #Op " " + formatTypeWithCRef<Right>() \
                                        + ") is not noexcept").constData(), \
                         Continue); \
            /* Ideally, operators should be noexcept, so warn if they are not. */ \
            /* Do not make it a hard error, because the fix is not always trivial. */ \
            QVERIFY(qtest_op_check_isImplNoexcept); \
        } \
        static_assert(std::is_convertible_v<decltype( \
                        std::declval<Left>() Op std::declval<Right>()), Result>); \
        if constexpr (!std::is_same_v<Left, Right>) { \
            static_assert(std::is_convertible_v<decltype( \
                            std::declval<Right>() Op std::declval<Left>()), Result>); \
        } \
    } while (false); \
    /* END */

/*!
    \internal

    This function checks that the types \c LeftType and \c RightType properly
    define {in}equality operators (== and !=). The checks are performed for
    all combinations of cvref-qualified lvalues and rvalues.
*/
template <typename LeftType, typename RightType = LeftType>
void testEqualityOperatorsCompile()
{
    FOR_EACH_CREF(CHECK_SINGLE_OPERATOR, LeftType, RightType, ==, bool)
    FOR_EACH_CREF(CHECK_SINGLE_OPERATOR, LeftType, RightType, !=, bool)
}

/*!
    \internal

    This function checks that the types \c LeftType and \c RightType properly
    define all comparison operators (==, !=, <, >, <=, >=). The checks are
    performed for all combinations of cvref-qualified lvalues and rvalues.

    If compiled in C++20 mode, also checks \c {operator<=>()} if that is
    implemented.
*/
template <typename LeftType, typename RightType = LeftType>
void testAllComparisonOperatorsCompile()
{
    testEqualityOperatorsCompile<LeftType, RightType>();
    if (QTest::currentTestFailed())
        return;
    FOR_EACH_CREF(CHECK_SINGLE_OPERATOR, LeftType, RightType, >, bool)
    FOR_EACH_CREF(CHECK_SINGLE_OPERATOR, LeftType, RightType, <, bool)
    FOR_EACH_CREF(CHECK_SINGLE_OPERATOR, LeftType, RightType, >=, bool)
    FOR_EACH_CREF(CHECK_SINGLE_OPERATOR, LeftType, RightType, <=, bool)
#ifdef __cpp_lib_three_way_comparison
    if constexpr (implementsThreeWayComparisonOp_v<LeftType, RightType>) {
        FOR_EACH_CREF(CHECK_SINGLE_OPERATOR, LeftType, RightType, <=>, std::partial_ordering)
    }
#endif
}

#undef CHECK_SINGLE_OPERATOR
#undef FOR_EACH_CREF

#define CHECK_RUNTIME_CREF(Func, Left, Right, Op, Expected) \
    do { \
        Func(Left, Right, Op, Expected); \
        Func(std::as_const(Left), Right, Op, Expected); \
        Func(Left, std::as_const(Right), Op, Expected); \
        Func(std::as_const(Left), std::as_const(Right), Op, Expected); \
    } while (false) \
    /* END */

#define CHECK_RUNTIME_LR(Left, Right, Op, Expected) \
    do { \
        QCOMPARE_EQ(Left Op Right, Expected); \
        QCOMPARE_EQ(std::move(Left) Op Right, Expected); \
        QCOMPARE_EQ(Left Op std::move(Right), Expected); \
        QCOMPARE_EQ(std::move(Left) Op std::move(Right), Expected); \
    } while (false) \
    /* END */

#ifdef __cpp_lib_three_way_comparison

// Hide the macro under an ifdef, because it otherwise triggers a warning
// in Clang C++17 build.
#define CHECK_RUNTIME_3WAY(Left, Right, Op, Expected) \
    do { \
        QCOMPARE_EQ((Left <=> Right) Op 0, Expected); \
        QCOMPARE_EQ((std::move(Left) <=> Right) Op 0, Expected); \
        QCOMPARE_EQ((Left <=> std::move(Right)) Op 0, Expected); \
        QCOMPARE_EQ((std::move(Left) <=> std::move(Right)) Op 0, Expected); \
    } while (false) \
    /* END */

#endif // __cpp_lib_three_way_comparison

/*!
    \internal
    Basic testing of equality operators.

    The helper function tests {in}equality operators (== and !=) for the \a lhs
    operand of type \c {LeftType} and the \a rhs operand of type \c {RightType}.

    The \a expectedEqual parameter is an expected result for \c {operator==()}.

    \note Any test calling this method will need to check the test state after
    doing so, if there is any later code in the test.

    \code
    QTime early(12, 34, 56, 00);
    QTime later(12, 34, 56, 01);
    QTestPrivate::testEqualityOperators(early, later, false);
    if (QTest:currentTestFailed())
        return;
    \endcode
*/
template <typename LeftType, typename RightType>
void testEqualityOperators(LeftType lhs, RightType rhs, bool expectedEqual)
{
    CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, ==, expectedEqual);
    CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, !=, !expectedEqual);
    if constexpr (!std::is_same_v<LeftType, RightType>) {
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, ==, expectedEqual);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, !=, !expectedEqual);
    }
}

/*!
    \internal
    Basic testing of equality and relation operators.

    The helper function tests all six relation and equality operators
    (==, !=, <, >, <=, >=) for the \a lhs operand of type \c {LeftType} and
    the \a rhs operand of type \c {RightType}.

    If compiled in C++20 mode, also checks \c {operator<=>()} if that is
    implemented.

    When compiled in C++17 mode, the \c OrderingType must be one of
    Qt::partial_ordering, Qt::strong_ordering, or Qt::weak_ordering.
    In C++20 mode, also the \c {std::*_ordering} types can be used.

    The \a expectedOrdering parameter provides the expected
    relation between \a lhs and \a rhs.

    \note Any test calling this method will need to check the test state after
    doing so, if there is any later code in the test.

    \code
    QDateTime now = QDateTime::currentDateTime();
    QDateTime later = now.addMSec(1);
    QTestPrivate::testComparisonOperators(now, later, Qt::weak_ordering::less);
    if (QTest:currentTestFailed())
        return;
    \endcode
*/
template <typename LeftType, typename RightType, typename OrderingType>
void testAllComparisonOperators(LeftType lhs, RightType rhs, OrderingType expectedOrdering)
{
    constexpr bool isQOrderingType = std::is_same_v<OrderingType, Qt::partial_ordering>
                                        || std::is_same_v<OrderingType, Qt::weak_ordering>
                                        || std::is_same_v<OrderingType, Qt::strong_ordering>;
#ifdef __cpp_lib_three_way_comparison
    constexpr bool isStdOrderingType = std::is_same_v<OrderingType, std::partial_ordering>
                                        || std::is_same_v<OrderingType, std::weak_ordering>
                                        || std::is_same_v<OrderingType, std::strong_ordering>;
#else
    constexpr bool isStdOrderingType = false;
#endif

    static_assert(isQOrderingType || isStdOrderingType,
                  "Please provide, as the expectedOrdering parameter, a value "
                  "of one of the Qt::{partial,weak,strong}_ordering or "
                  "std::{partial,weak,strong}_ordering types.");

    // We have all sorts of operator==() between Q*Ordering and std::*_ordering
    // types, so we can just compare to Qt::partial_ordering.
    const bool expectedEqual = expectedOrdering == Qt::partial_ordering::equivalent;
    const bool expectedLess = expectedOrdering == Qt::partial_ordering::less;
    const bool expectedUnordered = expectedOrdering == Qt::partial_ordering::unordered;

    CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, ==,
                       !expectedUnordered && expectedEqual);
    CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, !=,
                       expectedUnordered || !expectedEqual);
    CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, <,
                       !expectedUnordered && expectedLess);
    CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, >,
                       !expectedUnordered && !expectedLess && !expectedEqual);
    CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, <=,
                       !expectedUnordered && (expectedEqual || expectedLess));
    CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, >=,
                       !expectedUnordered && !expectedLess);
#ifdef __cpp_lib_three_way_comparison
    if constexpr (implementsThreeWayComparisonOp_v<LeftType, RightType>) {
        if constexpr (std::is_convertible_v<OrderingType, std::strong_ordering>)
            static_assert(std::is_same_v<decltype(lhs <=> rhs), std::strong_ordering>);
        else if constexpr (std::is_convertible_v<OrderingType, std::weak_ordering>)
            static_assert(std::is_same_v<decltype(lhs <=> rhs), std::weak_ordering>);
        else
            static_assert(std::is_same_v<decltype(lhs <=> rhs), std::partial_ordering>);

        CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, ==,
                           !expectedUnordered && expectedEqual);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, !=,
                           expectedUnordered || !expectedEqual);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, <,
                           !expectedUnordered && expectedLess);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, >,
                           !expectedUnordered && !expectedLess && !expectedEqual);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, <=,
                           !expectedUnordered && (expectedEqual || expectedLess));
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, lhs, rhs, >=,
                           !expectedUnordered && !expectedLess);
    }
#endif

    if constexpr (!std::is_same_v<LeftType, RightType>) {
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, ==,
                           !expectedUnordered && expectedEqual);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, !=,
                           expectedUnordered || !expectedEqual);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, <,
                           !expectedUnordered && !expectedLess && !expectedEqual);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, >,
                           !expectedUnordered && expectedLess);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, <=,
                           !expectedUnordered && !expectedLess);
        CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, >=,
                           !expectedUnordered && (expectedEqual || expectedLess));
#ifdef __cpp_lib_three_way_comparison
        if constexpr (implementsThreeWayComparisonOp_v<LeftType, RightType>) {
            if constexpr (std::is_convertible_v<OrderingType, std::strong_ordering>)
                static_assert(std::is_same_v<decltype(rhs <=> lhs), std::strong_ordering>);
            else if constexpr (std::is_convertible_v<OrderingType, std::weak_ordering>)
                static_assert(std::is_same_v<decltype(rhs <=> lhs), std::weak_ordering>);
            else
                static_assert(std::is_same_v<decltype(rhs <=> lhs), std::partial_ordering>);

            CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, ==,
                               !expectedUnordered && expectedEqual);
            CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, !=,
                               expectedUnordered || !expectedEqual);
            CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, <,
                               !expectedUnordered && !expectedLess && !expectedEqual);
            CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, >,
                               !expectedUnordered && expectedLess);
            CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, <=,
                               !expectedUnordered && !expectedLess);
            CHECK_RUNTIME_CREF(CHECK_RUNTIME_3WAY, rhs, lhs, >=,
                               !expectedUnordered && (expectedEqual || expectedLess));
        }
#endif
    }
}

#ifdef __cpp_lib_three_way_comparison
#undef CHECK_RUNTIME_3WAY
#endif
#undef CHECK_RUNTIME_LR
#undef CHECK_RUNTIME_CREF

} // namespace QTestPrivate

/*!
    \internal

    A helper macro that calls QTestPrivate::testEqualityOperators(), checks the
    test's state after the function is executed, and generates a meaningful
    debug message with the original file and line numbers if the test has
    failed.
*/
#define QT_TEST_EQUALITY_OPS(Left, Right, Expected) \
    do { \
        auto report = qScopeGuard([] { \
            qDebug("testEqualityOperators(" #Left ", " #Right ", " #Expected ") " \
                   "failed in " __FILE__ " on line %d", __LINE__); \
        }); \
        QTestPrivate::testEqualityOperators(Left, Right, Expected); \
        if (QTest::currentTestFailed()) \
            return; \
        report.dismiss(); \
    } while (false)

/*!
    \internal

    A helper macro that calls QTestPrivate::testAllComparisonOperators(), checks
    the test's state after the function is executed, and generates a meaningful
    debug message with the original file and line numbers if the test has
    failed.
*/
#define QT_TEST_ALL_COMPARISON_OPS(Left, Right, Expected) \
    do { \
        auto report = qScopeGuard([] { \
            qDebug("testAllComparisonOperators(" #Left ", " #Right ", " #Expected ") " \
                   "failed in " __FILE__ " on line %d", __LINE__); \
        }); \
        QTestPrivate::testAllComparisonOperators(Left, Right, Expected); \
        if (QTest::currentTestFailed()) \
            return; \
        report.dismiss(); \
    } while (false)

QT_END_NAMESPACE

#endif // QCOMPARISONTESTHELPER_P_H