aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-07-26 18:33:15 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-07-26 18:41:40 +0200
commitd720fa36ad4768ed1b948a92ba5287c30093fbec (patch)
tree3d566e0d4ab47981af85a783f81ebbd363d57f15 /test
parent33c537c84fea53c899fb5275256518598f66101e (diff)
downloadble-toys-d720fa36ad4768ed1b948a92ba5287c30093fbec.tar.gz
ble-toys-d720fa36ad4768ed1b948a92ba5287c30093fbec.tar.bz2
ble-toys-d720fa36ad4768ed1b948a92ba5287c30093fbec.tar.xz
ble-toys-d720fa36ad4768ed1b948a92ba5287c30093fbec.zip
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.
Diffstat (limited to 'test')
-rw-r--r--test/ByteBufferTest.cpp62
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);
+}