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