test_pcm_volume.cxx 5.09 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 <algorithm>

26 27 28
#include <string.h>

void
29
PcmVolumeTest::TestVolume8()
30
{
31
	constexpr unsigned N = 256;
32
	static int8_t zero[N];
33
	const auto src = TestDataBuffer<int8_t, N>();
34 35 36

	int8_t dest[N];

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

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

49
	std::copy(src.begin(), src.end(), dest);
50 51 52
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::S8, PCM_VOLUME_1 / 2));
53 54

	for (unsigned i = 0; i < N; ++i) {
55 56
		CPPUNIT_ASSERT(dest[i] >= (src[i] - 1) / 2);
		CPPUNIT_ASSERT(dest[i] <= src[i] / 2 + 1);
57 58 59 60
	}
}

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

	int16_t dest[N];

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

75
	std::copy(src.begin(), src.end(), dest);
76 77 78 79
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::S16, PCM_VOLUME_1));
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src)));
80

81
	std::copy(src.begin(), src.end(), dest);
82 83 84
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::S16, PCM_VOLUME_1 / 2));
85 86

	for (unsigned i = 0; i < N; ++i) {
87 88
		CPPUNIT_ASSERT(dest[i] >= (src[i] - 1) / 2);
		CPPUNIT_ASSERT(dest[i] <= src[i] / 2 + 1);
89 90 91 92
	}
}

void
93
PcmVolumeTest::TestVolume24()
94
{
95
	constexpr unsigned N = 256;
96
	static int32_t zero[N];
97
	const auto src = TestDataBuffer<int32_t, N>(RandomInt24());
98 99 100

	int32_t dest[N];

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

107
	std::copy(src.begin(), src.end(), dest);
108 109 110 111
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::S24_P32, PCM_VOLUME_1));
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src)));
112

113
	std::copy(src.begin(), src.end(), dest);
114 115 116
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::S24_P32, PCM_VOLUME_1 / 2));
117 118

	for (unsigned i = 0; i < N; ++i) {
119 120
		CPPUNIT_ASSERT(dest[i] >= (src[i] - 1) / 2);
		CPPUNIT_ASSERT(dest[i] <= src[i] / 2 + 1);
121 122 123 124
	}
}

void
125
PcmVolumeTest::TestVolume32()
126
{
127
	constexpr unsigned N = 256;
128
	static int32_t zero[N];
129
	const auto src = TestDataBuffer<int32_t, N>();
130 131 132

	int32_t dest[N];

133
	std::copy(src.begin(), src.end(), dest);
134 135 136 137
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::S32, 0));
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, zero, sizeof(zero)));
138

139
	std::copy(src.begin(), src.end(), dest);
140 141 142 143
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::S32, PCM_VOLUME_1));
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src)));
144

145
	std::copy(src.begin(), src.end(), dest);
146 147 148
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::S32, PCM_VOLUME_1 / 2));
149 150

	for (unsigned i = 0; i < N; ++i) {
151 152
		CPPUNIT_ASSERT(dest[i] >= (src[i] - 1) / 2);
		CPPUNIT_ASSERT(dest[i] <= src[i] / 2 + 1);
153 154 155 156
	}
}

void
157
PcmVolumeTest::TestVolumeFloat()
158
{
159
	constexpr unsigned N = 256;
160
	static float zero[N];
161
	const auto src = TestDataBuffer<float, N>(RandomFloat());
162 163 164

	float dest[N];

165
	std::copy(src.begin(), src.end(), dest);
166 167 168 169
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::FLOAT, 0));
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, zero, sizeof(zero)));
170

171
	std::copy(src.begin(), src.end(), dest);
172 173 174 175
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::FLOAT, PCM_VOLUME_1));
	CPPUNIT_ASSERT_EQUAL(0, memcmp(dest, src, sizeof(src)));
176

177
	std::copy(src.begin(), src.end(), dest);
178 179 180 181
	CPPUNIT_ASSERT_EQUAL(true,
			     pcm_volume(dest, sizeof(dest),
					SampleFormat::FLOAT,
					PCM_VOLUME_1 / 2));
182 183

	for (unsigned i = 0; i < N; ++i)
184
		CPPUNIT_ASSERT_DOUBLES_EQUAL(src[i] / 2, dest[i], 1);
185
}