annotate src/libsndfile-1.0.27/tests/error_test.c @ 127:7867fa7e1b6b

Current fftw source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:40:26 +0100
parents cd6cdf86811e
children
rev   line source
cannam@125 1 /*
cannam@125 2 ** Copyright (C) 1999-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
cannam@125 3 **
cannam@125 4 ** This program is free software; you can redistribute it and/or modify
cannam@125 5 ** it under the terms of the GNU General Public License as published by
cannam@125 6 ** the Free Software Foundation; either version 2 of the License, or
cannam@125 7 ** (at your option) any later version.
cannam@125 8 **
cannam@125 9 ** This program is distributed in the hope that it will be useful,
cannam@125 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@125 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@125 12 ** GNU General Public License for more details.
cannam@125 13 **
cannam@125 14 ** You should have received a copy of the GNU General Public License
cannam@125 15 ** along with this program; if not, write to the Free Software
cannam@125 16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
cannam@125 17 */
cannam@125 18
cannam@125 19 #include "sfconfig.h"
cannam@125 20
cannam@125 21 #include <stdio.h>
cannam@125 22 #include <stdlib.h>
cannam@125 23 #include <string.h>
cannam@125 24
cannam@125 25 #if HAVE_UNISTD_H
cannam@125 26 #include <unistd.h>
cannam@125 27 #endif
cannam@125 28
cannam@125 29 #if OS_IS_WIN32
cannam@125 30 #include <windows.h>
cannam@125 31 #endif
cannam@125 32
cannam@125 33 #include <sndfile.h>
cannam@125 34
cannam@125 35 #include "utils.h"
cannam@125 36
cannam@125 37 #define BUFFER_SIZE (1 << 15)
cannam@125 38 #define SHORT_BUFFER (256)
cannam@125 39
cannam@125 40 static void
cannam@125 41 error_number_test (void)
cannam@125 42 { const char *noerror, *errstr ;
cannam@125 43 int k ;
cannam@125 44
cannam@125 45 print_test_name ("error_number_test", "") ;
cannam@125 46
cannam@125 47 noerror = sf_error_number (0) ;
cannam@125 48
cannam@125 49 for (k = 1 ; k < 300 ; k++)
cannam@125 50 { errstr = sf_error_number (k) ;
cannam@125 51
cannam@125 52 /* Test for termination condition. */
cannam@125 53 if (errstr == noerror)
cannam@125 54 break ;
cannam@125 55
cannam@125 56 /* Test for error. */
cannam@125 57 if (strstr (errstr, "This is a bug in libsndfile."))
cannam@125 58 { printf ("\n\nError : error number %d : %s\n\n\n", k, errstr) ;
cannam@125 59 exit (1) ;
cannam@125 60 } ;
cannam@125 61 } ;
cannam@125 62
cannam@125 63
cannam@125 64 puts ("ok") ;
cannam@125 65 return ;
cannam@125 66 } /* error_number_test */
cannam@125 67
cannam@125 68 static void
cannam@125 69 error_value_test (void)
cannam@125 70 { static unsigned char aiff_data [0x1b0] =
cannam@125 71 { 'F' , 'O' , 'R' , 'M' ,
cannam@125 72 0x00, 0x00, 0x01, 0xA8, /* FORM length */
cannam@125 73
cannam@125 74 'A' , 'I' , 'F' , 'C' ,
cannam@125 75 } ;
cannam@125 76
cannam@125 77 const char *filename = "error.aiff" ;
cannam@125 78 SNDFILE *file ;
cannam@125 79 SF_INFO sfinfo ;
cannam@125 80 int error_num ;
cannam@125 81
cannam@125 82 print_test_name ("error_value_test", filename) ;
cannam@125 83
cannam@125 84 dump_data_to_file (filename, aiff_data, sizeof (aiff_data)) ;
cannam@125 85
cannam@125 86 file = sf_open (filename, SFM_READ, &sfinfo) ;
cannam@125 87 if (file != NULL)
cannam@125 88 { printf ("\n\nLine %d : Should not have been able to open this file.\n\n", __LINE__) ;
cannam@125 89 exit (1) ;
cannam@125 90 } ;
cannam@125 91
cannam@125 92 if ((error_num = sf_error (NULL)) <= 1 || error_num > 300)
cannam@125 93 { printf ("\n\nLine %d : Should not have had an error number of %d.\n\n", __LINE__, error_num) ;
cannam@125 94 exit (1) ;
cannam@125 95 } ;
cannam@125 96
cannam@125 97 remove (filename) ;
cannam@125 98 puts ("ok") ;
cannam@125 99 return ;
cannam@125 100 } /* error_value_test */
cannam@125 101
cannam@125 102 static void
cannam@125 103 no_file_test (const char * filename)
cannam@125 104 { SNDFILE *sndfile ;
cannam@125 105 SF_INFO sfinfo ;
cannam@125 106
cannam@125 107 print_test_name (__func__, filename) ;
cannam@125 108
cannam@125 109 unlink (filename) ;
cannam@125 110
cannam@125 111 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125 112 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
cannam@125 113
cannam@125 114 exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
cannam@125 115
cannam@125 116 unlink (filename) ;
cannam@125 117 puts ("ok") ;
cannam@125 118 } /* no_file_test */
cannam@125 119
cannam@125 120 static void
cannam@125 121 zero_length_test (const char *filename)
cannam@125 122 { SNDFILE *sndfile ;
cannam@125 123 SF_INFO sfinfo ;
cannam@125 124 FILE *file ;
cannam@125 125
cannam@125 126 print_test_name (__func__, filename) ;
cannam@125 127
cannam@125 128 /* Create a zero length file. */
cannam@125 129 file = fopen (filename, "w") ;
cannam@125 130 exit_if_true (file == NULL, "\n\nLine %d : fopen ('%s') failed.\n", __LINE__, filename) ;
cannam@125 131 fclose (file) ;
cannam@125 132
cannam@125 133 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125 134 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
cannam@125 135
cannam@125 136 exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
cannam@125 137
cannam@125 138 exit_if_true (0 && sf_error (NULL) != SF_ERR_UNRECOGNISED_FORMAT,
cannam@125 139 "\n\nLine %3d : Error : %s\n should be : %s\n", __LINE__,
cannam@125 140 sf_strerror (NULL), sf_error_number (SF_ERR_UNRECOGNISED_FORMAT)) ;
cannam@125 141
cannam@125 142 unlink (filename) ;
cannam@125 143 puts ("ok") ;
cannam@125 144 } /* zero_length_test */
cannam@125 145
cannam@125 146 static void
cannam@125 147 bad_wav_test (const char * filename)
cannam@125 148 { SNDFILE *sndfile ;
cannam@125 149 SF_INFO sfinfo ;
cannam@125 150
cannam@125 151 FILE *file ;
cannam@125 152 const char data [] = "RIFF WAVEfmt " ;
cannam@125 153
cannam@125 154 print_test_name (__func__, filename) ;
cannam@125 155
cannam@125 156 if ((file = fopen (filename, "w")) == NULL)
cannam@125 157 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
cannam@125 158 exit (1) ;
cannam@125 159 } ;
cannam@125 160
cannam@125 161 exit_if_true (fwrite (data, sizeof (data), 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ;
cannam@125 162 fclose (file) ;
cannam@125 163
cannam@125 164 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125 165 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
cannam@125 166
cannam@125 167 if (sndfile)
cannam@125 168 { printf ("\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
cannam@125 169 exit (1) ;
cannam@125 170 } ;
cannam@125 171
cannam@125 172 unlink (filename) ;
cannam@125 173 puts ("ok") ;
cannam@125 174 } /* bad_wav_test */
cannam@125 175
cannam@125 176 static void
cannam@125 177 error_close_test (void)
cannam@125 178 { static short buffer [SHORT_BUFFER] ;
cannam@125 179 const char *filename = "error_close.wav" ;
cannam@125 180 SNDFILE *sndfile ;
cannam@125 181 SF_INFO sfinfo ;
cannam@125 182 FILE *file ;
cannam@125 183
cannam@125 184 print_test_name (__func__, filename) ;
cannam@125 185
cannam@125 186 /* Open a FILE* from which we will extract a file descriptor. */
cannam@125 187 if ((file = fopen (filename, "w")) == NULL)
cannam@125 188 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
cannam@125 189 exit (1) ;
cannam@125 190 } ;
cannam@125 191
cannam@125 192 /* Set parameters for writing the file. */
cannam@125 193 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@125 194 sfinfo.channels = 1 ;
cannam@125 195 sfinfo.samplerate = 44100 ;
cannam@125 196 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
cannam@125 197
cannam@125 198 sndfile = sf_open_fd (fileno (file), SFM_WRITE, &sfinfo, SF_TRUE) ;
cannam@125 199 if (sndfile == NULL)
cannam@125 200 { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__, sf_strerror (NULL)) ;
cannam@125 201 exit (1) ;
cannam@125 202 } ;
cannam@125 203
cannam@125 204 test_write_short_or_die (sndfile, 0, buffer, ARRAY_LEN (buffer), __LINE__) ;
cannam@125 205
cannam@125 206 /* Now close the fd associated with file before calling sf_close. */
cannam@125 207 fclose (file) ;
cannam@125 208
cannam@125 209 if (sf_close (sndfile) == 0)
cannam@125 210 {
cannam@125 211 #if OS_IS_WIN32
cannam@125 212 OSVERSIONINFOEX osvi ;
cannam@125 213
cannam@125 214 memset (&osvi, 0, sizeof (OSVERSIONINFOEX)) ;
cannam@125 215 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX) ;
cannam@125 216
cannam@125 217 if (GetVersionEx ((OSVERSIONINFO *) &osvi))
cannam@125 218 { printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
cannam@125 219 printf ("\nHowever, this is a known bug in version %d.%d of windows so we'll ignore it.\n\n",
cannam@125 220 (int) osvi.dwMajorVersion, (int) osvi.dwMinorVersion) ;
cannam@125 221 } ;
cannam@125 222 #else
cannam@125 223 printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
cannam@125 224 exit (1) ;
cannam@125 225 #endif
cannam@125 226 } ;
cannam@125 227
cannam@125 228 unlink (filename) ;
cannam@125 229 puts ("ok") ;
cannam@125 230 } /* error_close_test */
cannam@125 231
cannam@125 232 int
cannam@125 233 main (void)
cannam@125 234 {
cannam@125 235 error_number_test () ;
cannam@125 236 error_value_test () ;
cannam@125 237
cannam@125 238 error_close_test () ;
cannam@125 239
cannam@125 240 no_file_test ("no_file.wav") ;
cannam@125 241 zero_length_test ("zero_length.wav") ;
cannam@125 242 bad_wav_test ("bad_wav.wav") ;
cannam@125 243
cannam@125 244 return 0 ;
cannam@125 245 } /* main */
cannam@125 246