comparison src/libsndfile-1.0.27/tests/misc_test.c @ 40:1df64224f5ac

Current libsndfile source
author Chris Cannam
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
comparison
equal deleted inserted replaced
39:7ddb4fc30dac 40:1df64224f5ac
1 /*
2 ** Copyright (C) 2001-2016 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 #include <inttypes.h>
26
27 #include <sys/stat.h>
28 #include <math.h>
29
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #if (HAVE_DECL_S_IRGRP == 0)
35 #include <sf_unistd.h>
36 #endif
37
38 #if (defined (WIN32) || defined (_WIN32))
39 #include <io.h>
40 #include <direct.h>
41 #endif
42
43 #include <sndfile.h>
44
45 #include "utils.h"
46
47 #define BUFFER_LEN (1 << 10)
48 #define LOG_BUFFER_SIZE 1024
49
50 static void zero_data_test (const char *filename, int format) ;
51 static void filesystem_full_test (int format) ;
52 static void permission_test (const char *filename, int typemajor) ;
53 static void wavex_amb_test (const char *filename) ;
54 static void rf64_downgrade_test (const char *filename) ;
55
56 int
57 main (int argc, char *argv [])
58 { int do_all = 0 ;
59 int test_count = 0 ;
60
61 if (argc != 2)
62 { printf ("Usage : %s <test>\n", argv [0]) ;
63 printf (" Where <test> is one of the following:\n") ;
64 printf (" wav - test WAV file peak chunk\n") ;
65 printf (" aiff - test AIFF file PEAK chunk\n") ;
66 printf (" all - perform all tests\n") ;
67 exit (1) ;
68 } ;
69
70 do_all = ! strcmp (argv [1], "all") ;
71
72 if (do_all || ! strcmp (argv [1], "wav"))
73 { zero_data_test ("zerolen.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
74 filesystem_full_test (SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
75 permission_test ("readonly.wav", SF_FORMAT_WAV) ;
76 wavex_amb_test ("ambisonic.wav") ;
77 test_count++ ;
78 } ;
79
80 if (do_all || ! strcmp (argv [1], "aiff"))
81 { zero_data_test ("zerolen.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
82 filesystem_full_test (SF_FORMAT_AIFF | SF_FORMAT_PCM_16) ;
83 permission_test ("readonly.aiff", SF_FORMAT_AIFF) ;
84 test_count++ ;
85 } ;
86
87 if (do_all || ! strcmp (argv [1], "au"))
88 { zero_data_test ("zerolen.au", SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
89 filesystem_full_test (SF_FORMAT_AU | SF_FORMAT_PCM_16) ;
90 permission_test ("readonly.au", SF_FORMAT_AU) ;
91 test_count++ ;
92 } ;
93
94 if (do_all || ! strcmp (argv [1], "caf"))
95 { zero_data_test ("zerolen.caf", SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
96 filesystem_full_test (SF_FORMAT_CAF | SF_FORMAT_PCM_16) ;
97 permission_test ("readonly.caf", SF_FORMAT_CAF) ;
98 test_count++ ;
99 } ;
100
101 if (do_all || ! strcmp (argv [1], "svx"))
102 { zero_data_test ("zerolen.svx", SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
103 filesystem_full_test (SF_FORMAT_SVX | SF_FORMAT_PCM_16) ;
104 permission_test ("readonly.svx", SF_FORMAT_SVX) ;
105 test_count++ ;
106 } ;
107
108 if (do_all || ! strcmp (argv [1], "nist"))
109 { zero_data_test ("zerolen.nist", SF_FORMAT_NIST | SF_FORMAT_PCM_16) ;
110 filesystem_full_test (SF_FORMAT_NIST | SF_FORMAT_PCM_16) ;
111 permission_test ("readonly.nist", SF_FORMAT_NIST) ;
112 test_count++ ;
113 } ;
114
115 if (do_all || ! strcmp (argv [1], "paf"))
116 { zero_data_test ("zerolen.paf", SF_FORMAT_PAF | SF_FORMAT_PCM_16) ;
117 filesystem_full_test (SF_FORMAT_PAF | SF_FORMAT_PCM_16) ;
118 permission_test ("readonly.paf", SF_FORMAT_PAF) ;
119 test_count++ ;
120 } ;
121
122 if (do_all || ! strcmp (argv [1], "ircam"))
123 { zero_data_test ("zerolen.ircam", SF_FORMAT_IRCAM | SF_FORMAT_PCM_16) ;
124 filesystem_full_test (SF_FORMAT_IRCAM | SF_FORMAT_PCM_16) ;
125 permission_test ("readonly.ircam", SF_FORMAT_IRCAM) ;
126 test_count++ ;
127 } ;
128
129 if (do_all || ! strcmp (argv [1], "voc"))
130 { zero_data_test ("zerolen.voc", SF_FORMAT_VOC | SF_FORMAT_PCM_16) ;
131 filesystem_full_test (SF_FORMAT_VOC | SF_FORMAT_PCM_16) ;
132 permission_test ("readonly.voc", SF_FORMAT_VOC) ;
133 test_count++ ;
134 } ;
135
136 if (do_all || ! strcmp (argv [1], "w64"))
137 { zero_data_test ("zerolen.w64", SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
138 filesystem_full_test (SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
139 permission_test ("readonly.w64", SF_FORMAT_W64) ;
140 test_count++ ;
141 } ;
142
143 if (do_all || ! strcmp (argv [1], "rf64"))
144 { zero_data_test ("zerolen.rf64", SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
145 filesystem_full_test (SF_FORMAT_RF64 | SF_FORMAT_PCM_16) ;
146 permission_test ("readonly.rf64", SF_FORMAT_RF64) ;
147 rf64_downgrade_test ("downgrade.wav") ;
148 test_count++ ;
149 } ;
150
151 if (do_all || ! strcmp (argv [1], "mat4"))
152 { zero_data_test ("zerolen.mat4", SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
153 filesystem_full_test (SF_FORMAT_MAT4 | SF_FORMAT_PCM_16) ;
154 permission_test ("readonly.mat4", SF_FORMAT_MAT4) ;
155 test_count++ ;
156 } ;
157
158 if (do_all || ! strcmp (argv [1], "mat5"))
159 { zero_data_test ("zerolen.mat5", SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
160 filesystem_full_test (SF_FORMAT_MAT5 | SF_FORMAT_PCM_16) ;
161 permission_test ("readonly.mat5", SF_FORMAT_MAT5) ;
162 test_count++ ;
163 } ;
164
165 if (do_all || ! strcmp (argv [1], "pvf"))
166 { zero_data_test ("zerolen.pvf", SF_FORMAT_PVF | SF_FORMAT_PCM_16) ;
167 filesystem_full_test (SF_FORMAT_PVF | SF_FORMAT_PCM_16) ;
168 permission_test ("readonly.pvf", SF_FORMAT_PVF) ;
169 test_count++ ;
170 } ;
171
172 if (do_all || ! strcmp (argv [1], "htk"))
173 { zero_data_test ("zerolen.htk", SF_FORMAT_HTK | SF_FORMAT_PCM_16) ;
174 filesystem_full_test (SF_FORMAT_HTK | SF_FORMAT_PCM_16) ;
175 permission_test ("readonly.htk", SF_FORMAT_HTK) ;
176 test_count++ ;
177 } ;
178
179 if (do_all || ! strcmp (argv [1], "avr"))
180 { zero_data_test ("zerolen.avr", SF_FORMAT_AVR | SF_FORMAT_PCM_16) ;
181 filesystem_full_test (SF_FORMAT_AVR | SF_FORMAT_PCM_16) ;
182 permission_test ("readonly.avr", SF_FORMAT_AVR) ;
183 test_count++ ;
184 } ;
185
186 if (do_all || ! strcmp (argv [1], "sds"))
187 { zero_data_test ("zerolen.sds", SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
188 filesystem_full_test (SF_FORMAT_SDS | SF_FORMAT_PCM_16) ;
189 permission_test ("readonly.sds", SF_FORMAT_SDS) ;
190 test_count++ ;
191 } ;
192
193 if (do_all || ! strcmp (argv [1], "mpc2k"))
194 { zero_data_test ("zerolen.mpc", SF_FORMAT_MPC2K | SF_FORMAT_PCM_16) ;
195 filesystem_full_test (SF_FORMAT_MPC2K | SF_FORMAT_PCM_16) ;
196 permission_test ("readonly.mpc", SF_FORMAT_MPC2K) ;
197 test_count++ ;
198 } ;
199
200 if (do_all || ! strcmp (argv [1], "ogg"))
201 { zero_data_test ("zerolen.oga", SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;
202 /*-filesystem_full_test (SF_FORMAT_OGG | SF_FORMAT_VORBIS) ;-*/
203 permission_test ("readonly.oga", SF_FORMAT_OGG) ;
204 test_count++ ;
205 } ;
206
207 if (test_count == 0)
208 { printf ("Mono : ************************************\n") ;
209 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
210 printf ("Mono : ************************************\n") ;
211 return 1 ;
212 } ;
213
214 return 0 ;
215 } /* main */
216
217
218 /*============================================================================================
219 ** Here are the test functions.
220 */
221
222 static void
223 zero_data_test (const char *filename, int format)
224 { SNDFILE *file ;
225 SF_INFO sfinfo ;
226
227 switch (format & SF_FORMAT_TYPEMASK)
228 { case SF_FORMAT_OGG :
229 if (HAVE_EXTERNAL_XIPH_LIBS == 0)
230 return ;
231 break ;
232 default :
233 break ;
234 } ;
235
236 print_test_name ("zero_data_test", filename) ;
237
238 sfinfo.samplerate = 44100 ;
239 sfinfo.format = format ;
240 sfinfo.channels = 1 ;
241 sfinfo.frames = 0 ;
242
243 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
244
245 sf_close (file) ;
246
247 memset (&sfinfo, 0, sizeof (sfinfo)) ;
248
249 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
250
251 sf_close (file) ;
252
253 unlink (filename) ;
254 puts ("ok") ;
255 } /* zero_data_test */
256
257 static void
258 filesystem_full_test (int format)
259 { SNDFILE *file ;
260 SF_INFO sfinfo ;
261 struct stat buf ;
262
263 const char *filename = "/dev/full", *errorstr ;
264
265 #if (defined (WIN32) || defined (_WIN32))
266 /* Can't run this test on Win32 so return. */
267 return ;
268 #endif
269
270 /* Make sure errno is zero before doing anything else. */
271 errno = 0 ;
272
273 print_test_name ("filesystem_full_test", filename) ;
274
275 if (stat (filename, &buf) != 0)
276 { puts ("/dev/full missing") ;
277 return ;
278 } ;
279
280 if (S_ISCHR (buf.st_mode) == 0 && S_ISBLK (buf.st_mode) == 0)
281 { puts ("/dev/full is not a device file") ;
282 return ;
283 } ;
284
285 sfinfo.samplerate = 44100 ;
286 sfinfo.format = format ;
287 sfinfo.channels = 1 ;
288 sfinfo.frames = 0 ;
289
290 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
291 { printf ("\n\nLine %d : Error, file should not have openned.\n", __LINE__ - 1) ;
292 exit (1) ;
293 } ;
294
295 errorstr = sf_strerror (file) ;
296
297 if (strstr (errorstr, " space ") == NULL || strstr (errorstr, "device") == NULL)
298 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
299 exit (1) ;
300 } ;
301
302 puts ("ok") ;
303 } /* filesystem_full_test */
304
305 static void
306 permission_test (const char *filename, int typemajor)
307 {
308 #if (OS_IS_WIN32)
309 /* Avoid compiler warnings. */
310 filename = filename ;
311 typemajor = typemajor ;
312
313 /* Can't run this test on Win32 so return. */
314 return ;
315 #else
316
317 FILE *textfile ;
318 SNDFILE *file ;
319 SF_INFO sfinfo ;
320 const char *errorstr ;
321
322 /* Make sure errno is zero before doing anything else. */
323 errno = 0 ;
324
325 if (getuid () == 0)
326 { /* If running as root bypass this test.
327 ** Root is allowed to open a readonly file for write.
328 */
329 return ;
330 } ;
331
332 print_test_name ("permission_test", filename) ;
333
334 if (access (filename, F_OK) == 0)
335 { chmod (filename, S_IWUSR) ;
336 unlink (filename) ;
337 } ;
338
339 if ((textfile = fopen (filename, "w")) == NULL)
340 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
341 exit (1) ;
342 } ;
343
344 fprintf (textfile, "This is a read only file.\n") ;
345 fclose (textfile) ;
346
347 if (chmod (filename, S_IRUSR | S_IRGRP))
348 { printf ("\n\nLine %d : chmod failed", __LINE__) ;
349 fflush (stdout) ;
350 perror ("") ;
351 exit (1) ;
352 } ;
353
354 sfinfo.samplerate = 44100 ;
355 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
356 sfinfo.channels = 1 ;
357 sfinfo.frames = 0 ;
358
359 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
360 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
361 exit (1) ;
362 } ;
363
364 errorstr = sf_strerror (file) ;
365
366 if (strstr (errorstr, "ermission denied") == NULL)
367 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
368 exit (1) ;
369 } ;
370
371 if (chmod (filename, S_IWUSR | S_IWGRP))
372 { printf ("\n\nLine %d : chmod failed", __LINE__) ;
373 fflush (stdout) ;
374 perror ("") ;
375 exit (1) ;
376 } ;
377
378 unlink (filename) ;
379
380 puts ("ok") ;
381
382 #endif
383 } /* permission_test */
384
385 static void
386 wavex_amb_test (const char *filename)
387 { static short buffer [800] ;
388 SNDFILE *file ;
389 SF_INFO sfinfo ;
390 sf_count_t frames ;
391
392 print_test_name (__func__, filename) ;
393
394 sfinfo.samplerate = 44100 ;
395 sfinfo.format = SF_FORMAT_WAVEX | SF_FORMAT_PCM_16 ;
396 sfinfo.channels = 4 ;
397
398 frames = ARRAY_LEN (buffer) / sfinfo.channels ;
399
400 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
401 sf_command (file, SFC_WAVEX_SET_AMBISONIC, NULL, SF_AMBISONIC_B_FORMAT) ;
402 test_writef_short_or_die (file, 0, buffer, frames, __LINE__) ;
403 sf_close (file) ;
404
405 memset (&sfinfo, 0, sizeof (sfinfo)) ;
406
407 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
408
409 exit_if_true (
410 sf_command (file, SFC_WAVEX_GET_AMBISONIC, NULL, 0) != SF_AMBISONIC_B_FORMAT,
411 "\n\nLine %d : Error, this file should be in Ambisonic B format.\n", __LINE__
412 ) ;
413
414 sf_close (file) ;
415
416 unlink (filename) ;
417 puts ("ok") ;
418 } /* wavex_amb_test */
419
420 static void
421 rf64_downgrade_test (const char *filename)
422 { static short output [BUFFER_LEN] ;
423 static short input [BUFFER_LEN] ;
424
425 SNDFILE *file ;
426 SF_INFO sfinfo ;
427 unsigned k ;
428
429 print_test_name (__func__, filename) ;
430
431 sf_info_clear (&sfinfo) ;
432
433 sfinfo.samplerate = 44100 ;
434 sfinfo.frames = ARRAY_LEN (output) ;
435 sfinfo.channels = 1 ;
436 sfinfo.format = SF_FORMAT_RF64 | SF_FORMAT_PCM_16 ;
437
438 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
439
440 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_FALSE) != SF_FALSE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
441 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_TRUE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
442
443 test_write_short_or_die (file, 0, output, ARRAY_LEN (output), __LINE__) ;
444
445 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_FALSE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
446 exit_if_true (sf_command (file, SFC_RF64_AUTO_DOWNGRADE, NULL, SF_TRUE) != SF_TRUE, "\n\nLine %d: sf_command failed.\n", __LINE__) ;
447
448 sf_close (file) ;
449
450 memset (input, 0, sizeof (input)) ;
451 sf_info_clear (&sfinfo) ;
452
453 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
454
455 exit_if_true (sfinfo.format != (SF_FORMAT_WAVEX | SF_FORMAT_PCM_16), "\n\nLine %d: RF64 to WAV downgrade failed.\n", __LINE__) ;
456 exit_if_true (sfinfo.frames != ARRAY_LEN (output), "\n\nLine %d: Incorrect number of frames in file (too short). (%d should be %d)\n", __LINE__, (int) sfinfo.frames, (int) ARRAY_LEN (output)) ;
457 exit_if_true (sfinfo.channels != 1, "\n\nLine %d: Incorrect number of channels in file.\n", __LINE__) ;
458
459 check_log_buffer_or_die (file, __LINE__) ;
460
461 test_read_short_or_die (file, 0, input, ARRAY_LEN (input), __LINE__) ;
462
463 sf_close (file) ;
464
465 for (k = 0 ; k < ARRAY_LEN (input) ; k++)
466 exit_if_true (input [k] != output [k],
467 "\n\nLine: %d: Error on input %d, expected %d, got %d\n", __LINE__, k, output [k], input [k]) ;
468
469 puts ("ok") ;
470 unlink (filename) ;
471
472 return ;
473 } /* rf64_downgrade_test */