Chris@0: /* Chris@0: ** Copyright (C) 2002-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: #include Chris@0: Chris@0: #if HAVE_UNISTD_H Chris@0: #include Chris@0: #endif Chris@0: Chris@0: #include Chris@0: Chris@0: #include "utils.h" Chris@0: Chris@0: #define BUFFER_LEN (1<<10) Chris@0: #define LOG_BUFFER_SIZE 1024 Chris@0: Chris@0: static void raw_offset_test (const char *filename, int typeminor) ; Chris@0: static void bad_raw_test (void) ; Chris@0: Chris@0: /* Force the start of this buffer to be double aligned. Sparc-solaris will Chris@0: ** choke if its not. Chris@0: */ Chris@0: static short data [BUFFER_LEN] ; Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { Chris@0: raw_offset_test ("offset.raw", SF_FORMAT_PCM_16) ; Chris@0: bad_raw_test () ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================================ Chris@0: ** Here are the test functions. Chris@0: */ Chris@0: Chris@0: static void Chris@0: raw_offset_test (const char *filename, int typeminor) Chris@0: { SNDFILE *sndfile ; Chris@0: SF_INFO sfinfo ; Chris@0: sf_count_t start ; Chris@0: int k ; Chris@0: Chris@0: print_test_name ("raw_offset_test", filename) ; Chris@0: Chris@0: sfinfo.samplerate = 44100 ; Chris@0: sfinfo.format = SF_FORMAT_RAW | typeminor ; Chris@0: sfinfo.channels = 1 ; Chris@0: sfinfo.frames = 0 ; Chris@0: Chris@0: sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ; Chris@0: Chris@0: start = 0 ; Chris@0: sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ; Chris@0: Chris@0: for (k = 0 ; k < BUFFER_LEN ; k++) Chris@0: data [k] = k ; Chris@0: test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; Chris@0: Chris@0: sf_close (sndfile) ; Chris@0: Chris@0: sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ; Chris@0: check_log_buffer_or_die (sndfile, __LINE__) ; Chris@0: Chris@0: if (abs (BUFFER_LEN - sfinfo.frames) > 1) Chris@0: { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames), BUFFER_LEN) ; Chris@0: dump_log_buffer (sndfile) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: memset (data, 0 , sizeof (data)) ; Chris@0: test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; Chris@0: for (k = 0 ; k < BUFFER_LEN ; k++) Chris@0: if (data [k] != k) Chris@0: printf ("Error : line %d\n", __LINE__) ; Chris@0: Chris@0: /* Set dataoffset to 2 bytes from beginning of file. */ Chris@0: start = 2 ; Chris@0: sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; Chris@0: Chris@0: /* Seek to new start */ Chris@0: test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; Chris@0: Chris@0: memset (data, 0 , sizeof (data)) ; Chris@0: test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ; Chris@0: for (k = 0 ; k < BUFFER_LEN - 1 ; k++) Chris@0: if (data [k] != k + 1) Chris@0: { printf ("Error : line %d\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Set dataoffset to 4 bytes from beginning of file. */ Chris@0: start = 4 ; Chris@0: sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; Chris@0: Chris@0: /* Seek to new start */ Chris@0: test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; Chris@0: Chris@0: memset (data, 0 , sizeof (data)) ; Chris@0: test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ; Chris@0: for (k = 0 ; k < BUFFER_LEN - 2 ; k++) Chris@0: if (data [k] != k + 2) Chris@0: { printf ("Error : line %d\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Set dataoffset back to 0 bytes from beginning of file. */ Chris@0: start = 0 ; Chris@0: sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ; Chris@0: Chris@0: /* Seek to new start */ Chris@0: test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ; Chris@0: Chris@0: memset (data, 0 , sizeof (data)) ; Chris@0: test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ; Chris@0: for (k = 0 ; k < BUFFER_LEN ; k++) Chris@0: if (data [k] != k) Chris@0: { printf ("Error : line %d\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sf_close (sndfile) ; Chris@0: unlink (filename) ; Chris@0: Chris@0: puts ("ok") ; Chris@0: } /* raw_offset_test */ Chris@0: Chris@0: static void Chris@0: bad_raw_test (void) Chris@0: { FILE *textfile ; Chris@0: SNDFILE *file ; Chris@0: SF_INFO sfinfo ; Chris@0: const char *errorstr, *filename = "bad.raw" ; Chris@0: Chris@0: print_test_name ("bad_raw_test", filename) ; Chris@0: Chris@0: if ((textfile = fopen (filename, "w")) == NULL) Chris@0: { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: fprintf (textfile, "This is not a valid file.\n") ; Chris@0: fclose (textfile) ; Chris@0: Chris@0: sfinfo.samplerate = 44100 ; Chris@0: sfinfo.format = SF_FORMAT_RAW | 0xABCD ; Chris@0: sfinfo.channels = 1 ; Chris@0: Chris@0: if ((file = sf_open (filename, SFM_READ, &sfinfo)) != NULL) Chris@0: { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: errorstr = sf_strerror (file) ; Chris@0: Chris@0: if (strstr (errorstr, "Bad format field in SF_INFO struct") == NULL) Chris@0: { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: unlink (filename) ; Chris@0: Chris@0: puts ("ok") ; Chris@0: } /* bad_raw_test */ Chris@0: