annotate src/libsndfile-1.0.27/tests/error_test.c @ 83:ae30d91d2ffe

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