Stdin, stdout and stderr updated, tested.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / tests / string_test.c
1 /*
2 ** Copyright (C) 2003-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 #include <math.h>
25
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #include <sndfile.h>
31
32 #include "utils.h"
33
34 #define BUFFER_LEN (1 << 10)
35 #define LOG_BUFFER_SIZE 1024
36
37 static void string_start_test (const char *filename, int typemajor) ;
38 static void string_start_end_test (const char *filename, int typemajor) ;
39 static void string_multi_set_test (const char *filename, int typemajor) ;
40 static void string_rdwr_test (const char *filename, int typemajor) ;
41 static void string_short_rdwr_test (const char *filename, int typemajor) ;
42
43 static void software_string_test (const char *filename) ;
44
45 static int str_count (const char * haystack, const char * needle) ;
46
47 int
48 main (int argc, char *argv [])
49 { int do_all = 0 ;
50 int test_count = 0 ;
51
52 if (argc != 2)
53 { printf ("Usage : %s <test>\n", argv [0]) ;
54 printf (" Where <test> is one of the following:\n") ;
55 printf (" wav - test adding strings to WAV files\n") ;
56 printf (" aiff - test adding strings to AIFF files\n") ;
57 printf (" flac - test adding strings to FLAC files\n") ;
58 printf (" ogg - test adding strings to OGG files\n") ;
59 printf (" all - perform all tests\n") ;
60 exit (1) ;
61 } ;
62
63 do_all =! strcmp (argv [1], "all") ;
64
65 if (do_all || ! strcmp (argv [1], "wav"))
66 { string_start_end_test ("strings.wav", SF_FORMAT_WAV) ;
67 string_multi_set_test ("multi.wav", SF_FORMAT_WAV) ;
68 string_rdwr_test ("rdwr.wav", SF_FORMAT_WAV) ;
69 string_short_rdwr_test ("short_rdwr.wav", SF_FORMAT_WAV) ;
70
71 string_start_end_test ("strings.wavex", SF_FORMAT_WAVEX) ;
72 string_multi_set_test ("multi.wavex", SF_FORMAT_WAVEX) ;
73 string_rdwr_test ("rdwr.wavex", SF_FORMAT_WAVEX) ;
74 string_short_rdwr_test ("short_rdwr.wavex", SF_FORMAT_WAVEX) ;
75
76 string_start_end_test ("strings.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ;
77 string_multi_set_test ("multi.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ;
78 string_rdwr_test ("rdwr.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ;
79 string_short_rdwr_test ("short_rdwr.rifx", SF_ENDIAN_BIG | SF_FORMAT_WAV) ;
80
81 software_string_test ("software_string.wav") ;
82 test_count++ ;
83 } ;
84
85 if (do_all || ! strcmp (argv [1], "aiff"))
86 { string_start_end_test ("strings.aiff", SF_FORMAT_AIFF) ;
87 /*
88 string_multi_set_test ("multi.aiff", SF_FORMAT_AIFF) ;
89 string_rdwr_test ("rdwr.aiff", SF_FORMAT_AIFF) ;
90 string_short_rdwr_test ("short_rdwr.aiff", SF_FORMAT_AIFF) ;
91 */
92 test_count++ ;
93 } ;
94
95 if (do_all || ! strcmp (argv [1], "flac"))
96 { if (HAVE_EXTERNAL_LIBS)
97 string_start_test ("strings.flac", SF_FORMAT_FLAC) ;
98 else
99 puts (" No FLAC tests because FLAC support was not compiled in.") ;
100 test_count++ ;
101 } ;
102
103 if (do_all || ! strcmp (argv [1], "ogg"))
104 { if (HAVE_EXTERNAL_LIBS)
105 string_start_test ("vorbis.oga", SF_FORMAT_OGG) ;
106 else
107 puts (" No Ogg/Vorbis tests because Ogg/Vorbis support was not compiled in.") ;
108 test_count++ ;
109 } ;
110
111 if (do_all || ! strcmp (argv [1], "rf64"))
112 { puts ("\n\n **** String test not working yet for RF64 format. ****\n") ;
113 /*
114 string_start_end_test ("strings.rf64", SF_FORMAT_RF64) ;
115 string_multi_set_test ("multi.rf64", SF_FORMAT_RF64) ;
116 string_rdwr_test ("rdwr.rf64", SF_FORMAT_RF64) ;
117 string_short_rdwr_test ("short_rdwr.rf64", SF_FORMAT_RF64) ;
118 */
119 test_count++ ;
120 } ;
121
122 if (test_count == 0)
123 { printf ("Mono : ************************************\n") ;
124 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
125 printf ("Mono : ************************************\n") ;
126 return 1 ;
127 } ;
128
129 return 0 ;
130 } /* main */
131
132
133 /*============================================================================================
134 ** Here are the test functions.
135 */
136
137 static const char
138 software [] = "software (libsndfile-X.Y.Z)",
139 artist [] = "The Artist",
140 copyright [] = "Copyright (c) 2001 Artist",
141 comment [] = "Comment goes here!!!",
142 date [] = "2001/01/27",
143 album [] = "The Album",
144 license [] = "The license",
145 title [] = "This is the title",
146 long_title [] = "This is a very long and very boring title for this file",
147 long_artist [] = "The artist who kept on changing its name",
148 genre [] = "The genre",
149 trackno [] = "Track three" ;
150
151
152 static short data_out [BUFFER_LEN] ;
153
154 static void
155 string_start_end_test (const char *filename, int typemajor)
156 { const char *cptr ;
157 SNDFILE *file ;
158 SF_INFO sfinfo ;
159 int errors = 0 ;
160
161 typemajor = typemajor ;
162
163 print_test_name ("string_start_end_test", filename) ;
164
165 sfinfo.samplerate = 44100 ;
166 sfinfo.channels = 1 ;
167 sfinfo.frames = 0 ;
168 sfinfo.format = typemajor | SF_FORMAT_PCM_16 ;
169
170 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
171
172 /* Write stuff at start of file. */
173 sf_set_string (file, SF_STR_TITLE, filename) ;
174 sf_set_string (file, SF_STR_SOFTWARE, software) ;
175 sf_set_string (file, SF_STR_ARTIST, artist) ;
176 sf_set_string (file, SF_STR_GENRE, genre) ;
177 sf_set_string (file, SF_STR_TRACKNUMBER, trackno) ;
178
179 /* Write data to file. */
180 test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
181 test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
182
183 /* Write more stuff at end of file. */
184 sf_set_string (file, SF_STR_COPYRIGHT, copyright) ;
185 sf_set_string (file, SF_STR_COMMENT, comment) ;
186 sf_set_string (file, SF_STR_DATE, date) ;
187 sf_set_string (file, SF_STR_ALBUM, album) ;
188 sf_set_string (file, SF_STR_LICENSE, license) ;
189
190 sf_close (file) ;
191
192 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
193
194 check_log_buffer_or_die (file, __LINE__) ;
195
196 if (sfinfo.frames != BUFFER_LEN)
197 { printf ("***** Bad frame count %d (should be %d)\n\n", (int) sfinfo.frames, BUFFER_LEN) ;
198 errors ++ ;
199 } ;
200
201 cptr = sf_get_string (file, SF_STR_TITLE) ;
202 if (cptr == NULL || strcmp (filename, cptr) != 0)
203 { if (errors++ == 0)
204 puts ("\n") ;
205 printf (" Bad filename : %s\n", cptr) ;
206 } ;
207
208 cptr = sf_get_string (file, SF_STR_COPYRIGHT) ;
209 if (cptr == NULL || strcmp (copyright, cptr) != 0)
210 { if (errors++ == 0)
211 puts ("\n") ;
212 printf (" Bad copyright : %s\n", cptr) ;
213 } ;
214
215 cptr = sf_get_string (file, SF_STR_SOFTWARE) ;
216 if (cptr == NULL || strstr (cptr, software) != cptr)
217 { if (errors++ == 0)
218 puts ("\n") ;
219 printf (" Bad software : %s\n", cptr) ;
220 } ;
221
222 if (str_count (cptr, "libsndfile") != 1)
223 { if (errors++ == 0)
224 puts ("\n") ;
225 printf (" Bad software : %s\n", cptr) ;
226 } ;
227
228 cptr = sf_get_string (file, SF_STR_ARTIST) ;
229 if (cptr == NULL || strcmp (artist, cptr) != 0)
230 { if (errors++ == 0)
231 puts ("\n") ;
232 printf (" Bad artist : %s\n", cptr) ;
233 } ;
234
235 cptr = sf_get_string (file, SF_STR_COMMENT) ;
236 if (cptr == NULL || strcmp (comment, cptr) != 0)
237 { if (errors++ == 0)
238 puts ("\n") ;
239 printf (" Bad comment : %s\n", cptr) ;
240 } ;
241
242 if (typemajor != SF_FORMAT_AIFF)
243 { cptr = sf_get_string (file, SF_STR_DATE) ;
244 if (cptr == NULL || strcmp (date, cptr) != 0)
245 { if (errors++ == 0)
246 puts ("\n") ;
247 printf (" Bad date : %s\n", cptr) ;
248 } ;
249
250 cptr = sf_get_string (file, SF_STR_GENRE) ;
251 if (cptr == NULL || strcmp (genre, cptr) != 0)
252 { if (errors++ == 0)
253 puts ("\n") ;
254 printf (" Bad genre : %s\n", cptr) ;
255 } ;
256 } ;
257
258 switch (typemajor)
259 { case SF_FORMAT_AIFF :
260 case SF_FORMAT_WAV :
261 case SF_FORMAT_WAVEX :
262 case SF_ENDIAN_BIG | SF_FORMAT_WAV :
263 break ;
264
265 default :
266 cptr = sf_get_string (file, SF_STR_ALBUM) ;
267 if (cptr == NULL || strcmp (album, cptr) != 0)
268 { if (errors++ == 0)
269 puts ("\n") ;
270 printf (" Bad album : %s\n", cptr) ;
271 } ;
272
273 cptr = sf_get_string (file, SF_STR_LICENSE) ;
274 if (cptr == NULL || strcmp (license, cptr) != 0)
275 { if (errors++ == 0)
276 puts ("\n") ;
277 printf (" Bad license : %s\n", cptr) ;
278 } ;
279
280 cptr = sf_get_string (file, SF_STR_TRACKNUMBER) ;
281 if (cptr == NULL || strcmp (genre, cptr) != 0)
282 { if (errors++ == 0)
283 puts ("\n") ;
284 printf (" Bad track no. : %s\n", cptr) ;
285 } ;
286 break ;
287 } ;
288
289 if (errors > 0)
290 { printf ("\n*** Error count : %d ***\n\n", errors) ;
291 dump_log_buffer (file) ;
292 exit (1) ;
293 } ;
294
295 sf_close (file) ;
296 unlink (filename) ;
297
298 puts ("ok") ;
299 } /* string_start_end_test */
300
301 static void
302 string_start_test (const char *filename, int typemajor)
303 { const char *cptr ;
304 SNDFILE *file ;
305 SF_INFO sfinfo ;
306 int errors = 0 ;
307
308 print_test_name ("string_start_test", filename) ;
309
310 sfinfo.samplerate = 44100 ;
311 sfinfo.channels = 1 ;
312 sfinfo.frames = 0 ;
313
314 switch (typemajor)
315 { case SF_FORMAT_OGG :
316 sfinfo.format = typemajor | SF_FORMAT_VORBIS ;
317 break ;
318
319 default :
320 sfinfo.format = typemajor | SF_FORMAT_PCM_16 ;
321 break ;
322 } ;
323
324 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
325
326 /* Write stuff at start of file. */
327 sf_set_string (file, SF_STR_TITLE, filename) ;
328 sf_set_string (file, SF_STR_SOFTWARE, software) ;
329 sf_set_string (file, SF_STR_ARTIST, artist) ;
330 sf_set_string (file, SF_STR_COPYRIGHT, copyright) ;
331 sf_set_string (file, SF_STR_COMMENT, comment) ;
332 sf_set_string (file, SF_STR_DATE, date) ;
333 sf_set_string (file, SF_STR_ALBUM, album) ;
334 sf_set_string (file, SF_STR_LICENSE, license) ;
335
336 /* Write data to file. */
337 test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
338
339 sf_close (file) ;
340
341 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
342
343 check_log_buffer_or_die (file, __LINE__) ;
344
345 if (sfinfo.frames != BUFFER_LEN)
346 { printf ("***** Bad frame count %d (should be %d)\n\n", (int) sfinfo.frames, BUFFER_LEN) ;
347 errors ++ ;
348 } ;
349
350 cptr = sf_get_string (file, SF_STR_TITLE) ;
351 if (cptr == NULL || strcmp (filename, cptr) != 0)
352 { if (errors++ == 0)
353 puts ("\n") ;
354 printf (" Bad filename : %s\n", cptr) ;
355 } ;
356
357 cptr = sf_get_string (file, SF_STR_COPYRIGHT) ;
358 if (cptr == NULL || strcmp (copyright, cptr) != 0)
359 { if (errors++ == 0)
360 puts ("\n") ;
361 printf (" Bad copyright : %s\n", cptr) ;
362 } ;
363
364 cptr = sf_get_string (file, SF_STR_SOFTWARE) ;
365 if (cptr == NULL || strstr (cptr, software) != cptr)
366 { if (errors++ == 0)
367 puts ("\n") ;
368 printf (" Bad software : %s\n", cptr) ;
369 } ;
370
371 if (str_count (cptr, "libsndfile") != 1)
372 { if (errors++ == 0)
373 puts ("\n") ;
374 printf (" Bad software : %s\n", cptr) ;
375 } ;
376
377 cptr = sf_get_string (file, SF_STR_ARTIST) ;
378 if (cptr == NULL || strcmp (artist, cptr) != 0)
379 { if (errors++ == 0)
380 puts ("\n") ;
381 printf (" Bad artist : %s\n", cptr) ;
382 } ;
383
384 cptr = sf_get_string (file, SF_STR_COMMENT) ;
385 if (cptr == NULL || strcmp (comment, cptr) != 0)
386 { if (errors++ == 0)
387 puts ("\n") ;
388 printf (" Bad comment : %s\n", cptr) ;
389 } ;
390
391 if (typemajor != SF_FORMAT_AIFF)
392 { cptr = sf_get_string (file, SF_STR_DATE) ;
393 if (cptr == NULL || strcmp (date, cptr) != 0)
394 { if (errors++ == 0)
395 puts ("\n") ;
396 printf (" Bad date : %s\n", cptr) ;
397 } ;
398 } ;
399
400 if (typemajor != SF_FORMAT_WAV && typemajor != SF_FORMAT_AIFF)
401 { cptr = sf_get_string (file, SF_STR_ALBUM) ;
402 if (cptr == NULL || strcmp (album, cptr) != 0)
403 { if (errors++ == 0)
404 puts ("\n") ;
405 printf (" Bad album : %s\n", cptr) ;
406 } ;
407 } ;
408
409 if (typemajor != SF_FORMAT_WAV && typemajor != SF_FORMAT_AIFF)
410 { cptr = sf_get_string (file, SF_STR_LICENSE) ;
411 if (cptr == NULL || strcmp (license, cptr) != 0)
412 { if (errors++ == 0)
413 puts ("\n") ;
414 printf (" Bad license : %s\n", cptr) ;
415 } ;
416 } ;
417
418 if (errors > 0)
419 { printf ("\n*** Error count : %d ***\n\n", errors) ;
420 dump_log_buffer (file) ;
421 exit (1) ;
422 } ;
423
424 sf_close (file) ;
425 unlink (filename) ;
426
427 puts ("ok") ;
428 } /* string_start_test */
429
430 static void
431 string_multi_set_test (const char *filename, int typemajor)
432 { static const char
433 new_software [] = "new software (libsndfile-X.Y.Z)",
434 new_copyright [] = "Copyright (c) 2001 New Artist",
435 new_artist [] = "The New Artist",
436 new_title [] = "This is the new title" ;
437
438 static char buffer [2048] ;
439 SNDFILE *file ;
440 SF_INFO sfinfo ;
441 int count ;
442
443 print_test_name (__func__, filename) ;
444
445 sfinfo.format = typemajor | SF_FORMAT_PCM_16 ;
446 sfinfo.samplerate = 44100 ;
447 sfinfo.channels = 1 ;
448 sfinfo.frames = 0 ;
449
450 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
451
452 /* Write stuff at start of file. */
453 sf_set_string (file, SF_STR_TITLE, title) ;
454 sf_set_string (file, SF_STR_SOFTWARE, software) ;
455 sf_set_string (file, SF_STR_ARTIST, artist) ;
456
457 /* Write data to file. */
458 test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
459
460 /* Write it all again. */
461
462 sf_set_string (file, SF_STR_TITLE, new_title) ;
463 sf_set_string (file, SF_STR_SOFTWARE, new_software) ;
464 sf_set_string (file, SF_STR_ARTIST, new_artist) ;
465
466 sf_set_string (file, SF_STR_COPYRIGHT, copyright) ;
467 sf_set_string (file, SF_STR_COMMENT, comment) ;
468 sf_set_string (file, SF_STR_DATE, date) ;
469 sf_set_string (file, SF_STR_ALBUM, album) ;
470 sf_set_string (file, SF_STR_LICENSE, license) ;
471 sf_set_string (file, SF_STR_COPYRIGHT, new_copyright) ;
472 sf_set_string (file, SF_STR_COMMENT, comment) ;
473 sf_set_string (file, SF_STR_DATE, date) ;
474 sf_set_string (file, SF_STR_ALBUM, album) ;
475 sf_set_string (file, SF_STR_LICENSE, license) ;
476
477 sf_close (file) ;
478
479 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
480 sf_command (file, SFC_GET_LOG_INFO, buffer, sizeof (buffer)) ;
481 sf_close (file) ;
482
483 count = str_count (buffer, new_title) ;
484 exit_if_true (count < 1, "\n\nLine %d : Could not find new_title in :\n%s\n", __LINE__, buffer) ;
485 exit_if_true (count > 1, "\n\nLine %d : new_title appears %d times in :\n\n%s\n", __LINE__, count, buffer) ;
486
487 count = str_count (buffer, software) ;
488 exit_if_true (count < 1, "\n\nLine %d : Could not find new_software in :\n%s\n", __LINE__, buffer) ;
489 exit_if_true (count > 1, "\n\nLine %d : new_software appears %d times in :\n\n%s\n", __LINE__, count, buffer) ;
490
491 count = str_count (buffer, new_artist) ;
492 exit_if_true (count < 1, "\n\nLine %d : Could not find new_artist in :\n%s\n", __LINE__, buffer) ;
493 exit_if_true (count > 1, "\n\nLine %d : new_artist appears %d times in :\n\n%s\n", __LINE__, count, buffer) ;
494
495 count = str_count (buffer, new_copyright) ;
496 exit_if_true (count < 1, "\n\nLine %d : Could not find new_copyright in :\n%s\n", __LINE__, buffer) ;
497 exit_if_true (count > 1, "\n\nLine %d : new_copyright appears %d times in :\n\n%s\n", __LINE__, count, buffer) ;
498
499 unlink (filename) ;
500
501 puts ("ok") ;
502 } /* string_multi_set_test */
503
504 static void
505 string_rdwr_test (const char *filename, int typemajor)
506 { SNDFILE *file ;
507 SF_INFO sfinfo ;
508 sf_count_t frames ;
509 const char * str ;
510
511 print_test_name (__func__, filename) ;
512 create_short_sndfile (filename, typemajor | SF_FORMAT_PCM_16, 2) ;
513
514 memset (&sfinfo, 0, sizeof (sfinfo)) ;
515 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
516 frames = sfinfo.frames ;
517 sf_set_string (file, SF_STR_TITLE, title) ;
518 sf_close (file) ;
519
520 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
521 exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %lld should be %lld.\n", __LINE__, sfinfo.frames, frames) ;
522 str = sf_get_string (file, SF_STR_TITLE) ;
523 exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
524 exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;
525 sf_close (file) ;
526
527 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
528 frames = sfinfo.frames ;
529 sf_set_string (file, SF_STR_TITLE, title) ;
530 sf_close (file) ;
531
532 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
533 str = sf_get_string (file, SF_STR_TITLE) ;
534 exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
535 sf_set_string (file, SF_STR_ARTIST, artist) ;
536 sf_close (file) ;
537
538 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
539
540 str = sf_get_string (file, SF_STR_ARTIST) ;
541 exit_if_true (str == NULL, "\n\nLine %d : SF_STR_ARTIST string is NULL.\n", __LINE__) ;
542 exit_if_true (strcmp (str, artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ;
543
544 str = sf_get_string (file, SF_STR_TITLE) ;
545 exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
546 exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;
547
548 exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %lld should be %lld.\n", __LINE__, sfinfo.frames, frames) ;
549
550 sf_close (file) ;
551 unlink (filename) ;
552
553 puts ("ok") ;
554 } /* string_rdwr_test */
555
556 static void
557 string_short_rdwr_test (const char *filename, int typemajor)
558 { SNDFILE *file ;
559 SF_INFO sfinfo ;
560 sf_count_t frames = BUFFER_LEN ;
561 const char * str ;
562
563 print_test_name (__func__, filename) ;
564
565 memset (&sfinfo, 0, sizeof (sfinfo)) ;
566 sfinfo.format = typemajor | SF_FORMAT_PCM_16 ;
567 sfinfo.samplerate = 44100 ;
568 sfinfo.channels = 1 ;
569 sfinfo.frames = 0 ;
570
571 file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;
572
573 /* Write data to file. */
574 test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
575
576 sf_set_string (file, SF_STR_TITLE, long_title) ;
577 sf_set_string (file, SF_STR_ARTIST, long_artist) ;
578 sf_close (file) ;
579
580 /* Open the file RDWR. */
581 file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
582 exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %lld should be %lld.\n", __LINE__, sfinfo.frames, frames) ;
583 str = sf_get_string (file, SF_STR_TITLE) ;
584 exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
585 exit_if_true (strcmp (str, long_title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;
586 str = sf_get_string (file, SF_STR_ARTIST) ;
587 exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
588 exit_if_true (strcmp (str, long_artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ;
589
590 /* Change title and artist. */
591 sf_set_string (file, SF_STR_TITLE, title) ;
592 sf_set_string (file, SF_STR_ARTIST, artist) ;
593
594 sf_close (file) ;
595
596 file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
597
598 check_log_buffer_or_die (file, __LINE__) ;
599
600 str = sf_get_string (file, SF_STR_TITLE) ;
601 exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
602 exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;
603
604 str = sf_get_string (file, SF_STR_ARTIST) ;
605 exit_if_true (str == NULL, "\n\nLine %d : SF_STR_ARTIST string is NULL.\n", __LINE__) ;
606 exit_if_true (strcmp (str, artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ;
607
608 sf_close (file) ;
609 unlink (filename) ;
610
611 puts ("ok") ;
612 } /* string_short_rdwr_test */
613
614 static int
615 str_count (const char * haystack, const char * needle)
616 { int count = 0 ;
617
618 while ((haystack = strstr (haystack, needle)) != NULL)
619 { count ++ ;
620 haystack ++ ;
621 } ;
622
623 return count ;
624 } /* str_count */
625
626 #define MIN(a,b) ((a) < (b) ? (a) : (b))
627
628
629 static void
630 software_string_test (const char *filename)
631 { size_t k ;
632
633 print_test_name (__func__, filename) ;
634
635 for (k = 0 ; k < 50 ; k++)
636 { const char *result ;
637 char sfname [64] ;
638 SNDFILE *file ;
639 SF_INFO info ;
640
641 sf_info_setup (&info, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 44100, 1) ;
642 file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_TRUE, __LINE__) ;
643
644 snprintf (sfname, MIN (k, sizeof (sfname)), "%s", "abcdefghijklmnopqrestvwxyz0123456789abcdefghijklmnopqrestvwxyz") ;
645
646 exit_if_true (sf_set_string (file, SF_STR_SOFTWARE, sfname),
647 "\n\nLine %d : sf_set_string (f, SF_STR_SOFTWARE, '%s') failed : %s\n", __LINE__, sfname, sf_strerror (file)) ;
648
649 sf_close (file) ;
650
651 file = test_open_file_or_die (filename, SFM_READ, &info, SF_TRUE, __LINE__) ;
652 result = sf_get_string (file, SF_STR_SOFTWARE) ;
653
654 exit_if_true (result == NULL, "\n\nLine %d : sf_get_string (file, SF_STR_SOFTWARE) returned NULL.\n\n", __LINE__) ;
655
656 exit_if_true (strstr (result, sfname) != result,
657 "\n\nLine %d : String '%s''%s' -> '%s'\n\n", sfname, result) ;
658 sf_close (file) ;
659 } ;
660
661 unlink (filename) ;
662 puts ("ok") ;
663 } /* new_test_test */