File indexing completed on 2024-05-05 04:20:55

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2022 Silas Henrique <silash35@gmail.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "kdtree.h"
0008 #include <QTest>
0009 #include <QVariantMap>
0010 
0011 class KdTreeTest : public QObject
0012 {
0013     Q_OBJECT
0014 
0015 private slots:
0016     void testSimple();
0017     void testSearch();
0018 };
0019 
0020 void KdTreeTest::testSimple()
0021 {
0022     KdTree tree;
0023 
0024     QCOMPARE(tree.isEmpty(), true);
0025     QVariantMap sanFracisco = QVariantMap();
0026     sanFracisco.insert("name", "San Francisco, California, USA");
0027     tree.insert(-12.9442526, -38.4938602, sanFracisco);
0028     QCOMPARE(tree.isEmpty(), false);
0029     tree.clear();
0030     QCOMPARE(tree.isEmpty(), true);
0031 }
0032 
0033 void KdTreeTest::testSearch()
0034 {
0035     KdTree tree;
0036 
0037     QVariantMap washington = QVariantMap();
0038     washington.insert("name", "Washington, District of Columbia, USA");
0039     tree.insert(38.893938, -77.1546608, washington);
0040 
0041     QVariantMap brasilia = QVariantMap();
0042     brasilia.insert("name", "Brasília, Federal District, Brazil");
0043     tree.insert(-15.721387, -48.0774441, brasilia);
0044 
0045     QVariantMap canberra = QVariantMap();
0046     canberra.insert("name", "Canberra, Australian Capital Territory, Australia");
0047     tree.insert(-35.3136188, 148.9896982, canberra);
0048 
0049     QVariantMap berlin = QVariantMap();
0050     berlin.insert("name", "Berlin, Germany");
0051     tree.insert(52.5069312, 13.1445517, berlin);
0052 
0053     KdNode *res = tree.findNearest(51.5287718, -0.2416818); // London, United Kingdom coordinates
0054     QCOMPARE(res->data.value("name"), berlin.value("name")); // Berlin is the nearest neighbour of London
0055 
0056     // Numbers should not lose precision (Apart from the normal loss with floats)
0057     // This prevents someone from using int variables to store the positions
0058     QCOMPARE(res->point.x(), 52.5069312);
0059     QCOMPARE(res->point.y(), 13.1445517);
0060 }
0061 
0062 QTEST_MAIN(KdTreeTest)
0063 
0064 #include "kdtreetest.moc"