From 06c898d7a994cc1c4193760cf348a3e69a741847 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 16 Nov 2020 13:18:12 +0100 Subject: Add some tests for various qmath.h functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously untested. Task-number: QTBUG-88183 Change-Id: I9e94308471651e447f35a21329e43a363fb23961 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Andrei Golubev Reviewed-by: Thiago Macieira --- tests/auto/corelib/kernel/qmath/tst_qmath.cpp | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/corelib/kernel/qmath/tst_qmath.cpp b/tests/auto/corelib/kernel/qmath/tst_qmath.cpp index feb704e0aa..e8855caf88 100644 --- a/tests/auto/corelib/kernel/qmath/tst_qmath.cpp +++ b/tests/auto/corelib/kernel/qmath/tst_qmath.cpp @@ -39,6 +39,10 @@ private slots: void degreesToRadians(); void radiansToDegrees_data(); void radiansToDegrees(); + void trigonometry_data(); + void trigonometry(); + void funcs_data(); + void funcs(); void qNextPowerOfTwo32S_data(); void qNextPowerOfTwo32S(); void qNextPowerOfTwo64S_data(); @@ -131,6 +135,70 @@ void tst_QMath::radiansToDegrees() QCOMPARE(qRadiansToDegrees(radiansDouble), degreesDouble); } +void tst_QMath::trigonometry_data() +{ + QTest::addColumn("x"); + QTest::addColumn("y"); + QTest::addColumn("angle"); + + QTest::newRow("zero") << 1.0 << 0.0 << 0.0; + QTest::newRow("turn/4") << 0.0 << 1.0 << M_PI_2; + QTest::newRow("turn/2") << -1.0 << 0.0 << M_PI; + QTest::newRow("3*turn/4") << 0.0 << -1.0 << -M_PI_2; +} + +void tst_QMath::trigonometry() +{ + QFETCH(const double, x); + QFETCH(const double, y); + QFETCH(const double, angle); + const double hypot = std::hypot(x, y); + QVERIFY(hypot > 0); + QCOMPARE(qAtan2(y, x), angle); + QCOMPARE(qSin(angle), y / hypot); + if (x >= 0 && (y || x)) // aSin() always in right half-plane + QCOMPARE(qAsin(y / hypot), angle); + QCOMPARE(qCos(angle), x / hypot); + if (y >= 0 && (y || x)) // aCos() always in upper half-plane + QCOMPARE(qAcos(x / hypot), angle); + if (x > 0) { + QCOMPARE(qTan(angle), y / x); + QCOMPARE(qAtan(y / x), angle); + } +} + +void tst_QMath::funcs_data() +{ + QTest::addColumn("value"); + QTest::addColumn("floor"); + QTest::addColumn("ceil"); + QTest::addColumn("abs"); + QTest::addColumn("sqrt"); + QTest::addColumn("log"); + QTest::addColumn("exp"); + QTest::addColumn("cube"); + const double nan = qQNaN(); + + QTest::newRow("0") << 0.0 << 0 << 0 << 0.0 << 0.0 << nan << 1.0 << 0.0; + QTest::newRow("1.44") + << 1.44 << 1 << 2 << 1.44 << 1.2 << 0.36464311358790924 << 4.220695816996552 << 2.985984; + QTest::newRow("-1.44") + << -1.44 << -2 << -1 << 1.44 << nan << nan << 0.23692775868212176 << -2.985984; +} + +void tst_QMath::funcs() +{ + QFETCH(double, value); + QTEST(qFloor(value), "floor"); + QTEST(qCeil(value), "ceil"); + QTEST(qFabs(value), "abs"); + if (value >= 0) + QTEST(qSqrt(value), "sqrt"); + if (value > 0) + QTEST(qLn(value), "log"); + QTEST(qExp(value), "exp"); + QTEST(qPow(value, 3), "cube"); +} void tst_QMath::qNextPowerOfTwo32S_data() { -- cgit v1.2.3