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
31#include <qpoint.h>
32
33class tst_QPoint : public QObject
34{
35 Q_OBJECT
36private slots:
37 void isNull();
38
39 void manhattanLength_data();
40 void manhattanLength();
41
42 void getSet_data();
43 void getSet();
44
45 void transposed();
46
47 void rx();
48 void ry();
49
50 void operator_add_data();
51 void operator_add();
52
53 void operator_subtract_data();
54 void operator_subtract();
55
56 void operator_multiply_data();
57 void operator_multiply();
58
59 void operator_divide_data();
60 void operator_divide();
61
62 void dotProduct_data();
63 void dotProduct();
64
65 void operator_unary_plus_data();
66 void operator_unary_plus();
67
68 void operator_unary_minus_data();
69 void operator_unary_minus();
70
71 void operator_eq_data();
72 void operator_eq();
73
74#ifndef QT_NO_DATASTREAM
75 void stream_data();
76 void stream();
77#endif
78};
79
80void tst_QPoint::isNull()
81{
82 QPoint point(0, 0);
83 QVERIFY(point.isNull());
84 ++point.rx();
85 QVERIFY(!point.isNull());
86 point.rx() -= 2;
87 QVERIFY(!point.isNull());
88}
89
90void tst_QPoint::manhattanLength_data()
91{
92 QTest::addColumn<QPoint>(name: "point");
93 QTest::addColumn<int>(name: "expected");
94
95 QTest::newRow(dataTag: "(0, 0)") << QPoint(0, 0) << 0;
96 QTest::newRow(dataTag: "(10, 0)") << QPoint(10, 0) << 10;
97 QTest::newRow(dataTag: "(0, 10)") << QPoint(0, 10) << 10;
98 QTest::newRow(dataTag: "(10, 20)") << QPoint(10, 20) << 30;
99 QTest::newRow(dataTag: "(-10, -20)") << QPoint(-10, -20) << 30;
100}
101
102void tst_QPoint::manhattanLength()
103{
104 QFETCH(QPoint, point);
105 QFETCH(int, expected);
106
107 QCOMPARE(point.manhattanLength(), expected);
108}
109
110void tst_QPoint::getSet_data()
111{
112 QTest::addColumn<int>(name: "i");
113
114 QTest::newRow(dataTag: "0") << 0;
115 QTest::newRow(dataTag: "INT_MIN") << INT_MIN;
116 QTest::newRow(dataTag: "INT_MAX") << INT_MAX;
117}
118
119void tst_QPoint::getSet()
120{
121 QFETCH(int, i);
122
123 QPoint point;
124 point.setX(i);
125 QCOMPARE(point.x(), i);
126
127 point.setY(i);
128 QCOMPARE(point.y(), i);
129}
130
131void tst_QPoint::transposed()
132{
133 QCOMPARE(QPoint(1, 2).transposed(), QPoint(2, 1));
134}
135
136void tst_QPoint::rx()
137{
138 const QPoint originalPoint(-1, 0);
139 QPoint point(originalPoint);
140 ++point.rx();
141 QCOMPARE(point.x(), originalPoint.x() + 1);
142}
143
144void tst_QPoint::ry()
145{
146 const QPoint originalPoint(0, -1);
147 QPoint point(originalPoint);
148 ++point.ry();
149 QCOMPARE(point.y(), originalPoint.y() + 1);
150}
151
152void tst_QPoint::operator_add_data()
153{
154 QTest::addColumn<QPoint>(name: "point1");
155 QTest::addColumn<QPoint>(name: "point2");
156 QTest::addColumn<QPoint>(name: "expected");
157
158 QTest::newRow(dataTag: "(0, 0) + (0, 0)") << QPoint(0, 0) << QPoint(0, 0) << QPoint(0, 0);
159 QTest::newRow(dataTag: "(0, 9) + (1, 0)") << QPoint(0, 9) << QPoint(1, 0) << QPoint(1, 9);
160 QTest::newRow(dataTag: "(INT_MIN, 0) + (1, 0)") << QPoint(INT_MIN, 0) << QPoint(1, 0) << QPoint(INT_MIN + 1, 0);
161 QTest::newRow(dataTag: "(INT_MAX, 0) + (-1, 0)") << QPoint(INT_MAX, 0) << QPoint(-1, 0) << QPoint(INT_MAX - 1, 0);
162}
163
164void tst_QPoint::operator_add()
165{
166 QFETCH(QPoint, point1);
167 QFETCH(QPoint, point2);
168 QFETCH(QPoint, expected);
169
170 QCOMPARE(point1 + point2, expected);
171 point1 += point2;
172 QCOMPARE(point1, expected);
173}
174
175void tst_QPoint::operator_subtract_data()
176{
177 QTest::addColumn<QPoint>(name: "point1");
178 QTest::addColumn<QPoint>(name: "point2");
179 QTest::addColumn<QPoint>(name: "expected");
180
181 QTest::newRow(dataTag: "(0, 0) - (0, 0)") << QPoint(0, 0) << QPoint(0, 0) << QPoint(0, 0);
182 QTest::newRow(dataTag: "(0, 9) - (1, 0)") << QPoint(0, 9) << QPoint(1, 0) << QPoint(-1, 9);
183 QTest::newRow(dataTag: "(INT_MAX, 0) - (1, 0)") << QPoint(INT_MAX, 0) << QPoint(1, 0) << QPoint(INT_MAX - 1, 0);
184 QTest::newRow(dataTag: "(INT_MIN, 0) - (-1, 0)") << QPoint(INT_MIN, 0) << QPoint(-1, 0) << QPoint(INT_MIN - -1, 0);
185}
186
187void tst_QPoint::operator_subtract()
188{
189 QFETCH(QPoint, point1);
190 QFETCH(QPoint, point2);
191 QFETCH(QPoint, expected);
192
193 QCOMPARE(point1 - point2, expected);
194 point1 -= point2;
195 QCOMPARE(point1, expected);
196}
197
198enum PrimitiveType { Int, Float, Double };
199
200Q_DECLARE_METATYPE(PrimitiveType)
201
202void tst_QPoint::operator_multiply_data()
203{
204 QTest::addColumn<QPoint>(name: "point");
205 QTest::addColumn<double>(name: "factorAsDouble");
206 QTest::addColumn<PrimitiveType>(name: "type");
207 QTest::addColumn<QPoint>(name: "expected");
208
209 QTest::newRow(dataTag: "(0, 0) * 0.0") << QPoint(0, 0) << 0.0 << Double << QPoint(0, 0);
210 QTest::newRow(dataTag: "(INT_MIN, 1) * 0.5") << QPoint(INT_MIN, 1) << 0.5 << Double << QPoint(qRound(INT_MIN * 0.5), 1);
211 QTest::newRow(dataTag: "(INT_MAX, 2) * 0.5") << QPoint(INT_MAX, 2) << 0.5 << Double << QPoint(qRound(INT_MAX * 0.5), 1);
212
213 QTest::newRow(dataTag: "(0, 0) * 0") << QPoint(0, 0) << 0.0 << Int << QPoint(0, 0);
214 QTest::newRow(dataTag: "(INT_MIN + 1, 0) * -1") << QPoint(INT_MIN + 1, 0) << -1.0 << Int << QPoint((INT_MIN + 1) * -1, 0);
215 QTest::newRow(dataTag: "(INT_MAX, 0) * -1") << QPoint(INT_MAX, 0) << -1.0 << Int << QPoint(INT_MAX * -1, 0);
216
217 QTest::newRow(dataTag: "(0, 0) * 0.0f") << QPoint(0, 0) << 0.0 << Float << QPoint(0, 0);
218 QTest::newRow(dataTag: "(INT_MIN, 0) * -0.5f") << QPoint(INT_MIN, 0) << -0.5 << Float << QPoint(qRound(INT_MIN * -0.5f), 0);
219}
220
221template<typename T>
222void multiplyTest(QPoint point, double factor, const QPoint &expected)
223{
224 T factorAsT = static_cast<T>(factor);
225
226 QCOMPARE(point * factorAsT, expected);
227 // Test with reversed argument version.
228 QCOMPARE(factorAsT * point, expected);
229 point *= factorAsT;
230 QCOMPARE(point, expected);
231}
232
233void tst_QPoint::operator_multiply()
234{
235 QFETCH(QPoint, point);
236 QFETCH(double, factorAsDouble);
237 QFETCH(PrimitiveType, type);
238 QFETCH(QPoint, expected);
239
240 if (type == Int)
241 multiplyTest<int>(point, factor: factorAsDouble, expected);
242 else if (type == Float)
243 multiplyTest<float>(point, factor: factorAsDouble, expected);
244 else if (type == Double)
245 multiplyTest<double>(point, factor: factorAsDouble, expected);
246}
247
248void tst_QPoint::operator_divide_data()
249{
250 QTest::addColumn<QPoint>(name: "point");
251 QTest::addColumn<qreal>(name: "divisor");
252 QTest::addColumn<QPoint>(name: "expected");
253
254 QTest::newRow(dataTag: "(0, 0) / 1") << QPoint(0, 0) << qreal(1) << QPoint(0, 0);
255 QTest::newRow(dataTag: "(0, 9) / 2") << QPoint(0, 9) << qreal(2) << QPoint(0, 5);
256 QTest::newRow(dataTag: "(INT_MAX, 0) / 2") << QPoint(INT_MAX, 0) << qreal(2) << QPoint(qRound(INT_MAX / qreal(2)), 0);
257 QTest::newRow(dataTag: "(INT_MIN, 0) / -1.5") << QPoint(INT_MIN, 0) << qreal(-1.5) << QPoint(qRound(INT_MIN / qreal(-1.5)), 0);
258}
259
260void tst_QPoint::operator_divide()
261{
262 QFETCH(QPoint, point);
263 QFETCH(qreal, divisor);
264 QFETCH(QPoint, expected);
265
266 QCOMPARE(point / divisor, expected);
267 point /= divisor;
268 QCOMPARE(point, expected);
269}
270
271void tst_QPoint::dotProduct_data()
272{
273 QTest::addColumn<QPoint>(name: "point1");
274 QTest::addColumn<QPoint>(name: "point2");
275 QTest::addColumn<int>(name: "expected");
276
277 QTest::newRow(dataTag: "(0, 0) dot (0, 0)") << QPoint(0, 0) << QPoint(0, 0)<< 0;
278 QTest::newRow(dataTag: "(10, 0) dot (0, 10)") << QPoint(10, 0) << QPoint(0, 10) << 0;
279 QTest::newRow(dataTag: "(0, 10) dot (10, 0)") << QPoint(0, 10) << QPoint(10, 0) << 0;
280 QTest::newRow(dataTag: "(10, 20) dot (-10, -20)") << QPoint(10, 20) << QPoint(-10, -20) << -500;
281 QTest::newRow(dataTag: "(-10, -20) dot (10, 20)") << QPoint(-10, -20) << QPoint(10, 20) << -500;
282}
283
284void tst_QPoint::dotProduct()
285{
286 QFETCH(QPoint, point1);
287 QFETCH(QPoint, point2);
288 QFETCH(int, expected);
289
290 QCOMPARE(QPoint::dotProduct(point1, point2), expected);
291}
292
293void tst_QPoint::operator_unary_plus_data()
294{
295 operator_unary_minus_data();
296}
297
298void tst_QPoint::operator_unary_plus()
299{
300 QFETCH(QPoint, point);
301 // Should be a NOOP.
302 QCOMPARE(+point, point);
303}
304
305void tst_QPoint::operator_unary_minus_data()
306{
307 QTest::addColumn<QPoint>(name: "point");
308 QTest::addColumn<QPoint>(name: "expected");
309
310 QTest::newRow(dataTag: "-(0, 0)") << QPoint(0, 0) << QPoint(0, 0);
311 QTest::newRow(dataTag: "-(-1, 0)") << QPoint(-1, 0) << QPoint(1, 0);
312 QTest::newRow(dataTag: "-(0, -1)") << QPoint(0, -1) << QPoint(0, 1);
313 QTest::newRow(dataTag: "-(-INT_MAX, INT_MAX)") << QPoint(-INT_MAX, INT_MAX) << QPoint(INT_MAX, -INT_MAX);
314}
315
316void tst_QPoint::operator_unary_minus()
317{
318 QFETCH(QPoint, point);
319 QFETCH(QPoint, expected);
320
321 QCOMPARE(-point, expected);
322}
323
324void tst_QPoint::operator_eq_data()
325{
326 QTest::addColumn<QPoint>(name: "point1");
327 QTest::addColumn<QPoint>(name: "point2");
328 QTest::addColumn<bool>(name: "expectEqual");
329
330 QTest::newRow(dataTag: "(0, 0) == (0, 0)") << QPoint(0, 0) << QPoint(0, 0) << true;
331 QTest::newRow(dataTag: "(-1, 0) == (-1, 0)") << QPoint(-1, 0) << QPoint(-1, 0) << true;
332 QTest::newRow(dataTag: "(-1, 0) != (0, 0)") << QPoint(-1, 0) << QPoint(0, 0) << false;
333 QTest::newRow(dataTag: "(-1, 0) != (0, -1)") << QPoint(-1, 0) << QPoint(0, -1) << false;
334 QTest::newRow(dataTag: "(1, 99999) != (-1, 99999)") << QPoint(1, 99999) << QPoint(-1, 99999) << false;
335 QTest::newRow(dataTag: "(INT_MIN, INT_MIN) == (INT_MIN, INT_MIN)") << QPoint(INT_MIN, INT_MIN) << QPoint(INT_MIN, INT_MIN) << true;
336 QTest::newRow(dataTag: "(INT_MAX, INT_MAX) == (INT_MAX, INT_MAX)") << QPoint(INT_MAX, INT_MAX) << QPoint(INT_MAX, INT_MAX) << true;
337}
338
339void tst_QPoint::operator_eq()
340{
341 QFETCH(QPoint, point1);
342 QFETCH(QPoint, point2);
343 QFETCH(bool, expectEqual);
344
345 bool equal = point1 == point2;
346 QCOMPARE(equal, expectEqual);
347 bool notEqual = point1 != point2;
348 QCOMPARE(notEqual, !expectEqual);
349}
350
351#ifndef QT_NO_DATASTREAM
352void tst_QPoint::stream_data()
353{
354 QTest::addColumn<QPoint>(name: "point");
355
356 QTest::newRow(dataTag: "(0, 0)") << QPoint(0, 0);
357 QTest::newRow(dataTag: "(-1, 1)") << QPoint(-1, 1);
358 QTest::newRow(dataTag: "(1, -1)") << QPoint(1, -1);
359 QTest::newRow(dataTag: "(INT_MIN, INT_MAX)") << QPoint(INT_MIN, INT_MAX);
360}
361
362void tst_QPoint::stream()
363{
364 QFETCH(QPoint, point);
365
366 QBuffer tmp;
367 QVERIFY(tmp.open(QBuffer::ReadWrite));
368 QDataStream stream(&tmp);
369 // Ensure that stream returned is the same stream we inserted into.
370 QDataStream &insertionStreamRef(stream << point);
371 QVERIFY(&insertionStreamRef == &stream);
372
373 tmp.seek(off: 0);
374 QPoint pointFromStream;
375 QDataStream &extractionStreamRef(stream >> pointFromStream);
376 QVERIFY(&extractionStreamRef == &stream);
377 QCOMPARE(pointFromStream, point);
378}
379#endif
380
381QTEST_MAIN(tst_QPoint)
382#include "tst_qpoint.moc"
383

source code of qtbase/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp