cannam@85: /* cannam@85: ** Copyright (C) 1999-2011 Erik de Castro Lopo cannam@85: ** cannam@85: ** This program is free software; you can redistribute it and/or modify cannam@85: ** it under the terms of the GNU General Public License as published by cannam@85: ** the Free Software Foundation; either version 2 of the License, or cannam@85: ** (at your option) any later version. cannam@85: ** cannam@85: ** This program is distributed in the hope that it will be useful, cannam@85: ** but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@85: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@85: ** GNU General Public License for more details. cannam@85: ** cannam@85: ** You should have received a copy of the GNU General Public License cannam@85: ** along with this program; if not, write to the Free Software cannam@85: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@85: */ cannam@85: cannam@85: #include "sfconfig.h" cannam@85: cannam@85: #include cannam@85: #include cannam@85: #include cannam@85: cannam@85: #if HAVE_UNISTD_H cannam@85: #include cannam@85: #endif cannam@85: cannam@85: #if OS_IS_WIN32 cannam@85: #include cannam@85: #endif cannam@85: cannam@85: #include cannam@85: cannam@85: #include "utils.h" cannam@85: cannam@85: #define BUFFER_SIZE (1<<15) cannam@85: #define SHORT_BUFFER (256) cannam@85: cannam@85: static void cannam@85: error_number_test (void) cannam@85: { const char *noerror, *errstr ; cannam@85: int k ; cannam@85: cannam@85: print_test_name ("error_number_test", "") ; cannam@85: cannam@85: noerror = sf_error_number (0) ; cannam@85: cannam@85: for (k = 1 ; k < 300 ; k++) cannam@85: { errstr = sf_error_number (k) ; cannam@85: cannam@85: /* Test for termination condition. */ cannam@85: if (errstr == noerror) cannam@85: break ; cannam@85: cannam@85: /* Test for error. */ cannam@85: if (strstr (errstr, "This is a bug in libsndfile.")) cannam@85: { printf ("\n\nError : error number %d : %s\n\n\n", k, errstr) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: } ; cannam@85: cannam@85: cannam@85: puts ("ok") ; cannam@85: return ; cannam@85: } /* error_number_test */ cannam@85: cannam@85: static void cannam@85: error_value_test (void) cannam@85: { static unsigned char aiff_data [0x1b0] = cannam@85: { 'F' , 'O' , 'R' , 'M' , cannam@85: 0x00, 0x00, 0x01, 0xA8, /* FORM length */ cannam@85: cannam@85: 'A' , 'I' , 'F' , 'C' , cannam@85: } ; cannam@85: cannam@85: const char *filename = "error.aiff" ; cannam@85: SNDFILE *file ; cannam@85: SF_INFO sfinfo ; cannam@85: int error_num ; cannam@85: cannam@85: print_test_name ("error_value_test", filename) ; cannam@85: cannam@85: dump_data_to_file (filename, aiff_data, sizeof (aiff_data)) ; cannam@85: cannam@85: file = sf_open (filename, SFM_READ, &sfinfo) ; cannam@85: if (file != NULL) cannam@85: { printf ("\n\nLine %d : Should not have been able to open this file.\n\n", __LINE__) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: if ((error_num = sf_error (NULL)) <= 1 || error_num > 300) cannam@85: { printf ("\n\nLine %d : Should not have had an error number of %d.\n\n", __LINE__, error_num) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: remove (filename) ; cannam@85: puts ("ok") ; cannam@85: return ; cannam@85: } /* error_value_test */ cannam@85: cannam@85: static void cannam@85: no_file_test (const char * filename) cannam@85: { SNDFILE *sndfile ; cannam@85: SF_INFO sfinfo ; cannam@85: cannam@85: print_test_name (__func__, filename) ; cannam@85: cannam@85: unlink (filename) ; cannam@85: cannam@85: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@85: sndfile = sf_open (filename, SFM_READ, &sfinfo) ; cannam@85: cannam@85: exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ; cannam@85: cannam@85: unlink (filename) ; cannam@85: puts ("ok") ; cannam@85: } /* no_file_test */ cannam@85: cannam@85: static void cannam@85: zero_length_test (const char *filename) cannam@85: { SNDFILE *sndfile ; cannam@85: SF_INFO sfinfo ; cannam@85: FILE *file ; cannam@85: cannam@85: print_test_name (__func__, filename) ; cannam@85: cannam@85: /* Create a zero length file. */ cannam@85: file = fopen (filename, "w") ; cannam@85: exit_if_true (file == NULL, "\n\nLine %d : fopen ('%s') failed.\n", __LINE__, filename) ; cannam@85: fclose (file) ; cannam@85: cannam@85: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@85: sndfile = sf_open (filename, SFM_READ, &sfinfo) ; cannam@85: cannam@85: exit_if_true (sndfile != NULL, "\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ; cannam@85: cannam@85: exit_if_true (0 && sf_error (NULL) != SF_ERR_UNRECOGNISED_FORMAT, cannam@85: "\n\nLine %3d : Error : %s\n should be : %s\n", __LINE__, cannam@85: sf_strerror (NULL), sf_error_number (SF_ERR_UNRECOGNISED_FORMAT)) ; cannam@85: cannam@85: unlink (filename) ; cannam@85: puts ("ok") ; cannam@85: } /* zero_length_test */ cannam@85: cannam@85: static void cannam@85: bad_wav_test (const char * filename) cannam@85: { SNDFILE *sndfile ; cannam@85: SF_INFO sfinfo ; cannam@85: cannam@85: FILE *file ; cannam@85: const char data [] = "RIFF WAVEfmt " ; cannam@85: cannam@85: print_test_name (__func__, filename) ; cannam@85: cannam@85: if ((file = fopen (filename, "w")) == NULL) cannam@85: { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: exit_if_true (fwrite (data, sizeof (data), 1, file) != 1, "\n\nLine %d : fwrite failed.\n", __LINE__) ; cannam@85: fclose (file) ; cannam@85: cannam@85: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@85: sndfile = sf_open (filename, SFM_READ, &sfinfo) ; cannam@85: cannam@85: if (sndfile) cannam@85: { printf ("\n\nLine %d : should not have received a valid SNDFILE* pointer.\n", __LINE__) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: unlink (filename) ; cannam@85: puts ("ok") ; cannam@85: } /* bad_wav_test */ cannam@85: cannam@85: static void cannam@85: error_close_test (void) cannam@85: { static short buffer [SHORT_BUFFER] ; cannam@85: const char *filename = "error_close.wav" ; cannam@85: SNDFILE *sndfile ; cannam@85: SF_INFO sfinfo ; cannam@85: FILE *file ; cannam@85: cannam@85: print_test_name (__func__, filename) ; cannam@85: cannam@85: /* Open a FILE* from which we will extract a file descriptor. */ cannam@85: if ((file = fopen (filename, "w")) == NULL) cannam@85: { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: /* Set parameters for writing the file. */ cannam@85: memset (&sfinfo, 0, sizeof (sfinfo)) ; cannam@85: sfinfo.channels = 1 ; cannam@85: sfinfo.samplerate = 44100 ; cannam@85: sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ; cannam@85: cannam@85: sndfile = sf_open_fd (fileno (file), SFM_WRITE, &sfinfo, SF_TRUE) ; cannam@85: if (sndfile == NULL) cannam@85: { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__, sf_strerror (NULL)) ; cannam@85: exit (1) ; cannam@85: } ; cannam@85: cannam@85: test_write_short_or_die (sndfile, 0, buffer, ARRAY_LEN (buffer), __LINE__) ; cannam@85: cannam@85: /* Now close the fd associated with file before calling sf_close. */ cannam@85: fclose (file) ; cannam@85: cannam@85: if (sf_close (sndfile) == 0) cannam@85: { cannam@85: #if OS_IS_WIN32 cannam@85: OSVERSIONINFOEX osvi ; cannam@85: cannam@85: memset (&osvi, 0, sizeof (OSVERSIONINFOEX)) ; cannam@85: osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX) ; cannam@85: cannam@85: if (GetVersionEx ((OSVERSIONINFO *) &osvi)) cannam@85: { printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ; cannam@85: printf ("\nHowever, this is a known bug in version %d.%d of windows so we'll ignore it.\n\n", cannam@85: (int) osvi.dwMajorVersion, (int) osvi.dwMinorVersion) ; cannam@85: } ; cannam@85: #else cannam@85: printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ; cannam@85: exit (1) ; cannam@85: #endif cannam@85: } ; cannam@85: cannam@85: unlink (filename) ; cannam@85: puts ("ok") ; cannam@85: } /* error_close_test */ cannam@85: cannam@85: int cannam@85: main (void) cannam@85: { cannam@85: error_number_test () ; cannam@85: error_value_test () ; cannam@85: cannam@85: error_close_test () ; cannam@85: cannam@85: no_file_test ("no_file.wav") ; cannam@85: zero_length_test ("zero_length.wav") ; cannam@85: bad_wav_test ("bad_wav.wav") ; cannam@85: cannam@85: return 0 ; cannam@85: } /* main */ cannam@85: