annotate src/libsndfile-1.0.25/tests/error_test.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 545efbb81310
children
rev   line source
cannam@85 1 /*
cannam@85 2 ** Copyright (C) 1999-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
cannam@85 25 #if HAVE_UNISTD_H
cannam@85 26 #include <unistd.h>
cannam@85 27 #endif
cannam@85 28
cannam@85 29 #if OS_IS_WIN32
cannam@85 30 #include <windows.h>
cannam@85 31 #endif
cannam@85 32
cannam@85 33 #include <sndfile.h>
cannam@85 34
cannam@85 35 #include "utils.h"
cannam@85 36
cannam@85 37 #define BUFFER_SIZE (1<<15)
cannam@85 38 #define SHORT_BUFFER (256)
cannam@85 39
cannam@85 40 static void
cannam@85 41 error_number_test (void)
cannam@85 42 { const char *noerror, *errstr ;
cannam@85 43 int k ;
cannam@85 44
cannam@85 45 print_test_name ("error_number_test", "") ;
cannam@85 46
cannam@85 47 noerror = sf_error_number (0) ;
cannam@85 48
cannam@85 49 for (k = 1 ; k < 300 ; k++)
cannam@85 50 { errstr = sf_error_number (k) ;
cannam@85 51
cannam@85 52 /* Test for termination condition. */
cannam@85 53 if (errstr == noerror)
cannam@85 54 break ;
cannam@85 55
cannam@85 56 /* Test for error. */
cannam@85 57 if (strstr (errstr, "This is a bug in libsndfile."))
cannam@85 58 { printf ("\n\nError : error number %d : %s\n\n\n", k, errstr) ;
cannam@85 59 exit (1) ;
cannam@85 60 } ;
cannam@85 61 } ;
cannam@85 62
cannam@85 63
cannam@85 64 puts ("ok") ;
cannam@85 65 return ;
cannam@85 66 } /* error_number_test */
cannam@85 67
cannam@85 68 static void
cannam@85 69 error_value_test (void)
cannam@85 70 { static unsigned char aiff_data [0x1b0] =
cannam@85 71 { 'F' , 'O' , 'R' , 'M' ,
cannam@85 72 0x00, 0x00, 0x01, 0xA8, /* FORM length */
cannam@85 73
cannam@85 74 'A' , 'I' , 'F' , 'C' ,
cannam@85 75 } ;
cannam@85 76
cannam@85 77 const char *filename = "error.aiff" ;
cannam@85 78 SNDFILE *file ;
cannam@85 79 SF_INFO sfinfo ;
cannam@85 80 int error_num ;
cannam@85 81
cannam@85 82 print_test_name ("error_value_test", filename) ;
cannam@85 83
cannam@85 84 dump_data_to_file (filename, aiff_data, sizeof (aiff_data)) ;
cannam@85 85
cannam@85 86 file = sf_open (filename, SFM_READ, &sfinfo) ;
cannam@85 87 if (file != NULL)
cannam@85 88 { printf ("\n\nLine %d : Should not have been able to open this file.\n\n", __LINE__) ;
cannam@85 89 exit (1) ;
cannam@85 90 } ;
cannam@85 91
cannam@85 92 if ((error_num = sf_error (NULL)) <= 1 || error_num > 300)
cannam@85 93 { printf ("\n\nLine %d : Should not have had an error number of %d.\n\n", __LINE__, error_num) ;
cannam@85 94 exit (1) ;
cannam@85 95 } ;
cannam@85 96
cannam@85 97 remove (filename) ;
cannam@85 98 puts ("ok") ;
cannam@85 99 return ;
cannam@85 100 } /* error_value_test */
cannam@85 101
cannam@85 102 static void
cannam@85 103 no_file_test (const char * filename)
cannam@85 104 { SNDFILE *sndfile ;
cannam@85 105 SF_INFO sfinfo ;
cannam@85 106
cannam@85 107 print_test_name (__func__, filename) ;
cannam@85 108
cannam@85 109 unlink (filename) ;
cannam@85 110
cannam@85 111 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 112 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
cannam@85 113
cannam@85 114 exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
cannam@85 115
cannam@85 116 unlink (filename) ;
cannam@85 117 puts ("ok") ;
cannam@85 118 } /* no_file_test */
cannam@85 119
cannam@85 120 static void
cannam@85 121 zero_length_test (const char *filename)
cannam@85 122 { SNDFILE *sndfile ;
cannam@85 123 SF_INFO sfinfo ;
cannam@85 124 FILE *file ;
cannam@85 125
cannam@85 126 print_test_name (__func__, filename) ;
cannam@85 127
cannam@85 128 /* Create a zero length file. */
cannam@85 129 file = fopen (filename, "w") ;
cannam@85 130 exit_if_true (file == NULL, "\n\nLine %d : fopen ('%s') failed.\n", __LINE__, filename) ;
cannam@85 131 fclose (file) ;
cannam@85 132
cannam@85 133 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 134 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
cannam@85 135
cannam@85 136 exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
cannam@85 137
cannam@85 138 exit_if_true (0 && sf_error (NULL) != SF_ERR_UNRECOGNISED_FORMAT,
cannam@85 139 "\n\nLine %3d : Error : %s\n should be : %s\n", __LINE__,
cannam@85 140 sf_strerror (NULL), sf_error_number (SF_ERR_UNRECOGNISED_FORMAT)) ;
cannam@85 141
cannam@85 142 unlink (filename) ;
cannam@85 143 puts ("ok") ;
cannam@85 144 } /* zero_length_test */
cannam@85 145
cannam@85 146 static void
cannam@85 147 bad_wav_test (const char * filename)
cannam@85 148 { SNDFILE *sndfile ;
cannam@85 149 SF_INFO sfinfo ;
cannam@85 150
cannam@85 151 FILE *file ;
cannam@85 152 const char data [] = "RIFF WAVEfmt " ;
cannam@85 153
cannam@85 154 print_test_name (__func__, filename) ;
cannam@85 155
cannam@85 156 if ((file = fopen (filename, "w")) == NULL)
cannam@85 157 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
cannam@85 158 exit (1) ;
cannam@85 159 } ;
cannam@85 160
cannam@85 161 exit_if_true (fwrite (data, sizeof (data), 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ;
cannam@85 162 fclose (file) ;
cannam@85 163
cannam@85 164 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 165 sndfile = sf_open (filename, SFM_READ, &sfinfo) ;
cannam@85 166
cannam@85 167 if (sndfile)
cannam@85 168 { printf ("\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ;
cannam@85 169 exit (1) ;
cannam@85 170 } ;
cannam@85 171
cannam@85 172 unlink (filename) ;
cannam@85 173 puts ("ok") ;
cannam@85 174 } /* bad_wav_test */
cannam@85 175
cannam@85 176 static void
cannam@85 177 error_close_test (void)
cannam@85 178 { static short buffer [SHORT_BUFFER] ;
cannam@85 179 const char *filename = "error_close.wav" ;
cannam@85 180 SNDFILE *sndfile ;
cannam@85 181 SF_INFO sfinfo ;
cannam@85 182 FILE *file ;
cannam@85 183
cannam@85 184 print_test_name (__func__, filename) ;
cannam@85 185
cannam@85 186 /* Open a FILE* from which we will extract a file descriptor. */
cannam@85 187 if ((file = fopen (filename, "w")) == NULL)
cannam@85 188 { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ;
cannam@85 189 exit (1) ;
cannam@85 190 } ;
cannam@85 191
cannam@85 192 /* Set parameters for writing the file. */
cannam@85 193 memset (&sfinfo, 0, sizeof (sfinfo)) ;
cannam@85 194 sfinfo.channels = 1 ;
cannam@85 195 sfinfo.samplerate = 44100 ;
cannam@85 196 sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
cannam@85 197
cannam@85 198 sndfile = sf_open_fd (fileno (file), SFM_WRITE, &sfinfo, SF_TRUE) ;
cannam@85 199 if (sndfile == NULL)
cannam@85 200 { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__, sf_strerror (NULL)) ;
cannam@85 201 exit (1) ;
cannam@85 202 } ;
cannam@85 203
cannam@85 204 test_write_short_or_die (sndfile, 0, buffer, ARRAY_LEN (buffer), __LINE__) ;
cannam@85 205
cannam@85 206 /* Now close the fd associated with file before calling sf_close. */
cannam@85 207 fclose (file) ;
cannam@85 208
cannam@85 209 if (sf_close (sndfile) == 0)
cannam@85 210 {
cannam@85 211 #if OS_IS_WIN32
cannam@85 212 OSVERSIONINFOEX osvi ;
cannam@85 213
cannam@85 214 memset (&osvi, 0, sizeof (OSVERSIONINFOEX)) ;
cannam@85 215 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX) ;
cannam@85 216
cannam@85 217 if (GetVersionEx ((OSVERSIONINFO *) &osvi))
cannam@85 218 { printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
cannam@85 219 printf ("\nHowever, this is a known bug in version %d.%d of windows so we'll ignore it.\n\n",
cannam@85 220 (int) osvi.dwMajorVersion, (int) osvi.dwMinorVersion) ;
cannam@85 221 } ;
cannam@85 222 #else
cannam@85 223 printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ;
cannam@85 224 exit (1) ;
cannam@85 225 #endif
cannam@85 226 } ;
cannam@85 227
cannam@85 228 unlink (filename) ;
cannam@85 229 puts ("ok") ;
cannam@85 230 } /* error_close_test */
cannam@85 231
cannam@85 232 int
cannam@85 233 main (void)
cannam@85 234 {
cannam@85 235 error_number_test () ;
cannam@85 236 error_value_test () ;
cannam@85 237
cannam@85 238 error_close_test () ;
cannam@85 239
cannam@85 240 no_file_test ("no_file.wav") ;
cannam@85 241 zero_length_test ("zero_length.wav") ;
cannam@85 242 bad_wav_test ("bad_wav.wav") ;
cannam@85 243
cannam@85 244 return 0 ;
cannam@85 245 } /* main */
cannam@85 246