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