summaryrefslogtreecommitdiffstats
path: root/tests/auto/testlib/selftests/float/tst_float.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/testlib/selftests/float/tst_float.cpp')
-rw-r--r--tests/auto/testlib/selftests/float/tst_float.cpp148
1 files changed, 134 insertions, 14 deletions
diff --git a/tests/auto/testlib/selftests/float/tst_float.cpp b/tests/auto/testlib/selftests/float/tst_float.cpp
index 2fffcc7803..621dd1ccd9 100644
--- a/tests/auto/testlib/selftests/float/tst_float.cpp
+++ b/tests/auto/testlib/selftests/float/tst_float.cpp
@@ -27,19 +27,108 @@
****************************************************************************/
#include <QtCore/QCoreApplication>
+#include <QtCore/qfloat16.h>
#include <QtTest/QtTest>
#include <QDebug>
+// Test proper handling of floating-point types
class tst_float: public QObject
{
Q_OBJECT
private slots:
+ void doubleComparisons() const;
+ void doubleComparisons_data() const;
void floatComparisons() const;
void floatComparisons_data() const;
+ void float16Comparisons() const;
+ void float16Comparisons_data() const;
void compareFloatTests() const;
void compareFloatTests_data() const;
};
+template<typename F>
+static void nonFinite_data(F zero, F one)
+{
+ using Bounds = std::numeric_limits<F>;
+
+ // QCOMPARE special-cases non-finite values
+ if (Bounds::has_quiet_NaN) {
+ const F nan = Bounds::quiet_NaN();
+ QTest::newRow("should PASS: NaN == NaN") << nan << nan;
+ QTest::newRow("should FAIL: NaN != 0") << nan << zero;
+ QTest::newRow("should FAIL: 0 != NaN") << zero << nan;
+ QTest::newRow("should FAIL: NaN != 1") << nan << one;
+ QTest::newRow("should FAIL: 1 != NaN") << one << nan;
+ }
+
+ if (Bounds::has_infinity) {
+ const F uge = Bounds::infinity();
+ QTest::newRow("should PASS: inf == inf") << uge << uge;
+ QTest::newRow("should PASS: -inf == -inf") << -uge << -uge;
+ QTest::newRow("should FAIL: inf != -inf") << uge << -uge;
+ QTest::newRow("should FAIL: -inf != inf") << -uge << uge;
+ if (Bounds::has_quiet_NaN) {
+ const F nan = Bounds::quiet_NaN();
+ QTest::newRow("should FAIL: inf != nan") << uge << nan;
+ QTest::newRow("should FAIL: nan != inf") << nan << uge;
+ QTest::newRow("should FAIL: -inf != nan") << -uge << nan;
+ QTest::newRow("should FAIL: nan != -inf") << nan << -uge;
+ }
+ QTest::newRow("should FAIL: inf != 0") << uge << zero;
+ QTest::newRow("should FAIL: 0 != inf") << zero << uge;
+ QTest::newRow("should FAIL: -inf != 0") << -uge << zero;
+ QTest::newRow("should FAIL: 0 != -inf") << zero << -uge;
+ QTest::newRow("should FAIL: inf != 1") << uge << one;
+ QTest::newRow("should FAIL: 1 != inf") << one << uge;
+ QTest::newRow("should FAIL: -inf != 1") << -uge << one;
+ QTest::newRow("should FAIL: 1 != -inf") << one << -uge;
+
+ const F big = Bounds::max();
+ QTest::newRow("should FAIL: inf != max") << uge << big;
+ QTest::newRow("should FAIL: inf != -max") << uge << -big;
+ QTest::newRow("should FAIL: max != inf") << big << uge;
+ QTest::newRow("should FAIL: -max != inf") << -big << uge;
+ QTest::newRow("should FAIL: -inf != max") << -uge << big;
+ QTest::newRow("should FAIL: -inf != -max") << -uge << -big;
+ QTest::newRow("should FAIL: max != -inf") << big << -uge;
+ QTest::newRow("should FAIL: -max != -inf") << -big << -uge;
+ }
+}
+
+void tst_float::doubleComparisons() const
+{
+ QFETCH(double, operandLeft);
+ QFETCH(double, operandRight);
+
+ QCOMPARE(operandLeft, operandRight);
+}
+
+void tst_float::doubleComparisons_data() const
+{
+ QTest::addColumn<double>("operandLeft");
+ QTest::addColumn<double>("operandRight");
+ double zero(0.), one(1.);
+
+ QTest::newRow("should FAIL 1") << one << 3.;
+ QTest::newRow("should PASS 1") << zero << zero;
+ QTest::newRow("should FAIL 2") << 1.e-7 << 3.e-7;
+
+ // QCOMPARE for doubles uses qFuzzyCompare(), which succeeds if the numbers
+ // differ by no more than 1e-12 times the smaller value. Thus
+ // QCOMPARE(1e12-2, 1e12-1) should fail, while QCOMPARE(1e12+1, 1e12+2)
+ // should pass.
+
+ QTest::newRow("should PASS 2") << 1e12 + one << 1e12 + 2.;
+ QTest::newRow("should FAIL 3") << 1e12 - one << 1e12 - 2.;
+ // ... but rounding makes that a bit unrelaible when scaled close to the bounds.
+ QTest::newRow("should PASS 3") << 1e-310 + 1e-322 << 1e-310 + 2e-322;
+ QTest::newRow("should FAIL 4") << 1e-310 - 1e-322 << 1e-310 - 3e-322;
+ QTest::newRow("should PASS 4") << 1e307 + 1e295 << 1e307 + 2e295;
+ QTest::newRow("should FAIL 5") << 1e307 - 1e295 << 1e307 - 3e295;
+
+ nonFinite_data(zero, one);
+}
+
void tst_float::floatComparisons() const
{
QFETCH(float, operandLeft);
@@ -52,31 +141,62 @@ void tst_float::floatComparisons_data() const
{
QTest::addColumn<float>("operandLeft");
QTest::addColumn<float>("operandRight");
+ float zero(0.f), one(1.f);
+
+ QTest::newRow("should FAIL 1") << one << 3.f;
+ QTest::newRow("should PASS 1") << zero << zero;
+ QTest::newRow("should FAIL 2") << 1.e-7f << 3.e-7f;
+
+ // QCOMPARE for floats uses qFuzzyCompare(), which succeeds if the numbers
+ // differ by no more than 1e-5 times the smaller value. Thus
+ // QCOMPARE(1e5-2, 1e5-1) should fail, while QCOMPARE(1e5+1, 1e5+2)
+ // should pass.
+
+ QTest::newRow("should PASS 2") << 1e5f + one << 1e5f + 2.f;
+ QTest::newRow("should FAIL 3") << 1e5f - one << 1e5f - 2.f;
+ // ... but rounding makes that a bit unrelaible when scaled close to the bounds.
+ QTest::newRow("should PASS 3") << 1e-39f + 1e-44f << 1e-39f + 2e-44f;
+ QTest::newRow("should FAIL 4") << 1e-39f - 1e-44f << 1e-39f - 3e-44f;
+ QTest::newRow("should PASS 4") << 1e38f + 1e33f << 1e38f + 2e33f;
+ QTest::newRow("should FAIL 5") << 1e38f - 1e33f << 1e38f - 3e33f;
+
+ nonFinite_data(zero, one);
+}
+
+void tst_float::float16Comparisons() const
+{
+ QFETCH(qfloat16, operandLeft);
+ QFETCH(qfloat16, operandRight);
+
+ QCOMPARE(operandLeft, operandRight);
+}
+
+void tst_float::float16Comparisons_data() const
+{
+ QTest::addColumn<qfloat16>("operandLeft");
+ QTest::addColumn<qfloat16>("operandRight");
QTest::newRow("should SUCCEED 1")
- << float(0)
- << float(0);
+ << qfloat16(0)
+ << qfloat16(0);
QTest::newRow("should FAIL 1")
- << float(1.00000)
- << float(3.00000);
+ << qfloat16(1.000)
+ << qfloat16(3.000);
QTest::newRow("should FAIL 2")
- << float(1.00000e-7f)
- << float(3.00000e-7f);
+ << qfloat16(1.000e-4f)
+ << qfloat16(3.000e-4f);
- // QCOMPARE for floats uses qFuzzyCompare(), which succeeds if the numbers
- // differ by no more than 1/100,000th of the smaller value. Thus
- // QCOMPARE(99998, 99999) should fail, while QCOMPARE(100001, 100002)
- // should pass.
+ // QCOMPARE for qfloat16s uses qFuzzyCompare()
QTest::newRow("should FAIL 3")
- << float(99998)
- << float(99999);
+ << qfloat16(98)
+ << qfloat16(99);
QTest::newRow("should SUCCEED 2")
- << float(100001)
- << float(100002);
+ << qfloat16(1001)
+ << qfloat16(1002);
}
void tst_float::compareFloatTests() const