1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2010-02-07
7  * Description : test for the simple datatypes and helper functions
8  *
9  * Copyright (C) 2010-2013 by Michael G. Hansen <mike at mghansen dot de>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "primitives_utest.h"
25 
26 // local includes
27 
28 #include "geoifacetypes.h"
29 #include "geoifacecommon.h"
30 
31 using namespace Digikam;
32 
testNoOp()33 void TestPrimitives::testNoOp()
34 {
35 }
36 
testParseLatLonString()37 void TestPrimitives::testParseLatLonString()
38 {
39     // make sure there is no crash on null-pointer
40     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("52,6"), nullptr));
41 
42     GeoCoordinates coordinate;
43 
44     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("52,6"), &coordinate));
45     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:52,6"));
46 
47     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("52.5,6.5"), &coordinate));
48     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:52.5,6.5"));
49 
50     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String(" 52.5, 6.5 "), &coordinate));
51     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:52.5,6.5"));
52 
53     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("-52.5, 6.5 "), &coordinate));
54     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:-52.5,6.5"));
55 
56     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("    -52.5,  6.5   "), &coordinate));
57     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:-52.5,6.5"));
58 
59     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("52.5,-6.5"), &coordinate));
60     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:52.5,-6.5"));
61 
62     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String(""), nullptr));
63     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("52.6"), nullptr));
64     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("52.6,"), nullptr));
65     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String(",6"), nullptr));
66     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("a52,6"), nullptr));
67     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("52,a"), nullptr));
68     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("52,6a"), nullptr));
69     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("(52,6)"), nullptr));
70 }
71 
testParseXYStringToPoint()72 void TestPrimitives::testParseXYStringToPoint()
73 {
74     // make sure there is no crash on null-pointer
75     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("(52,6)"), nullptr));
76 
77     QPoint point;
78 
79     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("(52,6)"), &point));
80     QCOMPARE(point, QPoint(52,6));
81 
82     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("(10,20)"), &point));
83     QCOMPARE(point, QPoint(10,20));
84 
85     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String(" ( 52, 6 ) "), &point));
86     QCOMPARE(point, QPoint(52,6));
87 
88     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("  ( 52, 6 )  "), &point));
89     QCOMPARE(point, QPoint(52,6));
90 
91     // We used to expect integer string results, but floats are also possible.
92     // BKO 270624
93     // GeoIfaceHelperParseXYStringToPoint always rounds them to 0.
94     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("  ( 52.5, 6.5 )  "), &point));
95     QCOMPARE(point, QPoint(52,6));
96     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("  ( -52.5, 6.5 )  "), &point));
97     QCOMPARE(point, QPoint(-52,6));
98 
99     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QString::fromLatin1("(204.94641003022224, 68.00444002512285)"), &point));
100 
101     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String(""), nullptr));
102     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("()"), nullptr));
103     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(52)"), nullptr));
104     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(52,6a)"), nullptr));
105     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(a52,6)"), nullptr));
106     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("52,6"), nullptr));
107     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(,6)"), nullptr));
108     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(6,)"), nullptr));
109 }
110 
testParseBoundsString()111 void TestPrimitives::testParseBoundsString()
112 {
113     // make sure there is no crash on null-pointer
114     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52,-6),(52,6))"), nullptr));
115 
116     GeoCoordinates::Pair bounds;
117 
118     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52,-6),(52,6))"), &bounds));
119     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:-52,-6"));
120     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:52,6"));
121 
122     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52,-6), (52,6))"), &bounds));
123     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:-52,-6"));
124     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:52,6"));
125 
126     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52, -6), (52, 6))"), &bounds));
127     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:-52,-6"));
128     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:52,6"));
129 
130     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((10,20),(30,40))"), &bounds));
131     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:10,20"));
132     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:30,40"));
133 
134     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5),(52.5,6.5))"), &bounds));
135     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:-52.5,-6.5"));
136     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:52.5,6.5"));
137 
138     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String(" (-52.5,-6.5),(52.5,6.5))"), nullptr));
139     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5),(52.5,6.5) "), nullptr));
140     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5), 52.5,6.5))"), nullptr));
141     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5  (52.5,6.5))"), nullptr));
142     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5 -6.5),(52.5,6.5))"), nullptr));
143     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5),(52.5 6.5))"), nullptr));
144     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("( -52.5,-6.5),(52.5,6.5))"), nullptr));
145     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5),(52.5,6.5)a"), nullptr));
146     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,),(52.5,6.5))"),     nullptr));
147 }
148 
testNormalizeBounds_data()149 void TestPrimitives::testNormalizeBounds_data()
150 {
151     QTest::addColumn<GeoCoordinates::Pair>("bounds");
152     QTest::addColumn<QList<GeoCoordinates::Pair> >("nbounds");
153 
154     // these ones should not be split:
155     QTest::newRow("top-left")
156         << GeoCoordinates::makePair(10, 20, 12, 22)
157         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(10, 20, 12, 22) );
158 
159     QTest::newRow("bottom-left")
160         << GeoCoordinates::makePair(-12, 20, -10, 22)
161         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(-12, 20, -10, 22) );
162 
163     QTest::newRow("top-right")
164         << GeoCoordinates::makePair(10, -22, 12, -20)
165         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(10, -22, 12, -20) );
166 
167     QTest::newRow("bottom-right")
168         << GeoCoordinates::makePair(-12, -22, -10, -20)
169         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(-12, -22, -10, -20) );
170 
171     QTest::newRow("cross_origin")
172         << GeoCoordinates::makePair(-12, -22, 10, 20)
173         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(-12, -22, 10, 20) );
174 
175     // these ones should be split:
176     QTest::newRow("cross_date_1")
177         << GeoCoordinates::makePair(10, 20, 15, -170)
178         << ( GeoCoordinates::PairList()
179             << GeoCoordinates::makePair(10, -180, 15, -170)
180             << GeoCoordinates::makePair(10, 20, 15, 180)
181         );
182 
183     QTest::newRow("cross_date_2")
184         << GeoCoordinates::makePair(-10, 20, 15, -170)
185         << ( GeoCoordinates::PairList()
186                 << GeoCoordinates::makePair(-10, -180, 15, -170)
187                 << GeoCoordinates::makePair(-10, 20, 15, 180)
188         );
189 }
190 
testNormalizeBounds()191 void TestPrimitives::testNormalizeBounds()
192 {
193     QFETCH(GeoCoordinates::Pair, bounds);
194 
195     QTEST(GeoIfaceHelperNormalizeBounds(bounds), "nbounds");
196 }
197 
testGroupStateComputer()198 void TestPrimitives::testGroupStateComputer()
199 {
200     {
201         // test selected state:
202         GroupStateComputer c1;
203         QCOMPARE(c1.getState(), SelectedNone);
204         c1.addSelectedState(SelectedNone);
205         QCOMPARE(c1.getState(), SelectedNone);
206         c1.addSelectedState(SelectedSome);
207         QCOMPARE(c1.getState(), SelectedSome);
208         c1.addSelectedState(SelectedAll);
209         QCOMPARE(c1.getState(), SelectedSome);
210         c1.clear();
211         QCOMPARE(c1.getState(), SelectedNone);
212         c1.addSelectedState(SelectedAll);
213         QCOMPARE(c1.getState(), SelectedAll);
214         c1.addSelectedState(SelectedSome);
215         QCOMPARE(c1.getState(), SelectedSome);
216         c1.clear();
217         QCOMPARE(c1.getState(), SelectedNone);
218         c1.addSelectedState(SelectedAll);
219         QCOMPARE(c1.getState(), SelectedAll);
220         c1.addSelectedState(SelectedNone);
221         QCOMPARE(c1.getState(), SelectedSome);
222     }
223 
224     {
225         // test selected state:
226         GroupStateComputer c1;
227         QCOMPARE(c1.getState(), FilteredPositiveNone);
228         c1.addFilteredPositiveState(FilteredPositiveNone);
229         QCOMPARE(c1.getState(), FilteredPositiveNone);
230         c1.addFilteredPositiveState(FilteredPositiveSome);
231         QCOMPARE(c1.getState(), FilteredPositiveSome);
232         c1.addFilteredPositiveState(FilteredPositiveAll);
233         QCOMPARE(c1.getState(), FilteredPositiveSome);
234         c1.clear();
235         QCOMPARE(c1.getState(), FilteredPositiveNone);
236         c1.addFilteredPositiveState(FilteredPositiveAll);
237         QCOMPARE(c1.getState(), FilteredPositiveAll);
238         c1.addFilteredPositiveState(FilteredPositiveSome);
239         QCOMPARE(c1.getState(), FilteredPositiveSome);
240         c1.clear();
241         QCOMPARE(c1.getState(), FilteredPositiveNone);
242         c1.addFilteredPositiveState(FilteredPositiveAll);
243         QCOMPARE(c1.getState(), FilteredPositiveAll);
244         c1.addFilteredPositiveState(FilteredPositiveNone);
245         QCOMPARE(c1.getState(), FilteredPositiveSome);
246     }
247 
248     {
249         // test selected state:
250         GroupStateComputer c1;
251         QCOMPARE(c1.getState(), RegionSelectedNone);
252         c1.addRegionSelectedState(RegionSelectedNone);
253         QCOMPARE(c1.getState(), RegionSelectedNone);
254         c1.addRegionSelectedState(RegionSelectedSome);
255         QCOMPARE(c1.getState(), RegionSelectedSome);
256         c1.addRegionSelectedState(RegionSelectedAll);
257         QCOMPARE(c1.getState(), RegionSelectedSome);
258         c1.clear();
259         QCOMPARE(c1.getState(), RegionSelectedNone);
260         c1.addRegionSelectedState(RegionSelectedAll);
261         QCOMPARE(c1.getState(), RegionSelectedAll);
262         c1.addRegionSelectedState(RegionSelectedSome);
263         QCOMPARE(c1.getState(), RegionSelectedSome);
264         c1.clear();
265         QCOMPARE(c1.getState(), RegionSelectedNone);
266         c1.addRegionSelectedState(RegionSelectedAll);
267         QCOMPARE(c1.getState(), RegionSelectedAll);
268         c1.addRegionSelectedState(RegionSelectedNone);
269         QCOMPARE(c1.getState(), RegionSelectedSome);
270     }
271 
272     /// @todo Test addState
273 }
274 
275 QTEST_GUILESS_MAIN(TestPrimitives)
276