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