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