Examples Makefile.
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / error_test.c
1 /*
2 ** Copyright (C) 1999-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
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #if OS_IS_WIN32
30 #include <windows.h>
31 #endif
32
33 #include <sndfile.h>
34
35 #include "utils.h"
36
37 #define BUFFER_SIZE (1<<15)
38 #define SHORT_BUFFER (256)
39
40 static void
41 error_number_test (void)
42 { const char *noerror, *errstr ;
43 int k ;
44
45 print_test_name ("error_number_test", "") ;
46
47 noerror = sf_error_number (0) ;
48
49 for (k = 1 ; k < 300 ; k++)
50 { errstr = sf_error_number (k) ;
51
52 /* Test for termination condition. */
53 if (errstr == noerror)
54 break ;
55
56 /* Test for error. */
57 if (strstr (errstr, "This is a bug in libsndfile."))
58 { printf ("\n\nError : error number %d : %s\n\n\n", k, errstr) ;
59 exit (1) ;
60 } ;
61 } ;
62
63
64 puts ("ok") ;
65 return ;
66 } /* error_number_test */
67
68 static void
69 error_value_test (void)
70 { static unsigned char aiff_data [0x1b0] =
71 { 'F' , 'O' , 'R' , 'M' ,
72 0x00, 0x00, 0x01, 0xA8, /* FORM length */
73
74 'A' , 'I' , 'F' , 'C' ,
75 } ;
76
77 const char *filename = "error.aiff" ;
78 SNDFILE *file ;
79 SF_INFO sfinfo ;
80 int error_num ;
81
82 print_test_name ("error_value_test", filename) ;
83
84 dump_data_to_file (filename, aiff_data, sizeof (aiff_data)) ;
85
86 file = sf_open (filename, SFM_READ, &sfinfo) ;
87 if (file != NULL)
88 { printf ("\n\nLine %d : Should not have been able to open this file.\n\n", __LINE__) ;
89 exit (1) ;
90 } ;
91
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) ;
94 exit (1) ;
95 } ;
96
97 remove (filename) ;
98 puts ("ok") ;
99 return ;
100 } /* error_value_test */
101
102 static void
103 no_file_test (const char * filename)
104 { SNDFILE *sndfile ;
105 SF_INFO sfinfo ;
106
107 print_test_name (__func__, filename) ;
108
109 unlink (filename) ;
110
111 memset (&sfinfo, 0, sizeof (sfinfo)) ;
112 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
113
114 exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
115
116 unlink (filename) ;
117 puts ("ok") ;
118 } /* no_file_test */
119
120 static void
121 zero_length_test (const char *filename)
122 { SNDFILE *sndfile ;
123 SF_INFO sfinfo ;
124 FILE *file ;
125
126 print_test_name (__func__, filename) ;
127
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) ;
131 fclose (file) ;
132
133 memset (&sfinfo, 0, sizeof (sfinfo)) ;
134 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
135
136 exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
137
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)) ;
141
142 unlink (filename) ;
143 puts ("ok") ;
144 } /* zero_length_test */
145
146 static void
147 bad_wav_test (const char * filename)
148 { SNDFILE *sndfile ;
149 SF_INFO sfinfo ;
150
151 FILE *file ;
152 const char data [] = "RIFF WAVEfmt " ;
153
154 print_test_name (__func__, filename) ;
155
156 if ((file = fopen (filename, "w")) == NULL)
157 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
158 exit (1) ;
159 } ;
160
161 exit_if_true (fwrite (data, sizeof (data), 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ;
162 fclose (file) ;
163
164 memset (&sfinfo, 0, sizeof (sfinfo)) ;
165 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
166
167 if (sndfile)
168 { printf ("\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
169 exit (1) ;
170 } ;
171
172 unlink (filename) ;
173 puts ("ok") ;
174 } /* bad_wav_test */
175
176 static void
177 error_close_test (void)
178 { static short buffer [SHORT_BUFFER] ;
179 const char *filename = "error_close.wav" ;
180 SNDFILE *sndfile ;
181 SF_INFO sfinfo ;
182 FILE *file ;
183
184 print_test_name (__func__, filename) ;
185
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__) ;
189 exit (1) ;
190 } ;
191
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 ;
197
198 sndfile = sf_open_fd (fileno (file), SFM_WRITE, &sfinfo, SF_TRUE) ;
199 if (sndfile == NULL)
200 { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__, sf_strerror (NULL)) ;
201 exit (1) ;
202 } ;
203
204 test_write_short_or_die (sndfile, 0, buffer, ARRAY_LEN (buffer), __LINE__) ;
205
206 /* Now close the fd associated with file before calling sf_close. */
207 fclose (file) ;
208
209 if (sf_close (sndfile) == 0)
210 {
211 #if OS_IS_WIN32
212 OSVERSIONINFOEX osvi ;
213
214 memset (&osvi, 0, sizeof (OSVERSIONINFOEX)) ;
215 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX) ;
216
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) ;
221 } ;
222 #else
223 printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
224 exit (1) ;
225 #endif
226 } ;
227
228 unlink (filename) ;
229 puts ("ok") ;
230 } /* error_close_test */
231
232 int
233 main (void)
234 {
235 error_number_test () ;
236 error_value_test () ;
237
238 error_close_test () ;
239
240 no_file_test ("no_file.wav") ;
241 zero_length_test ("zero_length.wav") ;
242 bad_wav_test ("bad_wav.wav") ;
243
244 return 0 ;
245 } /* main */
246