1[+ AutoGen5 template c +]
2/*
3** Copyright (C) 1999-2017 Erik de Castro Lopo <erikd@mega-nerd.com>
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
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18*/
19
20#include "sfconfig.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <math.h>
26#include <inttypes.h>
27
28
29#if HAVE_UNISTD_H
30#include <unistd.h>
31#else
32#include "sf_unistd.h"
33#endif
34
35#include <sndfile.h>
36
37#include "utils.h"
38#include "generate.h"
39
40#define	SAMPLE_RATE			11025
41#define	DATA_LENGTH			(1 << 12)
42
43#define	SILLY_WRITE_COUNT	(234)
44
45[+ FOR data_type
46+]static void	pcm_test_[+ (get "type_name") +] (const char *str, int format, int long_file_ok) ;
47[+ ENDFOR data_type
48+]
49static void empty_file_test (const char *filename, int format) ;
50
51typedef union
52{	double d [DATA_LENGTH] ;
53	float f [DATA_LENGTH] ;
54	int i [DATA_LENGTH] ;
55	short s [DATA_LENGTH] ;
56	char c [DATA_LENGTH] ;
57} BUFFER ;
58
59static	BUFFER	orig_data ;
60static	BUFFER	test_data ;
61
62int
63main (int argc, char **argv)
64{	int		do_all = 0 ;
65	int		test_count = 0 ;
66
67	count_open_files () ;
68
69	if (argc != 2)
70	{	printf ("Usage : %s <test>\n", argv [0]) ;
71		printf ("    Where <test> is one of the following:\n") ;
72		printf ("           wav   - test WAV file functions (little endian)\n") ;
73		printf ("           aiff  - test AIFF file functions (big endian)\n") ;
74		printf ("           au    - test AU file functions\n") ;
75		printf ("           avr   - test AVR file functions\n") ;
76		printf ("           caf   - test CAF file functions\n") ;
77		printf ("           raw   - test RAW header-less PCM file functions\n") ;
78		printf ("           paf   - test PAF file functions\n") ;
79		printf ("           svx   - test 8SVX/16SV file functions\n") ;
80		printf ("           nist  - test NIST Sphere file functions\n") ;
81		printf ("           ircam - test IRCAM file functions\n") ;
82		printf ("           voc   - Create Voice file functions\n") ;
83		printf ("           w64   - Sonic Foundry's W64 file functions\n") ;
84		printf ("           flac  - test FLAC file functions\n") ;
85		printf ("           mpc2k - test MPC 2000 file functions\n") ;
86		printf ("           rf64  - test RF64 file functions\n") ;
87		printf ("           all   - perform all tests\n") ;
88		exit (1) ;
89		} ;
90
91	do_all = !strcmp (argv [1], "all") ;
92
93	if (do_all || ! strcmp (argv [1], "wav"))
94	{	pcm_test_char	("char.wav"		, SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
95		pcm_test_short	("short.wav"	, SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
96		pcm_test_24bit	("24bit.wav"	, SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
97		pcm_test_int	("int.wav"		, SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
98
99		pcm_test_char	("char.rifx"	, SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_U8, SF_FALSE) ;
100		pcm_test_short	("short.rifx"	, SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_16, SF_FALSE) ;
101		pcm_test_24bit	("24bit.rifx"	, SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_24, SF_FALSE) ;
102		pcm_test_int	("int.rifx"		, SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_PCM_32, SF_FALSE) ;
103
104		pcm_test_24bit	("24bit.wavex"	, SF_FORMAT_WAVEX | SF_FORMAT_PCM_24, SF_FALSE) ;
105		pcm_test_int	("int.wavex"	, SF_FORMAT_WAVEX | SF_FORMAT_PCM_32, SF_FALSE) ;
106
107		/* Lite remove start */
108		pcm_test_float	("float.wav"	, SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
109		pcm_test_double	("double.wav"	, SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
110
111		pcm_test_float	("float.rifx"	, SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_FLOAT , SF_FALSE) ;
112		pcm_test_double	("double.rifx"	, SF_ENDIAN_BIG | SF_FORMAT_WAV | SF_FORMAT_DOUBLE, SF_FALSE) ;
113
114		pcm_test_float	("float.wavex"	, SF_FORMAT_WAVEX | SF_FORMAT_FLOAT , SF_FALSE) ;
115		pcm_test_double	("double.wavex"	, SF_FORMAT_WAVEX | SF_FORMAT_DOUBLE, SF_FALSE) ;
116		/* Lite remove end */
117
118		empty_file_test ("empty_char.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
119		empty_file_test ("empty_short.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
120		empty_file_test ("empty_float.wav", SF_FORMAT_WAV | SF_FORMAT_FLOAT) ;
121
122		test_count++ ;
123		} ;
124
125	if (do_all || ! strcmp (argv [1], "aiff"))
126	{	pcm_test_char	("char_u8.aiff"	, SF_FORMAT_AIFF | SF_FORMAT_PCM_U8, SF_FALSE) ;
127		pcm_test_char	("char_s8.aiff"	, SF_FORMAT_AIFF | SF_FORMAT_PCM_S8, SF_FALSE) ;
128		pcm_test_short	("short.aiff"	, SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
129		pcm_test_24bit	("24bit.aiff"	, SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
130		pcm_test_int	("int.aiff"		, SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
131
132		pcm_test_short	("short_sowt.aifc"	, SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
133		pcm_test_24bit	("24bit_sowt.aifc"	, SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
134		pcm_test_int	("int_sowt.aifc"	, SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
135
136		pcm_test_short	("short_twos.aifc"	, SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_16, SF_FALSE) ;
137		pcm_test_24bit	("24bit_twos.aifc"	, SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_24, SF_FALSE) ;
138		pcm_test_int	("int_twos.aifc"	, SF_ENDIAN_BIG | SF_FORMAT_AIFF | SF_FORMAT_PCM_32, SF_FALSE) ;
139
140		/* Lite remove start */
141		pcm_test_short	("dwvw16.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_16, SF_TRUE) ;
142		pcm_test_24bit	("dwvw24.aifc", SF_FORMAT_AIFF | SF_FORMAT_DWVW_24, SF_TRUE) ;
143
144		pcm_test_float	("float.aifc"	, SF_FORMAT_AIFF | SF_FORMAT_FLOAT , SF_FALSE) ;
145		pcm_test_double	("double.aifc"	, SF_FORMAT_AIFF | SF_FORMAT_DOUBLE, SF_FALSE) ;
146		/* Lite remove end */
147
148		empty_file_test ("empty_char.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
149		empty_file_test ("empty_short.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
150		empty_file_test ("empty_float.aiff", SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
151
152		test_count++ ;
153		} ;
154
155	if (do_all || ! strcmp (argv [1], "au"))
156	{	pcm_test_char	("char.au"	, SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
157		pcm_test_short	("short.au"	, SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
158		pcm_test_24bit	("24bit.au"	, SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
159		pcm_test_int	("int.au"	, SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
160		/* Lite remove start */
161		pcm_test_float	("float.au"	, SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
162		pcm_test_double	("double.au", SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
163		/* Lite remove end */
164
165		pcm_test_char	("char_le.au"	, SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_S8, SF_FALSE) ;
166		pcm_test_short	("short_le.au"	, SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_16, SF_FALSE) ;
167		pcm_test_24bit	("24bit_le.au"	, SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_24, SF_FALSE) ;
168		pcm_test_int	("int_le.au"	, SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_PCM_32, SF_FALSE) ;
169		/* Lite remove start */
170		pcm_test_float	("float_le.au"	, SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_FLOAT , SF_FALSE) ;
171		pcm_test_double	("double_le.au"	, SF_ENDIAN_LITTLE | SF_FORMAT_AU | SF_FORMAT_DOUBLE, SF_FALSE) ;
172		/* Lite remove end */
173		test_count++ ;
174		} ;
175
176	if (do_all || ! strcmp (argv [1], "caf"))
177	{	pcm_test_char	("char.caf"		, SF_FORMAT_CAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
178		pcm_test_short	("short.caf"	, SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
179		pcm_test_24bit	("24bit.caf"	, SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
180		pcm_test_int	("int.caf"		, SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
181		/* Lite remove start */
182		pcm_test_float	("float.caf"	, SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
183		pcm_test_double	("double.caf"	, SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
184		/* Lite remove end */
185
186		pcm_test_short	("short_le.caf"	, SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_16, SF_FALSE) ;
187		pcm_test_24bit	("24bit_le.caf"	, SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_24, SF_FALSE) ;
188		pcm_test_int	("int_le.caf"	, SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_PCM_32, SF_FALSE) ;
189		/* Lite remove start */
190		pcm_test_float	("float_le.caf"	, SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_FLOAT , SF_FALSE) ;
191		pcm_test_double	("double_le.caf", SF_ENDIAN_LITTLE | SF_FORMAT_CAF | SF_FORMAT_DOUBLE, SF_FALSE) ;
192
193		pcm_test_short	("alac16.caf"	, SF_FORMAT_CAF | SF_FORMAT_ALAC_16, SF_FALSE) ;
194		pcm_test_20bit	("alac20.caf"	, SF_FORMAT_CAF | SF_FORMAT_ALAC_20, SF_FALSE) ;
195		pcm_test_24bit	("alac24.caf"	, SF_FORMAT_CAF | SF_FORMAT_ALAC_24, SF_FALSE) ;
196		pcm_test_int	("alac32.caf"	, SF_FORMAT_CAF | SF_FORMAT_ALAC_32, SF_FALSE) ;
197
198		/* Lite remove end */
199		test_count++ ;
200		} ;
201
202	if (do_all || ! strcmp (argv [1], "raw"))
203	{	pcm_test_char	("char_s8.raw"	, SF_FORMAT_RAW | SF_FORMAT_PCM_S8, SF_FALSE) ;
204		pcm_test_char	("char_u8.raw"	, SF_FORMAT_RAW | SF_FORMAT_PCM_U8, SF_FALSE) ;
205
206		pcm_test_short	("short_le.raw"	, SF_ENDIAN_LITTLE	| SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
207		pcm_test_short	("short_be.raw"	, SF_ENDIAN_BIG		| SF_FORMAT_RAW | SF_FORMAT_PCM_16, SF_FALSE) ;
208		pcm_test_24bit	("24bit_le.raw"	, SF_ENDIAN_LITTLE	| SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
209		pcm_test_24bit	("24bit_be.raw"	, SF_ENDIAN_BIG		| SF_FORMAT_RAW | SF_FORMAT_PCM_24, SF_FALSE) ;
210		pcm_test_int	("int_le.raw"	, SF_ENDIAN_LITTLE	| SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
211		pcm_test_int	("int_be.raw"	, SF_ENDIAN_BIG		| SF_FORMAT_RAW | SF_FORMAT_PCM_32, SF_FALSE) ;
212
213		/* Lite remove start */
214		pcm_test_float	("float_le.raw"	, SF_ENDIAN_LITTLE	| SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
215		pcm_test_float	("float_be.raw"	, SF_ENDIAN_BIG		| SF_FORMAT_RAW | SF_FORMAT_FLOAT , SF_FALSE) ;
216
217		pcm_test_double	("double_le.raw", SF_ENDIAN_LITTLE	| SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
218		pcm_test_double	("double_be.raw", SF_ENDIAN_BIG		| SF_FORMAT_RAW | SF_FORMAT_DOUBLE, SF_FALSE) ;
219		/* Lite remove end */
220		test_count++ ;
221		} ;
222
223	/* Lite remove start */
224	if (do_all || ! strcmp (argv [1], "paf"))
225	{	pcm_test_char	("char_le.paf", SF_ENDIAN_LITTLE	| SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
226		pcm_test_char	("char_be.paf", SF_ENDIAN_BIG		| SF_FORMAT_PAF | SF_FORMAT_PCM_S8, SF_FALSE) ;
227		pcm_test_short	("short_le.paf", SF_ENDIAN_LITTLE	| SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
228		pcm_test_short	("short_be.paf", SF_ENDIAN_BIG		| SF_FORMAT_PAF | SF_FORMAT_PCM_16, SF_FALSE) ;
229		pcm_test_24bit	("24bit_le.paf", SF_ENDIAN_LITTLE	| SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
230		pcm_test_24bit	("24bit_be.paf", SF_ENDIAN_BIG		| SF_FORMAT_PAF | SF_FORMAT_PCM_24, SF_TRUE) ;
231		test_count++ ;
232		} ;
233
234	if (do_all || ! strcmp (argv [1], "svx"))
235	{	pcm_test_char	("char.svx" , SF_FORMAT_SVX | SF_FORMAT_PCM_S8, SF_FALSE) ;
236		pcm_test_short	("short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16, SF_FALSE) ;
237
238		empty_file_test ("empty_char.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_S8) ;
239		empty_file_test ("empty_short.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
240
241		test_count++ ;
242		} ;
243
244	if (do_all || ! strcmp (argv [1], "nist"))
245	{	pcm_test_short	("short_le.nist", SF_ENDIAN_LITTLE	| SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
246		pcm_test_short	("short_be.nist", SF_ENDIAN_BIG		| SF_FORMAT_NIST | SF_FORMAT_PCM_16, SF_FALSE) ;
247		pcm_test_24bit	("24bit_le.nist", SF_ENDIAN_LITTLE	| SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
248		pcm_test_24bit	("24bit_be.nist", SF_ENDIAN_BIG		| SF_FORMAT_NIST | SF_FORMAT_PCM_24, SF_FALSE) ;
249		pcm_test_int	("int_le.nist"	, SF_ENDIAN_LITTLE	| SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
250		pcm_test_int 	("int_be.nist"	, SF_ENDIAN_BIG		| SF_FORMAT_NIST | SF_FORMAT_PCM_32, SF_FALSE) ;
251
252		test_count++ ;
253		} ;
254
255	if (do_all || ! strcmp (argv [1], "ircam"))
256	{	pcm_test_short	("short_be.ircam"	, SF_ENDIAN_BIG	| SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
257		pcm_test_short	("short_le.ircam"	, SF_ENDIAN_LITTLE	| SF_FORMAT_IRCAM | SF_FORMAT_PCM_16, SF_FALSE) ;
258		pcm_test_int	("int_be.ircam"		, SF_ENDIAN_BIG	| SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
259		pcm_test_int 	("int_le.ircam"		, SF_ENDIAN_LITTLE	| SF_FORMAT_IRCAM | SF_FORMAT_PCM_32, SF_FALSE) ;
260		pcm_test_float	("float_be.ircam"	, SF_ENDIAN_BIG	| SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
261		pcm_test_float	("float_le.ircam"	, SF_ENDIAN_LITTLE	| SF_FORMAT_IRCAM | SF_FORMAT_FLOAT , SF_FALSE) ;
262
263		test_count++ ;
264		} ;
265
266	if (do_all || ! strcmp (argv [1], "voc"))
267	{	pcm_test_char 	("char.voc" , SF_FORMAT_VOC | SF_FORMAT_PCM_U8, SF_FALSE) ;
268		pcm_test_short	("short.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16, SF_FALSE) ;
269
270		test_count++ ;
271		} ;
272
273	if (do_all || ! strcmp (argv [1], "mat4"))
274	{	pcm_test_short	("short_be.mat4"	, SF_ENDIAN_BIG	| SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
275		pcm_test_short	("short_le.mat4"	, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT4 | SF_FORMAT_PCM_16, SF_FALSE) ;
276		pcm_test_int	("int_be.mat4"		, SF_ENDIAN_BIG	| SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
277		pcm_test_int 	("int_le.mat4"		, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT4 | SF_FORMAT_PCM_32, SF_FALSE) ;
278		pcm_test_float	("float_be.mat4"	, SF_ENDIAN_BIG	| SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
279		pcm_test_float	("float_le.mat4"	, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT4 | SF_FORMAT_FLOAT , SF_FALSE) ;
280		pcm_test_double	("double_be.mat4"	, SF_ENDIAN_BIG	| SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
281		pcm_test_double	("double_le.mat4"	, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT4 | SF_FORMAT_DOUBLE, SF_FALSE) ;
282
283		empty_file_test ("empty_short.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
284		empty_file_test ("empty_float.mat4", SF_FORMAT_MAT4 | SF_FORMAT_FLOAT) ;
285		test_count++ ;
286		} ;
287
288	if (do_all || ! strcmp (argv [1], "mat5"))
289	{	pcm_test_char 	("char_be.mat5"		, SF_ENDIAN_BIG	| SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
290		pcm_test_char 	("char_le.mat5"		, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8, SF_FALSE) ;
291		pcm_test_short	("short_be.mat5"	, SF_ENDIAN_BIG	| SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
292		pcm_test_short	("short_le.mat5"	, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT5 | SF_FORMAT_PCM_16, SF_FALSE) ;
293		pcm_test_int	("int_be.mat5"		, SF_ENDIAN_BIG	| SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
294		pcm_test_int 	("int_le.mat5"		, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT5 | SF_FORMAT_PCM_32, SF_FALSE) ;
295		pcm_test_float	("float_be.mat5"	, SF_ENDIAN_BIG	| SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
296		pcm_test_float	("float_le.mat5"	, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT5 | SF_FORMAT_FLOAT , SF_FALSE) ;
297		pcm_test_double	("double_be.mat5"	, SF_ENDIAN_BIG	| SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
298		pcm_test_double	("double_le.mat5"	, SF_ENDIAN_LITTLE	| SF_FORMAT_MAT5 | SF_FORMAT_DOUBLE, SF_FALSE) ;
299
300		increment_open_file_count () ;
301
302		empty_file_test ("empty_char.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_U8) ;
303		empty_file_test ("empty_short.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
304		empty_file_test ("empty_float.mat5", SF_FORMAT_MAT5 | SF_FORMAT_FLOAT) ;
305
306		test_count++ ;
307		} ;
308
309	if (do_all || ! strcmp (argv [1], "pvf"))
310	{	pcm_test_char 	("char.pvf"	, SF_FORMAT_PVF | SF_FORMAT_PCM_S8, SF_FALSE) ;
311		pcm_test_short	("short.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16, SF_FALSE) ;
312		pcm_test_int	("int.pvf"	, SF_FORMAT_PVF | SF_FORMAT_PCM_32, SF_FALSE) ;
313		test_count++ ;
314		} ;
315
316	if (do_all || ! strcmp (argv [1], "htk"))
317	{	pcm_test_short	("short.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16, SF_FALSE) ;
318		test_count++ ;
319		} ;
320
321	if (do_all || ! strcmp (argv [1], "mpc2k"))
322	{	pcm_test_short	("short.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16, SF_FALSE) ;
323		test_count++ ;
324		} ;
325
326	if (do_all || ! strcmp (argv [1], "avr"))
327	{	pcm_test_char 	("char_u8.avr"	, SF_FORMAT_AVR | SF_FORMAT_PCM_U8, SF_FALSE) ;
328		pcm_test_char 	("char_s8.avr"	, SF_FORMAT_AVR | SF_FORMAT_PCM_S8, SF_FALSE) ;
329		pcm_test_short	("short.avr"	, SF_FORMAT_AVR | SF_FORMAT_PCM_16, SF_FALSE) ;
330		test_count++ ;
331		} ;
332	/* Lite remove end */
333
334	if (do_all || ! strcmp (argv [1], "w64"))
335	{	pcm_test_char	("char.w64"		, SF_FORMAT_W64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
336		pcm_test_short	("short.w64"	, SF_FORMAT_W64 | SF_FORMAT_PCM_16, SF_FALSE) ;
337		pcm_test_24bit	("24bit.w64"	, SF_FORMAT_W64 | SF_FORMAT_PCM_24, SF_FALSE) ;
338		pcm_test_int	("int.w64"		, SF_FORMAT_W64 | SF_FORMAT_PCM_32, SF_FALSE) ;
339		/* Lite remove start */
340		pcm_test_float	("float.w64"	, SF_FORMAT_W64 | SF_FORMAT_FLOAT , SF_FALSE) ;
341		pcm_test_double	("double.w64"	, SF_FORMAT_W64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
342		/* Lite remove end */
343
344		empty_file_test ("empty_char.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
345		empty_file_test ("empty_short.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
346		empty_file_test ("empty_float.w64", SF_FORMAT_W64 | SF_FORMAT_FLOAT) ;
347
348		test_count++ ;
349		} ;
350
351	if (do_all || ! strcmp (argv [1], "sds"))
352	{	pcm_test_char	("char.sds"		, SF_FORMAT_SDS | SF_FORMAT_PCM_S8, SF_FALSE) ;
353		pcm_test_short	("short.sds"	, SF_FORMAT_SDS | SF_FORMAT_PCM_16, SF_FALSE) ;
354		pcm_test_24bit	("24bit.sds"	, SF_FORMAT_SDS | SF_FORMAT_PCM_24, SF_FALSE) ;
355
356		empty_file_test ("empty_char.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_S8) ;
357		empty_file_test ("empty_short.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
358
359		test_count++ ;
360		} ;
361
362	if (do_all || ! strcmp (argv [1], "sd2"))
363	{	pcm_test_char	("char.sd2"		, SF_FORMAT_SD2 | SF_FORMAT_PCM_S8, SF_TRUE) ;
364		pcm_test_short	("short.sd2"	, SF_FORMAT_SD2 | SF_FORMAT_PCM_16, SF_TRUE) ;
365		pcm_test_24bit	("24bit.sd2"	, SF_FORMAT_SD2 | SF_FORMAT_PCM_24, SF_TRUE) ;
366		pcm_test_int	("32bit.sd2"	, SF_FORMAT_SD2 | SF_FORMAT_PCM_32, SF_TRUE) ;
367		test_count++ ;
368		} ;
369
370	if (do_all || ! strcmp (argv [1], "flac"))
371	{	if (HAVE_EXTERNAL_XIPH_LIBS)
372		{	pcm_test_char	("char.flac"	, SF_FORMAT_FLAC | SF_FORMAT_PCM_S8, SF_TRUE) ;
373			pcm_test_short	("short.flac"	, SF_FORMAT_FLAC | SF_FORMAT_PCM_16, SF_TRUE) ;
374			pcm_test_24bit	("24bit.flac"	, SF_FORMAT_FLAC | SF_FORMAT_PCM_24, SF_TRUE) ;
375			}
376		else
377			puts ("    No FLAC tests because FLAC support was not compiled in.") ;
378		test_count++ ;
379		} ;
380
381	if (do_all || ! strcmp (argv [1], "rf64"))
382	{	pcm_test_char	("char.rf64"	, SF_FORMAT_RF64 | SF_FORMAT_PCM_U8, SF_FALSE) ;
383		pcm_test_short	("short.rf64"	, SF_FORMAT_RF64 | SF_FORMAT_PCM_16, SF_FALSE) ;
384		pcm_test_24bit	("24bit.rf64"	, SF_FORMAT_RF64 | SF_FORMAT_PCM_24, SF_FALSE) ;
385		pcm_test_int	("int.rf64"		, SF_FORMAT_RF64 | SF_FORMAT_PCM_32, SF_FALSE) ;
386
387		/* Lite remove start */
388		pcm_test_float	("float.rf64"	, SF_FORMAT_RF64 | SF_FORMAT_FLOAT , SF_FALSE) ;
389		pcm_test_double	("double.rf64"	, SF_FORMAT_RF64 | SF_FORMAT_DOUBLE, SF_FALSE) ;
390		empty_file_test ("empty_char.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_U8) ;
391		empty_file_test ("empty_short.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
392		empty_file_test ("empty_float.rf64", SF_FORMAT_RF64 | SF_FORMAT_FLOAT) ;
393		/* Lite remove end */
394
395		test_count++ ;
396		} ;
397
398	if (test_count == 0)
399	{	printf ("Mono : ************************************\n") ;
400		printf ("Mono : *  No '%s' test defined.\n", argv [1]) ;
401		printf ("Mono : ************************************\n") ;
402		return 1 ;
403		} ;
404
405	/* Only open file descriptors should be stdin, stdout and stderr. */
406	check_open_file_count_or_die (__LINE__) ;
407
408	return 0 ;
409} /* main */
410
411/*============================================================================================
412**	Helper functions and macros.
413*/
414
415static void	create_short_file (const char *filename) ;
416
417#define	CHAR_ERROR(x, y)		(abs ((x) - (y)) > 255)
418#define	INT_ERROR(x, y)			(((x) - (y)) != 0)
419#define	BIT_20_ERROR(x, y)		(abs ((x) - (y)) > 4095)
420#define	TRIBYTE_ERROR(x, y)		(abs ((x) - (y)) > 255)
421#define	FLOAT_ERROR(x, y)		(fabs ((x) - (y)) > 1e-5)
422
423#define CONVERT_DATA(k, len, new, orig)					\
424			{	for ((k) = 0 ; (k) < (len) ; (k) ++)	\
425					(new) [k] = (orig) [k] ;			\
426				}
427
428[+ FOR data_type
429+]
430/*======================================================================================
431*/
432
433static void mono_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
434static void stereo_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
435static void mono_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd) ;
436static void new_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int allow_fd) ;
437static void multi_seek_test (const char * filename, int format) ;
438static void write_seek_extend_test (const char * filename, int format) ;
439
440static void
441pcm_test_[+ (get "type_name") +] (const char *filename, int format, int long_file_ok)
442{	SF_INFO		sfinfo ;
443	[+ (get "data_type") +]		*orig ;
444	int			k, allow_fd ;
445
446	/* Sd2 files cannot be opened from an existing file descriptor. */
447	allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
448
449	print_test_name ("pcm_test_[+ (get "type_name") +]", filename) ;
450
451	sfinfo.samplerate	= 44100 ;
452	sfinfo.frames		= SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
453	sfinfo.channels		= 1 ;
454	sfinfo.format		= format ;
455
456	test_sf_format_or_die (&sfinfo, __LINE__) ;
457
458	gen_windowed_sine_double (orig_data.d, DATA_LENGTH, [+ (get "max_val") +]) ;
459
460	orig = orig_data.[+ (get "data_field") +] ;
461
462	/* Make this a macro so gdb steps over it in one go. */
463	CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
464
465	/* Some test broken out here. */
466
467	mono_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
468
469	/* Sub format DWVW does not allow seeking. */
470	if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
471			(format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
472	{	unlink (filename) ;
473		printf ("no seek : ok\n") ;
474		return ;
475		} ;
476
477	if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC
478		&& (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_16
479		&& (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_20
480		&& (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_24
481		&& (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_32
482		)
483		mono_rdwr_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
484
485	/* If the format doesn't support stereo we're done. */
486	sfinfo.channels = 2 ;
487	if (sf_format_check (&sfinfo) == 0)
488	{	unlink (filename) ;
489		puts ("no stereo : ok") ;
490		return ;
491		} ;
492
493	stereo_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
494
495	/* New read/write test. Not sure if this is needed yet. */
496
497	if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF
498			&& (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC
499			&& (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC
500			&& (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_16
501			&& (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_20
502			&& (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_24
503			&& (format & SF_FORMAT_SUBMASK) != SF_FORMAT_ALAC_32
504			)
505		new_rdwr_[+ (get "type_name") +]_test (filename, format, allow_fd) ;
506
507	delete_file (format, filename) ;
508
509	puts ("ok") ;
510	return ;
511} /* pcm_test_[+ (get "type_name") +] */
512
513static void
514mono_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
515{	SNDFILE		*file ;
516	SF_INFO		sfinfo ;
517	[+ (get "data_type") +]		*orig, *test ;
518	sf_count_t	count ;
519	int			k, items, total ;
520
521	sfinfo.samplerate	= 44100 ;
522	sfinfo.frames		= SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
523	sfinfo.channels		= 1 ;
524	sfinfo.format		= format ;
525
526	orig = orig_data.[+ (get "data_field") +] ;
527	test = test_data.[+ (get "data_field") +] ;
528
529	items = DATA_LENGTH ;
530
531	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
532
533	if (sfinfo.frames || sfinfo.sections || sfinfo.seekable)
534	{	printf ("\n\nLine %d : Weird SF_INFO fields.\n", __LINE__) ;
535		exit (1) ;
536		} ;
537
538	sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
539
540	test_write_[+ (get "data_type") +]_or_die (file, 0, orig, items, __LINE__) ;
541	sf_write_sync (file) ;
542	test_write_[+ (get "data_type") +]_or_die (file, 0, orig, items, __LINE__) ;
543	sf_write_sync (file) ;
544
545	/* Add non-audio data after the audio. */
546	sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
547
548	sf_close (file) ;
549
550	memset (test, 0, items * sizeof ([+ (get "data_type") +])) ;
551
552	if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
553		memset (&sfinfo, 0, sizeof (sfinfo)) ;
554
555	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
556
557	if (sfinfo.format != format)
558	{	printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
559		exit (1) ;
560		} ;
561
562	if (sfinfo.frames < 2 * items)
563	{	printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, items) ;
564		exit (1) ;
565		} ;
566
567	if (! long_file_ok && sfinfo.frames > 2 * items)
568	{	printf ("\n\nLine %d : Mono : Incorrect number of frames in file (too long). (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, items) ;
569		exit (1) ;
570		} ;
571
572	if (sfinfo.channels != 1)
573	{	printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
574		exit (1) ;
575		} ;
576
577	if (sfinfo.seekable != 1)
578	{	printf ("\n\nLine %d : File should be seekable.\n", __LINE__) ;
579		exit (1) ;
580		} ;
581
582	check_log_buffer_or_die (file, __LINE__) ;
583
584	test_read_[+ (get "data_type") +]_or_die (file, 0, test, items, __LINE__) ;
585	for (k = 0 ; k < items ; k++)
586		if ([+ (get "error_func") +] (orig [k], test [k]))
587		{	printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
588			oct_save_[+ (get "data_type") +] (orig, test, items) ;
589			exit (1) ;
590			} ;
591
592	/* Test multiple short reads. */
593	test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
594
595	total = 0 ;
596	for (k = 1 ; k <= 32 ; k++)
597	{	int ik ;
598
599		test_read_[+ (get "data_type") +]_or_die (file, 0, test + total, k, __LINE__) ;
600		total += k ;
601
602		for (ik = 0 ; ik < total ; ik++)
603			if ([+ (get "error_func") +] (orig [ik], test [ik]))
604			{	printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, ik, orig [ik], test [ik]) ;
605				exit (1) ;
606				} ;
607		} ;
608
609	/* Seek to start of file. */
610	test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
611
612	test_read_[+ (get "data_type") +]_or_die (file, 0, test, 4, __LINE__) ;
613	for (k = 0 ; k < 4 ; k++)
614		if ([+ (get "error_func") +] (orig [k], test [k]))
615		{	printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
616			exit (1) ;
617			} ;
618
619	/* For some codecs we can't go past here. */
620	if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
621			(format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
622	{	sf_close (file) ;
623		unlink (filename) ;
624		printf ("no seek : ") ;
625		return ;
626		} ;
627
628	/* Seek to offset from start of file. */
629	test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
630
631	test_read_[+ (get "data_type") +]_or_die (file, 0, test + 10, 4, __LINE__) ;
632	for (k = 10 ; k < 14 ; k++)
633		if ([+ (get "error_func") +] (orig [k], test [k]))
634		{	printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
635			exit (1) ;
636			} ;
637
638	/* Seek to offset from current position. */
639	test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
640
641	test_read_[+ (get "data_type") +]_or_die (file, 0, test + 20, 4, __LINE__) ;
642	for (k = 20 ; k < 24 ; k++)
643		if ([+ (get "error_func") +] (orig [k], test [k]))
644		{	printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
645			exit (1) ;
646			} ;
647
648	/* Seek to offset from end of file. */
649	test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
650
651	test_read_[+ (get "data_type") +]_or_die (file, 0, test + 10, 4, __LINE__) ;
652	for (k = 10 ; k < 14 ; k++)
653		if ([+ (get "error_func") +] (orig [k], test [k]))
654		{	printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
655			exit (1) ;
656			} ;
657
658	/* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
659	test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
660
661	count = 0 ;
662	while (count < sfinfo.frames)
663		count += sf_read_[+ (get "data_type") +] (file, test, 311) ;
664
665	/* Check that no error has occurred. */
666	if (sf_error (file))
667	{	printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
668		puts (sf_strerror (file)) ;
669		exit (1) ;
670		} ;
671
672	/* Check that we haven't read beyond EOF. */
673	if (count > sfinfo.frames)
674	{	printf ("\n\nLines %d : read past end of file (%" PRId64 " should be %" PRId64 ")\n", __LINE__, count, sfinfo.frames) ;
675		exit (1) ;
676		} ;
677
678	test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
679
680	sf_close (file) ;
681
682	multi_seek_test (filename, format) ;
683	write_seek_extend_test (filename, format) ;
684
685} /* mono_[+ (get "type_name") +]_test */
686
687static void
688stereo_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
689{	SNDFILE		*file ;
690	SF_INFO		sfinfo ;
691	[+ (get "data_type") +]		*orig, *test ;
692	int			k, items, frames ;
693
694	sfinfo.samplerate	= 44100 ;
695	sfinfo.frames		= SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
696	sfinfo.channels		= 2 ;
697	sfinfo.format		= format ;
698
699	gen_windowed_sine_double (orig_data.d, DATA_LENGTH, [+ (get "max_val") +]) ;
700
701	orig = orig_data.[+ (get "data_field") +] ;
702	test = test_data.[+ (get "data_field") +] ;
703
704	/* Make this a macro so gdb steps over it in one go. */
705	CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
706
707	items = DATA_LENGTH ;
708	frames = items / sfinfo.channels ;
709
710	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
711
712	sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
713
714	test_writef_[+ (get "data_type") +]_or_die (file, 0, orig, frames, __LINE__) ;
715
716	sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
717
718	sf_close (file) ;
719
720	memset (test, 0, items * sizeof ([+ (get "data_type") +])) ;
721
722	if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
723		memset (&sfinfo, 0, sizeof (sfinfo)) ;
724
725	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
726
727	if (sfinfo.format != format)
728	{	printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
729				__LINE__, format, sfinfo.format) ;
730		exit (1) ;
731		} ;
732
733	if (sfinfo.frames < frames)
734	{	printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n",
735				__LINE__, sfinfo.frames, frames) ;
736		exit (1) ;
737		} ;
738
739	if (! long_file_ok && sfinfo.frames > frames)
740	{	printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%" PRId64 " should be %d)\n",
741				__LINE__, sfinfo.frames, frames) ;
742		exit (1) ;
743		} ;
744
745	if (sfinfo.channels != 2)
746	{	printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
747		exit (1) ;
748		} ;
749
750	check_log_buffer_or_die (file, __LINE__) ;
751
752	test_readf_[+ (get "data_type") +]_or_die (file, 0, test, frames, __LINE__) ;
753	for (k = 0 ; k < items ; k++)
754		if ([+ (get "error_func") +] (test [k], orig [k]))
755		{	printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
756			exit (1) ;
757			} ;
758
759	/* Seek to start of file. */
760	test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
761
762	test_readf_[+ (get "data_type") +]_or_die (file, 0, test, 2, __LINE__) ;
763	for (k = 0 ; k < 4 ; k++)
764		if ([+ (get "error_func") +] (test [k], orig [k]))
765		{	printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
766			exit (1) ;
767			} ;
768
769	/* Seek to offset from start of file. */
770	test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
771
772	/* Check for errors here. */
773	if (sf_error (file))
774	{	printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
775		puts (sf_strerror (file)) ;
776		exit (1) ;
777		} ;
778
779	if (sf_read_[+ (get "data_type") +] (file, test, 1) > 0)
780	{	printf ("Line %d: Should return 0.\n", __LINE__) ;
781		exit (1) ;
782		} ;
783
784	if (! sf_error (file))
785	{	printf ("Line %d: Should return an error.\n", __LINE__) ;
786		exit (1) ;
787		} ;
788	/*-----------------------*/
789
790	test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 10, 2, __LINE__) ;
791	for (k = 20 ; k < 24 ; k++)
792		if ([+ (get "error_func") +] (test [k], orig [k]))
793		{	printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
794			exit (1) ;
795			} ;
796
797	/* Seek to offset from current position. */
798	test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
799
800	test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ;
801	for (k = 40 ; k < 44 ; k++)
802		if ([+ (get "error_func") +] (test [k], orig [k]))
803		{	printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
804			exit (1) ;
805			} ;
806
807	/* Seek to offset from end of file. */
808	test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
809
810	test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ;
811	for (k = 20 ; k < 24 ; k++)
812		if ([+ (get "error_func") +] (test [k], orig [k]))
813		{	printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
814			exit (1) ;
815			} ;
816
817	sf_close (file) ;
818} /* stereo_[+ (get "type_name") +]_test */
819
820static void
821mono_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
822{	SNDFILE		*file ;
823	SF_INFO		sfinfo ;
824	[+ (get "data_type") +]		*orig, *test ;
825	int			k, pass ;
826
827	switch (format & SF_FORMAT_SUBMASK)
828	{	case SF_FORMAT_ALAC_16 :
829		case SF_FORMAT_ALAC_20 :
830		case SF_FORMAT_ALAC_24 :
831		case SF_FORMAT_ALAC_32 :
832			allow_fd = 0 ;
833			break ;
834
835		default :
836			break ;
837		} ;
838
839	orig = orig_data.[+ (get "data_field") +] ;
840	test = test_data.[+ (get "data_field") +] ;
841
842	sfinfo.samplerate	= SAMPLE_RATE ;
843	sfinfo.frames		= DATA_LENGTH ;
844	sfinfo.channels		= 1 ;
845	sfinfo.format		= format ;
846
847	if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
848		|| (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
849		|| (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
850		unlink (filename) ;
851	else
852	{	/* Create a short file. */
853		create_short_file (filename) ;
854
855		/* Opening a already existing short file (ie invalid header) RDWR is disallowed.
856		** If this returns a valif pointer sf_open() screwed up.
857		*/
858		if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
859		{	printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
860			exit (1) ;
861			} ;
862
863		/* Truncate the file to zero bytes. */
864		if (truncate_file_to_zero (filename) < 0)
865		{	printf ("\n\nLine %d: truncate_file_to_zero (%s) failed", __LINE__, filename) ;
866			perror (NULL) ;
867			exit (1) ;
868			} ;
869		} ;
870
871	/* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
872	** all the usual data required when opening the file in WRITE mode.
873	*/
874	sfinfo.samplerate	= SAMPLE_RATE ;
875	sfinfo.frames		= DATA_LENGTH ;
876	sfinfo.channels		= 1 ;
877	sfinfo.format		= format ;
878
879	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
880
881	/* Do 3 writes followed by reads. After each, check the data and the current
882	** read and write offsets.
883	*/
884	for (pass = 1 ; pass <= 3 ; pass ++)
885	{	orig [20] = pass * 2 ;
886
887		/* Write some data. */
888		test_write_[+ (get "data_type") +]_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
889
890		test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
891
892		/* Read what we just wrote. */
893		test_read_[+ (get "data_type") +]_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
894
895		/* Check the data. */
896		for (k = 0 ; k < DATA_LENGTH ; k++)
897			if ([+ (get "error_func") +] (orig [k], test [k]))
898			{	printf ("\n\nLine %d (pass %d) A : Error at sample %d ([+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, pass, k, orig [k], test [k]) ;
899				oct_save_[+ (get "data_type") +] (orig, test, DATA_LENGTH) ;
900				exit (1) ;
901				} ;
902
903		test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
904		} ; /* for (pass ...) */
905
906	sf_close (file) ;
907
908	/* Open the file again to check the data. */
909	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
910
911	if (sfinfo.format != format)
912	{	printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
913		exit (1) ;
914		} ;
915
916	if (sfinfo.frames < 3 * DATA_LENGTH)
917	{	printf ("\n\nLine %d : Not enough frames in file. (%" PRId64 " < %d)\n", __LINE__, sfinfo.frames, 3 * DATA_LENGTH) ;
918		exit (1) ;
919		}
920
921	if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
922	{	printf ("\n\nLine %d : Incorrect number of frames in file. (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, 3 * DATA_LENGTH) ;
923		exit (1) ;
924		} ;
925
926	if (sfinfo.channels != 1)
927	{	printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
928		exit (1) ;
929		} ;
930
931	if (! long_file_ok)
932		test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
933	else
934		test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
935
936	for (pass = 1 ; pass <= 3 ; pass ++)
937	{	orig [20] = pass * 2 ;
938
939		test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
940
941		/* Read what we just wrote. */
942		test_read_[+ (get "data_type") +]_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
943
944		/* Check the data. */
945		for (k = 0 ; k < DATA_LENGTH ; k++)
946			if ([+ (get "error_func") +] (orig [k], test [k]))
947			{	printf ("\n\nLine %d (pass %d) B : Error at sample %d ([+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, pass, k, orig [k], test [k]) ;
948				oct_save_[+ (get "data_type") +] (orig, test, DATA_LENGTH) ;
949				exit (1) ;
950				} ;
951
952		} ; /* for (pass ...) */
953
954	sf_close (file) ;
955} /* mono_rdwr_[+ (get "data_type") +]_test */
956
957static void
958new_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int allow_fd)
959{	SNDFILE *wfile, *rwfile ;
960	SF_INFO	sfinfo ;
961	[+ (get "data_type") +]		*orig, *test ;
962	int		items, frames ;
963
964	orig = orig_data.[+ (get "data_field") +] ;
965	test = test_data.[+ (get "data_field") +] ;
966
967	sfinfo.samplerate	= 44100 ;
968	sfinfo.frames		= SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
969	sfinfo.channels		= 2 ;
970	sfinfo.format		= format ;
971
972	items = DATA_LENGTH ;
973	frames = items / sfinfo.channels ;
974
975	wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
976	sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
977	test_writef_[+ (get "data_type") +]_or_die (wfile, 1, orig, frames, __LINE__) ;
978	sf_write_sync (wfile) ;
979	test_writef_[+ (get "data_type") +]_or_die (wfile, 2, orig, frames, __LINE__) ;
980	sf_write_sync (wfile) ;
981
982	rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
983	if (sfinfo.frames != 2 * frames)
984	{	printf ("\n\nLine %d : incorrect number of frames in file (%" PRId64 " should be %d)\n\n", __LINE__, sfinfo.frames, 2 * frames) ;
985		exit (1) ;
986		} ;
987
988	test_writef_[+ (get "data_type") +]_or_die (wfile, 3, orig, frames, __LINE__) ;
989
990	test_readf_[+ (get "data_type") +]_or_die (rwfile, 1, test, frames, __LINE__) ;
991	test_readf_[+ (get "data_type") +]_or_die (rwfile, 2, test, frames, __LINE__) ;
992
993	sf_close (wfile) ;
994	sf_close (rwfile) ;
995} /* new_rdwr_[+ (get "type_name") +]_test */
996
997[+ ENDFOR data_type +]
998
999/*----------------------------------------------------------------------------------------
1000*/
1001
1002static void
1003empty_file_test (const char *filename, int format)
1004{	SNDFILE		*file ;
1005	SF_INFO	info ;
1006	int allow_fd ;
1007
1008	/* Sd2 files cannot be opened from an existing file descriptor. */
1009	allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
1010
1011	print_test_name ("empty_file_test", filename) ;
1012
1013	unlink (filename) ;
1014
1015	info.samplerate = 48000 ;
1016	info.channels = 2 ;
1017	info.format = format ;
1018	info.frames = 0 ;
1019
1020	if (sf_format_check (&info) == SF_FALSE)
1021	{	info.channels = 1 ;
1022		if (sf_format_check (&info) == SF_FALSE)
1023		{	puts ("invalid file format") ;
1024			return ;
1025			} ;
1026		} ;
1027
1028	/* Create an empty file. */
1029	file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ;
1030	sf_close (file) ;
1031
1032	/* Open for read and check the length. */
1033	file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
1034
1035	if (info.frames != 0)
1036	{	printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ;
1037			exit (1) ;
1038			} ;
1039
1040	sf_close (file) ;
1041
1042	/* Open for read/write and check the length. */
1043	file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ;
1044
1045	if (info.frames != 0)
1046	{	printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ;
1047		exit (1) ;
1048		} ;
1049
1050	sf_close (file) ;
1051
1052	/* Open for read and check the length. */
1053	file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
1054
1055	if (info.frames != 0)
1056	{	printf ("\n\nError : frame count (%" PRId64 ") should be zero.\n", info.frames) ;
1057		exit (1) ;
1058		} ;
1059
1060	sf_close (file) ;
1061
1062	check_open_file_count_or_die (__LINE__) ;
1063
1064	unlink (filename) ;
1065	puts ("ok") ;
1066
1067	return ;
1068} /* empty_file_test */
1069
1070
1071/*----------------------------------------------------------------------------------------
1072*/
1073
1074static void
1075create_short_file (const char *filename)
1076{	FILE *file ;
1077
1078	if (! (file = fopen (filename, "w")))
1079	{	printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ;
1080		fflush (stdout) ;
1081		perror (NULL) ;
1082		exit (1) ;
1083		} ;
1084
1085	fprintf (file, "This is the file data.\n") ;
1086
1087	fclose (file) ;
1088} /* create_short_file */
1089
1090
1091static void
1092multi_seek_test (const char * filename, int format)
1093{	SNDFILE * file ;
1094	SF_INFO info ;
1095	sf_count_t pos ;
1096	int k ;
1097
1098	/* This test doesn't work on the following. */
1099	switch (format & SF_FORMAT_TYPEMASK)
1100	{	case SF_FORMAT_RAW :
1101			return ;
1102
1103		default :
1104			break ;
1105		} ;
1106
1107	memset (&info, 0, sizeof (info)) ;
1108
1109	generate_file (filename, format, 88200) ;
1110
1111	file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
1112
1113	for (k = 0 ; k < 10 ; k++)
1114	{	pos = info.frames / (k + 2) ;
1115		test_seek_or_die (file, pos, SEEK_SET, pos, info.channels, __LINE__) ;
1116		} ;
1117
1118	sf_close (file) ;
1119} /* multi_seek_test */
1120
1121static void
1122write_seek_extend_test (const char * filename, int format)
1123{	SNDFILE * file ;
1124	SF_INFO info ;
1125	short	*orig, *test ;
1126	unsigned items, k ;
1127
1128	/* This test doesn't work on the following container formats. */
1129	switch (format & SF_FORMAT_TYPEMASK)
1130	{	case SF_FORMAT_FLAC :
1131		case SF_FORMAT_HTK :
1132		case SF_FORMAT_PAF :
1133		case SF_FORMAT_SDS :
1134		case SF_FORMAT_SVX :
1135			return ;
1136
1137		default :
1138			break ;
1139		} ;
1140
1141	/* This test doesn't work on the following codec formats. */
1142	switch (format & SF_FORMAT_SUBMASK)
1143	{	case SF_FORMAT_ALAC_16 :
1144		case SF_FORMAT_ALAC_20 :
1145		case SF_FORMAT_ALAC_24 :
1146		case SF_FORMAT_ALAC_32 :
1147			return ;
1148
1149		default :
1150			break ;
1151		} ;
1152
1153	memset (&info, 0, sizeof (info)) ;
1154
1155	info.samplerate = 48000 ;
1156	info.channels = 1 ;
1157	info.format = format ;
1158
1159	items = 512 ;
1160	exit_if_true (items > ARRAY_LEN (orig_data.s), "Line %d : Bad assumption.\n", __LINE__) ;
1161
1162	orig = orig_data.s ;
1163	test = test_data.s ;
1164
1165	for (k = 0 ; k < ARRAY_LEN (orig_data.s) ; k++)
1166		orig [k] = 0x3fff ;
1167
1168	file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_FALSE, __LINE__) ;
1169	test_write_short_or_die (file, 0, orig, items, __LINE__) ;
1170
1171	/* Extend the file using a seek. */
1172	test_seek_or_die (file, 2 * items, SEEK_SET, 2 * items, info.channels, __LINE__) ;
1173
1174	test_writef_short_or_die (file, 0, orig, items, __LINE__) ;
1175	sf_close (file) ;
1176
1177	file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
1178	test_read_short_or_die (file, 0, test, 3 * items, __LINE__) ;
1179	sf_close (file) ;
1180
1181	if (info.frames < 3 * items)
1182	{	printf ("\n\nLine %d : Incorrect number of frames in file (too short). (%" PRId64 " should be %d)\n", __LINE__, info.frames, 3 * items) ;
1183		exit (1) ;
1184		} ;
1185
1186	/* Can't do these formats due to scaling. */
1187	switch (format & SF_FORMAT_SUBMASK)
1188	{	case SF_FORMAT_PCM_S8 :
1189		case SF_FORMAT_PCM_U8 :
1190			return ;
1191		default :
1192			break ;
1193		} ;
1194
1195	for (k = 0 ; k < items ; k++)
1196	{	exit_if_true (test [k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, k, test [k]) ;
1197		exit_if_true (test [items + k] != 0, "Line %d : test [%d] == %d, should be 0.\n", __LINE__, items + k, test [items + k]) ;
1198		exit_if_true (test [2 * items + k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, 2 * items + k, test [2 * items + k]) ;
1199		} ;
1200
1201	return ;
1202} /* write_seek_extend_test */
1203
1204
1205