2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
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.
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.
37 #define BUFFER_SIZE (1<<15)
38 #define SHORT_BUFFER (256)
41 error_number_test (void)
42 { const char *noerror
, *errstr
;
45 print_test_name ("error_number_test", "") ;
47 noerror
= sf_error_number (0) ;
49 for (k
= 1 ; k
< 300 ; k
++)
50 { errstr
= sf_error_number (k
) ;
52 /* Test for termination condition. */
53 if (errstr
== noerror
)
57 if (strstr (errstr
, "This is a bug in libsndfile."))
58 { printf ("\n\nError : error number %d : %s\n\n\n", k
, errstr
) ;
66 } /* error_number_test */
69 error_value_test (void)
70 { static unsigned char aiff_data
[0x1b0] =
71 { 'F' , 'O' , 'R' , 'M' ,
72 0x00, 0x00, 0x01, 0xA8, /* FORM length */
74 'A' , 'I' , 'F' , 'C' ,
77 const char *filename
= "error.aiff" ;
82 print_test_name ("error_value_test", filename
) ;
84 dump_data_to_file (filename
, aiff_data
, sizeof (aiff_data
)) ;
86 file
= sf_open (filename
, SFM_READ
, &sfinfo
) ;
88 { printf ("\n\nLine %d : Should not have been able to open this file.\n\n", __LINE__
) ;
92 if ((error_num
= sf_error (NULL
)) <= 1 || error_num
> 300)
93 { printf ("\n\nLine %d : Should not have had an error number of %d.\n\n", __LINE__
, error_num
) ;
100 } /* error_value_test */
103 no_file_test (const char * filename
)
107 print_test_name (__func__
, filename
) ;
111 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
112 sndfile
= sf_open (filename
, SFM_READ
, &sfinfo
) ;
114 exit_if_true (sndfile
!= NULL
, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__
) ;
121 zero_length_test (const char *filename
)
126 print_test_name (__func__
, filename
) ;
128 /* Create a zero length file. */
129 file
= fopen (filename
, "w") ;
130 exit_if_true (file
== NULL
, "\n\nLine %d : fopen ('%s') failed.\n", __LINE__
, filename
) ;
133 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
134 sndfile
= sf_open (filename
, SFM_READ
, &sfinfo
) ;
136 exit_if_true (sndfile
!= NULL
, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__
) ;
138 exit_if_true (0 && sf_error (NULL
) != SF_ERR_UNRECOGNISED_FORMAT
,
139 "\n\nLine %3d : Error : %s\n should be : %s\n", __LINE__
,
140 sf_strerror (NULL
), sf_error_number (SF_ERR_UNRECOGNISED_FORMAT
)) ;
144 } /* zero_length_test */
147 bad_wav_test (const char * filename
)
152 const char data
[] = "RIFF WAVEfmt " ;
154 print_test_name (__func__
, filename
) ;
156 if ((file
= fopen (filename
, "w")) == NULL
)
157 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__
) ;
161 exit_if_true (fwrite (data
, sizeof (data
), 1, file
) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__
) ;
164 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
165 sndfile
= sf_open (filename
, SFM_READ
, &sfinfo
) ;
168 { printf ("\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__
) ;
177 error_close_test (void)
178 { static short buffer
[SHORT_BUFFER
] ;
179 const char *filename
= "error_close.wav" ;
184 print_test_name (__func__
, filename
) ;
186 /* Open a FILE* from which we will extract a file descriptor. */
187 if ((file
= fopen (filename
, "w")) == NULL
)
188 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__
) ;
192 /* Set parameters for writing the file. */
193 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
194 sfinfo
.channels
= 1 ;
195 sfinfo
.samplerate
= 44100 ;
196 sfinfo
.format
= SF_FORMAT_WAV
| SF_FORMAT_PCM_16
;
198 sndfile
= sf_open_fd (fileno (file
), SFM_WRITE
, &sfinfo
, SF_TRUE
) ;
200 { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__
, sf_strerror (NULL
)) ;
204 test_write_short_or_die (sndfile
, 0, buffer
, ARRAY_LEN (buffer
), __LINE__
) ;
206 /* Now close the fd associated with file before calling sf_close. */
209 if (sf_close (sndfile
) == 0)
212 OSVERSIONINFOEX osvi
;
214 memset (&osvi
, 0, sizeof (OSVERSIONINFOEX
)) ;
215 osvi
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFOEX
) ;
217 if (GetVersionEx ((OSVERSIONINFO
*) &osvi
))
218 { printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__
) ;
219 printf ("\nHowever, this is a known bug in version %d.%d of windows so we'll ignore it.\n\n",
220 (int) osvi
.dwMajorVersion
, (int) osvi
.dwMinorVersion
) ;
223 printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__
) ;
230 } /* error_close_test */
235 error_number_test () ;
236 error_value_test () ;
238 error_close_test () ;
240 no_file_test ("no_file.wav") ;
241 zero_length_test ("zero_length.wav") ;
242 bad_wav_test ("bad_wav.wav") ;