0c95012ae117edc05cc5bdc3c160cd91051a27d8
[situare] / tests / coordinates / geocoordinate / testgeocoordinate.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/scenecoordinate.h"
26 #include "map/mapcommon.h"
27
28 #include "coordinates/geocoordinate.h"
29
30 const double LATITUDE = 12.345678;
31 const double LONGITUDE = -89.765432;
32
33 const double ONE_SCENE_PIXEL_WIDTH_IN_DEGREES = 0.00000536441802978516;
34
35 class TestGeoCoordinate : public QObject
36 {
37     Q_OBJECT
38
39 private Q_SLOTS:
40     void constructors();
41     void isNull();
42     void conversion();
43     void conversion_data();
44     void settersAndGetters();
45 };
46
47 // for formatting the output of double valuest into the test log
48 namespace QTest {
49     template<>
50     char *toString(const double &number)
51     {
52         QByteArray ba;
53         ba += QByteArray::number(number, 'f', 9);
54         return qstrdup(ba.data());
55     }
56 }
57
58 void TestGeoCoordinate::constructors()
59 {
60     GeoCoordinate coordinate;
61     QVERIFY(coordinate.isNull());
62
63     GeoCoordinate coordinate2(LATITUDE, LONGITUDE);
64     QCOMPARE(coordinate2.latitude(), LATITUDE);
65     QCOMPARE(coordinate2.longitude(), LONGITUDE);
66
67     // NOTE: constructor with conversion from GeoCoordinate is tested in conversion() test slot
68 }
69
70 void TestGeoCoordinate::conversion()
71 {
72     // allow rounding error of one tenth of one scene pixel
73     const double MAX_ERROR = ONE_SCENE_PIXEL_WIDTH_IN_DEGREES * 0.1;
74
75     QFETCH(SceneCoordinate, sceneCoordinate);
76     QFETCH(GeoCoordinate, result);
77
78     GeoCoordinate geoCoordinate(sceneCoordinate);
79
80     QVERIFY(qAbs(geoCoordinate.latitude() - result.latitude()) < MAX_ERROR);
81     QVERIFY(qAbs(geoCoordinate.longitude() - result.longitude()) < MAX_ERROR);
82 }
83
84 void TestGeoCoordinate::conversion_data()
85 {
86     QTest::addColumn<SceneCoordinate>("sceneCoordinate");
87     QTest::addColumn<GeoCoordinate>("result");
88
89     QTest::newRow("top left") << SceneCoordinate(MAP_MIN_PIXEL_X, MAP_MIN_PIXEL_Y)
90                               << GeoCoordinate(MAX_LATITUDE, MIN_LONGITUDE);
91
92     const double LAST_SCENE_HORIZONTAL_PIXEL_LONGITUDE = MAX_LONGITUDE
93                                                          - ONE_SCENE_PIXEL_WIDTH_IN_DEGREES;
94
95     QTest::newRow("bottom right") << SceneCoordinate(MAP_MAX_PIXEL_X, MAP_MAX_PIXEL_Y)
96                                   << GeoCoordinate(MIN_LATITUDE, LAST_SCENE_HORIZONTAL_PIXEL_LONGITUDE);
97 }
98
99 void TestGeoCoordinate::isNull()
100 {
101     GeoCoordinate coordinate;
102     QVERIFY(coordinate.isNull());
103     coordinate.setLatitude(1);
104     QVERIFY(!coordinate.isNull());
105
106     GeoCoordinate coordinate2;
107     QVERIFY(coordinate2.isNull());
108     coordinate2.setLongitude(1);
109     QVERIFY(!coordinate.isNull());
110
111     GeoCoordinate coordinate3;
112     QVERIFY(coordinate3.isNull());
113     coordinate3.setLatitude(1);
114     coordinate3.setLongitude(1);
115     QVERIFY(!coordinate.isNull());
116 }
117
118 void TestGeoCoordinate::settersAndGetters()
119 {
120     GeoCoordinate coordinate;
121     QCOMPARE(coordinate.latitude(), (double)0);
122     QCOMPARE(coordinate.longitude(), (double)0);
123
124     coordinate.setLatitude(LATITUDE);
125     coordinate.setLongitude(LONGITUDE);
126
127     QCOMPARE(coordinate.latitude(), LATITUDE);
128     QCOMPARE(coordinate.longitude(), LONGITUDE);
129 }
130
131 QTEST_APPLESS_MAIN(TestGeoCoordinate);
132
133 #include "testgeocoordinate.moc"