test_pcm_volume.cxx 4.97 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "test_pcm_all.hxx"
Max Kellermann's avatar
Max Kellermann committed
21
#include "pcm/PcmVolume.hxx"
22
#include "test_pcm_util.hxx"
23 24 25

#include <glib.h>

26 27
#include <algorithm>

28 29 30
#include <string.h>

void
31
test_pcm_volume_8()
32
{
33
	constexpr unsigned N = 256;
34
	static int8_t zero[N];
35
	const auto src = TestDataBuffer<int8_t, N>();
36 37 38

	int8_t dest[N];

39
	std::copy(src.begin(), src.end(), dest);
40
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S8,
41 42 43
				   0), ==, true);
	g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0);

44
	std::copy(src.begin(), src.end(), dest);
45
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S8,
46 47 48
				   PCM_VOLUME_1), ==, true);
	g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0);

49
	std::copy(src.begin(), src.end(), dest);
50
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S8,
51 52 53 54 55 56 57 58 59
				   PCM_VOLUME_1 / 2), ==, true);

	for (unsigned i = 0; i < N; ++i) {
		g_assert_cmpint(dest[i], >=, (src[i] - 1) / 2);
		g_assert_cmpint(dest[i], <=, src[i] / 2 + 1);
	}
}

void
60
test_pcm_volume_16()
61
{
62
	constexpr unsigned N = 256;
63
	static int16_t zero[N];
64
	const auto src = TestDataBuffer<int16_t, N>();
65 66 67

	int16_t dest[N];

68
	std::copy(src.begin(), src.end(), dest);
69
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S16,
70 71 72
				   0), ==, true);
	g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0);

73
	std::copy(src.begin(), src.end(), dest);
74
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S16,
75 76 77
				   PCM_VOLUME_1), ==, true);
	g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0);

78
	std::copy(src.begin(), src.end(), dest);
79
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S16,
80 81 82 83 84 85 86 87 88
				   PCM_VOLUME_1 / 2), ==, true);

	for (unsigned i = 0; i < N; ++i) {
		g_assert_cmpint(dest[i], >=, (src[i] - 1) / 2);
		g_assert_cmpint(dest[i], <=, src[i] / 2 + 1);
	}
}

void
89
test_pcm_volume_24()
90
{
91
	constexpr unsigned N = 256;
92
	static int32_t zero[N];
93
	const auto src = TestDataBuffer<int32_t, N>(GlibRandomInt24());
94 95 96

	int32_t dest[N];

97
	std::copy(src.begin(), src.end(), dest);
98
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S24_P32,
99 100 101
				   0), ==, true);
	g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0);

102
	std::copy(src.begin(), src.end(), dest);
103
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S24_P32,
104 105 106
				   PCM_VOLUME_1), ==, true);
	g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0);

107
	std::copy(src.begin(), src.end(), dest);
108
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S24_P32,
109 110 111 112 113 114 115 116 117
				   PCM_VOLUME_1 / 2), ==, true);

	for (unsigned i = 0; i < N; ++i) {
		g_assert_cmpint(dest[i], >=, (src[i] - 1) / 2);
		g_assert_cmpint(dest[i], <=, src[i] / 2 + 1);
	}
}

void
118
test_pcm_volume_32()
119
{
120
	constexpr unsigned N = 256;
121
	static int32_t zero[N];
122
	const auto src = TestDataBuffer<int32_t, N>();
123 124 125

	int32_t dest[N];

126
	std::copy(src.begin(), src.end(), dest);
127
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S32,
128 129 130
				   0), ==, true);
	g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0);

131
	std::copy(src.begin(), src.end(), dest);
132
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S32,
133 134 135
				   PCM_VOLUME_1), ==, true);
	g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0);

136
	std::copy(src.begin(), src.end(), dest);
137
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::S32,
138 139 140 141 142 143 144 145 146
				   PCM_VOLUME_1 / 2), ==, true);

	for (unsigned i = 0; i < N; ++i) {
		g_assert_cmpint(dest[i], >=, (src[i] - 1) / 2);
		g_assert_cmpint(dest[i], <=, src[i] / 2 + 1);
	}
}

void
147
test_pcm_volume_float()
148
{
149
	constexpr unsigned N = 256;
150
	static float zero[N];
151
	const auto src = TestDataBuffer<float, N>(GlibRandomFloat());
152 153 154

	float dest[N];

155
	std::copy(src.begin(), src.end(), dest);
156
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::FLOAT,
157 158 159
				   0), ==, true);
	g_assert_cmpint(memcmp(dest, zero, sizeof(zero)), ==, 0);

160
	std::copy(src.begin(), src.end(), dest);
161
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::FLOAT,
162 163 164
				   PCM_VOLUME_1), ==, true);
	g_assert_cmpint(memcmp(dest, src, sizeof(src)), ==, 0);

165
	std::copy(src.begin(), src.end(), dest);
166
	g_assert_cmpint(pcm_volume(dest, sizeof(dest), SampleFormat::FLOAT,
167 168 169 170 171
				   PCM_VOLUME_1 / 2), ==, true);

	for (unsigned i = 0; i < N; ++i)
		g_assert_cmpfloat(dest[i], ==, src[i] / 2);
}