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