summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp
blob: c99b3d6e336c2a7c149af7bdb8a7fea84a08fb4f (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
// 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 <qmargins.h>

#include <array>

Q_DECLARE_METATYPE(QMargins)

class tst_QMargins : public QObject
{
    Q_OBJECT
private slots:
    void getSetCheck();
#ifndef QT_NO_DATASTREAM
    void dataStreamCheck();
#endif
    void operators();
#ifndef QT_NO_DEBUG_STREAM
    void debugStreamCheck();
#endif

    void getSetCheckF();
#ifndef QT_NO_DATASTREAM
    void dataStreamCheckF();
#endif
    void operatorsF();
#ifndef QT_NO_DEBUG_STREAM
    void debugStreamCheckF();
#endif

    void structuredBinding();

    void toMarginsF_data();
    void toMarginsF();
};

// Testing get/set functions
void tst_QMargins::getSetCheck()
{
    QMargins margins;
    // int QMargins::width()
    // void QMargins::setWidth(int)
    margins.setLeft(0);
    QCOMPARE(0, margins.left());
    margins.setTop(INT_MIN);
    QCOMPARE(INT_MIN, margins.top());
    margins.setBottom(INT_MAX);
    QCOMPARE(INT_MAX, margins.bottom());
    margins.setRight(INT_MAX);
    QCOMPARE(INT_MAX, margins.right());

    margins = QMargins();
    QVERIFY(margins.isNull());
    margins.setLeft(5);
    margins.setRight(5);
    QVERIFY(!margins.isNull());
    QCOMPARE(margins, QMargins(5, 0, 5, 0));
}

void tst_QMargins::operators()
{
    const QMargins m1(12, 14, 16, 18);
    const QMargins m2(2, 3, 4, 5);

    const QMargins added = m1 + m2;
    QCOMPARE(added, QMargins(14, 17, 20, 23));
    QMargins a = m1;
    a += m2;
    QCOMPARE(a, added);

    const QMargins subtracted = m1 - m2;
    QCOMPARE(subtracted, QMargins(10, 11, 12, 13));
    a = m1;
    a -= m2;
    QCOMPARE(a, subtracted);

    QMargins h = m1;
    h += 2;
    QCOMPARE(h, QMargins(14, 16, 18, 20));
    h -= 2;
    QCOMPARE(h, m1);

    const QMargins doubled = m1 * 2;
    QCOMPARE(doubled, QMargins(24, 28, 32, 36));
    QCOMPARE(2 * m1, doubled);
    QCOMPARE(qreal(2) * m1, doubled);
    QCOMPARE(m1 * qreal(2), doubled);

    a = m1;
    a *= 2;
    QCOMPARE(a, doubled);
    a = m1;
    a *= qreal(2);
    QCOMPARE(a, doubled);

    const QMargins halved = m1 / 2;
    QCOMPARE(halved, QMargins(6, 7, 8, 9));

    a = m1;
    a /= 2;
    QCOMPARE(a, halved);
    a = m1;
    a /= qreal(2);
    QCOMPARE(a, halved);

    QCOMPARE(m1 + (-m1), QMargins());

    QMargins m3 = QMargins(10, 11, 12, 13);
    QCOMPARE(m3 + 1, QMargins(11, 12, 13, 14));
    QCOMPARE(1 + m3, QMargins(11, 12, 13, 14));
    QCOMPARE(m3 - 1, QMargins(9, 10, 11, 12));
    QCOMPARE(+m3, QMargins(10, 11, 12, 13));
    QCOMPARE(-m3, QMargins(-10, -11, -12, -13));
}

#ifndef QT_NO_DEBUG_STREAM
// Testing QDebug operators
void tst_QMargins::debugStreamCheck()
{
    QMargins m(10, 11, 12, 13);
    const QString expected = "QMargins(10, 11, 12, 13)";
    QString result;
    QDebug(&result).nospace() << m;
    QCOMPARE(result, expected);
}
#endif

#ifndef QT_NO_DATASTREAM
// Testing QDataStream operators
void tst_QMargins::dataStreamCheck()
{
    QByteArray buffer;

    // stream out
    {
        QMargins marginsOut(0,INT_MIN,INT_MAX,6852);
        QDataStream streamOut(&buffer, QIODevice::WriteOnly);
        streamOut << marginsOut;
    }

    // stream in & compare
    {
        QMargins marginsIn;
        QDataStream streamIn(&buffer, QIODevice::ReadOnly);
        streamIn >> marginsIn;

        QCOMPARE(marginsIn.left(), 0);
        QCOMPARE(marginsIn.top(), INT_MIN);
        QCOMPARE(marginsIn.right(), INT_MAX);
        QCOMPARE(marginsIn.bottom(), 6852);
    }
}
#endif

// Testing get/set functions
void tst_QMargins::getSetCheckF()
{
    QMarginsF margins;
    // int QMarginsF::width()
    // void QMarginsF::setWidth(int)
    margins.setLeft(1.1);
    QCOMPARE(1.1, margins.left());
    margins.setTop(2.2);
    QCOMPARE(2.2, margins.top());
    margins.setBottom(3.3);
    QCOMPARE(3.3, margins.bottom());
    margins.setRight(4.4);
    QCOMPARE(4.4, margins.right());

    margins = QMarginsF();
    QVERIFY(margins.isNull());
    margins.setLeft(5.5);
    margins.setRight(5.5);
    QVERIFY(!margins.isNull());
    QCOMPARE(margins, QMarginsF(5.5, 0.0, 5.5, 0.0));
}

void tst_QMargins::operatorsF()
{
    const QMarginsF m1(12.1, 14.1, 16.1, 18.1);
    const QMarginsF m2(2.1, 3.1, 4.1, 5.1);

    const QMarginsF added = m1 + m2;
    QCOMPARE(added, QMarginsF(14.2, 17.2, 20.2, 23.2));
    QMarginsF a = m1;
    a += m2;
    QCOMPARE(a, added);

    const QMarginsF subtracted = m1 - m2;
    QCOMPARE(subtracted, QMarginsF(10.0, 11.0, 12.0, 13.0));
    a = m1;
    a -= m2;
    QCOMPARE(a, subtracted);

    QMarginsF h = m1;
    h += 2.1;
    QCOMPARE(h, QMarginsF(14.2, 16.2, 18.2, 20.2));
    h -= 2.1;
    QCOMPARE(h, m1);

    const QMarginsF doubled = m1 * 2.0;
    QCOMPARE(doubled, QMarginsF(24.2, 28.2, 32.2, 36.2));
    QCOMPARE(2.0 * m1, doubled);
    QCOMPARE(m1 * 2.0, doubled);

    a = m1;
    a *= 2.0;
    QCOMPARE(a, doubled);

    const QMarginsF halved = m1 / 2.0;
    QCOMPARE(halved, QMarginsF(6.05, 7.05, 8.05, 9.05));

    a = m1;
    a /= 2.0;
    QCOMPARE(a, halved);

    QCOMPARE(m1 + (-m1), QMarginsF());

    QMarginsF m3 = QMarginsF(10.3, 11.4, 12.5, 13.6);
    QCOMPARE(m3 + 1.1, QMarginsF(11.4, 12.5, 13.6, 14.7));
    QCOMPARE(1.1 + m3, QMarginsF(11.4, 12.5, 13.6, 14.7));
    QCOMPARE(m3 - 1.1, QMarginsF(9.2, 10.3, 11.4, 12.5));
    QCOMPARE(+m3, QMarginsF(10.3, 11.4, 12.5, 13.6));
    QCOMPARE(-m3, QMarginsF(-10.3, -11.4, -12.5, -13.6));
}

#ifndef QT_NO_DATASTREAM
// Testing QDataStream operators
void tst_QMargins::dataStreamCheckF()
{
    QByteArray buffer;

    // stream out
    {
        QMarginsF marginsOut(1.1, 2.2, 3.3, 4.4);
        QDataStream streamOut(&buffer, QIODevice::WriteOnly);
        streamOut << marginsOut;
    }

    // stream in & compare
    {
        QMarginsF marginsIn;
        QDataStream streamIn(&buffer, QIODevice::ReadOnly);
        streamIn >> marginsIn;

        QCOMPARE(marginsIn.left(), 1.1);
        QCOMPARE(marginsIn.top(), 2.2);
        QCOMPARE(marginsIn.right(), 3.3);
        QCOMPARE(marginsIn.bottom(), 4.4);
    }
}
#endif

#ifndef QT_NO_DEBUG_STREAM
// Testing QDebug operators
void tst_QMargins::debugStreamCheckF()
{
    QMarginsF m(10.1, 11.2, 12.3, 13.4);
    const QString expected = "QMarginsF(10.1, 11.2, 12.3, 13.4)";
    QString result;
    QDebug(&result).nospace() << m;
    QCOMPARE(result, expected);
}
#endif

void tst_QMargins::structuredBinding()
{
    {
        QMargins m(1, 2, 3, 4);
        auto [left, top, right, bottom] = m;
        QCOMPARE(left, 1);
        QCOMPARE(top, 2);
        QCOMPARE(right, 3);
        QCOMPARE(bottom, 4);
    }
    {
        QMargins m(1, 2, 3, 4);
        auto &[left, top, right, bottom] = m;
        QCOMPARE(left, 1);
        QCOMPARE(top, 2);
        QCOMPARE(right, 3);
        QCOMPARE(bottom, 4);

        left = 10;
        top = 20;
        right = 30;
        bottom = 40;
        QCOMPARE(m.left(), 10);
        QCOMPARE(m.top(), 20);
        QCOMPARE(m.right(), 30);
        QCOMPARE(m.bottom(), 40);
    }
    {
        QMarginsF m(1.0, 2.0, 3.0, 4.0);
        auto [left, top, right, bottom] = m;
        QCOMPARE(left, 1.0);
        QCOMPARE(top, 2.0);
        QCOMPARE(right, 3.0);
        QCOMPARE(bottom, 4.0);
    }
    {
        QMarginsF m(1.0, 2.0, 3.0, 4.0);
        auto &[left, top, right, bottom] = m;
        QCOMPARE(left, 1.0);
        QCOMPARE(top, 2.0);
        QCOMPARE(right, 3.0);
        QCOMPARE(bottom, 4.0);

        left = 10.0;
        top = 20.0;
        right = 30.0;
        bottom = 40.0;
        QCOMPARE(m.left(), 10.0);
        QCOMPARE(m.top(), 20.0);
        QCOMPARE(m.right(), 30.0);
        QCOMPARE(m.bottom(), 40.0);
    }
}

void tst_QMargins::toMarginsF_data()
{
    QTest::addColumn<QMargins>("input");
    QTest::addColumn<QMarginsF>("result");

    auto row = [](int x1, int y1, int x2, int y2) {
        QTest::addRow("(%d, %d, %d, %d)", x1, y1, x2, y2)
                << QMargins(x1, y1, x2, y2) << QMarginsF(x1, y1, x2, y2);
    };
    constexpr std::array samples = {-1, 0, 1};
    for (int x1 : samples) {
        for (int y1 : samples) {
            for (int x2 : samples) {
                for (int y2 : samples) {
                    row(x1, y1, x2, y2);
                }
            }
        }
    }
}

void tst_QMargins::toMarginsF()
{
    QFETCH(const QMargins, input);
    QFETCH(const QMarginsF, result);

    QCOMPARE(input.toMarginsF(), result);
}

QTEST_APPLESS_MAIN(tst_QMargins)
#include "tst_qmargins.moc"