From d835d348b3d2ba5e790855dde2d7824234e3c531 Mon Sep 17 00:00:00 2001 From: Aaron Rossetto Date: Wed, 19 Jan 2022 13:52:51 -0600 Subject: convert: Minor cleanup This commit implements some minor cleanup of various converter- and convert test-related code: * Improves the log messages regarding which converter was returned for a request. * Modifies the result checking code in the converter tests to only report an out-of-range sample error once, rather than reporting every out-of-range sample encountered during the test. This vastly cuts down on the output when a conversion has failed. * Adds a function `reverse_converter()` which, given a `convert::id_type` describing a conversion from C1 to C2, returns a `convert::id_type` describing the reverse conversion (C2 to C1). * Removes two redundant test cases from the converter test. --- host/tests/convert_test.cpp | 149 +++++++++++--------------------------------- 1 file changed, 36 insertions(+), 113 deletions(-) (limited to 'host/tests/convert_test.cpp') diff --git a/host/tests/convert_test.cpp b/host/tests/convert_test.cpp index ac0340cc1..f002b16a2 100644 --- a/host/tests/convert_test.cpp +++ b/host/tests/convert_test.cpp @@ -22,10 +22,25 @@ typedef std::complex fc64_t; #define MY_CHECK_CLOSE(a, b, f) \ { \ - BOOST_CHECK_MESSAGE(std::abs((a) - (b)) < f, \ + static bool error_encountered = false; \ + if(!error_encountered && (std::abs((a) - (b)) >= f)) { \ + BOOST_ERROR( \ "\n\t" << #a << " (" << (a) << ") error " << #b << " (" << (b) << ")"); \ + error_encountered = true; \ + } \ } +// Given a converter ID describing a conversion from input type to +// output type, return the 'reverse' converter ID from output type to +// input type +static convert::id_type reverse_converter(const convert::id_type& in) +{ + convert::id_type out = in; + std::swap(out.input_format, out.output_format); + std::swap(out.num_inputs, out.num_outputs); + return out; +} + /*********************************************************************** * Loopback runner: * convert input buffer into intermediate buffer @@ -43,8 +58,8 @@ static void loopback(size_t nsamps, // make this buffer large enough for all test types std::vector interm(nsamps); - std::vector input0(1, &input[0]), input1(1, &interm[0]); - std::vector output0(1, &interm[0]), output1(1, &output[0]); + std::vector input0{&input[0]}, input1{&interm[0]}; + std::vector output0{&interm[0]}, output1{&output[0]}; // convert to intermediate type convert::converter::sptr c0 = convert::get_converter(in_id, prio_in)(); @@ -66,17 +81,17 @@ static void test_convert_types_sc16( // fill the input samples std::vector input(nsamps), output(nsamps); for (sc16_t& in : input) + { in = sc16_t( short((float((std::rand()) / (double(RAND_MAX) / 2)) - 1) * 32767 / extra_div) & mask, short((float((std::rand()) / (double(RAND_MAX) / 2)) - 1) * 32767 / extra_div) & mask); + } // run the loopback and test convert::id_type in_id = id; - convert::id_type out_id = id; - std::swap(out_id.input_format, out_id.output_format); - std::swap(out_id.num_inputs, out_id.num_outputs); + convert::id_type out_id = reverse_converter(id); loopback(nsamps, in_id, out_id, input, output); BOOST_CHECK_EQUAL_COLLECTIONS( input.begin(), input.end(), output.begin(), output.end()); @@ -136,15 +151,15 @@ static void test_convert_types_for_floats( // fill the input samples std::vector input(nsamps), output(nsamps); for (data_type& in : input) + { in = data_type( ((std::rand() / (value_type(RAND_MAX) / 2)) - 1) * float(extra_scale), ((std::rand() / (value_type(RAND_MAX) / 2)) - 1) * float(extra_scale)); + } // run the loopback and test convert::id_type in_id = id; - convert::id_type out_id = id; - std::swap(out_id.input_format, out_id.output_format); - std::swap(out_id.num_inputs, out_id.num_outputs); + convert::id_type out_id = reverse_converter(id); // make a list of all prio: best/generic combos typedef std::pair int_pair_t; @@ -350,95 +365,6 @@ BOOST_AUTO_TEST_CASE(test_convert_types_fc32_with_fc32_chdr) } } -/*********************************************************************** - * Test float to short conversion loopback - **********************************************************************/ -BOOST_AUTO_TEST_CASE(test_convert_types_fc32_to_sc16) -{ - convert::id_type in_id; - in_id.input_format = "fc32"; - in_id.num_inputs = 1; - in_id.output_format = "sc16_item32_le"; - in_id.num_outputs = 1; - - convert::id_type out_id; - out_id.input_format = "sc16_item32_le"; - out_id.num_inputs = 1; - out_id.output_format = "sc16"; - out_id.num_outputs = 1; - - const size_t nsamps = 13; - std::vector input(nsamps); - for (fc32_t& in : input) - in = fc32_t( - (std::rand() / (RAND_MAX / 2.0)) - 1, (std::rand() / (RAND_MAX / 2.0)) - 1); - std::vector interm(nsamps); - std::vector output(nsamps); - - std::vector input0(1, &input[0]), input1(1, &interm[0]); - std::vector output0(1, &interm[0]), output1(1, &output[0]); - - // convert float to intermediate - convert::converter::sptr c0 = convert::get_converter(in_id)(); - c0->set_scalar(32767.); - c0->conv(input0, output0, nsamps); - - // convert intermediate to short - convert::converter::sptr c1 = convert::get_converter(out_id)(); - c1->set_scalar(1 / 32767.); - c1->conv(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) -{ - convert::id_type in_id; - in_id.input_format = "sc16"; - in_id.num_inputs = 1; - in_id.output_format = "sc16_item32_le"; - in_id.num_outputs = 1; - - convert::id_type out_id; - out_id.input_format = "sc16_item32_le"; - out_id.num_inputs = 1; - out_id.output_format = "fc32"; - out_id.num_outputs = 1; - - const size_t nsamps = 13; - std::vector input(nsamps); - for (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); - - std::vector input0(1, &input[0]), input1(1, &interm[0]); - std::vector output0(1, &interm[0]), output1(1, &output[0]); - - // convert short to intermediate - convert::converter::sptr c0 = convert::get_converter(in_id)(); - c0->set_scalar(32767.); - c0->conv(input0, output0, nsamps); - - // convert intermediate to float - convert::converter::sptr c1 = convert::get_converter(out_id)(); - c1->set_scalar(1 / 32767.); - c1->conv(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)); - } -} - /*********************************************************************** * Test sc8 conversions **********************************************************************/ @@ -509,16 +435,15 @@ static void test_convert_types_u8(size_t nsamps, convert::id_type& id) { // fill the input samples std::vector input(nsamps), output(nsamps); - for (uint8_t& in : input) + for (uint8_t& in : input) { in = uint8_t(std::rand() & 0xFF); + } // uint32_t d = 48; // for(uint8_t &in: input) in = d++; // run the loopback and test convert::id_type in_id = id; - convert::id_type out_id = id; - std::swap(out_id.input_format, out_id.output_format); - std::swap(out_id.num_inputs, out_id.num_outputs); + convert::id_type out_id = reverse_converter(id); loopback(nsamps, in_id, out_id, input, output); BOOST_CHECK_EQUAL_COLLECTIONS( input.begin(), input.end(), output.begin(), output.end()); @@ -565,14 +490,13 @@ static void test_convert_types_s8(size_t nsamps, convert::id_type& id) { // fill the input samples std::vector input(nsamps), output(nsamps); - for (int8_t& in : input) + for (int8_t& in : input) { in = int8_t(std::rand() & 0xFF); + } // run the loopback and test convert::id_type in_id = id; - convert::id_type out_id = id; - std::swap(out_id.input_format, out_id.output_format); - std::swap(out_id.num_inputs, out_id.num_outputs); + convert::id_type out_id = reverse_converter(id); loopback(nsamps, in_id, out_id, input, output); BOOST_CHECK_EQUAL_COLLECTIONS( input.begin(), input.end(), output.begin(), output.end()); @@ -619,14 +543,13 @@ static void test_convert_types_s16(size_t nsamps, convert::id_type& id) { // fill the input samples std::vector input(nsamps), output(nsamps); - for (int16_t& in : input) + for (int16_t& in : input) { in = int16_t(std::rand() & 0xFFFF); + } // run the loopback and test convert::id_type in_id = id; - convert::id_type out_id = id; - std::swap(out_id.input_format, out_id.output_format); - std::swap(out_id.num_inputs, out_id.num_outputs); + convert::id_type out_id = reverse_converter(id); loopback(nsamps, in_id, out_id, input, output); BOOST_CHECK_EQUAL_COLLECTIONS( input.begin(), input.end(), output.begin(), output.end()); @@ -674,14 +597,14 @@ static void test_convert_types_fc32(size_t nsamps, convert::id_type& id) // fill the input samples std::vector> input(nsamps), output(nsamps); for (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 convert::id_type in_id = id; - convert::id_type out_id = id; - std::swap(out_id.input_format, out_id.output_format); - std::swap(out_id.num_inputs, out_id.num_outputs); + convert::id_type out_id = reverse_converter(id); loopback(nsamps, in_id, out_id, input, output); for (size_t i = 0; i < nsamps; i++) { MY_CHECK_CLOSE(input[i].real(), output[i].real(), float(1. / (1 << 16))); -- cgit v1.2.3