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>
bb10
Ivan Solovev 2024-03-22 10:50:12 +01:00
parent d999e64692
commit fa0d77e290
3 changed files with 49 additions and 1 deletions

View File

@ -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)

View File

@ -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()); }

View File

@ -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");