aboutsummaryrefslogtreecommitdiff
path: root/sensor/test
diff options
context:
space:
mode:
Diffstat (limited to 'sensor/test')
-rw-r--r--sensor/test/CMakeLists.txt26
-rw-r--r--sensor/test/SampleTest.cpp76
-rw-r--r--sensor/test/SoilMoistureIoTest.cpp48
3 files changed, 102 insertions, 48 deletions
diff --git a/sensor/test/CMakeLists.txt b/sensor/test/CMakeLists.txt
new file mode 100644
index 0000000..2e68532
--- /dev/null
+++ b/sensor/test/CMakeLists.txt
@@ -0,0 +1,26 @@
+find_package(Boost COMPONENTS log regex unit_test_framework REQUIRED)
+
+# If we can change directory here add_definition and test-specific stuff could be moved to the test directory
+file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *Test.cpp)
+add_definitions(-DBOOST_TEST_DYN_LINK)
+
+foreach(testSrc ${TEST_SRCS})
+ get_filename_component(testName ${testSrc} NAME_WE)
+
+ #Add compile target
+ add_executable(${testName} ${testSrc})
+
+ include_directories(../include)
+
+ target_link_libraries(${testName} trygvis-sensor)
+ target_link_libraries(${testName} ${Boost_LIBRARIES})
+
+# set_target_properties(${testName} PROPERTIES
+# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test)
+
+ #Finally add it to test execution -
+ #Notice the WORKING_DIRECTORY and COMMAND
+ add_test(NAME ${testName}
+ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/testBin
+ COMMAND ${CMAKE_BINARY_DIR}/testBin/${testName})
+endforeach(testSrc)
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);
+}
diff --git a/sensor/test/SoilMoistureIoTest.cpp b/sensor/test/SoilMoistureIoTest.cpp
deleted file mode 100644
index 574885c..0000000
--- a/sensor/test/SoilMoistureIoTest.cpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#include "SensorSample.h"
-
-#define BOOST_TEST_MODULE "SoilMoistureIoTest"
-
-#include <boost/test/unit_test.hpp>
-
-using namespace trygvis::soil_moisture;
-
-BOOST_AUTO_TEST_CASE(key_value_parser) {
- KeyDictionary dict;
-
- auto buffer = make_shared<VectorSampleOutputStream>();
-
- auto parser = new 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_parser_with_custom_dict) {
- KeyDictionary dict;
-
- dict.indexOf("c");
- dict.indexOf("b");
-
- auto buffer = make_shared<VectorSampleOutputStream>();
-
- auto parser = new 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);
-}