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"
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_;
37 static bool die_(const char *msg)
39 printf("ERROR: %s\n", msg);
43 static void *malloc_or_die_(size_t size)
45 void *x = malloc(size);
47 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
53 static void init_metadata_blocks_()
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
59 remember, the metadata interface gets tested after the decoders,
60 so we do all the metadata manipulation here without it.
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);
77 padding_.is_last = false;
78 padding_.type = ::FLAC__METADATA_TYPE_PADDING;
79 padding_.length = 1234;
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;
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);
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;
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;
124 static void free_metadata_blocks_()
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);
133 static bool generate_file_()
135 printf("\n\ngenerating FLAC file for decoder tests...\n");
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_;
144 if(!file_utils__generate_flacfile(flacfilename_, &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_))
145 return die_("creating the encoded file");
151 class DecoderCommon {
154 unsigned current_metadata_number_;
156 bool error_occurred_;
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);
165 ::FLAC__StreamDecoderReadStatus DecoderCommon::common_read_callback_(FLAC__byte buffer[], unsigned *bytes)
168 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
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) {
176 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
178 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
182 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
186 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
189 ::FLAC__StreamDecoderWriteStatus DecoderCommon::common_write_callback_(const ::FLAC__Frame *frame)
192 return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
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)
198 printf("content... ");
202 return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
205 void DecoderCommon::common_metadata_callback_(const ::FLAC__StreamMetadata *metadata)
210 printf("%d... ", current_metadata_number_);
213 if(current_metadata_number_ >= num_expected_) {
214 (void)die_("got more metadata blocks than expected");
215 error_occurred_ = true;
218 if(!::FLAC__metadata_object_is_equal(expected_metadata_sequence_[current_metadata_number_], metadata)) {
219 (void)die_("metadata block mismatch");
220 error_occurred_ = true;
223 current_metadata_number_++;
226 void DecoderCommon::common_error_callback_(::FLAC__StreamDecoderErrorStatus status)
228 if(!ignore_errors_) {
229 printf("ERROR: got error callback: err = %u (%s)\n", (unsigned)status, ::FLAC__StreamDecoderErrorStatusString[status]);
230 error_occurred_ = true;
234 class StreamDecoder : public FLAC::Decoder::Stream, public DecoderCommon {
236 StreamDecoder(): FLAC::Decoder::Stream(), DecoderCommon() { }
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);
245 bool die(const char *msg = 0) const;
250 ::FLAC__StreamDecoderReadStatus StreamDecoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
252 return common_read_callback_(buffer, bytes);
255 ::FLAC__StreamDecoderWriteStatus StreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
259 return common_write_callback_(frame);
262 void StreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
264 common_metadata_callback_(metadata);
267 void StreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
269 common_error_callback_(status);
272 bool StreamDecoder::die(const char *msg) const
274 State state = get_state();
277 printf("FAILED, %s", msg);
281 printf(", state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)state), state.as_cstring());
286 bool StreamDecoder::test_respond()
288 printf("testing init()... ");
289 if(init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
293 current_metadata_number_ = 0;
295 if(::fseek(file_, 0, SEEK_SET) < 0) {
296 printf("FAILED rewinding input, errno = %d\n", errno);
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());
308 printf("testing finish()... ");
315 static bool test_stream_decoder()
317 StreamDecoder *decoder;
319 printf("\n+++ libFLAC++ unit test: FLAC::Decoder::Stream\n\n");
322 // test new -> delete
324 printf("allocating decoder instance... ");
325 decoder = new StreamDecoder();
327 printf("FAILED, new returned NULL\n");
332 printf("testing is_valid()... ");
333 if(!decoder->is_valid()) {
334 printf("FAILED, returned false\n");
339 printf("freeing decoder instance... ");
344 // test new -> init -> delete
346 printf("allocating decoder instance... ");
347 decoder = new StreamDecoder();
349 printf("FAILED, new returned NULL\n");
354 printf("testing is_valid()... ");
355 if(!decoder->is_valid()) {
356 printf("FAILED, returned false\n");
361 printf("testing init()... ");
362 if(decoder->init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
363 return decoder->die();
366 printf("freeing decoder instance... ");
374 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
376 printf("allocating decoder instance... ");
377 decoder = new StreamDecoder();
379 printf("FAILED, new returned NULL\n");
384 printf("testing is_valid()... ");
385 if(!decoder->is_valid()) {
386 printf("FAILED, returned false\n");
391 printf("testing init()... ");
392 if(decoder->init() != ::FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
393 return decoder->die();
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());
400 decoder->current_metadata_number_ = 0;
401 decoder->ignore_errors_ = false;
402 decoder->error_occurred_ = false;
404 printf("opening FLAC file... ");
405 decoder->file_ = ::fopen(flacfilename_, "rb");
406 if(0 == decoder->file_) {
412 printf("testing process_until_end_of_metadata()... ");
413 if(!decoder->process_until_end_of_metadata())
414 return decoder->die("returned false");
417 printf("testing process_single()... ");
418 if(!decoder->process_single())
419 return decoder->die("returned false");
422 printf("testing flush()... ");
423 if(!decoder->flush())
424 return decoder->die("returned false");
427 decoder->ignore_errors_ = true;
428 printf("testing process_single()... ");
429 if(!decoder->process_single())
430 return decoder->die("returned false");
432 decoder->ignore_errors_ = false;
434 printf("testing process_until_end_of_stream()... ");
435 if(!decoder->process_until_end_of_stream())
436 return decoder->die("returned false");
439 printf("testing get_channels()... ");
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);
449 printf("testing get_bits_per_sample()... ");
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);
459 printf("testing get_sample_rate()... ");
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);
469 printf("testing get_blocksize()... ");
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);
476 printf("testing get_channel_assignment()... ");
478 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
479 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
482 printf("testing reset()... ");
483 if(!decoder->reset())
484 return decoder->die("returned false");
487 decoder->current_metadata_number_ = 0;
489 printf("rewinding input... ");
490 if(::fseek(decoder->file_, 0, SEEK_SET) < 0) {
491 printf("FAILED, errno = %d\n", errno);
496 printf("testing process_until_end_of_stream()... ");
497 if(!decoder->process_until_end_of_stream())
498 return decoder->die("returned false");
501 printf("testing finish()... ");
509 printf("testing set_metadata_respond_all()... ");
510 if(!decoder->set_metadata_respond_all()) {
511 printf("FAILED, returned false\n");
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_;
524 if(!decoder->test_respond())
531 printf("testing set_metadata_ignore_all()... ");
532 if(!decoder->set_metadata_ignore_all()) {
533 printf("FAILED, returned false\n");
540 if(!decoder->test_respond())
544 * respond all, ignore VORBIS_COMMENT
547 printf("testing set_metadata_respond_all()... ");
548 if(!decoder->set_metadata_respond_all()) {
549 printf("FAILED, returned false\n");
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");
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_;
568 if(!decoder->test_respond())
572 * respond all, ignore APPLICATION
575 printf("testing set_metadata_respond_all()... ");
576 if(!decoder->set_metadata_respond_all()) {
577 printf("FAILED, returned false\n");
582 printf("testing set_metadata_ignore(APPLICATION)... ");
583 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
584 printf("FAILED, returned false\n");
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_;
595 if(!decoder->test_respond())
599 * respond all, ignore APPLICATION id of app#1
602 printf("testing set_metadata_respond_all()... ");
603 if(!decoder->set_metadata_respond_all()) {
604 printf("FAILED, returned false\n");
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");
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_;
623 if(!decoder->test_respond())
627 * respond all, ignore APPLICATION id of app#1 & app#2
630 printf("testing set_metadata_respond_all()... ");
631 if(!decoder->set_metadata_respond_all()) {
632 printf("FAILED, returned false\n");
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");
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");
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_;
657 if(!decoder->test_respond())
661 * ignore all, respond VORBIS_COMMENT
664 printf("testing set_metadata_ignore_all()... ");
665 if(!decoder->set_metadata_ignore_all()) {
666 printf("FAILED, returned false\n");
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");
679 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
681 if(!decoder->test_respond())
685 * ignore all, respond APPLICATION
688 printf("testing set_metadata_ignore_all()... ");
689 if(!decoder->set_metadata_ignore_all()) {
690 printf("FAILED, returned false\n");
695 printf("testing set_metadata_respond(APPLICATION)... ");
696 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
697 printf("FAILED, returned false\n");
703 expected_metadata_sequence_[num_expected_++] = &application1_;
704 expected_metadata_sequence_[num_expected_++] = &application2_;
706 if(!decoder->test_respond())
710 * ignore all, respond APPLICATION id of app#1
713 printf("testing set_metadata_ignore_all()... ");
714 if(!decoder->set_metadata_ignore_all()) {
715 printf("FAILED, returned false\n");
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");
728 expected_metadata_sequence_[num_expected_++] = &application1_;
730 if(!decoder->test_respond())
734 * ignore all, respond APPLICATION id of app#1 & app#2
737 printf("testing set_metadata_ignore_all()... ");
738 if(!decoder->set_metadata_ignore_all()) {
739 printf("FAILED, returned false\n");
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");
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");
759 expected_metadata_sequence_[num_expected_++] = &application1_;
760 expected_metadata_sequence_[num_expected_++] = &application2_;
762 if(!decoder->test_respond())
766 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
769 printf("testing set_metadata_respond_all()... ");
770 if(!decoder->set_metadata_respond_all()) {
771 printf("FAILED, returned false\n");
776 printf("testing set_metadata_ignore(APPLICATION)... ");
777 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
778 printf("FAILED, returned false\n");
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");
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_;
797 if(!decoder->test_respond())
801 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
804 printf("testing set_metadata_ignore_all()... ");
805 if(!decoder->set_metadata_ignore_all()) {
806 printf("FAILED, returned false\n");
811 printf("testing set_metadata_respond(APPLICATION)... ");
812 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
813 printf("FAILED, returned false\n");
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");
826 expected_metadata_sequence_[num_expected_++] = &application2_;
828 if(!decoder->test_respond())
831 /* done, now leave the sequence the way we found it... */
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_;
840 ::fclose(decoder->file_);
842 printf("freeing decoder instance... ");
846 printf("\nPASSED!\n");
851 class SeekableStreamDecoder : public FLAC::Decoder::SeekableStream, public DecoderCommon {
853 SeekableStreamDecoder(): FLAC::Decoder::SeekableStream(), DecoderCommon() { }
854 ~SeekableStreamDecoder() { }
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);
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);
866 bool die(const char *msg = 0) const;
871 ::FLAC__SeekableStreamDecoderReadStatus SeekableStreamDecoder::read_callback(FLAC__byte buffer[], unsigned *bytes)
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;
881 return ::FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
885 ::FLAC__SeekableStreamDecoderSeekStatus SeekableStreamDecoder::seek_callback(FLAC__uint64 absolute_byte_offset)
888 return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
890 if(::fseek(file_, (long)absolute_byte_offset, SEEK_SET) < 0) {
891 error_occurred_ = true;
892 return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
895 return ::FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
898 ::FLAC__SeekableStreamDecoderTellStatus SeekableStreamDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
901 return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
903 long offset = ::ftell(file_);
904 *absolute_byte_offset = (FLAC__uint64)offset;
907 error_occurred_ = true;
908 return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
911 return ::FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
914 ::FLAC__SeekableStreamDecoderLengthStatus SeekableStreamDecoder::length_callback(FLAC__uint64 *stream_length)
917 return ::FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
919 *stream_length = (FLAC__uint64)flacfilesize_;
920 return ::FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
923 bool SeekableStreamDecoder::eof_callback()
928 return (bool)feof(file_);
931 ::FLAC__StreamDecoderWriteStatus SeekableStreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
935 return common_write_callback_(frame);
938 void SeekableStreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
940 common_metadata_callback_(metadata);
943 void SeekableStreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
945 common_error_callback_(status);
948 bool SeekableStreamDecoder::die(const char *msg) const
950 State state = get_state();
953 printf("FAILED, %s", msg);
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());
966 bool SeekableStreamDecoder::test_respond()
968 if(!set_md5_checking(true)) {
969 printf("FAILED at set_md5_checking(), returned false\n");
973 printf("testing init()... ");
974 if(init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
978 current_metadata_number_ = 0;
980 if(::fseek(file_, 0, SEEK_SET) < 0) {
981 printf("FAILED rewinding input, errno = %d\n", errno);
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());
993 printf("testing finish()... ");
1000 static bool test_seekable_stream_decoder()
1002 SeekableStreamDecoder *decoder;
1004 printf("\n+++ libFLAC++ unit test: FLAC::Decoder::SeekableStream\n\n");
1007 // test new -> delete
1009 printf("allocating decoder instance... ");
1010 decoder = new SeekableStreamDecoder();
1012 printf("FAILED, new returned NULL\n");
1017 printf("testing is_valid()... ");
1018 if(!decoder->is_valid()) {
1019 printf("FAILED, returned false\n");
1024 printf("freeing decoder instance... ");
1029 // test new -> init -> delete
1031 printf("allocating decoder instance... ");
1032 decoder = new SeekableStreamDecoder();
1034 printf("FAILED, new returned NULL\n");
1039 printf("testing is_valid()... ");
1040 if(!decoder->is_valid()) {
1041 printf("FAILED, returned false\n");
1046 printf("testing init()... ");
1047 if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1048 return decoder->die();
1051 printf("freeing decoder instance... ");
1056 // test normal usage
1059 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1061 printf("allocating decoder instance... ");
1062 decoder = new SeekableStreamDecoder();
1064 printf("FAILED, new returned NULL\n");
1069 printf("testing is_valid()... ");
1070 if(!decoder->is_valid()) {
1071 printf("FAILED, returned false\n");
1076 printf("testing set_md5_checking()... ");
1077 if(!decoder->set_md5_checking(true)) {
1078 printf("FAILED, returned false\n");
1083 printf("testing init()... ");
1084 if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1085 return decoder->die();
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());
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());
1096 decoder->current_metadata_number_ = 0;
1097 decoder->ignore_errors_ = false;
1098 decoder->error_occurred_ = false;
1100 printf("opening FLAC file... ");
1101 decoder->file_ = ::fopen(flacfilename_, "rb");
1102 if(0 == decoder->file_) {
1108 printf("testing get_md5_checking()... ");
1109 if(!decoder->get_md5_checking()) {
1110 printf("FAILED, returned false, expected true\n");
1115 printf("testing process_until_end_of_metadata()... ");
1116 if(!decoder->process_until_end_of_metadata())
1117 return decoder->die("returned false");
1120 printf("testing process_single()... ");
1121 if(!decoder->process_single())
1122 return decoder->die("returned false");
1125 printf("testing flush()... ");
1126 if(!decoder->flush())
1127 return decoder->die("returned false");
1130 decoder->ignore_errors_ = true;
1131 printf("testing process_single()... ");
1132 if(!decoder->process_single())
1133 return decoder->die("returned false");
1135 decoder->ignore_errors_ = false;
1137 printf("testing seek_absolute()... ");
1138 if(!decoder->seek_absolute(0))
1139 return decoder->die("returned false");
1142 printf("testing process_until_end_of_stream()... ");
1143 if(!decoder->process_until_end_of_stream())
1144 return decoder->die("returned false");
1147 printf("testing get_channels()... ");
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);
1157 printf("testing get_bits_per_sample()... ");
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);
1167 printf("testing get_sample_rate()... ");
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);
1177 printf("testing get_blocksize()... ");
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);
1184 printf("testing get_channel_assignment()... ");
1186 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
1187 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
1190 printf("testing reset()... ");
1191 if(!decoder->reset())
1192 return decoder->die("returned false");
1195 decoder->current_metadata_number_ = 0;
1197 printf("rewinding input... ");
1198 if(::fseek(decoder->file_, 0, SEEK_SET) < 0) {
1199 printf("FAILED, errno = %d\n", errno);
1204 printf("testing process_until_end_of_stream()... ");
1205 if(!decoder->process_until_end_of_stream())
1206 return decoder->die("returned false");
1209 printf("testing finish()... ");
1217 printf("testing set_metadata_respond_all()... ");
1218 if(!decoder->set_metadata_respond_all()) {
1219 printf("FAILED, returned false\n");
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_;
1232 if(!decoder->test_respond())
1239 printf("testing set_metadata_ignore_all()... ");
1240 if(!decoder->set_metadata_ignore_all()) {
1241 printf("FAILED, returned false\n");
1248 if(!decoder->test_respond())
1252 * respond all, ignore VORBIS_COMMENT
1255 printf("testing set_metadata_respond_all()... ");
1256 if(!decoder->set_metadata_respond_all()) {
1257 printf("FAILED, returned false\n");
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");
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_;
1276 if(!decoder->test_respond())
1280 * respond all, ignore APPLICATION
1283 printf("testing set_metadata_respond_all()... ");
1284 if(!decoder->set_metadata_respond_all()) {
1285 printf("FAILED, returned false\n");
1290 printf("testing set_metadata_ignore(APPLICATION)... ");
1291 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1292 printf("FAILED, returned false\n");
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_;
1303 if(!decoder->test_respond())
1307 * respond all, ignore APPLICATION id of app#1
1310 printf("testing set_metadata_respond_all()... ");
1311 if(!decoder->set_metadata_respond_all()) {
1312 printf("FAILED, returned false\n");
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");
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_;
1331 if(!decoder->test_respond())
1335 * respond all, ignore APPLICATION id of app#1 & app#2
1338 printf("testing set_metadata_respond_all()... ");
1339 if(!decoder->set_metadata_respond_all()) {
1340 printf("FAILED, returned false\n");
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");
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");
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_;
1365 if(!decoder->test_respond())
1369 * ignore all, respond VORBIS_COMMENT
1372 printf("testing set_metadata_ignore_all()... ");
1373 if(!decoder->set_metadata_ignore_all()) {
1374 printf("FAILED, returned false\n");
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");
1387 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1389 if(!decoder->test_respond())
1393 * ignore all, respond APPLICATION
1396 printf("testing set_metadata_ignore_all()... ");
1397 if(!decoder->set_metadata_ignore_all()) {
1398 printf("FAILED, returned false\n");
1403 printf("testing set_metadata_respond(APPLICATION)... ");
1404 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1405 printf("FAILED, returned false\n");
1411 expected_metadata_sequence_[num_expected_++] = &application1_;
1412 expected_metadata_sequence_[num_expected_++] = &application2_;
1414 if(!decoder->test_respond())
1418 * ignore all, respond APPLICATION id of app#1
1421 printf("testing set_metadata_ignore_all()... ");
1422 if(!decoder->set_metadata_ignore_all()) {
1423 printf("FAILED, returned false\n");
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");
1436 expected_metadata_sequence_[num_expected_++] = &application1_;
1438 if(!decoder->test_respond())
1442 * ignore all, respond APPLICATION id of app#1 & app#2
1445 printf("testing set_metadata_ignore_all()... ");
1446 if(!decoder->set_metadata_ignore_all()) {
1447 printf("FAILED, returned false\n");
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");
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");
1467 expected_metadata_sequence_[num_expected_++] = &application1_;
1468 expected_metadata_sequence_[num_expected_++] = &application2_;
1470 if(!decoder->test_respond())
1474 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
1477 printf("testing set_metadata_respond_all()... ");
1478 if(!decoder->set_metadata_respond_all()) {
1479 printf("FAILED, returned false\n");
1484 printf("testing set_metadata_ignore(APPLICATION)... ");
1485 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1486 printf("FAILED, returned false\n");
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");
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_;
1505 if(!decoder->test_respond())
1509 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1512 printf("testing set_metadata_ignore_all()... ");
1513 if(!decoder->set_metadata_ignore_all()) {
1514 printf("FAILED, returned false\n");
1519 printf("testing set_metadata_respond(APPLICATION)... ");
1520 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1521 printf("FAILED, returned false\n");
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");
1534 expected_metadata_sequence_[num_expected_++] = &application2_;
1536 if(!decoder->test_respond())
1539 /* done, now leave the sequence the way we found it... */
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_;
1548 ::fclose(decoder->file_);
1550 printf("freeing decoder instance... ");
1554 printf("\nPASSED!\n");
1559 class FileDecoder : public FLAC::Decoder::File, public DecoderCommon {
1561 FileDecoder(): FLAC::Decoder::File(), DecoderCommon() { }
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);
1569 bool die(const char *msg = 0) const;
1571 bool test_respond();
1574 ::FLAC__StreamDecoderWriteStatus FileDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
1577 return common_write_callback_(frame);
1580 void FileDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
1582 common_metadata_callback_(metadata);
1585 void FileDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
1587 common_error_callback_(status);
1590 bool FileDecoder::die(const char *msg) const
1592 State state = get_state();
1595 printf("FAILED, %s", msg);
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());
1612 bool FileDecoder::test_respond()
1614 if(!set_filename(flacfilename_)) {
1615 printf("FAILED at set_filename(), returned false\n");
1619 if(!set_md5_checking(true)) {
1620 printf("FAILED at set_md5_checking(), returned false\n");
1624 printf("testing init()... ");
1625 if(init() != ::FLAC__FILE_DECODER_OK)
1629 current_metadata_number_ = 0;
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());
1639 printf("testing finish()... ");
1646 static bool test_file_decoder()
1648 FileDecoder *decoder;
1650 printf("\n+++ libFLAC++ unit test: FLAC::Decoder::File\n\n");
1653 // test new -> delete
1655 printf("allocating decoder instance... ");
1656 decoder = new FileDecoder();
1658 printf("FAILED, new returned NULL\n");
1663 printf("testing is_valid()... ");
1664 if(!decoder->is_valid()) {
1665 printf("FAILED, returned false\n");
1670 printf("freeing decoder instance... ");
1675 // test new -> init -> delete
1677 printf("allocating decoder instance... ");
1678 decoder = new FileDecoder();
1680 printf("FAILED, new returned NULL\n");
1685 printf("testing is_valid()... ");
1686 if(!decoder->is_valid()) {
1687 printf("FAILED, returned false\n");
1692 printf("testing init()... ");
1693 if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
1694 return decoder->die();
1697 printf("freeing decoder instance... ");
1702 // test normal usage
1705 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1707 printf("allocating decoder instance... ");
1708 decoder = new FileDecoder();
1710 printf("FAILED, new returned NULL\n");
1715 printf("testing is_valid()... ");
1716 if(!decoder->is_valid()) {
1717 printf("FAILED, returned false\n");
1722 printf("testing set_filename()... ");
1723 if(!decoder->set_filename(flacfilename_)) {
1724 printf("FAILED, returned false\n");
1729 printf("testing set_md5_checking()... ");
1730 if(!decoder->set_md5_checking(true)) {
1731 printf("FAILED, returned false\n");
1736 printf("testing init()... ");
1737 if(decoder->init() != ::FLAC__FILE_DECODER_OK)
1738 return decoder->die();
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());
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());
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());
1753 decoder->current_metadata_number_ = 0;
1754 decoder->ignore_errors_ = false;
1755 decoder->error_occurred_ = false;
1757 printf("testing get_md5_checking()... ");
1758 if(!decoder->get_md5_checking()) {
1759 printf("FAILED, returned false, expected true\n");
1764 printf("testing process_until_end_of_metadata()... ");
1765 if(!decoder->process_until_end_of_metadata())
1766 return decoder->die("returned false");
1769 printf("testing process_single()... ");
1770 if(!decoder->process_single())
1771 return decoder->die("returned false");
1774 printf("testing seek_absolute()... ");
1775 if(!decoder->seek_absolute(0))
1776 return decoder->die("returned false");
1779 printf("testing process_until_end_of_file()... ");
1780 if(!decoder->process_until_end_of_file())
1781 return decoder->die("returned false");
1784 printf("testing get_channels()... ");
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);
1794 printf("testing get_bits_per_sample()... ");
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);
1804 printf("testing get_sample_rate()... ");
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);
1814 printf("testing get_blocksize()... ");
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);
1821 printf("testing get_channel_assignment()... ");
1823 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
1824 printf("returned %u (%s)... OK\n", (unsigned)ca, ::FLAC__ChannelAssignmentString[ca]);
1827 printf("testing finish()... ");
1835 printf("testing set_metadata_respond_all()... ");
1836 if(!decoder->set_metadata_respond_all()) {
1837 printf("FAILED, returned false\n");
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_;
1850 if(!decoder->test_respond())
1857 printf("testing set_metadata_ignore_all()... ");
1858 if(!decoder->set_metadata_ignore_all()) {
1859 printf("FAILED, returned false\n");
1866 if(!decoder->test_respond())
1870 * respond all, ignore VORBIS_COMMENT
1873 printf("testing set_metadata_respond_all()... ");
1874 if(!decoder->set_metadata_respond_all()) {
1875 printf("FAILED, returned false\n");
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");
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_;
1894 if(!decoder->test_respond())
1898 * respond all, ignore APPLICATION
1901 printf("testing set_metadata_respond_all()... ");
1902 if(!decoder->set_metadata_respond_all()) {
1903 printf("FAILED, returned false\n");
1908 printf("testing set_metadata_ignore(APPLICATION)... ");
1909 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1910 printf("FAILED, returned false\n");
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_;
1921 if(!decoder->test_respond())
1925 * respond all, ignore APPLICATION id of app#1
1928 printf("testing set_metadata_respond_all()... ");
1929 if(!decoder->set_metadata_respond_all()) {
1930 printf("FAILED, returned false\n");
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");
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_;
1949 if(!decoder->test_respond())
1953 * respond all, ignore APPLICATION id of app#1 & app#2
1956 printf("testing set_metadata_respond_all()... ");
1957 if(!decoder->set_metadata_respond_all()) {
1958 printf("FAILED, returned false\n");
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");
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");
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_;
1983 if(!decoder->test_respond())
1987 * ignore all, respond VORBIS_COMMENT
1990 printf("testing set_metadata_ignore_all()... ");
1991 if(!decoder->set_metadata_ignore_all()) {
1992 printf("FAILED, returned false\n");
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");
2005 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
2007 if(!decoder->test_respond())
2011 * ignore all, respond APPLICATION
2014 printf("testing set_metadata_ignore_all()... ");
2015 if(!decoder->set_metadata_ignore_all()) {
2016 printf("FAILED, returned false\n");
2021 printf("testing set_metadata_respond(APPLICATION)... ");
2022 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
2023 printf("FAILED, returned false\n");
2029 expected_metadata_sequence_[num_expected_++] = &application1_;
2030 expected_metadata_sequence_[num_expected_++] = &application2_;
2032 if(!decoder->test_respond())
2036 * ignore all, respond APPLICATION id of app#1
2039 printf("testing set_metadata_ignore_all()... ");
2040 if(!decoder->set_metadata_ignore_all()) {
2041 printf("FAILED, returned false\n");
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");
2054 expected_metadata_sequence_[num_expected_++] = &application1_;
2056 if(!decoder->test_respond())
2060 * ignore all, respond APPLICATION id of app#1 & app#2
2063 printf("testing set_metadata_ignore_all()... ");
2064 if(!decoder->set_metadata_ignore_all()) {
2065 printf("FAILED, returned false\n");
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");
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");
2085 expected_metadata_sequence_[num_expected_++] = &application1_;
2086 expected_metadata_sequence_[num_expected_++] = &application2_;
2088 if(!decoder->test_respond())
2092 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
2095 printf("testing set_metadata_respond_all()... ");
2096 if(!decoder->set_metadata_respond_all()) {
2097 printf("FAILED, returned false\n");
2102 printf("testing set_metadata_ignore(APPLICATION)... ");
2103 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
2104 printf("FAILED, returned false\n");
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");
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_;
2123 if(!decoder->test_respond())
2127 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
2130 printf("testing set_metadata_ignore_all()... ");
2131 if(!decoder->set_metadata_ignore_all()) {
2132 printf("FAILED, returned false\n");
2137 printf("testing set_metadata_respond(APPLICATION)... ");
2138 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
2139 printf("FAILED, returned false\n");
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");
2152 expected_metadata_sequence_[num_expected_++] = &application2_;
2154 if(!decoder->test_respond())
2157 /* done, now leave the sequence the way we found it... */
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_;
2166 printf("freeing decoder instance... ");
2170 printf("\nPASSED!\n");
2175 bool test_decoders()
2177 init_metadata_blocks_();
2178 if(!generate_file_())
2181 if(!test_stream_decoder())
2184 if(!test_seekable_stream_decoder())
2187 if(!test_file_decoder())
2190 (void) file_utils__remove_file(flacfilename_);
2191 free_metadata_blocks_();