diff options
Diffstat (limited to 'tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp')
-rw-r--r-- | tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp | 158 |
1 files changed, 76 insertions, 82 deletions
diff --git a/tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp b/tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp index 5b54eeae01..d21fabd74e 100644 --- a/tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp +++ b/tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp @@ -1,31 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** 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. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> @@ -116,7 +91,7 @@ void tst_QNumeric::fuzzyCompare_data() QTest::addColumn<bool>("isEqual"); const F zero(0), one(1), ten(10); const F huge = Fuzzy<F>::scale, tiny = one / huge; - const F deci(.1), giga(1e9), nano(1e-9), big(1e7), small(1e-10); + const F deci(.1f), giga(1e9f), nano(1e-9f), big(1e7f), small(1e-10f); QTest::newRow("zero") << zero << zero << true; QTest::newRow("ten") << ten << ten << true; @@ -179,6 +154,15 @@ void tst_QNumeric::fuzzyIsNull() QCOMPARE(::qFuzzyIsNull(-value), isNull); } +static void clearFpExceptions() +{ + // Call after any functions that exercise floating-point exceptions, such as + // sqrt(-1) or log(0). +#ifdef Q_OS_WIN + _clearfp(); +#endif +} + #if defined __FAST_MATH__ && (__GNUC__ * 100 + __GNUC_MINOR__ >= 404) // turn -ffast-math off # pragma GCC optimize "no-fast-math" @@ -187,6 +171,7 @@ void tst_QNumeric::fuzzyIsNull() template<typename F> void tst_QNumeric::checkNaN(F nan) { + const auto cleanup = qScopeGuard([]() { clearFpExceptions(); }); #define CHECKNAN(value) \ do { \ const F v = (value); \ @@ -195,21 +180,23 @@ void tst_QNumeric::checkNaN(F nan) QVERIFY(!qIsFinite(v)); \ QVERIFY(!qIsInf(v)); \ } while (0) + const F zero(0), one(1), two(2); - QVERIFY(!(0 > nan)); - QVERIFY(!(0 < nan)); - QVERIFY(!(0 == nan)); + QVERIFY(!(zero > nan)); + QVERIFY(!(zero < nan)); + QVERIFY(!(zero == nan)); QVERIFY(!(nan == nan)); CHECKNAN(nan); - CHECKNAN(nan + 1); - CHECKNAN(nan - 1); + CHECKNAN(nan + one); + CHECKNAN(nan - one); CHECKNAN(-nan); - CHECKNAN(nan * 2.0); - CHECKNAN(nan / 2.0); - CHECKNAN(1.0 / nan); - CHECKNAN(0.0 / nan); - CHECKNAN(0.0 * nan); + CHECKNAN(nan * two); + CHECKNAN(nan / two); + CHECKNAN(one / nan); + CHECKNAN(zero / nan); + CHECKNAN(zero * nan); + CHECKNAN(sqrt(-one)); // When any NaN is expected, any NaN will do: QCOMPARE(nan, nan); @@ -297,6 +284,7 @@ void tst_QNumeric::generalNaN() template<typename F> void tst_QNumeric::infinity() { + const auto cleanup = qScopeGuard([]() { clearFpExceptions(); }); const F inf = qInf(); const F zero(0), one(1), two(2); QVERIFY(inf > zero); @@ -319,6 +307,7 @@ void tst_QNumeric::infinity() QCOMPARE(one / -inf, zero); QVERIFY(qIsNaN(zero * inf)); QVERIFY(qIsNaN(zero * -inf)); + QCOMPARE(log(zero), -inf); } template<typename F> @@ -332,8 +321,11 @@ void tst_QNumeric::classifyfp() QCOMPARE(qFpClassify(inf), FP_INFINITE); QCOMPARE(qFpClassify(-inf), FP_INFINITE); + QT_WARNING_PUSH; + QT_WARNING_DISABLE_MSVC(4056); QCOMPARE(qFpClassify(huge * two), FP_INFINITE); QCOMPARE(qFpClassify(huge * -two), FP_INFINITE); + QT_WARNING_POP; QCOMPARE(qFpClassify(one), FP_NORMAL); QCOMPARE(qFpClassify(huge), FP_NORMAL); @@ -402,9 +394,11 @@ void tst_QNumeric::distance() QFETCH(F, from); QFETCH(F, stop); QFETCH(Count, expectedDistance); -#ifdef Q_OS_QNX - QEXPECT_FAIL("denormal", "See QTBUG-37094", Continue); -#endif + if constexpr (std::numeric_limits<F>::has_denorm != std::denorm_present) { + if (qstrcmp(QTest::currentDataTag(), "denormal") == 0) { + QSKIP("Skipping 'denorm' as this type lacks denormals on this system"); + } + } QCOMPARE(qFloatDistance(from, stop), expectedDistance); QCOMPARE(qFloatDistance(stop, from), expectedDistance); } @@ -443,33 +437,33 @@ template <typename Int> static void addOverflow_template() #define ADD_COMPARE_NONOVF(v1, v2, expected) \ do { \ - QCOMPARE(add_overflow(Int(v1), Int(v2), &r), false); \ + QCOMPARE(qAddOverflow(Int(v1), Int(v2), &r), false); \ QCOMPARE(r, Int(expected)); \ - QCOMPARE(add_overflow(Int(v1), (std::integral_constant<Int, Int(v2)>()), &r), false); \ + QCOMPARE(qAddOverflow(Int(v1), (std::integral_constant<Int, Int(v2)>()), &r), false); \ QCOMPARE(r, Int(expected)); \ - QCOMPARE(add_overflow<v2>(Int(v1), &r), false); \ + QCOMPARE(qAddOverflow<v2>(Int(v1), &r), false); \ QCOMPARE(r, Int(expected)); \ } while (false) #define ADD_COMPARE_OVF(v1, v2) \ do { \ - QCOMPARE(add_overflow(Int(v1), Int(v2), &r), true); \ - QCOMPARE(add_overflow(Int(v1), (std::integral_constant<Int, Int(v2)>()), &r), true); \ - QCOMPARE(add_overflow<v2>(Int(v1), &r), true); \ + QCOMPARE(qAddOverflow(Int(v1), Int(v2), &r), true); \ + QCOMPARE(qAddOverflow(Int(v1), (std::integral_constant<Int, Int(v2)>()), &r), true); \ + QCOMPARE(qAddOverflow<v2>(Int(v1), &r), true); \ } while (false) #define SUB_COMPARE_NONOVF(v1, v2, expected) \ do { \ - QCOMPARE(sub_overflow(Int(v1), Int(v2), &r), false); \ + QCOMPARE(qSubOverflow(Int(v1), Int(v2), &r), false); \ QCOMPARE(r, Int(expected)); \ - QCOMPARE(sub_overflow(Int(v1), (std::integral_constant<Int, Int(v2)>()), &r), false); \ + QCOMPARE(qSubOverflow(Int(v1), (std::integral_constant<Int, Int(v2)>()), &r), false); \ QCOMPARE(r, Int(expected)); \ - QCOMPARE(sub_overflow<v2>(Int(v1), &r), false); \ + QCOMPARE(qSubOverflow<v2>(Int(v1), &r), false); \ QCOMPARE(r, Int(expected)); \ } while (false) #define SUB_COMPARE_OVF(v1, v2) \ do { \ - QCOMPARE(sub_overflow(Int(v1), Int(v2), &r), true); \ - QCOMPARE(sub_overflow(Int(v1), (std::integral_constant<Int, Int(v2)>()), &r), true); \ - QCOMPARE(sub_overflow<v2>(Int(v1), &r), true); \ + QCOMPARE(qSubOverflow(Int(v1), Int(v2), &r), true); \ + QCOMPARE(qSubOverflow(Int(v1), (std::integral_constant<Int, Int(v2)>()), &r), true); \ + QCOMPARE(qSubOverflow<v2>(Int(v1), &r), true); \ } while (false) // basic values @@ -620,18 +614,18 @@ template <typename Int> static void mulOverflow_template() #define MUL_COMPARE_NONOVF(v1, v2, expected) \ do { \ - QCOMPARE(mul_overflow(Int(v1), Int(v2), &r), false); \ + QCOMPARE(qMulOverflow(Int(v1), Int(v2), &r), false); \ QCOMPARE(r, Int(expected)); \ - QCOMPARE(mul_overflow(Int(v1), (std::integral_constant<Int, v2>()), &r), false); \ + QCOMPARE(qMulOverflow(Int(v1), (std::integral_constant<Int, v2>()), &r), false); \ QCOMPARE(r, Int(expected)); \ - QCOMPARE(mul_overflow<v2>(Int(v1), &r), false); \ + QCOMPARE(qMulOverflow<v2>(Int(v1), &r), false); \ QCOMPARE(r, Int(expected)); \ } while (false); #define MUL_COMPARE_OVF(v1, v2) \ do { \ - QCOMPARE(mul_overflow(Int(v1), Int(v2), &r), true); \ - QCOMPARE(mul_overflow(Int(v1), (std::integral_constant<Int, v2>()), &r), true); \ - QCOMPARE(mul_overflow<v2>(Int(v1), &r), true); \ + QCOMPARE(qMulOverflow(Int(v1), Int(v2), &r), true); \ + QCOMPARE(qMulOverflow(Int(v1), (std::integral_constant<Int, v2>()), &r), true); \ + QCOMPARE(qMulOverflow<v2>(Int(v1), &r), true); \ } while (false); // basic multiplications @@ -732,28 +726,28 @@ void tst_QNumeric::signedOverflow() const int maxInt = std::numeric_limits<int>::max(); int r; - QCOMPARE(add_overflow(minInt + 1, int(-1), &r), false); - QCOMPARE(add_overflow(minInt, int(-1), &r), true); - QCOMPARE(add_overflow(minInt, minInt, &r), true); - QCOMPARE(add_overflow(maxInt - 1, int(1), &r), false); - QCOMPARE(add_overflow(maxInt, int(1), &r), true); - QCOMPARE(add_overflow(maxInt, maxInt, &r), true); - - QCOMPARE(sub_overflow(minInt + 1, int(1), &r), false); - QCOMPARE(sub_overflow(minInt, int(1), &r), true); - QCOMPARE(sub_overflow(minInt, maxInt, &r), true); - QCOMPARE(sub_overflow(maxInt - 1, int(-1), &r), false); - QCOMPARE(sub_overflow(maxInt, int(-1), &r), true); - QCOMPARE(sub_overflow(maxInt, minInt, &r), true); - - QCOMPARE(mul_overflow(minInt, int(1), &r), false); - QCOMPARE(mul_overflow(minInt, int(-1), &r), true); - QCOMPARE(mul_overflow(minInt, int(2), &r), true); - QCOMPARE(mul_overflow(minInt, minInt, &r), true); - QCOMPARE(mul_overflow(maxInt, int(1), &r), false); - QCOMPARE(mul_overflow(maxInt, int(-1), &r), false); - QCOMPARE(mul_overflow(maxInt, int(2), &r), true); - QCOMPARE(mul_overflow(maxInt, maxInt, &r), true); + QCOMPARE(qAddOverflow(minInt + 1, int(-1), &r), false); + QCOMPARE(qAddOverflow(minInt, int(-1), &r), true); + QCOMPARE(qAddOverflow(minInt, minInt, &r), true); + QCOMPARE(qAddOverflow(maxInt - 1, int(1), &r), false); + QCOMPARE(qAddOverflow(maxInt, int(1), &r), true); + QCOMPARE(qAddOverflow(maxInt, maxInt, &r), true); + + QCOMPARE(qSubOverflow(minInt + 1, int(1), &r), false); + QCOMPARE(qSubOverflow(minInt, int(1), &r), true); + QCOMPARE(qSubOverflow(minInt, maxInt, &r), true); + QCOMPARE(qSubOverflow(maxInt - 1, int(-1), &r), false); + QCOMPARE(qSubOverflow(maxInt, int(-1), &r), true); + QCOMPARE(qSubOverflow(maxInt, minInt, &r), true); + + QCOMPARE(qMulOverflow(minInt, int(1), &r), false); + QCOMPARE(qMulOverflow(minInt, int(-1), &r), true); + QCOMPARE(qMulOverflow(minInt, int(2), &r), true); + QCOMPARE(qMulOverflow(minInt, minInt, &r), true); + QCOMPARE(qMulOverflow(maxInt, int(1), &r), false); + QCOMPARE(qMulOverflow(maxInt, int(-1), &r), false); + QCOMPARE(qMulOverflow(maxInt, int(2), &r), true); + QCOMPARE(qMulOverflow(maxInt, maxInt, &r), true); } QTEST_APPLESS_MAIN(tst_QNumeric) |