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