summaryrefslogtreecommitdiffstats
path: root/tests/auto/testlib/selftests/extendedcompare/tst_extendedcompare.cpp
blob: d198d621e1d3d7ae59c12e3994d68fc1a3d42415 (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
// 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 <QTest>
#include <QtCore/qtimer.h>

QT_BEGIN_NAMESPACE

#define COMPARE_WITH_TYPE(Type, arg1, arg2) \
switch (Type) { \
    case QTest::ComparisonOperation::CustomCompare:      QCOMPARE(arg1, arg2); break; \
    case QTest::ComparisonOperation::Equal:              QCOMPARE_EQ(arg1, arg2); break; \
    case QTest::ComparisonOperation::NotEqual:           QCOMPARE_NE(arg1, arg2); break; \
    case QTest::ComparisonOperation::LessThan:           QCOMPARE_LT(arg1, arg2); break; \
    case QTest::ComparisonOperation::LessThanOrEqual:    QCOMPARE_LE(arg1, arg2); break; \
    case QTest::ComparisonOperation::GreaterThan:        QCOMPARE_GT(arg1, arg2); break; \
    case QTest::ComparisonOperation::GreaterThanOrEqual: QCOMPARE_GE(arg1, arg2); break; \
}

class MyClass
{
public:
    MyClass(int v) : m_value(v) {}
    int value() const { return m_value; }
    void setValue(int v) { m_value = v; }

    friend bool operator==(const MyClass &lhs, const MyClass &rhs)
    {
        return lhs.m_value == rhs.m_value;
    }
    friend bool operator!=(const MyClass &lhs, const MyClass &rhs)
    {
        return lhs.m_value != rhs.m_value;
    }
    friend bool operator<(const MyClass &lhs, const MyClass &rhs)
    {
        return lhs.m_value < rhs.m_value;
    }
    friend bool operator<=(const MyClass &lhs, const MyClass &rhs)
    {
        return lhs.m_value <= rhs.m_value;
    }
    friend bool operator>(const MyClass &lhs, const MyClass &rhs)
    {
        return lhs.m_value > rhs.m_value;
    }
    friend bool operator>=(const MyClass &lhs, const MyClass &rhs)
    {
        return lhs.m_value >= rhs.m_value;
    }

private:
    int m_value;
};

// ClassWithPointerGetter returns a pointer, so pointers will be used during
// comparison. To get consistent results, we need to make sure that the pointer
// returned by first object is always smaller than the one returned by the
// second object.
// We will make sure that the objects are not destroyed until the comparison
// is finished by checking that the output does not contain 'MyClass(-1)'.
static MyClass valuesForClassWithPointerGetter[] = { MyClass(-1), MyClass(-1) };

class ClassWithPointerGetter
{
    Q_DISABLE_COPY_MOVE(ClassWithPointerGetter)
public:
    explicit ClassWithPointerGetter(int index) : m_index(index)
    {
        Q_ASSERT(m_index >= 0 && m_index < 2);
        valuesForClassWithPointerGetter[m_index].setValue(indexToValue(m_index));
    }
    ~ClassWithPointerGetter()
    {
        valuesForClassWithPointerGetter[m_index].setValue(-1);
    }

    const MyClass *getValuePointer() const { return &valuesForClassWithPointerGetter[m_index]; }

    static int indexToValue(int index) { return 2 - index; }
    static int valueToIndex(int value) { return 2 - value; }

private:
    int m_index;
};

// An auxiliary function to get a temporary object
static ClassWithPointerGetter getClassForValue(int val)
{
    return ClassWithPointerGetter(val);
}

// various toString() overloads
namespace QTest {

char *toString(const int *val)
{
    return val ? toString(*val) : toString(nullptr);
}

char *toString(const MyClass &val)
{
    char *msg = new char[128];
    qsnprintf(msg, 128, "MyClass(%d)", val.value());
    return msg;
}

char *toString(const MyClass *val)
{
    if (val) {
        char *msg = new char[128];
        const auto value = val->value();
        qsnprintf(msg, 128, "MyClass(%d) on memory address with index %d", value,
                  ClassWithPointerGetter::valueToIndex(value));
        return msg;
    }
    return toString(nullptr);
}

} // namespace QTest

enum MyUnregisteredEnum { MyUnregisteredEnumValue1, MyUnregisteredEnumValue2 };

class tst_ExtendedCompare : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase_data();
    void compareInts_data();
    void compareInts();
    void compareFloats_data();
    void compareFloats();
    void compareDoubles_data();
    void compareDoubles();
    void comparePointers_data();
    void comparePointers();
    void compareToNullptr_data();
    void compareToNullptr();
    void compareUnregistereEnum_data();
    void compareUnregistereEnum();
    void compareRegistereEnum_data();
    void compareRegistereEnum();
    void compareCustomTypes_data();
    void compareCustomTypes();
    void checkComparisonForTemporaryObjects();
    void checkComparisonWithTimeout();
};

