LICENSE, MINES
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / tests / misc_test.c
1 /*
2 ** Copyright (C) 2001-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software ; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation ; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY ; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program ; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <sys/stat.h>
27 #include <math.h>
28
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #if (HAVE_DECL_S_IRGRP == 0)
34 #include <sf_unistd.h>
35 #endif
36
37 #if (defined (WIN32) || defined (_WIN32))
38 #include <io.h>
39 #include <direct.h>
40 #endif
41
42 #include <sndfile.h>
43
44 #include "utils.h"
45
46 #define BUFFER_LEN (1<<10)
47 #define LOG_BUFFER_SIZE 1024
48
49 static void zero_data_test (const char *filename, int format) ;
50 static void filesystem_full_test (int format) ;
51 static void permission_test (const char *filename, int typemajor) ;
52 static void wavex_amb_test (const char *filename) ;
53
54 int
55 main (int argc, char *argv [])
56 { int do_all = 0 ;
57 int test_count = 0 ;
58
59 if (argc != 2)
60 { printf ("Usage : %s <test>\n", argv [0]) ;
61 printf (" Where <test> is one of the following:\n") ;
62 printf (" wav - test WAV file peak chunk\n") ;
63 printf (" aiff - test AIFF file PEAK chunk\n") ;
64 printf (" all - perform all tests\n") ;
65 exit (1) ;
66 } ;
67
68 do_all=!strcmp (argv [1], "all") ;
69
70 if (do_all || ! strcmp (argv [1], "wav"))
71 { zero_data_test ("zerolen.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
72 filesystem_full_test (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
73 permission_test ("readonly.wav", SF_FORMAT_WAV) ;
74 wavex_amb_test ("ambisonic.wav") ;
75 test_count++ ;
76 } ;
77
78 if (do_all || ! strcmp (argv [1], "aiff"))
79 { zero_data_test ("zerolen.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
80 filesystem_full_test (SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
81 permission_test ("readonly.aiff", SF_FORMAT_AIFF) ;
82 test_count++ ;
83 } ;
84
85 if (do_all || ! strcmp (argv [1], "au"))
86 { zero_data_test ("zerolen.au", SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
87 filesystem_full_test (SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
88 permission_test ("readonly.au", SF_FORMAT_AU) ;
89 test_count++ ;
90 } ;
91
92 if (do_all || ! strcmp (argv [1], "caf"))
93 { zero_data_test ("zerolen.caf", SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
94 filesystem_full_test (SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
95 permission_test ("readonly.caf", SF_FORMAT_CAF) ;
96 test_count++ ;
97 } ;
98
99 if (do_all || ! strcmp (argv [1], "svx"))
100 { zero_data_test ("zerolen.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
101 filesystem_full_test (SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
102 permission_test ("readonly.svx", SF_FORMAT_SVX) ;
103 test_count++ ;
104 } ;
105
106 if (do_all || ! strcmp (argv [1], "nist"))
107 { zero_data_test ("zerolen.nist", SF_FORMAT_NIST | SF_FORMAT_PCM_16) ;
108 filesystem_full_test (SF_FORMAT_NIST | SF_FORMAT_PCM_16) ;
109 permission_test ("readonly.nist", SF_FORMAT_NIST) ;
110 test_count++ ;
111 } ;
112
113 if (do_all || ! strcmp (argv [1], "paf"))
114 { zero_data_test ("zerolen.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_16) ;
115 filesystem_full_test (SF_FORMAT_PAF | SF_FORMAT_PCM_16) ;
116 permission_test ("readonly.paf", SF_FORMAT_PAF) ;
117 test_count++ ;
118 } ;
119
120 if (do_all || ! strcmp (argv [1], "ircam"))
121 { zero_data_test ("zerolen.ircam", SF_FORMAT_IRCAM | SF_FORMAT_PCM_16) ;
122 filesystem_full_test (SF_FORMAT_IRCAM | SF_FORMAT_PCM_16) ;
123 permission_test ("readonly.ircam", SF_FORMAT_IRCAM) ;
124 test_count++ ;
125 } ;
126
127 if (do_all || ! strcmp (argv [1], "voc"))
128 { zero_data_test ("zerolen.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16) ;
129 filesystem_full_test (SF_FORMAT_VOC | SF_FORMAT_PCM_16) ;
130 permission_test ("readonly.voc", SF_FORMAT_VOC) ;
131 test_count++ ;
132 } ;
133
134 if (do_all || ! strcmp (argv [1], "w64"))
135 { zero_data_test ("zerolen.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
136 filesystem_full_test (SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
137 permission_test ("readonly.w64", SF_FORMAT_W64) ;
138 test_count++ ;
139 } ;
140
141 if (do_all || ! strcmp (argv [1], "rf64"))
142 { zero_data_test ("zerolen.rf64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
143 filesystem_full_test (SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
144 permission_test ("readonly.rf64", SF_FORMAT_W64) ;
145 test_count++ ;
146 } ;
147
148 if (do_all || ! strcmp (argv [1], "mat4"))
149 { zero_data_test ("zerolen.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
150 filesystem_full_test (SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
151 permission_test ("readonly.mat4", SF_FORMAT_MAT4) ;
152 test_count++ ;
153 } ;
154
155 if (do_all || ! strcmp (argv [1], "mat5"))
156 { zero_data_test ("zerolen.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
157 filesystem_full_test (SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
158 permission_test ("readonly.mat5", SF_FORMAT_MAT5) ;
159 test_count++ ;
160 } ;
161
162 if (do_all || ! strcmp (argv [1], "pvf"))
163 { zero_data_test ("zerolen.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16) ;
164 filesystem_full_test (SF_FORMAT_PVF | SF_FORMAT_PCM_16) ;
165 permission_test ("readonly.pvf", SF_FORMAT_PVF) ;
166 test_count++ ;
167 } ;
168
169 if (do_all || ! strcmp (argv [1], "htk"))
170 { zero_data_test ("zerolen.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16) ;
171 filesystem_full_test (SF_FORMAT_HTK | SF_FORMAT_PCM_16) ;
172 permission_test ("readonly.htk", SF_FORMAT_HTK) ;
173 test_count++ ;
174 } ;
175
176 if (do_all || ! strcmp (argv [1], "avr"))
177 { zero_data_test ("zerolen.avr", SF_FORMAT_AVR | SF_FORMAT_PCM_16) ;
178 filesystem_full_test (SF_FORMAT_AVR | SF_FORMAT_PCM_16) ;
179 permission_test ("readonly.avr", SF_FORMAT_AVR) ;
180 test_count++ ;
181 } ;
182
183 if (do_all || ! strcmp (argv [1], "sds"))
184 { zero_data_test ("zerolen.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
185 filesystem_full_test (SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
186 permission_test ("readonly.sds", SF_FORMAT_SDS) ;
187 test_count++ ;
188 } ;
189
190 if (do_all || ! strcmp (argv [1], "mpc2k"))
191 { zero_data_test ("zerolen.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16) ;
192 filesystem_full_test (SF_FORMAT_MPC2K | SF_FORMAT_PCM_16) ;
193 permission_test ("readonly.mpc", SF_FORMAT_MPC2K) ;
194 test_count++ ;
195 } ;
196
197 if (do_all || ! strcmp (argv [1], "ogg"))
198 { zero_data_test ("zerolen.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;
199 /*-filesystem_full_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;-*/
200 permission_test ("readonly.oga", SF_FORMAT_OGG) ;
201 test_count++ ;
202 } ;
203
204 if (test_count == 0)
205 { printf ("Mono : ************************************\n") ;
206 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
207 printf ("Mono : ************************************\n") ;
208 return 1 ;
209 } ;
210
211 return 0 ;
212 } /* main */
213
214
215 /*============================================================================================
216 ** Here are the test functions.
217 */
218
219 static void
220 zero_data_test (const char *filename, int format)
221 { SNDFILE *file ;
222 SF_INFO sfinfo ;
223
224 switch (format & SF_FORMAT_TYPEMASK)
225 { case SF_FORMAT_OGG :
226 if (HAVE_EXTERNAL_LIBS == 0)
227 return ;
228 break ;
229 default :
230 break ;
231 } ;
232
233 print_test_name ("zero_data_test", filename) ;
234
235 sfinfo.samplerate = 44100 ;
236 sfinfo.format = format ;
237 sfinfo.channels = 1 ;
238 sfinfo.frames = 0 ;
239
240 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
241
242 sf_close (file) ;
243
244 memset (&sfinfo, 0, sizeof (sfinfo)) ;
245
246 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
247
248 sf_close (file) ;
249
250 unlink (filename) ;
251 puts ("ok") ;
252 } /* zero_data_test */
253
254 static void
255 filesystem_full_test (int format)
256 { SNDFILE *file ;
257 SF_INFO sfinfo ;
258 struct stat buf ;
259
260 const char *filename = "/dev/full", *errorstr ;
261
262 #if (defined (WIN32) || defined (_WIN32))
263 /* Can't run this test on Win32 so return. */
264 return ;
265 #endif
266
267 /* Make sure errno is zero before doing anything else. */
268 errno = 0 ;
269
270 print_test_name ("filesystem_full_test", filename) ;
271
272 if (stat (filename, &buf) != 0)
273 { puts ("/dev/full missing") ;
274 return ;
275 } ;
276
277 if (S_ISCHR (buf.st_mode) == 0 && S_ISBLK (buf.st_mode) == 0)
278 { puts ("/dev/full is not a device file") ;
279 return ;
280 } ;
281
282 sfinfo.samplerate = 44100 ;
283 sfinfo.format = format ;
284 sfinfo.channels = 1 ;
285 sfinfo.frames = 0 ;
286
287 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
288 { printf ("\n\nLine %d : Error, file should not have openned.\n", __LINE__ - 1) ;
289 exit (1) ;
290 } ;
291
292 errorstr = sf_strerror (file) ;
293
294 if (strstr (errorstr, " space ") == NULL || strstr (errorstr, "device") == NULL)
295 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
296 exit (1) ;
297 } ;
298
299 puts ("ok") ;
300 } /* filesystem_full_test */
301
302 static void
303 permission_test (const char *filename, int typemajor)
304 {
305 #if (OS_IS_WIN32)
306 /* Avoid compiler warnings. */
307 filename = filename ;
308 typemajor = typemajor ;
309
310 /* Can't run this test on Win32 so return. */
311 return ;
312 #else
313
314 FILE *textfile ;
315 SNDFILE *file ;
316 SF_INFO sfinfo ;
317 const char *errorstr ;
318
319 /* Make sure errno is zero before doing anything else. */
320 errno = 0 ;
321
322 if (getuid () == 0)
323 { /* If running as root bypass this test.
324 ** Root is allowed to open a readonly file for write.
325 */
326 return ;
327 } ;
328
329 print_test_name ("permission_test", filename) ;
330
331 if (access (filename, F_OK) == 0)
332 { chmod (filename, S_IWUSR) ;
333 unlink (filename) ;
334 } ;
335
336 if ((textfile = fopen (filename, "w")) == NULL)
337 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
338 exit (1) ;
339 } ;
340
341 fprintf (textfile, "This is a read only file.\n") ;
342 fclose (textfile) ;
343
344 if (chmod (filename, S_IRUSR | S_IRGRP))
345 { printf ("\n\nLine %d : chmod failed", __LINE__) ;
346 fflush (stdout) ;
347 perror ("") ;
348 exit (1) ;
349 } ;
350
351 sfinfo.samplerate = 44100 ;
352 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
353 sfinfo.channels = 1 ;
354 sfinfo.frames = 0 ;
355
356 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
357 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
358 exit (1) ;
359 } ;
360
361 errorstr = sf_strerror (file) ;
362
363 if (strstr (errorstr, "ermission denied") == NULL)
364 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
365 exit (1) ;
366 } ;
367
368 if (chmod (filename, S_IWUSR | S_IWGRP))
369 { printf ("\n\nLine %d : chmod failed", __LINE__) ;
370 fflush (stdout) ;
371 perror ("") ;
372 exit (1) ;
373 } ;
374
375 unlink (filename) ;
376
377 puts ("ok") ;
378
379 #endif
380 } /* permission_test */
381
382 static void
383 wavex_amb_test (const char *filename)
384 { static short buffer [800] ;
385 SNDFILE *file ;
386 SF_INFO sfinfo ;
387 sf_count_t frames ;
388
389 print_test_name (__func__, filename) ;
390
391 sfinfo.samplerate = 44100 ;
392 sfinfo.format = SF_FORMAT_WAVEX | SF_FORMAT_PCM_16 ;
393 sfinfo.channels = 4 ;
394
395 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
396
397 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
398 sf_command (file, SFC_WAVEX_SET_AMBISONIC, NULL, SF_AMBISONIC_B_FORMAT) ;
399 test_writef_short_or_die (file, 0, buffer, frames, __LINE__) ;
400 sf_close (file) ;
401
402 memset (&sfinfo, 0, sizeof (sfinfo)) ;
403
404 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
405
406 exit_if_true (
407 sf_command (file, SFC_WAVEX_GET_AMBISONIC, NULL, 0) != SF_AMBISONIC_B_FORMAT,
408 "\n\nLine %d : Error, this file should be in Ambisonic B format.\n", __LINE__
409 ) ;
410
411 sf_close (file) ;
412
413 unlink (filename) ;
414 puts ("ok") ;
415 } /* wavex_amb_test */