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        : 2011-01-12
0007  * Description : Test the TileIndex class
0008  *
0009  * SPDX-FileCopyrightText: 2011 by Michael G. Hansen <mike at mghansen dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "tileindex_utest.h"
0016 
0017 // C++ includes
0018 
0019 #include <cmath>
0020 
0021 // local includes
0022 
0023 #include "tileindex.h"
0024 
0025 using namespace Digikam;
0026 
0027 void TestTileIndex::testNoOp()
0028 {
0029 }
0030 
0031 void TestTileIndex::testBasics()
0032 {
0033     {
0034         // an empty index:
0035         TileIndex index1;
0036         QCOMPARE(index1.indexCount(), 0);
0037         QCOMPARE(index1.level(), 0);
0038         QVERIFY(index1.toIntList().isEmpty());
0039 
0040         TileIndex index2 = TileIndex::fromIntList(QIntList());
0041         QCOMPARE(index2.indexCount(), 0);
0042         QCOMPARE(index2.level(), 0);
0043         QVERIFY(index2.toIntList().isEmpty());
0044     }
0045 
0046     {
0047         // appending indexes:
0048         TileIndex index1;
0049         index1.appendLinearIndex(1);
0050         QCOMPARE(index1.indexCount(), 1);
0051         QCOMPARE(index1.level(), 0);
0052         QCOMPARE(index1.at(0), 1);
0053         QCOMPARE(index1.lastIndex(), 1);
0054     }
0055 }
0056 
0057 void TestTileIndex::testResizing()
0058 {
0059     TileIndex i1 = TileIndex::fromIntList(QIntList()<<1<<2<<3<<4);
0060     QCOMPARE(i1.indexCount(), 4);
0061 
0062     TileIndex i2 = i1.mid(1, 2);
0063     QCOMPARE(i2.indexCount(), 2);
0064     QCOMPARE(i2.at(0), 2);
0065     QCOMPARE(i2.at(1), 3);
0066 
0067     TileIndex i3 = i1;
0068     QCOMPARE(i3.indexCount(), 4);
0069     i3.oneUp();
0070     QCOMPARE(i3.indexCount(), 3);
0071     i3.oneUp();
0072     QCOMPARE(i3.indexCount(), 2);
0073 }
0074 
0075 void TestTileIndex::testIntListInteraction()
0076 {
0077     {
0078         for (int l = 0; l <= TileIndex::MaxLevel; ++l)
0079         {
0080             QIntList myList;
0081             TileIndex i1;
0082 
0083             for (int i = 0; i <= l; ++i)
0084             {
0085                 i1.appendLinearIndex(i);
0086                 myList << i;
0087             }
0088 
0089             const TileIndex i2 = TileIndex::fromIntList(myList);
0090             QVERIFY(TileIndex::indicesEqual(i1, i2, l));
0091 
0092             const QIntList il2 = i1.toIntList();
0093             QCOMPARE(myList, il2);
0094         }
0095     }
0096 }
0097 
0098 void TestTileIndex::testFromCoordinates()
0099 {
0100     auto comp = [] (std::initializer_list<int> list, double lat, double lon, int level)
0101     {
0102         auto tileIndex = TileIndex::fromCoordinates(GeoCoordinates(lat, lon), level);
0103         QCOMPARE(tileIndex.toIntList(), QIntList{list});
0104         // should this work?
0105         // QCOMPARE(TileIndex::fromCoordinates(tileIndex.toCoordinates(), level).toIntList(), QIntList{list});
0106     };
0107 
0108     QList<double> latSizes;
0109     QList<double> lonSizes;
0110     double latOne = -90;
0111     double lonOne = -180;
0112     double lat123 = -90;
0113     double lon321 = -180;
0114     for (int i = 0; i <= TileIndex::MaxLevel; ++i) {
0115         latSizes.push_back(180. * std::pow(10, -i-1));
0116         lonSizes.push_back(360. * std::pow(10, -i-1));
0117         // construct coordinate at always the second tile on level i
0118         latOne += latSizes.back();
0119         lonOne += lonSizes.back();
0120         // construct coordinate at always the i-th tile in latitude and (9-i)-th tile in longitude at level i
0121         lat123 += i * latSizes.back();
0122         lon321 += (9-i) * lonSizes.back();
0123     }
0124 
0125     comp({0,0,0,0,0,0,0,0,0,0}, -90,-180, TileIndex::MaxLevel);
0126     comp({99,99,99,99,99,99,99,99,99,99}, 90,180, TileIndex::MaxLevel);
0127     comp({55,0,0,0,0,0,0,0,0,0}, 0,0, TileIndex::MaxLevel);
0128     comp({11,11,11,11,11,11,11,11,11,11}, latOne, lonOne, TileIndex::MaxLevel);
0129     comp({55,0,0,0,0,73,0,0,0,0}, latSizes[5] * 7,lonSizes[5] * 3, TileIndex::MaxLevel);
0130     comp({55,0,0,0,0,3,0,0,90,0}, latSizes[8] * 9,lonSizes[5] * 3, TileIndex::MaxLevel);
0131     comp({81,0,0,0,0,3,0,0,90,0}, latSizes[0] * 3 + latSizes[8] * 9, lonSizes[0] * -4 + lonSizes[5] * 3, TileIndex::MaxLevel);
0132     comp({9,18,27,36,45,54,63,72,81,90}, lat123, lon321, TileIndex::MaxLevel);
0133     comp({9,18,27,36,45,54}, lat123, lon321, 5);
0134  }
0135 
0136 void TestTileIndex::testToCoordinates()
0137 {
0138     auto comp = [] (std::initializer_list<int> list, double lat, double lon)
0139     {
0140         auto coordinates = TileIndex::fromIntList(list).toCoordinates();
0141         QCOMPARE(coordinates.lat(), lat);
0142         QCOMPARE(coordinates.lon(), lon);
0143     };
0144 
0145     comp({55,0,0,0,0}, 0, 0);
0146     comp({55,0,0,14,0}, std::pow(TileIndex::Tiling, -4.0) * 180.0, 4.0 * std::pow(TileIndex::Tiling, -4.0) * 360.0);
0147     comp({55,0,0,0,0,0,0,0,0,99}, 9.0 * std::pow(TileIndex::Tiling, -10.0) * 180.0, 9.0 * std::pow(TileIndex::Tiling, -10.0) * 360.0);
0148     comp({99,99,99,99,99,99,99,99,99,99}, 90.0 - std::pow(TileIndex::Tiling, -10.0) * 180.0, 180.0 - std::pow(TileIndex::Tiling, -10.0) * 360.0);
0149 }
0150 
0151 void TestTileIndex::testToCoordinatesCorners()
0152 {
0153     auto comp = [] (std::initializer_list<int> list)
0154     {
0155         auto tileIndex = TileIndex::fromIntList(list);
0156         auto coordinates = tileIndex.toCoordinates();
0157         double latHeight = std::pow(0.1, list.size()) * 180.0;
0158         double lonWidth = std::pow(0.1, list.size()) * 360.0;
0159         QCOMPARE(tileIndex.toCoordinates(TileIndex::CornerSW).lat(), coordinates.lat());
0160         QCOMPARE(tileIndex.toCoordinates(TileIndex::CornerSW).lon(), coordinates.lon());
0161         QCOMPARE(tileIndex.toCoordinates(TileIndex::CornerNW).lat(), coordinates.lat() + latHeight);
0162         QCOMPARE(tileIndex.toCoordinates(TileIndex::CornerNW).lon(), coordinates.lon());
0163         QCOMPARE(tileIndex.toCoordinates(TileIndex::CornerNE).lat(), coordinates.lat() + latHeight);
0164         QCOMPARE(tileIndex.toCoordinates(TileIndex::CornerNE).lon(), coordinates.lon() + lonWidth);
0165         QCOMPARE(tileIndex.toCoordinates(TileIndex::CornerSE).lat(), coordinates.lat());
0166         QCOMPARE(tileIndex.toCoordinates(TileIndex::CornerSE).lon(), coordinates.lon() + lonWidth);
0167     };
0168 
0169     comp({55,0,0,0,0});
0170     comp({55,0,0,0,0,0,0,0,0,99});
0171     comp({99,99,99,99,99,99,99,99,99,99});
0172     comp({0,0,0,0,0,0,0,0,0,0});
0173 }
0174 
0175 
0176 /**
0177  * TileIndex is declared as Q_MOVABLE_TYPE, and here we verify that it still works with QList.
0178  */
0179 void TestTileIndex::testMovable()
0180 {
0181     {
0182         TileIndex i1;
0183 
0184         for (int i = 0; i <= TileIndex::MaxLevel; ++i)
0185         {
0186             i1.appendLinearIndex(i);
0187         }
0188 
0189         TileIndex::List l1;
0190 
0191         for (int i = 0; i < 10; ++i)
0192         {
0193             l1 << i1;
0194         }
0195 
0196         TileIndex::List l2 = l1;
0197         l2[0]              = l1.at(0);
0198 
0199         for (int i = 0; i < l1.count(); ++i)
0200         {
0201             QVERIFY(TileIndex::indicesEqual(i1, l2.at(i), TileIndex::MaxLevel));
0202         }
0203     }
0204 
0205 //     QBENCHMARK
0206 //     {
0207 //         TileIndex i1;
0208 //
0209 //         for (int i = 0; i <= TileIndex::MaxLevel; ++i)
0210 //         {
0211 //             i1.appendLinearIndex(i);
0212 //         }
0213 //
0214 //         TileIndex::List l1;
0215 //
0216 //         for (int i = 0; i < 100000; ++i)
0217 //         {
0218 //             l1 << i1;
0219 //         }
0220 //
0221 // //         QBENCHMARK
0222 //         {
0223 //             for (int i = 0; i < 100; ++i)
0224 //             {
0225 //                 TileIndex::List l2 = l1;
0226 //                 l2[0]              = i1;
0227 //             }
0228 //         }
0229 //     }
0230 }
0231 
0232 QTEST_GUILESS_MAIN(TestTileIndex)
0233 
0234 #include "moc_tileindex_utest.cpp"