void tst_ExtendedCompare::initTestCase_data()
{
    qRegisterMetaType<QTest::ComparisonOperation>();
    QTest::addColumn<QTest::ComparisonOperation>("operation");
    // Do not test plain old QCOMPARE() intentionally, as it's tested in other
    // places.
    QTest::newRow("EQ") << QTest::ComparisonOperation::Equal;
    QTest::newRow("NE") << QTest::ComparisonOperation::NotEqual;
    QTest::newRow("LT") << QTest::ComparisonOperation::LessThan;
    QTest::newRow("LE") << QTest::ComparisonOperation::LessThanOrEqual;
    QTest::newRow("GT") << QTest::ComparisonOperation::GreaterThan;
    QTest::newRow("GE") << QTest::ComparisonOperation::GreaterThanOrEqual;
}

#define GENERATE_DATA_FOR_TYPE(Type, val1, val2) \
do { \
    Q_ASSERT(val1 < val2); \
    QTest::addColumn<Type>("lhs"); \
    QTest::addColumn<Type>("rhs"); \
    QTest::newRow("left == right") << val1 << val1; \
    QTest::newRow("left < right") << val1 << val2; \
    QTest::newRow("left > right") << val2 << val1; \
} while (false)

#define EXECUTE_COMPARISON_FOR_TYPE(Type) \
do { \
    QFETCH_GLOBAL(QTest::ComparisonOperation, operation); \
    QFETCH(Type, lhs); \
    QFETCH(Type, rhs); \
    COMPARE_WITH_TYPE(operation, lhs, rhs); \
} while (false)

void tst_ExtendedCompare::compareInts_data()
{
    GENERATE_DATA_FOR_TYPE(int, 1, 2);
}

void tst_ExtendedCompare::compareInts()
{
    EXECUTE_COMPARISON_FOR_TYPE(int);
}

void tst_ExtendedCompare::compareFloats_data()
{
    GENERATE_DATA_FOR_TYPE(float, 1.0f, 1.1f);
}

void tst_ExtendedCompare::compareFloats()
{
    EXECUTE_COMPARISON_FOR_TYPE(float);
}

void tst_ExtendedCompare::compareDoubles_data()
{
    GENERATE_DATA_FOR_TYPE(double, 0.0, 0.1);
}

void tst_ExtendedCompare::compareDoubles()
{
    EXECUTE_COMPARISON_FOR_TYPE(double);
}

void tst_ExtendedCompare::comparePointers_data()
{
    static constexpr int values[] = { 1, 2 };
    GENERATE_DATA_FOR_TYPE(const int *, &values[0], &values[1]);
}

void tst_ExtendedCompare::comparePointers()
{
    EXECUTE_COMPARISON_FOR_TYPE(const int *);
}

void tst_ExtendedCompare::compareToNullptr_data()
{
    static const int *ptr = nullptr;
    static const int value = 1;
    GENERATE_DATA_FOR_TYPE(const int *, ptr, &value);
}

