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