1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
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. |
16 | ** |
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. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtTest/QtTest> |
30 | #include <qsize.h> |
31 | |
32 | Q_DECLARE_METATYPE(QMargins) |
33 | |
34 | class tst_QSize : public QObject |
35 | { |
36 | Q_OBJECT |
37 | private slots: |
38 | void getSetCheck(); |
39 | void scale(); |
40 | |
41 | void expandedTo(); |
42 | void expandedTo_data(); |
43 | |
44 | void boundedTo_data(); |
45 | void boundedTo(); |
46 | |
47 | void grownOrShrunkBy_data(); |
48 | void grownOrShrunkBy(); |
49 | |
50 | void transpose_data(); |
51 | void transpose(); |
52 | }; |
53 | |
54 | // Testing get/set functions |
55 | void tst_QSize::getSetCheck() |
56 | { |
57 | QSize obj1; |
58 | // int QSize::width() |
59 | // void QSize::setWidth(int) |
60 | obj1.setWidth(0); |
61 | QCOMPARE(0, obj1.width()); |
62 | obj1.setWidth(INT_MIN); |
63 | QCOMPARE(INT_MIN, obj1.width()); |
64 | obj1.setWidth(INT_MAX); |
65 | QCOMPARE(INT_MAX, obj1.width()); |
66 | |
67 | // int QSize::height() |
68 | // void QSize::setHeight(int) |
69 | obj1.setHeight(0); |
70 | QCOMPARE(0, obj1.height()); |
71 | obj1.setHeight(INT_MIN); |
72 | QCOMPARE(INT_MIN, obj1.height()); |
73 | obj1.setHeight(INT_MAX); |
74 | QCOMPARE(INT_MAX, obj1.height()); |
75 | |
76 | QSizeF obj2; |
77 | // qreal QSizeF::width() |
78 | // void QSizeF::setWidth(qreal) |
79 | obj2.setWidth(0.0); |
80 | QCOMPARE(0.0, obj2.width()); |
81 | obj2.setWidth(1.1); |
82 | QCOMPARE(1.1, obj2.width()); |
83 | |
84 | // qreal QSizeF::height() |
85 | // void QSizeF::setHeight(qreal) |
86 | obj2.setHeight(0.0); |
87 | QCOMPARE(0.0, obj2.height()); |
88 | obj2.setHeight(1.1); |
89 | QCOMPARE(1.1, obj2.height()); |
90 | } |
91 | |
92 | void tst_QSize::scale() |
93 | { |
94 | QSize t1( 10, 12 ); |
95 | t1.scale( w: 60, h: 60, mode: Qt::IgnoreAspectRatio ); |
96 | QCOMPARE( t1, QSize(60, 60) ); |
97 | |
98 | QSize t2( 10, 12 ); |
99 | t2.scale( w: 60, h: 60, mode: Qt::KeepAspectRatio ); |
100 | QCOMPARE( t2, QSize(50, 60) ); |
101 | |
102 | QSize t3( 10, 12 ); |
103 | t3.scale( w: 60, h: 60, mode: Qt::KeepAspectRatioByExpanding ); |
104 | QCOMPARE( t3, QSize(60, 72) ); |
105 | |
106 | QSize t4( 12, 10 ); |
107 | t4.scale( w: 60, h: 60, mode: Qt::KeepAspectRatio ); |
108 | QCOMPARE( t4, QSize(60, 50) ); |
109 | |
110 | QSize t5( 12, 10 ); |
111 | t5.scale( w: 60, h: 60, mode: Qt::KeepAspectRatioByExpanding ); |
112 | QCOMPARE( t5, QSize(72, 60) ); |
113 | |
114 | // test potential int overflow |
115 | QSize t6(88473, 88473); |
116 | t6.scale(w: 141817, h: 141817, mode: Qt::KeepAspectRatio); |
117 | QCOMPARE(t6, QSize(141817, 141817)); |
118 | |
119 | QSize t7(800, 600); |
120 | t7.scale(w: 400, INT_MAX, mode: Qt::KeepAspectRatio); |
121 | QCOMPARE(t7, QSize(400, 300)); |
122 | |
123 | QSize t8(800, 600); |
124 | t8.scale(INT_MAX, h: 150, mode: Qt::KeepAspectRatio); |
125 | QCOMPARE(t8, QSize(200, 150)); |
126 | |
127 | QSize t9(600, 800); |
128 | t9.scale(w: 300, INT_MAX, mode: Qt::KeepAspectRatio); |
129 | QCOMPARE(t9, QSize(300, 400)); |
130 | |
131 | QSize t10(600, 800); |
132 | t10.scale(INT_MAX, h: 200, mode: Qt::KeepAspectRatio); |
133 | QCOMPARE(t10, QSize(150, 200)); |
134 | |
135 | QSize t11(0, 0); |
136 | t11.scale(w: 240, h: 200, mode: Qt::IgnoreAspectRatio); |
137 | QCOMPARE(t11, QSize(240, 200)); |
138 | |
139 | QSize t12(0, 0); |
140 | t12.scale(w: 240, h: 200, mode: Qt::KeepAspectRatio); |
141 | QCOMPARE(t12, QSize(240, 200)); |
142 | |
143 | QSize t13(0, 0); |
144 | t13.scale(w: 240, h: 200, mode: Qt::KeepAspectRatioByExpanding); |
145 | QCOMPARE(t13, QSize(240, 200)); |
146 | } |
147 | |
148 | |
149 | void tst_QSize::expandedTo_data() |
150 | { |
151 | QTest::addColumn<QSize>(name: "input1" ); |
152 | QTest::addColumn<QSize>(name: "input2" ); |
153 | QTest::addColumn<QSize>(name: "expected" ); |
154 | |
155 | QTest::newRow(dataTag: "data0" ) << QSize(10,12) << QSize(6,4) << QSize(10,12); |
156 | QTest::newRow(dataTag: "data1" ) << QSize(0,0) << QSize(6,4) << QSize(6,4); |
157 | // This should pick the highest of w,h components independently of each other, |
158 | // thus the results don't have to be equal to neither input1 nor input2. |
159 | QTest::newRow(dataTag: "data3" ) << QSize(6,4) << QSize(4,6) << QSize(6,6); |
160 | } |
161 | |
162 | void tst_QSize::expandedTo() |
163 | { |
164 | QFETCH( QSize, input1); |
165 | QFETCH( QSize, input2); |
166 | QFETCH( QSize, expected); |
167 | |
168 | QCOMPARE( input1.expandedTo(input2), expected); |
169 | } |
170 | |
171 | void tst_QSize::boundedTo_data() |
172 | { |
173 | QTest::addColumn<QSize>(name: "input1" ); |
174 | QTest::addColumn<QSize>(name: "input2" ); |
175 | QTest::addColumn<QSize>(name: "expected" ); |
176 | |
177 | QTest::newRow(dataTag: "data0" ) << QSize(10,12) << QSize(6,4) << QSize(6,4); |
178 | QTest::newRow(dataTag: "data1" ) << QSize(0,0) << QSize(6,4) << QSize(0,0); |
179 | // This should pick the lowest of w,h components independently of each other, |
180 | // thus the results don't have to be equal to neither input1 nor input2. |
181 | QTest::newRow(dataTag: "data3" ) << QSize(6,4) << QSize(4,6) << QSize(4,4); |
182 | } |
183 | |
184 | void tst_QSize::boundedTo() |
185 | { |
186 | QFETCH( QSize, input1); |
187 | QFETCH( QSize, input2); |
188 | QFETCH( QSize, expected); |
189 | |
190 | QCOMPARE( input1.boundedTo(input2), expected); |
191 | } |
192 | |
193 | void tst_QSize::grownOrShrunkBy_data() |
194 | { |
195 | QTest::addColumn<QSize>(name: "input" ); |
196 | QTest::addColumn<QMargins>(name: "margins" ); |
197 | QTest::addColumn<QSize>(name: "grown" ); |
198 | QTest::addColumn<QSize>(name: "shrunk" ); |
199 | |
200 | auto row = [](QSize i, QMargins m, QSize g, QSize s) { |
201 | QTest::addRow(format: "{%d,%d}/{%d,%d,%d,%d}" , i.width(), i.height(), |
202 | m.left(), m.top(), m.right(), m.bottom()) |
203 | << i << m << g << s; |
204 | }; |
205 | |
206 | const QSize zero = {0, 0}; |
207 | const QSize some = {100, 200}; |
208 | const QMargins zeroMargins = {}; |
209 | const QMargins negative = {-1, -2, -3, -4}; |
210 | const QMargins positive = { 1, 2, 3, 4}; |
211 | |
212 | row(zero, zeroMargins, zero, zero); |
213 | row(zero, negative, {-4, -6}, { 4, 6}); |
214 | row(zero, positive, { 4, 6}, {-4, -6}); |
215 | row(some, zeroMargins, some, some); |
216 | row(some, negative, { 96, 194}, {104, 206}); |
217 | row(some, positive, {104, 206}, { 96, 194}); |
218 | } |
219 | |
220 | void tst_QSize::grownOrShrunkBy() |
221 | { |
222 | QFETCH(const QSize, input); |
223 | QFETCH(const QMargins, margins); |
224 | QFETCH(const QSize, grown); |
225 | QFETCH(const QSize, shrunk); |
226 | |
227 | QCOMPARE(input.grownBy(margins), grown); |
228 | QCOMPARE(input.shrunkBy(margins), shrunk); |
229 | QCOMPARE(grown.shrunkBy(margins), input); |
230 | QCOMPARE(shrunk.grownBy(margins), input); |
231 | } |
232 | |
233 | void tst_QSize::transpose_data() |
234 | { |
235 | QTest::addColumn<QSize>(name: "input1" ); |
236 | QTest::addColumn<QSize>(name: "expected" ); |
237 | |
238 | QTest::newRow(dataTag: "data0" ) << QSize(10,12) << QSize(12,10); |
239 | QTest::newRow(dataTag: "data1" ) << QSize(0,0) << QSize(0,0); |
240 | QTest::newRow(dataTag: "data3" ) << QSize(6,4) << QSize(4,6); |
241 | } |
242 | |
243 | void tst_QSize::transpose() |
244 | { |
245 | QFETCH( QSize, input1); |
246 | QFETCH( QSize, expected); |
247 | |
248 | // transpose() works only inplace and does not return anything, so we must do the operation itself before the compare. |
249 | input1.transpose(); |
250 | QCOMPARE(input1 , expected); |
251 | } |
252 | |
253 | QTEST_APPLESS_MAIN(tst_QSize) |
254 | #include "tst_qsize.moc" |
255 | |