aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am1
-rw-r--r--test/test_pcm_channels.cxx15
-rw-r--r--test/test_pcm_dither.cxx37
-rw-r--r--test/test_pcm_pack.cxx31
-rw-r--r--test/test_pcm_util.hxx68
-rw-r--r--test/test_pcm_volume.cxx75
6 files changed, 118 insertions, 109 deletions
diff --git a/Makefile.am b/Makefile.am
index bf2f40666..f9f2efbfe 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 38d030519..8e013d5af 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<int16_t, N * 2>();
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<int32_t, N * 2>();
struct pcm_buffer buffer;
pcm_buffer_init(&buffer);
diff --git a/test/test_pcm_dither.cxx b/test/test_pcm_dither.cxx
index 6d1ed686a..2fb976db5 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 <glib.h>
-/**
- * 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<int32_t, N>(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<int32_t, N>();
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 61acf81ed..e23acad58 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 <glib.h>
-/**
- * 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<int32_t, N>(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<uint8_t, N * 3>();
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 000000000..c039d6560
--- /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 <glib.h>
+
+#include <array>
+
+#include <stddef.h>
+#include <stdint.h>
+
+template<typename T>
+struct GlibRandomInt {
+ T operator()() const {
+ return T(g_random_int());
+ }
+};
+
+struct GlibRandomInt24 : GlibRandomInt<int32_t> {
+ 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<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>::begin;
+ using std::array<T, N>::end;
+ using std::array<T, N>::operator[];
+
+ template<typename G=GlibRandomInt<T>>
+ TestDataBuffer(G g=G()):std::array<T, N>() {
+ for (auto &i : *this)
+ i = g();
+
+ }
+
+ operator typename std::array<T, N>::const_pointer() const {
+ return begin();
+ }
+};
diff --git a/test/test_pcm_volume.cxx b/test/test_pcm_volume.cxx
index 4ec76e79c..1ab590490 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 <glib.h>
+#include <algorithm>
+
#include <string.h>
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, N>();
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, N>();
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<int32_t, N>(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, N>();
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<float, N>(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);