diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/ByteBufferTest.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/ByteBufferTest.cpp b/test/ByteBufferTest.cpp index 5aad376..8659f64 100644 --- a/test/ByteBufferTest.cpp +++ b/test/ByteBufferTest.cpp @@ -1,4 +1,5 @@ #include "ble/ByteBuffer.h" +#include <iomanip> #define BOOST_TEST_MODULE "ByteBuffer" @@ -89,3 +90,64 @@ BOOST_AUTO_TEST_CASE(view) { BOOST_CHECK_EQUAL(view1.read8(), 3); } + +BOOST_AUTO_TEST_CASE(ieee_11073_20601_float_1_0d) { + Bytes b(1000); + ByteBuffer buffer(b.bytes, b.capacity); + + buffer.writeFLOAT(1.0); + buffer.setCursor(0); + BOOST_CHECK_EQUAL(buffer.readFLOAT(), 1.0); +} + +BOOST_AUTO_TEST_CASE(ieee_11073_20601_float_nan) { + Bytes b(1000); + ByteBuffer buffer(b.bytes, b.capacity); + + BOOST_CHECK_EQUAL(std::isnan(stof("NaN")), true); + buffer.writeFLOAT(stof("NaN")); + buffer.setCursor(0); + BOOST_CHECK_EQUAL(std::isnan(buffer.readFLOAT()), true); +} + +void c(double input, uint32_t hx, double sigma = 0.00001) { + Bytes b(1000); + ByteBuffer buffer(b.bytes, b.capacity); + buffer.writeFLOAT(input); + buffer.setCursor(0); + double output = buffer.readFLOAT(); + + stringstream str; + auto actual_hex = ((uint32_t *) b.bytes.get())[0]; + str << "input=" << setw(20) << setprecision(10) << input << + ", output=" << setw(20) << setprecision(10) << output << + ", diff=" << setw(20) << setprecision(10) << fabs(input - output) << + ", expected=" << setw(8) << setfill('0') << hex << hx << + ", actual=" << setw(8) << setfill('0') << hex << actual_hex; + + if (std::isnan(input) || std::isnan(output)) { + BOOST_CHECK_EQUAL(std::isnan(input), std::isnan(output)); + } else if (input >= FLOAT::max) { + BOOST_CHECK_EQUAL(INFINITY, output); + } else if (input <= FLOAT::min) { + BOOST_CHECK_EQUAL(-INFINITY, output); + } else { + BOOST_CHECK_MESSAGE(fabs(input - output) < sigma, str.str()); + } +} + +BOOST_AUTO_TEST_CASE(ieee_11073_20601_float_lots) { + c(36.4, 0xFF00016C); + c(1, 0xFF000270); + c(62.4, 0xFF000270); + c(25.8, 0xFF000102); + c(-0.2, 0xFFFFFFFE); + c(0.03, 0xFE000003); + c(15000000.0, 0x0116e360); + c(15000000.1, 0x0116e360, 0.11); + c(1500000.1, 0x0016e360, 0.11); + c(1499999.99, 0x0016e360, 0.02); + c(NAN, 0x007FFFFF); + c(1e300, 0x007FFFFE); + c(-1e300, 0x00800002); +} |