aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_pcm_export.cxx
blob: 347e2394aa3dc5ceaa80e957c839a1e4a281d0c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2003-2015 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 "config.h"
#include "test_pcm_all.hxx"
#include "pcm/PcmExport.hxx"
#include "system/ByteOrder.hxx"
#include "util/ConstBuffer.hxx"

#include <string.h>

void
PcmExportTest::TestShift8()
{
	static constexpr int32_t src[] = { 0x0, 0x1, 0x100, 0x10000, 0xffffff };
	static constexpr uint32_t expected[] = { 0x0, 0x100, 0x10000, 0x1000000, 0xffffff00 };

	PcmExport e;
	e.Open(SampleFormat::S24_P32, 2, false, true, false, false);

	auto dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0);
}

void
PcmExportTest::TestPack24()
{
	static constexpr int32_t src[] = { 0x0, 0x1, 0x100, 0x10000, 0xffffff };

	static constexpr uint8_t expected_be[] = {
		0, 0, 0x0,
		0, 0, 0x1,
		0, 0x1, 0x00,
		0x1, 0x00, 0x00,
		0xff, 0xff, 0xff,
	};

	static constexpr uint8_t expected_le[] = {
		0, 0, 0x0,
		0x1, 0, 0,
		0x00, 0x1, 0,
		0, 0x00, 0x01,
		0xff, 0xff, 0xff,
	};

	static constexpr size_t expected_size = sizeof(expected_be);
	static const uint8_t *const expected = IsBigEndian()
		? expected_be : expected_le;

	PcmExport e;
	e.Open(SampleFormat::S24_P32, 2, false, false, true, false);

	auto dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(expected_size, dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0);
}

void
PcmExportTest::TestReverseEndian()
{
	static constexpr uint8_t src[] = {
		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
	};

	static constexpr uint8_t expected2[] = {
		2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11
	};

	static constexpr uint8_t expected4[] = {
		4, 3, 2, 1, 8, 7, 6, 5, 12, 11, 10, 9,
	};

	PcmExport e;
	e.Open(SampleFormat::S8, 2, false, false, false, true);

	auto dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(src), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, src, dest.size) == 0);

	e.Open(SampleFormat::S16, 2, false, false, false, true);
	dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(expected2), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected2, dest.size) == 0);

	e.Open(SampleFormat::S32, 2, false, false, false, true);
	dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(expected4), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected4, dest.size) == 0);
}

void
PcmExportTest::TestDop()
{
	static constexpr uint8_t src[] = {
		0x01, 0x23, 0x45, 0x67,
		0x89, 0xab, 0xcd, 0xef,
	};

	static constexpr uint32_t expected[] = {
		0xff050145,
		0xff052367,
		0xfffa89cd,
		0xfffaabef,
	};

	PcmExport e;
	e.Open(SampleFormat::DSD, 2, true, false, false, false);

	auto dest = e.Export({src, sizeof(src)});
	CPPUNIT_ASSERT_EQUAL(sizeof(expected), dest.size);
	CPPUNIT_ASSERT(memcmp(dest.data, expected, dest.size) == 0);
}