aboutsummaryrefslogtreecommitdiff
path: root/sensor/test/SampleTest.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-03-23 00:14:56 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-03-23 00:14:56 +0100
commitf110a0912efb7245d6d548aa8a5ac0c89bcd9dc0 (patch)
tree5e9be9b2b2eda5082089a42570c5b88b65647f8b /sensor/test/SampleTest.cpp
parent52d0fe67f8998b215a47ebc697600cae49a7b17a (diff)
downloadble-toys-f110a0912efb7245d6d548aa8a5ac0c89bcd9dc0.tar.gz
ble-toys-f110a0912efb7245d6d548aa8a5ac0c89bcd9dc0.tar.bz2
ble-toys-f110a0912efb7245d6d548aa8a5ac0c89bcd9dc0.tar.xz
ble-toys-f110a0912efb7245d6d548aa8a5ac0c89bcd9dc0.zip
o Replacing regex based parsing with simpler and more correct tokenizing.
o Flushing output after each sample. o Adding back tests.
Diffstat (limited to 'sensor/test/SampleTest.cpp')
-rw-r--r--sensor/test/SampleTest.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/sensor/test/SampleTest.cpp b/sensor/test/SampleTest.cpp
new file mode 100644
index 0000000..2404500
--- /dev/null
+++ b/sensor/test/SampleTest.cpp
@@ -0,0 +1,76 @@
+#include "trygvis/sensor.h"
+#include "trygvis/sensor/io.h"
+
+#define BOOST_TEST_MODULE "SampleTest"
+
+#include <boost/test/unit_test.hpp>
+
+using namespace trygvis::sensor;
+using namespace trygvis::sensor::io;
+using namespace std;
+using namespace boost;
+
+BOOST_AUTO_TEST_CASE(key_value_parser) {
+ KeyDictionary dict;
+
+ auto buffer = make_shared<VectorSampleOutputStream>();
+
+ auto parser = make_shared<KeyValueSampleStreamParser>(buffer, dict);
+
+ char data[] = "a=1, b=2, c=3\n";
+ parser->process(boost::asio::buffer(data, sizeof(data)));
+ BOOST_CHECK_EQUAL(buffer->samples.size(), 1);
+ BOOST_CHECK_EQUAL(dict.size(), 3);
+ auto it = dict.begin();
+ BOOST_CHECK_EQUAL((*it)->name, "a");
+ BOOST_CHECK_EQUAL((*it++)->index, 0);
+ BOOST_CHECK_EQUAL((*it)->name, "b");
+ BOOST_CHECK_EQUAL((*it++)->index, 1);
+ BOOST_CHECK_EQUAL((*it)->name, "c");
+ BOOST_CHECK_EQUAL((*it++)->index, 2);
+}
+
+BOOST_AUTO_TEST_CASE(key_value_parser2) {
+ KeyDictionary dict;
+
+ auto buffer = make_shared<VectorSampleOutputStream>();
+
+ auto parser = make_shared<KeyValueSampleStreamParser>(buffer, dict);
+
+ char data[] = "now=1,sensor 1=0.000999999833333\n";
+ parser->process(boost::asio::buffer(data, sizeof(data)));
+ BOOST_CHECK_EQUAL(buffer->samples.size(), 1);
+ SampleRecord& sample = buffer->samples[0];
+ BOOST_CHECK_EQUAL(dict.size(), 2);
+ auto it = dict.begin();
+ BOOST_CHECK_EQUAL((*it)->name, "now");
+ BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true);
+ BOOST_CHECK_EQUAL(sample.at(*it).get(), "1");
+ BOOST_CHECK_EQUAL((*it++)->index, 0);
+
+ BOOST_CHECK_EQUAL((*it)->name, "sensor 1");
+ BOOST_CHECK_EQUAL(!sample.at(*it).operator!(), true);
+ BOOST_CHECK_EQUAL(sample.at(*it).get(), "0.000999999833333");
+ BOOST_CHECK_EQUAL((*it++)->index, 1);
+}
+
+BOOST_AUTO_TEST_CASE(key_value_parser_with_custom_dict) {
+ KeyDictionary dict;
+
+ dict.indexOf("c");
+ dict.indexOf("b");
+
+ auto buffer = make_shared<VectorSampleOutputStream>();
+
+ auto parser = make_shared<KeyValueSampleStreamParser>(buffer, dict);
+
+ char data[] = "a=1, b=2, c=3\n";
+ parser->process(boost::asio::buffer(data, sizeof(data)));
+ BOOST_CHECK_EQUAL(buffer->samples.size(), 1);
+ BOOST_CHECK_EQUAL(dict.size(), 3);
+ auto it = dict.begin();
+ BOOST_CHECK_EQUAL((*it)->name, "c");
+ BOOST_CHECK_EQUAL((*it++)->index, 0);
+ BOOST_CHECK_EQUAL((*it)->name, "b");
+ BOOST_CHECK_EQUAL((*it++)->index, 1);
+}