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