Statistics
| Branch: | Tag: | Revision:

root / host / test / gain_group_test.cpp @ 7b066a45

History | View | Annotate | Download (3.72 KB)

1
//
2
// Copyright 2010 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#include <boost/test/unit_test.hpp>
19
#include <uhd/utils/gain_group.hpp>
20
#include <boost/bind.hpp>
21
#include <boost/math/special_functions/round.hpp>
22
#include <iostream>
23

    
24
#define rint(x) boost::math::iround(x)
25

    
26
using namespace uhd;
27

    
28
/***********************************************************************
29
 * Define gain element classes with needed functions
30
 **********************************************************************/
31
class gain_element1{
32
public:
33

    
34
    gain_range_t get_range(void){
35
        return gain_range_t(0, 90, 1);
36
    }
37

    
38
    float get_value(void){
39
        return _gain;
40
    }
41

    
42
    void set_value(float gain){
43
        float step = get_range().step;
44
        _gain = step*rint(gain/step);
45
    }
46

    
47
private:
48
    float _gain;
49
};
50

    
51
class gain_element2{
52
public:
53

    
54
    gain_range_t get_range(void){
55
        return gain_range_t(-20, 10, float(0.1));
56
    }
57

    
58
    float get_value(void){
59
        return _gain;
60
    }
61

    
62
    void set_value(float gain){
63
        float step = get_range().step;
64
        _gain = step*rint(gain/step);
65
    }
66

    
67
private:
68
    float _gain;
69
};
70

    
71
//create static instances of gain elements to be shared by the tests
72
static gain_element1 g1;
73
static gain_element2 g2;
74

    
75
static gain_group::sptr get_gain_group(size_t pri1 = 0, size_t pri2 = 0){
76
    //create instance of gain group
77
    gain_fcns_t gain_fcns;
78
    gain_group::sptr gg(gain_group::make());
79

    
80
    //load gain group with function sets
81
    gain_fcns.get_range = boost::bind(&gain_element1::get_range, &g1);
82
    gain_fcns.get_value = boost::bind(&gain_element1::get_value, &g1);
83
    gain_fcns.set_value = boost::bind(&gain_element1::set_value, &g1, _1);
84
    gg->register_fcns(gain_fcns, pri1);
85

    
86
    gain_fcns.get_range = boost::bind(&gain_element2::get_range, &g2);
87
    gain_fcns.get_value = boost::bind(&gain_element2::get_value, &g2);
88
    gain_fcns.set_value = boost::bind(&gain_element2::set_value, &g2, _1);
89
    gg->register_fcns(gain_fcns, pri2);
90

    
91
    return gg;
92
}
93

    
94
/***********************************************************************
95
 * Test cases
96
 **********************************************************************/
97
static const float tolerance = float(0.001);
98

    
99
BOOST_AUTO_TEST_CASE(test_gain_group_overall){
100
    gain_group::sptr gg = get_gain_group();
101

    
102
    //test the overall stuff
103
    gg->set_value(80);
104
    BOOST_CHECK_CLOSE(gg->get_value(), float(80), tolerance);
105
    BOOST_CHECK_CLOSE(gg->get_range().min, float(-20), tolerance);
106
    BOOST_CHECK_CLOSE(gg->get_range().max, float(100), tolerance);
107
    BOOST_CHECK_CLOSE(gg->get_range().step, float(0.1), tolerance);
108
}
109

    
110
BOOST_AUTO_TEST_CASE(test_gain_group_priority){
111
    gain_group::sptr gg = get_gain_group(0, 1);
112

    
113
    //test the overall stuff
114
    gg->set_value(80);
115
    BOOST_CHECK_CLOSE(gg->get_value(), float(80), tolerance);
116
    BOOST_CHECK_CLOSE(gg->get_range().min, float(-20), tolerance);
117
    BOOST_CHECK_CLOSE(gg->get_range().max, float(100), tolerance);
118
    BOOST_CHECK_CLOSE(gg->get_range().step, float(0.1), tolerance);
119

    
120
    //test the the higher priority gain got filled first (gain 2)
121
    BOOST_CHECK_CLOSE(g2.get_value(), g2.get_range().max, tolerance);
122
}