add new/missing files
[flac.git] / src / test_libFLAC++ / decoders.cpp
1 /* test_libFLAC++ - Unit tester for libFLAC++
2  * Copyright (C) 2002,2003  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #include "decoders.h"
20 extern "C" {
21 #include "file_utils.h"
22 }
23 #include "FLAC/assert.h"
24 #include "FLAC/metadata.h" // for ::FLAC__metadata_object_is_equal()
25 #include "FLAC++/decoder.h"
26 #include "share/grabbag.h"
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_;
33 static ::FLAC__StreamMetadata *expected_metadata_sequence_[6];
34 static unsigned num_expected_;
35 static const char *flacfilename_ = "metadata.flac";
36 static unsigned flacfilesize_;
37
38 static bool die_(const char *msg)
39 {
40         printf("ERROR: %s\n", msg);
41         return false;
42 }
43
44 static void *malloc_or_die_(size_t size)
45 {
46         void *x = malloc(size);
47         if(0 == x) {
48                 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
49                 exit(1);
50         }
51         return x;
52 }
53
54 static void init_metadata_blocks_()
55 {
56         /*
57                 most of the actual numbers and data in the blocks don't matter,
58                 we just want to make sure the decoder parses them correctly
59
60                 remember, the metadata interface gets tested after the decoders,
61                 so we do all the metadata manipulation here without it.
62         */
63
64         /* min/max_framesize and md5sum don't get written at first, so we have to leave them 0 */
65         streaminfo_.is_last = false;
66         streaminfo_.type = ::FLAC__METADATA_TYPE_STREAMINFO;
67         streaminfo_.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
68         streaminfo_.data.stream_info.min_blocksize = 576;
69         streaminfo_.data.stream_info.max_blocksize = 576;
70         streaminfo_.data.stream_info.min_framesize = 0;
71         streaminfo_.data.stream_info.max_framesize = 0;
72         streaminfo_.data.stream_info.sample_rate = 44100;
73         streaminfo_.data.stream_info.channels = 1;
74         streaminfo_.data.stream_info.bits_per_sample = 8;
75         streaminfo_.data.stream_info.total_samples = 0;
76         memset(streaminfo_.data.stream_info.md5sum, 0, 16);
77
78         padding_.is_last = false;
79         padding_.type = ::FLAC__METADATA_TYPE_PADDING;
80         padding_.length = 1234;
81
82         seektable_.is_last = false;
83         seektable_.type = ::FLAC__METADATA_TYPE_SEEKTABLE;
84         seektable_.data.seek_table.num_points = 2;
85         seektable_.length = seektable_.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
86         seektable_.data.seek_table.points = (::FLAC__StreamMetadata_SeekPoint*)malloc_or_die_(seektable_.data.seek_table.num_points * sizeof(::FLAC__StreamMetadata_SeekPoint));
87         seektable_.data.seek_table.points[0].sample_number = 0;
88         seektable_.data.seek_table.points[0].stream_offset = 0;
89         seektable_.data.seek_table.points[0].frame_samples = streaminfo_.data.stream_info.min_blocksize;
90         seektable_.data.seek_table.points[1].sample_number = ::FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
91         seektable_.data.seek_table.points[1].stream_offset = 1000;
92         seektable_.data.seek_table.points[1].frame_samples = streaminfo_.data.stream_info.min_blocksize;
93
94         application1_.is_last = false;
95         application1_.type = ::FLAC__METADATA_TYPE_APPLICATION;
96         application1_.length = 8;
97         memcpy(application1_.data.application.id, "\xfe\xdc\xba\x98", 4);
98         application1_.data.application.data = (FLAC__byte*)malloc_or_die_(4);
99         memcpy(application1_.data.application.data, "\xf0\xe1\xd2\xc3", 4);
100
101         application2_.is_last = false;
102         application2_.type = ::FLAC__METADATA_TYPE_APPLICATION;
103         application2_.length = 4;
104         memcpy(application2_.data.application.id, "\x76\x54\x32\x10", 4);
105         application2_.data.application.data = 0;
106
107         {
108                 const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);
109                 vorbiscomment_.is_last = true;
110                 vorbiscomment_.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
111                 vorbiscomment_.length = (4 + vendor_string_length) + 4 + (4 + 5) + (4 + 0);
112                 vorbiscomment_.data.vorbis_comment.vendor_string.length = vendor_string_length;
113                 vorbiscomment_.data.vorbis_comment.vendor_string.entry = (FLAC__byte*)malloc_or_die_(vendor_string_length);
114                 memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, FLAC__VENDOR_STRING, vendor_string_length);
115                 vorbiscomment_.data.vorbis_comment.num_comments = 2;
116                 vorbiscomment_.data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc_or_die_(vorbiscomment_.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
117                 vorbiscomment_.data.vorbis_comment.comments[0].length = 5;
118                 vorbiscomment_.data.vorbis_comment.comments[0].entry = (FLAC__byte*)malloc_or_die_(5);
119                 memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "ab=cd", 5);
120                 vorbiscomment_.data.vorbis_comment.comments[1].length = 0;
121                 vorbiscomment_.data.vorbis_comment.comments[1].entry = 0;
122         }
123 }
124
125 static void free_metadata_blocks_()
126 {
127         free(seektable_.data.seek_table.points);
128         free(application1_.data.application.data);
129         free(vorbiscomment_.data.vorbis_comment.vendor_string.entry);
130         free(vorbiscomment_.data.vorbis_comment.comments[0].entry);
131         free(vorbiscomment_.data.vorbis_comment.comments);
132 }
133
134 static bool generate_file_()
135 {
136         printf("\n\ngenerating FLAC file for decoder tests...\n");
137
138         expected_metadata_sequence_[0] = &padding_;
139         expected_metadata_sequence_[1] = &seektable_;
140         expected_metadata_sequence_[2] = &application1_;
141         expected_metadata_sequence_[3] = &application2_;
142         expected_metadata_sequence_[4] = &vorbiscomment_;
143         num_expected_ = 5;
144
145         if(!file_utils__generate_flacfile(flacfilename_, &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_))
146                 return die_("creating the encoded file");
147
148         return true;
149 }
150
151
152 class DecoderCommon {
153 public:
154         FILE *file_;
155         unsigned current_metadata_number_;
156         bool ignore_errors_;
157         bool error_occurred_;
158
159         DecoderCommon(): file_(0), current_metadata_number_(0), ignore_errors_(false), error_occurred_(false) { }
160         ::FLAC__StreamDecoderReadStatus common_read_callback_(FLAC__byte buffer[], unsigned *bytes);
161         ::FLAC__StreamDecoderWriteStatus common_write_callback_(const ::FLAC__Frame *frame);
162         void common_metadata_callback_(const ::FLAC__StreamMetadata *metadata);
163         void common_error_callback_(::FLAC__StreamDecoderErrorStatus status);
164 };
165
166 ::FLAC__StreamDecoderReadStatus DecoderCommon::common_read_callback_(FLAC__byte buffer[], unsigned *bytes)
167 {
168         if(error_occurred_)
169                 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
170
171         if(feof(file_))
172                 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
173         else if(*bytes > 0) {
174                 unsigned bytes_read = ::fread(buffer, 1, *bytes, file_);
175                 if(bytes_read == 0) {
176                         if(feof(file_))
177                                 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
178                         else
179                                 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
180                 }
181                 else {
182                         *bytes = bytes_read;
183                         return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
184                 }
185         }
186         else
187                 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
188 }
189
190 ::FLAC__StreamDecoderWriteStatus DecoderCommon::common_write_callback_(const ::FLAC__Frame *frame)
191 {
192         if(error_occurred_)
193                 return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
194
195         if(
196                 (frame->header.number_type == ::FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) ||
197                 (frame->header.number_type == ::FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0)
198         ) {
199                 printf("content... ");
200                 fflush(stdout);
201         }
202
203         return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
204 }
205
206 void DecoderCommon::common_metadata_callback_(const ::FLAC__StreamMetadata *metadata)
207 {
208         if(error_occurred_)
209                 return;
210
211         printf("%d... ", current_metadata_number_);
212         fflush(stdout);
213
214         if(current_metadata_number_ >= num_expected_) {
215                 (void)die_("got more metadata blocks than expected");
216                 error_occurred_ = true;
217         }
218         else {
219                 if(!::FLAC__metadata_object_is_equal(expected_metadata_sequence_[current_metadata_number_], metadata)) {
220                         (void)die_("metadata block mismatch");
221                         error_occurred_ = true;
222                 }
223         }
224         current_metadata_number_++;
225 }
226
227 void DecoderCommon::common_error_callback_(::FLAC__StreamDecoderErrorStatus status)
228 {
229         if(!ignore_errors_) {
230                 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, ::FLAC__StreamDecoderErrorStatusString[status]);
231                 error_occurred_ = true;
232         }
233 }
234
235 class StreamDecoder : public FLAC::Decoder::Stream, public DecoderCommon {
236 public:
237         StreamDecoder(): FLAC::Decoder::Stream(), DecoderCommon() { }
238         ~StreamDecoder() { }
239
240         // from FLAC::Decoder::Stream
241         ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes);
242         ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
243         void metadata_callback(const ::FLAC__StreamMetadata *metadata);
244         void error_callback(::FLAC__StreamDecoderErrorStatus status);
245
246         bool die(const char *msg = 0) const;
247
248         bool test_respond();
249 };
250
251 ::FLAC__StreamDecoderReadStatus StreamDecoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
252 {
253         return common_read_callback_(buffer, bytes);
254 }
255
256 ::FLAC__StreamDecoderWriteStatus StreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
257 {
258         (void)buffer;
259
260         return common_write_callback_(frame);
261 }
262
263 void StreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
264 {
265         common_metadata_callback_(metadata);
266 }
267
268 void StreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
269 {
270         common_error_callback_(status);
271 }
272
273 bool StreamDecoder::die(const char *msg) const
274 {
275         State state = get_state();
276
277         if(msg)
278                 printf("FAILED, %s", msg);
279         else
280                 printf("FAILED");
281
282         printf(", state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
283
284         return false;
285 }
286
287 bool StreamDecoder::test_respond()
288 {
289         printf("testing init()... ");
290         if(init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
291                 return die();
292         printf("OK\n");
293
294         current_metadata_number_ = 0;
295
296         if(::fseek(file_, 0, SEEK_SET) < 0) {
297                 printf("FAILED rewinding input, errno = %d\n", errno);
298                 return false;
299         }
300
301         printf("testing process_until_end_of_stream()... ");
302         if(!process_until_end_of_stream()) {
303                 State state = get_state();
304                 printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
305                 return false;
306         }
307         printf("OK\n");
308
309         printf("testing finish()... ");
310         finish();
311         printf("OK\n");
312
313         return true;
314 }
315
316 static bool test_stream_decoder()
317 {
318         StreamDecoder *decoder;
319
320         printf("\n+++ libFLAC++ unit test: FLAC::Decoder::Stream\n\n");
321
322         //
323         // test new -> delete
324         //
325         printf("allocating decoder instance... ");
326         decoder = new StreamDecoder();
327         if(0 == decoder) {
328                 printf("FAILED, new returned NULL\n");
329                 return false;
330         }
331         printf("OK\n");
332
333         printf("testing is_valid()... ");
334         if(!decoder->is_valid()) {
335                 printf("FAILED, returned false\n");
336                 return false;
337         }
338         printf("OK\n");
339
340         printf("freeing decoder instance... ");
341         delete decoder;
342         printf("OK\n");
343
344         //
345         // test new -> init -> delete
346         //
347         printf("allocating decoder instance... ");
348         decoder = new StreamDecoder();
349         if(0 == decoder) {
350                 printf("FAILED, new returned NULL\n");
351                 return false;
352         }
353         printf("OK\n");
354
355         printf("testing is_valid()... ");
356         if(!decoder->is_valid()) {
357                 printf("FAILED, returned false\n");
358                 return false;
359         }
360         printf("OK\n");
361
362         printf("testing init()... ");
363         if(decoder->init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
364                 return decoder->die();
365         printf("OK\n");
366
367         printf("freeing decoder instance... ");
368         delete decoder;
369         printf("OK\n");
370
371         //
372         // test normal usage
373         //
374         num_expected_ = 0;
375         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
376
377         printf("allocating decoder instance... ");
378         decoder = new StreamDecoder();
379         if(0 == decoder) {
380                 printf("FAILED, new returned NULL\n");
381                 return false;
382         }
383         printf("OK\n");
384
385         printf("testing is_valid()... ");
386         if(!decoder->is_valid()) {
387                 printf("FAILED, returned false\n");
388                 return false;
389         }
390         printf("OK\n");
391
392         printf("testing init()... ");
393         if(decoder->init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
394                 return decoder->die();
395         printf("OK\n");
396
397         printf("testing get_state()... ");
398         FLAC::Decoder::Stream::State state = decoder->get_state();
399         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
400
401         decoder->current_metadata_number_ = 0;
402         decoder->ignore_errors_ = false;
403         decoder->error_occurred_ = false;
404
405         printf("opening FLAC file... ");
406         decoder->file_ = ::fopen(flacfilename_, "rb");
407         if(0 == decoder->file_) {
408                 printf("ERROR\n");
409                 return false;
410         }
411         printf("OK\n");
412
413         printf("testing process_until_end_of_metadata()... ");
414         if(!decoder->process_until_end_of_metadata())
415                 return decoder->die("returned false");
416         printf("OK\n");
417
418         printf("testing process_single()... ");
419         if(!decoder->process_single())
420                 return decoder->die("returned false");
421         printf("OK\n");
422
423         printf("testing flush()... ");
424         if(!decoder->flush())
425                 return decoder->die("returned false");
426         printf("OK\n");
427
428         decoder->ignore_errors_ = true;
429         printf("testing process_single()... ");
430         if(!decoder->process_single())
431                 return decoder->die("returned false");
432         printf("OK\n");
433         decoder->ignore_errors_ = false;
434
435         printf("testing process_until_end_of_stream()... ");
436         if(!decoder->process_until_end_of_stream())
437                 return decoder->die("returned false");
438         printf("OK\n");
439
440         printf("testing get_channels()... ");
441         {
442                 unsigned channels = decoder->get_channels();
443                 if(channels != streaminfo_.data.stream_info.channels) {
444                         printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
445                         return false;
446                 }
447         }
448         printf("OK\n");
449
450         printf("testing get_bits_per_sample()... ");
451         {
452                 unsigned bits_per_sample = decoder->get_bits_per_sample();
453                 if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
454                         printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
455                         return false;
456                 }
457         }
458         printf("OK\n");
459
460         printf("testing get_sample_rate()... ");
461         {
462                 unsigned sample_rate = decoder->get_sample_rate();
463                 if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
464                         printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
465                         return false;
466                 }
467         }
468         printf("OK\n");
469
470         printf("testing get_blocksize()... ");
471         {
472                 unsigned blocksize = decoder->get_blocksize();
473                 /* value could be anything since we're at the last block, so accept any answer */
474                 printf("returned %u... OK\n", blocksize);
475         }
476
477         printf("testing get_channel_assignment()... ");
478         {
479                 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
480                 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
481         }
482
483         printf("testing reset()... ");
484         if(!decoder->reset())
485                 return decoder->die("returned false");
486         printf("OK\n");
487
488         decoder->current_metadata_number_ = 0;
489
490         printf("rewinding input... ");
491         if(::fseek(decoder->file_, 0, SEEK_SET) < 0) {
492                 printf("FAILED, errno = %d\n", errno);
493                 return false;
494         }
495         printf("OK\n");
496
497         printf("testing process_until_end_of_stream()... ");
498         if(!decoder->process_until_end_of_stream())
499                 return decoder->die("returned false");
500         printf("OK\n");
501
502         printf("testing finish()... ");
503         decoder->finish();
504         printf("OK\n");
505
506         /*
507          * respond all
508          */
509
510         printf("testing set_metadata_respond_all()... ");
511         if(!decoder->set_metadata_respond_all()) {
512                 printf("FAILED, returned false\n");
513                 return false;
514         }
515         printf("OK\n");
516
517         num_expected_ = 0;
518         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
519         expected_metadata_sequence_[num_expected_++] = &padding_;
520         expected_metadata_sequence_[num_expected_++] = &seektable_;
521         expected_metadata_sequence_[num_expected_++] = &application1_;
522         expected_metadata_sequence_[num_expected_++] = &application2_;
523         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
524
525         if(!decoder->test_respond())
526                 return false;
527
528         /*
529          * ignore all
530          */
531
532         printf("testing set_metadata_ignore_all()... ");
533         if(!decoder->set_metadata_ignore_all()) {
534                 printf("FAILED, returned false\n");
535                 return false;
536         }
537         printf("OK\n");
538
539         num_expected_ = 0;
540
541         if(!decoder->test_respond())
542                 return false;
543
544         /*
545          * respond all, ignore VORBIS_COMMENT
546          */
547
548         printf("testing set_metadata_respond_all()... ");
549         if(!decoder->set_metadata_respond_all()) {
550                 printf("FAILED, returned false\n");
551                 return false;
552         }
553         printf("OK\n");
554
555         printf("testing set_metadata_ignore(VORBIS_COMMENT)... ");
556         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
557                 printf("FAILED, returned false\n");
558                 return false;
559         }
560         printf("OK\n");
561
562         num_expected_ = 0;
563         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
564         expected_metadata_sequence_[num_expected_++] = &padding_;
565         expected_metadata_sequence_[num_expected_++] = &seektable_;
566         expected_metadata_sequence_[num_expected_++] = &application1_;
567         expected_metadata_sequence_[num_expected_++] = &application2_;
568
569         if(!decoder->test_respond())
570                 return false;
571
572         /*
573          * respond all, ignore APPLICATION
574          */
575
576         printf("testing set_metadata_respond_all()... ");
577         if(!decoder->set_metadata_respond_all()) {
578                 printf("FAILED, returned false\n");
579                 return false;
580         }
581         printf("OK\n");
582
583         printf("testing set_metadata_ignore(APPLICATION)... ");
584         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
585                 printf("FAILED, returned false\n");
586                 return false;
587         }
588         printf("OK\n");
589
590         num_expected_ = 0;
591         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
592         expected_metadata_sequence_[num_expected_++] = &padding_;
593         expected_metadata_sequence_[num_expected_++] = &seektable_;
594         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
595
596         if(!decoder->test_respond())
597                 return false;
598
599         /*
600          * respond all, ignore APPLICATION id of app#1
601          */
602
603         printf("testing set_metadata_respond_all()... ");
604         if(!decoder->set_metadata_respond_all()) {
605                 printf("FAILED, returned false\n");
606                 return false;
607         }
608         printf("OK\n");
609
610         printf("testing set_metadata_ignore_application(of app block #1)... ");
611         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
612                 printf("FAILED, returned false\n");
613                 return false;
614         }
615         printf("OK\n");
616
617         num_expected_ = 0;
618         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
619         expected_metadata_sequence_[num_expected_++] = &padding_;
620         expected_metadata_sequence_[num_expected_++] = &seektable_;
621         expected_metadata_sequence_[num_expected_++] = &application2_;
622         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
623
624         if(!decoder->test_respond())
625                 return false;
626
627         /*
628          * respond all, ignore APPLICATION id of app#1 & app#2
629          */
630
631         printf("testing set_metadata_respond_all()... ");
632         if(!decoder->set_metadata_respond_all()) {
633                 printf("FAILED, returned false\n");
634                 return false;
635         }
636         printf("OK\n");
637
638         printf("testing set_metadata_ignore_application(of app block #1)... ");
639         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
640                 printf("FAILED, returned false\n");
641                 return false;
642         }
643         printf("OK\n");
644
645         printf("testing set_metadata_ignore_application(of app block #2)... ");
646         if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) {
647                 printf("FAILED, returned false\n");
648                 return false;
649         }
650         printf("OK\n");
651
652         num_expected_ = 0;
653         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
654         expected_metadata_sequence_[num_expected_++] = &padding_;
655         expected_metadata_sequence_[num_expected_++] = &seektable_;
656         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
657
658         if(!decoder->test_respond())
659                 return false;
660
661         /*
662          * ignore all, respond VORBIS_COMMENT
663          */
664
665         printf("testing set_metadata_ignore_all()... ");
666         if(!decoder->set_metadata_ignore_all()) {
667                 printf("FAILED, returned false\n");
668                 return false;
669         }
670         printf("OK\n");
671
672         printf("testing set_metadata_respond(VORBIS_COMMENT)... ");
673         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
674                 printf("FAILED, returned false\n");
675                 return false;
676         }
677         printf("OK\n");
678
679         num_expected_ = 0;
680         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
681
682         if(!decoder->test_respond())
683                 return false;
684
685         /*
686          * ignore all, respond APPLICATION
687          */
688
689         printf("testing set_metadata_ignore_all()... ");
690         if(!decoder->set_metadata_ignore_all()) {
691                 printf("FAILED, returned false\n");
692                 return false;
693         }
694         printf("OK\n");
695
696         printf("testing set_metadata_respond(APPLICATION)... ");
697         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
698                 printf("FAILED, returned false\n");
699                 return false;
700         }
701         printf("OK\n");
702
703         num_expected_ = 0;
704         expected_metadata_sequence_[num_expected_++] = &application1_;
705         expected_metadata_sequence_[num_expected_++] = &application2_;
706
707         if(!decoder->test_respond())
708                 return false;
709
710         /*
711          * ignore all, respond APPLICATION id of app#1
712          */
713
714         printf("testing set_metadata_ignore_all()... ");
715         if(!decoder->set_metadata_ignore_all()) {
716                 printf("FAILED, returned false\n");
717                 return false;
718         }
719         printf("OK\n");
720
721         printf("testing set_metadata_respond_application(of app block #1)... ");
722         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
723                 printf("FAILED, returned false\n");
724                 return false;
725         }
726         printf("OK\n");
727
728         num_expected_ = 0;
729         expected_metadata_sequence_[num_expected_++] = &application1_;
730
731         if(!decoder->test_respond())
732                 return false;
733
734         /*
735          * ignore all, respond APPLICATION id of app#1 & app#2
736          */
737
738         printf("testing set_metadata_ignore_all()... ");
739         if(!decoder->set_metadata_ignore_all()) {
740                 printf("FAILED, returned false\n");
741                 return false;
742         }
743         printf("OK\n");
744
745         printf("testing set_metadata_respond_application(of app block #1)... ");
746         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
747                 printf("FAILED, returned false\n");
748                 return false;
749         }
750         printf("OK\n");
751
752         printf("testing set_metadata_respond_application(of app block #2)... ");
753         if(!decoder->set_metadata_respond_application(application2_.data.application.id)) {
754                 printf("FAILED, returned false\n");
755                 return false;
756         }
757         printf("OK\n");
758
759         num_expected_ = 0;
760         expected_metadata_sequence_[num_expected_++] = &application1_;
761         expected_metadata_sequence_[num_expected_++] = &application2_;
762
763         if(!decoder->test_respond())
764                 return false;
765
766         /*
767          * respond all, ignore APPLICATION, respond APPLICATION id of app#1
768          */
769
770         printf("testing set_metadata_respond_all()... ");
771         if(!decoder->set_metadata_respond_all()) {
772                 printf("FAILED, returned false\n");
773                 return false;
774         }
775         printf("OK\n");
776
777         printf("testing set_metadata_ignore(APPLICATION)... ");
778         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
779                 printf("FAILED, returned false\n");
780                 return false;
781         }
782         printf("OK\n");
783
784         printf("testing set_metadata_respond_application(of app block #1)... ");
785         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
786                 printf("FAILED, returned false\n");
787                 return false;
788         }
789         printf("OK\n");
790
791         num_expected_ = 0;
792         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
793         expected_metadata_sequence_[num_expected_++] = &padding_;
794         expected_metadata_sequence_[num_expected_++] = &seektable_;
795         expected_metadata_sequence_[num_expected_++] = &application1_;
796         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
797
798         if(!decoder->test_respond())
799                 return false;
800
801         /*
802          * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
803          */
804
805         printf("testing set_metadata_ignore_all()... ");
806         if(!decoder->set_metadata_ignore_all()) {
807                 printf("FAILED, returned false\n");
808                 return false;
809         }
810         printf("OK\n");
811
812         printf("testing set_metadata_respond(APPLICATION)... ");
813         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
814                 printf("FAILED, returned false\n");
815                 return false;
816         }
817         printf("OK\n");
818
819         printf("testing set_metadata_ignore_application(of app block #1)... ");
820         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
821                 printf("FAILED, returned false\n");
822                 return false;
823         }
824         printf("OK\n");
825
826         num_expected_ = 0;
827         expected_metadata_sequence_[num_expected_++] = &application2_;
828
829         if(!decoder->test_respond())
830                 return false;
831
832         /* done, now leave the sequence the way we found it... */
833         num_expected_ = 0;
834         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
835         expected_metadata_sequence_[num_expected_++] = &padding_;
836         expected_metadata_sequence_[num_expected_++] = &seektable_;
837         expected_metadata_sequence_[num_expected_++] = &application1_;
838         expected_metadata_sequence_[num_expected_++] = &application2_;
839         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
840
841         ::fclose(decoder->file_);
842
843         printf("freeing decoder instance... ");
844         delete decoder;
845         printf("OK\n");
846
847         printf("\nPASSED!\n");
848
849         return true;
850 }
851
852 class SeekableStreamDecoder : public FLAC::Decoder::SeekableStream, public DecoderCommon {
853 public:
854         SeekableStreamDecoder(): FLAC::Decoder::SeekableStream(), DecoderCommon() { }
855         ~SeekableStreamDecoder() { }
856
857         // from FLAC::Decoder::SeekableStream
858         ::FLAC__SeekableStreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes);
859         ::FLAC__SeekableStreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
860         ::FLAC__SeekableStreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
861         ::FLAC__SeekableStreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length);
862         bool eof_callback();
863         ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
864         void metadata_callback(const ::FLAC__StreamMetadata *metadata);
865         void error_callback(::FLAC__StreamDecoderErrorStatus status);
866
867         bool die(const char *msg = 0) const;
868
869         bool test_respond();
870 };
871
872 ::FLAC__SeekableStreamDecoderReadStatus SeekableStreamDecoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
873 {
874         switch(common_read_callback_(buffer, bytes)) {
875                 case ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
876                         return ::FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
877                 case ::FLAC__STREAM_DECODER_READ_STATUS_ABORT:
878                 case ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
879                         return ::FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
880                 default:
881                         FLAC__ASSERT(0);
882                         return ::FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
883         }
884 }
885
886 ::FLAC__SeekableStreamDecoderSeekStatus SeekableStreamDecoder::seek_callback(FLAC__uint64 absolute_byte_offset)
887 {
888         if(error_occurred_)
889                 return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
890
891         if(::fseek(file_, (long)absolute_byte_offset, SEEK_SET) < 0) {
892                 error_occurred_ = true;
893                 return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
894         }
895
896         return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
897 }
898
899 ::FLAC__SeekableStreamDecoderTellStatus SeekableStreamDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
900 {
901         if(error_occurred_)
902                 return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
903
904         long offset = ::ftell(file_);
905         *absolute_byte_offset = (FLAC__uint64)offset;
906
907         if(offset < 0) {
908                 error_occurred_ = true;
909                 return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
910         }
911
912         return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
913 }
914
915 ::FLAC__SeekableStreamDecoderLengthStatus SeekableStreamDecoder::length_callback(FLAC__uint64 *stream_length)
916 {
917         if(error_occurred_)
918                 return ::FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
919
920         *stream_length = (FLAC__uint64)flacfilesize_;
921         return ::FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
922 }
923
924 bool SeekableStreamDecoder::eof_callback()
925 {
926         if(error_occurred_)
927                 return true;
928
929         return (bool)feof(file_);
930 }
931
932 ::FLAC__StreamDecoderWriteStatus SeekableStreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
933 {
934         (void)buffer;
935
936         return common_write_callback_(frame);
937 }
938
939 void SeekableStreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
940 {
941         common_metadata_callback_(metadata);
942 }
943
944 void SeekableStreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
945 {
946         common_error_callback_(status);
947 }
948
949 bool SeekableStreamDecoder::die(const char *msg) const
950 {
951         State state = get_state();
952
953         if(msg)
954                 printf("FAILED, %s", msg);
955         else
956                 printf("FAILED");
957
958         printf(", state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamDecoderState)state), state.as_cstring());
959         if(state == ::FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
960                 FLAC::Decoder::Stream::State state_ = get_stream_decoder_state();
961                 printf("      stream decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state_), state_.as_cstring());
962         }
963
964         return false;
965 }
966
967 bool SeekableStreamDecoder::test_respond()
968 {
969         if(!set_md5_checking(true)) {
970                 printf("FAILED at set_md5_checking(), returned false\n");
971                 return false;
972         }
973
974         printf("testing init()... ");
975         if(init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
976                 return die();
977         printf("OK\n");
978
979         current_metadata_number_ = 0;
980
981         if(::fseek(file_, 0, SEEK_SET) < 0) {
982                 printf("FAILED rewinding input, errno = %d\n", errno);
983                 return false;
984         }
985
986         printf("testing process_until_end_of_stream()... ");
987         if(!process_until_end_of_stream()) {
988                 State state = get_state();
989                 printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamDecoderState)state), state.as_cstring());
990                 return false;
991         }
992         printf("OK\n");
993
994         printf("testing finish()... ");
995         finish();
996         printf("OK\n");
997
998         return true;
999 }
1000
1001 static bool test_seekable_stream_decoder()
1002 {
1003         SeekableStreamDecoder *decoder;
1004
1005         printf("\n+++ libFLAC++ unit test: FLAC::Decoder::SeekableStream\n\n");
1006
1007         //
1008         // test new -> delete
1009         //
1010         printf("allocating decoder instance... ");
1011         decoder = new SeekableStreamDecoder();
1012         if(0 == decoder) {
1013                 printf("FAILED, new returned NULL\n");
1014                 return false;
1015         }
1016         printf("OK\n");
1017
1018         printf("testing is_valid()... ");
1019         if(!decoder->is_valid()) {
1020                 printf("FAILED, returned false\n");
1021                 return false;
1022         }
1023         printf("OK\n");
1024
1025         printf("freeing decoder instance... ");
1026         delete decoder;
1027         printf("OK\n");
1028
1029         //
1030         // test new -> init -> delete
1031         //
1032         printf("allocating decoder instance... ");
1033         decoder = new SeekableStreamDecoder();
1034         if(0 == decoder) {
1035                 printf("FAILED, new returned NULL\n");
1036                 return false;
1037         }
1038         printf("OK\n");
1039
1040         printf("testing is_valid()... ");
1041         if(!decoder->is_valid()) {
1042                 printf("FAILED, returned false\n");
1043                 return false;
1044         }
1045         printf("OK\n");
1046
1047         printf("testing init()... ");
1048         if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1049                 return decoder->die();
1050         printf("OK\n");
1051
1052         printf("freeing decoder instance... ");
1053         delete decoder;
1054         printf("OK\n");
1055
1056         //
1057         // test normal usage
1058         //
1059         num_expected_ = 0;
1060         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1061
1062         printf("allocating decoder instance... ");
1063         decoder = new SeekableStreamDecoder();
1064         if(0 == decoder) {
1065                 printf("FAILED, new returned NULL\n");
1066                 return false;
1067         }
1068         printf("OK\n");
1069
1070         printf("testing is_valid()... ");
1071         if(!decoder->is_valid()) {
1072                 printf("FAILED, returned false\n");
1073                 return false;
1074         }
1075         printf("OK\n");
1076
1077         printf("testing set_md5_checking()... ");
1078         if(!decoder->set_md5_checking(true)) {
1079                 printf("FAILED, returned false\n");
1080                 return false;
1081         }
1082         printf("OK\n");
1083
1084         printf("testing init()... ");
1085         if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1086                 return decoder->die();
1087         printf("OK\n");
1088
1089         printf("testing get_state()... ");
1090         FLAC::Decoder::SeekableStream::State state = decoder->get_state();
1091         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__SeekableStreamDecoderState)state), state.as_cstring());
1092
1093         printf("testing get_stream_decoder_state()... ");
1094         FLAC::Decoder::Stream::State state_ = decoder->get_stream_decoder_state();
1095         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)state_), state_.as_cstring());
1096
1097         decoder->current_metadata_number_ = 0;
1098         decoder->ignore_errors_ = false;
1099         decoder->error_occurred_ = false;
1100
1101         printf("opening FLAC file... ");
1102         decoder->file_ = ::fopen(flacfilename_, "rb");
1103         if(0 == decoder->file_) {
1104                 printf("ERROR\n");
1105                 return false;
1106         }
1107         printf("OK\n");
1108
1109         printf("testing get_md5_checking()... ");
1110         if(!decoder->get_md5_checking()) {
1111                 printf("FAILED, returned false, expected true\n");
1112                 return false;
1113         }
1114         printf("OK\n");
1115
1116         printf("testing process_until_end_of_metadata()... ");
1117         if(!decoder->process_until_end_of_metadata())
1118                 return decoder->die("returned false");
1119         printf("OK\n");
1120
1121         printf("testing process_single()... ");
1122         if(!decoder->process_single())
1123                 return decoder->die("returned false");
1124         printf("OK\n");
1125
1126         printf("testing flush()... ");
1127         if(!decoder->flush())
1128                 return decoder->die("returned false");
1129         printf("OK\n");
1130
1131         decoder->ignore_errors_ = true;
1132         printf("testing process_single()... ");
1133         if(!decoder->process_single())
1134                 return decoder->die("returned false");
1135         printf("OK\n");
1136         decoder->ignore_errors_ = false;
1137
1138         printf("testing seek_absolute()... ");
1139         if(!decoder->seek_absolute(0))
1140                 return decoder->die("returned false");
1141         printf("OK\n");
1142
1143         printf("testing process_until_end_of_stream()... ");
1144         if(!decoder->process_until_end_of_stream())
1145                 return decoder->die("returned false");
1146         printf("OK\n");
1147
1148         printf("testing get_channels()... ");
1149         {
1150                 unsigned channels = decoder->get_channels();
1151                 if(channels != streaminfo_.data.stream_info.channels) {
1152                         printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
1153                         return false;
1154                 }
1155         }
1156         printf("OK\n");
1157
1158         printf("testing get_bits_per_sample()... ");
1159         {
1160                 unsigned bits_per_sample = decoder->get_bits_per_sample();
1161                 if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
1162                         printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
1163                         return false;
1164                 }
1165         }
1166         printf("OK\n");
1167
1168         printf("testing get_sample_rate()... ");
1169         {
1170                 unsigned sample_rate = decoder->get_sample_rate();
1171                 if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
1172                         printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
1173                         return false;
1174                 }
1175         }
1176         printf("OK\n");
1177
1178         printf("testing get_blocksize()... ");
1179         {
1180                 unsigned blocksize = decoder->get_blocksize();
1181                 /* value could be anything since we're at the last block, so accept any answer */
1182                 printf("returned %u... OK\n", blocksize);
1183         }
1184
1185         printf("testing get_channel_assignment()... ");
1186         {
1187                 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
1188                 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
1189         }
1190
1191         printf("testing reset()... ");
1192         if(!decoder->reset())
1193                 return decoder->die("returned false");
1194         printf("OK\n");
1195
1196         decoder->current_metadata_number_ = 0;
1197
1198         printf("rewinding input... ");
1199         if(::fseek(decoder->file_, 0, SEEK_SET) < 0) {
1200                 printf("FAILED, errno = %d\n", errno);
1201                 return false;
1202         }
1203         printf("OK\n");
1204
1205         printf("testing process_until_end_of_stream()... ");
1206         if(!decoder->process_until_end_of_stream())
1207                 return decoder->die("returned false");
1208         printf("OK\n");
1209
1210         printf("testing finish()... ");
1211         decoder->finish();
1212         printf("OK\n");
1213
1214         /*
1215          * respond all
1216          */
1217
1218         printf("testing set_metadata_respond_all()... ");
1219         if(!decoder->set_metadata_respond_all()) {
1220                 printf("FAILED, returned false\n");
1221                 return false;
1222         }
1223         printf("OK\n");
1224
1225         num_expected_ = 0;
1226         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1227         expected_metadata_sequence_[num_expected_++] = &padding_;
1228         expected_metadata_sequence_[num_expected_++] = &seektable_;
1229         expected_metadata_sequence_[num_expected_++] = &application1_;
1230         expected_metadata_sequence_[num_expected_++] = &application2_;
1231         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1232
1233         if(!decoder->test_respond())
1234                 return false;
1235
1236         /*
1237          * ignore all
1238          */
1239
1240         printf("testing set_metadata_ignore_all()... ");
1241         if(!decoder->set_metadata_ignore_all()) {
1242                 printf("FAILED, returned false\n");
1243                 return false;
1244         }
1245         printf("OK\n");
1246
1247         num_expected_ = 0;
1248
1249         if(!decoder->test_respond())
1250                 return false;
1251
1252         /*
1253          * respond all, ignore VORBIS_COMMENT
1254          */
1255
1256         printf("testing set_metadata_respond_all()... ");
1257         if(!decoder->set_metadata_respond_all()) {
1258                 printf("FAILED, returned false\n");
1259                 return false;
1260         }
1261         printf("OK\n");
1262
1263         printf("testing set_metadata_ignore(VORBIS_COMMENT)... ");
1264         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
1265                 printf("FAILED, returned false\n");
1266                 return false;
1267         }
1268         printf("OK\n");
1269
1270         num_expected_ = 0;
1271         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1272         expected_metadata_sequence_[num_expected_++] = &padding_;
1273         expected_metadata_sequence_[num_expected_++] = &seektable_;
1274         expected_metadata_sequence_[num_expected_++] = &application1_;
1275         expected_metadata_sequence_[num_expected_++] = &application2_;
1276
1277         if(!decoder->test_respond())
1278                 return false;
1279
1280         /*
1281          * respond all, ignore APPLICATION
1282          */
1283
1284         printf("testing set_metadata_respond_all()... ");
1285         if(!decoder->set_metadata_respond_all()) {
1286                 printf("FAILED, returned false\n");
1287                 return false;
1288         }
1289         printf("OK\n");
1290
1291         printf("testing set_metadata_ignore(APPLICATION)... ");
1292         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1293                 printf("FAILED, returned false\n");
1294                 return false;
1295         }
1296         printf("OK\n");
1297
1298         num_expected_ = 0;
1299         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1300         expected_metadata_sequence_[num_expected_++] = &padding_;
1301         expected_metadata_sequence_[num_expected_++] = &seektable_;
1302         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1303
1304         if(!decoder->test_respond())
1305                 return false;
1306
1307         /*
1308          * respond all, ignore APPLICATION id of app#1
1309          */
1310
1311         printf("testing set_metadata_respond_all()... ");
1312         if(!decoder->set_metadata_respond_all()) {
1313                 printf("FAILED, returned false\n");
1314                 return false;
1315         }
1316         printf("OK\n");
1317
1318         printf("testing set_metadata_ignore_application(of app block #1)... ");
1319         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1320                 printf("FAILED, returned false\n");
1321                 return false;
1322         }
1323         printf("OK\n");
1324
1325         num_expected_ = 0;
1326         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1327         expected_metadata_sequence_[num_expected_++] = &padding_;
1328         expected_metadata_sequence_[num_expected_++] = &seektable_;
1329         expected_metadata_sequence_[num_expected_++] = &application2_;
1330         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1331
1332         if(!decoder->test_respond())
1333                 return false;
1334
1335         /*
1336          * respond all, ignore APPLICATION id of app#1 & app#2
1337          */
1338
1339         printf("testing set_metadata_respond_all()... ");
1340         if(!decoder->set_metadata_respond_all()) {
1341                 printf("FAILED, returned false\n");
1342                 return false;
1343         }
1344         printf("OK\n");
1345
1346         printf("testing set_metadata_ignore_application(of app block #1)... ");
1347         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1348                 printf("FAILED, returned false\n");
1349                 return false;
1350         }
1351         printf("OK\n");
1352
1353         printf("testing set_metadata_ignore_application(of app block #2)... ");
1354         if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) {
1355                 printf("FAILED, returned false\n");
1356                 return false;
1357         }
1358         printf("OK\n");
1359
1360         num_expected_ = 0;
1361         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1362         expected_metadata_sequence_[num_expected_++] = &padding_;
1363         expected_metadata_sequence_[num_expected_++] = &seektable_;
1364         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1365
1366         if(!decoder->test_respond())
1367                 return false;
1368
1369         /*
1370          * ignore all, respond VORBIS_COMMENT
1371          */
1372
1373         printf("testing set_metadata_ignore_all()... ");
1374         if(!decoder->set_metadata_ignore_all()) {
1375                 printf("FAILED, returned false\n");
1376                 return false;
1377         }
1378         printf("OK\n");
1379
1380         printf("testing set_metadata_respond(VORBIS_COMMENT)... ");
1381         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
1382                 printf("FAILED, returned false\n");
1383                 return false;
1384         }
1385         printf("OK\n");
1386
1387         num_expected_ = 0;
1388         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1389
1390         if(!decoder->test_respond())
1391                 return false;
1392
1393         /*
1394          * ignore all, respond APPLICATION
1395          */
1396
1397         printf("testing set_metadata_ignore_all()... ");
1398         if(!decoder->set_metadata_ignore_all()) {
1399                 printf("FAILED, returned false\n");
1400                 return false;
1401         }
1402         printf("OK\n");
1403
1404         printf("testing set_metadata_respond(APPLICATION)... ");
1405         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1406                 printf("FAILED, returned false\n");
1407                 return false;
1408         }
1409         printf("OK\n");
1410
1411         num_expected_ = 0;
1412         expected_metadata_sequence_[num_expected_++] = &application1_;
1413         expected_metadata_sequence_[num_expected_++] = &application2_;
1414
1415         if(!decoder->test_respond())
1416                 return false;
1417
1418         /*
1419          * ignore all, respond APPLICATION id of app#1
1420          */
1421
1422         printf("testing set_metadata_ignore_all()... ");
1423         if(!decoder->set_metadata_ignore_all()) {
1424                 printf("FAILED, returned false\n");
1425                 return false;
1426         }
1427         printf("OK\n");
1428
1429         printf("testing set_metadata_respond_application(of app block #1)... ");
1430         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1431                 printf("FAILED, returned false\n");
1432                 return false;
1433         }
1434         printf("OK\n");
1435
1436         num_expected_ = 0;
1437         expected_metadata_sequence_[num_expected_++] = &application1_;
1438
1439         if(!decoder->test_respond())
1440                 return false;
1441
1442         /*
1443          * ignore all, respond APPLICATION id of app#1 & app#2
1444          */
1445
1446         printf("testing set_metadata_ignore_all()... ");
1447         if(!decoder->set_metadata_ignore_all()) {
1448                 printf("FAILED, returned false\n");
1449                 return false;
1450         }
1451         printf("OK\n");
1452
1453         printf("testing set_metadata_respond_application(of app block #1)... ");
1454         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1455                 printf("FAILED, returned false\n");
1456                 return false;
1457         }
1458         printf("OK\n");
1459
1460         printf("testing set_metadata_respond_application(of app block #2)... ");
1461         if(!decoder->set_metadata_respond_application(application2_.data.application.id)) {
1462                 printf("FAILED, returned false\n");
1463                 return false;
1464         }
1465         printf("OK\n");
1466
1467         num_expected_ = 0;
1468         expected_metadata_sequence_[num_expected_++] = &application1_;
1469         expected_metadata_sequence_[num_expected_++] = &application2_;
1470
1471         if(!decoder->test_respond())
1472                 return false;
1473
1474         /*
1475          * respond all, ignore APPLICATION, respond APPLICATION id of app#1
1476          */
1477
1478         printf("testing set_metadata_respond_all()... ");
1479         if(!decoder->set_metadata_respond_all()) {
1480                 printf("FAILED, returned false\n");
1481                 return false;
1482         }
1483         printf("OK\n");
1484
1485         printf("testing set_metadata_ignore(APPLICATION)... ");
1486         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1487                 printf("FAILED, returned false\n");
1488                 return false;
1489         }
1490         printf("OK\n");
1491
1492         printf("testing set_metadata_respond_application(of app block #1)... ");
1493         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1494                 printf("FAILED, returned false\n");
1495                 return false;
1496         }
1497         printf("OK\n");
1498
1499         num_expected_ = 0;
1500         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1501         expected_metadata_sequence_[num_expected_++] = &padding_;
1502         expected_metadata_sequence_[num_expected_++] = &seektable_;
1503         expected_metadata_sequence_[num_expected_++] = &application1_;
1504         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1505
1506         if(!decoder->test_respond())
1507                 return false;
1508
1509         /*
1510          * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1511          */
1512
1513         printf("testing set_metadata_ignore_all()... ");
1514         if(!decoder->set_metadata_ignore_all()) {
1515                 printf("FAILED, returned false\n");
1516                 return false;
1517         }
1518         printf("OK\n");
1519
1520         printf("testing set_metadata_respond(APPLICATION)... ");
1521         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1522                 printf("FAILED, returned false\n");
1523                 return false;
1524         }
1525         printf("OK\n");
1526
1527         printf("testing set_metadata_ignore_application(of app block #1)... ");
1528         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1529                 printf("FAILED, returned false\n");
1530                 return false;
1531         }
1532         printf("OK\n");
1533
1534         num_expected_ = 0;
1535         expected_metadata_sequence_[num_expected_++] = &application2_;
1536
1537         if(!decoder->test_respond())
1538                 return false;
1539
1540         /* done, now leave the sequence the way we found it... */
1541         num_expected_ = 0;
1542         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1543         expected_metadata_sequence_[num_expected_++] = &padding_;
1544         expected_metadata_sequence_[num_expected_++] = &seektable_;
1545         expected_metadata_sequence_[num_expected_++] = &application1_;
1546         expected_metadata_sequence_[num_expected_++] = &application2_;
1547         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1548
1549         ::fclose(decoder->file_);
1550
1551         printf("freeing decoder instance... ");
1552         delete decoder;
1553         printf("OK\n");
1554
1555         printf("\nPASSED!\n");
1556
1557         return true;
1558 }
1559
1560 class FileDecoder : public FLAC::Decoder::File, public DecoderCommon {
1561 public:
1562         FileDecoder(): FLAC::Decoder::File(), DecoderCommon() { }
1563         ~FileDecoder() { }
1564
1565         // from FLAC::Decoder::File
1566         ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
1567         void metadata_callback(const ::FLAC__StreamMetadata *metadata);
1568         void error_callback(::FLAC__StreamDecoderErrorStatus status);
1569
1570         bool die(const char *msg = 0) const;
1571
1572         bool test_respond();
1573 };
1574
1575 ::FLAC__StreamDecoderWriteStatus FileDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
1576 {
1577         (void)buffer;
1578         return common_write_callback_(frame);
1579 }
1580
1581 void FileDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
1582 {
1583         common_metadata_callback_(metadata);
1584 }
1585
1586 void FileDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
1587 {
1588         common_error_callback_(status);
1589 }
1590
1591 bool FileDecoder::die(const char *msg) const
1592 {
1593         State state = get_state();
1594
1595         if(msg)
1596                 printf("FAILED, %s", msg);
1597         else
1598                 printf("FAILED");
1599
1600         printf(", state = %u (%s)\n", (unsigned)((::FLAC__FileDecoderState)state), state.as_cstring());
1601         if(state == ::FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
1602                 FLAC::Decoder::SeekableStream::State state_ = get_seekable_stream_decoder_state();
1603                 printf("      seekable stream decoder state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamDecoderState)state_), state_.as_cstring());
1604                 if(state_ == ::FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR) {
1605                         FLAC::Decoder::Stream::State state__ = get_stream_decoder_state();
1606                         printf("      stream decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state__), state__.as_cstring());
1607                 }
1608         }
1609
1610         return false;
1611 }
1612
1613 bool FileDecoder::test_respond()
1614 {
1615         if(!set_filename(flacfilename_)) {
1616                 printf("FAILED at set_filename(), returned false\n");
1617                 return false;
1618         }
1619
1620         if(!set_md5_checking(true)) {
1621                 printf("FAILED at set_md5_checking(), returned false\n");
1622                 return false;
1623         }
1624
1625         printf("testing init()... ");
1626         if(init() != ::FLAC__FILE_DECODER_OK)
1627                 return die();
1628         printf("OK\n");
1629
1630         current_metadata_number_ = 0;
1631
1632         printf("testing process_until_end_of_file()... ");
1633         if(!process_until_end_of_file()) {
1634                 State state = get_state();
1635                 printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__FileDecoderState)state), state.as_cstring());
1636                 return false;
1637         }
1638         printf("OK\n");
1639
1640         printf("testing finish()... ");
1641         finish();
1642         printf("OK\n");
1643
1644         return true;
1645 }
1646
1647 static bool test_file_decoder()
1648 {
1649         FileDecoder *decoder;
1650
1651         printf("\n+++ libFLAC++ unit test: FLAC::Decoder::File\n\n");
1652
1653         //
1654         // test new -> delete
1655         //
1656         printf("allocating decoder instance... ");
1657         decoder = new FileDecoder();
1658         if(0 == decoder) {
1659                 printf("FAILED, new returned NULL\n");
1660                 return false;
1661         }
1662         printf("OK\n");
1663
1664         printf("testing is_valid()... ");
1665         if(!decoder->is_valid()) {
1666                 printf("FAILED, returned false\n");
1667                 return false;
1668         }
1669         printf("OK\n");
1670
1671         printf("freeing decoder instance... ");
1672         delete decoder;
1673         printf("OK\n");
1674
1675         //
1676         // test new -> init -> delete
1677         //
1678         printf("allocating decoder instance... ");
1679         decoder = new FileDecoder();
1680         if(0 == decoder) {
1681                 printf("FAILED, new returned NULL\n");
1682                 return false;
1683         }
1684         printf("OK\n");
1685
1686         printf("testing is_valid()... ");
1687         if(!decoder->is_valid()) {
1688                 printf("FAILED, returned false\n");
1689                 return false;
1690         }
1691         printf("OK\n");
1692
1693         printf("testing init()... ");
1694         if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1695                 return decoder->die();
1696         printf("OK\n");
1697
1698         printf("freeing decoder instance... ");
1699         delete decoder;
1700         printf("OK\n");
1701
1702         //
1703         // test normal usage
1704         //
1705         num_expected_ = 0;
1706         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1707
1708         printf("allocating decoder instance... ");
1709         decoder = new FileDecoder();
1710         if(0 == decoder) {
1711                 printf("FAILED, new returned NULL\n");
1712                 return false;
1713         }
1714         printf("OK\n");
1715
1716         printf("testing is_valid()... ");
1717         if(!decoder->is_valid()) {
1718                 printf("FAILED, returned false\n");
1719                 return false;
1720         }
1721         printf("OK\n");
1722
1723         printf("testing set_filename()... ");
1724         if(!decoder->set_filename(flacfilename_)) {
1725                 printf("FAILED, returned false\n");
1726                 return false;
1727         }
1728         printf("OK\n");
1729
1730         printf("testing set_md5_checking()... ");
1731         if(!decoder->set_md5_checking(true)) {
1732                 printf("FAILED, returned false\n");
1733                 return false;
1734         }
1735         printf("OK\n");
1736
1737         printf("testing init()... ");
1738         if(decoder->init() != ::FLAC__FILE_DECODER_OK)
1739                 return decoder->die();
1740         printf("OK\n");
1741
1742         printf("testing get_state()... ");
1743         FLAC::Decoder::File::State state = decoder->get_state();
1744         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__FileDecoderState)state), state.as_cstring());
1745
1746         printf("testing get_seekable_stream_decoder_state()... ");
1747         FLAC::Decoder::SeekableStream::State state_ = decoder->get_seekable_stream_decoder_state();
1748         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__SeekableStreamDecoderState)state_), state_.as_cstring());
1749
1750         printf("testing get_stream_decoder_state()... ");
1751         FLAC::Decoder::Stream::State state__ = decoder->get_stream_decoder_state();
1752         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)state__), state__.as_cstring());
1753
1754         decoder->current_metadata_number_ = 0;
1755         decoder->ignore_errors_ = false;
1756         decoder->error_occurred_ = false;
1757
1758         printf("testing get_md5_checking()... ");
1759         if(!decoder->get_md5_checking()) {
1760                 printf("FAILED, returned false, expected true\n");
1761                 return false;
1762         }
1763         printf("OK\n");
1764
1765         printf("testing process_until_end_of_metadata()... ");
1766         if(!decoder->process_until_end_of_metadata())
1767                 return decoder->die("returned false");
1768         printf("OK\n");
1769
1770         printf("testing process_single()... ");
1771         if(!decoder->process_single())
1772                 return decoder->die("returned false");
1773         printf("OK\n");
1774
1775         printf("testing seek_absolute()... ");
1776         if(!decoder->seek_absolute(0))
1777                 return decoder->die("returned false");
1778         printf("OK\n");
1779
1780         printf("testing process_until_end_of_file()... ");
1781         if(!decoder->process_until_end_of_file())
1782                 return decoder->die("returned false");
1783         printf("OK\n");
1784
1785         printf("testing get_channels()... ");
1786         {
1787                 unsigned channels = decoder->get_channels();
1788                 if(channels != streaminfo_.data.stream_info.channels) {
1789                         printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
1790                         return false;
1791                 }
1792         }
1793         printf("OK\n");
1794
1795         printf("testing get_bits_per_sample()... ");
1796         {
1797                 unsigned bits_per_sample = decoder->get_bits_per_sample();
1798                 if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
1799                         printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
1800                         return false;
1801                 }
1802         }
1803         printf("OK\n");
1804
1805         printf("testing get_sample_rate()... ");
1806         {
1807                 unsigned sample_rate = decoder->get_sample_rate();
1808                 if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
1809                         printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
1810                         return false;
1811                 }
1812         }
1813         printf("OK\n");
1814
1815         printf("testing get_blocksize()... ");
1816         {
1817                 unsigned blocksize = decoder->get_blocksize();
1818                 /* value could be anything since we're at the last block, so accept any answer */
1819                 printf("returned %u... OK\n", blocksize);
1820         }
1821
1822         printf("testing get_channel_assignment()... ");
1823         {
1824                 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
1825                 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
1826         }
1827
1828         printf("testing finish()... ");
1829         decoder->finish();
1830         printf("OK\n");
1831
1832         /*
1833          * respond all
1834          */
1835
1836         printf("testing set_metadata_respond_all()... ");
1837         if(!decoder->set_metadata_respond_all()) {
1838                 printf("FAILED, returned false\n");
1839                 return false;
1840         }
1841         printf("OK\n");
1842
1843         num_expected_ = 0;
1844         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1845         expected_metadata_sequence_[num_expected_++] = &padding_;
1846         expected_metadata_sequence_[num_expected_++] = &seektable_;
1847         expected_metadata_sequence_[num_expected_++] = &application1_;
1848         expected_metadata_sequence_[num_expected_++] = &application2_;
1849         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1850
1851         if(!decoder->test_respond())
1852                 return false;
1853
1854         /*
1855          * ignore all
1856          */
1857
1858         printf("testing set_metadata_ignore_all()... ");
1859         if(!decoder->set_metadata_ignore_all()) {
1860                 printf("FAILED, returned false\n");
1861                 return false;
1862         }
1863         printf("OK\n");
1864
1865         num_expected_ = 0;
1866
1867         if(!decoder->test_respond())
1868                 return false;
1869
1870         /*
1871          * respond all, ignore VORBIS_COMMENT
1872          */
1873
1874         printf("testing set_metadata_respond_all()... ");
1875         if(!decoder->set_metadata_respond_all()) {
1876                 printf("FAILED, returned false\n");
1877                 return false;
1878         }
1879         printf("OK\n");
1880
1881         printf("testing set_metadata_ignore(VORBIS_COMMENT)... ");
1882         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
1883                 printf("FAILED, returned false\n");
1884                 return false;
1885         }
1886         printf("OK\n");
1887
1888         num_expected_ = 0;
1889         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1890         expected_metadata_sequence_[num_expected_++] = &padding_;
1891         expected_metadata_sequence_[num_expected_++] = &seektable_;
1892         expected_metadata_sequence_[num_expected_++] = &application1_;
1893         expected_metadata_sequence_[num_expected_++] = &application2_;
1894
1895         if(!decoder->test_respond())
1896                 return false;
1897
1898         /*
1899          * respond all, ignore APPLICATION
1900          */
1901
1902         printf("testing set_metadata_respond_all()... ");
1903         if(!decoder->set_metadata_respond_all()) {
1904                 printf("FAILED, returned false\n");
1905                 return false;
1906         }
1907         printf("OK\n");
1908
1909         printf("testing set_metadata_ignore(APPLICATION)... ");
1910         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1911                 printf("FAILED, returned false\n");
1912                 return false;
1913         }
1914         printf("OK\n");
1915
1916         num_expected_ = 0;
1917         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1918         expected_metadata_sequence_[num_expected_++] = &padding_;
1919         expected_metadata_sequence_[num_expected_++] = &seektable_;
1920         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1921
1922         if(!decoder->test_respond())
1923                 return false;
1924
1925         /*
1926          * respond all, ignore APPLICATION id of app#1
1927          */
1928
1929         printf("testing set_metadata_respond_all()... ");
1930         if(!decoder->set_metadata_respond_all()) {
1931                 printf("FAILED, returned false\n");
1932                 return false;
1933         }
1934         printf("OK\n");
1935
1936         printf("testing set_metadata_ignore_application(of app block #1)... ");
1937         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1938                 printf("FAILED, returned false\n");
1939                 return false;
1940         }
1941         printf("OK\n");
1942
1943         num_expected_ = 0;
1944         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1945         expected_metadata_sequence_[num_expected_++] = &padding_;
1946         expected_metadata_sequence_[num_expected_++] = &seektable_;
1947         expected_metadata_sequence_[num_expected_++] = &application2_;
1948         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1949
1950         if(!decoder->test_respond())
1951                 return false;
1952
1953         /*
1954          * respond all, ignore APPLICATION id of app#1 & app#2
1955          */
1956
1957         printf("testing set_metadata_respond_all()... ");
1958         if(!decoder->set_metadata_respond_all()) {
1959                 printf("FAILED, returned false\n");
1960                 return false;
1961         }
1962         printf("OK\n");
1963
1964         printf("testing set_metadata_ignore_application(of app block #1)... ");
1965         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1966                 printf("FAILED, returned false\n");
1967                 return false;
1968         }
1969         printf("OK\n");
1970
1971         printf("testing set_metadata_ignore_application(of app block #2)... ");
1972         if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) {
1973                 printf("FAILED, returned false\n");
1974                 return false;
1975         }
1976         printf("OK\n");
1977
1978         num_expected_ = 0;
1979         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1980         expected_metadata_sequence_[num_expected_++] = &padding_;
1981         expected_metadata_sequence_[num_expected_++] = &seektable_;
1982         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1983
1984         if(!decoder->test_respond())
1985                 return false;
1986
1987         /*
1988          * ignore all, respond VORBIS_COMMENT
1989          */
1990
1991         printf("testing set_metadata_ignore_all()... ");
1992         if(!decoder->set_metadata_ignore_all()) {
1993                 printf("FAILED, returned false\n");
1994                 return false;
1995         }
1996         printf("OK\n");
1997
1998         printf("testing set_metadata_respond(VORBIS_COMMENT)... ");
1999         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
2000                 printf("FAILED, returned false\n");
2001                 return false;
2002         }
2003         printf("OK\n");
2004
2005         num_expected_ = 0;
2006         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
2007
2008         if(!decoder->test_respond())
2009                 return false;
2010
2011         /*
2012          * ignore all, respond APPLICATION
2013          */
2014
2015         printf("testing set_metadata_ignore_all()... ");
2016         if(!decoder->set_metadata_ignore_all()) {
2017                 printf("FAILED, returned false\n");
2018                 return false;
2019         }
2020         printf("OK\n");
2021
2022         printf("testing set_metadata_respond(APPLICATION)... ");
2023         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
2024                 printf("FAILED, returned false\n");
2025                 return false;
2026         }
2027         printf("OK\n");
2028
2029         num_expected_ = 0;
2030         expected_metadata_sequence_[num_expected_++] = &application1_;
2031         expected_metadata_sequence_[num_expected_++] = &application2_;
2032
2033         if(!decoder->test_respond())
2034                 return false;
2035
2036         /*
2037          * ignore all, respond APPLICATION id of app#1
2038          */
2039
2040         printf("testing set_metadata_ignore_all()... ");
2041         if(!decoder->set_metadata_ignore_all()) {
2042                 printf("FAILED, returned false\n");
2043                 return false;
2044         }
2045         printf("OK\n");
2046
2047         printf("testing set_metadata_respond_application(of app block #1)... ");
2048         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
2049                 printf("FAILED, returned false\n");
2050                 return false;
2051         }
2052         printf("OK\n");
2053
2054         num_expected_ = 0;
2055         expected_metadata_sequence_[num_expected_++] = &application1_;
2056
2057         if(!decoder->test_respond())
2058                 return false;
2059
2060         /*
2061          * ignore all, respond APPLICATION id of app#1 & app#2
2062          */
2063
2064         printf("testing set_metadata_ignore_all()... ");
2065         if(!decoder->set_metadata_ignore_all()) {
2066                 printf("FAILED, returned false\n");
2067                 return false;
2068         }
2069         printf("OK\n");
2070
2071         printf("testing set_metadata_respond_application(of app block #1)... ");
2072         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
2073                 printf("FAILED, returned false\n");
2074                 return false;
2075         }
2076         printf("OK\n");
2077
2078         printf("testing set_metadata_respond_application(of app block #2)... ");
2079         if(!decoder->set_metadata_respond_application(application2_.data.application.id)) {
2080                 printf("FAILED, returned false\n");
2081                 return false;
2082         }
2083         printf("OK\n");
2084
2085         num_expected_ = 0;
2086         expected_metadata_sequence_[num_expected_++] = &application1_;
2087         expected_metadata_sequence_[num_expected_++] = &application2_;
2088
2089         if(!decoder->test_respond())
2090                 return false;
2091
2092         /*
2093          * respond all, ignore APPLICATION, respond APPLICATION id of app#1
2094          */
2095
2096         printf("testing set_metadata_respond_all()... ");
2097         if(!decoder->set_metadata_respond_all()) {
2098                 printf("FAILED, returned false\n");
2099                 return false;
2100         }
2101         printf("OK\n");
2102
2103         printf("testing set_metadata_ignore(APPLICATION)... ");
2104         if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
2105                 printf("FAILED, returned false\n");
2106                 return false;
2107         }
2108         printf("OK\n");
2109
2110         printf("testing set_metadata_respond_application(of app block #1)... ");
2111         if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
2112                 printf("FAILED, returned false\n");
2113                 return false;
2114         }
2115         printf("OK\n");
2116
2117         num_expected_ = 0;
2118         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
2119         expected_metadata_sequence_[num_expected_++] = &padding_;
2120         expected_metadata_sequence_[num_expected_++] = &seektable_;
2121         expected_metadata_sequence_[num_expected_++] = &application1_;
2122         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
2123
2124         if(!decoder->test_respond())
2125                 return false;
2126
2127         /*
2128          * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
2129          */
2130
2131         printf("testing set_metadata_ignore_all()... ");
2132         if(!decoder->set_metadata_ignore_all()) {
2133                 printf("FAILED, returned false\n");
2134                 return false;
2135         }
2136         printf("OK\n");
2137
2138         printf("testing set_metadata_respond(APPLICATION)... ");
2139         if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
2140                 printf("FAILED, returned false\n");
2141                 return false;
2142         }
2143         printf("OK\n");
2144
2145         printf("testing set_metadata_ignore_application(of app block #1)... ");
2146         if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
2147                 printf("FAILED, returned false\n");
2148                 return false;
2149         }
2150         printf("OK\n");
2151
2152         num_expected_ = 0;
2153         expected_metadata_sequence_[num_expected_++] = &application2_;
2154
2155         if(!decoder->test_respond())
2156                 return false;
2157
2158         /* done, now leave the sequence the way we found it... */
2159         num_expected_ = 0;
2160         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
2161         expected_metadata_sequence_[num_expected_++] = &padding_;
2162         expected_metadata_sequence_[num_expected_++] = &seektable_;
2163         expected_metadata_sequence_[num_expected_++] = &application1_;
2164         expected_metadata_sequence_[num_expected_++] = &application2_;
2165         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
2166
2167         printf("freeing decoder instance... ");
2168         delete decoder;
2169         printf("OK\n");
2170
2171         printf("\nPASSED!\n");
2172
2173         return true;
2174 }
2175
2176 bool test_decoders()
2177 {
2178         init_metadata_blocks_();
2179         if(!generate_file_())
2180                 return false;
2181
2182         if(!test_stream_decoder())
2183                 return false;
2184
2185         if(!test_seekable_stream_decoder())
2186                 return false;
2187
2188         if(!test_file_decoder())
2189                 return false;
2190
2191         (void) grabbag__file_remove_file(flacfilename_);
2192         free_metadata_blocks_();
2193
2194         return true;
2195 }