From e42734c3f3f669039f5e088371ab1ef8c7674bb5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 31 Jan 2013 22:58:27 +0100 Subject: test/test_pcm: merge source buffer generator --- Makefile.am | 1 + test/test_pcm_channels.cxx | 15 ++++------ test/test_pcm_dither.cxx | 37 ++++++----------------- test/test_pcm_pack.cxx | 31 +++++-------------- test/test_pcm_util.hxx | 68 +++++++++++++++++++++++++++++++++++++++++ test/test_pcm_volume.cxx | 75 +++++++++++++++++----------------------------- 6 files changed, 118 insertions(+), 109 deletions(-) create mode 100644 test/test_pcm_util.hxx diff --git a/Makefile.am b/Makefile.am index bf2f4066..f9f2efbf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1401,6 +1401,7 @@ test_test_byte_reverse_LDADD = \ $(GLIB_LIBS) test_test_pcm_SOURCES = \ + test/test_pcm_util.hxx \ test/test_pcm_dither.cxx \ test/test_pcm_pack.cxx \ test/test_pcm_channels.cxx \ diff --git a/test/test_pcm_channels.cxx b/test/test_pcm_channels.cxx index 38d03051..8e013d5a 100644 --- a/test/test_pcm_channels.cxx +++ b/test/test_pcm_channels.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "test_pcm_all.hxx" +#include "test_pcm_util.hxx" #include "PcmChannels.hxx" #include "pcm_buffer.h" @@ -27,11 +28,8 @@ void test_pcm_channels_16() { - enum { N = 256 }; - int16_t src[N * 2]; - - for (unsigned i = 0; i < G_N_ELEMENTS(src); ++i) - src[i] = g_random_int(); + constexpr unsigned N = 256; + const auto src = TestDataBuffer(); struct pcm_buffer buffer; pcm_buffer_init(&buffer); @@ -65,11 +63,8 @@ test_pcm_channels_16() void test_pcm_channels_32() { - enum { N = 256 }; - int32_t src[N * 2]; - - for (unsigned i = 0; i < G_N_ELEMENTS(src); ++i) - src[i] = g_random_int(); + constexpr unsigned N = 256; + const auto src = TestDataBuffer(); struct pcm_buffer buffer; pcm_buffer_init(&buffer); diff --git a/test/test_pcm_dither.cxx b/test/test_pcm_dither.cxx index 6d1ed686..2fb976db 100644 --- a/test/test_pcm_dither.cxx +++ b/test/test_pcm_dither.cxx @@ -18,35 +18,20 @@ */ #include "test_pcm_all.hxx" +#include "test_pcm_util.hxx" #include "PcmDither.hxx" #include -/** - * Generate a random 24 bit PCM sample. - */ -static int32_t -random24() -{ - int32_t x = g_random_int() & 0xffffff; - if (x & 0x800000) - x |= 0xff000000; - return x; -} - void test_pcm_dither_24() { - PcmDither dither; - - enum { N = 256 }; - int32_t src[N]; - for (unsigned i = 0; i < N; ++i) - src[i] = random24(); + constexpr unsigned N = 256; + const auto src = TestDataBuffer(GlibRandomInt24()); int16_t dest[N]; - - dither.Dither24To16(dest, src, src + N); + PcmDither dither; + dither.Dither24To16(dest, src.begin(), src.end()); for (unsigned i = 0; i < N; ++i) { g_assert_cmpint(dest[i], >=, (src[i] >> 8) - 8); @@ -57,16 +42,12 @@ test_pcm_dither_24() void test_pcm_dither_32() { - PcmDither dither; - - enum { N = 256 }; - int32_t src[N]; - for (unsigned i = 0; i < N; ++i) - src[i] = g_random_int(); + constexpr unsigned N = 256; + const auto src = TestDataBuffer(); int16_t dest[N]; - - dither.Dither32To16(dest, src, src + N); + PcmDither dither; + dither.Dither32To16(dest, src.begin(), src.end()); for (unsigned i = 0; i < N; ++i) { g_assert_cmpint(dest[i], >=, (src[i] >> 16) - 8); diff --git a/test/test_pcm_pack.cxx b/test/test_pcm_pack.cxx index 61acf81e..e23acad5 100644 --- a/test/test_pcm_pack.cxx +++ b/test/test_pcm_pack.cxx @@ -18,6 +18,7 @@ */ #include "test_pcm_all.hxx" +#include "test_pcm_util.hxx" extern "C" { #include "pcm_pack.h" @@ -25,29 +26,14 @@ extern "C" { #include -/** - * Generate a random 24 bit PCM sample. - */ -static int32_t -random24() -{ - int32_t x = g_random_int() & 0xffffff; - if (x & 0x800000) - x |= 0xff000000; - return x; -} - void test_pcm_pack_24() { - enum { N = 256 }; - int32_t src[N * 3]; - for (unsigned i = 0; i < N; ++i) - src[i] = random24(); + constexpr unsigned N = 256; + const auto src = TestDataBuffer(GlibRandomInt24()); uint8_t dest[N * 3]; - - pcm_pack_24(dest, src, src + N); + pcm_pack_24(dest, src.begin(), src.end()); for (unsigned i = 0; i < N; ++i) { int32_t d; @@ -67,14 +53,11 @@ test_pcm_pack_24() void test_pcm_unpack_24() { - enum { N = 256 }; - uint8_t src[N * 3]; - for (unsigned i = 0; i < G_N_ELEMENTS(src); ++i) - src[i] = g_random_int_range(0, 256); + constexpr unsigned N = 256; + const auto src = TestDataBuffer(); int32_t dest[N]; - - pcm_unpack_24(dest, src, src + G_N_ELEMENTS(src)); + pcm_unpack_24(dest, src.begin(), src.end()); for (unsigned i = 0; i < N; ++i) { int32_t s; diff --git a/test/test_pcm_util.hxx b/test/test_pcm_util.hxx new file mode 100644 index 00000000..c039d656 --- /dev/null +++ b/test/test_pcm_util.hxx @@ -0,0 +1,68 @@ +/* + * 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 + +#include + +#include +#include + +template +struct GlibRandomInt { + T operator()() const { + return T(g_random_int()); + } +}; + +struct GlibRandomInt24 : GlibRandomInt { + int32_t operator()() const { + auto t = GlibRandomInt::operator()(); + t &= 0xffffff; + if (t & 0x800000) + t |= 0xff000000; + return t; + } +}; + +struct GlibRandomFloat { + float operator()() const { + return g_random_double_range(-1.0, 1.0); + } +}; + +template +class TestDataBuffer : std::array { +public: + using typename std::array::const_pointer; + using std::array::begin; + using std::array::end; + using std::array::operator[]; + + template> + TestDataBuffer(G g=G()):std::array() { + for (auto &i : *this) + i = g(); + + } + + operator typename std::array::const_pointer() const { + return begin(); + } +}; diff --git a/test/test_pcm_volume.cxx b/test/test_pcm_volume.cxx index 4ec76e79..1ab59049 100644 --- a/test/test_pcm_volume.cxx +++ b/test/test_pcm_volume.cxx @@ -19,33 +19,34 @@ #include "test_pcm_all.hxx" #include "PcmVolume.hxx" +#include "test_pcm_util.hxx" #include +#include + #include void test_pcm_volume_8() { - enum { N = 256 }; + constexpr unsigned N = 256; static int8_t zero[N]; - int8_t src[N]; - for (unsigned i = 0; i < N; ++i) - src[i] = g_random_int(); + const auto src = TestDataBuffer(); int8_t dest[N]; - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S8, 0), ==, true); g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S8, PCM_VOLUME_1), ==, true); g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S8, PCM_VOLUME_1 / 2), ==, true); @@ -58,25 +59,23 @@ test_pcm_volume_8() void test_pcm_volume_16() { - enum { N = 256 }; + constexpr unsigned N = 256; static int16_t zero[N]; - int16_t src[N]; - for (unsigned i = 0; i < N; ++i) - src[i] = g_random_int(); + const auto src = TestDataBuffer(); int16_t dest[N]; - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S16, 0), ==, true); g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S16, PCM_VOLUME_1), ==, true); g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S16, PCM_VOLUME_1 / 2), ==, true); @@ -86,40 +85,26 @@ test_pcm_volume_16() } } -/** - * Generate a random 24 bit PCM sample. - */ -static int32_t -random24() -{ - int32_t x = g_random_int() & 0xffffff; - if (x & 0x800000) - x |= 0xff000000; - return x; -} - void test_pcm_volume_24() { - enum { N = 256 }; + constexpr unsigned N = 256; static int32_t zero[N]; - int32_t src[N]; - for (unsigned i = 0; i < N; ++i) - src[i] = random24(); + const auto src = TestDataBuffer(GlibRandomInt24()); int32_t dest[N]; - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S24_P32, 0), ==, true); g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S24_P32, PCM_VOLUME_1), ==, true); g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S24_P32, PCM_VOLUME_1 / 2), ==, true); @@ -132,25 +117,23 @@ test_pcm_volume_24() void test_pcm_volume_32() { - enum { N = 256 }; + constexpr unsigned N = 256; static int32_t zero[N]; - int32_t src[N]; - for (unsigned i = 0; i < N; ++i) - src[i] = g_random_int(); + const auto src = TestDataBuffer(); int32_t dest[N]; - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S32, 0), ==, true); g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S32, PCM_VOLUME_1), ==, true); g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S32, PCM_VOLUME_1 / 2), ==, true); @@ -163,25 +146,23 @@ test_pcm_volume_32() void test_pcm_volume_float() { - enum { N = 256 }; + constexpr unsigned N = 256; static float zero[N]; - float src[N]; - for (unsigned i = 0; i < N; ++i) - src[i] = g_random_double_range(-1.0, 1.0); + const auto src = TestDataBuffer(GlibRandomFloat()); float dest[N]; - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_FLOAT, 0), ==, true); g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_FLOAT, PCM_VOLUME_1), ==, true); g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); - memcpy(dest, src, sizeof(src)); + std::copy(src.begin(), src.end(), dest); g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_FLOAT, PCM_VOLUME_1 / 2), ==, true); -- cgit v1.2.3