2 ** Copyright (C) 2002-2011 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.
34 #define BUFFER_LEN (1<<10)
35 #define LOG_BUFFER_SIZE 1024
37 static void raw_offset_test (const char *filename
, int typeminor
) ;
38 static void bad_raw_test (void) ;
40 /* Force the start of this buffer to be double aligned. Sparc-solaris will
43 static short data
[BUFFER_LEN
] ;
48 raw_offset_test ("offset.raw", SF_FORMAT_PCM_16
) ;
54 /*============================================================================================
55 ** Here are the test functions.
59 raw_offset_test (const char *filename
, int typeminor
)
65 print_test_name ("raw_offset_test", filename
) ;
67 sfinfo
.samplerate
= 44100 ;
68 sfinfo
.format
= SF_FORMAT_RAW
| typeminor
;
72 sndfile
= test_open_file_or_die (filename
, SFM_RDWR
, &sfinfo
, SF_TRUE
, __LINE__
) ;
75 sf_command (sndfile
, SFC_FILE_TRUNCATE
, &start
, sizeof (start
)) ;
77 for (k
= 0 ; k
< BUFFER_LEN
; k
++)
79 test_write_short_or_die (sndfile
, 0, data
, BUFFER_LEN
, __LINE__
) ;
83 sndfile
= test_open_file_or_die (filename
, SFM_READ
, &sfinfo
, SF_TRUE
, __LINE__
) ;
84 check_log_buffer_or_die (sndfile
, __LINE__
) ;
86 if (abs (BUFFER_LEN
- sfinfo
.frames
) > 1)
87 { printf ("\n\nLine %d : Incorrect sample count (%ld should be %d)\n", __LINE__
, SF_COUNT_TO_LONG (sfinfo
.frames
), BUFFER_LEN
) ;
88 dump_log_buffer (sndfile
) ;
92 memset (data
, 0 , sizeof (data
)) ;
93 test_read_short_or_die (sndfile
, 0, data
, BUFFER_LEN
, __LINE__
) ;
94 for (k
= 0 ; k
< BUFFER_LEN
; k
++)
96 printf ("Error : line %d\n", __LINE__
) ;
98 /* Set dataoffset to 2 bytes from beginning of file. */
100 sf_command (sndfile
, SFC_SET_RAW_START_OFFSET
, &start
, sizeof (start
)) ;
102 /* Seek to new start */
103 test_seek_or_die (sndfile
, 0, SEEK_SET
, 0, sfinfo
.channels
, __LINE__
) ;
105 memset (data
, 0 , sizeof (data
)) ;
106 test_read_short_or_die (sndfile
, 0, data
, BUFFER_LEN
- 1, __LINE__
) ;
107 for (k
= 0 ; k
< BUFFER_LEN
- 1 ; k
++)
108 if (data
[k
] != k
+ 1)
109 { printf ("Error : line %d\n", __LINE__
) ;
113 /* Set dataoffset to 4 bytes from beginning of file. */
115 sf_command (sndfile
, SFC_SET_RAW_START_OFFSET
, &start
, sizeof (start
)) ;
117 /* Seek to new start */
118 test_seek_or_die (sndfile
, 0, SEEK_SET
, 0, sfinfo
.channels
, __LINE__
) ;
120 memset (data
, 0 , sizeof (data
)) ;
121 test_read_short_or_die (sndfile
, 0, data
, BUFFER_LEN
- 2, __LINE__
) ;
122 for (k
= 0 ; k
< BUFFER_LEN
- 2 ; k
++)
123 if (data
[k
] != k
+ 2)
124 { printf ("Error : line %d\n", __LINE__
) ;
128 /* Set dataoffset back to 0 bytes from beginning of file. */
130 sf_command (sndfile
, SFC_SET_RAW_START_OFFSET
, &start
, sizeof (start
)) ;
132 /* Seek to new start */
133 test_seek_or_die (sndfile
, 0, SEEK_SET
, 0, sfinfo
.channels
, __LINE__
) ;
135 memset (data
, 0 , sizeof (data
)) ;
136 test_read_short_or_die (sndfile
, 0, data
, BUFFER_LEN
, __LINE__
) ;
137 for (k
= 0 ; k
< BUFFER_LEN
; k
++)
139 { printf ("Error : line %d\n", __LINE__
) ;
147 } /* raw_offset_test */
154 const char *errorstr
, *filename
= "bad.raw" ;
156 print_test_name ("bad_raw_test", filename
) ;
158 if ((textfile
= fopen (filename
, "w")) == NULL
)
159 { printf ("\n\nLine %d : not able to open text file for write.\n", __LINE__
) ;
163 fprintf (textfile
, "This is not a valid file.\n") ;
166 sfinfo
.samplerate
= 44100 ;
167 sfinfo
.format
= SF_FORMAT_RAW
| 0xABCD ;
168 sfinfo
.channels
= 1 ;
170 if ((file
= sf_open (filename
, SFM_READ
, &sfinfo
)) != NULL
)
171 { printf ("\n\nLine %d : Error, file should not have opened.\n", __LINE__
- 1) ;
175 errorstr
= sf_strerror (file
) ;
177 if (strstr (errorstr
, "Bad format field in SF_INFO struct") == NULL
)
178 { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__
- 1, errorstr
) ;