From 08dfff379865656e94b31fd565a4b13b4609ea63 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 1 Nov 2010 18:56:38 -0700 Subject: uhd: created a meta range that is a range of ranges for gains and freqs created a templated range that that holds a start, stop, and step created a meta-range template that is a vector of ranges meta-range can calculate the overall start, stop, step or be indexed to get at components replaced instances of range.min, max, step with the functions start() stop() and step() the xcvr frequency range is now expressed in as two ranges (have to fix its clip function though) --- host/include/uhd/types/CMakeLists.txt | 1 + host/include/uhd/types/dict.ipp | 11 ++- host/include/uhd/types/ranges.hpp | 81 +++++++++++++++++--- host/include/uhd/types/ranges.ipp | 134 ++++++++++++++++++++++++++++++++++ host/include/uhd/usrp/mimo_usrp.hpp | 2 +- 5 files changed, 211 insertions(+), 18 deletions(-) create mode 100644 host/include/uhd/types/ranges.ipp (limited to 'host/include') diff --git a/host/include/uhd/types/CMakeLists.txt b/host/include/uhd/types/CMakeLists.txt index a96976b5e..1d2c0c41c 100644 --- a/host/include/uhd/types/CMakeLists.txt +++ b/host/include/uhd/types/CMakeLists.txt @@ -25,6 +25,7 @@ INSTALL(FILES mac_addr.hpp metadata.hpp otw_type.hpp + ranges.ipp ranges.hpp serial.hpp stream_cmd.hpp diff --git a/host/include/uhd/types/dict.ipp b/host/include/uhd/types/dict.ipp index ba05d5272..f037d7988 100644 --- a/host/include/uhd/types/dict.ipp +++ b/host/include/uhd/types/dict.ipp @@ -46,12 +46,11 @@ namespace uhd{ /* NOP */ } - template - template - dict::dict(InputIterator first, InputIterator last){ - for(InputIterator it = first; it != last; it++){ - _map.push_back(*it); - } + template template + dict::dict(InputIterator first, InputIterator last): + _map(first, last) + { + /* NOP */ } template diff --git a/host/include/uhd/types/ranges.hpp b/host/include/uhd/types/ranges.hpp index a2057d1c8..0811fc4c8 100644 --- a/host/include/uhd/types/ranges.hpp +++ b/host/include/uhd/types/ranges.hpp @@ -19,28 +19,87 @@ #define INCLUDED_UHD_TYPES_RANGES_HPP #include +#include +#include namespace uhd{ /*! - * The gain range struct describes possible gain settings. - * The mimumum gain, maximum gain, and step size are in dB. + * A range object describes a set of discrete values of the form: + * y = start + step*n, where n is an integer between 0 and (stop - start)/step */ - struct UHD_API gain_range_t{ - float min, max, step; - gain_range_t(float min = 0.0, float max = 0.0, float step = 0.0); + template class range_t{ + public: + /*! + * Create a range from a single value. + * The step size will be taken as zero. + * \param value the only possible value in this range + */ + range_t(const T &value = T(0)); + + /*! + * Create a range from a full set of values. + * A step size of zero implies infinite precision. + * \param start the minimum value for this range + * \param stop the maximum value for this range + * \param step the step size for this range + */ + range_t(const T &start, const T &stop, const T &step = T(0)); + + //! Get the start value for this range. + const T start(void) const; + + //! Get the stop value for this range. + const T stop(void) const; + + //! Get the step value for this range. + const T step(void) const; + + private: UHD_PIMPL_DECL(impl) _impl; }; /*! - * The frequency range struct describes possible frequency settings. - * Because tuning is very granular (sub-Hz), step size is not listed. - * The mimumum frequency and maximum frequency are in Hz. + * A meta-range object holds a list of individual ranges. */ - struct UHD_API freq_range_t{ - double min, max; - freq_range_t(double min = 0.0, double max = 0.0); + template struct meta_range_t : std::vector >{ + + //! A default constructor for an empty meta-range + meta_range_t(void); + + /*! + * Input iterator constructor: + * Makes boost::assign::list_of work. + * \param first the begin iterator + * \param last the end iterator + */ + template + meta_range_t(InputIterator first, InputIterator last); + + /*! + * A convenience constructor for a single range. + * A step size of zero implies infinite precision. + * \param start the minimum value for this range + * \param stop the maximum value for this range + * \param step the step size for this range + */ + meta_range_t(const T &start, const T &stop, const T &step = T(0)); + + //! Get the overall start value for this meta-range. + const T start(void) const; + + //! Get the overall stop value for this meta-range. + const T stop(void) const; + + //! Get the overall step value for this meta-range. + const T step(void) const; + }; + typedef meta_range_t gain_range_t; + typedef meta_range_t freq_range_t; + } //namespace uhd +#include + #endif /* INCLUDED_UHD_TYPES_RANGES_HPP */ diff --git a/host/include/uhd/types/ranges.ipp b/host/include/uhd/types/ranges.ipp new file mode 100644 index 000000000..cb1628c53 --- /dev/null +++ b/host/include/uhd/types/ranges.ipp @@ -0,0 +1,134 @@ +// +// 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 . +// + +#ifndef INCLUDED_UHD_TYPES_RANGES_IPP +#define INCLUDED_UHD_TYPES_RANGES_IPP + +#include +#include +#include +#include + +namespace uhd{ + + /******************************************************************* + * range_t implementation code + ******************************************************************/ + template struct range_t::impl{ + impl(const T &start, const T &stop, const T &step): + start(start), stop(stop), step(step) + { + /* NOP */ + } + const T start, stop, step; + }; + + template range_t::range_t(const T &value): + _impl(UHD_PIMPL_MAKE(impl, (value, value, T(0)))) + { + /* NOP */ + } + + template range_t::range_t( + const T &start, const T &stop, const T &step + ): + _impl(UHD_PIMPL_MAKE(impl, (start, stop, step))) + { + if (stop < start){ + throw std::invalid_argument("cannot make range where stop < start"); + } + } + + template const T range_t::start(void) const{ + return _impl->start; + } + + template const T range_t::stop(void) const{ + return _impl->stop; + } + + template const T range_t::step(void) const{ + return _impl->step; + } + + /******************************************************************* + * meta_range_t implementation code + ******************************************************************/ + + template meta_range_t::meta_range_t(void){ + /* NOP */ + } + + template template + meta_range_t::meta_range_t( + InputIterator first, InputIterator last + ): + std::vector >(first, last) + { + /* NOP */ + } + + template meta_range_t::meta_range_t( + const T &start, const T &stop, const T &step + ): + std::vector > (1, range_t(start, stop, step)) + { + /* NOP */ + } + + template const T meta_range_t::start(void) const{ + if (this->empty()){ + throw std::runtime_error("cannot calculate overall start on empty meta-range"); + } + T min_start = this->at(0).start(); + for (size_t i = 1; i < this->size(); i++){ + min_start = std::min(min_start, this->at(i).start()); + } + return min_start; + } + + template const T meta_range_t::stop(void) const{ + if (this->empty()){ + throw std::runtime_error("cannot calculate overall stop on empty meta-range"); + } + T max_stop = this->at(0).stop(); + for (size_t i = 1; i < this->size(); i++){ + max_stop = std::max(max_stop, this->at(i).stop()); + } + return max_stop; + } + + template const T meta_range_t::step(void) const{ + if (this->empty()){ + throw std::runtime_error("cannot calculate overall step on empty meta-range"); + } + T min_step = this->at(0).step(); + for (size_t i = 1; i < this->size(); i++){ + if (this->at(i).start() < this->at(i-1).stop()){ + throw std::runtime_error("cannot calculate overall range when start(n) < stop(n-1) "); + } + if (this->at(i).start() != this->at(i).stop()){ + min_step = std::min(min_step, this->at(i).step()); + } + min_step = std::min(min_step, this->at(i).start() - this->at(i-1).stop()); + } + return min_step; + } + +} //namespace uhd + +#endif /* INCLUDED_UHD_TYPES_RANGES_IPP */ diff --git a/host/include/uhd/usrp/mimo_usrp.hpp b/host/include/uhd/usrp/mimo_usrp.hpp index a2092f04f..977559b0b 100644 --- a/host/include/uhd/usrp/mimo_usrp.hpp +++ b/host/include/uhd/usrp/mimo_usrp.hpp @@ -200,7 +200,7 @@ namespace uhd{ namespace usrp{ namespace /*anon*/{ static inline freq_range_t add_dsp_shift(const freq_range_t &range, wax::obj dsp){ double codec_rate = dsp[DSP_PROP_CODEC_RATE].as(); - return freq_range_t(range.min - codec_rate/2.0, range.max + codec_rate/2.0); + return freq_range_t(range.start() - codec_rate/2.0, range.stop() + codec_rate/2.0); } /*********************************************************************** -- cgit v1.2.3 From 775383e635cdf27ede64f5936a649570b74d7c70 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 1 Nov 2010 20:57:09 -0700 Subject: uhd: added meta-range clip and implemented in dboards, fixed step calculation --- host/include/uhd/types/ranges.hpp | 8 ++++ host/include/uhd/types/ranges.ipp | 78 ++++++++++++++++++++++++++---------- host/lib/usrp/dboard/db_dbsrx.cpp | 6 +-- host/lib/usrp/dboard/db_rfx.cpp | 2 +- host/lib/usrp/dboard/db_tvrx.cpp | 6 +-- host/lib/usrp/dboard/db_wbx.cpp | 9 ++--- host/lib/usrp/dboard/db_xcvr2450.cpp | 10 +++-- 7 files changed, 82 insertions(+), 37 deletions(-) (limited to 'host/include') diff --git a/host/include/uhd/types/ranges.hpp b/host/include/uhd/types/ranges.hpp index 0811fc4c8..206c64726 100644 --- a/host/include/uhd/types/ranges.hpp +++ b/host/include/uhd/types/ranges.hpp @@ -93,6 +93,14 @@ namespace uhd{ //! Get the overall step value for this meta-range. const T step(void) const; + /*! + * Clip the target value to a possible range value. + * \param value the value to clip to this range + * \param clip_step if true, clip to steps as well + * \return a value that is in one of the ranges + */ + const T clip(const T &value, bool clip_step = false) const; + }; typedef meta_range_t gain_range_t; diff --git a/host/include/uhd/types/ranges.ipp b/host/include/uhd/types/ranges.ipp index cb1628c53..7fd1bf2eb 100644 --- a/host/include/uhd/types/ranges.ipp +++ b/host/include/uhd/types/ranges.ipp @@ -19,6 +19,7 @@ #define INCLUDED_UHD_TYPES_RANGES_IPP #include +#include #include #include #include @@ -69,6 +70,21 @@ namespace uhd{ * meta_range_t implementation code ******************************************************************/ + namespace /*anon*/{ + template inline + void check_meta_range_monotonic(const meta_range_t &mr){ + if (mr.empty()){ + throw std::runtime_error("meta-range cannot be empty"); + } + for (size_t i = 1; i < mr.size(); i++){ + if (mr.at(i).start() < mr.at(i-1).stop()){ + throw std::runtime_error("meta-range is not monotonic"); + } + } + } + } //namespace /*anon*/ + + template meta_range_t::meta_range_t(void){ /* NOP */ } @@ -91,42 +107,60 @@ namespace uhd{ } template const T meta_range_t::start(void) const{ - if (this->empty()){ - throw std::runtime_error("cannot calculate overall start on empty meta-range"); - } - T min_start = this->at(0).start(); - for (size_t i = 1; i < this->size(); i++){ - min_start = std::min(min_start, this->at(i).start()); + check_meta_range_monotonic(*this); + T min_start = this->front().start(); + BOOST_FOREACH(const range_t &r, (*this)){ + min_start = std::min(min_start, r.start()); } return min_start; } template const T meta_range_t::stop(void) const{ - if (this->empty()){ - throw std::runtime_error("cannot calculate overall stop on empty meta-range"); - } - T max_stop = this->at(0).stop(); - for (size_t i = 1; i < this->size(); i++){ - max_stop = std::max(max_stop, this->at(i).stop()); + check_meta_range_monotonic(*this); + T max_stop = this->front().stop(); + BOOST_FOREACH(const range_t &r, (*this)){ + max_stop = std::max(max_stop, r.stop()); } return max_stop; } template const T meta_range_t::step(void) const{ - if (this->empty()){ - throw std::runtime_error("cannot calculate overall step on empty meta-range"); + check_meta_range_monotonic(*this); + std::vector non_zero_steps; + range_t last = this->front(); + BOOST_FOREACH(const range_t &r, (*this)){ + //steps at each range + if (r.step() != T(0)) non_zero_steps.push_back(r.step()); + //and steps in-between ranges + T ibtw_step = r.start() - last.stop(); + if (ibtw_step != T(0)) non_zero_steps.push_back(ibtw_step); + //store ref to last + last = r; } - T min_step = this->at(0).step(); - for (size_t i = 1; i < this->size(); i++){ - if (this->at(i).start() < this->at(i-1).stop()){ - throw std::runtime_error("cannot calculate overall range when start(n) < stop(n-1) "); + if (non_zero_steps.empty()) return T(0); //all zero steps, its zero... + return *std::min_element(non_zero_steps.begin(), non_zero_steps.end()); + } + + template const T meta_range_t::clip( + const T &value, bool clip_step + ) const{ + check_meta_range_monotonic(*this); + T last_stop = this->front().stop(); + BOOST_FOREACH(const range_t &r, (*this)){ + //in-between ranges, clip to nearest + if (value < r.start()){ + return (std::abs(value - r.start()) < std::abs(value - last_stop))? + r.start() : last_stop; } - if (this->at(i).start() != this->at(i).stop()){ - min_step = std::min(min_step, this->at(i).step()); + //in this range, clip here + if (value <= r.stop()){ + if (not clip_step or r.step() == T(0)) return value; + return boost::math::round((value - r.start())/r.step())*r.step() + r.start(); } - min_step = std::min(min_step, this->at(i).start() - this->at(i-1).stop()); + //continue on to the next range + last_stop = r.stop(); } - return min_step; + return last_stop; } } //namespace uhd diff --git a/host/lib/usrp/dboard/db_dbsrx.cpp b/host/lib/usrp/dboard/db_dbsrx.cpp index 70bf03158..5316fb952 100644 --- a/host/lib/usrp/dboard/db_dbsrx.cpp +++ b/host/lib/usrp/dboard/db_dbsrx.cpp @@ -229,7 +229,7 @@ dbsrx::~dbsrx(void){ * Tuning **********************************************************************/ void dbsrx::set_lo_freq(double target_freq){ - target_freq = std::clip(target_freq, dbsrx_freq_range.start(), dbsrx_freq_range.stop()); + target_freq = dbsrx_freq_range.clip(target_freq); double actual_freq=0.0, pfd_freq=0.0, ref_clock=0.0; int R=0, N=0, r=0, m=0; @@ -417,7 +417,7 @@ void dbsrx::set_lo_freq(double target_freq){ */ static int gain_to_gc2_vga_reg(float &gain){ int reg = 0; - gain = std::clip(gain, dbsrx_gain_ranges["GC2"].start(), dbsrx_gain_ranges["GC2"].stop()); + gain = dbsrx_gain_ranges["GC2"].clip(gain); // Half dB steps from 0-5dB, 1dB steps from 5-24dB if (gain < 5) { @@ -443,7 +443,7 @@ static int gain_to_gc2_vga_reg(float &gain){ */ static float gain_to_gc1_rfvga_dac(float &gain){ //clip the input - gain = std::clip(gain, dbsrx_gain_ranges["GC1"].start(), dbsrx_gain_ranges["GC1"].stop()); + gain = dbsrx_gain_ranges["GC1"].clip(gain); //voltage level constants static const float max_volts = float(1.2), min_volts = float(2.7); diff --git a/host/lib/usrp/dboard/db_rfx.cpp b/host/lib/usrp/dboard/db_rfx.cpp index bb9e19708..4e73fb3a3 100644 --- a/host/lib/usrp/dboard/db_rfx.cpp +++ b/host/lib/usrp/dboard/db_rfx.cpp @@ -292,7 +292,7 @@ double rfx_xcvr::set_lo_freq( ) % (target_freq/1e6) << std::endl; //clip the input - target_freq = std::clip(target_freq, _freq_range.start(), _freq_range.stop()); + target_freq = _freq_range.clip(target_freq); if (_div2[unit]) target_freq *= 2; //map prescalers to the register enums diff --git a/host/lib/usrp/dboard/db_tvrx.cpp b/host/lib/usrp/dboard/db_tvrx.cpp index 94ab86898..17fdad74a 100644 --- a/host/lib/usrp/dboard/db_tvrx.cpp +++ b/host/lib/usrp/dboard/db_tvrx.cpp @@ -277,7 +277,7 @@ static double gain_interp(double gain, boost::array db_vector, boost static float rf_gain_to_voltage(float gain, double lo_freq){ //clip the input - gain = std::clip(gain, get_tvrx_gain_ranges()["RF"].start(), get_tvrx_gain_ranges()["RF"].stop()); + gain = get_tvrx_gain_ranges()["RF"].clip(gain); //first we need to find out what band we're in, because gains are different across different bands std::string band = get_band(lo_freq + tvrx_if_freq); @@ -305,7 +305,7 @@ static float rf_gain_to_voltage(float gain, double lo_freq){ static float if_gain_to_voltage(float gain){ //clip the input - gain = std::clip(gain, get_tvrx_gain_ranges()["IF"].start(), get_tvrx_gain_ranges()["IF"].stop()); + gain = get_tvrx_gain_ranges()["IF"].clip(gain); double gain_volts = gain_interp(gain, tvrx_if_gains_db, tvrx_gains_volts); double dac_volts = gain_volts / opamp_gain; @@ -337,7 +337,7 @@ void tvrx::set_gain(float gain, const std::string &name){ */ void tvrx::set_freq(double freq) { - freq = std::clip(freq, tvrx_freq_range.start(), tvrx_freq_range.stop()); + freq = tvrx_freq_range.clip(freq); std::string prev_band = get_band(_lo_freq - tvrx_if_freq); std::string new_band = get_band(freq); diff --git a/host/lib/usrp/dboard/db_wbx.cpp b/host/lib/usrp/dboard/db_wbx.cpp index d68cd4eaa..dd5bd600b 100644 --- a/host/lib/usrp/dboard/db_wbx.cpp +++ b/host/lib/usrp/dboard/db_wbx.cpp @@ -198,16 +198,15 @@ wbx_xcvr::~wbx_xcvr(void){ **********************************************************************/ static int rx_pga0_gain_to_iobits(float &gain){ //clip the input - gain = std::clip(gain, wbx_rx_gain_ranges["PGA0"].start(), wbx_rx_gain_ranges["PGA0"].stop()); + gain = wbx_rx_gain_ranges["PGA0"].clip(gain); //convert to attenuation and update iobits for atr float attn = wbx_rx_gain_ranges["PGA0"].stop() - gain; //calculate the attenuation - int attn_code = int(floor(attn*2)); + int attn_code = boost::math::iround(attn*2); int iobits = ((~attn_code) << RX_ATTN_SHIFT) & RX_ATTN_MASK; - if (wbx_debug) std::cerr << boost::format( "WBX Attenuation: %f dB, Code: %d, IO Bits %x, Mask: %x" ) % attn % attn_code % (iobits & RX_ATTN_MASK) % RX_ATTN_MASK << std::endl; @@ -220,7 +219,7 @@ static int rx_pga0_gain_to_iobits(float &gain){ static float tx_pga0_gain_to_dac_volts(float &gain){ //clip the input - gain = std::clip(gain, wbx_tx_gain_ranges["PGA0"].start(), wbx_tx_gain_ranges["PGA0"].stop()); + gain = wbx_tx_gain_ranges["PGA0"].clip(gain); //voltage level constants static const float max_volts = float(0.5), min_volts = float(1.4); @@ -328,7 +327,7 @@ double wbx_xcvr::set_lo_freq( ) % (target_freq/1e6) << std::endl; //clip the input - target_freq = std::clip(target_freq, wbx_freq_range.start(), wbx_freq_range.stop()); + target_freq = wbx_freq_range.clip(target_freq); //map prescaler setting to mininmum integer divider (N) values (pg.18 prescaler) static const uhd::dict prescaler_to_min_int_div = map_list_of diff --git a/host/lib/usrp/dboard/db_xcvr2450.cpp b/host/lib/usrp/dboard/db_xcvr2450.cpp index d3084b0a0..a3a1e6242 100644 --- a/host/lib/usrp/dboard/db_xcvr2450.cpp +++ b/host/lib/usrp/dboard/db_xcvr2450.cpp @@ -84,7 +84,11 @@ static const uhd::dict xcvr_tx_gain_ranges = map_list ("BB", gain_range_t(0, 5, 1.5)) ; static const uhd::dict xcvr_rx_gain_ranges = map_list_of - ("LNA", gain_range_t(0, 30.5, 15)) + ("LNA", gain_range_t(list_of + (range_t(0)) + (range_t(15)) + (range_t(30.5)) + )) ("VGA", gain_range_t(0, 62, 2.0)) ; @@ -262,8 +266,8 @@ void xcvr2450::update_atr(void){ **********************************************************************/ void xcvr2450::set_lo_freq(double target_freq){ - //clip the input to the range (TODO FIXME not right) - target_freq = std::clip(target_freq, xcvr_freq_range.start(), xcvr_freq_range.stop()); + //clip the input to the range + target_freq = xcvr_freq_range.clip(target_freq); //variables used in the calculation below double scaler = xcvr2450::is_highband(target_freq)? (4.0/5.0) : (4.0/3.0); -- cgit v1.2.3 From 06e2e1e02fdc93eb00f6cf0d532ffc2943feaf06 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Wed, 10 Nov 2010 22:47:45 -0800 Subject: uhd: made unit test for meta range and fixed bug --- host/include/uhd/types/ranges.ipp | 4 +-- host/test/CMakeLists.txt | 1 + host/test/ranges_test.cpp | 57 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 host/test/ranges_test.cpp (limited to 'host/include') diff --git a/host/include/uhd/types/ranges.ipp b/host/include/uhd/types/ranges.ipp index 7fd1bf2eb..8b602a24d 100644 --- a/host/include/uhd/types/ranges.ipp +++ b/host/include/uhd/types/ranges.ipp @@ -130,10 +130,10 @@ namespace uhd{ range_t last = this->front(); BOOST_FOREACH(const range_t &r, (*this)){ //steps at each range - if (r.step() != T(0)) non_zero_steps.push_back(r.step()); + if (r.step() > T(0)) non_zero_steps.push_back(r.step()); //and steps in-between ranges T ibtw_step = r.start() - last.stop(); - if (ibtw_step != T(0)) non_zero_steps.push_back(ibtw_step); + if (ibtw_step > T(0)) non_zero_steps.push_back(ibtw_step); //store ref to last last = r; } diff --git a/host/test/CMakeLists.txt b/host/test/CMakeLists.txt index d67399e5b..5d7433c67 100644 --- a/host/test/CMakeLists.txt +++ b/host/test/CMakeLists.txt @@ -26,6 +26,7 @@ SET(test_sources dict_test.cpp error_test.cpp gain_group_test.cpp + ranges_test.cpp subdev_spec_test.cpp time_spec_test.cpp tune_helper_test.cpp diff --git a/host/test/ranges_test.cpp b/host/test/ranges_test.cpp new file mode 100644 index 000000000..ad61867e1 --- /dev/null +++ b/host/test/ranges_test.cpp @@ -0,0 +1,57 @@ +// +// 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 . +// + +#include +#include +#include + +using namespace uhd; + +static const double tolerance = 0.001; + +BOOST_AUTO_TEST_CASE(test_ranges_bounds){ + meta_range_t mr; + mr.push_back(range_t(-1.0, +1.0, 0.1)); + BOOST_CHECK_CLOSE(mr.start(), -1.0, tolerance); + BOOST_CHECK_CLOSE(mr.stop(), +1.0, tolerance); + BOOST_CHECK_CLOSE(mr.step(), 0.1, tolerance); + + mr.push_back(range_t(40.0, 60.0, 1.0)); + BOOST_CHECK_CLOSE(mr.start(), -1.0, tolerance); + BOOST_CHECK_CLOSE(mr.stop(), 60.0, tolerance); + BOOST_CHECK_CLOSE(mr.step(), 0.1, tolerance); + + BOOST_CHECK_EQUAL(mr.size(), 2); + + BOOST_CHECK_CLOSE(mr[0].start(), -1.0, tolerance); + BOOST_CHECK_CLOSE(mr[0].stop(), +1.0, tolerance); + BOOST_CHECK_CLOSE(mr[0].step(), 0.1, tolerance); +} + +BOOST_AUTO_TEST_CASE(test_ranges_clip){ + meta_range_t mr; + mr.push_back(range_t(-1.0, +1.0, 0.1)); + mr.push_back(range_t(40.0, 60.0, 1.0)); + + BOOST_CHECK_CLOSE(mr.clip(-30.0), -1.0, tolerance); + BOOST_CHECK_CLOSE(mr.clip(70.0), 60.0, tolerance); + BOOST_CHECK_CLOSE(mr.clip(20.0), 1.0, tolerance); + BOOST_CHECK_CLOSE(mr.clip(50.0), 50.0, tolerance); + + BOOST_CHECK_CLOSE(mr.clip(50.9, false), 50.9, tolerance); + BOOST_CHECK_CLOSE(mr.clip(50.9, true), 51.0, tolerance); +} -- cgit v1.2.3