From d720fa36ad4768ed1b948a92ba5287c30093fbec Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 26 Jul 2015 18:33:15 +0200 Subject: o Overhaul of the bluetooth code. - Adding support for reading FLOAT (specified in IEEE 11073-20601) values from a bluetooth device. - More shared pointers to help keep track of the object's lifecycle. Makes sure that the connections are released back to Linux, Linux is way to sensitive with crashing applications. o Adding support for reading the temperature sensors from the SoilMoisture device. --- test/ByteBufferTest.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'test') 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 #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); +} -- cgit v1.2.3