c8f72509b6fd2435df67379a315355fb6624eac1
[situare] / tests / coordinates / scenecoordinate / testscenecoordinate.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QtCore/QString>
23 #include <QtTest/QtTest>
24
25 #include "coordinates/geocoordinate.h"
26 #include "map/mapcommon.h"
27
28 #include "coordinates/scenecoordinate.h"
29
30 const double X = 12.345678;
31 const double Y = -89.765432;
32
33 const double ORIGIN = 1000;
34 const double DELTA = 500;
35
36 class TestSceneCoordinate : public QObject
37 {
38     Q_OBJECT
39
40 private Q_SLOTS:
41     void azimuthTo();
42     void azimuthTo_data();
43     void constructors();
44     void conversion();
45     void conversion_data();
46     void isNull();
47     void operators();
48     void settersAndGetters();
49 };
50
51 // for formatting the output of double valuest into the test log
52 namespace QTest {
53     template<>
54     char *toString(const double &number)
55     {
56         QByteArray ba;
57         ba += QByteArray::number(number, 'f', 9);
58         return qstrdup(ba.data());
59     }
60 }
61
62 void TestSceneCoordinate::azimuthTo()
63 {
64     QFETCH(SceneCoordinate, to);
65     QFETCH(qreal, expectedAzimuth);
66
67     SceneCoordinate from(ORIGIN, ORIGIN);
68
69     QCOMPARE(from.azimuthTo(to), expectedAzimuth);
70 }
71
72 void TestSceneCoordinate::azimuthTo_data()
73 {
74     QTest::addColumn<SceneCoordinate>("to");
75     QTest::addColumn<qreal>("expectedAzimuth");
76
77     QTest::newRow("N") <<  SceneCoordinate(ORIGIN, ORIGIN - DELTA) << 0.0;
78     QTest::newRow("NE") <<  SceneCoordinate(ORIGIN + DELTA, ORIGIN - DELTA) << 45.0;
79     QTest::newRow("E") <<  SceneCoordinate(ORIGIN + DELTA, ORIGIN) << 90.0;
80     QTest::newRow("SE") <<  SceneCoordinate(ORIGIN + DELTA, ORIGIN + DELTA) << 135.0;
81     QTest::newRow("S") <<  SceneCoordinate(ORIGIN, ORIGIN + DELTA) << 180.0;
82     QTest::newRow("SW") <<  SceneCoordinate(ORIGIN - DELTA, ORIGIN + DELTA) << 225.0;
83     QTest::newRow("W") <<  SceneCoordinate(ORIGIN - DELTA, ORIGIN) << 270.0;
84     QTest::newRow("NW") <<  SceneCoordinate(ORIGIN - DELTA, ORIGIN - DELTA) << 315.0;
85 }
86
87 void TestSceneCoordinate::constructors()
88 {
89     SceneCoordinate coordinate;
90     QVERIFY(coordinate.isNull());
91
92     SceneCoordinate coordinate2(X, Y);
93     QCOMPARE(coordinate2.x(), X);
94     QCOMPARE(coordinate2.y(), Y);
95
96     // NOTE: constructor with conversion from GeoCoordinate is tested in conversion() test slot
97 }
98
99 void TestSceneCoordinate::conversion()
100 {
101     QFETCH(GeoCoordinate, geoCoordinate);
102     QFETCH(SceneCoordinate, result);
103
104     SceneCoordinate sceneCoordinate(geoCoordinate);
105
106     // Comparison is done using only the integer parts because data type of the scene coordinate
107     // values is double so the result is not exact pixel value but is one containing a fractional
108     // part. Also the rounding errors below one pixel are insignificant.
109
110     QCOMPARE(int(sceneCoordinate.x()), int(result.x()));
111     QCOMPARE(int(sceneCoordinate.y()), int(result.y()));
112 }
113
114 void TestSceneCoordinate::conversion_data()
115 {
116     QTest::addColumn<GeoCoordinate>("geoCoordinate");
117     QTest::addColumn<SceneCoordinate>("result");
118
119     QTest::newRow("top left pixel") << GeoCoordinate(OSM_MAX_LATITUDE, MIN_LONGITUDE)
120                                     << SceneCoordinate(0, 0);
121
122     const double ONE_SCENE_PIXEL_WIDTH_IN_DEGREES = 0.00000536441802978516;
123     const double LAST_SCENE_HORIZONTAL_PIXEL_LONGITUDE = MAX_LONGITUDE
124                                                          - ONE_SCENE_PIXEL_WIDTH_IN_DEGREES;
125     QTest::newRow("bottom right pixel")
126             << GeoCoordinate(-OSM_MAX_LATITUDE, LAST_SCENE_HORIZONTAL_PIXEL_LONGITUDE)
127             << SceneCoordinate(OSM_MAP_MAX_PIXEL_X, OSM_MAP_MAX_PIXEL_Y);
128
129     QTest::newRow("southeast corner with 180 degrees longitude")
130             << GeoCoordinate(-OSM_MAX_LATITUDE, MAX_LONGITUDE)
131             << SceneCoordinate(OSM_MAP_MIN_PIXEL_X, OSM_MAP_MAX_PIXEL_Y);
132
133     QTest::newRow("southeast corner just little over west edge of the map")
134             << GeoCoordinate(-OSM_MAX_LATITUDE, MIN_LONGITUDE - ONE_SCENE_PIXEL_WIDTH_IN_DEGREES)
135             << SceneCoordinate(OSM_MAP_MAX_PIXEL_X, OSM_MAP_MAX_PIXEL_Y);
136 }
137
138 void TestSceneCoordinate::isNull()
139 {
140     SceneCoordinate coordinate;
141     QVERIFY(coordinate.isNull());
142     coordinate.setX(1);
143     QVERIFY(!coordinate.isNull());
144
145     SceneCoordinate coordinate2;
146     QVERIFY(coordinate2.isNull());
147     coordinate2.setY(1);
148     QVERIFY(!coordinate.isNull());
149
150     SceneCoordinate coordinate3;
151     QVERIFY(coordinate3.isNull());
152     coordinate3.setX(1);
153     coordinate3.setY(1);
154     QVERIFY(!coordinate.isNull());
155 }
156
157 void TestSceneCoordinate::operators()
158 {
159     // operator*=
160     SceneCoordinate coordinate(100, 30);
161     coordinate *= 3;
162     QCOMPARE(coordinate.x(), 300.0);
163     QCOMPARE(coordinate.y(), 90.0);
164
165     // operator+=
166     SceneCoordinate coordinate2(110, 40);
167     coordinate2 += SceneCoordinate(40, 75);
168     QCOMPARE(coordinate2.x(), 150.0);
169     QCOMPARE(coordinate2.y(), 115.0);
170
171     // operator-=
172     SceneCoordinate coordinate3(120, 50);
173     coordinate3 -= SceneCoordinate(45, 80);
174     QCOMPARE(coordinate3.x(), 75.0);
175     QCOMPARE(coordinate3.y(), -30.0);
176
177     // operator+
178     SceneCoordinate coordinate4(130, 60);
179     SceneCoordinate coordinate5(25, 10);
180     SceneCoordinate result = coordinate4 + coordinate5;
181     QCOMPARE(result.x(), 155.0);
182     QCOMPARE(result.y(), 70.0);
183     QVERIFY(&result != &coordinate4);
184     QVERIFY(&result != &coordinate5);
185
186     // operator-
187     SceneCoordinate coordinate6(140, 70);
188     SceneCoordinate coordinate7(15, 5);
189     SceneCoordinate result2 = coordinate6 - coordinate7;
190     QCOMPARE(result2.x(), 125.0);
191     QCOMPARE(result2.y(), 65.0);
192     QVERIFY(&result2 != &coordinate6);
193     QVERIFY(&result2 != &coordinate7);
194
195     // operator*
196     SceneCoordinate coordinate8(10, 20);
197     SceneCoordinate result3 = 4 * coordinate8;
198     QCOMPARE(result3.x(), 40.0);
199     QCOMPARE(result3.y(), 80.0);
200     QVERIFY(&result3 != &coordinate8);
201 }
202
203 void TestSceneCoordinate::settersAndGetters()
204 {
205     SceneCoordinate coordinate;
206     QCOMPARE(coordinate.x(), (double)0);
207     QCOMPARE(coordinate.y(), (double)0);
208
209     coordinate.setX(X);
210     coordinate.setY(Y);
211
212     QCOMPARE(coordinate.x(), X);
213     QCOMPARE(coordinate.y(), Y);
214 }
215
216 QTEST_APPLESS_MAIN(TestSceneCoordinate);
217
218 #include "testscenecoordinate.moc"