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 "qgeotilespec_p.h"
33 
34 QT_USE_NAMESPACE
35 
36 class tst_QGeoTileSpec : public QObject
37 {
38     Q_OBJECT
39 
40 public:
41     tst_QGeoTileSpec();
42 
43 private:
44     void populateGeoTileSpecData();
45 
46 private Q_SLOTS:
47     void constructorTest_data();
48     void constructorTest();
49     void pluginTest();
50     void zoomTest();
51     void xTest();
52     void yTest();
53     void mapIdTest();
54     void assignsOperatorTest_data();
55     void assignsOperatorTest();
56     void equalsOperatorTest_data();
57     void equalsOperatorTest();
58     void lessThanOperatorTest_data();
59     void lessThanOperatorTest();
60     void qHashTest_data();
61     void qHashTest();
62 };
63 
tst_QGeoTileSpec()64 tst_QGeoTileSpec::tst_QGeoTileSpec()
65 {
66 }
67 
populateGeoTileSpecData()68 void tst_QGeoTileSpec::populateGeoTileSpecData(){
69     QTest::addColumn<QString>("plugin");
70     QTest::addColumn<int>("mapId");
71     QTest::addColumn<int>("zoom");
72     QTest::addColumn<int>("x");
73     QTest::addColumn<int>("y");
74     QTest::newRow("zeros") << QString() << 0 << 0 << 0 << 0;
75     QTest::newRow("valid") << QString("geo plugin") << 455 << 1 << 20 << 50;
76     QTest::newRow("negative values") << QString("geo plugin negative") << -350 << 2 << -20 << -50;
77 }
78 
constructorTest_data()79 void tst_QGeoTileSpec::constructorTest_data()
80 {
81     populateGeoTileSpecData();
82 }
83 
constructorTest()84 void tst_QGeoTileSpec::constructorTest()
85 {
86     QFETCH(QString,plugin);
87     QFETCH(int,zoom);
88     QFETCH(int,mapId);
89     QFETCH(int,x);
90     QFETCH(int,y);
91 
92     // test constructor copy with default values
93     QGeoTileSpec testObj;
94     QGeoTileSpec testObj2(testObj);
95     QCOMPARE(testObj.plugin(), testObj2.plugin());
96     QCOMPARE(testObj.mapId(), testObj2.mapId());
97     QCOMPARE(testObj.zoom(), testObj2.zoom());
98     QCOMPARE(testObj.x(), testObj2.x());
99     QCOMPARE(testObj.y(), testObj2.y());
100 
101     // test second construct
102     QGeoTileSpec testObj3(plugin, mapId, zoom, x, y);
103     QCOMPARE(testObj3.plugin(), plugin);
104     QCOMPARE(testObj3.mapId(), mapId);
105     QCOMPARE(testObj3.zoom(), zoom);
106     QCOMPARE(testObj3.x(), x);
107     QCOMPARE(testObj3.y(), y);
108 }
109 
pluginTest()110 void tst_QGeoTileSpec::pluginTest()
111 {
112     QGeoTileSpec tileSpec;
113     QCOMPARE(tileSpec.plugin(), QString());
114 
115     QGeoTileSpec tileSpec2(QString("plugin test"),1,10,10,5);
116     QCOMPARE(tileSpec2.plugin(), QString("plugin test"));
117 }
118 
zoomTest()119 void tst_QGeoTileSpec::zoomTest()
120 {
121     QGeoTileSpec tileSpec;
122     QVERIFY(tileSpec.zoom() == -1);
123     tileSpec.setZoom(1);
124     QVERIFY(tileSpec.zoom() == 1);
125 
126     QGeoTileSpec tileSpec2 = tileSpec;
127     QVERIFY(tileSpec2.zoom() == 1);
128     tileSpec.setZoom(2);
129     QVERIFY(tileSpec2.zoom() == 1);
130 }
131 
xTest()132 void tst_QGeoTileSpec::xTest()
133 {
134     QGeoTileSpec tileSpec;
135     QVERIFY(tileSpec.x() == -1);
136     tileSpec.setX(10);
137     QVERIFY(tileSpec.x() == 10);
138 
139     QGeoTileSpec tileSpec2 = tileSpec;
140     QVERIFY(tileSpec2.x() == 10);
141     tileSpec.setX(30);
142     QVERIFY(tileSpec2.x() == 10);
143 }
144 
yTest()145 void tst_QGeoTileSpec::yTest()
146 {
147     QGeoTileSpec tileSpec;
148     QVERIFY(tileSpec.y() == -1);
149     tileSpec.setY(20);
150     QVERIFY(tileSpec.y() == 20);
151 
152     QGeoTileSpec tileSpec2 = tileSpec;
153     QVERIFY(tileSpec2.y() == 20);
154     tileSpec.setY(40);
155     QVERIFY(tileSpec2.y() == 20);
156 }
157 
mapIdTest()158 void tst_QGeoTileSpec::mapIdTest()
159 {
160     QGeoTileSpec tileSpec;
161     QVERIFY(tileSpec.mapId() == 0);
162     tileSpec.setMapId(1);
163     QVERIFY(tileSpec.mapId() == 1);
164 
165     QGeoTileSpec tileSpec2 = tileSpec;
166     QVERIFY(tileSpec2.mapId() == 1);
167     tileSpec.setMapId(5);
168     QVERIFY(tileSpec2.mapId() == 1);
169 }
170 
assignsOperatorTest_data()171 void tst_QGeoTileSpec::assignsOperatorTest_data()
172 {
173     populateGeoTileSpecData();
174 }
175 
176 
assignsOperatorTest()177 void tst_QGeoTileSpec::assignsOperatorTest()
178 {
179     QFETCH(QString,plugin);
180     QFETCH(int,mapId);
181     QFETCH(int,zoom);
182     QFETCH(int,x);
183     QFETCH(int,y);
184 
185     QGeoTileSpec testObj(plugin, mapId, zoom, x, y);
186     QGeoTileSpec testObj2;
187     testObj2 = testObj;
188     // test the correctness of the asignment operator
189     QVERIFY2(testObj2.plugin() == plugin, "Plugin not copied correctly");
190     QVERIFY2(testObj2.zoom() == zoom, "Zoom not copied correctly");
191     QVERIFY2(testObj2.mapId() == mapId, "Map Id not copied correctly");
192     QVERIFY2(testObj2.x() == x, "X not copied correctly");
193     QVERIFY2(testObj2.y() == y, "Y not copied correctly");
194     // verify that values have not changed after an assignment
195     QVERIFY2(testObj.plugin() == testObj2.plugin(), "Plugin not copied correctly");
196     QVERIFY2(testObj.zoom() == testObj2.zoom(), "Zoom not copied correctly");
197     QVERIFY2(testObj.mapId() == testObj2.mapId(), "Map Id not copied correctly");
198     QVERIFY2(testObj.x() == testObj2.x(), "X not copied correctly");
199     QVERIFY2(testObj.y() == testObj2.y(), "Y not copied correctly");
200 }
201 
202 
equalsOperatorTest_data()203 void tst_QGeoTileSpec::equalsOperatorTest_data()
204 {
205     populateGeoTileSpecData();
206 }
207 
equalsOperatorTest()208 void tst_QGeoTileSpec::equalsOperatorTest()
209 {
210     QFETCH(QString,plugin);
211     QFETCH(int,mapId);
212     QFETCH(int,zoom);
213     QFETCH(int,x);
214     QFETCH(int,y);
215 
216     QGeoTileSpec testObj(plugin, mapId, zoom, x, y);
217     QGeoTileSpec testObj2(plugin, mapId, zoom, x, y);
218     QVERIFY2(testObj == testObj2, "Equals operator is not correct");
219 
220     // test QGeoTileSpec pairs where they differ in one field
221     testObj2.setZoom(zoom+1);
222     QVERIFY2(!(testObj == testObj2), "Equals operator is not correct");
223     testObj2 = testObj;
224     testObj2.setMapId(mapId+1);
225     QVERIFY2(!(testObj == testObj2), "Equals operator is not correct");
226     testObj2 = testObj;
227     testObj2.setX(x+1);
228     QVERIFY2(!(testObj == testObj2), "Equals operator is not correct");
229     testObj2 = testObj;
230     testObj2.setY(y+1);
231     QVERIFY2(!(testObj == testObj2), "Equals operator is not correct");
232 }
233 
lessThanOperatorTest_data()234 void tst_QGeoTileSpec::lessThanOperatorTest_data()
235 {
236     populateGeoTileSpecData();
237 }
238 
lessThanOperatorTest()239 void tst_QGeoTileSpec::lessThanOperatorTest()
240 {
241     QFETCH(QString,plugin);
242     QFETCH(int,mapId);
243     QFETCH(int,zoom);
244     QFETCH(int,x);
245     QFETCH(int,y);
246 
247     QGeoTileSpec testObj(plugin, mapId, zoom, x, y);
248     QGeoTileSpec testObj2(testObj);
249     QVERIFY(!(testObj < testObj2));
250 
251     testObj2.setMapId(mapId-1);
252     QVERIFY2(testObj2 < testObj, "Less than operator is not correct for mapId");
253     testObj2 = testObj;
254     testObj2.setZoom(zoom-1);
255     QVERIFY2(testObj2 < testObj, "Less than operator is not correct for zoom");
256     testObj2 = testObj;
257     testObj2.setX(x-1);
258     QVERIFY2(testObj2 < testObj, "Less than operator is not correct for x");
259     testObj2 = testObj;
260     testObj2.setY(y-1);
261     QVERIFY2(testObj2 < testObj, "Less than operator is not correct for y");
262 
263     // less than comparisons are done in the order: plugin -> mapId -> zoom -> x -> y
264     // the test below checks if the order is correct
265     QGeoTileSpec testObj3(plugin + QString('a'), mapId-1, zoom-1, x-1, y-1);
266     QVERIFY2(testObj < testObj3, "Order of less than operator is not correct");
267     QGeoTileSpec testObj4(plugin, mapId+1, zoom-1, x-1, y-1);
268     QVERIFY2(testObj < testObj4, "Order of less than operator is not correct");
269     QGeoTileSpec testObj5(plugin, mapId, zoom+1, x-1, y-1);
270     QVERIFY2(testObj < testObj5, "Order of less than operator is not correct");
271     QGeoTileSpec testObj6(plugin, mapId, zoom, x+1, y-1);
272     QVERIFY2(testObj < testObj6, "Order of less than operator is not correct");
273     QGeoTileSpec testObj7(plugin, mapId, zoom, x, y+1);
274     QVERIFY2(testObj < testObj7, "Order of less than operator is not correct");
275 
276     QGeoTileSpec testObj8(plugin, mapId-1, zoom+1, x+1, y+1);
277     QVERIFY2(testObj8 < testObj, "Order of less than operator is not correct");
278     QGeoTileSpec testObj9(plugin, mapId, zoom-1, x+1, y+1);
279     QVERIFY2(testObj9 < testObj, "Order of less than operator is not correct");
280     QGeoTileSpec testObj10(plugin, mapId, zoom, x-1, y+1);
281     QVERIFY2(testObj10 < testObj, "Order of less than operator is not correct");
282 }
283 
qHashTest_data()284 void tst_QGeoTileSpec::qHashTest_data(){
285     populateGeoTileSpecData();
286 }
287 
qHashTest()288 void tst_QGeoTileSpec::qHashTest()
289 {
290     QGeoTileSpec testObj;
291     unsigned int hash1 = qHash(testObj);
292     QGeoTileSpec testObj2;
293     testObj2 = testObj;
294     unsigned int hash2 = qHash(testObj2);
295     QCOMPARE(hash1, hash2);
296 
297     QFETCH(QString,plugin);
298     QFETCH(int,mapId);
299     QFETCH(int,zoom);
300     QFETCH(int,x);
301     QFETCH(int,y);
302 
303     QGeoTileSpec testObj3(plugin, mapId, zoom, x, y);
304     unsigned int hash3 = qHash(testObj3);
305     QVERIFY(hash1 != hash3);
306 
307     testObj2.setMapId(testObj3.mapId()+1);
308     testObj2.setZoom(testObj3.zoom()+1);
309     testObj2.setX(testObj3.x()*5);
310     testObj2.setY(testObj3.y()*10);
311     hash2 = qHash(testObj2);
312     QVERIFY(hash2 != hash3);
313 }
314 
315 QTEST_APPLESS_MAIN(tst_QGeoTileSpec)
316 
317 #include "tst_qgeotilespec.moc"
318 
319 
320