expand round-trip tests to include odd bits per sample (all 4<=bps<=24)
[platform/upstream/flac.git] / test / test_flac.sh
1 #!/bin/sh
2
3 #  FLAC - Free Lossless Audio Codec
4 #  Copyright (C) 2001,2002,2003,2004,2005,2006  Josh Coalson
5 #
6 #  This file is part the FLAC project.  FLAC is comprised of several
7 #  components distributed under difference licenses.  The codec libraries
8 #  are distributed under Xiph.Org's BSD-like license (see the file
9 #  COPYING.Xiph in this distribution).  All other programs, libraries, and
10 #  plugins are distributed under the GPL (see COPYING.GPL).  The documentation
11 #  is distributed under the Gnu FDL (see COPYING.FDL).  Each file in the
12 #  FLAC distribution contains at the top the terms under which it may be
13 #  distributed.
14 #
15 #  Since this particular file is relevant to all components of FLAC,
16 #  it may be distributed under the Xiph.Org license, which is the least
17 #  restrictive of those mentioned above.  See the file COPYING.Xiph in this
18 #  distribution.
19
20 die ()
21 {
22         echo $* 1>&2
23         exit 1
24 }
25
26 if [ x = x"$1" ] ; then
27         BUILD=debug
28 else
29         BUILD="$1"
30 fi
31
32 # change to 'false' to show flac output (useful for debugging)
33 if true ; then
34         SILENT='--silent'
35         TOTALLY_SILENT='--totally-silent'
36 else
37         SILENT=''
38         TOTALLY_SILENT=''
39 fi
40
41 LD_LIBRARY_PATH=`pwd`/../src/libFLAC/.libs:$LD_LIBRARY_PATH
42 LD_LIBRARY_PATH=`pwd`/../src/libOggFLAC/.libs:$LD_LIBRARY_PATH
43 LD_LIBRARY_PATH=`pwd`/../src/share/grabbag/.libs:$LD_LIBRARY_PATH
44 LD_LIBRARY_PATH=`pwd`/../src/share/getopt/.libs:$LD_LIBRARY_PATH
45 LD_LIBRARY_PATH=`pwd`/../src/share/replaygain_analysis/.libs:$LD_LIBRARY_PATH
46 LD_LIBRARY_PATH=`pwd`/../src/share/replaygain_synthesis/.libs:$LD_LIBRARY_PATH
47 LD_LIBRARY_PATH=`pwd`/../src/share/utf8/.libs:$LD_LIBRARY_PATH
48 LD_LIBRARY_PATH=`pwd`/../obj/$BUILD/lib:$LD_LIBRARY_PATH
49 export LD_LIBRARY_PATH
50 PATH=`pwd`/../src/flac:$PATH
51 PATH=`pwd`/../src/metaflac:$PATH
52 PATH=`pwd`/../src/test_streams:$PATH
53 PATH=`pwd`/../obj/$BUILD/bin:$PATH
54
55 flac --help 1>/dev/null 2>/dev/null || die "ERROR can't find flac executable"
56
57 run_flac ()
58 {
59         if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
60                 valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --logfile-fd=4 flac $* 4>>test_flac.valgrind.log
61         else
62                 flac $*
63         fi
64 }
65
66 run_metaflac ()
67 {
68         if [ x"$FLAC__TEST_WITH_VALGRIND" = xyes ] ; then
69                 valgrind --leak-check=yes --show-reachable=yes --num-callers=100 --logfile-fd=4 metaflac $* 4>>test_flac.valgrind.log
70         else
71                 metaflac $*
72         fi
73 }
74
75 md5cmp ()
76 {
77         #n=`( [ -f "$1" ] && [ -f "$2" ] && metaflac --show-md5sum --no-filename "$1" "$2" 2>/dev/null || die "ERROR: comparing FLAC files $1 and $2 by MD5 sum" ) | uniq | wc -l`
78         n=`( [ -f "$1" ] && [ -f "$2" ] && metaflac --show-md5sum --no-filename "$1" "$2" 2>/dev/null || exit 1 ) | uniq | wc -l`
79         [ "$n" != "" ] && [ $n = 1 ]
80 }
81
82 if [ `env | grep -ic '^comspec='` != 0 ] ; then
83         is_win=yes
84 else
85         is_win=no
86 fi
87
88 echo "Checking for --ogg support in flac..."
89 if flac --ogg $SILENT --force-raw-format --endian=little --sign=signed --channels=1 --bps=8 --sample-rate=44100 -c $0 1>/dev/null 2>&1 ; then
90         has_ogg=yes;
91         echo "flac --ogg works"
92 else
93         has_ogg=no;
94         echo "flac --ogg doesn't work"
95 fi
96
97 echo "Generating streams..."
98 if [ ! -f wacky1.wav ] ; then
99         test_streams || die "ERROR during test_streams"
100 fi
101
102 ############################################################################
103 # test that flac doesn't automatically overwrite files unless -f is used
104 ############################################################################
105
106 echo "Try encoding to a file that exists; should fail"
107 cp wacky1.wav exist.wav
108 touch exist.flac
109 if run_flac $TOTALLY_SILENT -0 exist.wav ; then
110         die "ERROR: it should have failed but didn't"
111 else
112         echo "OK, it failed as it should"
113 fi
114
115 echo "Try encoding with -f to a file that exists; should succeed"
116 if run_flac $TOTALLY_SILENT -0 --force exist.wav ; then
117         echo "OK, it succeeded as it should"
118 else
119         die "ERROR: it should have succeeded but didn't"
120 fi
121
122 echo "Try decoding to a file that exists; should fail"
123 if run_flac $TOTALLY_SILENT -d exist.flac ; then
124         die "ERROR: it should have failed but didn't"
125 else
126         echo "OK, it failed as it should"
127 fi
128
129 echo "Try decoding with -f to a file that exists; should succeed"
130 if run_flac $TOTALLY_SILENT -d -f exist.flac ; then
131         echo "OK, it succeeded as it should"
132 else
133         die "ERROR: it should have succeeded but didn't"
134 fi
135
136 rm -f exist.wav exist.flac
137
138 ############################################################################
139 # basic 'round-trip' tests of various kinds of streams
140 ############################################################################
141
142 rt_test_raw ()
143 {
144         f="$1"
145         channels=`echo $f | awk -F- '{print $2}'`
146         bps=`echo $f | awk -F- '{print $3}'`
147         echo -n "round-trip test ($f) encode... "
148         run_flac $SILENT --force --verify --force-raw-format --endian=little --sign=signed --sample-rate=44100 --bps=$bps --channels=$channels --lax -o rt.flac $f || die "ERROR"
149         echo -n "decode... "
150         run_flac $SILENT --force --decode --force-raw-format --endian=little --sign=signed -o rt.raw rt.flac || die "ERROR"
151         echo -n "compare... "
152         cmp $f rt.raw || die "ERROR: file mismatch"
153         echo "OK"
154         rm -f rt.flac rt.raw
155 }
156
157 rt_test_wav ()
158 {
159         f="$1"
160         echo -n "round-trip test ($f) encode... "
161         run_flac $SILENT --force --verify --channel-map=none --lax -o rt.flac $f || die "ERROR"
162         echo -n "decode... "
163         run_flac $SILENT --force --decode --channel-map=none -o rt.wav rt.flac || die "ERROR"
164         echo -n "compare... "
165         cmp $f rt.wav || die "ERROR: file mismatch"
166         echo "OK"
167         rm -f rt.flac rt.wav
168 }
169
170 rt_test_aiff ()
171 {
172         f="$1"
173         echo -n "round-trip test ($f) encode... "
174         run_flac $SILENT --force --verify --channel-map=none --lax -o rt.flac $f || die "ERROR"
175         echo -n "decode... "
176         run_flac $SILENT --force --decode --channel-map=none -o rt.aiff rt.flac || die "ERROR"
177         echo -n "compare... "
178         cmp $f rt.aiff || die "ERROR: file mismatch"
179         echo "OK"
180         rm -f rt.flac rt.aiff
181 }
182
183 # assumes input file is WAVE; does not check the metadata-preserving features of flac-to-flac; that is checked later
184 rt_test_flac ()
185 {
186         f="$1"
187         echo -n "round-trip test ($f->flac->flac->wav) encode... "
188         run_flac $SILENT --force --verify --channel-map=none --lax -o rt.flac $f || die "ERROR"
189         echo -n "re-encode... "
190         run_flac $SILENT --force --verify --lax -o rt2.flac rt.flac || die "ERROR"
191         echo -n "decode... "
192         run_flac $SILENT --force --decode --channel-map=none -o rt.wav rt2.flac || die "ERROR"
193         echo -n "compare... "
194         cmp $f rt.wav || die "ERROR: file mismatch"
195         echo "OK"
196         rm -f rt.wav rt.flac rt2.flac
197 }
198
199 for f in rt-*.raw ; do
200         rt_test_raw $f
201 done
202 for f in rt-*.wav ; do
203         rt_test_wav $f
204 done
205 for f in rt-*.aiff ; do
206         rt_test_aiff $f
207 done
208 for f in rt-*.wav ; do
209         rt_test_flac $f
210 done
211
212 ############################################################################
213 # test --skip and --until
214 ############################################################################
215
216 #
217 # first make some chopped-up raw files
218 #
219 echo "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMN" > master.raw
220 dddie="die ERROR: creating files with dd"
221 dd if=master.raw ibs=1 count=50 of=50c.raw 2>/dev/null || $dddie
222 dd if=master.raw ibs=1 skip=10 count=40 of=50c.skip10.raw 2>/dev/null || $dddie
223 dd if=master.raw ibs=1 skip=11 count=39 of=50c.skip11.raw 2>/dev/null || $dddie
224 dd if=master.raw ibs=1 skip=20 count=30 of=50c.skip20.raw 2>/dev/null || $dddie
225 dd if=master.raw ibs=1 skip=30 count=20 of=50c.skip30.raw 2>/dev/null || $dddie
226 dd if=master.raw ibs=1 skip=40 count=10 of=50c.skip40.raw 2>/dev/null || $dddie
227 dd if=master.raw ibs=1 count=10 of=50c.until10.raw 2>/dev/null || $dddie
228 dd if=master.raw ibs=1 count=20 of=50c.until20.raw 2>/dev/null || $dddie
229 dd if=master.raw ibs=1 count=30 of=50c.until30.raw 2>/dev/null || $dddie
230 dd if=master.raw ibs=1 count=39 of=50c.until39.raw 2>/dev/null || $dddie
231 dd if=master.raw ibs=1 count=40 of=50c.until40.raw 2>/dev/null || $dddie
232 dd if=master.raw ibs=1 skip=10 count=20 of=50c.skip10.until30.raw 2>/dev/null || $dddie
233 dd if=master.raw ibs=1 skip=10 count=29 of=50c.skip10.until39.raw 2>/dev/null || $dddie
234 dd if=master.raw ibs=1 skip=10 count=30 of=50c.skip10.until40.raw 2>/dev/null || $dddie
235 dd if=master.raw ibs=1 skip=20 count=10 of=50c.skip20.until30.raw 2>/dev/null || $dddie
236 dd if=master.raw ibs=1 skip=20 count=20 of=50c.skip20.until40.raw 2>/dev/null || $dddie
237
238 wav_eopt="$SILENT --force --verify --lax"
239 wav_dopt="$SILENT --force --decode"
240
241 raw_eopt="$wav_eopt --force-raw-format --endian=big --sign=signed --sample-rate=10 --bps=8 --channels=1"
242 raw_dopt="$wav_dopt --force-raw-format --endian=big --sign=signed"
243
244 #
245 # convert them to WAVE and AIFF files
246 #
247 convert_to_wav ()
248 {
249         run_flac "$2" $1.raw || die "ERROR converting $1.raw to WAVE"
250         run_flac "$3" $1.flac || die "ERROR converting $1.raw to WAVE"
251 }
252 convert_to_wav 50c "$raw_eopt" "$wav_dopt"
253 convert_to_wav 50c.skip10 "$raw_eopt" "$wav_dopt"
254 convert_to_wav 50c.skip11 "$raw_eopt" "$wav_dopt"
255 convert_to_wav 50c.skip20 "$raw_eopt" "$wav_dopt"
256 convert_to_wav 50c.skip30 "$raw_eopt" "$wav_dopt"
257 convert_to_wav 50c.skip40 "$raw_eopt" "$wav_dopt"
258 convert_to_wav 50c.until10 "$raw_eopt" "$wav_dopt"
259 convert_to_wav 50c.until20 "$raw_eopt" "$wav_dopt"
260 convert_to_wav 50c.until30 "$raw_eopt" "$wav_dopt"
261 convert_to_wav 50c.until39 "$raw_eopt" "$wav_dopt"
262 convert_to_wav 50c.until40 "$raw_eopt" "$wav_dopt"
263 convert_to_wav 50c.skip10.until30 "$raw_eopt" "$wav_dopt"
264 convert_to_wav 50c.skip10.until39 "$raw_eopt" "$wav_dopt"
265 convert_to_wav 50c.skip10.until40 "$raw_eopt" "$wav_dopt"
266 convert_to_wav 50c.skip20.until30 "$raw_eopt" "$wav_dopt"
267 convert_to_wav 50c.skip20.until40 "$raw_eopt" "$wav_dopt"
268
269 convert_to_aiff ()
270 {
271         run_flac "$2" $1.raw || die "ERROR converting $1.raw to AIFF"
272         run_flac "$3" $1.flac -o $1.aiff || die "ERROR converting $1.raw to AIFF"
273 }
274 convert_to_aiff 50c "$raw_eopt" "$wav_dopt"
275 convert_to_aiff 50c.skip10 "$raw_eopt" "$wav_dopt"
276 convert_to_aiff 50c.skip11 "$raw_eopt" "$wav_dopt"
277 convert_to_aiff 50c.skip20 "$raw_eopt" "$wav_dopt"
278 convert_to_aiff 50c.skip30 "$raw_eopt" "$wav_dopt"
279 convert_to_aiff 50c.skip40 "$raw_eopt" "$wav_dopt"
280 convert_to_aiff 50c.until10 "$raw_eopt" "$wav_dopt"
281 convert_to_aiff 50c.until20 "$raw_eopt" "$wav_dopt"
282 convert_to_aiff 50c.until30 "$raw_eopt" "$wav_dopt"
283 convert_to_aiff 50c.until39 "$raw_eopt" "$wav_dopt"
284 convert_to_aiff 50c.until40 "$raw_eopt" "$wav_dopt"
285 convert_to_aiff 50c.skip10.until30 "$raw_eopt" "$wav_dopt"
286 convert_to_aiff 50c.skip10.until39 "$raw_eopt" "$wav_dopt"
287 convert_to_aiff 50c.skip10.until40 "$raw_eopt" "$wav_dopt"
288 convert_to_aiff 50c.skip20.until30 "$raw_eopt" "$wav_dopt"
289 convert_to_aiff 50c.skip20.until40 "$raw_eopt" "$wav_dopt"
290
291 test_skip_until ()
292 {
293         in_fmt=$1
294         out_fmt=$2
295
296         [ "$in_fmt" = wav ] || [ "$in_fmt" = aiff ] || [ "$in_fmt" = raw ] || [ "$in_fmt" = flac ] || die "ERROR: internal error, bad 'in' format '$in_fmt'"
297
298         [ "$out_fmt" = flac ] || [ "$out_fmt" = ogg ] || die "ERROR: internal error, bad 'out' format '$out_fmt'"
299
300         if [ $in_fmt = raw ] ; then
301                 eopt="$raw_eopt"
302                 dopt="$raw_dopt"
303         else
304                 eopt="$wav_eopt"
305                 dopt="$wav_dopt"
306         fi
307
308         if [ $in_fmt = flac ] && [ $out_fmt = flac ] ; then
309                 CMP=md5cmp
310         else
311                 CMP=cmp
312         fi
313
314         if [ $out_fmt = ogg ] ; then
315                 eopt="--ogg $eopt"
316         fi
317
318         #
319         # test --skip when encoding
320         #
321
322         desc="($in_fmt<->$out_fmt)"
323
324         echo -n "testing --skip=# (encode) $desc... "
325         run_flac $eopt --skip=10 -o z50c.skip10.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
326         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.$in_fmt z50c.skip10.$out_fmt || die "ERROR decoding FLAC file $desc"
327         $CMP 50c.skip10.$in_fmt z50c.skip10.$in_fmt || die "ERROR: file mismatch for --skip=10 (encode) $desc"
328         rm -f z50c.skip10.$out_fmt z50c.skip10.$in_fmt
329         echo OK
330
331         echo -n "testing --skip=mm:ss (encode) $desc... "
332         run_flac $eopt --skip=0:01 -o z50c.skip0:01.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
333         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip0:01.$in_fmt z50c.skip0:01.$out_fmt || die "ERROR decoding FLAC file $desc"
334         $CMP 50c.skip10.$in_fmt z50c.skip0:01.$in_fmt || die "ERROR: file mismatch for --skip=0:01 (encode) $desc"
335         rm -f z50c.skip0:01.$out_fmt z50c.skip0:01.$in_fmt
336         echo OK
337
338         echo -n "testing --skip=mm:ss.sss (encode) $desc... "
339         run_flac $eopt --skip=0:01.1001 -o z50c.skip0:01.1001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
340         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip0:01.1001.$in_fmt z50c.skip0:01.1001.$out_fmt || die "ERROR decoding FLAC file $desc"
341         $CMP 50c.skip11.$in_fmt z50c.skip0:01.1001.$in_fmt || die "ERROR: file mismatch for --skip=0:01.1001 (encode) $desc"
342         rm -f z50c.skip0:01.1001.$out_fmt z50c.skip0:01.1001.$in_fmt
343         echo OK
344
345         #
346         # test --skip when decoding
347         #
348
349         if [ $in_fmt != $out_fmt ] ; then run_flac $eopt -o z50c.$out_fmt 50c.$in_fmt ; else cp -f 50c.$in_fmt z50c.$out_fmt ; fi || die "ERROR generating FLAC file $desc"
350
351         echo -n "testing --skip=# (decode) $desc... "
352         run_flac $dopt --skip=10 -o z50c.skip10.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
353         $CMP 50c.skip10.$in_fmt z50c.skip10.$in_fmt || die "ERROR: file mismatch for --skip=10 (decode) $desc"
354         rm -f z50c.skip10.$in_fmt
355         echo OK
356
357         echo -n "testing --skip=mm:ss (decode) $desc... "
358         run_flac $dopt --skip=0:01 -o z50c.skip0:01.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
359         $CMP 50c.skip10.$in_fmt z50c.skip0:01.$in_fmt || die "ERROR: file mismatch for --skip=0:01 (decode) $desc"
360         rm -f z50c.skip0:01.$in_fmt
361         echo OK
362
363         echo -n "testing --skip=mm:ss.sss (decode) $desc... "
364         run_flac $dopt --skip=0:01.1001 -o z50c.skip0:01.1001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
365         $CMP 50c.skip11.$in_fmt z50c.skip0:01.1001.$in_fmt || die "ERROR: file mismatch for --skip=0:01.1001 (decode) $desc"
366         rm -f z50c.skip0:01.1001.$in_fmt
367         echo OK
368
369         rm -f z50c.$out_fmt
370
371         #
372         # test --until when encoding
373         #
374
375         echo -n "testing --until=# (encode) $desc... "
376         run_flac $eopt --until=40 -o z50c.until40.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
377         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until40.$in_fmt z50c.until40.$out_fmt || die "ERROR decoding FLAC file $desc"
378         $CMP 50c.until40.$in_fmt z50c.until40.$in_fmt || die "ERROR: file mismatch for --until=40 (encode) $desc"
379         rm -f z50c.until40.$out_fmt z50c.until40.$in_fmt
380         echo OK
381
382         echo -n "testing --until=mm:ss (encode) $desc... "
383         run_flac $eopt --until=0:04 -o z50c.until0:04.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
384         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until0:04.$in_fmt z50c.until0:04.$out_fmt || die "ERROR decoding FLAC file $desc"
385         $CMP 50c.until40.$in_fmt z50c.until0:04.$in_fmt || die "ERROR: file mismatch for --until=0:04 (encode) $desc"
386         rm -f z50c.until0:04.$out_fmt z50c.until0:04.$in_fmt
387         echo OK
388
389         echo -n "testing --until=mm:ss.sss (encode) $desc... "
390         run_flac $eopt --until=0:03.9001 -o z50c.until0:03.9001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
391         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until0:03.9001.$in_fmt z50c.until0:03.9001.$out_fmt || die "ERROR decoding FLAC file $desc"
392         $CMP 50c.until39.$in_fmt z50c.until0:03.9001.$in_fmt || die "ERROR: file mismatch for --until=0:03.9001 (encode) $desc"
393         rm -f z50c.until0:03.9001.$out_fmt z50c.until0:03.9001.$in_fmt
394         echo OK
395
396         echo -n "testing --until=-# (encode) $desc... "
397         run_flac $eopt --until=-10 -o z50c.until-10.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
398         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until-10.$in_fmt z50c.until-10.$out_fmt || die "ERROR decoding FLAC file $desc"
399         $CMP 50c.until40.$in_fmt z50c.until-10.$in_fmt || die "ERROR: file mismatch for --until=-10 (encode) $desc"
400         rm -f z50c.until-10.$out_fmt z50c.until-10.$in_fmt
401         echo OK
402
403         echo -n "testing --until=-mm:ss (encode) $desc... "
404         run_flac $eopt --until=-0:01 -o z50c.until-0:01.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
405         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until-0:01.$in_fmt z50c.until-0:01.$out_fmt || die "ERROR decoding FLAC file $desc"
406         $CMP 50c.until40.$in_fmt z50c.until-0:01.$in_fmt || die "ERROR: file mismatch for --until=-0:01 (encode) $desc"
407         rm -f z50c.until-0:01.$out_fmt z50c.until-0:01.$in_fmt
408         echo OK
409
410         echo -n "testing --until=-mm:ss.sss (encode) $desc... "
411         run_flac $eopt --until=-0:01.1001 -o z50c.until-0:01.1001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
412         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.until-0:01.1001.$in_fmt z50c.until-0:01.1001.$out_fmt || die "ERROR decoding FLAC file $desc"
413         $CMP 50c.until39.$in_fmt z50c.until-0:01.1001.$in_fmt || die "ERROR: file mismatch for --until=-0:01.1001 (encode) $desc"
414         rm -f z50c.until-0:01.1001.$out_fmt z50c.until-0:01.1001.$in_fmt
415         echo OK
416
417         #
418         # test --until when decoding
419         #
420
421         if [ $in_fmt != $out_fmt ] ; then run_flac $eopt -o z50c.$out_fmt 50c.$in_fmt ; else cp -f 50c.$in_fmt z50c.$out_fmt ; fi || die "ERROR generating FLAC file $desc"
422
423         echo -n "testing --until=# (decode) $desc... "
424         run_flac $dopt --until=40 -o z50c.until40.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
425         $CMP 50c.until40.$in_fmt z50c.until40.$in_fmt || die "ERROR: file mismatch for --until=40 (decode) $desc"
426         rm -f z50c.until40.$in_fmt
427         echo OK
428
429         echo -n "testing --until=mm:ss (decode) $desc... "
430         run_flac $dopt --until=0:04 -o z50c.until0:04.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
431         $CMP 50c.until40.$in_fmt z50c.until0:04.$in_fmt || die "ERROR: file mismatch for --until=0:04 (decode) $desc"
432         rm -f z50c.until0:04.$in_fmt
433         echo OK
434
435         echo -n "testing --until=mm:ss.sss (decode) $desc... "
436         run_flac $dopt --until=0:03.9001 -o z50c.until0:03.9001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
437         $CMP 50c.until39.$in_fmt z50c.until0:03.9001.$in_fmt || die "ERROR: file mismatch for --until=0:03.9001 (decode) $desc"
438         rm -f z50c.until0:03.9001.$in_fmt
439         echo OK
440
441         echo -n "testing --until=-# (decode) $desc... "
442         run_flac $dopt --until=-10 -o z50c.until-10.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
443         $CMP 50c.until40.$in_fmt z50c.until-10.$in_fmt || die "ERROR: file mismatch for --until=-10 (decode) $desc"
444         rm -f z50c.until-10.$in_fmt
445         echo OK
446
447         echo -n "testing --until=-mm:ss (decode) $desc... "
448         run_flac $dopt --until=-0:01 -o z50c.until-0:01.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
449         $CMP 50c.until40.$in_fmt z50c.until-0:01.$in_fmt || die "ERROR: file mismatch for --until=-0:01 (decode) $desc"
450         rm -f z50c.until-0:01.$in_fmt
451         echo OK
452
453         echo -n "testing --until=-mm:ss.sss (decode) $desc... "
454         run_flac $dopt --until=-0:01.1001 -o z50c.until-0:01.1001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
455         $CMP 50c.until39.$in_fmt z50c.until-0:01.1001.$in_fmt || die "ERROR: file mismatch for --until=-0:01.1001 (decode) $desc"
456         rm -f z50c.until-0:01.1001.$in_fmt
457         echo OK
458
459         rm -f z50c.$out_fmt
460
461         #
462         # test --skip and --until when encoding
463         #
464
465         echo -n "testing --skip=10 --until=# (encode) $desc... "
466         run_flac $eopt --skip=10 --until=40 -o z50c.skip10.until40.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
467         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until40.$in_fmt z50c.skip10.until40.$out_fmt || die "ERROR decoding FLAC file $desc"
468         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until40.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=40 (encode) $desc"
469         rm -f z50c.skip10.until40.$out_fmt z50c.skip10.until40.$in_fmt
470         echo OK
471
472         echo -n "testing --skip=10 --until=mm:ss (encode) $desc... "
473         run_flac $eopt --skip=10 --until=0:04 -o z50c.skip10.until0:04.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
474         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until0:04.$in_fmt z50c.skip10.until0:04.$out_fmt || die "ERROR decoding FLAC file $desc"
475         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until0:04.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=0:04 (encode) $desc"
476         rm -f z50c.skip10.until0:04.$out_fmt z50c.skip10.until0:04.$in_fmt
477         echo OK
478
479         echo -n "testing --skip=10 --until=mm:ss.sss (encode) $desc... "
480         run_flac $eopt --skip=10 --until=0:03.9001 -o z50c.skip10.until0:03.9001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
481         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until0:03.9001.$in_fmt z50c.skip10.until0:03.9001.$out_fmt || die "ERROR decoding FLAC file $desc"
482         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until0:03.9001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=0:03.9001 (encode) $desc"
483         rm -f z50c.skip10.until0:03.9001.$out_fmt z50c.skip10.until0:03.9001.$in_fmt
484         echo OK
485
486         echo -n "testing --skip=10 --until=+# (encode) $desc... "
487         run_flac $eopt --skip=10 --until=+30 -o z50c.skip10.until+30.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
488         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until+30.$in_fmt z50c.skip10.until+30.$out_fmt || die "ERROR decoding FLAC file $desc"
489         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until+30.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=+30 (encode) $desc"
490         rm -f z50c.skip10.until+30.$out_fmt z50c.skip10.until+30.$in_fmt
491         echo OK
492
493         echo -n "testing --skip=10 --until=+mm:ss (encode) $desc... "
494         run_flac $eopt --skip=10 --until=+0:03 -o z50c.skip10.until+0:03.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
495         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until+0:03.$in_fmt z50c.skip10.until+0:03.$out_fmt || die "ERROR decoding FLAC file $desc"
496         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until+0:03.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=+0:03 (encode) $desc"
497         rm -f z50c.skip10.until+0:03.$out_fmt z50c.skip10.until+0:03.$in_fmt
498         echo OK
499
500         echo -n "testing --skip=10 --until=+mm:ss.sss (encode) $desc... "
501         run_flac $eopt --skip=10 --until=+0:02.9001 -o z50c.skip10.until+0:02.9001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
502         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until+0:02.9001.$in_fmt z50c.skip10.until+0:02.9001.$out_fmt || die "ERROR decoding FLAC file $desc"
503         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until+0:02.9001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=+0:02.9001 (encode) $desc"
504         rm -f z50c.skip10.until+0:02.9001.$out_fmt z50c.skip10.until+0:02.9001.$in_fmt
505         echo OK
506
507         echo -n "testing --skip=10 --until=-# (encode) $desc... "
508         run_flac $eopt --skip=10 --until=-10 -o z50c.skip10.until-10.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
509         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until-10.$in_fmt z50c.skip10.until-10.$out_fmt || die "ERROR decoding FLAC file $desc"
510         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until-10.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-10 (encode) $desc"
511         rm -f z50c.skip10.until-10.$out_fmt z50c.skip10.until-10.$in_fmt
512         echo OK
513
514         echo -n "testing --skip=10 --until=-mm:ss (encode) $desc... "
515         run_flac $eopt --skip=10 --until=-0:01 -o z50c.skip10.until-0:01.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
516         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until-0:01.$in_fmt z50c.skip10.until-0:01.$out_fmt || die "ERROR decoding FLAC file $desc"
517         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until-0:01.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-0:01 (encode) $desc"
518         rm -f z50c.skip10.until-0:01.$out_fmt z50c.skip10.until-0:01.$in_fmt
519         echo OK
520
521         echo -n "testing --skip=10 --until=-mm:ss.sss (encode) $desc... "
522         run_flac $eopt --skip=10 --until=-0:01.1001 -o z50c.skip10.until-0:01.1001.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
523         [ $in_fmt = $out_fmt ] || run_flac $dopt -o z50c.skip10.until-0:01.1001.$in_fmt z50c.skip10.until-0:01.1001.$out_fmt || die "ERROR decoding FLAC file $desc"
524         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until-0:01.1001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-0:01.1001 (encode) $desc"
525         rm -f z50c.skip10.until-0:01.1001.$out_fmt z50c.skip10.until-0:01.1001.$in_fmt
526         echo OK
527
528         #
529         # test --skip and --until when decoding
530         #
531
532         if [ $in_fmt != $out_fmt ] ; then run_flac $eopt -o z50c.$out_fmt 50c.$in_fmt ; else cp -f 50c.$in_fmt z50c.$out_fmt ; fi || die "ERROR generating FLAC file $desc"
533
534
535         echo -n "testing --skip=10 --until=# (decode) $desc... "
536         run_flac $dopt --skip=10 --until=40 -o z50c.skip10.until40.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
537         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until40.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=40 (decode) $desc"
538         rm -f z50c.skip10.until40.$in_fmt
539         echo OK
540
541         echo -n "testing --skip=10 --until=mm:ss (decode) $desc... "
542         run_flac $dopt --skip=10 --until=0:04 -o z50c.skip10.until0:04.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
543         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until0:04.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=0:04 (decode) $desc"
544         rm -f z50c.skip10.until0:04.$in_fmt
545         echo OK
546
547         echo -n "testing --skip=10 --until=mm:ss.sss (decode) $desc... "
548         run_flac $dopt --skip=10 --until=0:03.9001 -o z50c.skip10.until0:03.9001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
549         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until0:03.9001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=0:03.9001 (decode) $desc"
550         rm -f z50c.skip10.until0:03.9001.$in_fmt
551         echo OK
552
553         echo -n "testing --skip=10 --until=-# (decode) $desc... "
554         run_flac $dopt --skip=10 --until=-10 -o z50c.skip10.until-10.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
555         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until-10.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-10 (decode) $desc"
556         rm -f z50c.skip10.until-10.$in_fmt
557         echo OK
558
559         echo -n "testing --skip=10 --until=-mm:ss (decode) $desc... "
560         run_flac $dopt --skip=10 --until=-0:01 -o z50c.skip10.until-0:01.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
561         $CMP 50c.skip10.until40.$in_fmt z50c.skip10.until-0:01.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-0:01 (decode) $desc"
562         rm -f z50c.skip10.until-0:01.$in_fmt
563         echo OK
564
565         echo -n "testing --skip=10 --until=-mm:ss.sss (decode) $desc... "
566         run_flac $dopt --skip=10 --until=-0:01.1001 -o z50c.skip10.until-0:01.1001.$in_fmt z50c.$out_fmt || die "ERROR decoding FLAC file $desc"
567         $CMP 50c.skip10.until39.$in_fmt z50c.skip10.until-0:01.1001.$in_fmt || die "ERROR: file mismatch for --skip=10 --until=-0:01.1001 (decode) $desc"
568         rm -f z50c.skip10.until-0:01.1001.$in_fmt
569         echo OK
570
571         rm -f z50c.$out_fmt
572 }
573
574 test_skip_until raw flac
575 test_skip_until wav flac
576 test_skip_until aiff flac
577 test_skip_until flac flac
578
579 if [ $has_ogg = "yes" ] ; then
580         test_skip_until raw ogg
581         test_skip_until wav ogg
582         test_skip_until aiff ogg
583         #@@@ doesn't work yet, no comparison step written since metaflac doesn't work on ogg flac yet
584         #@@@test_skip_until flac ogg
585 fi
586
587 echo "testing seek extremes:"
588
589 run_flac --verify --force $SILENT --force-raw-format --endian=big --sign=signed --sample-rate=44100 --bps=16 --channels=2 --blocksize=576 noise.raw || die "ERROR generating FLAC file"
590
591 if [ $is_win = no ] ; then
592         total_noise_cdda_samples=`run_metaflac --show-total-samples noise.flac`
593         [ $? = 0 ] || die "ERROR getting total sample count from noise.flac"
594 else
595         # some flavors of cygwin don't seem to treat the \x0d as a word
596         # separator, so we hard code it.  we'll just have to fix it later
597         # if we change the way noise.flac is made.
598         total_noise_cdda_samples=393216
599 fi
600
601 echo -n "testing --skip=0... "
602 run_flac $wav_dopt --skip=0 -o z.wav noise.flac || die "ERROR decoding FLAC file noise.flac"
603 echo OK
604
605 for delta in 2 1 ; do
606         n=`expr $total_noise_cdda_samples - $delta`
607         echo -n "testing --skip=$n... "
608         run_flac $wav_dopt --skip=$n -o z.wav noise.flac || die "ERROR decoding FLAC file noise.flac"
609         echo OK
610 done
611
612 rm noise.flac z.wav
613
614
615 ############################################################################
616 # test --input-size
617 ############################################################################
618
619 #@@@ cat will not work on old cygwin, need to fix
620 if [ $is_win = no ] ; then
621         echo -n "testing --input-size=50 --skip=10... "
622         cat 50c.raw | run_flac $raw_eopt --input-size=50 --skip=10 -o z50c.skip10.flac - || die "ERROR generating FLAC file"
623         run_flac $raw_dopt -o z50c.skip10.raw z50c.skip10.flac || die "ERROR decoding FLAC file"
624         cmp 50c.skip10.raw z50c.skip10.raw || die "ERROR: file mismatch for --input-size=50 --skip=10"
625         rm -f z50c.skip10.raw z50c.skip10.flac
626         echo OK
627 fi
628
629
630 ############################################################################
631 # test --cue
632 ############################################################################
633
634 #
635 # create the cue sheet
636 #
637 cuesheet=cuetest.cue
638 cat > $cuesheet << EOF
639 CATALOG 1234567890123
640 FILE "blah" WAVE
641   TRACK 01 AUDIO
642     INDEX 01 0
643     INDEX 02 10
644     INDEX 03 20
645   TRACK 02 AUDIO
646     INDEX 01 30
647   TRACK 04 AUDIO
648     INDEX 01 40
649 EOF
650
651 test_cue ()
652 {
653         in_fmt=$1
654         out_fmt=$2
655
656         [ "$in_fmt" = wav ] || [ "$in_fmt" = aiff ] || [ "$in_fmt" = raw ] || [ "$in_fmt" = flac ] || die "ERROR: internal error, bad 'in' format '$in_fmt'"
657
658         [ "$out_fmt" = flac ] || [ "$out_fmt" = ogg ] || die "ERROR: internal error, bad 'out' format '$out_fmt'"
659
660         if [ $in_fmt = raw ] ; then
661                 eopt="$raw_eopt"
662                 dopt="$raw_dopt"
663         else
664                 eopt="$wav_eopt"
665                 dopt="$wav_dopt"
666         fi
667
668         if [ $in_fmt = flac ] && [ $out_fmt = flac ] ; then
669                 CMP=md5cmp
670         else
671                 CMP=cmp
672         fi
673
674         if [ $out_fmt = ogg ] ; then
675                 eopt="--ogg $eopt"
676         fi
677
678         desc="($in_fmt<->$out_fmt)"
679
680         #
681         # for this we need just need just one FLAC file; --cue only works while decoding
682         #
683         run_flac $eopt --cuesheet=$cuesheet -o z50c.cue.$out_fmt 50c.$in_fmt || die "ERROR generating FLAC file $desc"
684
685         # To make it easy to translate from cue point to sample numbers, the
686         # file has a sample rate of 10 Hz and a cuesheet like so:
687         #
688         # TRACK 01, INDEX 01 : 0:00.00 -> sample 0
689         # TRACK 01, INDEX 02 : 0:01.00 -> sample 10
690         # TRACK 01, INDEX 03 : 0:02.00 -> sample 20
691         # TRACK 02, INDEX 01 : 0:03.00 -> sample 30
692         # TRACK 04, INDEX 01 : 0:04.00 -> sample 40
693         #
694         echo -n "testing --cue=- $desc... "
695         run_flac $dopt -o z50c.cued.$in_fmt --cue=- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
696         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=- $desc"
697         rm -f z50c.cued.$in_fmt
698         echo OK
699
700         echo -n "testing --cue=1.0 $desc... "
701         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
702         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.0 $desc"
703         rm -f z50c.cued.$in_fmt
704         echo OK
705
706         echo -n "testing --cue=1.0- $desc... "
707         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.0- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
708         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.0- $desc"
709         rm -f z50c.cued.$in_fmt
710         echo OK
711
712         echo -n "testing --cue=1.1 $desc... "
713         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.1 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
714         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.1 $desc"
715         rm -f z50c.cued.$in_fmt
716         echo OK
717
718         echo -n "testing --cue=1.1- $desc... "
719         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.1- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
720         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.1- $desc"
721         rm -f z50c.cued.$in_fmt
722         echo OK
723
724         echo -n "testing --cue=1.2 $desc... "
725         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.2 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
726         $CMP 50c.skip10.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.2 $desc"
727         rm -f z50c.cued.$in_fmt
728         echo OK
729
730         echo -n "testing --cue=1.2- $desc... "
731         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.2- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
732         $CMP 50c.skip10.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.2- $desc"
733         rm -f z50c.cued.$in_fmt
734         echo OK
735
736         echo -n "testing --cue=1.4 $desc... "
737         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.4 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
738         $CMP 50c.skip20.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.4 $desc"
739         rm -f z50c.cued.$in_fmt
740         echo OK
741
742         echo -n "testing --cue=1.4- $desc... "
743         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.4- z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
744         $CMP 50c.skip20.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.4- $desc"
745         rm -f z50c.cued.$in_fmt
746         echo OK
747
748         echo -n "testing --cue=-5.0 $desc... "
749         run_flac $dopt -o z50c.cued.$in_fmt --cue=-5.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
750         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=-5.0 $desc"
751         rm -f z50c.cued.$in_fmt
752         echo OK
753
754         echo -n "testing --cue=-4.1 $desc... "
755         run_flac $dopt -o z50c.cued.$in_fmt --cue=-4.1 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
756         $CMP 50c.until40.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=-4.1 $desc"
757         rm -f z50c.cued.$in_fmt
758         echo OK
759
760         echo -n "testing --cue=-3.1 $desc... "
761         run_flac $dopt -o z50c.cued.$in_fmt --cue=-3.1 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
762         $CMP 50c.until40.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=-3.1 $desc"
763         rm -f z50c.cued.$in_fmt
764         echo OK
765
766         echo -n "testing --cue=-1.4 $desc... "
767         run_flac $dopt -o z50c.cued.$in_fmt --cue=-1.4 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
768         $CMP 50c.until30.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=-1.4 $desc"
769         rm -f z50c.cued.$in_fmt
770         echo OK
771
772         echo -n "testing --cue=1.0-5.0 $desc... "
773         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.0-5.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
774         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.0-5.0 $desc"
775         rm -f z50c.cued.$in_fmt
776         echo OK
777
778         echo -n "testing --cue=1.1-5.0 $desc... "
779         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.1-5.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
780         $CMP 50c.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.1-5.0 $desc"
781         rm -f z50c.cued.$in_fmt
782         echo OK
783
784         echo -n "testing --cue=1.2-4.1 $desc... "
785         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.2-4.1 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
786         $CMP 50c.skip10.until40.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.2-4.1 $desc"
787         rm -f z50c.cued.$in_fmt
788         echo OK
789
790         echo -n "testing --cue=1.4-2.0 $desc... "
791         run_flac $dopt -o z50c.cued.$in_fmt --cue=1.4-2.0 z50c.cue.$out_fmt || die "ERROR decoding FLAC file $desc"
792         $CMP 50c.skip20.until30.$in_fmt z50c.cued.$in_fmt || die "ERROR: file mismatch for --cue=1.4-2.0 $desc"
793         rm -f z50c.cued.$in_fmt
794         echo OK
795
796         rm -f z50c.cue.$out_fmt
797 }
798
799 test_cue raw flac
800 test_cue wav flac
801 test_cue aiff flac
802 test_cue flac flac
803
804 if [ $has_ogg = "yes" ] ; then
805         test_cue raw ogg
806         test_cue wav ogg
807         test_cue aiff ogg
808         #@@@ doesn't work yet, no comparison step written since metaflac doesn't work on ogg flac yet
809         #@@@test_cue flac ogg
810 fi
811
812 ############################################################################
813 # test 'fixup' code that happens when a FLAC file with total_samples == 0
814 # in the STREAMINFO block is converted to WAVE or AIFF, requiring the
815 # decoder go back and fix up the chunk headers
816 ############################################################################
817
818 echo -n "WAVE fixup test... "
819
820 echo -n "prepare... "
821 convert_to_wav noise "$raw_eopt" "$wav_dopt" || die "ERROR creating reference WAVE"
822
823 echo -n "encode... "
824 # the pipe from 'cat' to 'flac' does not work on cygwin because of the EOF/
825 # binary-mode stdin problem, so we use an undocumented option to metaflac to
826 # set the total sample count to 0
827 if [ $is_win = yes ] ; then
828         run_flac $raw_eopt noise.raw -o fixup.flac || die "ERROR generating FLAC file"
829         run_metaflac --set-total-samples=0 fixup.flac 2> /dev/null
830 else
831         cat noise.raw | run_flac $raw_eopt - -c > fixup.flac || die "ERROR generating FLAC file"
832 fi
833
834 echo -n "decode... "
835 run_flac $wav_dopt fixup.flac -o fixup.wav || die "ERROR decoding FLAC file"
836
837 echo -n "compare... "
838 cmp noise.wav fixup.wav || die "ERROR: file mismatch"
839
840 echo OK
841 rm -f noise.wav fixup.wav fixup.flac
842
843 echo -n "AIFF fixup test... "
844
845 echo -n "prepare... "
846 convert_to_aiff noise "$raw_eopt" "$wav_dopt" || die "ERROR creating reference AIFF"
847
848 echo -n "encode... "
849 # the pipe from 'cat' to 'flac' does not work on cygwin because of the EOF/
850 # binary-mode stdin problem, so we use an undocumented option to metaflac to
851 # set the total sample count to 0
852 if [ $is_win = yes ] ; then
853         run_flac $raw_eopt noise.raw -o fixup.flac || die "ERROR generating FLAC file"
854         run_metaflac --set-total-samples=0 fixup.flac 2> /dev/null
855 else
856         cat noise.raw | run_flac $raw_eopt - -c > fixup.flac || die "ERROR generating FLAC file"
857 fi
858
859 echo -n "decode... "
860 run_flac $wav_dopt fixup.flac -o fixup.aiff || die "ERROR decoding FLAC file"
861
862 echo -n "compare... "
863 cmp noise.aiff fixup.aiff || die "ERROR: file mismatch"
864
865 echo OK
866 rm -f noise.aiff fixup.aiff fixup.flac
867
868
869 ############################################################################
870 # multi-file tests
871 ############################################################################
872
873 echo "Generating multiple input files from noise..."
874 multifile_format_decode="--endian=big --sign=signed"
875 multifile_format_encode="$multifile_format_decode --sample-rate=44100 --bps=16 --channels=2"
876 short_noise_cdda_samples=`expr $total_noise_cdda_samples / 8`
877 run_flac --verify --force $SILENT --force-raw-format $multifile_format_encode --until=$short_noise_cdda_samples -o shortnoise.flac noise.raw || die "ERROR generating FLAC file"
878 run_flac --decode --force $SILENT shortnoise.flac -o shortnoise.raw --force-raw-format $multifile_format_decode || die "ERROR generating RAW file"
879 run_flac --decode --force $SILENT shortnoise.flac || die "ERROR generating WAVE file"
880 run_flac --decode --force $SILENT shortnoise.flac -o shortnoise.aiff || die "ERROR generating AIFF file"
881 cp shortnoise.flac file0.flac
882 cp shortnoise.flac file1.flac
883 cp shortnoise.flac file2.flac
884 rm -f shortnoise.flac
885 cp shortnoise.wav file0.wav
886 cp shortnoise.wav file1.wav
887 cp shortnoise.wav file2.wav
888 rm -f shortnoise.wav
889 cp shortnoise.aiff file0.aiff
890 cp shortnoise.aiff file1.aiff
891 cp shortnoise.aiff file2.aiff
892 rm -f shortnoise.aiff
893 cp shortnoise.raw file0.raw
894 cp shortnoise.raw file1.raw
895 cp shortnoise.raw file2.raw
896 rm -f shortnoise.raw
897 # create authoritative sector-aligned files for comparison
898 file0_samples=`expr \( $short_noise_cdda_samples / 588 \) \* 588`
899 file0_remainder=`expr $short_noise_cdda_samples - $file0_samples`
900 file1_samples=`expr \( \( $file0_remainder + $short_noise_cdda_samples \) / 588 \) \* 588`
901 file1_remainder=`expr $file0_remainder + $short_noise_cdda_samples - $file1_samples`
902 file1_samples=`expr $file1_samples - $file0_remainder`
903 file2_samples=`expr \( \( $file1_remainder + $short_noise_cdda_samples \) / 588 \) \* 588`
904 file2_remainder=`expr $file1_remainder + $short_noise_cdda_samples - $file2_samples`
905 file2_samples=`expr $file2_samples - $file1_remainder`
906 if [ $file2_remainder != '0' ] ; then
907         file2_samples=`expr $file2_samples + $file2_remainder`
908         file2_remainder=`expr 588 - $file2_remainder`
909 fi
910
911 dd if=file0.raw ibs=4 count=$file0_samples of=file0s.raw 2>/dev/null || $dddie
912 dd if=file0.raw ibs=4 count=$file0_remainder of=file1s.raw skip=$file0_samples 2>/dev/null || $dddie
913 dd if=file1.raw ibs=4 count=$file1_samples of=z.raw 2>/dev/null || $dddie
914 cat z.raw >> file1s.raw || die "ERROR: cat-ing sector-aligned files"
915 dd if=file1.raw ibs=4 count=$file1_remainder of=file2s.raw skip=$file1_samples 2>/dev/null || $dddie
916 dd if=file2.raw ibs=4 count=$file2_samples of=z.raw 2>/dev/null || $dddie
917 cat z.raw >> file2s.raw || die "ERROR: cat-ing sector-aligned files"
918 dd if=/dev/zero ibs=4 count=$file2_remainder of=z.raw 2>/dev/null || $dddie
919 cat z.raw >> file2s.raw || die "ERROR: cat-ing sector-aligned files"
920 rm -f z.raw
921
922 convert_to_wav file0s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned WAVE"
923 convert_to_wav file1s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned WAVE"
924 convert_to_wav file2s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned WAVE"
925
926 convert_to_aiff file0s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned AIFF"
927 convert_to_aiff file1s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned AIFF"
928 convert_to_aiff file2s "$multifile_format_encode --force" "$SILENT --force --decode" || die "ERROR creating authoritative sector-aligned AIFF"
929
930 test_multifile ()
931 {
932         input_type=$1
933         streamtype=$2
934         sector_align=$3
935         encode_options="$4"
936
937         extra_encode_options=""
938         extra_decode_options=""
939         if [ $input_type = "raw" ] ; then
940                 extra_encode_options="--force-raw-format $multifile_format_encode"
941                 extra_decode_options="--force-raw-format $multifile_format_decode"
942         else
943                 if [ $input_type = "aiff" ] ; then
944                         extra_decode_options="--force-aiff-format"
945                 fi
946         fi
947
948         if [ $streamtype = ogg ] ; then
949                 suffix=ogg
950                 encode_options="$encode_options --ogg"
951         else
952                 suffix=flac
953         fi
954
955         if [ $sector_align = sector_align ] ; then
956                 encode_options="$encode_options --sector-align"
957         fi
958
959         if [ $input_type = flac ] ; then
960                 CMP=md5cmp
961         else
962                 CMP=cmp
963         fi
964
965         for n in 0 1 2 ; do
966                 cp file$n.$input_type file${n}x.$input_type
967         done
968         run_flac --force $encode_options $extra_encode_options file0x.$input_type file1x.$input_type file2x.$input_type || die "ERROR"
969         run_flac --force --decode $extra_decode_options file0x.$suffix file1x.$suffix file2x.$suffix || die "ERROR"
970         if [ $sector_align != sector_align ] ; then
971                 for n in 0 1 2 ; do
972                         $CMP file$n.$input_type file${n}x.$input_type || die "ERROR: file mismatch on file #$n"
973                 done
974         else
975                 for n in 0 1 2 ; do
976                         $CMP file${n}s.$input_type file${n}x.$input_type || die "ERROR: file mismatch on file #$n"
977                 done
978         fi
979         for n in 0 1 2 ; do
980                 rm -f file${n}x.$suffix file${n}x.$input_type
981         done
982 }
983
984 for input_type in raw wav aiff flac ; do
985         echo "Testing multiple $input_type files without verify..."
986         test_multifile $input_type flac no_sector_align ""
987
988         echo "Testing multiple $input_type files with verify..."
989         test_multifile $input_type flac no_sector_align "--verify"
990
991         if [ $input_type != flac ] ; then # --sector-align not supported for FLAC input
992                 echo "Testing multiple $input_type files with --sector-align, without verify..."
993                 test_multifile $input_type flac sector_align ""
994
995                 echo "Testing multiple $input_type files with --sector-align, with verify..."
996                 test_multifile $input_type flac sector_align "--verify"
997         fi
998
999         if [ $has_ogg = "yes" ] ; then
1000                 echo "Testing multiple $input_type files with --ogg, without verify..."
1001                 test_multifile $input_type ogg no_sector_align ""
1002
1003                 echo "Testing multiple $input_type files with --ogg, with verify..."
1004                 test_multifile $input_type ogg no_sector_align "--verify"
1005
1006                 if [ $input_type != flac ] ; then # --sector-align not supported for FLAC input
1007                         echo "Testing multiple $input_type files with --ogg and --sector-align, without verify..."
1008                         test_multifile $input_type ogg sector_align ""
1009
1010                         echo "Testing multiple $input_type files with --ogg and --sector-align, with verify..."
1011                         test_multifile $input_type ogg sector_align "--verify"
1012                 fi
1013
1014                 echo "Testing multiple $input_type files with --ogg and --serial-number, with verify..."
1015                 test_multifile $input_type ogg no_sector_align "--serial-number=321 --verify"
1016         fi
1017 done
1018
1019
1020 ############################################################################
1021 # test the metadata-handling properties of flac-to-flac encoding
1022 ############################################################################
1023
1024 echo "Testing the metadata-handling properties of flac-to-flac encoding..."
1025
1026 testdir="flac-to-flac-metadata-test-files"
1027 filter ()
1028 {
1029         # minor danger, changing vendor strings will change the length of the
1030         # VORBIS_COMMENT block, but if we add "^  length: " to the patterns,
1031         # we lose info about PADDING size that we need
1032         grep -Ev '^  vendor string: |^  m..imum .....size: ' | sed -e 's/, stream_offset.*//'
1033 }
1034 flac2flac ()
1035 {
1036         file="$1"
1037         case="$2"
1038         args="$3"
1039         expect="$case-expect.meta"
1040         echo -n "$case... "
1041         run_flac $SILENT -f -o out.flac $args $file || die "ERROR encoding FLAC file"
1042         run_metaflac --list out.flac | filter > out.meta || die "ERROR listing metadata of output FLAC file"
1043         diff -q -w $expect out.meta 2>/dev/null || die "ERROR: metadata does not match expected $expect"
1044         echo OK
1045 }
1046
1047 #filter=', stream_offset.*|^  vendor string: |^  length: |^  m..imum .....size: '
1048 cd $testdir || die "ERROR changing to directory $testdir"
1049
1050 # case 00a: no alterations on a file with all metadata types, keep all metadata, in same order
1051 flac2flac input-SCVAUP.flac case00a ""
1052 # case 01a: on file with multiple PADDING blocks, they should be aggregated into one at the end
1053 flac2flac input-SCVPAP.flac case01a ""
1054 # case 01b: on file with multiple PADDING blocks and --no-padding specified, they should all be deleted
1055 flac2flac input-SCVPAP.flac case01b "--no-padding"
1056 # case 01c: on file with multiple PADDING blocks and -P specified, they should all be overwritten with -P value
1057 flac2flac input-SCVPAP.flac case01c "-P 1234"
1058 # case 01d: on file with no PADDING blocks, use -P setting
1059 flac2flac input-SCVA.flac case01d "-P 1234"
1060 # case 01e: on file with no PADDING blocks and no -P given, use default padding
1061 flac2flac input-SCVA.flac case01e ""
1062 # case 02a: on file with no VORBIS_COMMENT block, add new VORBIS_COMMENT
1063 flac2flac input-SCPAP.flac case02a ""
1064 # case 02b: on file with no VORBIS_COMMENT block and --tag, add new VORBIS_COMMENT with tags
1065 flac2flac input-SCPAP.flac case02b "--tag=artist=0"
1066 # case 02c: on file with VORBIS_COMMENT block and --tag, replace existing VORBIS_COMMENT with new tags
1067 flac2flac input-SCVAUP.flac case02c "$TOTALLY_SILENT --tag=artist=0"
1068 # case 03a: on file with no CUESHEET block and --cuesheet specified, add it
1069 flac2flac input-SVAUP.flac case03a "--cuesheet=input0.cue"
1070 # case 03b: on file with CUESHEET block and --cuesheet specified, overwrite existing CUESHEET
1071 flac2flac input-SCVAUP.flac case03b "$TOTALLY_SILENT --cuesheet=input0.cue"
1072 # case 03c: on file with CUESHEET block and size-changing option specified, drop existing CUESHEET
1073 flac2flac input-SCVAUP.flac case03c "$TOTALLY_SILENT --skip=1"
1074 # case 04a: on file with no SEEKTABLE block and --no-seektable specified, no SEEKTABLE
1075 flac2flac input-VA.flac case04a "--no-padding --no-seektable"
1076 # case 04b: on file with no SEEKTABLE block and -S specified, new SEEKTABLE
1077 flac2flac input-VA.flac case04b "--no-padding -S 5x"
1078 # case 04c: on file with no SEEKTABLE block and no seektable options specified, new SEEKTABLE with default points
1079 flac2flac input-VA.flac case04c "--no-padding"
1080 # case 04d: on file with SEEKTABLE block and --no-seektable specified, drop existing SEEKTABLE
1081 flac2flac input-SCVA.flac case04d "--no-padding --no-seektable"
1082 # case 04e: on file with SEEKTABLE block and -S specified, overwrite existing SEEKTABLE
1083 flac2flac input-SCVA.flac case04e "$TOTALLY_SILENT --no-padding -S 5x"
1084 # case 04f: on file with SEEKTABLE block and size-changing option specified, drop existing SEEKTABLE, new SEEKTABLE with default points
1085 #(already covered by case03c)
1086
1087 rm -f out.flac out.meta
1088
1089 cd ..