1 /* test_libFLAC++ - Unit tester for libFLAC++
2 * Copyright (C) 2002 Josh Coalson
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.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "file_utils.h"
23 #include "FLAC/assert.h"
24 #include "FLAC/metadata.h" // for ::FLAC__metadata_object_is_equal()
25 #include "FLAC++/decoder.h"
26 #include "share/file_utils.h"
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_;
38 static bool die_(const char *msg)
40 printf("ERROR: %s\n", msg);
44 static void *malloc_or_die_(size_t size)
46 void *x = malloc(size);
48 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
54 static void init_metadata_blocks_()
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
60 remember, the metadata interface gets tested after the decoders,
61 so we do all the metadata manipulation here without it.
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);
78 padding_.is_last = false;
79 padding_.type = ::FLAC__METADATA_TYPE_PADDING;
80 padding_.length = 1234;
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;
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);
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;
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;
125 static void free_metadata_blocks_()
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);
134 static bool generate_file_()
136 printf("\n\ngenerating FLAC file for decoder tests...\n");
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_;
145 if(!file_utils__generate_flacfile(flacfilename_, &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_))
146 return die_("creating the encoded file");
152 class DecoderCommon {
155 unsigned current_metadata_number_;
157 bool error_occurred_;
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);
166 ::FLAC__StreamDecoderReadStatus DecoderCommon::common_read_callback_(FLAC__byte buffer[], unsigned *bytes)
169 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
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) {
177 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
179 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
183 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
187 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
190 ::FLAC__StreamDecoderWriteStatus DecoderCommon::common_write_callback_(const ::FLAC__Frame *frame)
193 return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
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)
199 printf("content... ");
203 return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
206 void DecoderCommon::common_metadata_callback_(const ::FLAC__StreamMetadata *metadata)
211 printf("%d... ", current_metadata_number_);
214 if(current_metadata_number_ >= num_expected_) {
215 (void)die_("got more metadata blocks than expected");
216 error_occurred_ = true;
219 if(!::FLAC__metadata_object_is_equal(expected_metadata_sequence_[current_metadata_number_], metadata)) {
220 (void)die_("metadata block mismatch");
221 error_occurred_ = true;
224 current_metadata_number_++;
227 void DecoderCommon::common_error_callback_(::FLAC__StreamDecoderErrorStatus status)
229 if(!ignore_errors_) {
230 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, ::FLAC__StreamDecoderErrorStatusString[status]);
231 error_occurred_ = true;
235 class StreamDecoder : public FLAC::Decoder::Stream, public DecoderCommon {
237 StreamDecoder(): FLAC::Decoder::Stream(), DecoderCommon() { }
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);
246 bool die(const char *msg = 0) const;
251 ::FLAC__StreamDecoderReadStatus StreamDecoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
253 return common_read_callback_(buffer, bytes);
256 ::FLAC__StreamDecoderWriteStatus StreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
260 return common_write_callback_(frame);
263 void StreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
265 common_metadata_callback_(metadata);
268 void StreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
270 common_error_callback_(status);
273 bool StreamDecoder::die(const char *msg) const
275 State state = get_state();
278 printf("FAILED, %s", msg);
282 printf(", state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
287 bool StreamDecoder::test_respond()
289 printf("testing init()... ");
290 if(init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
294 current_metadata_number_ = 0;
296 if(::fseek(file_, 0, SEEK_SET) < 0) {
297 printf("FAILED rewinding input, errno = %d\n", errno);
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());
309 printf("testing finish()... ");
316 static bool test_stream_decoder()
318 StreamDecoder *decoder;
320 printf("\n+++ libFLAC++ unit test: FLAC::Decoder::Stream\n\n");
323 // test new -> delete
325 printf("allocating decoder instance... ");
326 decoder = new StreamDecoder();
328 printf("FAILED, new returned NULL\n");
333 printf("testing is_valid()... ");
334 if(!decoder->is_valid()) {
335 printf("FAILED, returned false\n");
340 printf("freeing decoder instance... ");
345 // test new -> init -> delete
347 printf("allocating decoder instance... ");
348 decoder = new StreamDecoder();
350 printf("FAILED, new returned NULL\n");
355 printf("testing is_valid()... ");
356 if(!decoder->is_valid()) {
357 printf("FAILED, returned false\n");
362 printf("testing init()... ");
363 if(decoder->init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
364 return decoder->die();
367 printf("freeing decoder instance... ");
375 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
377 printf("allocating decoder instance... ");
378 decoder = new StreamDecoder();
380 printf("FAILED, new returned NULL\n");
385 printf("testing is_valid()... ");
386 if(!decoder->is_valid()) {
387 printf("FAILED, returned false\n");
392 printf("testing init()... ");
393 if(decoder->init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
394 return decoder->die();
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());
401 decoder->current_metadata_number_ = 0;
402 decoder->ignore_errors_ = false;
403 decoder->error_occurred_ = false;
405 printf("opening FLAC file... ");
406 decoder->file_ = ::fopen(flacfilename_, "rb");
407 if(0 == decoder->file_) {
413 printf("testing process_until_end_of_metadata()... ");
414 if(!decoder->process_until_end_of_metadata())
415 return decoder->die("returned false");
418 printf("testing process_single()... ");
419 if(!decoder->process_single())
420 return decoder->die("returned false");
423 printf("testing flush()... ");
424 if(!decoder->flush())
425 return decoder->die("returned false");
428 decoder->ignore_errors_ = true;
429 printf("testing process_single()... ");
430 if(!decoder->process_single())
431 return decoder->die("returned false");
433 decoder->ignore_errors_ = false;
435 printf("testing process_until_end_of_stream()... ");
436 if(!decoder->process_until_end_of_stream())
437 return decoder->die("returned false");
440 printf("testing get_channels()... ");
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);
450 printf("testing get_bits_per_sample()... ");
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);
460 printf("testing get_sample_rate()... ");
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);
470 printf("testing get_blocksize()... ");
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);
477 printf("testing get_channel_assignment()... ");
479 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
480 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
483 printf("testing reset()... ");
484 if(!decoder->reset())
485 return decoder->die("returned false");
488 decoder->current_metadata_number_ = 0;
490 printf("rewinding input... ");
491 if(::fseek(decoder->file_, 0, SEEK_SET) < 0) {
492 printf("FAILED, errno = %d\n", errno);
497 printf("testing process_until_end_of_stream()... ");
498 if(!decoder->process_until_end_of_stream())
499 return decoder->die("returned false");
502 printf("testing finish()... ");
510 printf("testing set_metadata_respond_all()... ");
511 if(!decoder->set_metadata_respond_all()) {
512 printf("FAILED, returned false\n");
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_;
525 if(!decoder->test_respond())
532 printf("testing set_metadata_ignore_all()... ");
533 if(!decoder->set_metadata_ignore_all()) {
534 printf("FAILED, returned false\n");
541 if(!decoder->test_respond())
545 * respond all, ignore VORBIS_COMMENT
548 printf("testing set_metadata_respond_all()... ");
549 if(!decoder->set_metadata_respond_all()) {
550 printf("FAILED, returned false\n");
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");
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_;
569 if(!decoder->test_respond())
573 * respond all, ignore APPLICATION
576 printf("testing set_metadata_respond_all()... ");
577 if(!decoder->set_metadata_respond_all()) {
578 printf("FAILED, returned false\n");
583 printf("testing set_metadata_ignore(APPLICATION)... ");
584 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
585 printf("FAILED, returned false\n");
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_;
596 if(!decoder->test_respond())
600 * respond all, ignore APPLICATION id of app#1
603 printf("testing set_metadata_respond_all()... ");
604 if(!decoder->set_metadata_respond_all()) {
605 printf("FAILED, returned false\n");
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");
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_;
624 if(!decoder->test_respond())
628 * respond all, ignore APPLICATION id of app#1 & app#2
631 printf("testing set_metadata_respond_all()... ");
632 if(!decoder->set_metadata_respond_all()) {
633 printf("FAILED, returned false\n");
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");
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");
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_;
658 if(!decoder->test_respond())
662 * ignore all, respond VORBIS_COMMENT
665 printf("testing set_metadata_ignore_all()... ");
666 if(!decoder->set_metadata_ignore_all()) {
667 printf("FAILED, returned false\n");
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");
680 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
682 if(!decoder->test_respond())
686 * ignore all, respond APPLICATION
689 printf("testing set_metadata_ignore_all()... ");
690 if(!decoder->set_metadata_ignore_all()) {
691 printf("FAILED, returned false\n");
696 printf("testing set_metadata_respond(APPLICATION)... ");
697 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
698 printf("FAILED, returned false\n");
704 expected_metadata_sequence_[num_expected_++] = &application1_;
705 expected_metadata_sequence_[num_expected_++] = &application2_;
707 if(!decoder->test_respond())
711 * ignore all, respond APPLICATION id of app#1
714 printf("testing set_metadata_ignore_all()... ");
715 if(!decoder->set_metadata_ignore_all()) {
716 printf("FAILED, returned false\n");
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");
729 expected_metadata_sequence_[num_expected_++] = &application1_;
731 if(!decoder->test_respond())
735 * ignore all, respond APPLICATION id of app#1 & app#2
738 printf("testing set_metadata_ignore_all()... ");
739 if(!decoder->set_metadata_ignore_all()) {
740 printf("FAILED, returned false\n");
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");
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");
760 expected_metadata_sequence_[num_expected_++] = &application1_;
761 expected_metadata_sequence_[num_expected_++] = &application2_;
763 if(!decoder->test_respond())
767 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
770 printf("testing set_metadata_respond_all()... ");
771 if(!decoder->set_metadata_respond_all()) {
772 printf("FAILED, returned false\n");
777 printf("testing set_metadata_ignore(APPLICATION)... ");
778 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
779 printf("FAILED, returned false\n");
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");
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_;
798 if(!decoder->test_respond())
802 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
805 printf("testing set_metadata_ignore_all()... ");
806 if(!decoder->set_metadata_ignore_all()) {
807 printf("FAILED, returned false\n");
812 printf("testing set_metadata_respond(APPLICATION)... ");
813 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
814 printf("FAILED, returned false\n");
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");
827 expected_metadata_sequence_[num_expected_++] = &application2_;
829 if(!decoder->test_respond())
832 /* done, now leave the sequence the way we found it... */
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_;
841 ::fclose(decoder->file_);
843 printf("freeing decoder instance... ");
847 printf("\nPASSED!\n");
852 class SeekableStreamDecoder : public FLAC::Decoder::SeekableStream, public DecoderCommon {
854 SeekableStreamDecoder(): FLAC::Decoder::SeekableStream(), DecoderCommon() { }
855 ~SeekableStreamDecoder() { }
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);
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);
867 bool die(const char *msg = 0) const;
872 ::FLAC__SeekableStreamDecoderReadStatus SeekableStreamDecoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
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;
882 return ::FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
886 ::FLAC__SeekableStreamDecoderSeekStatus SeekableStreamDecoder::seek_callback(FLAC__uint64 absolute_byte_offset)
889 return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
891 if(::fseek(file_, (long)absolute_byte_offset, SEEK_SET) < 0) {
892 error_occurred_ = true;
893 return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
896 return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
899 ::FLAC__SeekableStreamDecoderTellStatus SeekableStreamDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
902 return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
904 long offset = ::ftell(file_);
905 *absolute_byte_offset = (FLAC__uint64)offset;
908 error_occurred_ = true;
909 return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
912 return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
915 ::FLAC__SeekableStreamDecoderLengthStatus SeekableStreamDecoder::length_callback(FLAC__uint64 *stream_length)
918 return ::FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
920 *stream_length = (FLAC__uint64)flacfilesize_;
921 return ::FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
924 bool SeekableStreamDecoder::eof_callback()
929 return (bool)feof(file_);
932 ::FLAC__StreamDecoderWriteStatus SeekableStreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
936 return common_write_callback_(frame);
939 void SeekableStreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
941 common_metadata_callback_(metadata);
944 void SeekableStreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
946 common_error_callback_(status);
949 bool SeekableStreamDecoder::die(const char *msg) const
951 State state = get_state();
954 printf("FAILED, %s", msg);
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());
967 bool SeekableStreamDecoder::test_respond()
969 if(!set_md5_checking(true)) {
970 printf("FAILED at set_md5_checking(), returned false\n");
974 printf("testing init()... ");
975 if(init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
979 current_metadata_number_ = 0;
981 if(::fseek(file_, 0, SEEK_SET) < 0) {
982 printf("FAILED rewinding input, errno = %d\n", errno);
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());
994 printf("testing finish()... ");
1001 static bool test_seekable_stream_decoder()
1003 SeekableStreamDecoder *decoder;
1005 printf("\n+++ libFLAC++ unit test: FLAC::Decoder::SeekableStream\n\n");
1008 // test new -> delete
1010 printf("allocating decoder instance... ");
1011 decoder = new SeekableStreamDecoder();
1013 printf("FAILED, new returned NULL\n");
1018 printf("testing is_valid()... ");
1019 if(!decoder->is_valid()) {
1020 printf("FAILED, returned false\n");
1025 printf("freeing decoder instance... ");
1030 // test new -> init -> delete
1032 printf("allocating decoder instance... ");
1033 decoder = new SeekableStreamDecoder();
1035 printf("FAILED, new returned NULL\n");
1040 printf("testing is_valid()... ");
1041 if(!decoder->is_valid()) {
1042 printf("FAILED, returned false\n");
1047 printf("testing init()... ");
1048 if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1049 return decoder->die();
1052 printf("freeing decoder instance... ");
1057 // test normal usage
1060 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1062 printf("allocating decoder instance... ");
1063 decoder = new SeekableStreamDecoder();
1065 printf("FAILED, new returned NULL\n");
1070 printf("testing is_valid()... ");
1071 if(!decoder->is_valid()) {
1072 printf("FAILED, returned false\n");
1077 printf("testing set_md5_checking()... ");
1078 if(!decoder->set_md5_checking(true)) {
1079 printf("FAILED, returned false\n");
1084 printf("testing init()... ");
1085 if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1086 return decoder->die();
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());
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());
1097 decoder->current_metadata_number_ = 0;
1098 decoder->ignore_errors_ = false;
1099 decoder->error_occurred_ = false;
1101 printf("opening FLAC file... ");
1102 decoder->file_ = ::fopen(flacfilename_, "rb");
1103 if(0 == decoder->file_) {
1109 printf("testing get_md5_checking()... ");
1110 if(!decoder->get_md5_checking()) {
1111 printf("FAILED, returned false, expected true\n");
1116 printf("testing process_until_end_of_metadata()... ");
1117 if(!decoder->process_until_end_of_metadata())
1118 return decoder->die("returned false");
1121 printf("testing process_single()... ");
1122 if(!decoder->process_single())
1123 return decoder->die("returned false");
1126 printf("testing flush()... ");
1127 if(!decoder->flush())
1128 return decoder->die("returned false");
1131 decoder->ignore_errors_ = true;
1132 printf("testing process_single()... ");
1133 if(!decoder->process_single())
1134 return decoder->die("returned false");
1136 decoder->ignore_errors_ = false;
1138 printf("testing seek_absolute()... ");
1139 if(!decoder->seek_absolute(0))
1140 return decoder->die("returned false");
1143 printf("testing process_until_end_of_stream()... ");
1144 if(!decoder->process_until_end_of_stream())
1145 return decoder->die("returned false");
1148 printf("testing get_channels()... ");
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);
1158 printf("testing get_bits_per_sample()... ");
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);
1168 printf("testing get_sample_rate()... ");
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);
1178 printf("testing get_blocksize()... ");
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);
1185 printf("testing get_channel_assignment()... ");
1187 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
1188 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
1191 printf("testing reset()... ");
1192 if(!decoder->reset())
1193 return decoder->die("returned false");
1196 decoder->current_metadata_number_ = 0;
1198 printf("rewinding input... ");
1199 if(::fseek(decoder->file_, 0, SEEK_SET) < 0) {
1200 printf("FAILED, errno = %d\n", errno);
1205 printf("testing process_until_end_of_stream()... ");
1206 if(!decoder->process_until_end_of_stream())
1207 return decoder->die("returned false");
1210 printf("testing finish()... ");
1218 printf("testing set_metadata_respond_all()... ");
1219 if(!decoder->set_metadata_respond_all()) {
1220 printf("FAILED, returned false\n");
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_;
1233 if(!decoder->test_respond())
1240 printf("testing set_metadata_ignore_all()... ");
1241 if(!decoder->set_metadata_ignore_all()) {
1242 printf("FAILED, returned false\n");
1249 if(!decoder->test_respond())
1253 * respond all, ignore VORBIS_COMMENT
1256 printf("testing set_metadata_respond_all()... ");
1257 if(!decoder->set_metadata_respond_all()) {
1258 printf("FAILED, returned false\n");
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");
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_;
1277 if(!decoder->test_respond())
1281 * respond all, ignore APPLICATION
1284 printf("testing set_metadata_respond_all()... ");
1285 if(!decoder->set_metadata_respond_all()) {
1286 printf("FAILED, returned false\n");
1291 printf("testing set_metadata_ignore(APPLICATION)... ");
1292 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1293 printf("FAILED, returned false\n");
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_;
1304 if(!decoder->test_respond())
1308 * respond all, ignore APPLICATION id of app#1
1311 printf("testing set_metadata_respond_all()... ");
1312 if(!decoder->set_metadata_respond_all()) {
1313 printf("FAILED, returned false\n");
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");
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_;
1332 if(!decoder->test_respond())
1336 * respond all, ignore APPLICATION id of app#1 & app#2
1339 printf("testing set_metadata_respond_all()... ");
1340 if(!decoder->set_metadata_respond_all()) {
1341 printf("FAILED, returned false\n");
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");
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");
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_;
1366 if(!decoder->test_respond())
1370 * ignore all, respond VORBIS_COMMENT
1373 printf("testing set_metadata_ignore_all()... ");
1374 if(!decoder->set_metadata_ignore_all()) {
1375 printf("FAILED, returned false\n");
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");
1388 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1390 if(!decoder->test_respond())
1394 * ignore all, respond APPLICATION
1397 printf("testing set_metadata_ignore_all()... ");
1398 if(!decoder->set_metadata_ignore_all()) {
1399 printf("FAILED, returned false\n");
1404 printf("testing set_metadata_respond(APPLICATION)... ");
1405 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1406 printf("FAILED, returned false\n");
1412 expected_metadata_sequence_[num_expected_++] = &application1_;
1413 expected_metadata_sequence_[num_expected_++] = &application2_;
1415 if(!decoder->test_respond())
1419 * ignore all, respond APPLICATION id of app#1
1422 printf("testing set_metadata_ignore_all()... ");
1423 if(!decoder->set_metadata_ignore_all()) {
1424 printf("FAILED, returned false\n");
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");
1437 expected_metadata_sequence_[num_expected_++] = &application1_;
1439 if(!decoder->test_respond())
1443 * ignore all, respond APPLICATION id of app#1 & app#2
1446 printf("testing set_metadata_ignore_all()... ");
1447 if(!decoder->set_metadata_ignore_all()) {
1448 printf("FAILED, returned false\n");
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");
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");
1468 expected_metadata_sequence_[num_expected_++] = &application1_;
1469 expected_metadata_sequence_[num_expected_++] = &application2_;
1471 if(!decoder->test_respond())
1475 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
1478 printf("testing set_metadata_respond_all()... ");
1479 if(!decoder->set_metadata_respond_all()) {
1480 printf("FAILED, returned false\n");
1485 printf("testing set_metadata_ignore(APPLICATION)... ");
1486 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1487 printf("FAILED, returned false\n");
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");
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_;
1506 if(!decoder->test_respond())
1510 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1513 printf("testing set_metadata_ignore_all()... ");
1514 if(!decoder->set_metadata_ignore_all()) {
1515 printf("FAILED, returned false\n");
1520 printf("testing set_metadata_respond(APPLICATION)... ");
1521 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1522 printf("FAILED, returned false\n");
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");
1535 expected_metadata_sequence_[num_expected_++] = &application2_;
1537 if(!decoder->test_respond())
1540 /* done, now leave the sequence the way we found it... */
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_;
1549 ::fclose(decoder->file_);
1551 printf("freeing decoder instance... ");
1555 printf("\nPASSED!\n");
1560 class FileDecoder : public FLAC::Decoder::File, public DecoderCommon {
1562 FileDecoder(): FLAC::Decoder::File(), DecoderCommon() { }
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);
1570 bool die(const char *msg = 0) const;
1572 bool test_respond();
1575 ::FLAC__StreamDecoderWriteStatus FileDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
1578 return common_write_callback_(frame);
1581 void FileDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
1583 common_metadata_callback_(metadata);
1586 void FileDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
1588 common_error_callback_(status);
1591 bool FileDecoder::die(const char *msg) const
1593 State state = get_state();
1596 printf("FAILED, %s", msg);
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());
1613 bool FileDecoder::test_respond()
1615 if(!set_filename(flacfilename_)) {
1616 printf("FAILED at set_filename(), returned false\n");
1620 if(!set_md5_checking(true)) {
1621 printf("FAILED at set_md5_checking(), returned false\n");
1625 printf("testing init()... ");
1626 if(init() != ::FLAC__FILE_DECODER_OK)
1630 current_metadata_number_ = 0;
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());
1640 printf("testing finish()... ");
1647 static bool test_file_decoder()
1649 FileDecoder *decoder;
1651 printf("\n+++ libFLAC++ unit test: FLAC::Decoder::File\n\n");
1654 // test new -> delete
1656 printf("allocating decoder instance... ");
1657 decoder = new FileDecoder();
1659 printf("FAILED, new returned NULL\n");
1664 printf("testing is_valid()... ");
1665 if(!decoder->is_valid()) {
1666 printf("FAILED, returned false\n");
1671 printf("freeing decoder instance... ");
1676 // test new -> init -> delete
1678 printf("allocating decoder instance... ");
1679 decoder = new FileDecoder();
1681 printf("FAILED, new returned NULL\n");
1686 printf("testing is_valid()... ");
1687 if(!decoder->is_valid()) {
1688 printf("FAILED, returned false\n");
1693 printf("testing init()... ");
1694 if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1695 return decoder->die();
1698 printf("freeing decoder instance... ");
1703 // test normal usage
1706 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1708 printf("allocating decoder instance... ");
1709 decoder = new FileDecoder();
1711 printf("FAILED, new returned NULL\n");
1716 printf("testing is_valid()... ");
1717 if(!decoder->is_valid()) {
1718 printf("FAILED, returned false\n");
1723 printf("testing set_filename()... ");
1724 if(!decoder->set_filename(flacfilename_)) {
1725 printf("FAILED, returned false\n");
1730 printf("testing set_md5_checking()... ");
1731 if(!decoder->set_md5_checking(true)) {
1732 printf("FAILED, returned false\n");
1737 printf("testing init()... ");
1738 if(decoder->init() != ::FLAC__FILE_DECODER_OK)
1739 return decoder->die();
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());
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());
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());
1754 decoder->current_metadata_number_ = 0;
1755 decoder->ignore_errors_ = false;
1756 decoder->error_occurred_ = false;
1758 printf("testing get_md5_checking()... ");
1759 if(!decoder->get_md5_checking()) {
1760 printf("FAILED, returned false, expected true\n");
1765 printf("testing process_until_end_of_metadata()... ");
1766 if(!decoder->process_until_end_of_metadata())
1767 return decoder->die("returned false");
1770 printf("testing process_single()... ");
1771 if(!decoder->process_single())
1772 return decoder->die("returned false");
1775 printf("testing seek_absolute()... ");
1776 if(!decoder->seek_absolute(0))
1777 return decoder->die("returned false");
1780 printf("testing process_until_end_of_file()... ");
1781 if(!decoder->process_until_end_of_file())
1782 return decoder->die("returned false");
1785 printf("testing get_channels()... ");
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);
1795 printf("testing get_bits_per_sample()... ");
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);
1805 printf("testing get_sample_rate()... ");
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);
1815 printf("testing get_blocksize()... ");
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);
1822 printf("testing get_channel_assignment()... ");
1824 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
1825 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
1828 printf("testing finish()... ");
1836 printf("testing set_metadata_respond_all()... ");
1837 if(!decoder->set_metadata_respond_all()) {
1838 printf("FAILED, returned false\n");
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_;
1851 if(!decoder->test_respond())
1858 printf("testing set_metadata_ignore_all()... ");
1859 if(!decoder->set_metadata_ignore_all()) {
1860 printf("FAILED, returned false\n");
1867 if(!decoder->test_respond())
1871 * respond all, ignore VORBIS_COMMENT
1874 printf("testing set_metadata_respond_all()... ");
1875 if(!decoder->set_metadata_respond_all()) {
1876 printf("FAILED, returned false\n");
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");
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_;
1895 if(!decoder->test_respond())
1899 * respond all, ignore APPLICATION
1902 printf("testing set_metadata_respond_all()... ");
1903 if(!decoder->set_metadata_respond_all()) {
1904 printf("FAILED, returned false\n");
1909 printf("testing set_metadata_ignore(APPLICATION)... ");
1910 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1911 printf("FAILED, returned false\n");
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_;
1922 if(!decoder->test_respond())
1926 * respond all, ignore APPLICATION id of app#1
1929 printf("testing set_metadata_respond_all()... ");
1930 if(!decoder->set_metadata_respond_all()) {
1931 printf("FAILED, returned false\n");
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");
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_;
1950 if(!decoder->test_respond())
1954 * respond all, ignore APPLICATION id of app#1 & app#2
1957 printf("testing set_metadata_respond_all()... ");
1958 if(!decoder->set_metadata_respond_all()) {
1959 printf("FAILED, returned false\n");
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");
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");
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_;
1984 if(!decoder->test_respond())
1988 * ignore all, respond VORBIS_COMMENT
1991 printf("testing set_metadata_ignore_all()... ");
1992 if(!decoder->set_metadata_ignore_all()) {
1993 printf("FAILED, returned false\n");
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");
2006 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
2008 if(!decoder->test_respond())
2012 * ignore all, respond APPLICATION
2015 printf("testing set_metadata_ignore_all()... ");
2016 if(!decoder->set_metadata_ignore_all()) {
2017 printf("FAILED, returned false\n");
2022 printf("testing set_metadata_respond(APPLICATION)... ");
2023 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
2024 printf("FAILED, returned false\n");
2030 expected_metadata_sequence_[num_expected_++] = &application1_;
2031 expected_metadata_sequence_[num_expected_++] = &application2_;
2033 if(!decoder->test_respond())
2037 * ignore all, respond APPLICATION id of app#1
2040 printf("testing set_metadata_ignore_all()... ");
2041 if(!decoder->set_metadata_ignore_all()) {
2042 printf("FAILED, returned false\n");
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");
2055 expected_metadata_sequence_[num_expected_++] = &application1_;
2057 if(!decoder->test_respond())
2061 * ignore all, respond APPLICATION id of app#1 & app#2
2064 printf("testing set_metadata_ignore_all()... ");
2065 if(!decoder->set_metadata_ignore_all()) {
2066 printf("FAILED, returned false\n");
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");
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");
2086 expected_metadata_sequence_[num_expected_++] = &application1_;
2087 expected_metadata_sequence_[num_expected_++] = &application2_;
2089 if(!decoder->test_respond())
2093 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
2096 printf("testing set_metadata_respond_all()... ");
2097 if(!decoder->set_metadata_respond_all()) {
2098 printf("FAILED, returned false\n");
2103 printf("testing set_metadata_ignore(APPLICATION)... ");
2104 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
2105 printf("FAILED, returned false\n");
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");
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_;
2124 if(!decoder->test_respond())
2128 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
2131 printf("testing set_metadata_ignore_all()... ");
2132 if(!decoder->set_metadata_ignore_all()) {
2133 printf("FAILED, returned false\n");
2138 printf("testing set_metadata_respond(APPLICATION)... ");
2139 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
2140 printf("FAILED, returned false\n");
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");
2153 expected_metadata_sequence_[num_expected_++] = &application2_;
2155 if(!decoder->test_respond())
2158 /* done, now leave the sequence the way we found it... */
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_;
2167 printf("freeing decoder instance... ");
2171 printf("\nPASSED!\n");
2176 bool test_decoders()
2178 init_metadata_blocks_();
2179 if(!generate_file_())
2182 if(!test_stream_decoder())
2185 if(!test_seekable_stream_decoder())
2188 if(!test_file_decoder())
2191 (void) FLAC__file_utils_remove_file(flacfilename_);
2192 free_metadata_blocks_();