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 = \
$(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 \
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
......
/*
* 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 @@
#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);
......
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