r663: This commit was generated by cvs2svn to compensate for changes in r662,
[cinelerra_cv.git] / libsndfile / tests / misc_test.c
blob82fd34c1ab68223338cd489e4ce6722fa581c66b
1 /*
2 ** Copyright (C) 2001-2003 Erik de Castro Lopo <erikd@zip.com.au>
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.
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.
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <math.h>
27 #if (defined (WIN32) || defined (_WIN32))
28 #include <io.h>
29 #include <direct.h>
31 #define _IFMT _S_IFMT
32 #define _IFREG _S_IFREG
34 #define S_ISREG(m) ((m) & _S_IFREG)
36 #define S_IRWXU 0000700 /* rwx, owner */
37 #define S_IRUSR 0000400 /* read permission, owner */
38 #define S_IWUSR 0000200 /* write permission, owner */
39 #define S_IXUSR 0000100 /* execute/search permission, owner */
40 #define S_IRWXG 0000070 /* rwx, group */
41 #define S_IRGRP 0000040 /* read permission, group */
42 #define S_IWGRP 0000020 /* write permission, grougroup */
43 #define S_IXGRP 0000010 /* execute/search permission, group */
44 #define S_IRWXO 0000007 /* rwx, other */
45 #define S_IROTH 0000004 /* read permission, other */
46 #define S_IWOTH 0000002 /* write permission, other */
47 #define S_IXOTH 0000001 /* execute/search permission, other */
49 #endif
51 #include <sndfile.h>
53 #include "utils.h"
55 #define BUFFER_LEN (1<<10)
56 #define LOG_BUFFER_SIZE 1024
58 static void update_header_test (char *filename, int typemajor) ;
59 static void zero_data_test (char *filename, int typemajor) ;
60 static void filesystem_full_test (int typemajor) ;
61 static void permission_test (char *filename, int typemajor) ;
63 /* Force the start of this buffer to be double aligned. Sparc-solaris will
64 ** choke if its not.
66 static short data_out [BUFFER_LEN] ;
67 static short data_in [BUFFER_LEN] ;
69 int
70 main (int argc, char *argv[])
71 { int do_all = 0 ;
72 int test_count = 0 ;
74 if (argc != 2)
75 { printf ("Usage : %s <test>\n", argv [0]) ;
76 printf (" Where <test> is one of the following:\n") ;
77 printf (" wav - test WAV file peak chunk\n") ;
78 printf (" aiff - test AIFF file PEAK chunk\n") ;
79 printf (" all - perform all tests\n") ;
80 exit (1) ;
81 } ;
83 do_all=!strcmp (argv [1], "all");
85 if (do_all || ! strcmp (argv [1], "wav"))
86 { update_header_test ("header.wav", SF_FORMAT_WAV) ;
87 zero_data_test ("zerolen.wav", SF_FORMAT_WAV) ;
88 filesystem_full_test (SF_FORMAT_WAV) ;
89 permission_test ("readonly.wav", SF_FORMAT_WAV) ;
90 test_count++ ;
91 } ;
93 if (do_all || ! strcmp (argv [1], "aiff"))
94 { update_header_test ("header.aiff", SF_FORMAT_AIFF) ;
95 zero_data_test ("zerolen.aiff", SF_FORMAT_AIFF) ;
96 filesystem_full_test (SF_FORMAT_AIFF) ;
97 permission_test ("readonly.aiff", SF_FORMAT_AIFF) ;
98 test_count++ ;
99 } ;
101 if (do_all || ! strcmp (argv [1], "au"))
102 { update_header_test ("header.au", SF_FORMAT_AU) ;
103 zero_data_test ("zerolen.au", SF_FORMAT_AU) ;
104 filesystem_full_test (SF_FORMAT_AU) ;
105 permission_test ("readonly.au", SF_FORMAT_AU) ;
106 test_count++ ;
109 if (do_all || ! strcmp (argv [1], "svx"))
110 { update_header_test ("header.svx", SF_FORMAT_SVX) ;
111 zero_data_test ("zerolen.svx", SF_FORMAT_SVX) ;
112 filesystem_full_test (SF_FORMAT_SVX) ;
113 permission_test ("readonly.svx", SF_FORMAT_SVX) ;
114 test_count++ ;
117 if (do_all || ! strcmp (argv [1], "nist"))
118 { update_header_test ("header.nist", SF_FORMAT_NIST) ;
119 zero_data_test ("zerolen.nist", SF_FORMAT_NIST) ;
120 filesystem_full_test (SF_FORMAT_NIST) ;
121 permission_test ("readonly.nist", SF_FORMAT_NIST) ;
122 test_count++ ;
125 if (do_all || ! strcmp (argv [1], "paf"))
126 { update_header_test ("header.paf", SF_FORMAT_PAF) ;
127 zero_data_test ("zerolen.paf", SF_FORMAT_PAF) ;
128 filesystem_full_test (SF_FORMAT_PAF) ;
129 permission_test ("readonly.paf", SF_FORMAT_PAF) ;
130 test_count++ ;
133 if (do_all || ! strcmp (argv [1], "ircam"))
134 { update_header_test ("header.ircam", SF_FORMAT_IRCAM) ;
135 zero_data_test ("zerolen.ircam", SF_FORMAT_IRCAM) ;
136 filesystem_full_test (SF_FORMAT_IRCAM) ;
137 permission_test ("readonly.ircam", SF_FORMAT_IRCAM) ;
138 test_count++ ;
141 if (do_all || ! strcmp (argv [1], "voc"))
142 { update_header_test ("header.voc", SF_FORMAT_VOC) ;
143 zero_data_test ("zerolen.voc", SF_FORMAT_VOC) ;
144 filesystem_full_test (SF_FORMAT_VOC) ;
145 permission_test ("readonly.voc", SF_FORMAT_VOC) ;
146 test_count++ ;
149 if (do_all || ! strcmp (argv [1], "w64"))
150 { update_header_test ("header.w64", SF_FORMAT_W64) ;
151 zero_data_test ("zerolen.w64", SF_FORMAT_W64) ;
152 filesystem_full_test (SF_FORMAT_W64) ;
153 permission_test ("readonly.w64", SF_FORMAT_W64) ;
154 test_count++ ;
157 if (do_all || ! strcmp (argv [1], "mat4"))
158 { update_header_test ("header.mat4", SF_FORMAT_MAT4) ;
159 zero_data_test ("zerolen.mat4", SF_FORMAT_MAT4) ;
160 filesystem_full_test (SF_FORMAT_MAT4) ;
161 permission_test ("readonly.mat4", SF_FORMAT_MAT4) ;
162 test_count++ ;
165 if (do_all || ! strcmp (argv [1], "mat5"))
166 { update_header_test ("header.mat5", SF_FORMAT_MAT5) ;
167 zero_data_test ("zerolen.mat5", SF_FORMAT_MAT5) ;
168 filesystem_full_test (SF_FORMAT_MAT5) ;
169 permission_test ("readonly.mat5", SF_FORMAT_MAT5) ;
170 test_count++ ;
173 if (do_all || ! strcmp (argv [1], "pvf"))
174 { update_header_test ("header.pvf", SF_FORMAT_PVF) ;
175 zero_data_test ("zerolen.pvf", SF_FORMAT_PVF) ;
176 filesystem_full_test (SF_FORMAT_PVF) ;
177 permission_test ("readonly.pvf", SF_FORMAT_PVF) ;
178 test_count++ ;
181 if (do_all || ! strcmp (argv [1], "htk"))
182 { update_header_test ("header.htk", SF_FORMAT_HTK) ;
183 zero_data_test ("zerolen.htk", SF_FORMAT_HTK) ;
184 filesystem_full_test (SF_FORMAT_HTK) ;
185 permission_test ("readonly.htk", SF_FORMAT_HTK) ;
186 test_count++ ;
189 if (test_count == 0)
190 { printf ("Mono : ************************************\n") ;
191 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
192 printf ("Mono : ************************************\n") ;
193 return 1 ;
196 return 0;
197 } /* main */
200 /*============================================================================================
201 ** Here are the test functions.
204 static void
205 update_header_test (char *filename, int typemajor)
206 { SNDFILE *outfile, *infile ;
207 SF_INFO sfinfo ;
208 int k, frames ;
210 print_test_name ("update_header_test", filename) ;
212 sfinfo.samplerate = 44100 ;
213 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
214 sfinfo.channels = 1 ;
215 sfinfo.frames = 0 ;
217 frames = BUFFER_LEN / sfinfo.channels ;
219 outfile = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, __LINE__) ;
221 for (k = 0 ; k < BUFFER_LEN ; k++)
222 data_out [k] = k + 1 ;
223 test_write_short_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
225 if (typemajor != SF_FORMAT_HTK)
226 { /* The HTK header is not correct when the file is firstw written. */
227 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ;
228 sf_close (infile) ;
231 sf_command (outfile, SFC_UPDATE_HEADER_NOW, NULL, 0) ;
233 /* Open file and check log buffer for an error. If header update failed
234 ** the the log buffer will contain errors.
236 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ;
237 check_log_buffer_or_die (infile) ;
239 if (abs (BUFFER_LEN - sfinfo.frames) > 1)
240 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ;
241 dump_log_buffer (infile) ;
242 exit (1) ;
245 test_read_short_or_die (infile, 0, data_in, BUFFER_LEN, __LINE__) ;
246 for (k = 0 ; k < BUFFER_LEN ; k++)
247 if (data_out [k] != k + 1)
248 printf ("Error : line %d\n", __LINE__) ;
250 sf_close (infile) ;
252 /* Set auto update on. */
253 sf_command (outfile, SFC_SET_UPDATE_HEADER_AUTO, NULL, SF_TRUE) ;
255 /* Write more data_out. */
256 for (k = 0 ; k < BUFFER_LEN ; k++)
257 data_out [k] = k + 2 ;
258 test_write_short_or_die (outfile, 0, data_out, BUFFER_LEN, __LINE__) ;
260 /* Open file again and make sure no errors in log buffer. */
261 infile = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ;
262 check_log_buffer_or_die (infile) ;
264 if (abs (2 * BUFFER_LEN - sfinfo.frames) > 1)
265 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), 2 * BUFFER_LEN) ;
266 dump_log_buffer (infile) ;
267 exit (1) ;
270 sf_close (infile) ;
272 sf_close (outfile) ;
274 unlink (filename) ;
275 puts ("ok") ;
276 } /* update_header_test */
278 static void
279 zero_data_test (char *filename, int typemajor)
280 { SNDFILE *file ;
281 SF_INFO sfinfo ;
282 int frames ;
284 print_test_name ("zero_data_test", filename) ;
286 sfinfo.samplerate = 44100 ;
287 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
288 sfinfo.channels = 1 ;
289 sfinfo.frames = 0 ;
291 frames = BUFFER_LEN / sfinfo.channels ;
293 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, __LINE__) ;
295 sf_close (file) ;
297 memset (&sfinfo, 0, sizeof (sfinfo)) ;
299 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, __LINE__) ;
301 sf_close (file) ;
303 unlink (filename) ;
304 puts ("ok") ;
305 } /* zero_data_test */
307 static void
308 filesystem_full_test (int typemajor)
309 { SNDFILE *file ;
310 SF_INFO sfinfo ;
311 struct stat buf ;
313 const char *filename = "/dev/full", *errorstr ;
314 int frames ;
316 #if (defined (WIN32) || defined (_WIN32))
317 /* Can't run this test on Win32 so return. */
318 return ;
319 #endif
321 print_test_name ("filesystem_full_test", filename) ;
323 if (stat (filename, &buf) != 0)
324 { puts ("/dev/full missing") ;
325 return ;
328 if (S_ISCHR (buf.st_mode) == 0 && S_ISBLK (buf.st_mode) == 0)
329 { puts ("/dev/full is not a device file") ;
330 return ;
333 sfinfo.samplerate = 44100 ;
334 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
335 sfinfo.channels = 1 ;
336 sfinfo.frames = 0 ;
338 frames = BUFFER_LEN / sfinfo.channels ;
340 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
341 { printf ("\n\nLine %d : Error, file should not have openned.\n", __LINE__ - 1) ;
342 exit (1) ;
345 errorstr = sf_strerror (file) ;
347 if (strstr (errorstr, " space ") == NULL || strstr (errorstr, "device") == NULL)
348 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
349 exit (1) ;
352 puts ("ok") ;
353 } /* filesystem_full_test */
355 static void
356 permission_test (char *filename, int typemajor)
357 { FILE *textfile ;
358 SNDFILE *file ;
359 SF_INFO sfinfo ;
360 const char *errorstr ;
362 int frames ;
364 #if (defined (WIN32) || defined (_WIN32))
365 /* Can't run this test on Win32 so return. */
366 return ;
367 #endif
369 if (getuid () == 0)
370 { /* If running as root bypass this test.
371 ** Root is allowed to open a readonly file for write.
373 return ;
376 print_test_name ("permission_test", filename) ;
378 if ((textfile = fopen (filename, "w")) == NULL)
379 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
380 exit (1) ;
383 fprintf (textfile, "This is a read only file.\n") ;
384 fclose (textfile) ;
386 if (chmod (filename, S_IRUSR | S_IRGRP))
387 { printf ("\n\nLine %d : chmod failed", __LINE__) ;
388 fflush (stdout) ;
389 perror ("") ;
390 exit (1) ;
393 sfinfo.samplerate = 44100 ;
394 sfinfo.format = (typemajor | SF_FORMAT_PCM_16) ;
395 sfinfo.channels = 1 ;
396 sfinfo.frames = 0 ;
398 frames = BUFFER_LEN / sfinfo.channels ;
400 if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL)
401 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
402 exit (1) ;
405 errorstr = sf_strerror (file) ;
407 if (strstr (errorstr, "ould not open file") == NULL)
408 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;
409 exit (1) ;
412 if (chmod (filename, S_IWUSR | S_IWGRP))
413 { printf ("\n\nLine %d : chmod failed", __LINE__) ;
414 fflush (stdout) ;
415 perror ("") ;
416 exit (1) ;
419 unlink (filename) ;
421 puts ("ok") ;
422 } /* permission_test */