Add test for qRound
Add test for qRound that covers some edge cases for rounding. Note that as of right now, this test fails and the docs have been updated to warn that it should not be depended on for strict correctness. Change-Id: I1a61bca47abd77855fe7c13ded44e913cc7e8722 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
parent
c3ffa6036b
commit
3ca921293a
@ -968,6 +968,8 @@ static_assert((std::is_same<qsizetype, qptrdiff>::value));
|
||||
|
||||
Rounds half away from zero (e.g. 0.5 -> 1, -0.5 -> -1).
|
||||
|
||||
\note This function does not guarantee correctness for high precisions.
|
||||
|
||||
Example:
|
||||
|
||||
\snippet code/src_corelib_global_qglobal.cpp 11A
|
||||
@ -980,6 +982,8 @@ static_assert((std::is_same<qsizetype, qptrdiff>::value));
|
||||
|
||||
Rounds half away from zero (e.g. 0.5f -> 1, -0.5f -> -1).
|
||||
|
||||
\note This function does not guarantee correctness for high precisions.
|
||||
|
||||
Example:
|
||||
|
||||
\snippet code/src_corelib_global_qglobal.cpp 11B
|
||||
@ -992,6 +996,8 @@ static_assert((std::is_same<qsizetype, qptrdiff>::value));
|
||||
|
||||
Rounds half away from zero (e.g. 0.5 -> 1, -0.5 -> -1).
|
||||
|
||||
\note This function does not guarantee correctness for high precisions.
|
||||
|
||||
Example:
|
||||
|
||||
\snippet code/src_corelib_global_qglobal.cpp 12A
|
||||
@ -1004,6 +1010,8 @@ static_assert((std::is_same<qsizetype, qptrdiff>::value));
|
||||
|
||||
Rounds half away from zero (e.g. 0.5f -> 1, -0.5f -> -1).
|
||||
|
||||
\note This function does not guarantee correctness for high precisions.
|
||||
|
||||
Example:
|
||||
|
||||
\snippet code/src_corelib_global_qglobal.cpp 12B
|
||||
|
@ -33,6 +33,8 @@
|
||||
#include <QSysInfo>
|
||||
#include <QLatin1String>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
class tst_QGlobal: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -52,6 +54,10 @@ private slots:
|
||||
void buildAbiEndianness();
|
||||
void testqOverload();
|
||||
void testqMinMax();
|
||||
void qRoundFloats_data();
|
||||
void qRoundFloats();
|
||||
void qRoundDoubles_data();
|
||||
void qRoundDoubles();
|
||||
};
|
||||
|
||||
extern "C" { // functions in qglobal.c
|
||||
@ -602,6 +608,56 @@ void tst_QGlobal::testqMinMax()
|
||||
compare(qMax(quint64(1), ushort(2)), quint64(2));
|
||||
}
|
||||
|
||||
void tst_QGlobal::qRoundFloats_data()
|
||||
{
|
||||
QTest::addColumn<float>("actual");
|
||||
QTest::addColumn<float>("expected");
|
||||
|
||||
QTest::newRow("round half") << 0.5f << 1.0f;
|
||||
QTest::newRow("round negative half") << -0.5f << -1.0f;
|
||||
QTest::newRow("round negative") << -1.4f << -1.0f;
|
||||
QTest::newRow("round largest representable float less than 0.5") << std::nextafter(0.5f, 0.0f) << 0.0f;
|
||||
}
|
||||
|
||||
void tst_QGlobal::qRoundFloats() {
|
||||
QFETCH(float, actual);
|
||||
QFETCH(float, expected);
|
||||
|
||||
QEXPECT_FAIL("round largest representable float less than 0.5",
|
||||
"We know qRound fails in this case, but decided that we value simplicity over correctness",
|
||||
Continue);
|
||||
QCOMPARE(qRound(actual), expected);
|
||||
|
||||
QEXPECT_FAIL("round largest representable float less than 0.5",
|
||||
"We know qRound fails in this case, but decided that we value simplicity over correctness",
|
||||
Continue);
|
||||
QCOMPARE(qRound64(actual), expected);
|
||||
}
|
||||
|
||||
void tst_QGlobal::qRoundDoubles_data() {
|
||||
QTest::addColumn<double>("actual");
|
||||
QTest::addColumn<double>("expected");
|
||||
|
||||
QTest::newRow("round half") << 0.5 << 1.0;
|
||||
QTest::newRow("round negative half") << -0.5 << -1.0;
|
||||
QTest::newRow("round negative") << -1.4 << -1.0;
|
||||
QTest::newRow("round largest representable double less than 0.5") << std::nextafter(0.5, 0.0) << 0.0;
|
||||
}
|
||||
|
||||
void tst_QGlobal::qRoundDoubles() {
|
||||
QFETCH(double, actual);
|
||||
QFETCH(double, expected);
|
||||
|
||||
QEXPECT_FAIL("round largest representable double less than 0.5",
|
||||
"We know qRound fails in this case, but decided that we value simplicity over correctness",
|
||||
Continue);
|
||||
QCOMPARE(qRound(actual), expected);
|
||||
|
||||
QEXPECT_FAIL("round largest representable double less than 0.5",
|
||||
"We know qRound fails in this case, but decided that we value simplicity over correctness",
|
||||
Continue);
|
||||
QCOMPARE(qRound64(actual), expected);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QGlobal)
|
||||
#include "tst_qglobal.moc"
|
||||
|
Loading…
Reference in New Issue
Block a user