Skip to content

Commit

Permalink
tst_Parser: add some testing rows for floating point data
Browse files Browse the repository at this point in the history
Signed-off-by: Thiago Macieira <[email protected]>
  • Loading branch information
thiagomacieira committed Feb 7, 2025
1 parent bad81be commit ec5b38e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/parser/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,46 @@ void addIntegers()
<< std::numeric_limits<quint64>::max() << qint64(-123456) << true << false;
}

[[maybe_unused]] void addFloatingPoint()
{
QTest::addColumn<QByteArray>("data");
QTest::addColumn<double>("expectedValue");
QTest::addColumn<CborType>("expectedType");

QTest::newRow("0.f16") << raw("\xf9\0\0") << 0. << CborHalfFloatType;
QTest::newRow("0.f") << raw("\xfa\0\0\0\0") << 0. << CborFloatType;
QTest::newRow("0.") << raw("\xfb\0\0\0\0\0\0\0\0") << 0. << CborDoubleType;
QTest::newRow("-1.f16") << raw("\xf9\xbc\x00") << -1. << CborHalfFloatType;
QTest::newRow("-1.f") << raw("\xfa\xbf\x80\0\0") << -1. << CborFloatType;
QTest::newRow("-1.") << raw("\xfb\xbf\xf0\0\0\0\0\0\0") << -1. << CborDoubleType;
QTest::newRow("65504.f16") << raw("\xf9\x7b\xff") << 65504. << CborHalfFloatType;
QTest::newRow("16777215.f") << raw("\xfa\x4b\x7f\xff\xff") << 16777215. << CborFloatType;
QTest::newRow("16777215.") << raw("\xfb\x41\x6f\xff\xff\xe0\0\0\0") << 16777215. << CborDoubleType;
QTest::newRow("-16777215.f") << raw("\xfa\xcb\x7f\xff\xff") << -16777215. << CborFloatType;
QTest::newRow("-16777215.") << raw("\xfb\xc1\x6f\xff\xff\xe0\0\0\0") << -16777215. << CborDoubleType;

QTest::newRow("0.5f16") << raw("\xf9\x38\0") << 0.5 << CborHalfFloatType;
QTest::newRow("0.5f") << raw("\xfa\x3f\0\0\0") << 0.5 << CborFloatType;
QTest::newRow("0.5") << raw("\xfb\x3f\xe0\0\0\0\0\0\0") << 0.5 << CborDoubleType;
QTest::newRow("2.f16^11-1") << raw("\xf9\x67\xff") << 2047. << CborHalfFloatType;
QTest::newRow("2.f^24-1") << raw("\xfa\x4b\x7f\xff\xff") << 16777215. << CborFloatType;
QTest::newRow("2.^53-1") << raw("\xfb\x43\x3f\xff\xff""\xff\xff\xff\xff") << double(Q_INT64_C(1) << 53) - 1 << CborDoubleType;
QTest::newRow("2.f^64-epsilon") << raw("\xfa\x5f\x7f\xff\xff") << 0x1.fffffep+63 << CborFloatType;
QTest::newRow("2.^64-epsilon") << raw("\xfb\x43\xef\xff\xff""\xff\xff\xff\xff") << 0x1.fffffffffffffp+63 << CborDoubleType;
QTest::newRow("2.f^64") << raw("\xfa\x5f\x80\0\0") << 0x1p64 << CborFloatType;
QTest::newRow("2.^64") << raw("\xfb\x43\xf0\0\0\0\0\0\0") << 0x1p64 << CborDoubleType;

QTest::newRow("nan_f16") << raw("\xf9\x7e\x00") << qQNaN() << CborHalfFloatType;
QTest::newRow("nan_f") << raw("\xfa\x7f\xc0\0\0") << qQNaN() << CborFloatType;
QTest::newRow("nan") << raw("\xfb\x7f\xf8\0\0\0\0\0\0") << qQNaN() << CborDoubleType;
QTest::newRow("-inf_f16") << raw("\xf9\xfc\x00") << -qInf() << CborHalfFloatType;
QTest::newRow("-inf_f") << raw("\xfa\xff\x80\0\0") << -qInf() << CborFloatType;
QTest::newRow("-inf") << raw("\xfb\xff\xf0\0\0\0\0\0\0") << -qInf() << CborDoubleType;
QTest::newRow("+inf_f16") << raw("\xf9\x7c\x00") << qInf() << CborHalfFloatType;
QTest::newRow("+inf_f") << raw("\xfa\x7f\x80\0\0") << qInf() << CborFloatType;
QTest::newRow("+inf") << raw("\xfb\x7f\xf0\0\0\0\0\0\0") << qInf() << CborDoubleType;
}

void addColumns()
{
QTest::addColumn<QByteArray>("data");
Expand Down
44 changes: 44 additions & 0 deletions tests/parser/tst_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
# include <windows.h>
#endif

#ifndef QCOMPARE_EQ
// added for Qt 6.4
# define QCOMPARE_EQ QCOMPARE
#endif

namespace QTest {
template<> char *toString<CborError>(const CborError &err)
Expand All @@ -57,6 +61,8 @@ private slots:
void integers();
void halfFloat_data();
void halfFloat();
void floatingPoint_data();
void floatingPoint();
void fixed_data();
void fixed();
void strings_data();
Expand Down Expand Up @@ -451,6 +457,44 @@ void tst_Parser::halfFloat()
}
}

void tst_Parser::floatingPoint_data()
{
addFloatingPoint();
}

void tst_Parser::floatingPoint()
{
QFETCH(QByteArray, data);
QFETCH(CborType, expectedType);
QFETCH(double, expectedValue);
bool isNaN = std::isnan(expectedValue);

ParserWrapper w;
CborError err = w.init(data);
QVERIFY2(!err, QByteArray("Got error \"") + cbor_error_string(err) + "\"");
QCOMPARE(cbor_value_get_type(&w.first), expectedType);

float f;
double d;
if (expectedType == CborHalfFloatType) {
QVERIFY(cbor_value_is_half_float(&w.first));
QCOMPARE(cbor_value_get_half_float_as_float(&w.first, &f), CborNoError);
}
if (expectedType == CborFloatType) {
QVERIFY(cbor_value_is_float(&w.first));
QCOMPARE(cbor_value_get_float(&w.first, &f), CborNoError);
}
if (expectedType == CborDoubleType) {
QVERIFY(cbor_value_is_double(&w.first));
QCOMPARE(cbor_value_get_double(&w.first, &d), CborNoError);
} else {
d = f;
}
QCOMPARE(std::isnan(d), isNaN);
if (!isNaN)
QCOMPARE_EQ(d, expectedValue);
}

void tst_Parser::fixed_data()
{
addColumns();
Expand Down

0 comments on commit ec5b38e

Please sign in to comment.