From 800f58430fdc577a2edd717d27b5902f75a1b1e4 Mon Sep 17 00:00:00 2001 From: Ben Hilburn Date: Fri, 27 Jun 2014 17:44:04 -0700 Subject: math: Added a new uhd::math namespace + float comparison routines * Float comparison is applied to tuning logic in DSP cores. * Properly using INT_MAX/MIN constants, defined in utils/math.hpp --- host/tests/CMakeLists.txt | 2 + host/tests/fp_compare_delta_test.cpp | 250 +++++++++++++++++++++++++++++++++ host/tests/fp_compare_epsilon_test.cpp | 238 +++++++++++++++++++++++++++++++ 3 files changed, 490 insertions(+) create mode 100644 host/tests/fp_compare_delta_test.cpp create mode 100644 host/tests/fp_compare_epsilon_test.cpp (limited to 'host/tests') diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 7c4815004..62544b69b 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -31,6 +31,8 @@ SET(test_sources cast_test.cpp dict_test.cpp error_test.cpp + fp_compare_delta_test.cpp + fp_compare_epsilon_test.cpp gain_group_test.cpp msg_test.cpp property_test.cpp diff --git a/host/tests/fp_compare_delta_test.cpp b/host/tests/fp_compare_delta_test.cpp new file mode 100644 index 000000000..9b009a79d --- /dev/null +++ b/host/tests/fp_compare_delta_test.cpp @@ -0,0 +1,250 @@ +// +// Copyright 2014 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 + +using namespace uhd::math::fp_compare; + +BOOST_AUTO_TEST_CASE(fp_compare_delta_constructors) { + // Test default constructor + fp_compare_delta alpha = fp_compare_delta(7457392.0); + fp_compare_delta beta = fp_compare_delta(alpha._value); + BOOST_CHECK_EQUAL(alpha._value, beta._value); + BOOST_CHECK_EQUAL(alpha._delta, beta._delta); + + // Test constructor with specified delta + fp_compare_delta foxtrot = fp_compare_delta(alpha._value, + uhd::math::SINGLE_PRECISION_DELTA); + fp_compare_delta gamma = fp_compare_delta(alpha._value, + 2 * uhd::math::SINGLE_PRECISION_DELTA); + BOOST_CHECK_EQUAL(alpha._delta, foxtrot._delta); + BOOST_CHECK(not (alpha._delta == gamma._delta)); + + // Test copy-constructor + fp_compare_delta charlie = fp_compare_delta(alpha); + BOOST_CHECK_EQUAL(alpha._value, charlie._value); + BOOST_CHECK_EQUAL(alpha._delta, charlie._delta); + + // Test assignment operator + fp_compare_delta delta = beta; + BOOST_CHECK_EQUAL(alpha._value, delta._value); + BOOST_CHECK_EQUAL(alpha._delta, delta._delta); +} + +BOOST_AUTO_TEST_CASE(double_compare_constructors) { + // Test default constructor + fp_compare_delta alpha = fp_compare_delta(45739210286.0101); + fp_compare_delta beta = fp_compare_delta(alpha._value); + BOOST_CHECK_EQUAL(alpha._value, beta._value); + BOOST_CHECK_EQUAL(alpha._delta, beta._delta); + + // Test constructor with specified delta + fp_compare_delta foxtrot = fp_compare_delta(alpha._value, + uhd::math::DOUBLE_PRECISION_DELTA); + fp_compare_delta gamma = fp_compare_delta(alpha._value, 2.0e-6); + BOOST_CHECK_EQUAL(alpha._delta, foxtrot._delta); + BOOST_CHECK(not (alpha._delta == gamma._delta)); + + // Test copy-constructor + fp_compare_delta charlie = fp_compare_delta(alpha); + BOOST_CHECK_EQUAL(alpha._value, charlie._value); + BOOST_CHECK_EQUAL(alpha._delta, charlie._delta); + + // Test assignment operator + fp_compare_delta delta = beta; + BOOST_CHECK_EQUAL(alpha._value, delta._value); + BOOST_CHECK_EQUAL(alpha._delta, delta._delta); +} + +BOOST_AUTO_TEST_CASE(float_equality_operators) { + // Test basic equality operator + fp_compare_delta alpha = fp_compare_delta(1.0); + fp_compare_delta beta = fp_compare_delta(alpha._value); + BOOST_CHECK(alpha == beta); + BOOST_CHECK(alpha == float(alpha._value)); + + // Test equality edge case at difference = delta + fp_compare_delta charlie = fp_compare_delta(alpha._value + + uhd::math::SINGLE_PRECISION_DELTA); + BOOST_CHECK(not (alpha == charlie)); + BOOST_CHECK(not (alpha == float(alpha._value + uhd::math::SINGLE_PRECISION_DELTA))); +} + +BOOST_AUTO_TEST_CASE(double_equality_operators) { + // Test basic equality operator + fp_compare_delta alpha = fp_compare_delta(1.0); + fp_compare_delta beta = fp_compare_delta(alpha._value); + BOOST_CHECK(alpha == beta); + BOOST_CHECK(alpha == double(beta._value)); + + // Test equality edge case at delta = delta + fp_compare_delta charlie = fp_compare_delta(alpha._value + + uhd::math::DOUBLE_PRECISION_DELTA); + BOOST_CHECK(not (alpha == charlie)); + BOOST_CHECK(not (alpha == double(alpha._value + uhd::math::DOUBLE_PRECISION_DELTA))); +} + +BOOST_AUTO_TEST_CASE(float_inequality_operators) { + // Test inequality operator, which is based on equality operator + fp_compare_delta alpha = fp_compare_delta(127.0); + fp_compare_delta beta = fp_compare_delta(alpha._value + 1.19e-3); + + BOOST_CHECK(alpha != beta); + BOOST_CHECK(alpha != float(alpha._value + 1.19e-3)); +} + +BOOST_AUTO_TEST_CASE(double_inequality_operators) { + // Test inequality operator, which is based on equality operator + fp_compare_delta alpha = fp_compare_delta(1.0); + fp_compare_delta beta = fp_compare_delta(alpha._value + 1.19e-5); + + BOOST_CHECK(alpha != beta); + BOOST_CHECK(alpha != double(alpha._value + 1.19e-5)); +} + +BOOST_AUTO_TEST_CASE(float_lessthan_operators) { + // Test less-than operator + fp_compare_delta alpha = fp_compare_delta(274192.7); + fp_compare_delta beta = fp_compare_delta(alpha._value - 0.2); + + BOOST_CHECK(beta < alpha); + BOOST_CHECK(float(alpha._value - 0.2) < alpha); + + // Confirm false less-than case + fp_compare_delta charlie = fp_compare_delta(alpha._value - 1.2); + + BOOST_CHECK(not (alpha < charlie)); + BOOST_CHECK(not (alpha < float(alpha._value - 1.2))); +} + +BOOST_AUTO_TEST_CASE(double_lessthan_operators) { + // Test less-than operator + fp_compare_delta alpha = fp_compare_delta(274192856.762312); + fp_compare_delta beta = fp_compare_delta(alpha._value - 0.0002); + + BOOST_CHECK(beta < alpha); + BOOST_CHECK(double(alpha._value - 0.0002) < alpha); + + // Confirm false less-than case + fp_compare_delta charlie = fp_compare_delta(alpha._value - 1.0012); + + BOOST_CHECK(not (alpha < charlie)); + BOOST_CHECK(not (alpha < double(alpha._value - 1.0012))); +} + +BOOST_AUTO_TEST_CASE(float_lessthanequals_operators) { + // Test that <= correctly reports for equal values + fp_compare_delta alpha = fp_compare_delta(827.3); + fp_compare_delta beta = fp_compare_delta(alpha._value); + + BOOST_CHECK(alpha <= beta); + BOOST_CHECK(alpha <= float(alpha._value)); + + // Test that <= correctly reports for less-than values + fp_compare_delta charlie = fp_compare_delta(alpha._value - 1.2); + + BOOST_CHECK(charlie <= alpha); + BOOST_CHECK(float(alpha._value - 1.2) <= alpha); +} + +BOOST_AUTO_TEST_CASE(double_lessthanequals_operators) { + // Test that <= correctly reports for equal values + fp_compare_delta alpha = fp_compare_delta(837652123.383764); + fp_compare_delta beta = fp_compare_delta(alpha._value); + + BOOST_CHECK(alpha <= beta); + BOOST_CHECK(alpha <= double(alpha._value)); + + // Test that <= correctly reports for less-than values + fp_compare_delta charlie = fp_compare_delta(alpha._value - 0.0012); + + BOOST_CHECK(charlie <= alpha); + BOOST_CHECK(double(alpha._value - 0.0012) <= alpha); +} + +BOOST_AUTO_TEST_CASE(float_greaterthan_operators) { + // Test basic greater-than functionality + fp_compare_delta alpha = fp_compare_delta(98325.4); + fp_compare_delta beta = fp_compare_delta(alpha._value + 0.15); + + BOOST_CHECK(beta > alpha); + BOOST_CHECK(float(alpha._value + 0.15) > alpha); + + // Test false greater-than case + fp_compare_delta charlie = fp_compare_delta(alpha._value + 1.2); + + BOOST_CHECK(not (alpha > charlie)); + BOOST_CHECK(not (alpha > float(alpha._value + 1.2))); +} + +BOOST_AUTO_TEST_CASE(double_greaterthan_operators) { + // Test basic greater-than functionality + fp_compare_delta alpha = fp_compare_delta(643907213.428475); + fp_compare_delta beta = fp_compare_delta(alpha._value + 0.0002); + + BOOST_CHECK(beta > alpha); + BOOST_CHECK(double(alpha._value + 0.0002) > alpha); + + // Test false greater-than case + fp_compare_delta charlie = fp_compare_delta(alpha._value + 0.0012); + + BOOST_CHECK(not (alpha > charlie)); + BOOST_CHECK(not (alpha > double(alpha._value + 0.0012))); +} + +BOOST_AUTO_TEST_CASE(float_greaterthanequals_operators) { + // Test that >= correctly reports for equal values + fp_compare_delta alpha = fp_compare_delta(7834.89); + fp_compare_delta beta = fp_compare_delta(alpha._value); + + BOOST_CHECK(alpha >= beta); + BOOST_CHECK(alpha >= float(alpha._value)); + + // Test that >= correctly reports for greater-than values + fp_compare_delta charlie = fp_compare_delta(alpha._value + 4.8); + + BOOST_CHECK(charlie >= alpha); + BOOST_CHECK(float(alpha._value + 4.8) >= alpha); +} + +BOOST_AUTO_TEST_CASE(double_greaterthanequals_operators) { + // Test that >= correctly reports for equal values + fp_compare_delta alpha = fp_compare_delta(737623834.89843); + fp_compare_delta beta = fp_compare_delta(alpha._value); + + BOOST_CHECK(alpha >= beta); + BOOST_CHECK(alpha >= double(alpha._value)); + + // Test that >= correctly reports for greater-than values + fp_compare_delta charlie = fp_compare_delta(alpha._value + 3.0008); + + BOOST_CHECK(charlie >= alpha); + BOOST_CHECK(double(alpha._value + 3.0008) >= alpha); +} + +BOOST_AUTO_TEST_CASE(frequency_compare_function) { + + BOOST_CHECK(uhd::math::frequencies_are_equal(6817333232, 6817333232)); + BOOST_CHECK(!uhd::math::frequencies_are_equal(6817333233, 6817333232)); + BOOST_CHECK(uhd::math::frequencies_are_equal(6817333232.1, 6817333232.1)); + BOOST_CHECK(!uhd::math::frequencies_are_equal(6817333232.5, 6817333232.6)); + BOOST_CHECK(uhd::math::frequencies_are_equal(16.8173332321e9, 16.8173332321e9)); + BOOST_CHECK(!uhd::math::frequencies_are_equal(16.8173332322e9, 16.8173332321e9)); + BOOST_CHECK(!uhd::math::frequencies_are_equal(5.0, 4.0)); + BOOST_CHECK(uhd::math::frequencies_are_equal(48750000, 48749999.9946)); +} diff --git a/host/tests/fp_compare_epsilon_test.cpp b/host/tests/fp_compare_epsilon_test.cpp new file mode 100644 index 000000000..5790318c2 --- /dev/null +++ b/host/tests/fp_compare_epsilon_test.cpp @@ -0,0 +1,238 @@ +// +// Copyright 2014 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 + +using namespace uhd::math::fp_compare; + +BOOST_AUTO_TEST_CASE(fp_compare_epsilon_constructors) { + // Test default constructor + fp_compare_epsilon alpha = fp_compare_epsilon(7457392.0); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value); + BOOST_CHECK_EQUAL(alpha._value, beta._value); + BOOST_CHECK_EQUAL(alpha._epsilon, beta._epsilon); + + // Test constructor with specified epsilon + fp_compare_epsilon foxtrot = fp_compare_epsilon(alpha._value, + uhd::math::SINGLE_PRECISION_EPSILON); + fp_compare_epsilon gamma = fp_compare_epsilon(alpha._value, 2.0e-1); + BOOST_CHECK_EQUAL(alpha._epsilon, foxtrot._epsilon); + BOOST_CHECK(not (alpha._epsilon == gamma._epsilon)); + + // Test copy-constructor + fp_compare_epsilon charlie = fp_compare_epsilon(alpha); + BOOST_CHECK_EQUAL(alpha._value, charlie._value); + BOOST_CHECK_EQUAL(alpha._epsilon, charlie._epsilon); + + // Test assignment operator + fp_compare_epsilon delta = beta; + BOOST_CHECK_EQUAL(alpha._value, delta._value); + BOOST_CHECK_EQUAL(alpha._epsilon, delta._epsilon); +} + +BOOST_AUTO_TEST_CASE(double_compare_constructors) { + // Test default constructor + fp_compare_epsilon alpha = fp_compare_epsilon(45739210286.0101); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value); + BOOST_CHECK_EQUAL(alpha._value, beta._value); + BOOST_CHECK_EQUAL(alpha._epsilon, beta._epsilon); + + // Test constructor with specified epsilon + fp_compare_epsilon foxtrot = fp_compare_epsilon(alpha._value, + uhd::math::DOUBLE_PRECISION_EPSILON); + fp_compare_epsilon gamma = fp_compare_epsilon(alpha._value, 2.0e-6); + BOOST_CHECK_EQUAL(alpha._epsilon, foxtrot._epsilon); + BOOST_CHECK(not (alpha._epsilon == gamma._epsilon)); + + // Test copy-constructor + fp_compare_epsilon charlie = fp_compare_epsilon(alpha); + BOOST_CHECK_EQUAL(alpha._value, charlie._value); + BOOST_CHECK_EQUAL(alpha._epsilon, charlie._epsilon); + + // Test assignment operator + fp_compare_epsilon delta = beta; + BOOST_CHECK_EQUAL(alpha._value, delta._value); + BOOST_CHECK_EQUAL(alpha._epsilon, delta._epsilon); +} + +BOOST_AUTO_TEST_CASE(float_equality_operators) { + // Test basic equality operator + fp_compare_epsilon alpha = fp_compare_epsilon(1.0); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value); + BOOST_CHECK(alpha == beta); + BOOST_CHECK(alpha == float(alpha._value)); + + // Test equality edge case at delta = epsilon + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value + + uhd::math::SINGLE_PRECISION_EPSILON); + BOOST_CHECK(not (alpha == charlie)); + BOOST_CHECK(not (alpha == float(alpha._value + uhd::math::SINGLE_PRECISION_EPSILON))); +} + +BOOST_AUTO_TEST_CASE(double_equality_operators) { + // Test basic equality operator + fp_compare_epsilon alpha = fp_compare_epsilon(1.0); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value); + BOOST_CHECK(alpha == beta); + BOOST_CHECK(alpha == double(beta._value)); + + // Test equality edge case at delta = epsilon + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value + + uhd::math::DOUBLE_PRECISION_EPSILON); + BOOST_CHECK(not (alpha == charlie)); + BOOST_CHECK(not (alpha == double(alpha._value + + uhd::math::DOUBLE_PRECISION_EPSILON))); +} + +BOOST_AUTO_TEST_CASE(float_inequality_operators) { + // Test inequality operator, which is based on equality operator + fp_compare_epsilon alpha = fp_compare_epsilon(127.0); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value + 1.19e-5); + + BOOST_CHECK(alpha != beta); + BOOST_CHECK(alpha != float(alpha._value + 1.19e-5)); +} + +BOOST_AUTO_TEST_CASE(double_inequality_operators) { + // Test inequality operator, which is based on equality operator + fp_compare_epsilon alpha = fp_compare_epsilon(1.0); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value + 1.19e-10); + + BOOST_CHECK(alpha != beta); + BOOST_CHECK(alpha != double(alpha._value + 1.19e-10)); +} + +BOOST_AUTO_TEST_CASE(float_lessthan_operators) { + // Test less-than operator + fp_compare_epsilon alpha = fp_compare_epsilon(274192.7); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value - 0.15); + + BOOST_CHECK(beta < alpha); + BOOST_CHECK(float(alpha._value - 0.15) < alpha); + + // Confirm false less-than case + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value - 1.2); + + BOOST_CHECK(not (alpha < charlie)); + BOOST_CHECK(not (alpha < float(alpha._value - 1.2))); +} + +BOOST_AUTO_TEST_CASE(double_lessthan_operators) { + // Test less-than operator + fp_compare_epsilon alpha = fp_compare_epsilon(274192856.762312); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value - 0.0002); + + BOOST_CHECK(beta < alpha); + BOOST_CHECK(double(alpha._value - 0.0002) < alpha); + + // Confirm false less-than case + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value - 1.0012); + + BOOST_CHECK(not (alpha < charlie)); + BOOST_CHECK(not (alpha < double(alpha._value - 1.0012))); +} + +BOOST_AUTO_TEST_CASE(float_lessthanequals_operators) { + // Test that <= correctly reports for equal values + fp_compare_epsilon alpha = fp_compare_epsilon(827.3); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value); + + BOOST_CHECK(alpha <= beta); + BOOST_CHECK(alpha <= float(alpha._value)); + + // Test that <= correctly reports for less-than values + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value - 1.2); + + BOOST_CHECK(charlie <= alpha); + BOOST_CHECK(float(alpha._value - 1.2) <= alpha); +} + +BOOST_AUTO_TEST_CASE(double_lessthanequals_operators) { + // Test that <= correctly reports for equal values + fp_compare_epsilon alpha = fp_compare_epsilon(837652123.383764); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value); + + BOOST_CHECK(alpha <= beta); + BOOST_CHECK(alpha <= double(alpha._value)); + + // Test that <= correctly reports for less-than values + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value - 0.0012); + + BOOST_CHECK(charlie <= alpha); + BOOST_CHECK(double(alpha._value - 0.0012) <= alpha); +} + +BOOST_AUTO_TEST_CASE(float_greaterthan_operators) { + // Test basic greater-than functionality + fp_compare_epsilon alpha = fp_compare_epsilon(98325.4); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value + 0.15); + + BOOST_CHECK(beta > alpha); + BOOST_CHECK(float(alpha._value + 0.15) > alpha); + + // Test false greater-than case + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value + 1.2); + + BOOST_CHECK(not (alpha > charlie)); + BOOST_CHECK(not (alpha > float(alpha._value + 1.2))); +} + +BOOST_AUTO_TEST_CASE(double_greaterthan_operators) { + // Test basic greater-than functionality + fp_compare_epsilon alpha = fp_compare_epsilon(643907213.428475); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value + 0.0002); + + BOOST_CHECK(beta > alpha); + BOOST_CHECK(double(alpha._value + 0.0002) > alpha); + + // Test false greater-than case + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value + 0.0012); + + BOOST_CHECK(not (alpha > charlie)); + BOOST_CHECK(not (alpha > double(alpha._value + 0.0012))); +} + +BOOST_AUTO_TEST_CASE(float_greaterthanequals_operators) { + // Test that >= correctly reports for equal values + fp_compare_epsilon alpha = fp_compare_epsilon(7834.89); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value); + + BOOST_CHECK(alpha >= beta); + BOOST_CHECK(alpha >= float(alpha._value)); + + // Test that >= correctly reports for greater-than values + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value + 4.8); + + BOOST_CHECK(charlie >= alpha); + BOOST_CHECK(float(alpha._value + 4.8) >= alpha); +} + +BOOST_AUTO_TEST_CASE(double_greaterthanequals_operators) { + // Test that >= correctly reports for equal values + fp_compare_epsilon alpha = fp_compare_epsilon(737623834.89843); + fp_compare_epsilon beta = fp_compare_epsilon(alpha._value); + + BOOST_CHECK(alpha >= beta); + BOOST_CHECK(alpha >= double(alpha._value)); + + // Test that >= correctly reports for greater-than values + fp_compare_epsilon charlie = fp_compare_epsilon(alpha._value + 3.0008); + + BOOST_CHECK(charlie >= alpha); + BOOST_CHECK(double(alpha._value + 3.0008) >= alpha); +} -- cgit v1.2.3