Clean up some floating point tests TODO:s
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 02fecd2..35aa385 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -1205,7 +1205,6 @@
pDecodedItem->val.epochDate.nSeconds = (int64_t)pDecodedItem->val.uint64;
break;
- // TODO: test this with float and half input
case QCBOR_TYPE_DOUBLE:
case QCBOR_TYPE_FLOAT:
#ifndef QCBOR_DISABLE_FLOAT_HW_USE
diff --git a/test/float_tests.c b/test/float_tests.c
index 735a55d..8886ab8 100644
--- a/test/float_tests.c
+++ b/test/float_tests.c
@@ -21,49 +21,69 @@
#include "half_to_double_from_rfc7049.h"
-
+// A series of half precision values to test half-precision decoding
+/*
+ As decoded by http://cbor.me
+ {"zero": 0.0,
+ "infinitity": Infinity,
+ "negative infinitity": -Infinity,
+ "NaN": NaN,
+ "one": 1.0,
+ "one third": 0.333251953125,
+ "largest half-precision": 65504.0,
+ "too-large half-precision": Infinity,
+ "smallest subnormal": 5.960464477539063e-8,
+ "smallest normal": 0.00006097555160522461,
+ "biggest subnormal": 0.00006103515625,
+ "subnormal single": 0.0,
+ 3: -2.0,
+ 4: NaN,
+ 5: NaN,
+ 6: NaN,
+ 7: NaN}
+ */
static const uint8_t spExpectedHalf[] = {
0xB1,
0x64,
0x7A, 0x65, 0x72, 0x6F,
- 0xF9, 0x00, 0x00, // 0.000
+ 0xF9, 0x00, 0x00, // half-precision 0.000
0x6A,
0x69, 0x6E, 0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79,
- 0xF9, 0x7C, 0x00, // Infinity
+ 0xF9, 0x7C, 0x00, // Infinity
0x73,
0x6E, 0x65, 0x67, 0x61, 0x74, 0x69, 0x76, 0x65, 0x20, 0x69, 0x6E,
0x66, 0x69, 0x6E, 0x69, 0x74, 0x69, 0x74, 0x79,
- 0xF9, 0xFC, 0x00, // -Inifinity
+ 0xF9, 0xFC, 0x00, // -Inifinity
0x63,
0x4E, 0x61, 0x4E,
- 0xF9, 0x7E, 0x00, // NaN
+ 0xF9, 0x7E, 0x00, // NaN
0x63,
0x6F, 0x6E, 0x65,
- 0xF9, 0x3C, 0x00, // 1.0
+ 0xF9, 0x3C, 0x00, // 1.0
0x69,
0x6F, 0x6E, 0x65, 0x20, 0x74, 0x68, 0x69, 0x72, 0x64,
- 0xF9, 0x35, 0x55, // 0.333251953125
+ 0xF9, 0x35, 0x55, // half-precsion one third 0.333251953125
0x76,
0x6C, 0x61, 0x72, 0x67, 0x65, 0x73, 0x74, 0x20, 0x68, 0x61, 0x6C,
0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69, 0x6F, 0x6E,
- 0xF9, 0x7B, 0xFF, // 65504.0
+ 0xF9, 0x7B, 0xFF, // largest half-precision 65504.0
0x78, 0x18,
0x74, 0x6F, 0x6F, 0x2D, 0x6C, 0x61, 0x72, 0x67, 0x65, 0x20, 0x68,
0x61, 0x6C, 0x66, 0x2D, 0x70, 0x72, 0x65, 0x63, 0x69, 0x73, 0x69,
0x6F, 0x6E,
- 0xF9, 0x7C, 0x00, // Infinity
+ 0xF9, 0x7C, 0x00, // Infinity
0x72,
0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75,
0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
- 0xF9, 0x00, 0x01, // 0.000000059604
- 0x6F,
- 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F,
- 0x72, 0x6D, 0x61, 0x6C,
- 0xF9, 0x03, 0xFF, // 0.0000609755516
+ 0xF9, 0x00, 0x01, // Smallest half-precision subnormal 0.000000059604645
0x71,
0x62, 0x69, 0x67, 0x67, 0x65, 0x73, 0x74, 0x20, 0x73, 0x75, 0x62,
0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C,
- 0xF9, 0x04, 0x00, // 0.000061988
+ 0xF9, 0x03, 0xFF, // Largest half-precision subnormal 0.0000609755516
+ 0x6F,
+ 0x73, 0x6D, 0x61, 0x6C, 0x6C, 0x65, 0x73, 0x74, 0x20, 0x6E, 0x6F,
+ 0x72, 0x6D, 0x61, 0x6C,
+ 0xF9, 0x04, 0x00, // Smallest half-precision normal 0.000061988
0x70,
0x73, 0x75, 0x62, 0x6E, 0x6F, 0x72, 0x6D, 0x61, 0x6C, 0x20, 0x73,
0x69, 0x6E, 0x67, 0x6C, 0x65,
@@ -78,118 +98,124 @@
0xF9, 0x7E, 0x0F, // qNaN with payload 0x0f
0x07,
0xF9, 0x7C, 0x0F, // sNaN with payload 0x0f
-
};
int32_t HalfPrecisionDecodeBasicTests()
{
- UsefulBufC HalfPrecision = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedHalf);
+ UsefulBufC HalfPrecision = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedHalf);
- QCBORDecodeContext DC;
- QCBORDecode_Init(&DC, HalfPrecision, 0);
+ QCBORDecodeContext DC;
+ QCBORDecode_Init(&DC, HalfPrecision, 0);
- QCBORItem Item;
+ QCBORItem Item;
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_MAP) {
- return -1;
- }
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_MAP) {
+ return -1;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0F) {
- return -2;
- }
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0) {
+ return -2;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) {
- return -3;
- }
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) {
+ return -3;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -INFINITY) {
- return -4;
- }
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -INFINITY) {
+ return -4;
+ }
- // TODO, is this really converting right? It is carrying payload, but
- // this confuses things.
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || !isnan(Item.val.dfnum)) {
- return -5;
- }
+ // TODO: NAN-related is this really converting right? It is carrying payload, but
+ // this confuses things.
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || !isnan(Item.val.dfnum)) {
+ return -5;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 1.0F) {
- return -6;
- }
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 1.0) {
+ return -6;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.333251953125F) {
- return -7;
- }
+ // Approximately 1/3
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.333251953125) {
+ return -7;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 65504.0F) {
- return -8;
- }
+ // Largest half-precision
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 65504.0) {
+ return -8;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) {
- return -9;
- }
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != INFINITY) {
+ return -9;
+ }
- QCBORDecode_GetNext(&DC, &Item); // TODO: check this
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000000596046448F) {
- return -10;
- }
+ // Smallest half-precision subnormal
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.00000005960464477539063) {
+ return -10;
+ }
- QCBORDecode_GetNext(&DC, &Item); // TODO: check this
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000609755516F) {
- return -11;
- }
+ // Largest half-precision subnormal
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.00006097555160522461) {
+ return -11;
+ }
- QCBORDecode_GetNext(&DC, &Item); // TODO check this
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0000610351563F) {
- return -12;
- }
+ // Smallest half-precision normal
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.00006103515625) {
+ return -12;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0) {
- return -13;
- }
+ // half-precision zero
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != 0.0) {
+ return -13;
+ }
- QCBORDecode_GetNext(&DC, &Item);
- if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -2.0F) {
- return -14;
- }
+ // negative 2
+ QCBORDecode_GetNext(&DC, &Item);
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE || Item.val.dfnum != -2.0) {
+ return -14;
+ }
- // TODO: double check these four tests
- QCBORDecode_GetNext(&DC, &Item); // qNaN
- if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
- UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff8000000000000ULL) {
- return -15;
- }
- QCBORDecode_GetNext(&DC, &Item); // sNaN
- if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
- UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff0000000000001ULL) {
- return -16;
- }
- QCBORDecode_GetNext(&DC, &Item); // qNaN with payload 0x0f
- if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
- UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff800000000000fULL) {
- return -17;
- }
- QCBORDecode_GetNext(&DC, &Item); // sNaN with payload 0x0f
- if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
- UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff000000000000fULL) {
- return -18;
- }
+ // TODO: NAN-related double check these four tests
+ QCBORDecode_GetNext(&DC, &Item); // qNaN
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
+ UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff8000000000000ULL) {
+ return -15;
+ }
+ QCBORDecode_GetNext(&DC, &Item); // sNaN
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
+ UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff0000000000001ULL) {
+ return -16;
+ }
+ QCBORDecode_GetNext(&DC, &Item); // qNaN with payload 0x0f
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
+ UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff800000000000fULL) {
+ return -17;
+ }
+ QCBORDecode_GetNext(&DC, &Item); // sNaN with payload 0x0f
+ if(Item.uDataType != QCBOR_TYPE_DOUBLE ||
+ UsefulBufUtil_CopyDoubleToUint64(Item.val.dfnum) != 0x7ff000000000000fULL) {
+ return -18;
+ }
- if(QCBORDecode_Finish(&DC)) {
- return -19;
- }
+ if(QCBORDecode_Finish(&DC)) {
+ return -19;
+ }
- return 0;
+ return 0;
}
@@ -316,8 +342,7 @@
{
UsefulBuf_MAKE_STACK_UB(EncodedHalfsMem, 420);
-#define QCBOREncode_AddDoubleAsSmallestToMap QCBOREncode_AddDoubleToMap
-#define QCBOREncode_AddDoubleAsSmallestToMapN QCBOREncode_AddDoubleToMapN
+#define QCBOREncode_AddDoubleToMap QCBOREncode_AddDoubleToMap
QCBOREncodeContext EC;
@@ -327,63 +352,63 @@
// 64 # text(4)
// 7A65726F # "zero"
// F9 0000 # primitive(0)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "zero", 0.00);
+ QCBOREncode_AddDoubleToMap(&EC, "zero", 0.00);
// 64 # text(4)
// 7A65726F # "negative zero"
// F9 8000 # primitive(0)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "negative zero", -0.00);
+ QCBOREncode_AddDoubleToMap(&EC, "negative zero", -0.00);
// 6A # text(10)
// 696E66696E6974697479 # "infinitity"
// F9 7C00 # primitive(31744)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "infinitity", INFINITY);
+ QCBOREncode_AddDoubleToMap(&EC, "infinitity", INFINITY);
// 73 # text(19)
// 6E6567617469766520696E66696E6974697479 # "negative infinitity"
// F9 FC00 # primitive(64512)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "negative infinitity", -INFINITY);
+ QCBOREncode_AddDoubleToMap(&EC, "negative infinitity", -INFINITY);
// 63 # text(3)
// 4E614E # "NaN"
// F9 7E00 # primitive(32256)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "NaN", NAN);
+ QCBOREncode_AddDoubleToMap(&EC, "NaN", NAN);
// TODO: test a few NaN variants
// 63 # text(3)
// 6F6E65 # "one"
// F9 3C00 # primitive(15360)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "one", 1.0);
+ QCBOREncode_AddDoubleToMap(&EC, "one", 1.0);
// 69 # text(9)
// 6F6E65207468697264 # "one third"
// F9 3555 # primitive(13653)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "one third", 0.333251953125);
+ QCBOREncode_AddDoubleToMap(&EC, "one third", 0.333251953125);
// 76 # text(22)
// 6C6172676573742068616C662D707265636973696F6E # "largest half-precision"
// F9 7BFF # primitive(31743)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "largest half-precision",65504.0);
+ QCBOREncode_AddDoubleToMap(&EC, "largest half-precision",65504.0);
// 76 # text(22)
// 6C6172676573742068616C662D707265636973696F6E # "largest half-precision"
// F9 7BFF # primitive(31743)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "largest half-precision point one",65504.1);
+ QCBOREncode_AddDoubleToMap(&EC, "largest half-precision point one",65504.1);
// Float 65536.0F is 0x47800000 in hex. It has an exponent of 16, which
// is larger than 15, the largest half-precision exponent
// 78 18 # text(24)
// 746F6F2D6C617267652068616C662D707265636973696F6E # "too-large half-precision"
// FA 47800000 # primitive(31743)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "too-large half-precision", 65536.0);
+ QCBOREncode_AddDoubleToMap(&EC, "too-large half-precision", 65536.0);
// The smallest possible half-precision subnormal, but digitis are lost converting
// to half, so this turns into a double
// 72 # text(18)
// 736D616C6C657374207375626E6F726D616C # "smallest subnormal"
// FB 3E700000001C5F68 # primitive(4499096027744984936)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "smallest subnormal", 0.0000000596046448);
+ QCBOREncode_AddDoubleToMap(&EC, "smallest subnormal", 0.0000000596046448);
// The smallest possible half-precision snormal, but digitis are lost converting
// to half, so this turns into a single TODO: confirm this is right
@@ -391,47 +416,47 @@
// 736D616C6C657374206E6F726D616C # "smallest normal"
// FA 387FFFFF # primitive(947912703)
// in hex single is 0x387fffff, exponent -15, significand 7fffff
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "smallest normal", 0.0000610351526F);
+ QCBOREncode_AddDoubleToMap(&EC, "smallest normal", 0.0000610351526F);
// 71 # text(17)
// 62696767657374207375626E6F726D616C # "biggest subnormal"
// F9 0400 # primitive(1024)
// in hex single is 0x38800000, exponent -14, significand 0
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "biggest subnormal", 0.0000610351563F);
+ QCBOREncode_AddDoubleToMap(&EC, "biggest subnormal", 0.0000610351563F);
// 70 # text(16)
// 7375626E6F726D616C2073696E676C65 # "subnormal single"
// FB 37C16C2800000000 # primitive(4017611261645684736)
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "subnormal single", 4e-40F);
+ QCBOREncode_AddDoubleToMap(&EC, "subnormal single", 4e-40F);
// 03 # unsigned(3)
// F9 C000 # primitive(49152)
- QCBOREncode_AddDoubleAsSmallestToMapN(&EC, 3, -2.0);
+ QCBOREncode_AddDoubleToMapN(&EC, 3, -2.0);
// 70 # text(16)
// 6C617267652073696E676C6520657870 # "large single exp"
// FA 7F400000 # primitive(2134900736)
// (0x01LL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) | ((127LL + DOUBLE_EXPONENT_BIAS) << DOUBLE_EXPONENT_SHIFT);
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "large single exp", 2.5521177519070385E+38); // Exponent fits single
+ QCBOREncode_AddDoubleToMap(&EC, "large single exp", 2.5521177519070385E+38); // Exponent fits single
// 74 # text(20)
// 746F6F2D6C617267652073696E676C6520657870 # "too-large single exp"
// FB 47F8000000000000 # primitive(5185894970917126144)
// (0x01LL << (DOUBLE_NUM_SIGNIFICAND_BITS-1)) | ((128LL + DOUBLE_EXPONENT_BIAS) << DOUBLE_EXPONENT_SHIFT);
// Exponent too large for single
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "too-large single exp", 5.104235503814077E+38);
+ QCBOREncode_AddDoubleToMap(&EC, "too-large single exp", 5.104235503814077E+38);
// 66 # text(6)
// 646664666465 # "dfdfde"
// FA 4B800000 # primitive(1266679808)
// Single with no precision loss
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "biggest single with prec", 16777216);
+ QCBOREncode_AddDoubleToMap(&EC, "biggest single with prec", 16777216);
// 78 18 # text(24)
// 626967676573742073696E676C6520776974682070726563 # "biggest single with prec"
// FA 4B800000 # primitive(1266679808)
// Double becuase of precision loss
- QCBOREncode_AddDoubleAsSmallestToMap(&EC, "first single with prec loss", 16777217);
+ QCBOREncode_AddDoubleToMap(&EC, "first single with prec loss", 16777217);
// Just a convenient marker when cutting and pasting encoded CBOR
QCBOREncode_AddSZStringToMapN(&EC, 1, "fin");