annotate src/libsndfile-1.0.25/tests/string_test.c @ 85:545efbb81310

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