Bugs fixed for stdin and stdout.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / tests / cpp_test.cc
1 /*
2 ** Copyright (C) 2006-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 <cstdio>
20 #include <cstdlib>
21 #include <cstring>
22
23 #include <sndfile.hh>
24
25 #include "utils.h"
26
27 static short sbuffer [100] ;
28 static int ibuffer [100] ;
29 static float fbuffer [100] ;
30 static double dbuffer [100] ;
31
32 static void
33 ceeplusplus_wchar_test (void)
34 {
35 #if 0
36 LPCWSTR filename = L"wchar_test.wav" ;
37
38 print_test_name (__func__, "ceeplusplus_wchar_test.wav") ;
39
40 /* Use this scope to make sure the created file is closed. */
41 {
42 SndfileHandle file (filename, SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 2, 44100) ;
43
44 if (file.refCount () != 1)
45 { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ;
46 exit (1) ;
47 } ;
48
49 /* This should check that the file did in fact get created with a
50 ** wchar_t * filename.
51 */
52 exit_if_true (
53 GetFileAttributesW (filename) == INVALID_FILE_ATTRIBUTES,
54 "\n\nLine %d : GetFileAttributes failed.\n\n", __LINE__
55 ) ;
56 }
57
58 /* Use this because the file was created with CreateFileW. */
59 DeleteFileW (filename) ;
60
61 puts ("ok") ;
62 #endif
63 } /* ceeplusplus_wchar_test */
64
65
66
67 static void
68 create_file (const char * filename, int format)
69 { SndfileHandle file ;
70
71 if (file.refCount () != 0)
72 { printf ("\n\n%s %d : Error : Reference count (%d) should be zero.\n\n", __func__, __LINE__, file.refCount ()) ;
73 exit (1) ;
74 } ;
75
76 file = SndfileHandle (filename, SFM_WRITE, format, 2, 48000) ;
77
78 if (file.refCount () != 1)
79 { printf ("\n\n%s %d : Error : Reference count (%d) should be 1.\n\n", __func__, __LINE__, file.refCount ()) ;
80 exit (1) ;
81 } ;
82
83 file.setString (SF_STR_TITLE, filename) ;
84
85 /* Item write. */
86 file.write (sbuffer, ARRAY_LEN (sbuffer)) ;
87 file.write (ibuffer, ARRAY_LEN (ibuffer)) ;
88 file.write (fbuffer, ARRAY_LEN (fbuffer)) ;
89 file.write (dbuffer, ARRAY_LEN (dbuffer)) ;
90
91 /* Frame write. */
92 file.writef (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
93 file.writef (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
94 file.writef (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
95 file.writef (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
96
97 /* RAII takes care of the SndfileHandle. */
98 } /* create_file */
99
100 static void
101 check_title (const SndfileHandle & file, const char * filename)
102 { const char *title = NULL ;
103
104 title = file.getString (SF_STR_TITLE) ;
105
106 if (title == NULL)
107 { printf ("\n\n%s %d : Error : No title.\n\n", __func__, __LINE__) ;
108 exit (1) ;
109 } ;
110
111 if (strcmp (filename, title) != 0)
112 { printf ("\n\n%s %d : Error : title '%s' should be '%s'\n\n", __func__, __LINE__, title, filename) ;
113 exit (1) ;
114 } ;
115
116 return ;
117 } /* check_title */
118
119 static void
120 read_file (const char * filename, int format)
121 { SndfileHandle file ;
122 sf_count_t count ;
123
124 if (file)
125 { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
126 exit (1) ;
127 } ;
128
129 file = SndfileHandle (filename) ;
130
131 if (1)
132 { SndfileHandle file2 = file ;
133
134 if (file.refCount () != 2 || file2.refCount () != 2)
135 { printf ("\n\n%s %d : Error : Reference count (%d) should be two.\n\n", __func__, __LINE__, file.refCount ()) ;
136 exit (1) ;
137 } ;
138 } ;
139
140 if (file.refCount () != 1)
141 { printf ("\n\n%s %d : Error : Reference count (%d) should be one.\n\n", __func__, __LINE__, file.refCount ()) ;
142 exit (1) ;
143 } ;
144
145 if (! file)
146 { printf ("\n\n%s %d : Error : should not be here.\n\n", __func__, __LINE__) ;
147 exit (1) ;
148 } ;
149
150 if (file.format () != format)
151 { printf ("\n\n%s %d : Error : format 0x%08x should be 0x%08x.\n\n", __func__, __LINE__, file.format (), format) ;
152 exit (1) ;
153 } ;
154
155 if (file.channels () != 2)
156 { printf ("\n\n%s %d : Error : channels %d should be 2.\n\n", __func__, __LINE__, file.channels ()) ;
157 exit (1) ;
158 } ;
159
160 if (file.frames () != ARRAY_LEN (sbuffer) * 4)
161 { printf ("\n\n%s %d : Error : frames %ld should be %lu.\n\n", __func__, __LINE__,
162 SF_COUNT_TO_LONG (file.frames ()), (long unsigned int) ARRAY_LEN (sbuffer) * 4 / 2) ;
163 exit (1) ;
164 } ;
165
166 switch (format & SF_FORMAT_TYPEMASK)
167 { case SF_FORMAT_AU :
168 break ;
169
170 default :
171 check_title (file, filename) ;
172 break ;
173 } ;
174
175 /* Item read. */
176 file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
177 file.read (ibuffer, ARRAY_LEN (ibuffer)) ;
178 file.read (fbuffer, ARRAY_LEN (fbuffer)) ;
179 file.read (dbuffer, ARRAY_LEN (dbuffer)) ;
180
181 /* Frame read. */
182 file.readf (sbuffer, ARRAY_LEN (sbuffer) / file.channels ()) ;
183 file.readf (ibuffer, ARRAY_LEN (ibuffer) / file.channels ()) ;
184 file.readf (fbuffer, ARRAY_LEN (fbuffer) / file.channels ()) ;
185 file.readf (dbuffer, ARRAY_LEN (dbuffer) / file.channels ()) ;
186
187 count = file.seek (file.frames () - 10, SEEK_SET) ;
188 if (count != file.frames () - 10)
189 { printf ("\n\n%s %d : Error : offset (%ld) should be %ld\n\n", __func__, __LINE__,
190 SF_COUNT_TO_LONG (count), SF_COUNT_TO_LONG (file.frames () - 10)) ;
191 exit (1) ;
192 } ;
193
194 count = file.read (sbuffer, ARRAY_LEN (sbuffer)) ;
195 if (count != 10 * file.channels ())
196 { printf ("\n\n%s %d : Error : count (%ld) should be %ld\n\n", __func__, __LINE__,
197 SF_COUNT_TO_LONG (count), SF_COUNT_TO_LONG (10 * file.channels ())) ;
198 exit (1) ;
199 } ;
200
201 /* RAII takes care of the SndfileHandle. */
202 } /* read_file */
203
204 static void
205 ceeplusplus_test (const char *filename, int format)
206 {
207 print_test_name ("ceeplusplus_test", filename) ;
208
209 create_file (filename, format) ;
210 read_file (filename, format) ;
211
212 remove (filename) ;
213 puts ("ok") ;
214 } /* ceeplusplus_test */
215
216 static void
217 ceeplusplus_extra_test (void)
218 { SndfileHandle file ;
219 const char * filename = "bad_file_name.wav" ;
220 int error ;
221
222 print_test_name ("ceeplusplus_extra_test", filename) ;
223
224 file = SndfileHandle (filename) ;
225
226 error = file.error () ;
227 if (error == 0)
228 { printf ("\n\n%s %d : error should be zero.\n\n", __func__, __LINE__) ;
229 exit (1) ;
230 } ;
231
232 if (file.strError () == NULL)
233 { printf ("\n\n%s %d : strError should not return NULL.\n\n", __func__, __LINE__) ;
234 exit (1) ;
235 } ;
236
237 if (file.seek (0, SEEK_SET) != 0)
238 { printf ("\n\n%s %d : bad seek ().\n\n", __func__, __LINE__) ;
239 exit (1) ;
240 } ;
241
242 puts ("ok") ;
243 } /* ceeplusplus_extra_test */
244
245
246 static void
247 ceeplusplus_rawhandle_test (const char *filename)
248 {
249 SNDFILE* handle ;
250 {
251 SndfileHandle file (filename) ;
252 handle = file.rawHandle () ;
253 sf_read_float (handle, fbuffer, ARRAY_LEN (fbuffer)) ;
254 }
255 } /* ceeplusplus_rawhandle_test */
256
257 static void
258 ceeplusplus_takeOwnership_test (const char *filename)
259 {
260 SNDFILE* handle ;
261 {
262 SndfileHandle file (filename) ;
263 handle = file.takeOwnership () ;
264 }
265
266 if (sf_read_float (handle, fbuffer, ARRAY_LEN (fbuffer)) <= 0)
267 { printf ("\n\n%s %d : error when taking ownership of handle.\n\n", __func__, __LINE__) ;
268 exit (1) ;
269 }
270
271 if (sf_close (handle) != 0)
272 { printf ("\n\n%s %d : cannot close file.\n\n", __func__, __LINE__) ;
273 exit (1) ;
274 }
275
276 SndfileHandle file (filename) ;
277 SndfileHandle file2 (file) ;
278
279 if (file2.takeOwnership ())
280 { printf ("\n\n%s %d : taking ownership of shared handle is not allowed.\n\n", __func__, __LINE__) ;
281 exit (1) ;
282 }
283 } /* ceeplusplus_takeOwnership_test */
284
285 static void
286 ceeplusplus_handle_test (const char *filename, int format)
287 {
288 print_test_name ("ceeplusplus_handle_test", filename) ;
289
290 create_file (filename, format) ;
291
292 if (0) ceeplusplus_rawhandle_test (filename) ;
293 ceeplusplus_takeOwnership_test (filename) ;
294
295 remove (filename) ;
296 puts ("ok") ;
297 } /* ceeplusplus_test */
298
299 int
300 main (void)
301 {
302 ceeplusplus_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
303 ceeplusplus_test ("cpp_test.aiff", SF_FORMAT_AIFF | SF_FORMAT_PCM_S8) ;
304 ceeplusplus_test ("cpp_test.au", SF_FORMAT_AU | SF_FORMAT_FLOAT) ;
305
306 ceeplusplus_extra_test () ;
307 ceeplusplus_handle_test ("cpp_test.wav", SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
308
309 ceeplusplus_wchar_test () ;
310
311 return 0 ;
312 } /* main */
313