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 <QtCore/QString>
30#include <QtTest/QtTest>
31
32#include <QtPositioning/private/qdoublevector2d_p.h>
33#include <QtPositioning/private/qdoublevector3d_p.h>
34
35QT_USE_NAMESPACE
36
37class tst_doubleVectors : public QObject
38{
39 Q_OBJECT
40
41private Q_SLOTS:
42 // 2D
43 void constructor2dTest();
44 void basicFunctions2dTest();
45 void unaryOperator2dTest();
46 void binaryOperator2dTest();
47
48 // 3D
49 void constructor3dTest();
50 void basicFunctions3dTest();
51 void unaryOperator3dTest();
52 void binaryOperator3dTest();
53};
54
55// DoubleVector2D
56
57void tst_doubleVectors::constructor2dTest()
58{
59 // empty constructor, since it sets to 0, we should check, in case people rely on it
60 QDoubleVector2D v1;
61 QCOMPARE(v1.x(), 0.0);
62 QCOMPARE(v1.y(), 0.0);
63 QCOMPARE(v1.isNull(), true);
64 v1 = QDoubleVector2D(1.1, -2.5); // assignment and constructor
65 QCOMPARE(v1.x(), 1.1);
66 QCOMPARE(v1.y(), -2.5);
67 QDoubleVector2D v2(v1); // copy constructor
68 QCOMPARE(v2.x(), 1.1);
69 QCOMPARE(v2.y(), -2.5);
70 const QDoubleVector3D v3d(2.2, 3.3, 4.4);
71 QDoubleVector2D v3(v3d); // constructor from 3d vector, just copies x and y
72 QCOMPARE(v3.x(), 2.2);
73 QCOMPARE(v3.y(), 3.3);
74 QCOMPARE(v3.isNull(), false);
75}
76
77void tst_doubleVectors::basicFunctions2dTest()
78{
79 QDoubleVector2D v1;
80 v1.setX(3.0);
81 v1.setY(4.0);
82 QCOMPARE(v1.x(), 3.0);
83 QCOMPARE(v1.y(), 4.0);
84 QCOMPARE(v1.length(), 5.0);
85 QDoubleVector2D v2 = v1.normalized();
86 QCOMPARE(v1.lengthSquared(), 25.0);
87 v1.normalize();
88 QCOMPARE(v1.x(), 3.0/5.0);
89 QCOMPARE(v1.y(), 4.0/5.0);
90 QCOMPARE(v2.x(), 3.0/5.0);
91 QCOMPARE(v2.y(), 4.0/5.0);
92
93 QDoubleVector3D v3d = v1.toVector3D();
94 QCOMPARE(v3d.x(), 3.0/5.0);
95 QCOMPARE(v3d.y(), 4.0/5.0);
96 QCOMPARE(v3d.z(), 0.0);
97}
98
99void tst_doubleVectors::unaryOperator2dTest()
100{
101 QDoubleVector2D v1(1.1, 2.2);
102 QDoubleVector2D v2 = -v1;
103 QCOMPARE(v2.x(), -1.1);
104 QCOMPARE(v2.y(), -2.2);
105
106 v1 *= 2.0;
107 QCOMPARE(v1.x(), 2.2);
108 QCOMPARE(v1.y(), 4.4);
109
110 v2 /= 2.0;
111 QCOMPARE(v2.x(), -0.55);
112 QCOMPARE(v2.y(), -1.1);
113
114 v1 += v2;
115 QCOMPARE(v1.x(), 1.65);
116 QCOMPARE(v1.y(), 3.3);
117
118 v1 -= v2;
119 QCOMPARE(v1.x(), 2.2);
120 QCOMPARE(v1.y(), 4.4);
121
122 v1 *= v2;
123 QCOMPARE(v1.x(), -1.21);
124 QCOMPARE(v1.y(), -4.84);
125}
126
127void tst_doubleVectors::binaryOperator2dTest()
128{
129 QDoubleVector2D v1(1.1, 2.2);
130 QDoubleVector2D v2(3.4, 4.4);
131 QDoubleVector2D v3 = v1 + v2;
132 QCOMPARE(v3.x(), 4.5);
133 QCOMPARE(v3.y(), 6.6);
134
135 QDoubleVector2D v4 = v1 - v2;
136 QCOMPARE(v4.x(), -2.3);
137 QCOMPARE(v4.y(), -2.2);
138
139 QDoubleVector2D v5 = v2 * 2;
140 QCOMPARE(v5.x(), 6.8);
141 QCOMPARE(v5.y(), 8.8);
142
143 QDoubleVector2D v6 = 2 * v2;
144 QCOMPARE(v6.x(), 6.8);
145 QCOMPARE(v6.y(), 8.8);
146
147 QDoubleVector2D v7 = v2 / 2;
148 QCOMPARE(v7.x(), 1.7);
149 QCOMPARE(v7.y(), 2.2);
150
151 double d = QDoubleVector2D::dotProduct(v1, v2);
152 QCOMPARE(d, 13.42);
153
154 QCOMPARE(v5 == v6, true);
155 QCOMPARE(v5 != v6, false);
156 QCOMPARE(v6 == v7, false);
157 QCOMPARE(v6 != v7, true);
158}
159
160
161
162// DoubleVector3D
163
164
165void tst_doubleVectors::constructor3dTest()
166{
167 // empty constructor, since it sets to 0, we should check, in case people rely on it
168 QDoubleVector3D v1;
169 QCOMPARE(v1.x(), 0.0);
170 QCOMPARE(v1.y(), 0.0);
171 QCOMPARE(v1.z(), 0.0);
172 QCOMPARE(v1.isNull(), true);
173 v1 = QDoubleVector3D(1.1, -2.5, 3.2); // assignment and constructor
174 QCOMPARE(v1.x(), 1.1);
175 QCOMPARE(v1.y(), -2.5);
176 QCOMPARE(v1.z(), 3.2);
177 QDoubleVector3D v2(v1); // copy constructor
178 QCOMPARE(v2.x(), 1.1);
179 QCOMPARE(v2.y(), -2.5);
180 QCOMPARE(v2.z(), 3.2);
181 const QDoubleVector2D v2d(2.2, 3.3);
182 QDoubleVector3D v3(v2d); // constructor from 3d vector, just copies x and y
183 QCOMPARE(v3.x(), 2.2);
184 QCOMPARE(v3.y(), 3.3);
185 QCOMPARE(v3.z(), 0.0);
186 QCOMPARE(v3.isNull(), false);
187 const QDoubleVector2D v2d2(2.2, 3.3);
188 QDoubleVector3D v4(v2d2, -13.6); // constructor from 2d vector
189 QCOMPARE(v4.x(), 2.2);
190 QCOMPARE(v4.y(), 3.3);
191 QCOMPARE(v4.z(), -13.6);
192}
193
194void tst_doubleVectors::basicFunctions3dTest()
195{
196 QDoubleVector3D v1;
197 v1.setX(2.0);
198 v1.setY(3.0);
199 v1.setZ(6.0);
200 QCOMPARE(v1.x(), 2.0);
201 QCOMPARE(v1.y(), 3.0);
202 QCOMPARE(v1.z(), 6.0);
203 QCOMPARE(v1.length(), 7.0);
204 QDoubleVector3D v2 = v1.normalized();
205 QCOMPARE(v1.lengthSquared(), 49.0);
206 v1.normalize();
207 QCOMPARE(v1.x(), 2.0/7.0);
208 QCOMPARE(v1.y(), 3.0/7.0);
209 QCOMPARE(v1.z(), 6.0/7.0);
210 QCOMPARE(v2.x(), 2.0/7.0);
211 QCOMPARE(v2.y(), 3.0/7.0);
212 QCOMPARE(v2.z(), 6.0/7.0);
213
214 QDoubleVector2D v2d = v1.toVector2D();
215 QCOMPARE(v2d.x(), 2.0/7.0);
216 QCOMPARE(v2d.y(), 3.0/7.0);
217}
218
219void tst_doubleVectors::unaryOperator3dTest()
220{
221 QDoubleVector3D v1(1.1, 2.2, 3.3);
222 QDoubleVector3D v2 = -v1;
223 QCOMPARE(v2.x(), -1.1);
224 QCOMPARE(v2.y(), -2.2);
225 QCOMPARE(v2.z(), -3.3);
226
227 v1 *= 2.0;
228 QCOMPARE(v1.x(), 2.2);
229 QCOMPARE(v1.y(), 4.4);
230 QCOMPARE(v1.z(), 6.6);
231
232 v2 /= 2.0;
233 QCOMPARE(v2.x(), -0.55);
234 QCOMPARE(v2.y(), -1.1);
235 QCOMPARE(v2.z(), -1.65);
236
237 v1 += v2;
238 QCOMPARE(v1.x(), 1.65);
239 QCOMPARE(v1.y(), 3.3);
240 QCOMPARE(v1.z(), 4.95);
241
242 v1 -= v2;
243 QCOMPARE(v1.x(), 2.2);
244 QCOMPARE(v1.y(), 4.4);
245 QCOMPARE(v1.z(), 6.6);
246
247 v1 *= v2;
248 QCOMPARE(v1.x(), -1.21);
249 QCOMPARE(v1.y(), -4.84);
250 QCOMPARE(v1.z(), -10.89);
251}
252
253void tst_doubleVectors::binaryOperator3dTest()
254{
255 QDoubleVector3D v1(1.1, 2.2, 3.3);
256 QDoubleVector3D v2(3.4, 4.4, 5.5);
257 QDoubleVector3D v3 = v1 + v2;
258 QCOMPARE(v3.x(), 4.5);
259 QCOMPARE(v3.y(), 6.6);
260 QCOMPARE(v3.z(), 8.8);
261
262 QDoubleVector3D v4 = v1 - v2;
263 QCOMPARE(v4.x(), -2.3);
264 QCOMPARE(v4.y(), -2.2);
265 QCOMPARE(v4.z(), -2.2);
266
267 QDoubleVector3D v5 = v2 * 2;
268 QCOMPARE(v5.x(), 6.8);
269 QCOMPARE(v5.y(), 8.8);
270 QCOMPARE(v5.z(), 11.0);
271
272 QDoubleVector3D v6 = 2 * v2;
273 QCOMPARE(v6.x(), 6.8);
274 QCOMPARE(v6.y(), 8.8);
275 QCOMPARE(v6.z(), 11.0);
276
277 QDoubleVector3D v7 = v2 / 2;
278 QCOMPARE(v7.x(), 1.7);
279 QCOMPARE(v7.y(), 2.2);
280 QCOMPARE(v7.z(), 2.75);
281
282 double d = QDoubleVector3D::dotProduct(v1, v2);
283 QCOMPARE(d, 31.57);
284
285 QCOMPARE(v5 == v6, true);
286 QCOMPARE(v5 != v6, false);
287 QCOMPARE(v6 == v7, false);
288 QCOMPARE(v6 != v7, true);
289}
290
291QTEST_APPLESS_MAIN(tst_doubleVectors)
292
293#include "tst_doublevectors.moc"
294

source code of qtlocation/tests/auto/doublevectors/tst_doublevectors.cpp