2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Sami Rämö - sami.ramo@ixonos.com
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.
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.
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,
22 #include <QtCore/QString>
23 #include <QtTest/QtTest>
25 #include "coordinates/geocoordinate.h"
26 #include "map/mapcommon.h"
28 #include "coordinates/scenecoordinate.h"
30 const double X = 12.345678;
31 const double Y = -89.765432;
33 const double ORIGIN = 1000;
34 const double DELTA = 500;
36 class TestSceneCoordinate : public QObject
42 void azimuthTo_data();
45 void conversion_data();
48 void settersAndGetters();
51 // for formatting the output of double valuest into the test log
54 char *toString(const double &number)
57 ba += QByteArray::number(number, 'f', 9);
58 return qstrdup(ba.data());
62 void TestSceneCoordinate::azimuthTo()
64 QFETCH(SceneCoordinate, to);
65 QFETCH(qreal, expectedAzimuth);
67 SceneCoordinate from(ORIGIN, ORIGIN);
69 QCOMPARE(from.azimuthTo(to), expectedAzimuth);
72 void TestSceneCoordinate::azimuthTo_data()
74 QTest::addColumn<SceneCoordinate>("to");
75 QTest::addColumn<qreal>("expectedAzimuth");
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;
87 void TestSceneCoordinate::constructors()
89 SceneCoordinate coordinate;
90 QVERIFY(coordinate.isNull());
92 SceneCoordinate coordinate2(X, Y);
93 QCOMPARE(coordinate2.x(), X);
94 QCOMPARE(coordinate2.y(), Y);
96 // NOTE: constructor with conversion from GeoCoordinate is tested in conversion() test slot
99 void TestSceneCoordinate::conversion()
101 QFETCH(GeoCoordinate, geoCoordinate);
102 QFETCH(SceneCoordinate, result);
104 SceneCoordinate sceneCoordinate(geoCoordinate);
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.
110 QCOMPARE(int(sceneCoordinate.x()), int(result.x()));
111 QCOMPARE(int(sceneCoordinate.y()), int(result.y()));
114 void TestSceneCoordinate::conversion_data()
116 QTest::addColumn<GeoCoordinate>("geoCoordinate");
117 QTest::addColumn<SceneCoordinate>("result");
119 QTest::newRow("top left pixel") << GeoCoordinate(OSM_MAX_LATITUDE, MIN_LONGITUDE)
120 << SceneCoordinate(0, 0);
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);
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);
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);
138 void TestSceneCoordinate::isNull()
140 SceneCoordinate coordinate;
141 QVERIFY(coordinate.isNull());
143 QVERIFY(!coordinate.isNull());
145 SceneCoordinate coordinate2;
146 QVERIFY(coordinate2.isNull());
148 QVERIFY(!coordinate.isNull());
150 SceneCoordinate coordinate3;
151 QVERIFY(coordinate3.isNull());
154 QVERIFY(!coordinate.isNull());
157 void TestSceneCoordinate::operators()
160 SceneCoordinate coordinate(100, 30);
162 QCOMPARE(coordinate.x(), 300.0);
163 QCOMPARE(coordinate.y(), 90.0);
166 SceneCoordinate coordinate2(110, 40);
167 coordinate2 += SceneCoordinate(40, 75);
168 QCOMPARE(coordinate2.x(), 150.0);
169 QCOMPARE(coordinate2.y(), 115.0);
172 SceneCoordinate coordinate3(120, 50);
173 coordinate3 -= SceneCoordinate(45, 80);
174 QCOMPARE(coordinate3.x(), 75.0);
175 QCOMPARE(coordinate3.y(), -30.0);
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);
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);
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);
203 void TestSceneCoordinate::settersAndGetters()
205 SceneCoordinate coordinate;
206 QCOMPARE(coordinate.x(), (double)0);
207 QCOMPARE(coordinate.y(), (double)0);
212 QCOMPARE(coordinate.x(), X);
213 QCOMPARE(coordinate.y(), Y);
216 QTEST_APPLESS_MAIN(TestSceneCoordinate);
218 #include "testscenecoordinate.moc"