QSizeF: add qFuzzyCompare and qFuzzyIsNull overloads

[ChangeLog][QtCore][QSizeF] Added qFuzzyCompare and qFuzzyIsNull
overloads for QSizeF

Drop the custom qFuzzyCompare() implementation from
tst_qgraphicsgridlayout.cpp

Task-number: QTBUG-120308
Change-Id: I32d11c1c81d0503914a8168b5e960a8c6ce943d9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ivan Solovev 2024-04-17 17:57:35 +02:00
parent c2826805de
commit 3ca9877f8c
4 changed files with 72 additions and 6 deletions

View File

@ -808,7 +808,24 @@ QSizeF QSizeF::scaled(const QSizeF &s, Qt::AspectRatioMode mode) const noexcept
\sa expandedTo(), scale()
*/
/*!
\fn bool QSizeF::qFuzzyCompare(const QSizeF &lhs, const QSizeF &rhs)
\since 6.8
Returns \c true if the size \a lhs is approximately equal to the
size \a rhs; otherwise returns \c false.
The sizes are considered approximately equal if their width and
height are approximately equal.
*/
/*!
\fn bool QSizeF::qFuzzyIsNull(const QSizeF &size)
\since 6.8
Returns \c true if both width and height of the size \a size
are approximately equal to zero.
*/
/*****************************************************************************
QSizeF stream functions

View File

@ -246,12 +246,18 @@ public:
private:
QT_WARNING_PUSH
QT_WARNING_DISABLE_FLOAT_COMPARE
friend constexpr bool comparesEqual(const QSizeF &s1, const QSizeF &s2) noexcept
friend constexpr bool qFuzzyCompare(const QSizeF &s1, const QSizeF &s2) noexcept
{
// Cannot use qFuzzyCompare(), because it will give incorrect results
// if one of the arguments is 0.0.
return ((!s1.wd || !s2.wd) ? qFuzzyIsNull(s1.wd - s2.wd) : qFuzzyCompare(s1.wd, s2.wd))
&& ((!s1.ht || !s2.ht) ? qFuzzyIsNull(s1.ht - s2.ht) : qFuzzyCompare(s1.ht, s2.ht));
}
QT_WARNING_POP
friend constexpr bool qFuzzyIsNull(const QSizeF &size) noexcept
{ return qFuzzyIsNull(size.wd) && qFuzzyIsNull(size.ht); }
friend constexpr bool comparesEqual(const QSizeF &lhs, const QSizeF &rhs) noexcept
{ return qFuzzyCompare(lhs, rhs); }
Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QSizeF)
friend constexpr bool comparesEqual(const QSizeF &lhs, const QSize &rhs) noexcept
{ return comparesEqual(lhs, rhs.toSizeF()); }

View File

@ -39,9 +39,15 @@ private slots:
void compare_data();
void compare();
void fuzzyCompare_data();
void fuzzyCompare();
void isNull_data();
void isNull();
void fuzzyIsNull_data();
void fuzzyIsNull();
void scale();
void expandedTo();
@ -100,6 +106,20 @@ void tst_QSizeF::compare()
QT_TEST_EQUALITY_OPS(lhs, rhsFixed, mixedResult);
}
void tst_QSizeF::fuzzyCompare_data()
{
compare_data();
}
void tst_QSizeF::fuzzyCompare()
{
QFETCH(QSizeF, lhs);
QFETCH(QSizeF, rhs);
QFETCH(bool, result);
QCOMPARE_EQ(qFuzzyCompare(lhs, rhs), result);
}
void tst_QSizeF::isNull_data()
{
QTest::addColumn<qreal>("width");
@ -114,6 +134,7 @@ void tst_QSizeF::isNull_data()
QTest::newRow("0, -0.1") << qreal(0) << qreal(-0.1) << false;
QTest::newRow("0.1, 0") << qreal(0.1) << qreal(0) << false;
QTest::newRow("0, 0.1") << qreal(0) << qreal(0.1) << false;
QTest::newRow("qreal_min, -qreal_min") << qreal_min << -qreal_min << false;
}
void tst_QSizeF::isNull()
@ -128,6 +149,33 @@ void tst_QSizeF::isNull()
QCOMPARE(size.isNull(), isNull);
}
void tst_QSizeF::fuzzyIsNull_data()
{
QTest::addColumn<qreal>("width");
QTest::addColumn<qreal>("height");
QTest::addColumn<bool>("fuzzyNull");
QTest::newRow("0, 0") << qreal(0.0) << qreal(0.0) << true;
QTest::newRow("-0, -0") << qreal(-0.0) << qreal(-0.0) << true;
QTest::newRow("0, -0") << qreal(0) << qreal(-0.0) << true;
QTest::newRow("-0, 0") << qreal(-0.0) << qreal(0) << true;
QTest::newRow("-0.1, 0") << qreal(-0.1) << qreal(0) << false;
QTest::newRow("0, -0.1") << qreal(0) << qreal(-0.1) << false;
QTest::newRow("0.1, 0") << qreal(0.1) << qreal(0) << false;
QTest::newRow("0, 0.1") << qreal(0) << qreal(0.1) << false;
QTest::newRow("qreal_min, -qreal_min") << qreal_min << -qreal_min << true;
}
void tst_QSizeF::fuzzyIsNull()
{
QFETCH(qreal, width);
QFETCH(qreal, height);
QFETCH(bool, fuzzyNull);
QSizeF size(width, height);
QCOMPARE(qFuzzyIsNull(size), fuzzyNull);
}
void tst_QSizeF::scale() {
QSizeF t1(10.4, 12.8);
t1.scale(60.6, 60.6, Qt::IgnoreAspectRatio);

View File

@ -2976,11 +2976,6 @@ static QSizeF wfh(Qt::SizeHint /*which*/, const QSizeF &constraint)
return result;
}
bool qFuzzyCompare(const QSizeF &a, const QSizeF &b)
{
return qFuzzyCompare(a.width(), b.width()) && qFuzzyCompare(a.height(), b.height());
}
void tst_QGraphicsGridLayout::heightForWidth()
{
QGraphicsWidget *widget = new QGraphicsWidget;