uhd/host/tests/gain_group_test.cpp
Martin Braun d3a16b7022 uhd: Replace all occurrences of boost::bind with std::bind
Note: Replacing everything with a lambda would be even better, but that
can't be easily scripted so we'll do this as a first step to reduce the
Boost footprint.

This also removes occurences of #include <boost/bind.hpp>, and makes
sure all usages of std::bind have an #include <functional>. clang-format
wasn't always applied to minimize the changeset in this commit, however,
it was applied to the blocks of #includes.

Due to conflicts with other Boost libraries, the placeholders _1, _2,
etc. could not be directly used, but had to be explicitly called out
(as std::placeholders::_1, etc.). This makes the use of std::bind even
uglier, which serves as another reminder that using std::bind (and even
more so, boost::bind) should be avoided.

nirio/rpc/rpc_client.cpp still contains a reference to boost::bind. It
was not possible to remove it by simply doing a search and replace, so
it will be removed in a separate commit.
2019-11-26 12:21:32 -08:00

121 lines
3.3 KiB
C++

//
// Copyright 2010-2011 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include <uhd/utils/gain_group.hpp>
#include <boost/math/special_functions/round.hpp>
#include <boost/test/unit_test.hpp>
#include <functional>
#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);
}
double get_value(void)
{
return _gain;
}
void set_value(double gain)
{
double step = get_range().step();
_gain = step * rint(gain / step);
}
private:
double _gain;
};
class gain_element2
{
public:
gain_range_t get_range(void)
{
return gain_range_t(-20, 10, 0.1);
}
double get_value(void)
{
return _gain;
}
void set_value(double gain)
{
double step = get_range().step();
_gain = step * rint(gain / step);
}
private:
double _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 = std::bind(&gain_element1::get_range, &g1);
gain_fcns.get_value = std::bind(&gain_element1::get_value, &g1);
gain_fcns.set_value = std::bind(&gain_element1::set_value, &g1, std::placeholders::_1);
gg->register_fcns("g1", gain_fcns, pri1);
gain_fcns.get_range = std::bind(&gain_element2::get_range, &g2);
gain_fcns.get_value = std::bind(&gain_element2::get_value, &g2);
gain_fcns.set_value = std::bind(&gain_element2::set_value, &g2, std::placeholders::_1);
gg->register_fcns("g2", 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.0, tolerance);
BOOST_CHECK_CLOSE(gg->get_range().start(), -20.0, tolerance);
BOOST_CHECK_CLOSE(gg->get_range().stop(), 100.0, 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.0, tolerance);
BOOST_CHECK_CLOSE(gg->get_range().start(), -20.0, tolerance);
BOOST_CHECK_CLOSE(gg->get_range().stop(), 100.0, 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().stop(), tolerance);
}