diff options
Diffstat (limited to 'tests/auto/corelib/tools/qrect/tst_qrect.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qrect/tst_qrect.cpp | 190 |
1 files changed, 141 insertions, 49 deletions
diff --git a/tests/auto/corelib/tools/qrect/tst_qrect.cpp b/tests/auto/corelib/tools/qrect/tst_qrect.cpp index 4057eb67fb..c7c8b3a560 100644 --- a/tests/auto/corelib/tools/qrect/tst_qrect.cpp +++ b/tests/auto/corelib/tools/qrect/tst_qrect.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <qrect.h> @@ -32,6 +7,9 @@ #include <limits.h> #include <qdebug.h> +#include <private/qcomparisontesthelper_p.h> + +#include <array> class tst_QRect : public QObject { @@ -57,8 +35,14 @@ public: static QPoint getQPointCase( QPointCases p ); private slots: + void comparisonCompiles(); + void comparison_data(); + void comparison(); + void fuzzyComparison_data(); + void fuzzyComparison(); void isNull_data(); void isNull(); + void fuzzyIsNull(); void newIsEmpty_data(); void newIsEmpty(); void newIsValid_data(); @@ -124,6 +108,9 @@ private slots: void margins(); void marginsf(); + void toRectF_data(); + void toRectF(); + void translate_data(); void translate(); @@ -181,6 +168,8 @@ private slots: #define LARGE 1000000000 static bool isLarge(int x) { return x > LARGE || x < -LARGE; } +static constexpr qreal qreal_min = std::numeric_limits<qreal>::min(); + QRect tst_QRect::getQRectCase( QRectCases c ) { // Should return the best variety of possible QRects, if a @@ -263,6 +252,80 @@ QPoint tst_QRect::getQPointCase( QPointCases p ) } } +void tst_QRect::comparisonCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile<QRect>(); + QTestPrivate::testEqualityOperatorsCompile<QRectF>(); + QTestPrivate::testEqualityOperatorsCompile<QRectF, QRect>(); +} + +void tst_QRect::comparison_data() +{ + QTest::addColumn<QRectF>("lhsF"); + QTest::addColumn<QRectF>("rhsF"); + QTest::addColumn<bool>("result"); + QTest::addColumn<bool>("floatResult"); + QTest::addColumn<bool>("mixedResult"); + + QTest::newRow("Invalid_vs_Invalid") << getQRectCase(InvalidQRect).toRectF() + << getQRectCase(InvalidQRect).toRectF() + << true << true << true; + + QTest::newRow("Null_vs_Null") << getQRectCase(NullQRect).toRectF() + << getQRectCase(NullQRect).toRectF() + << true << true << true; + + QTest::newRow("Empty_vs_Empty") << getQRectCase(EmptyQRect).toRectF() + << getQRectCase(EmptyQRect).toRectF() + << true << true << true; + + QTest::newRow("NegativeSize_vs_NegativeSize") << getQRectCase(NegativeSizeQRect).toRectF() + << getQRectCase(NegativeSizeQRect).toRectF() + << true << true << true; + + QTest::newRow("Invalid_vs_Null") << getQRectCase(InvalidQRect).toRectF() + << getQRectCase(NullQRect).toRectF() + << false << false << false; + + QTest::newRow("NearlySimilar") << QRectF(QPointF(1.1, 9.9), QPointF(9.9, 1.1)) + << QRectF(QPointF(1., 10.), QPointF(10., 1.)) + << true << false << true; + + QTest::newRow("WithQREAL_MIN") << QRectF(QPointF(0., -10.), QPointF(-1., 0.)) + << QRectF(QPointF(-qreal_min, -10.), QPointF(-1., qreal_min)) + << true << true << true; +} + +void tst_QRect::comparison() +{ + QFETCH(const QRectF, lhsF); + QFETCH(const QRectF, rhsF); + QFETCH(const bool, result); + QFETCH(const bool, floatResult); + QFETCH(const bool, mixedResult); + + const QRect lhs = lhsF.toRect(); + const QRect rhs = rhsF.toRect(); + + QT_TEST_EQUALITY_OPS(lhs, rhs, result); + QT_TEST_EQUALITY_OPS(lhsF, rhsF, floatResult); + QT_TEST_EQUALITY_OPS(lhs, rhsF, mixedResult); +} + +void tst_QRect::fuzzyComparison_data() +{ + comparison_data(); +} + +void tst_QRect::fuzzyComparison() +{ + QFETCH(const QRectF, lhsF); + QFETCH(const QRectF, rhsF); + QFETCH(const bool, floatResult); + + QCOMPARE_EQ(qFuzzyCompare(lhsF, rhsF), floatResult); +} + void tst_QRect::isNull_data() { QTest::addColumn<QRect>("r"); @@ -292,6 +355,14 @@ void tst_QRect::isNull() QVERIFY( rf.isNull() == isNull ); } +void tst_QRect::fuzzyIsNull() +{ + QRectF rf(QPointF(-qreal_min, qreal_min), QPointF(qreal_min, -qreal_min)); + + QVERIFY(!rf.isNull()); // QRectF::isNull() does strict comparison + QVERIFY(qFuzzyIsNull(rf)); +} + void tst_QRect::newIsEmpty_data() { QTest::addColumn<QRect>("r"); @@ -2526,16 +2597,11 @@ void tst_QRect::newMoveLeft_data() { // QTest::newRow( "LargestCoordQRect_MinimumInt" ) -- Not tested as it would cause an overflow - QTest::newRow( "LargestCoordQRect_MiddleNegativeInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( MiddleNegativeInt ) - << QRect( QPoint( INT_MIN/2, INT_MIN ), QPoint(INT_MIN/2-1, INT_MAX ) ); - QTest::newRow( "LargestCoordQRect_ZeroInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( ZeroInt ) - << QRect( QPoint( 0, INT_MIN ), QPoint(-1, INT_MAX ) ); - QTest::newRow( "LargestCoordQRect_MiddlePositiveInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( MiddlePositiveInt ) - << QRect( QPoint( INT_MAX/2, INT_MIN ), QPoint(INT_MAX/2-1, INT_MAX ) ); - QTest::newRow( "LargestCoordQRect_MaximumInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( MaximumInt ) - << QRect( QPoint( INT_MAX, INT_MIN ), QPoint(INT_MAX-1, INT_MAX ) ); - QTest::newRow( "LargestCoordQRect_RandomInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( RandomInt ) - << QRect( QPoint( 4953, INT_MIN ), QPoint(4952, INT_MAX ) ); + // QTest::newRow( "LargestCoordQRect_MiddleNegativeInt" ) -- Not tested as it would cause an overflow + // QTest::newRow( "LargestCoordQRect_ZeroInt" ) -- Not tested as it would cause an overflow + // QTest::newRow( "LargestCoordQRect_MiddlePositiveInt" ) -- Not tested as it would cause an overflow + // QTest::newRow( "LargestCoordQRect_MaximumInt" ) -- Not tested as it would cause an overflow + // QTest::newRow( "LargestCoordQRect_RandomInt" ) -- Not tested as it would cause an overflow } { @@ -2695,16 +2761,11 @@ void tst_QRect::newMoveTop_data() { // QTest::newRow( "LargestCoordQRect_MinimumInt" ) -- Not tested as it would cause an overflow - QTest::newRow( "LargestCoordQRect_MiddleNegativeInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( MiddleNegativeInt ) - << QRect( QPoint(INT_MIN,INT_MIN/2), QPoint(INT_MAX,INT_MIN/2-1) ); - QTest::newRow( "LargestCoordQRect_ZeroInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( ZeroInt ) - << QRect( QPoint(INT_MIN,0), QPoint(INT_MAX,-1) ); - QTest::newRow( "LargestCoordQRect_MiddlePositiveInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( MiddlePositiveInt ) - << QRect( QPoint(INT_MIN,INT_MAX/2), QPoint(INT_MAX,INT_MAX/2-1) ); - QTest::newRow( "LargestCoordQRect_MaximumInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( MaximumInt ) - << QRect( QPoint(INT_MIN,INT_MAX), QPoint(INT_MAX,INT_MAX-1) ); - QTest::newRow( "LargestCoordQRect_RandomInt" ) << getQRectCase( LargestCoordQRect ) << getIntCase( RandomInt ) - << QRect( QPoint(INT_MIN,4953), QPoint(INT_MAX,4952) ); + // QTest::newRow( "LargestCoordQRect_MiddleNegativeInt" ) -- Not tested as it would cause an overflow + // QTest::newRow( "LargestCoordQRect_ZeroInt" ) -- Not tested as it would cause an overflow + // QTest::newRow( "LargestCoordQRect_MiddlePositiveInt" ) -- Not tested as it would cause an overflow + // QTest::newRow( "LargestCoordQRect_MaximumInt" ) -- Not tested as it would cause an overflow + // QTest::newRow( "LargestCoordQRect_RandomInt" ) -- Not tested as it would cause an overflow } { @@ -3536,6 +3597,39 @@ void tst_QRect::marginsf() QCOMPARE(a, rectangle.marginsRemoved(margins)); } +void tst_QRect::toRectF_data() +{ + QTest::addColumn<QRect>("input"); + QTest::addColumn<QRectF>("result"); + + auto row = [](int x1, int y1, int w, int h) { + // QRectF -> QRect conversion tries to maintain size(), not bottomRight(), + // so compare in (topLeft(), size()) space + QTest::addRow("((%d, %d) (%dx%d))", x1, y1, w, h) + << QRect({x1, y1}, QSize{w, h}) << QRectF(QPointF(x1, y1), QSizeF(w, h)); + }; + constexpr std::array samples = {-1, 0, 1}; + for (int x1 : samples) { + for (int y1 : samples) { + for (int w : samples) { + for (int h : samples) { + row(x1, y1, w, h); + } + } + } + } +} + +void tst_QRect::toRectF() +{ + QFETCH(const QRect, input); + QFETCH(const QRectF, result); + + QCOMPARE(result.toRect(), input); // consistency check + QCOMPARE(input.toRectF(), result); +} + + void tst_QRect::translate_data() { QTest::addColumn<QRect>("r"); @@ -4313,8 +4407,6 @@ void tst_QRect::containsPointF_data() QTest::addColumn<QPointF>("point"); QTest::addColumn<bool>("contains"); - QTest::newRow("test 27") << QRectF() << QPointF() << false; - QTest::newRow("test 01") << QRectF(0, 0, 10, 10) << QPointF( 0, 0) << true; QTest::newRow("test 02") << QRectF(0, 0, 10, 10) << QPointF( 0, 10) << true; QTest::newRow("test 03") << QRectF(0, 0, 10, 10) << QPointF(10, 0) << true; |