diff options
author | Martin Braun <martin.braun@ettus.com> | 2017-09-29 13:51:43 -0700 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2017-12-22 15:04:02 -0800 |
commit | 21605b4e8b58053f4fa91a0a5136261da33e3bfb (patch) | |
tree | 551c8313dcff1047598bcf4a0f00cf0bdd9323e2 | |
parent | aeaea4936011665e2bbad66e1fdf4628e2b940f2 (diff) | |
download | uhd-21605b4e8b58053f4fa91a0a5136261da33e3bfb.tar.gz uhd-21605b4e8b58053f4fa91a0a5136261da33e3bfb.tar.bz2 uhd-21605b4e8b58053f4fa91a0a5136261da33e3bfb.zip |
types: sensor_value_t can now be created from map
- Adds unit tests for sensor_value_t also
-rw-r--r-- | host/include/uhd/types/sensors.hpp | 14 | ||||
-rw-r--r-- | host/lib/types/sensors.cpp | 51 | ||||
-rw-r--r-- | host/tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/tests/sensors_test.cpp | 95 |
4 files changed, 161 insertions, 0 deletions
diff --git a/host/include/uhd/types/sensors.hpp b/host/include/uhd/types/sensors.hpp index 6dede02dd..66501a7e9 100644 --- a/host/include/uhd/types/sensors.hpp +++ b/host/include/uhd/types/sensors.hpp @@ -8,6 +8,7 @@ #define INCLUDED_UHD_TYPES_SENSORS_HPP #include <uhd/config.hpp> +#include <map> #include <string> namespace uhd{ @@ -25,6 +26,7 @@ namespace uhd{ * //prints Temperature: 38.5 C */ struct UHD_API sensor_value_t{ + typedef std::map<std::string, std::string> sensor_map_t; /*! * Create a sensor value from a boolean. @@ -81,6 +83,18 @@ namespace uhd{ ); /*! + * Create a sensor value from a map. + * + * The map must have the following keys: name, type, value, and unit. + * + * type must one of the following strings: BOOLEAN, INTEGER, REALNUM, + * or STRING (see data_type_t). + */ + sensor_value_t( + const std::map<std::string, std::string> &sensor_dict + ); + + /*! * Create a sensor value from another sensor value. * \param source the source sensor value to copy */ diff --git a/host/lib/types/sensors.cpp b/host/lib/types/sensors.cpp index 4bffc1bd9..d015cf666 100644 --- a/host/lib/types/sensors.cpp +++ b/host/lib/types/sensors.cpp @@ -57,6 +57,57 @@ sensor_value_t::sensor_value_t( /* NOP */ } +static sensor_value_t::data_type_t _string_to_type( + const std::string &type_str +) { + if (type_str == "STRING") { + return sensor_value_t::STRING; + } else if (type_str == "REALNUM") { + return sensor_value_t::REALNUM; + } else if (type_str == "INTEGER") { + return sensor_value_t::INTEGER; + } else if (type_str == "BOOLEAN") { + return sensor_value_t::BOOLEAN; + } else { + throw uhd::value_error( + std::string("Invalid sensor value type: ") + type_str + ); + } +} + +sensor_value_t::sensor_value_t( + const std::map<std::string, std::string> &sensor_dict +): + name(sensor_dict.at("name")), + value(sensor_dict.at("value")), + unit(sensor_dict.at("unit")), + type(_string_to_type(sensor_dict.at("type"))) +{ + UHD_ASSERT_THROW(not name.empty()); + UHD_ASSERT_THROW(not value.empty()); + try { + if (type == INTEGER) { + to_int(); + } else if (type == REALNUM) { + to_real(); + } + } + catch (const std::invalid_argument&) { + throw uhd::value_error(str( + boost::format("Could not convert sensor value `%s' to type `%s'") + % value + % sensor_dict.at("type") + )); + } + catch (const std::out_of_range&) { + throw uhd::value_error(str( + boost::format("Could not convert sensor value `%s' to type `%s'") + % value + % sensor_dict.at("type") + )); + } +} + sensor_value_t::sensor_value_t(const sensor_value_t& source) { *this = source; diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 1b8d2acf7..7fcf1652c 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -30,6 +30,7 @@ SET(test_sources property_test.cpp ranges_test.cpp sid_t_test.cpp + sensors_test.cpp sph_recv_test.cpp sph_send_test.cpp subdev_spec_test.cpp diff --git a/host/tests/sensors_test.cpp b/host/tests/sensors_test.cpp new file mode 100644 index 000000000..083a376f6 --- /dev/null +++ b/host/tests/sensors_test.cpp @@ -0,0 +1,95 @@ +// +// Copyright 2017 Ettus Research (National Instruments Corp.) +// +// SPDX-License-Identifier: GPL-3.0 +// + +#include <boost/test/unit_test.hpp> +#include <uhd/types/sensors.hpp> +#include <map> +#include <string> + +using uhd::sensor_value_t; + +BOOST_AUTO_TEST_CASE(test_sensor_bool) { + auto sensor_bool = sensor_value_t( + "bool_sensor", + true, + "true_unit", + "false_unit" + ); + BOOST_CHECK(sensor_bool.to_bool()); + BOOST_CHECK_EQUAL(sensor_bool.unit, "true_unit"); + + std::map<std::string, std::string> sensor_map = { + {"name", "bool_sensor"}, + {"type", "BOOLEAN"}, + {"unit", "true_unit"}, + {"value", "true"}, + }; + + auto sensor_bool2 = sensor_value_t(sensor_map); + BOOST_CHECK(sensor_bool2.to_bool()); + BOOST_CHECK_EQUAL(sensor_bool2.unit, "true_unit"); +} + + +BOOST_AUTO_TEST_CASE(test_sensor_real) { + const double sens_val = 2.25; + const std::string sens_units = "floats"; + const std::string sens_name = "real_sensor"; + auto sensor_real = sensor_value_t(sens_name, sens_val, sens_units); + BOOST_CHECK_EQUAL(sensor_real.to_real(), sens_val); + BOOST_CHECK_EQUAL(sensor_real.unit, sens_units); + + std::map<std::string, std::string> sensor_map = { + {"name", sens_name}, + {"type", "REALNUM"}, + {"unit", sens_units}, + {"value", std::to_string(sens_val)}, + }; + + auto sensor_real2 = sensor_value_t(sensor_map); + BOOST_CHECK_EQUAL(sensor_real2.to_real(), sens_val); + BOOST_CHECK_EQUAL(sensor_real2.unit, sens_units); +} + +BOOST_AUTO_TEST_CASE(test_sensor_int) { + const int sens_val = 5; + const std::string sens_units = "ints"; + const std::string sens_name = "int_sensor"; + auto sensor_int = sensor_value_t(sens_name, sens_val, sens_units); + BOOST_CHECK_EQUAL(sensor_int.to_int(), sens_val); + BOOST_CHECK_EQUAL(sensor_int.unit, sens_units); + + std::map<std::string, std::string> sensor_map = { + {"name", sens_name}, + {"type", "INTEGER"}, + {"unit", sens_units}, + {"value", std::to_string(sens_val)}, + }; + + auto sensor_int2 = sensor_value_t(sensor_map); + BOOST_CHECK_EQUAL(sensor_int2.to_int(), sens_val); + BOOST_CHECK_EQUAL(sensor_int2.unit, sens_units); +} + +BOOST_AUTO_TEST_CASE(test_sensor_string) { + const std::string sens_val = "foo"; + const std::string sens_units = "strings"; + const std::string sens_name = "str_sensor"; + auto sensor_str = sensor_value_t(sens_name, sens_val, sens_units); + BOOST_CHECK_EQUAL(sensor_str.value, sens_val); + BOOST_CHECK_EQUAL(sensor_str.unit, sens_units); + + std::map<std::string, std::string> sensor_map = { + {"name", sens_name}, + {"type", "STRING"}, + {"unit", sens_units}, + {"value", sens_val}, + }; + + auto sensor_str2 = sensor_value_t(sensor_map); + BOOST_CHECK_EQUAL(sensor_str2.unit, sens_units); +} + |