From 6dfec83051c3426fcf2336f8b7acfdc290efe94c Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 15 Apr 2020 18:31:51 +0200 Subject: Add tests for qFuzzyIsNull() to tst_QNumeric and tst_QFloat16 They were missing. I also wanted to verify that it's true for sub-normal values. At the same time, relocate qfloat16's implementation of qFuzzyIsNull() to between those of qFuzzyCompare() and qIsNull(), since its apparent absence initially confused me. Change-Id: I9637c0070e754d16744c76fc9f846596257c6a63 Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira --- tests/auto/corelib/global/qfloat16/CMakeLists.txt | 2 ++ .../auto/corelib/global/qfloat16/tst_qfloat16.cpp | 31 +++++++++++++++++- .../auto/corelib/global/qnumeric/tst_qnumeric.cpp | 38 +++++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) (limited to 'tests/auto/corelib/global') diff --git a/tests/auto/corelib/global/qfloat16/CMakeLists.txt b/tests/auto/corelib/global/qfloat16/CMakeLists.txt index b2848846fa..1dbc95ec95 100644 --- a/tests/auto/corelib/global/qfloat16/CMakeLists.txt +++ b/tests/auto/corelib/global/qfloat16/CMakeLists.txt @@ -7,4 +7,6 @@ qt_internal_add_test(tst_qfloat16 SOURCES tst_qfloat16.cpp + PUBLIC_LIBRARIES + Qt::TestPrivate ) diff --git a/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp b/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp index 96051cbe2c..71a05d3031 100644 --- a/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp +++ b/tests/auto/corelib/global/qfloat16/tst_qfloat16.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2019 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Copyright (C) 2016 by Southwest Research Institute (R) ** Contact: https://www.qt.io/licensing/ ** @@ -43,6 +43,8 @@ class tst_qfloat16: public QObject private slots: void fuzzyCompare_data(); void fuzzyCompare(); + void fuzzyIsNull_data(); + void fuzzyIsNull(); void ltgt_data(); void ltgt(); void qNaN(); @@ -111,6 +113,33 @@ void tst_qfloat16::fuzzyCompare() } } +void tst_qfloat16::fuzzyIsNull_data() +{ + QTest::addColumn("value"); + QTest::addColumn("isNull"); + using Bounds = std::numeric_limits; + const qfloat16 one(1), huge(1000), tiny(0.00099f); + + QTest::newRow("zero") << qfloat16(0.0f) << true; + QTest::newRow("min") << Bounds::min() << true; + QTest::newRow("denorm_min") << Bounds::denorm_min() << true; + QTest::newRow("tiny") << tiny << true; + + QTest::newRow("deci") << qfloat16(.1) << false; + QTest::newRow("one") << one << false; + QTest::newRow("ten") << qfloat16(10) << false; + QTest::newRow("huge") << huge << false; +} + +void tst_qfloat16::fuzzyIsNull() +{ + QFETCH(qfloat16, value); + QFETCH(bool, isNull); + + QCOMPARE(::qFuzzyIsNull(value), isNull); + QCOMPARE(::qFuzzyIsNull(-value), isNull); +} + void tst_qfloat16::ltgt_data() { QTest::addColumn("val1"); diff --git a/tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp b/tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp index 0cfb8541ab..5b54eeae01 100644 --- a/tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp +++ b/tests/auto/corelib/global/qnumeric/tst_qnumeric.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2019 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Copyright (C) 2016 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** @@ -52,6 +52,8 @@ class tst_QNumeric: public QObject // Support for floating-point: template inline void fuzzyCompare_data(); template inline void fuzzyCompare(); + template inline void fuzzyIsNull_data(); + template inline void fuzzyIsNull(); template inline void checkNaN(F nan); template inline void rawNaN_data(); template inline void rawNaN(); @@ -71,6 +73,10 @@ private slots: void fuzzyCompareF() { fuzzyCompare(); } void fuzzyCompareD_data() { fuzzyCompare_data(); } void fuzzyCompareD() { fuzzyCompare(); } + void fuzzyIsNullF_data() { fuzzyIsNull_data(); } + void fuzzyIsNullF() { fuzzyIsNull(); } + void fuzzyIsNullD_data() { fuzzyIsNull_data(); } + void fuzzyIsNullD() { fuzzyIsNull(); } void rawNaNF_data() { rawNaN_data(); } void rawNaNF() { rawNaN(); } void rawNaND_data() { rawNaN_data(); } @@ -143,6 +149,36 @@ void tst_QNumeric::fuzzyCompare() QCOMPARE(::qFuzzyCompare(-val2, -val1), isEqual); } +template +void tst_QNumeric::fuzzyIsNull_data() +{ + QTest::addColumn("value"); + QTest::addColumn("isNull"); + using Bounds = std::numeric_limits; + const F one(1), huge = Fuzzy::scale, tiny = one / huge; + + QTest::newRow("zero") << F(0) << true; + QTest::newRow("min") << Bounds::min() << true; + QTest::newRow("denorm_min") << Bounds::denorm_min() << true; + QTest::newRow("tiny") << tiny << true; + + QTest::newRow("deci") << F(.1) << false; + QTest::newRow("one") << one << false; + QTest::newRow("ten") << F(10) << false; + QTest::newRow("large") << F(1e9) << false; + QTest::newRow("huge") << huge << false; +} + +template +void tst_QNumeric::fuzzyIsNull() +{ + QFETCH(F, value); + QFETCH(bool, isNull); + + QCOMPARE(::qFuzzyIsNull(value), isNull); + QCOMPARE(::qFuzzyIsNull(-value), isNull); +} + #if defined __FAST_MATH__ && (__GNUC__ * 100 + __GNUC_MINOR__ >= 404) // turn -ffast-math off # pragma GCC optimize "no-fast-math" -- cgit v1.2.3