From d0039bd4128eed56acccce7b20c913afa8cb7381 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 8 Jun 2010 00:00:54 -0700 Subject: Replaced convert types with generated convert types that handles more cases. Added unit test for generated converter. --- host/test/CMakeLists.txt | 1 + host/test/convert_types_test.cpp | 123 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create 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 7fd3dd401..74f3376e6 100644 --- a/host/test/CMakeLists.txt +++ b/host/test/CMakeLists.txt @@ -23,6 +23,7 @@ ADD_EXECUTABLE(main_test addr_test.cpp buffer_test.cpp byteswap_test.cpp + convert_types_test.cpp dict_test.cpp error_test.cpp gain_handler_test.cpp diff --git a/host/test/convert_types_test.cpp b/host/test/convert_types_test.cpp new file mode 100644 index 000000000..096da441b --- /dev/null +++ b/host/test/convert_types_test.cpp @@ -0,0 +1,123 @@ +// +// 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 + +using namespace uhd; + +template +void loopback( + const io_type_t &io_type, + const otw_type_t &otw_type, + const host_type *input, + host_type *output +){ + dev_type dev[nsamps]; + + //convert to dev type + transport::convert_io_type_to_otw_type( + input, io_type, + dev, otw_type, + nsamps + ); + + //convert back to host type + transport::convert_otw_type_to_io_type( + dev, otw_type, + output, io_type, + nsamps + ); +} + +typedef std::complex sc16_t; + +BOOST_AUTO_TEST_CASE(test_convert_types_be_sc16){ + sc16_t in_sc16[] = { + sc16_t(0, -1234), sc16_t(4321, 1234), + sc16_t(9876, -4567), sc16_t(8912, 0) + }, out_sc16[4]; + + 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; + + loopback(io_type, otw_type, in_sc16, out_sc16); + BOOST_CHECK_EQUAL_COLLECTIONS(in_sc16, in_sc16+4, out_sc16, out_sc16+4); +} + +BOOST_AUTO_TEST_CASE(test_convert_types_le_sc16){ + sc16_t in_sc16[] = { + sc16_t(0, -1234), sc16_t(4321, 1234), + sc16_t(9876, -4567), sc16_t(8912, 0) + }, out_sc16[4]; + + 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; + + loopback(io_type, otw_type, in_sc16, out_sc16); + BOOST_CHECK_EQUAL_COLLECTIONS(in_sc16, in_sc16+4, out_sc16, out_sc16+4); +} + +typedef std::complex fc32_t; + +#define BOOST_CHECK_CLOSE_COMPLEX(a1, a2, p) \ + BOOST_CHECK_CLOSE(a1.real(), a2.real(), p); \ + BOOST_CHECK_CLOSE(a1.imag(), a2.imag(), p); + +BOOST_AUTO_TEST_CASE(test_convert_types_be_fc32){ + fc32_t in_fc32[] = { + fc32_t(0, -0.2), fc32_t(0.03, -0.16), + fc32_t(1.0, .45), fc32_t(0.09, 0) + }, out_fc32[4]; + + 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; + + loopback(io_type, otw_type, in_fc32, out_fc32); + + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[0], out_fc32[0], 0.1); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[1], out_fc32[1], 0.1); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[2], out_fc32[2], 0.1); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[3], out_fc32[3], 0.1); +} + +BOOST_AUTO_TEST_CASE(test_convert_types_le_fc32){ + fc32_t in_fc32[] = { + fc32_t(0, -0.2), fc32_t(0.03, -0.16), + fc32_t(1.0, .45), fc32_t(0.09, 0) + }, out_fc32[4]; + + 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; + + loopback(io_type, otw_type, in_fc32, out_fc32); + + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[0], out_fc32[0], 0.1); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[1], out_fc32[1], 0.1); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[2], out_fc32[2], 0.1); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[3], out_fc32[3], 0.1); +} -- cgit v1.2.3 From 37cf8a77e86de2d9247738ed6ce7b2534e66e2f8 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 8 Jun 2010 14:55:57 -0700 Subject: float casts to remove msvc warnings --- host/test/convert_types_test.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'host/test') diff --git a/host/test/convert_types_test.cpp b/host/test/convert_types_test.cpp index 096da441b..1587be57f 100644 --- a/host/test/convert_types_test.cpp +++ b/host/test/convert_types_test.cpp @@ -84,10 +84,12 @@ typedef std::complex fc32_t; BOOST_CHECK_CLOSE(a1.real(), a2.real(), p); \ BOOST_CHECK_CLOSE(a1.imag(), a2.imag(), p); +static const float tolerance = float(0.1); + BOOST_AUTO_TEST_CASE(test_convert_types_be_fc32){ fc32_t in_fc32[] = { - fc32_t(0, -0.2), fc32_t(0.03, -0.16), - fc32_t(1.0, .45), fc32_t(0.09, 0) + fc32_t(float(0), float(-0.2)), fc32_t(float(0.03), float(-0.16)), + fc32_t(float(1.0), float(.45)), fc32_t(float(0.09), float(0)) }, out_fc32[4]; io_type_t io_type(io_type_t::COMPLEX_FLOAT32); @@ -97,16 +99,16 @@ BOOST_AUTO_TEST_CASE(test_convert_types_be_fc32){ loopback(io_type, otw_type, in_fc32, out_fc32); - BOOST_CHECK_CLOSE_COMPLEX(in_fc32[0], out_fc32[0], 0.1); - BOOST_CHECK_CLOSE_COMPLEX(in_fc32[1], out_fc32[1], 0.1); - BOOST_CHECK_CLOSE_COMPLEX(in_fc32[2], out_fc32[2], 0.1); - BOOST_CHECK_CLOSE_COMPLEX(in_fc32[3], out_fc32[3], 0.1); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[0], out_fc32[0], tolerance); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[1], out_fc32[1], tolerance); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[2], out_fc32[2], tolerance); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[3], out_fc32[3], tolerance); } BOOST_AUTO_TEST_CASE(test_convert_types_le_fc32){ fc32_t in_fc32[] = { - fc32_t(0, -0.2), fc32_t(0.03, -0.16), - fc32_t(1.0, .45), fc32_t(0.09, 0) + fc32_t(float(0), float(-0.2)), fc32_t(float(0.03), float(-0.16)), + fc32_t(float(1.0), float(.45)), fc32_t(float(0.09), float(0)) }, out_fc32[4]; io_type_t io_type(io_type_t::COMPLEX_FLOAT32); @@ -116,8 +118,8 @@ BOOST_AUTO_TEST_CASE(test_convert_types_le_fc32){ loopback(io_type, otw_type, in_fc32, out_fc32); - BOOST_CHECK_CLOSE_COMPLEX(in_fc32[0], out_fc32[0], 0.1); - BOOST_CHECK_CLOSE_COMPLEX(in_fc32[1], out_fc32[1], 0.1); - BOOST_CHECK_CLOSE_COMPLEX(in_fc32[2], out_fc32[2], 0.1); - BOOST_CHECK_CLOSE_COMPLEX(in_fc32[3], out_fc32[3], 0.1); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[0], out_fc32[0], tolerance); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[1], out_fc32[1], tolerance); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[2], out_fc32[2], tolerance); + BOOST_CHECK_CLOSE_COMPLEX(in_fc32[3], out_fc32[3], tolerance); } -- cgit v1.2.3