Simplified logic of parsing sizes of rice-partitions
[flac.git] / src / test_libFLAC++ / decoders.cpp
bloba3633092e579f9dd0feba1c69dec4eef7d3985ec
1 /* test_libFLAC++ - Unit tester for libFLAC++
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "decoders.h"
29 #include "FLAC/assert.h"
30 #include "FLAC/metadata.h" // for ::FLAC__metadata_object_is_equal()
31 #include "FLAC++/decoder.h"
32 #include "share/grabbag.h"
33 #include "share/compat.h"
34 extern "C" {
35 #include "test_libs_common/file_utils_flac.h"
36 #include "test_libs_common/metadata_utils.h"
39 #ifdef _MSC_VER
40 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
41 #pragma warning ( disable : 4800 )
42 #endif
44 typedef enum {
45 LAYER_STREAM = 0, /* FLAC__stream_decoder_init_stream() without seeking */
46 LAYER_SEEKABLE_STREAM, /* FLAC__stream_decoder_init_stream() with seeking */
47 LAYER_FILE, /* FLAC__stream_decoder_init_FILE() */
48 LAYER_FILENAME /* FLAC__stream_decoder_init_file() */
49 } Layer;
51 static const char * const LayerString[] = {
52 "Stream",
53 "Seekable Stream",
54 "FILE*",
55 "Filename"
58 static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_;
59 static ::FLAC__StreamMetadata *expected_metadata_sequence_[9];
60 static uint32_t num_expected_;
61 static FLAC__off_t flacfilesize_;
63 static const char *flacfilename(bool is_ogg)
65 return is_ogg? "metadata.oga" : "metadata.flac";
68 static bool die_(const char *msg)
70 printf("ERROR: %s\n", msg);
71 return false;
74 static FLAC__bool die_s_(const char *msg, const FLAC::Decoder::Stream *decoder)
76 FLAC::Decoder::Stream::State state = decoder->get_state();
78 if(msg)
79 printf("FAILED, %s", msg);
80 else
81 printf("FAILED");
83 printf(", state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
85 return false;
88 static void init_metadata_blocks_()
90 mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
93 static void free_metadata_blocks_()
95 mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
98 static bool generate_file_(FLAC__bool is_ogg)
100 printf("\n\ngenerating %sFLAC file for decoder tests...\n", is_ogg? "Ogg ":"");
102 num_expected_ = 0;
103 expected_metadata_sequence_[num_expected_++] = &padding_;
104 expected_metadata_sequence_[num_expected_++] = &seektable_;
105 expected_metadata_sequence_[num_expected_++] = &application1_;
106 expected_metadata_sequence_[num_expected_++] = &application2_;
107 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
108 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
109 expected_metadata_sequence_[num_expected_++] = &picture_;
110 expected_metadata_sequence_[num_expected_++] = &unknown_;
111 /* WATCHOUT: for Ogg FLAC the encoder should move the VORBIS_COMMENT block to the front, right after STREAMINFO */
113 if(!file_utils__generate_flacfile(is_ogg, flacfilename(is_ogg), &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_))
114 return die_("creating the encoded file");
116 return true;
120 class DecoderCommon {
121 public:
122 Layer layer_;
123 uint32_t current_metadata_number_;
124 bool ignore_errors_;
125 bool error_occurred_;
127 DecoderCommon(Layer layer): layer_(layer), current_metadata_number_(0), ignore_errors_(false), error_occurred_(false) { }
128 virtual ~DecoderCommon(void) { }
129 ::FLAC__StreamDecoderWriteStatus common_write_callback_(const ::FLAC__Frame *frame);
130 void common_metadata_callback_(const ::FLAC__StreamMetadata *metadata);
131 void common_error_callback_(::FLAC__StreamDecoderErrorStatus status);
134 ::FLAC__StreamDecoderWriteStatus DecoderCommon::common_write_callback_(const ::FLAC__Frame *frame)
136 if(error_occurred_)
137 return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
140 (frame->header.number_type == ::FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) ||
141 (frame->header.number_type == ::FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0)
143 printf("content... ");
144 fflush(stdout);
147 return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
150 void DecoderCommon::common_metadata_callback_(const ::FLAC__StreamMetadata *metadata)
152 if(error_occurred_)
153 return;
155 printf("%u... ", current_metadata_number_);
156 fflush(stdout);
158 if(current_metadata_number_ >= num_expected_) {
159 (void)die_("got more metadata blocks than expected");
160 error_occurred_ = true;
162 else {
163 if(!::FLAC__metadata_object_is_equal(expected_metadata_sequence_[current_metadata_number_], metadata)) {
164 (void)die_("metadata block mismatch");
165 error_occurred_ = true;
168 current_metadata_number_++;
171 void DecoderCommon::common_error_callback_(::FLAC__StreamDecoderErrorStatus status)
173 if(!ignore_errors_) {
174 printf("ERROR: got error callback: err = %u (%s)\n", (uint32_t)status, ::FLAC__StreamDecoderErrorStatusString[status]);
175 error_occurred_ = true;
179 class StreamDecoder : public FLAC::Decoder::Stream, public DecoderCommon {
180 public:
181 FILE *file_;
183 StreamDecoder(Layer layer): FLAC::Decoder::Stream(), DecoderCommon(layer), file_(0) { }
184 ~StreamDecoder() { }
186 // from FLAC::Decoder::Stream
187 ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
188 ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
189 ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
190 ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length);
191 bool eof_callback();
192 ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
193 void metadata_callback(const ::FLAC__StreamMetadata *metadata);
194 void error_callback(::FLAC__StreamDecoderErrorStatus status);
196 bool test_respond(bool is_ogg);
197 private:
198 StreamDecoder(const StreamDecoder&);
199 StreamDecoder&operator=(const StreamDecoder&);
202 ::FLAC__StreamDecoderReadStatus StreamDecoder::read_callback(FLAC__byte buffer[], size_t *bytes)
204 const size_t requested_bytes = *bytes;
206 if(error_occurred_)
207 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
209 if(feof(file_)) {
210 *bytes = 0;
211 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
213 else if(requested_bytes > 0) {
214 *bytes = ::fread(buffer, 1, requested_bytes, file_);
215 if(*bytes == 0) {
216 if(feof(file_))
217 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
218 else
219 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
221 else {
222 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
225 else
226 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
229 ::FLAC__StreamDecoderSeekStatus StreamDecoder::seek_callback(FLAC__uint64 absolute_byte_offset)
231 if(layer_ == LAYER_STREAM)
232 return ::FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
234 if(error_occurred_)
235 return ::FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
237 if(fseeko(file_, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) {
238 error_occurred_ = true;
239 return ::FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
242 return ::FLAC__STREAM_DECODER_SEEK_STATUS_OK;
245 ::FLAC__StreamDecoderTellStatus StreamDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
247 if(layer_ == LAYER_STREAM)
248 return ::FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
250 if(error_occurred_)
251 return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
253 FLAC__off_t offset = ftello(file_);
254 *absolute_byte_offset = (FLAC__uint64)offset;
256 if(offset < 0) {
257 error_occurred_ = true;
258 return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
261 return ::FLAC__STREAM_DECODER_TELL_STATUS_OK;
264 ::FLAC__StreamDecoderLengthStatus StreamDecoder::length_callback(FLAC__uint64 *stream_length)
266 if(layer_ == LAYER_STREAM)
267 return ::FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
269 if(error_occurred_)
270 return ::FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
272 *stream_length = (FLAC__uint64)flacfilesize_;
273 return ::FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
276 bool StreamDecoder::eof_callback()
278 if(layer_ == LAYER_STREAM)
279 return false;
281 if(error_occurred_)
282 return true;
284 return (bool)feof(file_);
287 ::FLAC__StreamDecoderWriteStatus StreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
289 (void)buffer;
291 return common_write_callback_(frame);
294 void StreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
296 common_metadata_callback_(metadata);
299 void StreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
301 common_error_callback_(status);
304 bool StreamDecoder::test_respond(bool is_ogg)
306 ::FLAC__StreamDecoderInitStatus init_status;
308 if(!set_md5_checking(true)) {
309 printf("FAILED at set_md5_checking(), returned false\n");
310 return false;
313 printf("testing init%s()... ", is_ogg? "_ogg":"");
314 init_status = is_ogg? init_ogg() : init();
315 if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
316 return die_s_(0, this);
317 printf("OK\n");
319 current_metadata_number_ = 0;
321 if(fseeko(file_, 0, SEEK_SET) < 0) {
322 printf("FAILED rewinding input, errno = %d\n", errno);
323 return false;
326 printf("testing process_until_end_of_stream()... ");
327 if(!process_until_end_of_stream()) {
328 State state = get_state();
329 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
330 return false;
332 printf("OK\n");
334 printf("testing finish()... ");
335 if(!finish()) {
336 State state = get_state();
337 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
338 return false;
340 printf("OK\n");
342 return true;
345 class FileDecoder : public FLAC::Decoder::File, public DecoderCommon {
346 public:
347 FileDecoder(Layer layer): FLAC::Decoder::File(), DecoderCommon(layer) { }
348 ~FileDecoder() { }
350 // from FLAC::Decoder::Stream
351 ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
352 void metadata_callback(const ::FLAC__StreamMetadata *metadata);
353 void error_callback(::FLAC__StreamDecoderErrorStatus status);
355 bool test_respond(bool is_ogg);
358 ::FLAC__StreamDecoderWriteStatus FileDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
360 (void)buffer;
361 return common_write_callback_(frame);
364 void FileDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
366 common_metadata_callback_(metadata);
369 void FileDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
371 common_error_callback_(status);
374 bool FileDecoder::test_respond(bool is_ogg)
376 ::FLAC__StreamDecoderInitStatus init_status;
378 if(!set_md5_checking(true)) {
379 printf("FAILED at set_md5_checking(), returned false\n");
380 return false;
383 switch(layer_) {
384 case LAYER_FILE:
386 printf("opening %sFLAC file... ", is_ogg? "Ogg ":"");
387 FILE *file = ::flac_fopen(flacfilename(is_ogg), "rb");
388 if(0 == file) {
389 printf("ERROR (%s)\n", strerror(errno));
390 return false;
392 printf("OK\n");
394 printf("testing init%s()... ", is_ogg? "_ogg":"");
395 init_status = is_ogg? init_ogg(file) : init(file);
397 break;
398 case LAYER_FILENAME:
399 printf("testing init%s()... ", is_ogg? "_ogg":"");
400 init_status = is_ogg? init_ogg(flacfilename(is_ogg)) : init(flacfilename(is_ogg));
401 break;
402 default:
403 die_("internal error 001");
404 return false;
406 if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
407 return die_s_(0, this);
408 printf("OK\n");
410 current_metadata_number_ = 0;
412 printf("testing process_until_end_of_stream()... ");
413 if(!process_until_end_of_stream()) {
414 State state = get_state();
415 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
416 return false;
418 printf("OK\n");
420 printf("testing finish()... ");
421 if(!finish()) {
422 State state = get_state();
423 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
424 return false;
426 printf("OK\n");
428 return true;
432 static FLAC::Decoder::Stream *new_by_layer(Layer layer)
434 if(layer < LAYER_FILE)
435 return new StreamDecoder(layer);
436 else
437 return new FileDecoder(layer);
440 static bool test_stream_decoder(Layer layer, bool is_ogg)
442 FLAC::Decoder::Stream *decoder;
443 ::FLAC__StreamDecoderInitStatus init_status;
444 bool expect;
446 printf("\n+++ libFLAC++ unit test: FLAC::Decoder::%s (layer: %s, format: %s)\n\n", layer<LAYER_FILE? "Stream":"File", LayerString[layer], is_ogg? "Ogg FLAC" : "FLAC");
449 // test new -> delete
451 printf("allocating decoder instance... ");
452 decoder = new_by_layer(layer);
453 if(0 == decoder) {
454 printf("FAILED, new returned NULL\n");
455 return false;
457 printf("OK\n");
459 printf("testing is_valid()... ");
460 if(!decoder->is_valid()) {
461 printf("FAILED, returned false\n");
462 delete decoder;
463 return false;
465 printf("OK\n");
467 printf("freeing decoder instance... ");
468 delete decoder;
469 printf("OK\n");
472 // test new -> init -> delete
474 printf("allocating decoder instance... ");
475 decoder = new_by_layer(layer);
476 if(0 == decoder) {
477 printf("FAILED, new returned NULL\n");
478 return false;
480 printf("OK\n");
482 printf("testing is_valid()... ");
483 if(!decoder->is_valid()) {
484 printf("FAILED, returned false\n");
485 delete decoder;
486 return false;
488 printf("OK\n");
490 printf("testing init%s()... ", is_ogg? "_ogg":"");
491 switch(layer) {
492 case LAYER_STREAM:
493 case LAYER_SEEKABLE_STREAM:
494 dynamic_cast<StreamDecoder*>(decoder)->file_ = stdin;
495 init_status = is_ogg? decoder->init_ogg() : decoder->init();
496 break;
497 case LAYER_FILE:
498 init_status = is_ogg?
499 dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(stdin) :
500 dynamic_cast<FLAC::Decoder::File*>(decoder)->init(stdin);
501 break;
502 case LAYER_FILENAME:
503 init_status = is_ogg?
504 dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(flacfilename(is_ogg)) :
505 dynamic_cast<FLAC::Decoder::File*>(decoder)->init(flacfilename(is_ogg));
506 break;
507 default:
508 die_("internal error 006");
509 delete decoder;
510 return false;
512 if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
513 return die_s_(0, decoder);
514 printf("OK\n");
516 printf("freeing decoder instance... ");
517 delete decoder;
518 printf("OK\n");
521 // test normal usage
523 num_expected_ = 0;
524 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
526 printf("allocating decoder instance... ");
527 decoder = new_by_layer(layer);
528 if(0 == decoder) {
529 printf("FAILED, new returned NULL\n");
530 return false;
532 printf("OK\n");
534 printf("testing is_valid()... ");
535 if(!decoder->is_valid()) {
536 printf("FAILED, returned false\n");
537 delete decoder;
538 return false;
540 printf("OK\n");
542 if(is_ogg) {
543 printf("testing set_ogg_serial_number()... ");
544 if(!decoder->set_ogg_serial_number(file_utils__ogg_serial_number))
545 return die_s_("returned false", decoder);
546 printf("OK\n");
549 if(!decoder->set_md5_checking(true)) {
550 printf("FAILED at set_md5_checking(), returned false\n");
551 return false;
554 switch(layer) {
555 case LAYER_STREAM:
556 case LAYER_SEEKABLE_STREAM:
557 printf("opening %sFLAC file... ", is_ogg? "Ogg ":"");
558 dynamic_cast<StreamDecoder*>(decoder)->file_ = ::flac_fopen(flacfilename(is_ogg), "rb");
559 if(0 == dynamic_cast<StreamDecoder*>(decoder)->file_) {
560 printf("ERROR (%s)\n", strerror(errno));
561 return false;
563 printf("OK\n");
565 printf("testing init%s()... ", is_ogg? "_ogg":"");
566 init_status = is_ogg? decoder->init_ogg() : decoder->init();
567 break;
568 case LAYER_FILE:
570 printf("opening FLAC file... ");
571 FILE *file = ::flac_fopen(flacfilename(is_ogg), "rb");
572 if(0 == file) {
573 printf("ERROR (%s)\n", strerror(errno));
574 return false;
576 printf("OK\n");
578 printf("testing init%s()... ", is_ogg? "_ogg":"");
579 init_status = is_ogg?
580 dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(file) :
581 dynamic_cast<FLAC::Decoder::File*>(decoder)->init(file);
583 break;
584 case LAYER_FILENAME:
585 printf("testing init%s()... ", is_ogg? "_ogg":"");
586 init_status = is_ogg?
587 dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(flacfilename(is_ogg)) :
588 dynamic_cast<FLAC::Decoder::File*>(decoder)->init(flacfilename(is_ogg));
589 break;
590 default:
591 die_("internal error 009");
592 return false;
594 if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
595 return die_s_(0, decoder);
596 printf("OK\n");
598 printf("testing get_state()... ");
599 FLAC::Decoder::Stream::State state = decoder->get_state();
600 printf("returned state = %u (%s)... OK\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
602 dynamic_cast<DecoderCommon*>(decoder)->current_metadata_number_ = 0;
603 dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = false;
604 dynamic_cast<DecoderCommon*>(decoder)->error_occurred_ = false;
606 printf("testing get_md5_checking()... ");
607 if(!decoder->get_md5_checking()) {
608 printf("FAILED, returned false, expected true\n");
609 return false;
611 printf("OK\n");
613 printf("testing process_until_end_of_metadata()... ");
614 if(!decoder->process_until_end_of_metadata())
615 return die_s_("returned false", decoder);
616 printf("OK\n");
618 printf("testing process_single()... ");
619 if(!decoder->process_single())
620 return die_s_("returned false", decoder);
621 printf("OK\n");
623 printf("testing skip_single_frame()... ");
624 if(!decoder->skip_single_frame())
625 return die_s_("returned false", decoder);
626 printf("OK\n");
628 if(layer < LAYER_FILE) {
629 printf("testing flush()... ");
630 if(!decoder->flush())
631 return die_s_("returned false", decoder);
632 printf("OK\n");
634 dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = true;
635 printf("testing process_single()... ");
636 if(!decoder->process_single())
637 return die_s_("returned false", decoder);
638 printf("OK\n");
639 dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = false;
642 expect = (layer != LAYER_STREAM);
643 printf("testing seek_absolute()... ");
644 if(decoder->seek_absolute(0) != expect)
645 return die_s_(expect? "returned false" : "returned true", decoder);
646 printf("OK\n");
648 printf("testing process_until_end_of_stream()... ");
649 if(!decoder->process_until_end_of_stream())
650 return die_s_("returned false", decoder);
651 printf("OK\n");
653 expect = (layer != LAYER_STREAM);
654 printf("testing seek_absolute()... ");
655 if(decoder->seek_absolute(0) != expect)
656 return die_s_(expect? "returned false" : "returned true", decoder);
657 printf("OK\n");
659 printf("testing get_channels()... ");
661 uint32_t channels = decoder->get_channels();
662 if(channels != streaminfo_.data.stream_info.channels) {
663 printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
664 return false;
667 printf("OK\n");
669 printf("testing get_bits_per_sample()... ");
671 uint32_t bits_per_sample = decoder->get_bits_per_sample();
672 if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
673 printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
674 return false;
677 printf("OK\n");
679 printf("testing get_sample_rate()... ");
681 uint32_t sample_rate = decoder->get_sample_rate();
682 if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
683 printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
684 return false;
687 printf("OK\n");
689 printf("testing get_blocksize()... ");
691 uint32_t blocksize = decoder->get_blocksize();
692 /* value could be anything since we're at the last block, so accept any reasonable answer */
693 printf("returned %u... %s\n", blocksize, blocksize>0? "OK" : "FAILED");
694 if(blocksize == 0)
695 return false;
698 printf("testing get_channel_assignment()... ");
700 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
701 printf("returned %u (%s)... OK\n", (uint32_t)ca, ::FLAC__ChannelAssignmentString[ca]);
704 if(layer < LAYER_FILE) {
705 printf("testing reset()... ");
706 if(!decoder->reset())
707 return die_s_("returned false", decoder);
708 printf("OK\n");
710 if(layer == LAYER_STREAM) {
711 /* after a reset() we have to rewind the input ourselves */
712 printf("rewinding input... ");
713 if(fseeko(dynamic_cast<StreamDecoder*>(decoder)->file_, 0, SEEK_SET) < 0) {
714 printf("FAILED, errno = %d\n", errno);
715 return false;
717 printf("OK\n");
720 dynamic_cast<DecoderCommon*>(decoder)->current_metadata_number_ = 0;
722 printf("testing process_until_end_of_stream()... ");
723 if(!decoder->process_until_end_of_stream())
724 return die_s_("returned false", decoder);
725 printf("OK\n");
728 printf("testing finish()... ");
729 if(!decoder->finish()) {
730 state = decoder->get_state();
731 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
732 return false;
734 printf("OK\n");
737 * respond all
740 printf("testing set_metadata_respond_all()... ");
741 if(!decoder->set_metadata_respond_all()) {
742 printf("FAILED, returned false\n");
743 return false;
745 printf("OK\n");
747 num_expected_ = 0;
748 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping */
749 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
750 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
751 expected_metadata_sequence_[num_expected_++] = &padding_;
752 expected_metadata_sequence_[num_expected_++] = &seektable_;
753 expected_metadata_sequence_[num_expected_++] = &application1_;
754 expected_metadata_sequence_[num_expected_++] = &application2_;
755 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
756 expected_metadata_sequence_[num_expected_++] = &picture_;
757 expected_metadata_sequence_[num_expected_++] = &unknown_;
759 else {
760 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
761 expected_metadata_sequence_[num_expected_++] = &padding_;
762 expected_metadata_sequence_[num_expected_++] = &seektable_;
763 expected_metadata_sequence_[num_expected_++] = &application1_;
764 expected_metadata_sequence_[num_expected_++] = &application2_;
765 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
766 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
767 expected_metadata_sequence_[num_expected_++] = &picture_;
768 expected_metadata_sequence_[num_expected_++] = &unknown_;
771 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
772 return false;
775 * ignore all
778 printf("testing set_metadata_ignore_all()... ");
779 if(!decoder->set_metadata_ignore_all()) {
780 printf("FAILED, returned false\n");
781 return false;
783 printf("OK\n");
785 num_expected_ = 0;
787 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
788 return false;
791 * respond all, ignore VORBIS_COMMENT
794 printf("testing set_metadata_respond_all()... ");
795 if(!decoder->set_metadata_respond_all()) {
796 printf("FAILED, returned false\n");
797 return false;
799 printf("OK\n");
801 printf("testing set_metadata_ignore(VORBIS_COMMENT)... ");
802 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
803 printf("FAILED, returned false\n");
804 return false;
806 printf("OK\n");
808 num_expected_ = 0;
809 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
810 expected_metadata_sequence_[num_expected_++] = &padding_;
811 expected_metadata_sequence_[num_expected_++] = &seektable_;
812 expected_metadata_sequence_[num_expected_++] = &application1_;
813 expected_metadata_sequence_[num_expected_++] = &application2_;
814 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
815 expected_metadata_sequence_[num_expected_++] = &picture_;
816 expected_metadata_sequence_[num_expected_++] = &unknown_;
818 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
819 return false;
822 * respond all, ignore APPLICATION
825 printf("testing set_metadata_respond_all()... ");
826 if(!decoder->set_metadata_respond_all()) {
827 printf("FAILED, returned false\n");
828 return false;
830 printf("OK\n");
832 printf("testing set_metadata_ignore(APPLICATION)... ");
833 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
834 printf("FAILED, returned false\n");
835 return false;
837 printf("OK\n");
839 num_expected_ = 0;
840 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping */
841 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
842 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
843 expected_metadata_sequence_[num_expected_++] = &padding_;
844 expected_metadata_sequence_[num_expected_++] = &seektable_;
845 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
846 expected_metadata_sequence_[num_expected_++] = &picture_;
847 expected_metadata_sequence_[num_expected_++] = &unknown_;
849 else {
850 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
851 expected_metadata_sequence_[num_expected_++] = &padding_;
852 expected_metadata_sequence_[num_expected_++] = &seektable_;
853 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
854 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
855 expected_metadata_sequence_[num_expected_++] = &picture_;
856 expected_metadata_sequence_[num_expected_++] = &unknown_;
859 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
860 return false;
863 * respond all, ignore APPLICATION id of app#1
866 printf("testing set_metadata_respond_all()... ");
867 if(!decoder->set_metadata_respond_all()) {
868 printf("FAILED, returned false\n");
869 return false;
871 printf("OK\n");
873 printf("testing set_metadata_ignore_application(of app block #1)... ");
874 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
875 printf("FAILED, returned false\n");
876 return false;
878 printf("OK\n");
880 num_expected_ = 0;
881 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping */
882 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
883 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
884 expected_metadata_sequence_[num_expected_++] = &padding_;
885 expected_metadata_sequence_[num_expected_++] = &seektable_;
886 expected_metadata_sequence_[num_expected_++] = &application2_;
887 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
888 expected_metadata_sequence_[num_expected_++] = &picture_;
889 expected_metadata_sequence_[num_expected_++] = &unknown_;
891 else {
892 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
893 expected_metadata_sequence_[num_expected_++] = &padding_;
894 expected_metadata_sequence_[num_expected_++] = &seektable_;
895 expected_metadata_sequence_[num_expected_++] = &application2_;
896 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
897 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
898 expected_metadata_sequence_[num_expected_++] = &picture_;
899 expected_metadata_sequence_[num_expected_++] = &unknown_;
902 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
903 return false;
906 * respond all, ignore APPLICATION id of app#1 & app#2
909 printf("testing set_metadata_respond_all()... ");
910 if(!decoder->set_metadata_respond_all()) {
911 printf("FAILED, returned false\n");
912 return false;
914 printf("OK\n");
916 printf("testing set_metadata_ignore_application(of app block #1)... ");
917 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
918 printf("FAILED, returned false\n");
919 return false;
921 printf("OK\n");
923 printf("testing set_metadata_ignore_application(of app block #2)... ");
924 if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) {
925 printf("FAILED, returned false\n");
926 return false;
928 printf("OK\n");
930 num_expected_ = 0;
931 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping */
932 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
933 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
934 expected_metadata_sequence_[num_expected_++] = &padding_;
935 expected_metadata_sequence_[num_expected_++] = &seektable_;
936 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
937 expected_metadata_sequence_[num_expected_++] = &picture_;
938 expected_metadata_sequence_[num_expected_++] = &unknown_;
940 else {
941 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
942 expected_metadata_sequence_[num_expected_++] = &padding_;
943 expected_metadata_sequence_[num_expected_++] = &seektable_;
944 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
945 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
946 expected_metadata_sequence_[num_expected_++] = &picture_;
947 expected_metadata_sequence_[num_expected_++] = &unknown_;
950 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
951 return false;
954 * ignore all, respond VORBIS_COMMENT
957 printf("testing set_metadata_ignore_all()... ");
958 if(!decoder->set_metadata_ignore_all()) {
959 printf("FAILED, returned false\n");
960 return false;
962 printf("OK\n");
964 printf("testing set_metadata_respond(VORBIS_COMMENT)... ");
965 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
966 printf("FAILED, returned false\n");
967 return false;
969 printf("OK\n");
971 num_expected_ = 0;
972 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
974 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
975 return false;
978 * ignore all, respond APPLICATION
981 printf("testing set_metadata_ignore_all()... ");
982 if(!decoder->set_metadata_ignore_all()) {
983 printf("FAILED, returned false\n");
984 return false;
986 printf("OK\n");
988 printf("testing set_metadata_respond(APPLICATION)... ");
989 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
990 printf("FAILED, returned false\n");
991 return false;
993 printf("OK\n");
995 num_expected_ = 0;
996 expected_metadata_sequence_[num_expected_++] = &application1_;
997 expected_metadata_sequence_[num_expected_++] = &application2_;
999 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1000 return false;
1003 * ignore all, respond APPLICATION id of app#1
1006 printf("testing set_metadata_ignore_all()... ");
1007 if(!decoder->set_metadata_ignore_all()) {
1008 printf("FAILED, returned false\n");
1009 return false;
1011 printf("OK\n");
1013 printf("testing set_metadata_respond_application(of app block #1)... ");
1014 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1015 printf("FAILED, returned false\n");
1016 return false;
1018 printf("OK\n");
1020 num_expected_ = 0;
1021 expected_metadata_sequence_[num_expected_++] = &application1_;
1023 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1024 return false;
1027 * ignore all, respond APPLICATION id of app#1 & app#2
1030 printf("testing set_metadata_ignore_all()... ");
1031 if(!decoder->set_metadata_ignore_all()) {
1032 printf("FAILED, returned false\n");
1033 return false;
1035 printf("OK\n");
1037 printf("testing set_metadata_respond_application(of app block #1)... ");
1038 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1039 printf("FAILED, returned false\n");
1040 return false;
1042 printf("OK\n");
1044 printf("testing set_metadata_respond_application(of app block #2)... ");
1045 if(!decoder->set_metadata_respond_application(application2_.data.application.id)) {
1046 printf("FAILED, returned false\n");
1047 return false;
1049 printf("OK\n");
1051 num_expected_ = 0;
1052 expected_metadata_sequence_[num_expected_++] = &application1_;
1053 expected_metadata_sequence_[num_expected_++] = &application2_;
1055 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1056 return false;
1059 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
1062 printf("testing set_metadata_respond_all()... ");
1063 if(!decoder->set_metadata_respond_all()) {
1064 printf("FAILED, returned false\n");
1065 return false;
1067 printf("OK\n");
1069 printf("testing set_metadata_ignore(APPLICATION)... ");
1070 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1071 printf("FAILED, returned false\n");
1072 return false;
1074 printf("OK\n");
1076 printf("testing set_metadata_respond_application(of app block #1)... ");
1077 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1078 printf("FAILED, returned false\n");
1079 return false;
1081 printf("OK\n");
1083 num_expected_ = 0;
1084 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping */
1085 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1086 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1087 expected_metadata_sequence_[num_expected_++] = &padding_;
1088 expected_metadata_sequence_[num_expected_++] = &seektable_;
1089 expected_metadata_sequence_[num_expected_++] = &application1_;
1090 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1091 expected_metadata_sequence_[num_expected_++] = &picture_;
1092 expected_metadata_sequence_[num_expected_++] = &unknown_;
1094 else {
1095 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1096 expected_metadata_sequence_[num_expected_++] = &padding_;
1097 expected_metadata_sequence_[num_expected_++] = &seektable_;
1098 expected_metadata_sequence_[num_expected_++] = &application1_;
1099 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1100 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1101 expected_metadata_sequence_[num_expected_++] = &picture_;
1102 expected_metadata_sequence_[num_expected_++] = &unknown_;
1105 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1106 return false;
1109 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1112 printf("testing set_metadata_ignore_all()... ");
1113 if(!decoder->set_metadata_ignore_all()) {
1114 printf("FAILED, returned false\n");
1115 return false;
1117 printf("OK\n");
1119 printf("testing set_metadata_respond(APPLICATION)... ");
1120 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1121 printf("FAILED, returned false\n");
1122 return false;
1124 printf("OK\n");
1126 printf("testing set_metadata_ignore_application(of app block #1)... ");
1127 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1128 printf("FAILED, returned false\n");
1129 return false;
1131 printf("OK\n");
1133 num_expected_ = 0;
1134 expected_metadata_sequence_[num_expected_++] = &application2_;
1136 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1137 return false;
1139 if(layer < LAYER_FILE) /* for LAYER_FILE, FLAC__stream_decoder_finish() closes the file */
1140 ::fclose(dynamic_cast<StreamDecoder*>(decoder)->file_);
1142 printf("freeing decoder instance... ");
1143 delete decoder;
1144 printf("OK\n");
1146 printf("\nPASSED!\n");
1148 return true;
1151 bool test_decoders()
1153 FLAC__bool is_ogg = false;
1155 while(1) {
1156 init_metadata_blocks_();
1158 if(!generate_file_(is_ogg))
1159 return false;
1161 if(!test_stream_decoder(LAYER_STREAM, is_ogg))
1162 return false;
1164 if(!test_stream_decoder(LAYER_SEEKABLE_STREAM, is_ogg))
1165 return false;
1167 if(!test_stream_decoder(LAYER_FILE, is_ogg))
1168 return false;
1170 if(!test_stream_decoder(LAYER_FILENAME, is_ogg))
1171 return false;
1173 (void) grabbag__file_remove_file(flacfilename(is_ogg));
1175 free_metadata_blocks_();
1177 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)
1178 break;
1179 is_ogg = true;
1182 return true;