diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-03-22 10:50:12 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-05-10 15:33:39 +0200 |
commit | fa0d77e290f5ccb5afa7d02716f8726aa6b810e6 (patch) | |
tree | a2c134cace3c0fa290f80edc73c470a0f49cda99 | |
parent | d999e646929889a0e25df2be4a097196c9ea3a49 (diff) |
Add qFuzzyCompare() and qFuzzyIsNull() overloads for QPointF
Use the new qFuzzyCompare() overload in op==(QPointF, QPointF).
[ChangeLog][QtCore][QPointF] Added qFuzzyCompare() and qFuzzyIsNull()
overloads for QPointF.
Task-number: QTBUG-120308
Change-Id: I522164acb65432bf55c58b55575f25535d27e27a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/tools/qpoint.cpp | 20 | ||||
-rw-r--r-- | src/corelib/tools/qpoint.h | 9 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qpointf/tst_qpointf.cpp | 21 |
3 files changed, 49 insertions, 1 deletions
diff --git a/src/corelib/tools/qpoint.cpp b/src/corelib/tools/qpoint.cpp index ce7514cddd..775a354469 100644 --- a/src/corelib/tools/qpoint.cpp +++ b/src/corelib/tools/qpoint.cpp @@ -762,6 +762,26 @@ size_t qHash(QPoint key, size_t seed) noexcept \sa qFuzzyCompare */ +/*! + \fn bool QPointF::qFuzzyCompare(const QPointF &p1, const QPointF &p2) + \since 6.8 + + Returns \c true if \a p1 is approximately equal to \a p2; otherwise + returns \c false. + + \sa qFuzzyIsNull +*/ + +/*! + \fn bool QPointF::qFuzzyIsNull(const QPointF &point) + \since 6.8 + + Returns \c true if \a point is approximately equal to a point + \c {(0.0, 0.0)}. + + \sa qFuzzyCompare +*/ + #ifndef QT_NO_DATASTREAM /*! \fn QDataStream &operator<<(QDataStream &stream, const QPointF &point) diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h index 2960033c6d..50b4c864be 100644 --- a/src/corelib/tools/qpoint.h +++ b/src/corelib/tools/qpoint.h @@ -6,6 +6,7 @@ #include <QtCore/qcompare.h> #include <QtCore/qnamespace.h> +#include <QtCore/qnumeric.h> #include <QtCore/q20type_traits.h> #include <QtCore/q23utility.h> @@ -246,12 +247,18 @@ public: private: QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE - friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept + friend constexpr bool qFuzzyCompare(const QPointF &p1, const QPointF &p2) noexcept { return ((!p1.xp || !p2.xp) ? qFuzzyIsNull(p1.xp - p2.xp) : qFuzzyCompare(p1.xp, p2.xp)) && ((!p1.yp || !p2.yp) ? qFuzzyIsNull(p1.yp - p2.yp) : qFuzzyCompare(p1.yp, p2.yp)); } QT_WARNING_POP + friend constexpr bool qFuzzyIsNull(const QPointF &point) noexcept + { + return qFuzzyIsNull(point.xp) && qFuzzyIsNull(point.yp); + } + friend constexpr bool comparesEqual(const QPointF &p1, const QPointF &p2) noexcept + { return qFuzzyCompare(p1, p2); } Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QPointF) friend constexpr bool comparesEqual(const QPointF &p1, const QPoint &p2) noexcept { return comparesEqual(p1, p2.toPointF()); } diff --git a/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp b/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp index e75dd9d5ab..ebbac4ec7c 100644 --- a/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp +++ b/tests/auto/corelib/tools/qpointf/tst_qpointf.cpp @@ -76,6 +76,9 @@ private slots: void operator_eq_data(); void operator_eq(); + void fuzzyCompare_data(); + void fuzzyCompare(); + void toPoint_data(); void toPoint(); @@ -103,15 +106,19 @@ void tst_QPointF::isNull() { QPointF point(0, 0); QVERIFY(point.isNull()); + QVERIFY(qFuzzyIsNull(point)); ++point.rx(); QVERIFY(!point.isNull()); + QVERIFY(!qFuzzyIsNull(point)); point.rx() -= 2; QVERIFY(!point.isNull()); + QVERIFY(!qFuzzyIsNull(point)); QPointF nullNegativeZero(qreal(-0.0), qreal(-0.0)); QCOMPARE(nullNegativeZero.x(), (qreal)-0.0f); QCOMPARE(nullNegativeZero.y(), (qreal)-0.0f); QVERIFY(nullNegativeZero.isNull()); + QVERIFY(qFuzzyIsNull(nullNegativeZero)); } void tst_QPointF::manhattanLength_data() @@ -385,6 +392,20 @@ void tst_QPointF::operator_eq() QT_TEST_EQUALITY_OPS(point1, intPoint2, expectIntEqual); } +void tst_QPointF::fuzzyCompare_data() +{ + operator_eq_data(); +} + +void tst_QPointF::fuzzyCompare() +{ + QFETCH(QPointF, point1); + QFETCH(QPointF, point2); + QFETCH(bool, expectEqual); + + QCOMPARE_EQ(qFuzzyCompare(point1, point2), expectEqual); +} + void tst_QPointF::toPoint_data() { QTest::addColumn<QPointF>("pointf"); |