summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qline
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/tools/qline')
-rw-r--r--tests/auto/corelib/tools/qline/CMakeLists.txt17
-rw-r--r--tests/auto/corelib/tools/qline/qline.pro5
-rw-r--r--tests/auto/corelib/tools/qline/tst_qline.cpp254
3 files changed, 216 insertions, 60 deletions
diff --git a/tests/auto/corelib/tools/qline/CMakeLists.txt b/tests/auto/corelib/tools/qline/CMakeLists.txt
index 24de03c81a..7d9fdf51a9 100644
--- a/tests/auto/corelib/tools/qline/CMakeLists.txt
+++ b/tests/auto/corelib/tools/qline/CMakeLists.txt
@@ -1,18 +1,27 @@
-# Generated from qline.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_qline Test:
#####################################################################
-qt_add_test(tst_qline
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qline LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qline
SOURCES
tst_qline.cpp
+ LIBRARIES
+ Qt::TestPrivate
)
## Scopes:
#####################################################################
-qt_extend_target(tst_qline CONDITION UNIX AND NOT APPLE AND NOT HAIKU AND NOT INTEGRITY AND NOT VXWORKS
- PUBLIC_LIBRARIES
+qt_internal_extend_target(tst_qline CONDITION UNIX AND NOT APPLE AND NOT HAIKU AND NOT INTEGRITY AND NOT VXWORKS
+ LIBRARIES
m
)
diff --git a/tests/auto/corelib/tools/qline/qline.pro b/tests/auto/corelib/tools/qline/qline.pro
deleted file mode 100644
index 81e2f17118..0000000000
--- a/tests/auto/corelib/tools/qline/qline.pro
+++ /dev/null
@@ -1,5 +0,0 @@
-CONFIG += testcase
-TARGET = tst_qline
-QT = core testlib
-SOURCES = tst_qline.cpp
-unix:!darwin:!vxworks:!haiku:!integrity: LIBS+=-lm
diff --git a/tests/auto/corelib/tools/qline/tst_qline.cpp b/tests/auto/corelib/tools/qline/tst_qline.cpp
index 7e07e9d1e2..10069e821b 100644
--- a/tests/auto/corelib/tools/qline/tst_qline.cpp
+++ b/tests/auto/corelib/tools/qline/tst_qline.cpp
@@ -1,47 +1,33 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 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$
-**
-****************************************************************************/
-
-#include <QtTest/QtTest>
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QTest>
#include <qline.h>
#include <qmath.h>
+#include <private/qcomparisontesthelper_p.h>
+
+#include <array>
class tst_QLine : public QObject
{
Q_OBJECT
private slots:
+ void testComparisonCompiles();
+ void testComparison_data();
+ void testComparison();
+
+ void testFuzzyCompare_data();
+ void testFuzzyCompare();
+
+ void testIsNull_data();
+ void testIsNull();
+
void testIntersection();
void testIntersection_data();
void testLength();
void testLength_data();
- void testSetLength();
-
void testCenter();
void testCenter_data();
@@ -60,9 +46,126 @@ private slots:
void testAngleTo_data();
void testSet();
+
+ void toLineF_data();
+ void toLineF();
};
const qreal epsilon = sizeof(qreal) == sizeof(double) ? 1e-8 : 1e-4;
+constexpr static qreal qreal_min = std::numeric_limits<qreal>::min();
+
+void tst_QLine::testComparisonCompiles()
+{
+ QTestPrivate::testEqualityOperatorsCompile<QLine>();
+ QTestPrivate::testEqualityOperatorsCompile<QLineF>();
+ QTestPrivate::testEqualityOperatorsCompile<QLineF, QLine>();
+}
+
+void tst_QLine::testComparison_data()
+{
+ QTest::addColumn<double>("xa1");
+ QTest::addColumn<double>("ya1");
+ QTest::addColumn<double>("xa2");
+ QTest::addColumn<double>("ya2");
+ QTest::addColumn<double>("xb1");
+ QTest::addColumn<double>("yb1");
+ QTest::addColumn<double>("xb2");
+ QTest::addColumn<double>("yb2");
+ QTest::addColumn<bool>("result");
+ QTest::addColumn<bool>("floatResult");
+ QTest::addColumn<bool>("mixedResult");
+
+ auto row = [&](double xa1, double ya1, double xa2, double ya2,
+ double xb1, double yb1, double xb2, double yb2,
+ bool result, bool floatResult, bool mixedResult)
+ {
+ QString str;
+ QDebug dbg(&str);
+ dbg.nospace() << "[(" << xa1 << ", " << ya1 << "); (" << xa2 << ", " << ya2 << ")] vs [("
+ << xb1 << ", " << yb1 << "); (" << xb2 << ", " << yb2 << ")]";
+ QTest::addRow("%s", str.toLatin1().constData())
+ << xa1 << ya1 << xa2 << ya2 << xb1 << yb1 << xb2 << yb2
+ << result << floatResult << mixedResult;
+ };
+
+ row(-1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, true, true, true);
+ row(-1.1, -0.9, 1.1, 0.9, -1.0, -1.0, 1.0, 1.0, true, false, false);
+ row(-1.0, -1.0, 1.0, 1.0, -0.9, -1.1, 0.9, 1.1, true, false, true);
+ row(-qreal_min, -1.0, 1.0, qreal_min, 0.0, -1.1, 0.9, 0.0, true, false, true);
+}
+
+void tst_QLine::testComparison()
+{
+ QFETCH(double, xa1);
+ QFETCH(double, ya1);
+ QFETCH(double, xa2);
+ QFETCH(double, ya2);
+ QFETCH(double, xb1);
+ QFETCH(double, yb1);
+ QFETCH(double, xb2);
+ QFETCH(double, yb2);
+ QFETCH(bool, result);
+ QFETCH(bool, floatResult);
+ QFETCH(bool, mixedResult);
+
+ const QLineF l1f(xa1, ya1, xa2, ya2);
+ const QLine l1 = l1f.toLine();
+
+ const QLineF l2f(xb1, yb1, xb2, yb2);
+ const QLine l2 = l2f.toLine();
+
+ QT_TEST_EQUALITY_OPS(l1, l2, result);
+ QT_TEST_EQUALITY_OPS(l1f, l2f, floatResult);
+ QT_TEST_EQUALITY_OPS(l1f, l2, mixedResult);
+}
+
+void tst_QLine::testFuzzyCompare_data()
+{
+ testComparison_data();
+}
+
+void tst_QLine::testFuzzyCompare()
+{
+ QFETCH(double, xa1);
+ QFETCH(double, ya1);
+ QFETCH(double, xa2);
+ QFETCH(double, ya2);
+ QFETCH(double, xb1);
+ QFETCH(double, yb1);
+ QFETCH(double, xb2);
+ QFETCH(double, yb2);
+ QFETCH(bool, floatResult);
+
+ const QLineF l1f(xa1, ya1, xa2, ya2);
+ const QLineF l2f(xb1, yb1, xb2, yb2);
+
+ QCOMPARE_EQ(qFuzzyCompare(l1f, l2f), floatResult);
+}
+
+void tst_QLine::testIsNull_data()
+{
+ QTest::addColumn<QLineF>("lineF");
+ QTest::addColumn<bool>("result");
+ QTest::addColumn<bool>("floatResult");
+
+ QTest::newRow("non-null") << QLineF(1.0, 1.0, 2.0, 2.0) << false << false;
+ QTest::newRow("null") << QLineF(1.0, 1.0, 1.0, 1.0) << true << true;
+ QTest::newRow("null_int_non-null_float") << QLineF(1.0, 1.0, 1.1, 1.1) << true << false;
+ QTest::newRow("with_qreal_min") << QLineF(-qreal_min, qreal_min, 0.0, 0.0) << true << true;
+}
+
+void tst_QLine::testIsNull()
+{
+ QFETCH(QLineF, lineF);
+ QFETCH(bool, result);
+ QFETCH(bool, floatResult);
+
+ const QLine line = lineF.toLine();
+
+ QCOMPARE_EQ(line.isNull(), result);
+ QCOMPARE_EQ(lineF.isNull(), floatResult);
+ QCOMPARE_EQ(qFuzzyIsNull(lineF), floatResult);
+}
void tst_QLine::testSet()
{
@@ -214,11 +317,13 @@ void tst_QLine::testLength_data()
QTest::addColumn<double>("vx");
QTest::addColumn<double>("vy");
- QTest::newRow("[1,0]*2") << 0.0 << 0.0 << 1.0 << 0.0 << 1.0 << 2.0 << 2.0 << 0.0;
- QTest::newRow("[0,1]*2") << 0.0 << 0.0 << 0.0 << 1.0 << 1.0 << 2.0 << 0.0 << 2.0;
- QTest::newRow("[-1,0]*2") << 0.0 << 0.0 << -1.0 << 0.0 << 1.0 << 2.0 << -2.0 << 0.0;
- QTest::newRow("[0,-1]*2") << 0.0 << 0.0 << 0.0 << -1.0 << 1.0 << 2.0 << 0.0 << -2.0;
- QTest::newRow("[1,1]->|1|") << 0.0 << 0.0 << 1.0 << 1.0
+ // Test name: [dx,dy]->|lenToSet| (x1,x2)
+ // with the last part omitted if (0,0)
+ QTest::newRow("[1,0]->|2|") << 0.0 << 0.0 << 1.0 << 0.0 << 1.0 << 2.0 << 2.0 << 0.0;
+ QTest::newRow("[0,1]->|2|") << 0.0 << 0.0 << 0.0 << 1.0 << 1.0 << 2.0 << 0.0 << 2.0;
+ QTest::newRow("[-1,0]->|2|") << 0.0 << 0.0 << -1.0 << 0.0 << 1.0 << 2.0 << -2.0 << 0.0;
+ QTest::newRow("[0,-1]->|2|") << 0.0 << 0.0 << 0.0 << -1.0 << 1.0 << 2.0 << 0.0 << -2.0;
+ QTest::newRow("[1,1]->->|1|") << 0.0 << 0.0 << 1.0 << 1.0
<< M_SQRT2 << 1.0 << M_SQRT1_2 << M_SQRT1_2;
QTest::newRow("[-1,1]->|1|") << 0.0 << 0.0 << -1.0 << 1.0
<< M_SQRT2 << 1.0 << -M_SQRT1_2 << M_SQRT1_2;
@@ -226,10 +331,10 @@ void tst_QLine::testLength_data()
<< M_SQRT2 << 1.0 << M_SQRT1_2 << -M_SQRT1_2;
QTest::newRow("[-1,-1]->|1|") << 0.0 << 0.0 << -1.0 << -1.0
<< M_SQRT2 << 1.0 << -M_SQRT1_2 << -M_SQRT1_2;
- QTest::newRow("[1,0]*2 (2,2)") << 2.0 << 2.0 << 3.0 << 2.0 << 1.0 << 2.0 << 2.0 << 0.0;
- QTest::newRow("[0,1]*2 (2,2)") << 2.0 << 2.0 << 2.0 << 3.0 << 1.0 << 2.0 << 0.0 << 2.0;
- QTest::newRow("[-1,0]*2 (2,2)") << 2.0 << 2.0 << 1.0 << 2.0 << 1.0 << 2.0 << -2.0 << 0.0;
- QTest::newRow("[0,-1]*2 (2,2)") << 2.0 << 2.0 << 2.0 << 1.0 << 1.0 << 2.0 << 0.0 << -2.0;
+ QTest::newRow("[1,0]->|2| (2,2)") << 2.0 << 2.0 << 3.0 << 2.0 << 1.0 << 2.0 << 2.0 << 0.0;
+ QTest::newRow("[0,1]->|2| (2,2)") << 2.0 << 2.0 << 2.0 << 3.0 << 1.0 << 2.0 << 0.0 << 2.0;
+ QTest::newRow("[-1,0]->|2| (2,2)") << 2.0 << 2.0 << 1.0 << 2.0 << 1.0 << 2.0 << -2.0 << 0.0;
+ QTest::newRow("[0,-1]->|2| (2,2)") << 2.0 << 2.0 << 2.0 << 1.0 << 1.0 << 2.0 << 0.0 << -2.0;
QTest::newRow("[1,1]->|1| (2,2)") << 2.0 << 2.0 << 3.0 << 3.0
<< M_SQRT2 << 1.0 << M_SQRT1_2 << M_SQRT1_2;
QTest::newRow("[-1,1]->|1| (2,2)") << 2.0 << 2.0 << 1.0 << 3.0
@@ -238,6 +343,20 @@ void tst_QLine::testLength_data()
<< M_SQRT2 << 1.0 << M_SQRT1_2 << -M_SQRT1_2;
QTest::newRow("[-1,-1]->|1| (2,2)") << 2.0 << 2.0 << 1.0 << 1.0
<< M_SQRT2 << 1.0 << -M_SQRT1_2 << -M_SQRT1_2;
+ const double small = qSqrt(std::numeric_limits<qreal>::denorm_min()) / 8;
+ QTest::newRow("[small,small]->|2| (-small/2,-small/2)")
+ << -(small * .5) << -(small * .5) << (small * .5) << (small * .5)
+ << (small * M_SQRT2) << (2 * M_SQRT2) << 2.0 << 2.0;
+ const double tiny = std::numeric_limits<qreal>::min() / 2;
+ QTest::newRow("[tiny,tiny]->|2| (-tiny/2,-tiny/2)")
+ << -(tiny * .5) << -(tiny * .5) << (tiny * .5) << (tiny * .5)
+ << (tiny * M_SQRT2) << (2 * M_SQRT2) << 2.0 << 2.0;
+ QTest::newRow("[1+3e-13,1+4e-13]|1895| (1, 1)")
+ << 1.0 << 1.0 << (1 + 3e-13) << (1 + 4e-13)
+ << 5e-13 << 1895.0 << 1137.0 << 1516.0;
+ QTest::newRow("[4e-323,5e-324]|1892|") // Unavoidable underflow: denormals
+ << 0.0 << 0.0 << 4e-323 << 5e-324
+ << 4e-323 << 1892.0 << 4e-323 << 5e-324; // vx, vy values ignored
}
void tst_QLine::testLength()
@@ -255,18 +374,21 @@ void tst_QLine::testLength()
QCOMPARE(l.length(), qreal(length));
l.setLength(lengthToSet);
- QCOMPARE(l.length(), qreal(lengthToSet));
- QCOMPARE(l.dx(), qreal(vx));
- QCOMPARE(l.dy(), qreal(vy));
-}
-void tst_QLine::testSetLength()
-{
- QLineF l(0, 0, 4e-323, 5e-324);
- const qreal newLength = 1892;
- const qreal oldLength = l.length();
- l.setLength(newLength);
- QCOMPARE(l.length(), oldLength ? newLength : 0);
+ if constexpr (std::numeric_limits<double>::has_denorm != std::denorm_present) {
+ if (qstrcmp(QTest::currentDataTag(), "[tiny,tiny]->|2| (-tiny/2,-tiny/2)") == 0
+ || qstrcmp(QTest::currentDataTag(), "[4e-323,5e-324]|1892|") == 0) {
+ QSKIP("Skipping 'denorm' as this type lacks denormals on this system");
+ }
+ }
+ // Scaling tiny values up to big can be imprecise: don't try to test vx, vy
+ if (length > 0 && qFuzzyIsNull(length)) {
+ QVERIFY(l.length() > lengthToSet / 2 && l.length() < lengthToSet * 2);
+ } else {
+ QCOMPARE(l.length(), length > 0 ? qreal(lengthToSet) : qreal(length));
+ QCOMPARE(l.dx(), qreal(vx));
+ QCOMPARE(l.dy(), qreal(vy));
+ }
}
void tst_QLine::testCenter()
@@ -485,5 +607,35 @@ void tst_QLine::testAngleTo_data()
}
}
+void tst_QLine::toLineF_data()
+{
+ QTest::addColumn<QLine>("input");
+ QTest::addColumn<QLineF>("result");
+
+ auto row = [](int x1, int y1, int x2, int y2) {
+ QTest::addRow("((%d, %d)->(%d, %d))", x1, y1, x2, y2)
+ << QLine(x1, y1, x2, y2) << QLineF(x1, y1, x2, y2);
+ };
+ constexpr std::array samples = {-1, 0, 1};
+ for (int x1 : samples) {
+ for (int y1 : samples) {
+ for (int x2 : samples) {
+ for (int y2 : samples) {
+ row(x1, y1, x2, y2);
+ }
+ }
+ }
+ }
+}
+
+void tst_QLine::toLineF()
+{
+ QFETCH(const QLine, input);
+ QFETCH(const QLineF, result);
+
+ QCOMPARE(input.toLineF(), result);
+}
+
+
QTEST_MAIN(tst_QLine)
#include "tst_qline.moc"