1 /*
2 	Copyright (C) 2011, Michael Pruett. All rights reserved.
3 
4 	Redistribution and use in source and binary forms, with or without
5 	modification, are permitted provided that the following conditions
6 	are met:
7 
8 	1. Redistributions of source code must retain the above copyright
9 	notice, this list of conditions and the following disclaimer.
10 
11 	2. Redistributions in binary form must reproduce the above copyright
12 	notice, this list of conditions and the following disclaimer in the
13 	documentation and/or other materials provided with the distribution.
14 
15 	3. The name of the author may not be used to endorse or promote products
16 	derived from this software without specific prior written permission.
17 
18 	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include <audiofile.h>
31 #include <stdio.h>
32 #include <gtest/gtest.h>
33 
34 struct ErrorListener *g_listener;
35 
36 struct ErrorListener
37 {
ErrorListenerErrorListener38 	ErrorListener(long expectedError) :
39 		m_expectedError(expectedError),
40 		m_receivedError(-1),
41 		m_oldErrorFunction(0)
42 	{
43 		g_listener = this;
44 		m_oldErrorFunction = afSetErrorHandler(errorHandlerHelper);
45 	}
~ErrorListenerErrorListener46 	~ErrorListener()
47 	{
48 		g_listener = 0;
49 		EXPECT_EQ(m_expectedError, m_receivedError);
50 		afSetErrorHandler(m_oldErrorFunction);
51 	}
52 
53 	long m_expectedError;
54 	long m_receivedError;
55 	AFerrfunc m_oldErrorFunction;
56 
errorHandlerHelperErrorListener57 	static void errorHandlerHelper(long error, const char *description)
58 	{
59 		g_listener->errorHandler(error, description);
60 	}
errorHandlerErrorListener61 	void errorHandler(long error, const char *description)
62 	{
63 		m_receivedError = error;
64 		EXPECT_EQ(m_expectedError, m_receivedError);
65 	}
66 };
67 
68 #define TEST_ERROR(ExpectedError, Message, TestBody) \
69 	{ \
70 		SCOPED_TRACE(Message); \
71 		ErrorListener el(ExpectedError); \
72 		TestBody; \
73 	}
74 
TEST(Data,Null)75 TEST(Data, Null)
76 {
77 	TEST_ERROR(AF_BAD_FILEHANDLE, "closing null file handle",
78 		afCloseFile(AF_NULL_FILEHANDLE));
79 
80 	TEST_ERROR(AF_BAD_FILEHANDLE, "reading from null file handle",
81 		afReadFrames(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, NULL, 0));
82 
83 	TEST_ERROR(AF_BAD_FILEHANDLE, "writing to null file handle",
84 		afWriteFrames(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, NULL, 0));
85 
86 	TEST_ERROR(AF_BAD_FILEHANDLE, "setting position on null file handle",
87 		afSeekFrame(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, 0));
88 
89 	TEST_ERROR(AF_BAD_FILEHANDLE, "retrieving position on null file handle",
90 		afTellFrame(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
91 }
92 
TEST(Channels,Null)93 TEST(Channels, Null)
94 {
95 	TEST_ERROR(AF_BAD_FILESETUP, "initializing channels of null file setup",
96 		afInitChannels(AF_NULL_FILESETUP, AF_DEFAULT_TRACK, 1));
97 
98 	TEST_ERROR(AF_BAD_FILEHANDLE, "getting channels of null file handle",
99 		afGetChannels(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
100 
101 	TEST_ERROR(AF_BAD_FILEHANDLE, "getting virtual channels of null file handle",
102 		afGetVirtualChannels(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
103 
104 	TEST_ERROR(AF_BAD_FILEHANDLE, "setting virtual channels of null file handle",
105 		afSetVirtualChannels(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, 1));
106 
107 	TEST_ERROR(AF_BAD_FILEHANDLE, "setting channel matrix of null file handle",
108 		afSetChannelMatrix(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, NULL));
109 }
110 
TEST(Rate,Null)111 TEST(Rate, Null)
112 {
113 	TEST_ERROR(AF_BAD_FILESETUP, "initializing rate of null file setup",
114 		afInitRate(AF_NULL_FILESETUP, AF_DEFAULT_TRACK, 44100));
115 
116 	TEST_ERROR(AF_BAD_FILEHANDLE, "getting rate of null file handle",
117 		afGetRate(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
118 }
119 
TEST(Compression,Null)120 TEST(Compression, Null)
121 {
122 	TEST_ERROR(AF_BAD_FILESETUP, "initializing compression of null file setup",
123 		afInitCompression(AF_NULL_FILESETUP, AF_DEFAULT_TRACK,
124 			AF_COMPRESSION_NONE));
125 
126 	TEST_ERROR(AF_BAD_FILEHANDLE, "getting compression of null file handle",
127 		afGetCompression(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
128 }
129 
TEST(SampleFormat,Null)130 TEST(SampleFormat, Null)
131 {
132 	TEST_ERROR(AF_BAD_FILESETUP,
133 		"initializing sample format of null file setup",
134 		afInitSampleFormat(AF_NULL_FILESETUP, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16));
135 
136 	TEST_ERROR(AF_BAD_FILEHANDLE,
137 		"getting sample format of null file handle",
138 		afGetSampleFormat(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, NULL, NULL));
139 
140 	TEST_ERROR(AF_BAD_FILEHANDLE,
141 		"getting virtual sample format of null file handle",
142 		afGetVirtualSampleFormat(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, NULL, NULL));
143 
144 	TEST_ERROR(AF_BAD_FILEHANDLE,
145 		"setting virtual sample format of null file handle",
146 		afSetVirtualSampleFormat(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16));
147 }
148 
TEST(ByteOrder,Null)149 TEST(ByteOrder, Null)
150 {
151 	TEST_ERROR(AF_BAD_FILESETUP,
152 		"initializing byte order of null file setup",
153 		afInitByteOrder(AF_NULL_FILESETUP, AF_DEFAULT_TRACK, AF_BYTEORDER_BIGENDIAN));
154 
155 	TEST_ERROR(AF_BAD_FILEHANDLE,
156 		"getting byte order of null file handle",
157 		afGetByteOrder(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
158 
159 	TEST_ERROR(AF_BAD_FILEHANDLE,
160 		"getting virtual byte order of null file handle",
161 		afGetVirtualByteOrder(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
162 
163 	TEST_ERROR(AF_BAD_FILEHANDLE,
164 		"setting virtual byte order of null file handle",
165 		afSetVirtualByteOrder(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, AF_BYTEORDER_BIGENDIAN));
166 }
167 
TEST(DataOffset,Null)168 TEST(DataOffset, Null)
169 {
170 	TEST_ERROR(AF_BAD_FILESETUP, "initializing data offset of null file setup",
171 		afInitDataOffset(AF_NULL_FILESETUP, AF_DEFAULT_TRACK, 0));
172 
173 	TEST_ERROR(AF_BAD_FILEHANDLE, "getting data offset of null file handle",
174 		afGetDataOffset(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
175 }
176 
TEST(FrameCount,Null)177 TEST(FrameCount, Null)
178 {
179 	TEST_ERROR(AF_BAD_FILESETUP, "initializing frame count of null file setup",
180 		afInitFrameCount(AF_NULL_FILESETUP, AF_DEFAULT_TRACK, 0));
181 
182 	TEST_ERROR(AF_BAD_FILEHANDLE, "getting frame count of null file handle",
183 		afGetFrameCount(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK));
184 }
185 
TEST(AES,Null)186 TEST(AES, Null)
187 {
188 	TEST_ERROR(AF_BAD_FILESETUP,
189 		"initializing AES channel data of null file setup",
190 		afInitAESChannelData(AF_NULL_FILESETUP, AF_DEFAULT_TRACK));
191 
192 	TEST_ERROR(AF_BAD_FILESETUP,
193 		"initializing AES channel data of null file setup",
194 		afInitAESChannelDataTo(AF_NULL_FILESETUP, AF_DEFAULT_TRACK, 1));
195 
196 	TEST_ERROR(AF_BAD_FILEHANDLE,
197 		"getting AES channel data of null file handle",
198 		afGetAESChannelData(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, NULL));
199 
200 	TEST_ERROR(AF_BAD_FILEHANDLE,
201 		"setting AES channel data of null file handle",
202 		afSetAESChannelData(AF_NULL_FILEHANDLE, AF_DEFAULT_TRACK, NULL));
203 }
204 
TEST(Setup,Null)205 TEST(Setup, Null)
206 {
207 	TEST_ERROR(AF_BAD_FILESETUP, "freeing null file setup",
208 		afFreeFileSetup(AF_NULL_FILESETUP));
209 }
210 
TEST(File,Bad)211 TEST(File, Bad)
212 {
213 	TEST_ERROR(AF_BAD_OPEN, "opening nonexistent file for reading",
214 		afOpenFile("sldkjflsdkfjalksdjflaksdjflsakfdj", "r", NULL));
215 
216 	TEST_ERROR(AF_BAD_ACCMODE, "opening file with null access mode",
217 		afOpenFile("", NULL, NULL));
218 
219 	TEST_ERROR(AF_BAD_ACCMODE, "opening file with invalid access mode",
220 		afOpenFile("", "x", NULL));
221 
222 	TEST_ERROR(AF_BAD_FILEFMT, "initializing file format to invalid value",
223 		AFfilesetup setup = afNewFileSetup();
224 		afInitFileFormat(setup, 91094);
225 		afFreeFileSetup(setup));
226 
227 	TEST_ERROR(AF_BAD_SAMPFMT,
228 		"initializing sample format and sample width to invalid values",
229 		AFfilesetup setup = afNewFileSetup();
230 		afInitSampleFormat(setup, AF_DEFAULT_TRACK, 3992, 3932);
231 		afFreeFileSetup(setup));
232 }
233 
TEST(Query,Bad)234 TEST(Query, Bad)
235 {
236 	TEST_ERROR(AF_BAD_QUERY, "querying on bad selectors",
237 		afQueryLong(AF_QUERYTYPE_INST, 9999, 9999, 9999, 9999));
238 
239 	TEST_ERROR(AF_BAD_QUERY, "querying on bad selectors",
240 		afQueryLong(AF_QUERYTYPE_INSTPARAM, 9999, 9999, 9999, 9999));
241 
242 	TEST_ERROR(AF_BAD_QUERY, "querying on bad selectors",
243 		afQueryLong(AF_QUERYTYPE_FILEFMT, 9999, 9999, 9999, 9999));
244 
245 	TEST_ERROR(AF_BAD_QUERY, "querying on bad selectors",
246 		afQueryLong(AF_QUERYTYPE_COMPRESSION, 9999, 9999, 9999, 9999));
247 
248 	TEST_ERROR(AF_BAD_QUERY, "querying on bad selectors",
249 		afQueryLong(AF_QUERYTYPE_MARK, 9999, 9999, 9999, 9999));
250 
251 	TEST_ERROR(AF_BAD_QUERYTYPE, "querying using bad query type",
252 		afQueryLong(9999, 9999, 9999, 9999, 9999));
253 }
254 
main(int argc,char ** argv)255 int main(int argc, char **argv)
256 {
257 	::testing::InitGoogleTest(&argc, argv);
258 	return RUN_ALL_TESTS();
259 }
260