From 466bb0a7c25a2a4d252582f812edc83ba6facbfe Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 4 Jan 2011 19:29:39 -0800 Subject: uhd: switched the unit test to the new convert API, implemented in vrt pkt handler --- host/test/CMakeLists.txt | 2 +- host/test/convert_test.cpp | 234 +++++++++++++++++++++++++++++++++++++ host/test/convert_types_test.cpp | 245 --------------------------------------- 3 files changed, 235 insertions(+), 246 deletions(-) create mode 100644 host/test/convert_test.cpp delete mode 100644 host/test/convert_types_test.cpp (limited to 'host/test') diff --git a/host/test/CMakeLists.txt b/host/test/CMakeLists.txt index bdbde4b2c..581799d98 100644 --- a/host/test/CMakeLists.txt +++ b/host/test/CMakeLists.txt @@ -22,7 +22,7 @@ SET(test_sources addr_test.cpp buffer_test.cpp byteswap_test.cpp - convert_types_test.cpp + convert_test.cpp dict_test.cpp error_test.cpp gain_group_test.cpp diff --git a/host/test/convert_test.cpp b/host/test/convert_test.cpp new file mode 100644 index 000000000..de0245c1d --- /dev/null +++ b/host/test/convert_test.cpp @@ -0,0 +1,234 @@ +// +// 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 +#include +#include +#include +#include +#include + +using namespace uhd; + +//typedefs for complex types +typedef std::complex sc16_t; +typedef std::complex fc32_t; + +#define MY_CHECK_CLOSE(a, b, f) if ((std::abs(a) > (f) and std::abs(b) > (f))) \ + BOOST_CHECK_CLOSE_FRACTION(a, b, f) + +/*********************************************************************** + * Loopback runner: + * convert input buffer into intermediate buffer + * convert intermediate buffer into output buffer + **********************************************************************/ +template static void loopback( + size_t nsamps, + const io_type_t &io_type, + const otw_type_t &otw_type, + const Range &input, + Range &output +){ + //item32 is largest device type + std::vector interm(nsamps); + + convert::input_type input0(1, &input[0]), input1(1, &interm[0]); + convert::output_type output0(1, &interm[0]), output1(1, &output[0]); + + //convert to intermediate type + convert::io_type_to_otw_type( + io_type, otw_type, input0, output0, nsamps + ); + + //convert back to host type + convert::otw_type_to_io_type( + io_type, otw_type, input1, output1, nsamps + ); +} + +/*********************************************************************** + * Test short conversion + **********************************************************************/ +static void test_convert_types_sc16( + size_t nsamps, + const io_type_t &io_type, + const otw_type_t &otw_type +){ + //fill the input samples + std::vector input(nsamps), output(nsamps); + BOOST_FOREACH(sc16_t &in, input) in = sc16_t( + std::rand()-(RAND_MAX/2), + std::rand()-(RAND_MAX/2) + ); + + //run the loopback and test + loopback(nsamps, io_type, otw_type, input, output); + BOOST_CHECK_EQUAL_COLLECTIONS(input.begin(), input.end(), output.begin(), output.end()); +} + +BOOST_AUTO_TEST_CASE(test_convert_types_be_sc16){ + io_type_t io_type(io_type_t::COMPLEX_INT16); + otw_type_t otw_type; + otw_type.byteorder = otw_type_t::BO_BIG_ENDIAN; + otw_type.width = 16; + + //try various lengths to test edge cases + for (size_t nsamps = 1; nsamps < 16; nsamps++){ + test_convert_types_sc16(nsamps, io_type, otw_type); + } +} + +BOOST_AUTO_TEST_CASE(test_convert_types_le_sc16){ + io_type_t io_type(io_type_t::COMPLEX_INT16); + otw_type_t otw_type; + otw_type.byteorder = otw_type_t::BO_LITTLE_ENDIAN; + otw_type.width = 16; + + //try various lengths to test edge cases + for (size_t nsamps = 1; nsamps < 16; nsamps++){ + test_convert_types_sc16(nsamps, io_type, otw_type); + } +} + +/*********************************************************************** + * Test float conversion + **********************************************************************/ +static void test_convert_types_fc32( + size_t nsamps, + const io_type_t &io_type, + const otw_type_t &otw_type +){ + //fill the input samples + std::vector input(nsamps), output(nsamps); + BOOST_FOREACH(fc32_t &in, input) in = fc32_t( + (std::rand()/float(RAND_MAX/2)) - 1, + (std::rand()/float(RAND_MAX/2)) - 1 + ); + + //run the loopback and test + loopback(nsamps, io_type, otw_type, input, output); + for (size_t i = 0; i < nsamps; i++){ + MY_CHECK_CLOSE(input[i].real(), output[i].real(), float(0.01)); + MY_CHECK_CLOSE(input[i].imag(), output[i].imag(), float(0.01)); + } +} + +BOOST_AUTO_TEST_CASE(test_convert_types_be_fc32){ + io_type_t io_type(io_type_t::COMPLEX_FLOAT32); + otw_type_t otw_type; + otw_type.byteorder = otw_type_t::BO_BIG_ENDIAN; + otw_type.width = 16; + + //try various lengths to test edge cases + for (size_t nsamps = 1; nsamps < 16; nsamps++){ + test_convert_types_fc32(nsamps, io_type, otw_type); + } +} + +BOOST_AUTO_TEST_CASE(test_convert_types_le_fc32){ + io_type_t io_type(io_type_t::COMPLEX_FLOAT32); + otw_type_t otw_type; + otw_type.byteorder = otw_type_t::BO_LITTLE_ENDIAN; + otw_type.width = 16; + + //try various lengths to test edge cases + for (size_t nsamps = 1; nsamps < 16; nsamps++){ + test_convert_types_fc32(nsamps, io_type, otw_type); + } +} + +/*********************************************************************** + * Test float to short conversion loopback + **********************************************************************/ +BOOST_AUTO_TEST_CASE(test_convert_types_fc32_to_sc16){ + io_type_t io_type_in(io_type_t::COMPLEX_FLOAT32); + io_type_t io_type_out(io_type_t::COMPLEX_INT16); + + otw_type_t otw_type; + otw_type.byteorder = otw_type_t::BO_NATIVE; + otw_type.width = 16; + + const size_t nsamps = 13; + std::vector input(nsamps); + BOOST_FOREACH(fc32_t &in, input) in = fc32_t( + (std::rand()/float(RAND_MAX/2)) - 1, + (std::rand()/float(RAND_MAX/2)) - 1 + ); + std::vector interm(nsamps); + std::vector output(nsamps); + + convert::input_type input0(1, &input[0]), input1(1, &interm[0]); + convert::output_type output0(1, &interm[0]), output1(1, &output[0]); + + //convert float to intermediate + convert::io_type_to_otw_type( + io_type_in, otw_type, input0, output0, nsamps + ); + + //convert intermediate to short + convert::otw_type_to_io_type( + io_type_out, otw_type, input1, output1, nsamps + ); + + //test that the inputs and outputs match + for (size_t i = 0; i < nsamps; i++){ + MY_CHECK_CLOSE(input[i].real(), output[i].real()/float(32767), float(0.01)); + MY_CHECK_CLOSE(input[i].imag(), output[i].imag()/float(32767), float(0.01)); + } +} + +/*********************************************************************** + * Test short to float conversion loopback + **********************************************************************/ +BOOST_AUTO_TEST_CASE(test_convert_types_sc16_to_fc32){ + io_type_t io_type_in(io_type_t::COMPLEX_INT16); + io_type_t io_type_out(io_type_t::COMPLEX_FLOAT32); + + otw_type_t otw_type; + otw_type.byteorder = otw_type_t::BO_NATIVE; + otw_type.width = 16; + + const size_t nsamps = 13; + std::vector input(nsamps); + BOOST_FOREACH(sc16_t &in, input) in = sc16_t( + std::rand()-(RAND_MAX/2), + std::rand()-(RAND_MAX/2) + ); + std::vector interm(nsamps); + std::vector output(nsamps); + + convert::input_type input0(1, &input[0]), input1(1, &interm[0]); + convert::output_type output0(1, &interm[0]), output1(1, &output[0]); + + //convert short to intermediate + convert::io_type_to_otw_type( + io_type_in, otw_type, input0, output0, nsamps + ); + + //convert intermediate to float + convert::otw_type_to_io_type( + io_type_out, otw_type, input1, output1, nsamps + ); + + //test that the inputs and outputs match + for (size_t i = 0; i < nsamps; i++){ + MY_CHECK_CLOSE(input[i].real()/float(32767), output[i].real(), float(0.01)); + MY_CHECK_CLOSE(input[i].imag()/float(32767), output[i].imag(), float(0.01)); + } +} diff --git a/host/test/convert_types_test.cpp b/host/test/convert_types_test.cpp deleted file mode 100644 index 378e184de..000000000 --- a/host/test/convert_types_test.cpp +++ /dev/null @@ -1,245 +0,0 @@ -// -// 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 -#include -#include -#include -#include -#include - -using namespace uhd; - -//typedefs for complex types -typedef std::complex sc16_t; -typedef std::complex fc32_t; - -//extract pointer to POD since using &vector.front() throws in MSVC -template void * pod2ptr(T &pod){ - return boost::asio::buffer_cast(boost::asio::buffer(pod)); -} -template const void * pod2ptr(const T &pod){ - return boost::asio::buffer_cast(boost::asio::buffer(pod)); -} - -#define MY_CHECK_CLOSE(a, b, f) if ((std::abs(a) > (f) and std::abs(b) > (f))) \ - BOOST_CHECK_CLOSE_FRACTION(a, b, f) - -/*********************************************************************** - * Loopback runner: - * convert input buffer into intermediate buffer - * convert intermediate buffer into output buffer - **********************************************************************/ -template static void loopback( - size_t nsamps, - const io_type_t &io_type, - const otw_type_t &otw_type, - const Range &input, - Range &output -){ - //item32 is largest device type - std::vector dev(nsamps); - - //convert to dev type - transport::convert_io_type_to_otw_type( - pod2ptr(input), io_type, - pod2ptr(dev), otw_type, - nsamps - ); - - //convert back to host type - transport::convert_otw_type_to_io_type( - pod2ptr(dev), otw_type, - pod2ptr(output), io_type, - nsamps - ); -} - -/*********************************************************************** - * Test short conversion - **********************************************************************/ -static void test_convert_types_sc16( - size_t nsamps, - const io_type_t &io_type, - const otw_type_t &otw_type -){ - //fill the input samples - std::vector input(nsamps), output(nsamps); - BOOST_FOREACH(sc16_t &in, input) in = sc16_t( - std::rand()-(RAND_MAX/2), - std::rand()-(RAND_MAX/2) - ); - - //run the loopback and test - loopback(nsamps, io_type, otw_type, input, output); - BOOST_CHECK_EQUAL_COLLECTIONS(input.begin(), input.end(), output.begin(), output.end()); -} - -BOOST_AUTO_TEST_CASE(test_convert_types_be_sc16){ - io_type_t io_type(io_type_t::COMPLEX_INT16); - otw_type_t otw_type; - otw_type.byteorder = otw_type_t::BO_BIG_ENDIAN; - otw_type.width = 16; - - //try various lengths to test edge cases - for (size_t nsamps = 0; nsamps < 16; nsamps++){ - test_convert_types_sc16(nsamps, io_type, otw_type); - } -} - -BOOST_AUTO_TEST_CASE(test_convert_types_le_sc16){ - io_type_t io_type(io_type_t::COMPLEX_INT16); - otw_type_t otw_type; - otw_type.byteorder = otw_type_t::BO_LITTLE_ENDIAN; - otw_type.width = 16; - - //try various lengths to test edge cases - for (size_t nsamps = 0; nsamps < 16; nsamps++){ - test_convert_types_sc16(nsamps, io_type, otw_type); - } -} - -/*********************************************************************** - * Test float conversion - **********************************************************************/ -static void test_convert_types_fc32( - size_t nsamps, - const io_type_t &io_type, - const otw_type_t &otw_type -){ - //fill the input samples - std::vector input(nsamps), output(nsamps); - BOOST_FOREACH(fc32_t &in, input) in = fc32_t( - (std::rand()/float(RAND_MAX/2)) - 1, - (std::rand()/float(RAND_MAX/2)) - 1 - ); - - //run the loopback and test - loopback(nsamps, io_type, otw_type, input, output); - for (size_t i = 0; i < nsamps; i++){ - MY_CHECK_CLOSE(input[i].real(), output[i].real(), float(0.01)); - MY_CHECK_CLOSE(input[i].imag(), output[i].imag(), float(0.01)); - } -} - -BOOST_AUTO_TEST_CASE(test_convert_types_be_fc32){ - io_type_t io_type(io_type_t::COMPLEX_FLOAT32); - otw_type_t otw_type; - otw_type.byteorder = otw_type_t::BO_BIG_ENDIAN; - otw_type.width = 16; - - //try various lengths to test edge cases - for (size_t nsamps = 0; nsamps < 16; nsamps++){ - test_convert_types_fc32(nsamps, io_type, otw_type); - } -} - -BOOST_AUTO_TEST_CASE(test_convert_types_le_fc32){ - io_type_t io_type(io_type_t::COMPLEX_FLOAT32); - otw_type_t otw_type; - otw_type.byteorder = otw_type_t::BO_LITTLE_ENDIAN; - otw_type.width = 16; - - //try various lengths to test edge cases - for (size_t nsamps = 0; nsamps < 16; nsamps++){ - test_convert_types_fc32(nsamps, io_type, otw_type); - } -} - -/*********************************************************************** - * Test float to short conversion loopback - **********************************************************************/ -BOOST_AUTO_TEST_CASE(test_convert_types_fc32_to_sc16){ - io_type_t io_type_in(io_type_t::COMPLEX_FLOAT32); - io_type_t io_type_out(io_type_t::COMPLEX_INT16); - - otw_type_t otw_type; - otw_type.byteorder = otw_type_t::BO_NATIVE; - otw_type.width = 16; - - const size_t nsamps = 13; - std::vector input(nsamps); - BOOST_FOREACH(fc32_t &in, input) in = fc32_t( - (std::rand()/float(RAND_MAX/2)) - 1, - (std::rand()/float(RAND_MAX/2)) - 1 - ); - - //convert float to dev - std::vector tmp(nsamps); - transport::convert_io_type_to_otw_type( - pod2ptr(input), io_type_in, - pod2ptr(tmp), otw_type, - nsamps - ); - - //convert dev to short - std::vector output(nsamps); - transport::convert_otw_type_to_io_type( - pod2ptr(tmp), otw_type, - pod2ptr(output), io_type_out, - nsamps - ); - - //test that the inputs and outputs match - for (size_t i = 0; i < nsamps; i++){ - MY_CHECK_CLOSE(input[i].real(), output[i].real()/float(32767), float(0.01)); - MY_CHECK_CLOSE(input[i].imag(), output[i].imag()/float(32767), float(0.01)); - } -} - -/*********************************************************************** - * Test short to float conversion loopback - **********************************************************************/ -BOOST_AUTO_TEST_CASE(test_convert_types_sc16_to_fc32){ - io_type_t io_type_in(io_type_t::COMPLEX_INT16); - io_type_t io_type_out(io_type_t::COMPLEX_FLOAT32); - - otw_type_t otw_type; - otw_type.byteorder = otw_type_t::BO_NATIVE; - otw_type.width = 16; - - const size_t nsamps = 13; - std::vector input(nsamps); - BOOST_FOREACH(sc16_t &in, input) in = sc16_t( - std::rand()-(RAND_MAX/2), - std::rand()-(RAND_MAX/2) - ); - - //convert short to dev - std::vector tmp(nsamps); - transport::convert_io_type_to_otw_type( - pod2ptr(input), io_type_in, - pod2ptr(tmp), otw_type, - nsamps - ); - - //convert dev to float - std::vector output(nsamps); - transport::convert_otw_type_to_io_type( - pod2ptr(tmp), otw_type, - pod2ptr(output), io_type_out, - nsamps - ); - - //test that the inputs and outputs match - for (size_t i = 0; i < nsamps; i++){ - MY_CHECK_CLOSE(input[i].real()/float(32767), output[i].real(), float(0.01)); - MY_CHECK_CLOSE(input[i].imag()/float(32767), output[i].imag(), float(0.01)); - } -} -- cgit v1.2.3