Minor test fixes; minor code improvements
diff --git a/src/ieee754.c b/src/ieee754.c
index 0a36b28..4419f72 100644
--- a/src/ieee754.c
+++ b/src/ieee754.c
@@ -690,10 +690,11 @@
Result.type = IEEE754_ToInt_NO_CONVERSION;
}
} else if(nDoubleUnbiasedExponent == DOUBLE_EXPONENT_INF_OR_NAN) {
- /* --- NAN or INFINITY --- */
if(uDoubleSignificand != 0) {
- Result.type = IEEE754_To_int_NaN; /* dCBOR doesn't care about payload */
+ /* --- NAN --- */
+ Result.type = IEEE754_ToInt_NAN; /* dCBOR doesn't care about payload */
} else {
+ /* --- INIFINITY --- */
Result.type = IEEE754_ToInt_NO_CONVERSION;
}
} else if(nDoubleUnbiasedExponent < 0 ||
@@ -773,7 +774,7 @@
} else if(nSingleUnbiasedExponent == SINGLE_EXPONENT_INF_OR_NAN) {
/* --- NAN or INFINITY --- */
if(uSingleleSignificand != 0) {
- Result.type = IEEE754_To_int_NaN; /* dCBOR doesn't care about payload */
+ Result.type = IEEE754_ToInt_NAN; /* dCBOR doesn't care about payload */
} else {
Result.type = IEEE754_ToInt_NO_CONVERSION;
}
@@ -791,8 +792,7 @@
* 64-bit integers always have more precision than the 52-bits
* of a double.
*/
- nNonZeroBitsCount = IEEE754_Private_CountNonZeroBits(SINGLE_NUM_SIGNIFICAND_BITS, uSingleleSignificand);
-
+ nNonZeroBitsCount = IEEE754_Private_CountNonZeroBits(SINGLE_NUM_SIGNIFICAND_BITS, uSingleleSignificand);
if(nNonZeroBitsCount && nNonZeroBitsCount > nSingleUnbiasedExponent) {
/* --- Not a whole number --- */
@@ -801,7 +801,7 @@
/* --- CONVERTABLE WHOLE NUMBER --- */
/* Add in the one that is implied in normal floats */
uInteger = uSingleleSignificand + (1ULL << SINGLE_NUM_SIGNIFICAND_BITS);
- /* Factor in the exponent */
+ /* Factor in the exponent */
if(nSingleUnbiasedExponent < SINGLE_NUM_SIGNIFICAND_BITS) {
/* Numbers less than 2^23 with up to 23 significant bits */
uInteger >>= SINGLE_NUM_SIGNIFICAND_BITS - nSingleUnbiasedExponent;
diff --git a/src/ieee754.h b/src/ieee754.h
index dfe9989..02b8a39 100644
--- a/src/ieee754.h
+++ b/src/ieee754.h
@@ -97,7 +97,7 @@
enum {IEEE754_ToInt_IS_INT,
IEEE754_ToInt_IS_UINT,
IEEE754_ToInt_NO_CONVERSION,
- IEEE754_To_int_NaN
+ IEEE754_ToInt_NAN
} type;
union {
uint64_t un_signed;
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index a1fb737..da24c91 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -35,6 +35,10 @@
#include "qcbor/qcbor_encode.h"
#include "ieee754.h"
+#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
+#include <math.h> /* Only for NAN */
+#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
/**
* @file qcbor_encode.c
@@ -834,7 +838,6 @@
UsefulBufUtil_CopyDoubleToUint64(dNum));
}
-#include <math.h> // For NaN. Maybe a better way? TODO:
/*
* Public functions for adding a double. See qcbor/qcbor_encode.h
@@ -863,7 +866,7 @@
case IEEE754_ToInt_IS_UINT:
QCBOREncode_AddUInt64(pMe, IntResult.integer.un_signed);
return;
- case IEEE754_To_int_NaN:
+ case IEEE754_ToInt_NAN:
dNum = NAN;
bNoNaNPayload = true;
break;
@@ -932,7 +935,7 @@
case IEEE754_ToInt_IS_UINT:
QCBOREncode_AddUInt64(pMe, IntResult.integer.un_signed);
return;
- case IEEE754_To_int_NaN:
+ case IEEE754_ToInt_NAN:
fNum = NAN;
bNoNaNPayload = true;
break;
diff --git a/test/float_tests.c b/test/float_tests.c
index 3df8639..a46f090 100644
--- a/test/float_tests.c
+++ b/test/float_tests.c
@@ -397,7 +397,7 @@
FloatValuesTests(void)
{
unsigned int uTestIndex;
- const struct FloatTestCase *pTestCase;
+ const struct FloatTestCase *pTestCase;
const struct NaNTestCase *pNaNTestCase;
MakeUsefulBufOnStack( TestOutBuffer, 20);
UsefulBufC TestOutput;
@@ -414,13 +414,6 @@
for(uTestIndex = 0; FloatTestCases[uTestIndex].Preferred.len != 0; uTestIndex++) {
pTestCase = &FloatTestCases[uTestIndex];
-
- // 9223372036854774784
- if(pTestCase->dNumber == 1.7976931348623157e308 ||
- uTestIndex == 77) {
- uErr = 99; /* For setting break points for particular tests */
- }
-
/* Number Encode of Preferred */
QCBOREncode_Init(&EnCtx, TestOutBuffer);
QCBOREncode_AddDouble(&EnCtx, pTestCase->dNumber);
@@ -685,10 +678,10 @@
QCBOREncode_AddDouble(&EnCtx, UsefulBufUtil_CopyUint64ToDouble(pNaNTestCase->uDouble));
uErr = QCBOREncode_Finish(&EnCtx, &TestOutput);
if(uErr != QCBOR_SUCCESS) {
- return MakeTestResultCode(uTestIndex+100, 11, uErr);;
+ return MakeTestResultCode(uTestIndex+100, 14, uErr);;
}
if(UsefulBuf_Compare(TestOutput, pNaNTestCase->DCBOR)) {
- return MakeTestResultCode(uTestIndex+100, 11, 200);
+ return MakeTestResultCode(uTestIndex+100, 14, 200);
}
}
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index dc186f2..744b011 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -692,10 +692,12 @@
/* Improvement: this test should be broken down into several so it is more
* managable. Tags and labels could be more sensible */
QCBOREncodeContext ECtx;
- UsefulBufC Enc;
- size_t size;
- int nReturn = 0;
- QCBORError uExpectedErr;
+ UsefulBufC Enc;
+ size_t size;
+ int nReturn;
+ QCBORError uExpectedErr;
+
+ nReturn = 0;
QCBOREncode_Init(&ECtx, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
QCBOREncode_Allow(&ECtx, QCBOR_ENCODE_ALLOW_ALL);
@@ -768,11 +770,10 @@
/* 0x7ffc0000UL is a NaN with a payload. */
QCBOREncode_AddFloat(&ECtx, UsefulBufUtil_CopyUint32ToFloat(0x7ffc0000UL));
if(QCBOREncode_Finish(&ECtx, &Enc) != uExpectedErr) {
- nReturn = -24;
+ nReturn = -25;
goto Done;
}
-
Done:
return nReturn;
}
@@ -3438,7 +3439,10 @@
QCBOREncode_CloseMap(&EC);
- QCBOREncode_Finish(&EC, &Encoded);
+ uExpectedErr = QCBOREncode_Finish(&EC, &Encoded);
+ if(uExpectedErr != QCBOR_SUCCESS) {
+ return 2;
+ }
static const uint8_t spExpectedCDE[] = {
0xA6, 0x61, 0x61, 0x01, 0x61, 0x62, 0xF9, 0x7E,