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