annotate src/libsndfile-1.0.25/tests/string_test.c @ 0:c7265573341e

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