Commit a7a10d03 authored by Max Kellermann's avatar Max Kellermann

test/test_pcm: add unit test for pcm_mix()

parent ef99d6ce
......@@ -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 = \
......
......@@ -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
......@@ -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();
}
/*
* 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>();
}
......@@ -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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment