diff options
author | Max Kellermann <max@duempel.org> | 2013-02-01 15:52:03 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-02-01 15:52:03 +0100 |
commit | a7a10d03c9bf8b6398408edaeae64ffb35a83bb3 (patch) | |
tree | 11337371be33f82be3a523e3890fbfa4515a83a1 | |
parent | ef99d6ce3d9b82cb249ae08b390049a6f9cd9d56 (diff) | |
download | mpd-a7a10d03c9bf8b6398408edaeae64ffb35a83bb3.tar.gz mpd-a7a10d03c9bf8b6398408edaeae64ffb35a83bb3.tar.xz mpd-a7a10d03c9bf8b6398408edaeae64ffb35a83bb3.zip |
test/test_pcm: add unit test for pcm_mix()
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | test/test_pcm_all.hxx | 12 | ||||
-rw-r--r-- | test/test_pcm_main.cxx | 5 | ||||
-rw-r--r-- | test/test_pcm_mix.cxx | 84 | ||||
-rw-r--r-- | test/test_pcm_util.hxx | 17 |
5 files changed, 119 insertions, 0 deletions
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 <glib.h> + +template<typename T, sample_format format, typename G=GlibRandomInt<T>> +void +TestPcmMix(G g=G()) +{ + constexpr unsigned N = 256; + const auto src1 = TestDataBuffer<T, N>(g); + const auto src2 = TestDataBuffer<T, N>(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<int8_t, SAMPLE_FORMAT_S8>(); +} + +void +test_pcm_mix_16() +{ + TestPcmMix<int16_t, SAMPLE_FORMAT_S16>(); +} + +void +test_pcm_mix_24() +{ + TestPcmMix<int32_t, SAMPLE_FORMAT_S24_P32>(GlibRandomInt24()); +} + +void +test_pcm_mix_32() +{ + TestPcmMix<int32_t, SAMPLE_FORMAT_S32>(); +} 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<typename T, size_t N> class TestDataBuffer : std::array<T, N> { public: using typename std::array<T, N>::const_pointer; + using std::array<T, N>::size; using std::array<T, N>::begin; using std::array<T, N>::end; using std::array<T, N>::operator[]; @@ -66,3 +67,19 @@ public: return begin(); } }; + +template<typename T> +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; +} |