Commit 6b85b3ce authored by Volker Krause's avatar Volker Krause
Browse files

Add unit tests and benchmark for coverage checks

Now that this is externally accessible we can actually test it, unlike the
old code. The benchmark will become relevant when moving to the more
detailed coverage areas from the Transport API Repository.
parent f12840db
......@@ -7,6 +7,7 @@
#include <KPublicTransport/Backend>
#include <KPublicTransport/BackendModel>
#include <KPublicTransport/CoverageArea>
#include <KPublicTransport/Location>
#include <KPublicTransport/Manager>
#include <QAbstractItemModelTester>
......@@ -19,6 +20,8 @@ using namespace KPublicTransport;
class BackendTest : public QObject
{
Q_OBJECT
private:
Manager m_ptMgr; // for the benchmark
private Q_SLOTS:
void initTestCase()
......@@ -47,6 +50,49 @@ private Q_SLOTS:
}
}
void testCoverageHitDetection()
{
Manager mgr;
auto b = *std::find_if(mgr.backends().begin(), mgr.backends().end(), [](const auto &b) { return b.identifier() == QLatin1String("de_db"); });
QCOMPARE(b.identifier(), QLatin1String("de_db"));
auto c = b.coverageArea(CoverageArea::Realtime);
QVERIFY(!c.isEmpty());
Location l1;
l1.setCoordinate(52.5, 13.0);
QVERIFY(c.coversLocation(l1));
l1.setCoordinate(13.0, 52.5);
QVERIFY(!c.coversLocation(l1));
Location l2;
l2.setCountry(QStringLiteral("DE"));
QVERIFY(c.coversLocation(l2));
l2.setCountry(QStringLiteral("NZ"));
QVERIFY(!c.coversLocation(l2));
b = *std::find_if(mgr.backends().begin(), mgr.backends().end(), [](const auto &b) { return b.identifier() == QLatin1String("un_gbfs"); });
QCOMPARE(b.identifier(), QLatin1String("un_gbfs"));
c = b.coverageArea(CoverageArea::Realtime);
QVERIFY(!c.isEmpty());
QVERIFY(c.coversLocation(l1));
QVERIFY(c.coversLocation(l2));
}
void testCoverageBenchmark()
{
Location loc;
loc.setCoordinate(52.5, 13.0);
QBENCHMARK {
for (const auto &b : m_ptMgr.backends()) {
for (const auto type : { CoverageArea::Realtime, CoverageArea::Regular, CoverageArea::Any }) {
const auto c = b.coverageArea(type);
c.coversLocation(loc);
}
}
}
}
void testBackendModel()
{
BackendModel model;
......
......@@ -39,7 +39,7 @@ static QStringView countryCode(QStringView isoCode)
return isoCode.size() < 2 ? QStringView() : isoCode.left(2);
}
bool CoverageArea::coveresLocation(const Location &loc) const
bool CoverageArea::coversLocation(const Location &loc) const
{
if (loc.hasCoordinate() && !d->area.isEmpty()) {
return d->area.containsPoint({loc.longitude(), loc.latitude()}, Qt::WindingFill);
......@@ -48,6 +48,9 @@ bool CoverageArea::coveresLocation(const Location &loc) const
// TODO we could do a more precise check for ISO 3166-2 subdivision codes when available
if (loc.country().size() == 2 && !d->regions.empty()) {
if (d->regions.size() == 1 && d->regions.at(0) == QLatin1String("UN")) { // global coverage
return true;
}
return std::binary_search(d->regions.begin(), d->regions.end(), loc.country(), [](const auto &lhs, const auto &rhs) {
return countryCode(lhs) < countryCode(rhs);
});
......
......@@ -43,7 +43,7 @@ public:
bool isEmpty() const;
/** Checks whether @p loc is covered by this area. */
bool coveresLocation(const Location &loc) const;
bool coversLocation(const Location &loc) const;
/** Read a single coverage area information from a JSON object
* in Transport API Repository format.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment