File indexing completed on 2025-01-19 03:57:45

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-02-07
0007  * Description : test for the simple datatypes and helper functions
0008  *
0009  * SPDX-FileCopyrightText: 2010-2013 by Michael G. Hansen <mike at mghansen dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "primitives_utest.h"
0016 
0017 // local includes
0018 
0019 #include "geoifacetypes.h"
0020 #include "geoifacecommon.h"
0021 
0022 using namespace Digikam;
0023 
0024 void TestPrimitives::testNoOp()
0025 {
0026 }
0027 
0028 void TestPrimitives::testParseLatLonString()
0029 {
0030     // make sure there is no crash on null-pointer
0031     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("52,6"), nullptr));
0032 
0033     GeoCoordinates coordinate;
0034 
0035     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("52,6"), &coordinate));
0036     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:52,6"));
0037 
0038     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("52.5,6.5"), &coordinate));
0039     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:52.5,6.5"));
0040 
0041     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String(" 52.5, 6.5 "), &coordinate));
0042     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:52.5,6.5"));
0043 
0044     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("-52.5, 6.5 "), &coordinate));
0045     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:-52.5,6.5"));
0046 
0047     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("    -52.5,  6.5   "), &coordinate));
0048     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:-52.5,6.5"));
0049 
0050     QVERIFY(GeoIfaceHelperParseLatLonString(QLatin1String("52.5,-6.5"), &coordinate));
0051     QCOMPARE(coordinate.geoUrl(), QLatin1String("geo:52.5,-6.5"));
0052 
0053     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String(""), nullptr));
0054     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("52.6"), nullptr));
0055     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("52.6,"), nullptr));
0056     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String(",6"), nullptr));
0057     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("a52,6"), nullptr));
0058     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("52,a"), nullptr));
0059     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("52,6a"), nullptr));
0060     QVERIFY(!GeoIfaceHelperParseLatLonString(QLatin1String("(52,6)"), nullptr));
0061 }
0062 
0063 void TestPrimitives::testParseXYStringToPoint()
0064 {
0065     // make sure there is no crash on null-pointer
0066     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("(52,6)"), nullptr));
0067 
0068     QPoint point;
0069 
0070     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("(52,6)"), &point));
0071     QCOMPARE(point, QPoint(52,6));
0072 
0073     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("(10,20)"), &point));
0074     QCOMPARE(point, QPoint(10,20));
0075 
0076     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String(" ( 52, 6 ) "), &point));
0077     QCOMPARE(point, QPoint(52,6));
0078 
0079     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("  ( 52, 6 )  "), &point));
0080     QCOMPARE(point, QPoint(52,6));
0081 
0082     // We used to expect integer string results, but floats are also possible.
0083     // BKO 270624
0084     // GeoIfaceHelperParseXYStringToPoint always rounds them to 0.
0085     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("  ( 52.5, 6.5 )  "), &point));
0086     QCOMPARE(point, QPoint(52,6));
0087     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QLatin1String("  ( -52.5, 6.5 )  "), &point));
0088     QCOMPARE(point, QPoint(-52,6));
0089 
0090     QVERIFY(GeoIfaceHelperParseXYStringToPoint(QString::fromLatin1("(204.94641003022224, 68.00444002512285)"), &point));
0091 
0092     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String(""), nullptr));
0093     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("()"), nullptr));
0094     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(52)"), nullptr));
0095     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(52,6a)"), nullptr));
0096     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(a52,6)"), nullptr));
0097     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("52,6"), nullptr));
0098     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(,6)"), nullptr));
0099     QVERIFY(!GeoIfaceHelperParseXYStringToPoint(QLatin1String("(6,)"), nullptr));
0100 }
0101 
0102 void TestPrimitives::testParseBoundsString()
0103 {
0104     // make sure there is no crash on null-pointer
0105     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52,-6),(52,6))"), nullptr));
0106 
0107     GeoCoordinates::Pair bounds;
0108 
0109     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52,-6),(52,6))"), &bounds));
0110     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:-52,-6"));
0111     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:52,6"));
0112 
0113     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52,-6), (52,6))"), &bounds));
0114     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:-52,-6"));
0115     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:52,6"));
0116 
0117     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52, -6), (52, 6))"), &bounds));
0118     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:-52,-6"));
0119     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:52,6"));
0120 
0121     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((10,20),(30,40))"), &bounds));
0122     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:10,20"));
0123     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:30,40"));
0124 
0125     QVERIFY(GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5),(52.5,6.5))"), &bounds));
0126     QCOMPARE(bounds.first.geoUrl(), QLatin1String("geo:-52.5,-6.5"));
0127     QCOMPARE(bounds.second.geoUrl(), QLatin1String("geo:52.5,6.5"));
0128 
0129     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String(" (-52.5,-6.5),(52.5,6.5))"), nullptr));
0130     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5),(52.5,6.5) "), nullptr));
0131     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5), 52.5,6.5))"), nullptr));
0132     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5  (52.5,6.5))"), nullptr));
0133     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5 -6.5),(52.5,6.5))"), nullptr));
0134     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5),(52.5 6.5))"), nullptr));
0135     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("( -52.5,-6.5),(52.5,6.5))"), nullptr));
0136     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,-6.5),(52.5,6.5)a"), nullptr));
0137     QVERIFY(!GeoIfaceHelperParseBoundsString(QLatin1String("((-52.5,),(52.5,6.5))"),     nullptr));
0138 }
0139 
0140 void TestPrimitives::testNormalizeBounds_data()
0141 {
0142     QTest::addColumn<GeoCoordinates::Pair>("bounds");
0143     QTest::addColumn<QList<GeoCoordinates::Pair> >("nbounds");
0144 
0145     // these ones should not be split:
0146     QTest::newRow("top-left")
0147         << GeoCoordinates::makePair(10, 20, 12, 22)
0148         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(10, 20, 12, 22) );
0149 
0150     QTest::newRow("bottom-left")
0151         << GeoCoordinates::makePair(-12, 20, -10, 22)
0152         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(-12, 20, -10, 22) );
0153 
0154     QTest::newRow("top-right")
0155         << GeoCoordinates::makePair(10, -22, 12, -20)
0156         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(10, -22, 12, -20) );
0157 
0158     QTest::newRow("bottom-right")
0159         << GeoCoordinates::makePair(-12, -22, -10, -20)
0160         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(-12, -22, -10, -20) );
0161 
0162     QTest::newRow("cross_origin")
0163         << GeoCoordinates::makePair(-12, -22, 10, 20)
0164         << ( GeoCoordinates::PairList() << GeoCoordinates::makePair(-12, -22, 10, 20) );
0165 
0166     // these ones should be split:
0167     QTest::newRow("cross_date_1")
0168         << GeoCoordinates::makePair(10, 20, 15, -170)
0169         << ( GeoCoordinates::PairList()
0170             << GeoCoordinates::makePair(10, -180, 15, -170)
0171             << GeoCoordinates::makePair(10, 20, 15, 180)
0172         );
0173 
0174     QTest::newRow("cross_date_2")
0175         << GeoCoordinates::makePair(-10, 20, 15, -170)
0176         << ( GeoCoordinates::PairList()
0177                 << GeoCoordinates::makePair(-10, -180, 15, -170)
0178                 << GeoCoordinates::makePair(-10, 20, 15, 180)
0179         );
0180 }
0181 
0182 void TestPrimitives::testNormalizeBounds()
0183 {
0184     QFETCH(GeoCoordinates::Pair, bounds);
0185 
0186     QTEST(GeoIfaceHelperNormalizeBounds(bounds), "nbounds");
0187 }
0188 
0189 void TestPrimitives::testGroupStateComputer()
0190 {
0191     {
0192         // test selected state:
0193         GroupStateComputer c1;
0194         QCOMPARE(c1.getState(), SelectedNone);
0195         c1.addSelectedState(SelectedNone);
0196         QCOMPARE(c1.getState(), SelectedNone);
0197         c1.addSelectedState(SelectedSome);
0198         QCOMPARE(c1.getState(), SelectedSome);
0199         c1.addSelectedState(SelectedAll);
0200         QCOMPARE(c1.getState(), SelectedSome);
0201         c1.clear();
0202         QCOMPARE(c1.getState(), SelectedNone);
0203         c1.addSelectedState(SelectedAll);
0204         QCOMPARE(c1.getState(), SelectedAll);
0205         c1.addSelectedState(SelectedSome);
0206         QCOMPARE(c1.getState(), SelectedSome);
0207         c1.clear();
0208         QCOMPARE(c1.getState(), SelectedNone);
0209         c1.addSelectedState(SelectedAll);
0210         QCOMPARE(c1.getState(), SelectedAll);
0211         c1.addSelectedState(SelectedNone);
0212         QCOMPARE(c1.getState(), SelectedSome);
0213     }
0214 
0215     {
0216         // test selected state:
0217         GroupStateComputer c1;
0218         QCOMPARE(c1.getState(), FilteredPositiveNone);
0219         c1.addFilteredPositiveState(FilteredPositiveNone);
0220         QCOMPARE(c1.getState(), FilteredPositiveNone);
0221         c1.addFilteredPositiveState(FilteredPositiveSome);
0222         QCOMPARE(c1.getState(), FilteredPositiveSome);
0223         c1.addFilteredPositiveState(FilteredPositiveAll);
0224         QCOMPARE(c1.getState(), FilteredPositiveSome);
0225         c1.clear();
0226         QCOMPARE(c1.getState(), FilteredPositiveNone);
0227         c1.addFilteredPositiveState(FilteredPositiveAll);
0228         QCOMPARE(c1.getState(), FilteredPositiveAll);
0229         c1.addFilteredPositiveState(FilteredPositiveSome);
0230         QCOMPARE(c1.getState(), FilteredPositiveSome);
0231         c1.clear();
0232         QCOMPARE(c1.getState(), FilteredPositiveNone);
0233         c1.addFilteredPositiveState(FilteredPositiveAll);
0234         QCOMPARE(c1.getState(), FilteredPositiveAll);
0235         c1.addFilteredPositiveState(FilteredPositiveNone);
0236         QCOMPARE(c1.getState(), FilteredPositiveSome);
0237     }
0238 
0239     {
0240         // test selected state:
0241         GroupStateComputer c1;
0242         QCOMPARE(c1.getState(), RegionSelectedNone);
0243         c1.addRegionSelectedState(RegionSelectedNone);
0244         QCOMPARE(c1.getState(), RegionSelectedNone);
0245         c1.addRegionSelectedState(RegionSelectedSome);
0246         QCOMPARE(c1.getState(), RegionSelectedSome);
0247         c1.addRegionSelectedState(RegionSelectedAll);
0248         QCOMPARE(c1.getState(), RegionSelectedSome);
0249         c1.clear();
0250         QCOMPARE(c1.getState(), RegionSelectedNone);
0251         c1.addRegionSelectedState(RegionSelectedAll);
0252         QCOMPARE(c1.getState(), RegionSelectedAll);
0253         c1.addRegionSelectedState(RegionSelectedSome);
0254         QCOMPARE(c1.getState(), RegionSelectedSome);
0255         c1.clear();
0256         QCOMPARE(c1.getState(), RegionSelectedNone);
0257         c1.addRegionSelectedState(RegionSelectedAll);
0258         QCOMPARE(c1.getState(), RegionSelectedAll);
0259         c1.addRegionSelectedState(RegionSelectedNone);
0260         QCOMPARE(c1.getState(), RegionSelectedSome);
0261     }
0262 
0263     /// @todo Test addState
0264 }
0265 
0266 QTEST_GUILESS_MAIN(TestPrimitives)
0267 
0268 #include "moc_primitives_utest.cpp"