Skip to content

Commit 345ffce

Browse files
Add support for more half-float values
Since Qt 5.9 has qfloat16, it became easier to write values. There was no point with just the stand-in type, since the encoder would write exactly the bytes we gave it. Signed-off-by: Thiago Macieira <[email protected]>
1 parent b087939 commit 345ffce

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

tests/encoder/tst_encoder.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <QtTest>
2626
#include "cbor.h"
2727

28+
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
29+
#include <qfloat16.h>
30+
#endif
31+
2832
Q_DECLARE_METATYPE(CborError)
2933

3034
class tst_Encoder : public QObject
@@ -201,8 +205,13 @@ CborError encodeVariant(CborEncoder *encoder, const QVariant &v)
201205
default:
202206
if (type == qMetaTypeId<SimpleType>())
203207
return cbor_encode_simple_value(encoder, v.value<SimpleType>().type);
208+
#if QT_VERSION < QT_VERSION_CHECK(5, 9, 0)
204209
if (type == qMetaTypeId<Float16Standin>())
205210
return cbor_encode_half_float(encoder, v.constData());
211+
#else
212+
if (type == qMetaTypeId<qfloat16>())
213+
return cbor_encode_half_float(encoder, v.constData());
214+
#endif
206215
if (type == qMetaTypeId<Tag>()) {
207216
CborError err = cbor_encode_tag(encoder, v.value<Tag>().tag);
208217
if (err && !isOomError(err))
@@ -328,7 +337,16 @@ void addFixedData()
328337
QTest::newRow("simple255") << raw("\xf8\xff") << QVariant::fromValue(SimpleType{255});
329338

330339
// floating point
331-
QTest::newRow("0f16") << raw("\xf9\0\0") << QVariant::fromValue(Float16Standin{0x0000});
340+
#if QT_VERSION < QT_VERSION_CHECK(5, 9, 0)
341+
QTest::newRow("0.f16") << raw("\xf9\0\0") << QVariant::fromValue(Float16Standin{0x0000});
342+
#else
343+
QTest::newRow("0.f16") << raw("\xf9\0\0") << QVariant::fromValue(qfloat16(0));
344+
QTest::newRow("-1.f16") << raw("\xf9\xbc\0") << QVariant::fromValue(qfloat16(-1));
345+
QTest::newRow("1.5f16") << raw("\xf9\x3e\0") << QVariant::fromValue(qfloat16(1.5));
346+
QTest::newRow("nan_f16") << raw("\xf9\x7e\0") << QVariant::fromValue<qfloat16>(myNaNf());
347+
QTest::newRow("-inf_f16") << raw("\xf9\xfc\0") << QVariant::fromValue<qfloat16>(myNInff());
348+
QTest::newRow("+inf_f16") << raw("\xf9\x7c\0") << QVariant::fromValue<qfloat16>(myInff());
349+
#endif
332350

333351
QTest::newRow("0.f") << raw("\xfa\0\0\0\0") << QVariant::fromValue(0.f);
334352
QTest::newRow("0.") << raw("\xfb\0\0\0\0\0\0\0\0") << QVariant(0.);

0 commit comments

Comments
 (0)