Commit e42734c3 authored by Max Kellermann's avatar Max Kellermann

test/test_pcm: merge source buffer generator

parent eab78ab9
...@@ -1401,6 +1401,7 @@ test_test_byte_reverse_LDADD = \ ...@@ -1401,6 +1401,7 @@ test_test_byte_reverse_LDADD = \
$(GLIB_LIBS) $(GLIB_LIBS)
test_test_pcm_SOURCES = \ test_test_pcm_SOURCES = \
test/test_pcm_util.hxx \
test/test_pcm_dither.cxx \ test/test_pcm_dither.cxx \
test/test_pcm_pack.cxx \ test/test_pcm_pack.cxx \
test/test_pcm_channels.cxx \ test/test_pcm_channels.cxx \
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "test_pcm_all.hxx" #include "test_pcm_all.hxx"
#include "test_pcm_util.hxx"
#include "PcmChannels.hxx" #include "PcmChannels.hxx"
#include "pcm_buffer.h" #include "pcm_buffer.h"
...@@ -27,11 +28,8 @@ ...@@ -27,11 +28,8 @@
void void
test_pcm_channels_16() test_pcm_channels_16()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
int16_t src[N * 2]; const auto src = TestDataBuffer<int16_t, N * 2>();
for (unsigned i = 0; i < G_N_ELEMENTS(src); ++i)
src[i] = g_random_int();
struct pcm_buffer buffer; struct pcm_buffer buffer;
pcm_buffer_init(&buffer); pcm_buffer_init(&buffer);
...@@ -65,11 +63,8 @@ test_pcm_channels_16() ...@@ -65,11 +63,8 @@ test_pcm_channels_16()
void void
test_pcm_channels_32() test_pcm_channels_32()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
int32_t src[N * 2]; const auto src = TestDataBuffer<int32_t, N * 2>();
for (unsigned i = 0; i < G_N_ELEMENTS(src); ++i)
src[i] = g_random_int();
struct pcm_buffer buffer; struct pcm_buffer buffer;
pcm_buffer_init(&buffer); pcm_buffer_init(&buffer);
......
...@@ -18,35 +18,20 @@ ...@@ -18,35 +18,20 @@
*/ */
#include "test_pcm_all.hxx" #include "test_pcm_all.hxx"
#include "test_pcm_util.hxx"
#include "PcmDither.hxx" #include "PcmDither.hxx"
#include <glib.h> #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 void
test_pcm_dither_24() test_pcm_dither_24()
{ {
PcmDither dither; constexpr unsigned N = 256;
const auto src = TestDataBuffer<int32_t, N>(GlibRandomInt24());
enum { N = 256 };
int32_t src[N];
for (unsigned i = 0; i < N; ++i)
src[i] = random24();
int16_t dest[N]; int16_t dest[N];
PcmDither dither;
dither.Dither24To16(dest, src, src + N); dither.Dither24To16(dest, src.begin(), src.end());
for (unsigned i = 0; i < N; ++i) { for (unsigned i = 0; i < N; ++i) {
g_assert_cmpint(dest[i], >=, (src[i] >> 8) - 8); g_assert_cmpint(dest[i], >=, (src[i] >> 8) - 8);
...@@ -57,16 +42,12 @@ test_pcm_dither_24() ...@@ -57,16 +42,12 @@ test_pcm_dither_24()
void void
test_pcm_dither_32() test_pcm_dither_32()
{ {
PcmDither dither; constexpr unsigned N = 256;
const auto src = TestDataBuffer<int32_t, N>();
enum { N = 256 };
int32_t src[N];
for (unsigned i = 0; i < N; ++i)
src[i] = g_random_int();
int16_t dest[N]; int16_t dest[N];
PcmDither dither;
dither.Dither32To16(dest, src, src + N); dither.Dither32To16(dest, src.begin(), src.end());
for (unsigned i = 0; i < N; ++i) { for (unsigned i = 0; i < N; ++i) {
g_assert_cmpint(dest[i], >=, (src[i] >> 16) - 8); g_assert_cmpint(dest[i], >=, (src[i] >> 16) - 8);
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
*/ */
#include "test_pcm_all.hxx" #include "test_pcm_all.hxx"
#include "test_pcm_util.hxx"
extern "C" { extern "C" {
#include "pcm_pack.h" #include "pcm_pack.h"
...@@ -25,29 +26,14 @@ extern "C" { ...@@ -25,29 +26,14 @@ extern "C" {
#include <glib.h> #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 void
test_pcm_pack_24() test_pcm_pack_24()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
int32_t src[N * 3]; const auto src = TestDataBuffer<int32_t, N>(GlibRandomInt24());
for (unsigned i = 0; i < N; ++i)
src[i] = random24();
uint8_t dest[N * 3]; uint8_t dest[N * 3];
pcm_pack_24(dest, src.begin(), src.end());
pcm_pack_24(dest, src, src + N);
for (unsigned i = 0; i < N; ++i) { for (unsigned i = 0; i < N; ++i) {
int32_t d; int32_t d;
...@@ -67,14 +53,11 @@ test_pcm_pack_24() ...@@ -67,14 +53,11 @@ test_pcm_pack_24()
void void
test_pcm_unpack_24() test_pcm_unpack_24()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
uint8_t src[N * 3]; const auto src = TestDataBuffer<uint8_t, N * 3>();
for (unsigned i = 0; i < G_N_ELEMENTS(src); ++i)
src[i] = g_random_int_range(0, 256);
int32_t dest[N]; int32_t dest[N];
pcm_unpack_24(dest, src.begin(), src.end());
pcm_unpack_24(dest, src, src + G_N_ELEMENTS(src));
for (unsigned i = 0; i < N; ++i) { for (unsigned i = 0; i < N; ++i) {
int32_t s; int32_t s;
......
/*
* 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();
}
};
...@@ -19,33 +19,34 @@ ...@@ -19,33 +19,34 @@
#include "test_pcm_all.hxx" #include "test_pcm_all.hxx"
#include "PcmVolume.hxx" #include "PcmVolume.hxx"
#include "test_pcm_util.hxx"
#include <glib.h> #include <glib.h>
#include <algorithm>
#include <string.h> #include <string.h>
void void
test_pcm_volume_8() test_pcm_volume_8()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
static int8_t zero[N]; static int8_t zero[N];
int8_t src[N]; const auto src = TestDataBuffer<int8_t, N>();
for (unsigned i = 0; i < N; ++i)
src[i] = g_random_int();
int8_t dest[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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S8,
0), ==, true); 0), ==, true);
g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S8,
PCM_VOLUME_1), ==, true); PCM_VOLUME_1), ==, true);
g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S8,
PCM_VOLUME_1 / 2), ==, true); PCM_VOLUME_1 / 2), ==, true);
...@@ -58,25 +59,23 @@ test_pcm_volume_8() ...@@ -58,25 +59,23 @@ test_pcm_volume_8()
void void
test_pcm_volume_16() test_pcm_volume_16()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
static int16_t zero[N]; static int16_t zero[N];
int16_t src[N]; const auto src = TestDataBuffer<int16_t, N>();
for (unsigned i = 0; i < N; ++i)
src[i] = g_random_int();
int16_t dest[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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S16,
0), ==, true); 0), ==, true);
g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S16,
PCM_VOLUME_1), ==, true); PCM_VOLUME_1), ==, true);
g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S16,
PCM_VOLUME_1 / 2), ==, true); PCM_VOLUME_1 / 2), ==, true);
...@@ -86,40 +85,26 @@ test_pcm_volume_16() ...@@ -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 void
test_pcm_volume_24() test_pcm_volume_24()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
static int32_t zero[N]; static int32_t zero[N];
int32_t src[N]; const auto src = TestDataBuffer<int32_t, N>(GlibRandomInt24());
for (unsigned i = 0; i < N; ++i)
src[i] = random24();
int32_t dest[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_S24_P32, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S24_P32,
0), ==, true); 0), ==, true);
g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S24_P32,
PCM_VOLUME_1), ==, true); PCM_VOLUME_1), ==, true);
g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S24_P32,
PCM_VOLUME_1 / 2), ==, true); PCM_VOLUME_1 / 2), ==, true);
...@@ -132,25 +117,23 @@ test_pcm_volume_24() ...@@ -132,25 +117,23 @@ test_pcm_volume_24()
void void
test_pcm_volume_32() test_pcm_volume_32()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
static int32_t zero[N]; static int32_t zero[N];
int32_t src[N]; const auto src = TestDataBuffer<int32_t, N>();
for (unsigned i = 0; i < N; ++i)
src[i] = g_random_int();
int32_t dest[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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S32,
0), ==, true); 0), ==, true);
g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S32,
PCM_VOLUME_1), ==, true); PCM_VOLUME_1), ==, true);
g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_S32,
PCM_VOLUME_1 / 2), ==, true); PCM_VOLUME_1 / 2), ==, true);
...@@ -163,25 +146,23 @@ test_pcm_volume_32() ...@@ -163,25 +146,23 @@ test_pcm_volume_32()
void void
test_pcm_volume_float() test_pcm_volume_float()
{ {
enum { N = 256 }; constexpr unsigned N = 256;
static float zero[N]; static float zero[N];
float src[N]; const auto src = TestDataBuffer<float, N>(GlibRandomFloat());
for (unsigned i = 0; i < N; ++i)
src[i] = g_random_double_range(-1.0, 1.0);
float dest[N]; 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_FLOAT,
0), ==, true); 0), ==, true);
g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_FLOAT,
PCM_VOLUME_1), ==, true); PCM_VOLUME_1), ==, true);
g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0); 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, g_assert_cmpint(pcm_volume(dest, sizeof(dest), SAMPLE_FORMAT_FLOAT,
PCM_VOLUME_1 / 2), ==, true); PCM_VOLUME_1 / 2), ==, 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