aboutsummaryrefslogtreecommitdiffstats
path: root/host/test
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2010-07-23 21:53:37 -0700
committerJosh Blum <josh@joshknows.com>2010-07-27 15:45:49 -0700
commit3b056a00425c8c1cc907a14983611268efb86878 (patch)
tree35a46dc3eb3e59d6081ad84a73e0680a0cefbae4 /host/test
parentc9301fc2c47f9141597041d07d7a0bb36be81e08 (diff)
downloaduhd-3b056a00425c8c1cc907a14983611268efb86878.tar.gz
uhd-3b056a00425c8c1cc907a14983611268efb86878.tar.bz2
uhd-3b056a00425c8c1cc907a14983611268efb86878.zip
uhd: added gain group and unit testing for it
Diffstat (limited to 'host/test')
-rw-r--r--host/test/CMakeLists.txt1
-rw-r--r--host/test/gain_group_test.cpp122
2 files changed, 123 insertions, 0 deletions
diff --git a/host/test/CMakeLists.txt b/host/test/CMakeLists.txt
index b7dcb741a..25cae6e7f 100644
--- a/host/test/CMakeLists.txt
+++ b/host/test/CMakeLists.txt
@@ -26,6 +26,7 @@ ADD_EXECUTABLE(main_test
convert_types_test.cpp
dict_test.cpp
error_test.cpp
+ gain_group_test.cpp
gain_handler_test.cpp
tune_helper_test.cpp
vrt_test.cpp
diff --git a/host/test/gain_group_test.cpp b/host/test/gain_group_test.cpp
new file mode 100644
index 000000000..4d337afb9
--- /dev/null
+++ b/host/test/gain_group_test.cpp
@@ -0,0 +1,122 @@
+//
+// Copyright 2010 Ettus Research LLC
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+
+#include <boost/test/unit_test.hpp>
+#include <uhd/utils/gain_group.hpp>
+#include <boost/bind.hpp>
+#include <boost/math/special_functions/round.hpp>
+#include <iostream>
+
+#define rint(x) boost::math::iround(x)
+
+using namespace uhd;
+
+/***********************************************************************
+ * Define gain element classes with needed functions
+ **********************************************************************/
+class gain_element1{
+public:
+
+ gain_range_t get_range(void){
+ return gain_range_t(0, 90, 1);
+ }
+
+ float get_value(void){
+ return _gain;
+ }
+
+ void set_value(float gain){
+ float step = get_range().step;
+ _gain = step*rint(gain/step);
+ }
+
+private:
+ float _gain;
+};
+
+class gain_element2{
+public:
+
+ gain_range_t get_range(void){
+ return gain_range_t(-20, 10, 0.1);
+ }
+
+ float get_value(void){
+ return _gain;
+ }
+
+ void set_value(float gain){
+ float step = get_range().step;
+ _gain = step*rint(gain/step);
+ }
+
+private:
+ float _gain;
+};
+
+//create static instances of gain elements to be shared by the tests
+static gain_element1 g1;
+static gain_element2 g2;
+
+static gain_group::sptr get_gain_group(size_t pri1 = 0, size_t pri2 = 0){
+ //create instance of gain group
+ gain_fcns_t gain_fcns;
+ gain_group::sptr gg(gain_group::make());
+
+ //load gain group with function sets
+ gain_fcns.get_range = boost::bind(&gain_element1::get_range, &g1);
+ gain_fcns.get_value = boost::bind(&gain_element1::get_value, &g1);
+ gain_fcns.set_value = boost::bind(&gain_element1::set_value, &g1, _1);
+ gg->register_fcns(gain_fcns, pri1);
+
+ gain_fcns.get_range = boost::bind(&gain_element2::get_range, &g2);
+ gain_fcns.get_value = boost::bind(&gain_element2::get_value, &g2);
+ gain_fcns.set_value = boost::bind(&gain_element2::set_value, &g2, _1);
+ gg->register_fcns(gain_fcns, pri2);
+
+ return gg;
+}
+
+/***********************************************************************
+ * Test cases
+ **********************************************************************/
+static const double tolerance = 0.001;
+
+BOOST_AUTO_TEST_CASE(test_gain_group_overall){
+ gain_group::sptr gg = get_gain_group();
+
+ //test the overall stuff
+ gg->set_value(80);
+ BOOST_CHECK_CLOSE(gg->get_value(), 80, tolerance);
+ BOOST_CHECK_CLOSE(gg->get_range().min, -20, tolerance);
+ BOOST_CHECK_CLOSE(gg->get_range().max, 100, tolerance);
+ BOOST_CHECK_CLOSE(gg->get_range().step, 0.1, tolerance);
+}
+
+BOOST_AUTO_TEST_CASE(test_gain_group_priority){
+ gain_group::sptr gg = get_gain_group(0, 1);
+
+ //test the overall stuff
+ gg->set_value(80);
+ BOOST_CHECK_CLOSE(gg->get_value(), 80, tolerance);
+ BOOST_CHECK_CLOSE(gg->get_range().min, -20, tolerance);
+ BOOST_CHECK_CLOSE(gg->get_range().max, 100, tolerance);
+ BOOST_CHECK_CLOSE(gg->get_range().step, 0.1, tolerance);
+
+ //test the the higher priority gain got filled first (gain 2)
+ BOOST_CHECK_CLOSE(g2.get_value(), g2.get_range().max, tolerance);
+}