From a7a10d03c9bf8b6398408edaeae64ffb35a83bb3 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 1 Feb 2013 15:52:03 +0100 Subject: test/test_pcm: add unit test for pcm_mix() --- Makefile.am | 1 + test/test_pcm_all.hxx | 12 ++++++++ test/test_pcm_main.cxx | 5 +++ test/test_pcm_mix.cxx | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_pcm_util.hxx | 17 ++++++++++ 5 files changed, 119 insertions(+) create mode 100644 test/test_pcm_mix.cxx diff --git a/Makefile.am b/Makefile.am index 840fc8163..de818855e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1407,6 +1407,7 @@ test_test_pcm_SOURCES = \ test/test_pcm_channels.cxx \ test/test_pcm_format.cxx \ test/test_pcm_volume.cxx \ + test/test_pcm_mix.cxx \ test/test_pcm_all.hxx \ test/test_pcm_main.cxx test_test_pcm_LDADD = \ diff --git a/test/test_pcm_all.hxx b/test/test_pcm_all.hxx index 7159b4651..18202454b 100644 --- a/test/test_pcm_all.hxx +++ b/test/test_pcm_all.hxx @@ -65,4 +65,16 @@ test_pcm_format_16_to_32(); void test_pcm_format_float(); +void +test_pcm_mix_8(); + +void +test_pcm_mix_16(); + +void +test_pcm_mix_24(); + +void +test_pcm_mix_32(); + #endif diff --git a/test/test_pcm_main.cxx b/test/test_pcm_main.cxx index 01cc731bf..a221b26af 100644 --- a/test/test_pcm_main.cxx +++ b/test/test_pcm_main.cxx @@ -43,5 +43,10 @@ main(int argc, char **argv) g_test_add_func("/pcm/format/16_to_32", test_pcm_format_16_to_32); g_test_add_func("/pcm/format/float", test_pcm_format_float); + g_test_add_func("/pcm/mix/8", test_pcm_mix_8); + g_test_add_func("/pcm/mix/16", test_pcm_mix_16); + g_test_add_func("/pcm/mix/24", test_pcm_mix_24); + g_test_add_func("/pcm/mix/32", test_pcm_mix_32); + g_test_run(); } diff --git a/test/test_pcm_mix.cxx b/test/test_pcm_mix.cxx new file mode 100644 index 000000000..a6e01d20f --- /dev/null +++ b/test/test_pcm_mix.cxx @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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 2 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "test_pcm_all.hxx" +#include "test_pcm_util.hxx" +#include "PcmMix.hxx" + +#include + +template> +void +TestPcmMix(G g=G()) +{ + constexpr unsigned N = 256; + const auto src1 = TestDataBuffer(g); + const auto src2 = TestDataBuffer(g); + + /* portion1=1.0: result must be equal to src1 */ + auto result = src1; + bool success = pcm_mix(result.begin(), src2.begin(), sizeof(result), + format, 1.0); + g_assert(success); + AssertEqualWithTolerance(result, src1, 1); + + /* portion1=0.0: result must be equal to src2 */ + result = src1; + success = pcm_mix(result.begin(), src2.begin(), sizeof(result), + format, 0.0); + g_assert(success); + AssertEqualWithTolerance(result, src2, 1); + + /* portion1=0.5 */ + result = src1; + success = pcm_mix(result.begin(), src2.begin(), sizeof(result), + format, 0.5); + g_assert(success); + + auto expected = src1; + for (unsigned i = 0; i < N; ++i) + expected[i] = (int64_t(src1[i]) + int64_t(src2[i])) / 2; + + AssertEqualWithTolerance(result, expected, 1); +} + +void +test_pcm_mix_8() +{ + TestPcmMix(); +} + +void +test_pcm_mix_16() +{ + TestPcmMix(); +} + +void +test_pcm_mix_24() +{ + TestPcmMix(GlibRandomInt24()); +} + +void +test_pcm_mix_32() +{ + TestPcmMix(); +} diff --git a/test/test_pcm_util.hxx b/test/test_pcm_util.hxx index c039d6560..84ba074fd 100644 --- a/test/test_pcm_util.hxx +++ b/test/test_pcm_util.hxx @@ -51,6 +51,7 @@ template class TestDataBuffer : std::array { public: using typename std::array::const_pointer; + using std::array::size; using std::array::begin; using std::array::end; using std::array::operator[]; @@ -66,3 +67,19 @@ public: return begin(); } }; + +template +bool +AssertEqualWithTolerance(const T &a, const T &b, unsigned tolerance) +{ + g_assert_cmpint(a.size(), ==, b.size()); + + for (unsigned i = 0; i < a.size(); ++i) { + int64_t x = a[i], y = b[i]; + + g_assert_cmpint(x, >=, y - int64_t(tolerance)); + g_assert_cmpint(x, <=, y + int64_t(tolerance)); + } + + return true; +} -- cgit v1.2.3