1 /*
2 	Audio File Library
3 	Copyright (C) 2013 Michael Pruett <michael@68k.org>
4 
5 	This program is free software; you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation; either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	This program is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License along
16 	with this program; if not, write to the Free Software Foundation, Inc.,
17 	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include <audiofile.h>
21 #include <gtest/gtest.h>
22 #include <stdint.h>
23 #include <unistd.h>
24 
25 #include "Lossless.h"
26 #include "TestUtilities.h"
27 
28 template <typename SampleType>
testFLAC(int channelCount,int sampleWidth,int frameCount)29 static void testFLAC(int channelCount, int sampleWidth, int frameCount)
30 {
31 	testLossless<SampleType>("FLAC", AF_FILE_FLAC, AF_COMPRESSION_FLAC,
32 		channelCount, sampleWidth, frameCount);
33 }
34 
TEST(FLAC,FLAC_16)35 TEST(FLAC, FLAC_16)
36 {
37 	for (int channelCount=1; channelCount<=8; channelCount++)
38 		testFLAC<int16_t>(channelCount, 16, 82421);
39 }
40 
TEST(FLAC,FLAC_24)41 TEST(FLAC, FLAC_24)
42 {
43 	for (int channelCount=1; channelCount<=8; channelCount++)
44 		testFLAC<int32_t>(channelCount, 24, 82421);
45 }
46 
testInvalidSampleFormat(int sampleFormat,int sampleWidth)47 static void testInvalidSampleFormat(int sampleFormat, int sampleWidth)
48 {
49 	std::string testFileName;
50 	ASSERT_TRUE(createTemporaryFile("FLAC", &testFileName));
51 
52 	AFfilesetup setup = afNewFileSetup();
53 	afInitFileFormat(setup, AF_FILE_FLAC);
54 	afInitChannels(setup, AF_DEFAULT_TRACK, 1);
55 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, sampleFormat, sampleWidth);
56 	afInitCompression(setup, AF_DEFAULT_TRACK, AF_COMPRESSION_FLAC);
57 	AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup);
58 	ASSERT_FALSE(file);
59 	afFreeFileSetup(setup);
60 
61 	ASSERT_EQ(::unlink(testFileName.c_str()), 0);
62 }
63 
TEST(FLAC,InvalidSampleWidths)64 TEST(FLAC, InvalidSampleWidths)
65 {
66 	IgnoreErrors ignoreErrors;
67 
68 	for (int sampleWidth=1; sampleWidth<=32; sampleWidth++)
69 	{
70 		if (sampleWidth == 16 || sampleWidth == 24)
71 			continue;
72 
73 		testInvalidSampleFormat(AF_SAMPFMT_TWOSCOMP, sampleWidth);
74 	}
75 }
76 
TEST(FLAC,InvalidSampleFormats)77 TEST(FLAC, InvalidSampleFormats)
78 {
79 	IgnoreErrors ignoreErrors;
80 
81 	for (int sampleWidth=1; sampleWidth<=32; sampleWidth++)
82 		testInvalidSampleFormat(AF_SAMPFMT_UNSIGNED, sampleWidth);
83 
84 	testInvalidSampleFormat(AF_SAMPFMT_FLOAT, 32);
85 	testInvalidSampleFormat(AF_SAMPFMT_DOUBLE, 64);
86 }
87 
TEST(FLAC,InvalidChannels)88 TEST(FLAC, InvalidChannels)
89 {
90 	IgnoreErrors ignoreErrors;
91 
92 	std::string testFileName;
93 	ASSERT_TRUE(createTemporaryFile("FLAC", &testFileName));
94 
95 	AFfilesetup setup = afNewFileSetup();
96 	afInitFileFormat(setup, AF_FILE_FLAC);
97 	afInitChannels(setup, AF_DEFAULT_TRACK, 9);
98 	afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
99 	afInitCompression(setup, AF_DEFAULT_TRACK, AF_COMPRESSION_FLAC);
100 	AFfilehandle file = afOpenFile(testFileName.c_str(), "w", setup);
101 	ASSERT_FALSE(file);
102 	afFreeFileSetup(setup);
103 
104 	ASSERT_EQ(::unlink(testFileName.c_str()), 0);
105 }
106 
main(int argc,char ** argv)107 int main(int argc, char **argv)
108 {
109 	::testing::InitGoogleTest(&argc, argv);
110 	return RUN_ALL_TESTS();
111 }
112