void tst_ExtendedCompare::compareToNullptr()
{
    EXECUTE_COMPARISON_FOR_TYPE(const int *);
}

void tst_ExtendedCompare::compareUnregistereEnum_data()
{
    GENERATE_DATA_FOR_TYPE(MyUnregisteredEnum, MyUnregisteredEnumValue1, MyUnregisteredEnumValue2);
}

void tst_ExtendedCompare::compareUnregistereEnum()
{
    EXECUTE_COMPARISON_FOR_TYPE(MyUnregisteredEnum);
}

void tst_ExtendedCompare::compareRegistereEnum_data()
{
    GENERATE_DATA_FOR_TYPE(Qt::DayOfWeek, Qt::Monday, Qt::Sunday);
}

void tst_ExtendedCompare::compareRegistereEnum()
{
    EXECUTE_COMPARISON_FOR_TYPE(Qt::DayOfWeek);
}

void tst_ExtendedCompare::compareCustomTypes_data()
{
    static const MyClass val1(1);
    static const MyClass val2(2);
    GENERATE_DATA_FOR_TYPE(MyClass, val1, val2);
}

void tst_ExtendedCompare::compareCustomTypes()
{
    EXECUTE_COMPARISON_FOR_TYPE(MyClass);
}

void tst_ExtendedCompare::checkComparisonForTemporaryObjects()
{
    // This test checks that temporary objects live until the end of
    // comparison.

    QFETCH_GLOBAL(QTest::ComparisonOperation, operation);
    COMPARE_WITH_TYPE(operation, getClassForValue(0).getValuePointer(),
                      getClassForValue(1).getValuePointer());
}

class ClassWithDeferredSetter : public MyClass
{
public:
    ClassWithDeferredSetter(int value) : MyClass(value) {}

    void setValueDeferred(int value)
    {
        QTimer::singleShot(100, [this, value] { setValue(value); });
    }
};

namespace QTest {

char *toString(const ClassWithDeferredSetter &val)
{
    char *msg = new char[128];
    qsnprintf(msg, 128, "ClassWithDeferredSetter(%d)", val.value());
    return msg;
}

} // namespace QTest

void tst_ExtendedCompare::checkComparisonWithTimeout()
{
    QFETCH_GLOBAL(QTest::ComparisonOperation, operation);
    ClassWithDeferredSetter c(0);
    c.setValueDeferred(1);
    switch (operation) {
    case QTest::ComparisonOperation::Equal:
        QTRY_COMPARE_EQ_WITH_TIMEOUT(c, ClassWithDeferredSetter(1), 300);
        break;
    case QTest::ComparisonOperation::NotEqual:
        QTRY_COMPARE_NE_WITH_TIMEOUT(c, ClassWithDeferredSetter(0), 300);
        break;
    case QTest::ComparisonOperation::LessThan:
        QTRY_COMPARE_LT_WITH_TIMEOUT(c, ClassWithDeferredSetter(0), 300);
        break;
    case QTest::ComparisonOperation::LessThanOrEqual:
        QTRY_COMPARE_LE_WITH_TIMEOUT(c, ClassWithDeferredSetter(-1), 300);
        break;
    case QTest::ComparisonOperation::GreaterThan:
        QTRY_COMPARE_GT_WITH_TIMEOUT(c, ClassWithDeferredSetter(1), 300);
        break;
    case QTest::ComparisonOperation::GreaterThanOrEqual:
        QTRY_COMPARE_GE_WITH_TIMEOUT(c, ClassWithDeferredSetter(1), 300);
        break;
    case QTest::ComparisonOperation::CustomCompare:
        QFAIL("Unexpected comparison operation");
        break;
    }
}

QT_END_NAMESPACE

QTEST_MAIN(tst_ExtendedCompare)
#include "tst_extendedcompare.moc"