diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2021-07-26 17:05:11 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2021-07-28 11:42:44 +0200 |
commit | 71bed5569c615a84fe91d3a55bb84fc8010c5d5a (patch) | |
tree | 981853ec196034e6cc89d5e68901126f1f81ea55 | |
parent | 91f5e5028e2f2f139c16223aa0616a492df9bf2d (diff) | |
download | qtlocation-71bed5569c615a84fe91d3a55bb84fc8010c5d5a.tar.gz |
QGeoLocation: add qHash overload
Task-number: QTBUG-95163
Pick-to: 6.2
Change-Id: I6b867a27095c26dbc803609b7d0da6d23cd46249
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r-- | src/positioning/qgeolocation.cpp | 19 | ||||
-rw-r--r-- | src/positioning/qgeolocation.h | 2 | ||||
-rw-r--r-- | tests/auto/qgeolocation/tst_qgeolocation.cpp | 53 | ||||
-rw-r--r-- | tests/auto/qgeolocation/tst_qgeolocation.h | 2 |
4 files changed, 75 insertions, 1 deletions
diff --git a/src/positioning/qgeolocation.cpp b/src/positioning/qgeolocation.cpp index 38de9d04..a7c86f1f 100644 --- a/src/positioning/qgeolocation.cpp +++ b/src/positioning/qgeolocation.cpp @@ -40,7 +40,7 @@ #include "qgeolocation.h" #include "qgeolocation_p.h" -QT_USE_NAMESPACE +QT_BEGIN_NAMESPACE QGeoLocationPrivate::QGeoLocationPrivate() : QSharedData() @@ -263,3 +263,20 @@ bool QGeoLocation::equals(const QGeoLocation &lhs, const QGeoLocation &rhs) { return (*(lhs.d.constData()) == *(rhs.d.constData())); } + +/*! + \relates QGeoLocation + + Returns the hash value for the \a location, using \a seed for the + calculation. + + \note The hash does not take extended attributes into account. This means + that two geo location objects that differ only in the extended attributes + will provide similar hashes. +*/ +size_t qHash(const QGeoLocation &location, size_t seed) noexcept +{ + return qHashMulti(seed, location.coordinate(), location.boundingShape(), location.address()); +} + +QT_END_NAMESPACE diff --git a/src/positioning/qgeolocation.h b/src/positioning/qgeolocation.h index 0c86be26..3fe20986 100644 --- a/src/positioning/qgeolocation.h +++ b/src/positioning/qgeolocation.h @@ -90,6 +90,8 @@ private: QSharedDataPointer<QGeoLocationPrivate> d; }; +Q_POSITIONING_EXPORT size_t qHash(const QGeoLocation &location, size_t seed = 0) noexcept; + Q_DECLARE_SHARED(QGeoLocation) QT_END_NAMESPACE diff --git a/tests/auto/qgeolocation/tst_qgeolocation.cpp b/tests/auto/qgeolocation/tst_qgeolocation.cpp index e2935b4c..553d32cd 100644 --- a/tests/auto/qgeolocation/tst_qgeolocation.cpp +++ b/tests/auto/qgeolocation/tst_qgeolocation.cpp @@ -355,5 +355,58 @@ void tst_QGeoLocation::isEmpty() QVERIFY(location.isEmpty()); } +void tst_QGeoLocation::hashing() +{ + QFETCH(QGeoLocation, leftLocation); + QFETCH(QGeoLocation, rightLocation); + QFETCH(bool, result); + + const size_t leftHash = qHash(leftLocation); + const size_t rightHash = qHash(rightLocation); + QCOMPARE(leftHash == rightHash, result); +} + +void tst_QGeoLocation::hashing_data() +{ + QTest::addColumn<QGeoLocation>("leftLocation"); + QTest::addColumn<QGeoLocation>("rightLocation"); + QTest::addColumn<bool>("result"); + + QTest::newRow("empty") << QGeoLocation() << QGeoLocation() << true; + + QGeoAddress address; + address.setCity("city"); + address.setPostalCode("1234"); + address.setDistrict("district"); + address.setStreet("street"); + address.setStreetNumber("12"); + + QGeoLocation leftLocation; + leftLocation.setAddress(address); + leftLocation.setCoordinate(QGeoCoordinate(1, 1)); + leftLocation.setBoundingShape(QGeoCircle(QGeoCoordinate(1, 1), 10)); + + // do not copy, so that they have different d_ptr's + QGeoLocation rightLocation; + rightLocation.setAddress(address); + rightLocation.setCoordinate(QGeoCoordinate(1, 1)); + rightLocation.setBoundingShape(QGeoCircle(QGeoCoordinate(1, 1), 10)); + + QTest::newRow("same locations") << leftLocation << rightLocation << true; + + QGeoLocation rightLocationCopy = rightLocation; + rightLocationCopy.setBoundingShape(QGeoRectangle(QGeoCoordinate(2, 0), QGeoCoordinate(0, 2))); + QTest::newRow("different shapes") << leftLocation << rightLocationCopy << false; + + rightLocationCopy = rightLocation; + rightLocationCopy.setCoordinate(QGeoCoordinate(2, 1)); + QTest::newRow("different coordinates") << leftLocation << rightLocationCopy << false; + + rightLocationCopy = rightLocation; + address.setState("state"); + rightLocationCopy.setAddress(address); + QTest::newRow("different addresses") << leftLocation << rightLocationCopy << false; +} + QTEST_APPLESS_MAIN(tst_QGeoLocation); diff --git a/tests/auto/qgeolocation/tst_qgeolocation.h b/tests/auto/qgeolocation/tst_qgeolocation.h index a603503f..28fae766 100644 --- a/tests/auto/qgeolocation/tst_qgeolocation.h +++ b/tests/auto/qgeolocation/tst_qgeolocation.h @@ -69,6 +69,8 @@ private Q_SLOTS: void comparison(); void comparison_data(); void isEmpty(); + void hashing(); + void hashing_data(); //End Unit Tests for qgeolocation.h private: |