1
/****************************************************************************
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
17
** GNU General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU
19
** General Public License version 3 as published by the Free Software
20
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21
** included in the packaging of this file. Please review the following
22
** information to ensure the GNU General Public License requirements will
23
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
27
****************************************************************************/
29
#include <QtTest/QtTest>
32
Q_DECLARE_METATYPE(QMargins)
34
class tst_QMargins : public QObject
39
void dataStreamCheck();
43
void dataStreamCheckF();
47
// Testing get/set functions
48
void tst_QMargins::getSetCheck()
51
// int QMargins::width()
52
// void QMargins::setWidth(int)
54
QCOMPARE(0, margins.left());
55
margins.setTop(INT_MIN);
56
QCOMPARE(INT_MIN, margins.top());
57
margins.setBottom(INT_MAX);
58
QCOMPARE(INT_MAX, margins.bottom());
59
margins.setRight(INT_MAX);
60
QCOMPARE(INT_MAX, margins.right());
63
QVERIFY(margins.isNull());
66
QVERIFY(!margins.isNull());
67
QCOMPARE(margins, QMargins(5, 0, 5, 0));
70
void tst_QMargins::operators()
72
const QMargins m1(12, 14, 16, 18);
73
const QMargins m2(2, 3, 4, 5);
75
const QMargins added = m1 + m2;
76
QCOMPARE(added, QMargins(14, 17, 20, 23));
81
const QMargins subtracted = m1 - m2;
82
QCOMPARE(subtracted, QMargins(10, 11, 12, 13));
85
QCOMPARE(a, subtracted);
89
QCOMPARE(h, QMargins(14, 16, 18, 20));
93
const QMargins doubled = m1 * 2;
94
QCOMPARE(doubled, QMargins(24, 28, 32, 36));
95
QCOMPARE(2 * m1, doubled);
96
QCOMPARE(qreal(2) * m1, doubled);
97
QCOMPARE(m1 * qreal(2), doubled);
101
QCOMPARE(a, doubled);
104
QCOMPARE(a, doubled);
106
const QMargins halved = m1 / 2;
107
QCOMPARE(halved, QMargins(6, 7, 8, 9));
116
QCOMPARE(m1 + (-m1), QMargins());
118
QMargins m3 = QMargins(10, 11, 12, 13);
119
QCOMPARE(m3 + 1, QMargins(11, 12, 13, 14));
120
QCOMPARE(1 + m3, QMargins(11, 12, 13, 14));
121
QCOMPARE(m3 - 1, QMargins(9, 10, 11, 12));
122
QCOMPARE(+m3, QMargins(10, 11, 12, 13));
123
QCOMPARE(-m3, QMargins(-10, -11, -12, -13));
126
// Testing QDataStream operators
127
void tst_QMargins::dataStreamCheck()
133
QMargins marginsOut(0,INT_MIN,INT_MAX,6852);
134
QDataStream streamOut(&buffer, QIODevice::WriteOnly);
135
streamOut << marginsOut;
138
// stream in & compare
141
QDataStream streamIn(&buffer, QIODevice::ReadOnly);
142
streamIn >> marginsIn;
144
QCOMPARE(marginsIn.left(), 0);
145
QCOMPARE(marginsIn.top(), INT_MIN);
146
QCOMPARE(marginsIn.right(), INT_MAX);
147
QCOMPARE(marginsIn.bottom(), 6852);
151
// Testing get/set functions
152
void tst_QMargins::getSetCheckF()
155
// int QMarginsF::width()
156
// void QMarginsF::setWidth(int)
157
margins.setLeft(1.1);
158
QCOMPARE(1.1, margins.left());
160
QCOMPARE(2.2, margins.top());
161
margins.setBottom(3.3);
162
QCOMPARE(3.3, margins.bottom());
163
margins.setRight(4.4);
164
QCOMPARE(4.4, margins.right());
166
margins = QMarginsF();
167
QVERIFY(margins.isNull());
168
margins.setLeft(5.5);
169
margins.setRight(5.5);
170
QVERIFY(!margins.isNull());
171
QCOMPARE(margins, QMarginsF(5.5, 0.0, 5.5, 0.0));
174
void tst_QMargins::operatorsF()
176
const QMarginsF m1(12.1, 14.1, 16.1, 18.1);
177
const QMarginsF m2(2.1, 3.1, 4.1, 5.1);
179
const QMarginsF added = m1 + m2;
180
QCOMPARE(added, QMarginsF(14.2, 17.2, 20.2, 23.2));
185
const QMarginsF subtracted = m1 - m2;
186
QCOMPARE(subtracted, QMarginsF(10.0, 11.0, 12.0, 13.0));
189
QCOMPARE(a, subtracted);
193
QCOMPARE(h, QMarginsF(14.2, 16.2, 18.2, 20.2));
197
const QMarginsF doubled = m1 * 2.0;
198
QCOMPARE(doubled, QMarginsF(24.2, 28.2, 32.2, 36.2));
199
QCOMPARE(2.0 * m1, doubled);
200
QCOMPARE(m1 * 2.0, doubled);
204
QCOMPARE(a, doubled);
206
const QMarginsF halved = m1 / 2.0;
207
QCOMPARE(halved, QMarginsF(6.05, 7.05, 8.05, 9.05));
213
QCOMPARE(m1 + (-m1), QMarginsF());
215
QMarginsF m3 = QMarginsF(10.3, 11.4, 12.5, 13.6);
216
QCOMPARE(m3 + 1.1, QMarginsF(11.4, 12.5, 13.6, 14.7));
217
QCOMPARE(1.1 + m3, QMarginsF(11.4, 12.5, 13.6, 14.7));
218
QCOMPARE(m3 - 1.1, QMarginsF(9.2, 10.3, 11.4, 12.5));
219
QCOMPARE(+m3, QMarginsF(10.3, 11.4, 12.5, 13.6));
220
QCOMPARE(-m3, QMarginsF(-10.3, -11.4, -12.5, -13.6));
223
// Testing QDataStream operators
224
void tst_QMargins::dataStreamCheckF()
230
QMarginsF marginsOut(1.1, 2.2, 3.3, 4.4);
231
QDataStream streamOut(&buffer, QIODevice::WriteOnly);
232
streamOut << marginsOut;
235
// stream in & compare
238
QDataStream streamIn(&buffer, QIODevice::ReadOnly);
239
streamIn >> marginsIn;
241
QCOMPARE(marginsIn.left(), 1.1);
242
QCOMPARE(marginsIn.top(), 2.2);
243
QCOMPARE(marginsIn.right(), 3.3);
244
QCOMPARE(marginsIn.bottom(), 4.4);
248
QTEST_APPLESS_MAIN(tst_QMargins)
249
#include "tst_qmargins.moc"