2 ** Copyright (C) 2002-2014 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #define BUFFER_LEN (1 << 10)
36 #define LOG_BUFFER_SIZE 1024
38 static void raw_offset_test (const char *filename, int typeminor) ;
39 static void bad_raw_test (void) ;
41 /* Force the start of this buffer to be double aligned. Sparc-solaris will
44 static short data [BUFFER_LEN] ;
49 raw_offset_test ("offset.raw", SF_FORMAT_PCM_16) ;
55 /*============================================================================================
56 ** Here are the test functions.
60 raw_offset_test (const char *filename, int typeminor)
66 print_test_name ("raw_offset_test", filename) ;
68 sfinfo.samplerate = 44100 ;
69 sfinfo.format = SF_FORMAT_RAW | typeminor ;
73 sndfile = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
76 sf_command (sndfile, SFC_FILE_TRUNCATE, &start, sizeof (start)) ;
78 for (k = 0 ; k < BUFFER_LEN ; k++)
80 test_write_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
84 sndfile = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
85 check_log_buffer_or_die (sndfile, __LINE__) ;
87 if (ABS (BUFFER_LEN - sfinfo.frames) > 1)
88 { printf ("\n\nLine %d : Incorrect sample count (%" PRId64 " should be %d)\n", __LINE__, sfinfo.frames, BUFFER_LEN) ;
89 dump_log_buffer (sndfile) ;
93 memset (data, 0 , sizeof (data)) ;
94 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
95 for (k = 0 ; k < BUFFER_LEN ; k++)
97 printf ("Error : line %d\n", __LINE__) ;
99 /* Set dataoffset to 2 bytes from beginning of file. */
101 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
103 /* Seek to new start */
104 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
106 memset (data, 0 , sizeof (data)) ;
107 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 1, __LINE__) ;
108 for (k = 0 ; k < BUFFER_LEN - 1 ; k++)
109 if (data [k] != k + 1)
110 { printf ("Error : line %d\n", __LINE__) ;
114 /* Set dataoffset to 4 bytes from beginning of file. */
116 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
118 /* Seek to new start */
119 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
121 memset (data, 0 , sizeof (data)) ;
122 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN - 2, __LINE__) ;
123 for (k = 0 ; k < BUFFER_LEN - 2 ; k++)
124 if (data [k] != k + 2)
125 { printf ("Error : line %d\n", __LINE__) ;
129 /* Set dataoffset back to 0 bytes from beginning of file. */
131 sf_command (sndfile, SFC_SET_RAW_START_OFFSET, &start, sizeof (start)) ;
133 /* Seek to new start */
134 test_seek_or_die (sndfile, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;
136 memset (data, 0 , sizeof (data)) ;
137 test_read_short_or_die (sndfile, 0, data, BUFFER_LEN, __LINE__) ;
138 for (k = 0 ; k < BUFFER_LEN ; k++)
140 { printf ("Error : line %d\n", __LINE__) ;
148 } /* raw_offset_test */
155 const char *errorstr, *filename = "bad.raw" ;
157 print_test_name ("bad_raw_test", filename) ;
159 if ((textfile = fopen (filename, "w")) == NULL)
160 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__) ;
164 fprintf (textfile, "This is not a valid file.\n") ;
167 sfinfo.samplerate = 44100 ;
168 sfinfo.format = SF_FORMAT_RAW | 0xABCD ;
169 sfinfo.channels = 1 ;
171 if ((file = sf_open (filename, SFM_READ, &sfinfo)) != NULL)
172 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__ - 1) ;
176 errorstr = sf_strerror (file) ;
178 if (strstr (errorstr, "Bad format field in SF_INFO struct") == NULL)
179 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ;