Git init
[external/libsndfile.git] / tests / write_read_test.tpl
1 [+ AutoGen5 template c +]
2 /*
3 ** Copyright (C) 1999-2009 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_TRUE) ;
349                 pcm_test_short  ("short.sds"    , SF_FORMAT_SDS | SF_FORMAT_PCM_16, SF_TRUE) ;
350                 pcm_test_24bit  ("24bit.sds"    , SF_FORMAT_SDS | SF_FORMAT_PCM_24, SF_TRUE) ;
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, *test ;
438         int                     k, items, 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         test = test_data.[+ (get "data_field") +] ;
454
455         /* Make this a macro so gdb steps over it in one go. */
456         CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
457
458         items = DATA_LENGTH ;
459
460         /* Some test broken out here. */
461
462         mono_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
463
464         /* Sub format DWVW does not allow seeking. */
465         if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
466                         (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
467         {       unlink (filename) ;
468                 printf ("no seek : ok\n") ;
469                 return ;
470                 } ;
471
472         if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
473                 mono_rdwr_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
474
475         /* If the format doesn't support stereo we're done. */
476         sfinfo.channels = 2 ;
477         if (sf_format_check (&sfinfo) == 0)
478         {       unlink (filename) ;
479                 puts ("no stereo : ok") ;
480                 return ;
481                 } ;
482
483         stereo_[+ (get "type_name") +]_test (filename, format, long_file_ok, allow_fd) ;
484
485         /* New read/write test. Not sure if this is needed yet. */
486
487         if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF &&
488                         (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC &&
489                         (format & SF_FORMAT_TYPEMASK) != SF_FORMAT_FLAC)
490                 new_rdwr_[+ (get "type_name") +]_test (filename, format, allow_fd) ;
491
492         delete_file (format, filename) ;
493
494         puts ("ok") ;
495         return ;
496 } /* pcm_test_[+ (get "type_name") +] */
497
498 static void
499 mono_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
500 {       SNDFILE         *file ;
501         SF_INFO         sfinfo ;
502         [+ (get "data_type") +]         *orig, *test ;
503         sf_count_t      count ;
504         int                     k, items ;
505
506         sfinfo.samplerate       = 44100 ;
507         sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
508         sfinfo.channels         = 1 ;
509         sfinfo.format           = format ;
510
511         orig = orig_data.[+ (get "data_field") +] ;
512         test = test_data.[+ (get "data_field") +] ;
513
514         items = DATA_LENGTH ;
515
516         file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
517
518         sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
519
520         test_write_[+ (get "data_type") +]_or_die (file, 0, orig, items, __LINE__) ;
521         sf_write_sync (file) ;
522         test_write_[+ (get "data_type") +]_or_die (file, 0, orig, items, __LINE__) ;
523         sf_write_sync (file) ;
524
525         /* Add non-audio data after the audio. */
526         sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
527
528         sf_close (file) ;
529
530         memset (test, 0, items * sizeof ([+ (get "data_type") +])) ;
531
532         if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
533                 memset (&sfinfo, 0, sizeof (sfinfo)) ;
534
535         file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
536
537         if (sfinfo.format != format)
538         {       printf ("\n\nLine %d : Mono : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
539                 exit (1) ;
540                 } ;
541
542         if (sfinfo.frames < 2 * items)
543         {       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) ;
544                 exit (1) ;
545                 } ;
546
547         if (! long_file_ok && sfinfo.frames > 2 * items)
548         {       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) ;
549                 exit (1) ;
550                 } ;
551
552         if (sfinfo.channels != 1)
553         {       printf ("\n\nLine %d : Mono : Incorrect number of channels in file.\n", __LINE__) ;
554                 exit (1) ;
555                 } ;
556
557         check_log_buffer_or_die (file, __LINE__) ;
558
559         test_read_[+ (get "data_type") +]_or_die (file, 0, test, items, __LINE__) ;
560         for (k = 0 ; k < items ; k++)
561                 if ([+ (get "error_func") +] (orig [k], test [k]))
562                 {       printf ("\n\nLine %d: Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
563                         oct_save_[+ (get "data_type") +] (orig, test, items) ;
564                         exit (1) ;
565                         } ;
566
567         /* Seek to start of file. */
568         test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
569
570         test_read_[+ (get "data_type") +]_or_die (file, 0, test, 4, __LINE__) ;
571         for (k = 0 ; k < 4 ; k++)
572                 if ([+ (get "error_func") +] (orig [k], test [k]))
573                 {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
574                         exit (1) ;
575                         } ;
576
577         if ((format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_16 ||
578                         (format & SF_FORMAT_SUBMASK) == SF_FORMAT_DWVW_24)
579         {       sf_close (file) ;
580                 unlink (filename) ;
581                 printf ("no seek : ") ;
582                 return ;
583                 } ;
584
585         /* Seek to offset from start of file. */
586         test_seek_or_die (file, items + 10, SEEK_SET, items + 10, sfinfo.channels, __LINE__) ;
587
588         test_read_[+ (get "data_type") +]_or_die (file, 0, test + 10, 4, __LINE__) ;
589         for (k = 10 ; k < 14 ; k++)
590                 if ([+ (get "error_func") +] (orig [k], test [k]))
591                 {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
592                         exit (1) ;
593                         } ;
594
595         /* Seek to offset from current position. */
596         test_seek_or_die (file, 6, SEEK_CUR, items + 20, sfinfo.channels, __LINE__) ;
597
598         test_read_[+ (get "data_type") +]_or_die (file, 0, test + 20, 4, __LINE__) ;
599         for (k = 20 ; k < 24 ; k++)
600                 if ([+ (get "error_func") +] (orig [k], test [k]))
601                 {       printf ("\n\nLine %d : Mono : Incorrect sample A (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
602                         exit (1) ;
603                         } ;
604
605         /* Seek to offset from end of file. */
606         test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
607
608         test_read_[+ (get "data_type") +]_or_die (file, 0, test + 10, 4, __LINE__) ;
609         for (k = 10 ; k < 14 ; k++)
610                 if ([+ (get "error_func") +] (orig [k], test [k]))
611                 {       printf ("\n\nLine %d : Mono : Incorrect sample D (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, test [k], orig [k]) ;
612                         exit (1) ;
613                         } ;
614
615         /* Check read past end of file followed by sf_seek (sndfile, 0, SEEK_CUR). */
616         test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
617
618         count = 0 ;
619         while (count < sfinfo.frames)
620                 count += sf_read_[+ (get "data_type") +] (file, test, 311) ;
621
622         /* Check that no error has occurred. */
623         if (sf_error (file))
624         {       printf ("\n\nLine %d : Mono : error where there shouldn't have been one.\n", __LINE__) ;
625                 puts (sf_strerror (file)) ;
626                 exit (1) ;
627                 } ;
628
629         /* Check that we haven't read beyond EOF. */
630         if (count > sfinfo.frames)
631         {       printf ("\n\nLines %d : read past end of file (%ld should be %ld)\n", __LINE__, (long) count, (long) sfinfo.frames) ;
632                 exit (1) ;
633                 } ;
634
635         test_seek_or_die (file, 0, SEEK_CUR, sfinfo.frames, sfinfo.channels, __LINE__) ;
636
637         sf_close (file) ;
638
639         multi_seek_test (filename, format) ;
640         write_seek_extend_test (filename, format) ;
641
642 } /* mono_[+ (get "type_name") +]_test */
643
644 static void
645 stereo_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
646 {       SNDFILE         *file ;
647         SF_INFO         sfinfo ;
648         [+ (get "data_type") +]         *orig, *test ;
649         int                     k, items, frames ;
650
651         sfinfo.samplerate       = 44100 ;
652         sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
653         sfinfo.channels         = 2 ;
654         sfinfo.format           = format ;
655
656         gen_windowed_sine_double (orig_data.d, DATA_LENGTH, [+ (get "max_val") +]) ;
657
658         orig = orig_data.[+ (get "data_field") +] ;
659         test = test_data.[+ (get "data_field") +] ;
660
661         /* Make this a macro so gdb steps over it in one go. */
662         CONVERT_DATA (k, DATA_LENGTH, orig, orig_data.d) ;
663
664         items = DATA_LENGTH ;
665         frames = items / sfinfo.channels ;
666
667         file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
668
669         sf_set_string (file, SF_STR_ARTIST, "Your name here") ;
670
671         test_writef_[+ (get "data_type") +]_or_die (file, 0, orig, frames, __LINE__) ;
672
673         sf_set_string (file, SF_STR_COPYRIGHT, "Copyright (c) 2003") ;
674
675         sf_close (file) ;
676
677         memset (test, 0, items * sizeof ([+ (get "data_type") +])) ;
678
679         if ((format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
680                 memset (&sfinfo, 0, sizeof (sfinfo)) ;
681
682         file = test_open_file_or_die (filename, SFM_READ, &sfinfo, allow_fd, __LINE__) ;
683
684         if (sfinfo.format != format)
685         {       printf ("\n\nLine %d : Stereo : Returned format incorrect (0x%08X => 0x%08X).\n",
686                                 __LINE__, format, sfinfo.format) ;
687                 exit (1) ;
688                 } ;
689
690         if (sfinfo.frames < frames)
691         {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too short). (%ld should be %d)\n",
692                                 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
693                 exit (1) ;
694                 } ;
695
696         if (! long_file_ok && sfinfo.frames > frames)
697         {       printf ("\n\nLine %d : Stereo : Incorrect number of frames in file (too long). (%ld should be %d)\n",
698                                 __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), frames) ;
699                 exit (1) ;
700                 } ;
701
702         if (sfinfo.channels != 2)
703         {       printf ("\n\nLine %d : Stereo : Incorrect number of channels in file.\n", __LINE__) ;
704                 exit (1) ;
705                 } ;
706
707         check_log_buffer_or_die (file, __LINE__) ;
708
709         test_readf_[+ (get "data_type") +]_or_die (file, 0, test, frames, __LINE__) ;
710         for (k = 0 ; k < items ; k++)
711                 if ([+ (get "error_func") +] (test [k], orig [k]))
712                 {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
713                         exit (1) ;
714                         } ;
715
716         /* Seek to start of file. */
717         test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
718
719         test_readf_[+ (get "data_type") +]_or_die (file, 0, test, 2, __LINE__) ;
720         for (k = 0 ; k < 4 ; k++)
721                 if ([+ (get "error_func") +] (test [k], orig [k]))
722                 {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
723                         exit (1) ;
724                         } ;
725
726         /* Seek to offset from start of file. */
727         test_seek_or_die (file, 10, SEEK_SET, 10, sfinfo.channels, __LINE__) ;
728
729         /* Check for errors here. */
730         if (sf_error (file))
731         {       printf ("Line %d: Should NOT return an error.\n", __LINE__) ;
732                 puts (sf_strerror (file)) ;
733                 exit (1) ;
734                 } ;
735
736         if (sf_read_[+ (get "data_type") +] (file, test, 1) > 0)
737         {       printf ("Line %d: Should return 0.\n", __LINE__) ;
738                 exit (1) ;
739                 } ;
740
741         if (! sf_error (file))
742         {       printf ("Line %d: Should return an error.\n", __LINE__) ;
743                 exit (1) ;
744                 } ;
745         /*-----------------------*/
746
747         test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 10, 2, __LINE__) ;
748         for (k = 20 ; k < 24 ; k++)
749                 if ([+ (get "error_func") +] (test [k], orig [k]))
750                 {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
751                         exit (1) ;
752                         } ;
753
754         /* Seek to offset from current position. */
755         test_seek_or_die (file, 8, SEEK_CUR, 20, sfinfo.channels, __LINE__) ;
756
757         test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ;
758         for (k = 40 ; k < 44 ; k++)
759                 if ([+ (get "error_func") +] (test [k], orig [k]))
760                 {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
761                         exit (1) ;
762                         } ;
763
764         /* Seek to offset from end of file. */
765         test_seek_or_die (file, -1 * (sfinfo.frames - 10), SEEK_END, 10, sfinfo.channels, __LINE__) ;
766
767         test_readf_[+ (get "data_type") +]_or_die (file, 0, test + 20, 2, __LINE__) ;
768         for (k = 20 ; k < 24 ; k++)
769                 if ([+ (get "error_func") +] (test [k], orig [k]))
770                 {       printf ("\n\nLine %d : Stereo : Incorrect sample (#%d : [+ (get "format_char") +] => [+ (get "format_char") +]).\n", __LINE__, k, orig [k], test [k]) ;
771                         exit (1) ;
772                         } ;
773
774         sf_close (file) ;
775 } /* stereo_[+ (get "type_name") +]_test */
776
777 static void
778 mono_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int long_file_ok, int allow_fd)
779 {       SNDFILE         *file ;
780         SF_INFO         sfinfo ;
781         [+ (get "data_type") +]         *orig, *test ;
782         int                     k, pass ;
783
784         orig = orig_data.[+ (get "data_field") +] ;
785         test = test_data.[+ (get "data_field") +] ;
786
787         sfinfo.samplerate       = SAMPLE_RATE ;
788         sfinfo.frames           = DATA_LENGTH ;
789         sfinfo.channels         = 1 ;
790         sfinfo.format           = format ;
791
792         if ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW
793                 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_AU
794                 || (format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2)
795                 unlink (filename) ;
796         else
797         {       /* Create a short file. */
798                 create_short_file (filename) ;
799
800                 /* Opening a already existing short file (ie invalid header) RDWR is disallowed.
801                 ** If this returns a valif pointer sf_open() screwed up.
802                 */
803                 if ((file = sf_open (filename, SFM_RDWR, &sfinfo)))
804                 {       printf ("\n\nLine %d: sf_open should (SFM_RDWR) have failed but didn't.\n", __LINE__) ;
805                         exit (1) ;
806                         } ;
807
808                 /* Truncate the file to zero bytes. */
809                 if (truncate (filename, 0) < 0)
810                 {       printf ("\n\nLine %d: truncate (%s) failed", __LINE__, filename) ;
811                         perror (NULL) ;
812                         exit (1) ;
813                         } ;
814                 } ;
815
816         /* Opening a zero length file RDWR is allowed, but the SF_INFO struct must contain
817         ** all the usual data required when opening the file in WRITE mode.
818         */
819         sfinfo.samplerate       = SAMPLE_RATE ;
820         sfinfo.frames           = DATA_LENGTH ;
821         sfinfo.channels         = 1 ;
822         sfinfo.format           = format ;
823
824         file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
825
826         /* Do 3 writes followed by reads. After each, check the data and the current
827         ** read and write offsets.
828         */
829         for (pass = 1 ; pass <= 3 ; pass ++)
830         {       orig [20] = pass * 2 ;
831
832                 /* Write some data. */
833                 test_write_[+ (get "data_type") +]_or_die (file, pass, orig, DATA_LENGTH, __LINE__) ;
834
835                 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, pass * DATA_LENGTH) ;
836
837                 /* Read what we just wrote. */
838                 test_read_[+ (get "data_type") +]_or_die (file, 0, test, DATA_LENGTH, __LINE__) ;
839
840                 /* Check the data. */
841                 for (k = 0 ; k < DATA_LENGTH ; k++)
842                         if ([+ (get "error_func") +] (orig [k], test [k]))
843                         {       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]) ;
844                                 oct_save_[+ (get "data_type") +] (orig, test, DATA_LENGTH) ;
845                                 exit (1) ;
846                                 } ;
847
848                 test_read_write_position_or_die (file, __LINE__, pass, pass * DATA_LENGTH, pass * DATA_LENGTH) ;
849                 } ; /* for (pass ...) */
850
851         sf_close (file) ;
852
853         /* Open the file again to check the data. */
854         file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
855
856         if (sfinfo.format != format)
857         {       printf ("\n\nLine %d : Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, format, sfinfo.format) ;
858                 exit (1) ;
859                 } ;
860
861         if (sfinfo.frames < 3 * DATA_LENGTH)
862         {       printf ("\n\nLine %d : Not enough frames in file. (%ld < %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 3 * DATA_LENGTH ) ;
863                 exit (1) ;
864                 }
865
866         if (! long_file_ok && sfinfo.frames != 3 * DATA_LENGTH)
867         {       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 ) ;
868                 exit (1) ;
869                 } ;
870
871         if (sfinfo.channels != 1)
872         {       printf ("\n\nLine %d : Incorrect number of channels in file.\n", __LINE__) ;
873                 exit (1) ;
874                 } ;
875
876         if (! long_file_ok)
877                 test_read_write_position_or_die (file, __LINE__, 0, 0, 3 * DATA_LENGTH) ;
878         else
879                 test_seek_or_die (file, 3 * DATA_LENGTH, SFM_WRITE | SEEK_SET, 3 * DATA_LENGTH, sfinfo.channels, __LINE__) ;
880
881         for (pass = 1 ; pass <= 3 ; pass ++)
882         {       orig [20] = pass * 2 ;
883
884                 test_read_write_position_or_die (file, __LINE__, pass, (pass - 1) * DATA_LENGTH, 3 * DATA_LENGTH) ;
885
886                 /* Read what we just wrote. */
887                 test_read_[+ (get "data_type") +]_or_die (file, pass, test, DATA_LENGTH, __LINE__) ;
888
889                 /* Check the data. */
890                 for (k = 0 ; k < DATA_LENGTH ; k++)
891                         if ([+ (get "error_func") +] (orig [k], test [k]))
892                         {       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]) ;
893                                 oct_save_[+ (get "data_type") +] (orig, test, DATA_LENGTH) ;
894                                 exit (1) ;
895                                 } ;
896
897                 } ; /* for (pass ...) */
898
899         sf_close (file) ;
900 } /* mono_rdwr_[+ (get "data_type") +]_test */
901
902 static void
903 new_rdwr_[+ (get "type_name") +]_test (const char *filename, int format, int allow_fd)
904 {       SNDFILE *wfile, *rwfile ;
905         SF_INFO sfinfo ;
906         [+ (get "data_type") +]         *orig, *test ;
907         int             items, frames ;
908
909         orig = orig_data.[+ (get "data_field") +] ;
910         test = test_data.[+ (get "data_field") +] ;
911
912         sfinfo.samplerate       = 44100 ;
913         sfinfo.frames           = SILLY_WRITE_COUNT ; /* Wrong length. Library should correct this on sf_close. */
914         sfinfo.channels         = 2 ;
915         sfinfo.format           = format ;
916
917         items = DATA_LENGTH ;
918         frames = items / sfinfo.channels ;
919
920         wfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, allow_fd, __LINE__) ;
921         sf_command (wfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
922         test_writef_[+ (get "data_type") +]_or_die (wfile, 1, orig, frames, __LINE__) ;
923         sf_write_sync (wfile) ;
924         test_writef_[+ (get "data_type") +]_or_die (wfile, 2, orig, frames, __LINE__) ;
925         sf_write_sync (wfile) ;
926
927         rwfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, allow_fd, __LINE__) ;
928         if (sfinfo.frames != 2 * frames)
929         {       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) ;
930                 exit (1) ;
931                 } ;
932
933         test_writef_[+ (get "data_type") +]_or_die (wfile, 3, orig, frames, __LINE__) ;
934
935         test_readf_[+ (get "data_type") +]_or_die (rwfile, 1, test, frames, __LINE__) ;
936         test_readf_[+ (get "data_type") +]_or_die (rwfile, 2, test, frames, __LINE__) ;
937
938         sf_close (wfile) ;
939         sf_close (rwfile) ;
940 } /* new_rdwr_[+ (get "type_name") +]_test */
941
942 [+ ENDFOR data_type +]
943
944 /*----------------------------------------------------------------------------------------
945 */
946
947 static void
948 empty_file_test (const char *filename, int format)
949 {       SNDFILE         *file ;
950         SF_INFO info ;
951         int allow_fd ;
952
953         /* Sd2 files cannot be opened from an existing file descriptor. */
954         allow_fd = ((format & SF_FORMAT_TYPEMASK) == SF_FORMAT_SD2) ? SF_FALSE : SF_TRUE ;
955
956         print_test_name ("empty_file_test", filename) ;
957
958         unlink (filename) ;
959
960         info.samplerate = 48000 ;
961         info.channels = 2 ;
962         info.format = format ;
963
964         if (sf_format_check (&info) == SF_FALSE)
965         {       info.channels = 1 ;
966                 if (sf_format_check (&info) == SF_FALSE)
967                 {       puts ("invalid file format") ;
968                         return ;
969                         } ;
970                 } ;
971
972         /* Create an empty file. */
973         file = test_open_file_or_die (filename, SFM_WRITE, &info, allow_fd, __LINE__) ;
974         sf_close (file) ;
975
976         /* Open for read and check the length. */
977         file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
978
979         if (SF_COUNT_TO_LONG (info.frames) != 0)
980         {       printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
981                         exit (1) ;
982                         } ;
983
984         sf_close (file) ;
985
986         /* Open for read/write and check the length. */
987         file = test_open_file_or_die (filename, SFM_RDWR, &info, allow_fd, __LINE__) ;
988
989         if (SF_COUNT_TO_LONG (info.frames) != 0)
990         {       printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
991                 exit (1) ;
992                 } ;
993
994         sf_close (file) ;
995
996         /* Open for read and check the length. */
997         file = test_open_file_or_die (filename, SFM_READ, &info, allow_fd, __LINE__) ;
998
999         if (SF_COUNT_TO_LONG (info.frames) != 0)
1000         {       printf ("\n\nError : frame count (%ld) should be zero.\n", SF_COUNT_TO_LONG (info.frames)) ;
1001                 exit (1) ;
1002                 } ;
1003
1004         sf_close (file) ;
1005
1006         check_open_file_count_or_die (__LINE__) ;
1007
1008         unlink (filename) ;
1009         puts ("ok") ;
1010
1011         return ;
1012 } /* empty_file_test */
1013
1014
1015 /*----------------------------------------------------------------------------------------
1016 */
1017
1018 static void
1019 create_short_file (const char *filename)
1020 {       FILE *file ;
1021
1022         if (! (file = fopen (filename, "w")))
1023         {       printf ("create_short_file : fopen (%s, \"w\") failed.", filename) ;
1024                 fflush (stdout) ;
1025                 perror (NULL) ;
1026                 exit (1) ;
1027                 } ;
1028
1029         fprintf (file, "This is the file data.\n") ;
1030
1031         fclose (file) ;
1032 } /* create_short_file */
1033
1034 #if (defined (WIN32) || defined (__WIN32))
1035
1036 /* Win32 does not have truncate (nor does it have the POSIX function ftruncate).
1037 ** Hack somethng up here to over come this. This function can only truncate to a
1038 ** length of zero.
1039 */
1040
1041 static int
1042 truncate (const char *filename, int ignored)
1043 {       int fd ;
1044
1045         ignored = 0 ;
1046
1047         if ((fd = open (filename, O_RDWR | O_TRUNC | O_BINARY)) < 0)
1048                 return 0 ;
1049
1050         close (fd) ;
1051
1052         return 0 ;
1053 } /* truncate */
1054
1055 #endif
1056
1057 static void
1058 multi_seek_test (const char * filename, int format)
1059 {       SNDFILE * file ;
1060         SF_INFO info ;
1061         sf_count_t pos ;
1062         int k ;
1063
1064         /* This test doesn't work on the following. */
1065         switch (format & SF_FORMAT_TYPEMASK)
1066         {       case SF_FORMAT_RAW :
1067                         return ;
1068
1069                 default :
1070                         break ;
1071                 } ;
1072
1073         memset (&info, 0, sizeof (info)) ;
1074
1075         generate_file (filename, format, 88200) ;
1076
1077         file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
1078
1079         for (k = 0 ; k < 10 ; k++)
1080         {       pos = info.frames / (k + 2) ;
1081                 test_seek_or_die (file, pos, SEEK_SET, pos, info.channels, __LINE__) ;
1082                 } ;
1083
1084         sf_close (file) ;
1085 } /* multi_seek_test */
1086
1087 static void
1088 write_seek_extend_test (const char * filename, int format)
1089 {       SNDFILE * file ;
1090         SF_INFO info ;
1091         short   *orig, *test ;
1092         unsigned items, k ;
1093
1094         /* This test doesn't work on the following. */
1095         switch (format & SF_FORMAT_TYPEMASK)
1096         {       case SF_FORMAT_FLAC :
1097                 case SF_FORMAT_HTK :
1098                 case SF_FORMAT_PAF :
1099                 case SF_FORMAT_SDS :
1100                 case SF_FORMAT_SVX :
1101                         return ;
1102
1103                 default :
1104                         break ;
1105                 } ;
1106
1107         memset (&info, 0, sizeof (info)) ;
1108
1109         info.samplerate = 48000 ;
1110         info.channels = 1 ;
1111         info.format = format ;
1112
1113         items = 512 ;
1114         exit_if_true (items > ARRAY_LEN (orig_data.s), "Line %d : Bad assumption.\n", __LINE__) ;
1115
1116         orig = orig_data.s ;
1117         test = test_data.s ;
1118
1119         for (k = 0 ; k < ARRAY_LEN (orig_data.s) ; k++)
1120                 orig [k] = 0x3fff ;
1121
1122         file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_FALSE, __LINE__) ;
1123         test_write_short_or_die (file, 0, orig, items, __LINE__) ;
1124
1125         /* Extend the file using a seek. */
1126         test_seek_or_die (file, 2 * items, SEEK_SET, 2 * items, info.channels, __LINE__) ;
1127
1128         test_writef_short_or_die (file, 0, orig, items, __LINE__) ;
1129         sf_close (file) ;
1130
1131         file = test_open_file_or_die (filename, SFM_READ, &info, SF_FALSE, __LINE__) ;
1132         test_read_short_or_die (file, 0, test, 3 * items, __LINE__) ;
1133         sf_close (file) ;
1134
1135         /* Can't do these formats due to scaling. */
1136         switch (format & SF_FORMAT_SUBMASK)
1137         {       case SF_FORMAT_PCM_S8 :
1138                 case SF_FORMAT_PCM_U8 :
1139                         return ;
1140                 default :
1141                         break ;
1142                 } ;
1143
1144         for (k = 0 ; k < items ; k++)
1145         {       exit_if_true (test [k] != 0x3fff, "Line %d : test [%d] == %d, should be 0x3fff.\n", __LINE__, k, test [k]) ;
1146                 exit_if_true (test [items + k] != 0, "Line %d : test [%d] == %d, should be 0.\n", __LINE__, items + k, test [items + k]) ;
1147                 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]) ;
1148                 } ;
1149
1150         return ;
1151 } /* write_seek_extend_test */
1152
1153