minor changes in anticipation of release
[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         seekable_stream_decoder_client_data_struct decoder_client_data;
1000
1001         printf("\n+++ libFLAC unit test: FLAC__SeekableStreamDecoder\n\n");
1002
1003         printf("testing FLAC__seekable_stream_decoder_new()... ");
1004         decoder = FLAC__seekable_stream_decoder_new();
1005         if(0 == decoder) {
1006                 printf("FAILED, returned NULL\n");
1007                 return false;
1008         }
1009         printf("OK\n");
1010
1011         printf("testing FLAC__seekable_stream_decoder_delete()... ");
1012         FLAC__seekable_stream_decoder_delete(decoder);
1013         printf("OK\n");
1014
1015         printf("testing FLAC__seekable_stream_decoder_new()... ");
1016         decoder = FLAC__seekable_stream_decoder_new();
1017         if(0 == decoder) {
1018                 printf("FAILED, returned NULL\n");
1019                 return false;
1020         }
1021         printf("OK\n");
1022
1023         printf("testing FLAC__seekable_stream_decoder_init()... ");
1024         if(FLAC__seekable_stream_decoder_init(decoder) == FLAC__SEEKABLE_STREAM_DECODER_OK)
1025                 return die_ss_(0, decoder);
1026         printf("OK\n");
1027
1028         printf("testing FLAC__seekable_stream_decoder_delete()... ");
1029         FLAC__seekable_stream_decoder_delete(decoder);
1030         printf("OK\n");
1031
1032         num_expected_ = 0;
1033         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1034
1035         printf("testing FLAC__seekable_stream_decoder_new()... ");
1036         decoder = FLAC__seekable_stream_decoder_new();
1037         if(0 == decoder) {
1038                 printf("FAILED, returned NULL\n");
1039                 return false;
1040         }
1041         printf("OK\n");
1042
1043         printf("testing FLAC__seekable_stream_decoder_set_read_callback()... ");
1044         if(!FLAC__seekable_stream_decoder_set_read_callback(decoder, seekable_stream_decoder_read_callback_))
1045                 return die_ss_("returned false", decoder);
1046         printf("OK\n");
1047
1048         printf("testing FLAC__seekable_stream_decoder_set_seek_callback()... ");
1049         if(!FLAC__seekable_stream_decoder_set_seek_callback(decoder, seekable_stream_decoder_seek_callback_))
1050                 return die_ss_("returned false", decoder);
1051         printf("OK\n");
1052
1053         printf("testing FLAC__seekable_stream_decoder_set_tell_callback()... ");
1054         if(!FLAC__seekable_stream_decoder_set_tell_callback(decoder, seekable_stream_decoder_tell_callback_))
1055                 return die_ss_("returned false", decoder);
1056         printf("OK\n");
1057
1058         printf("testing FLAC__seekable_stream_decoder_set_length_callback()... ");
1059         if(!FLAC__seekable_stream_decoder_set_length_callback(decoder, seekable_stream_decoder_length_callback_))
1060                 return die_ss_("returned false", decoder);
1061         printf("OK\n");
1062
1063         printf("testing FLAC__seekable_stream_decoder_set_eof_callback()... ");
1064         if(!FLAC__seekable_stream_decoder_set_eof_callback(decoder, seekable_stream_decoder_eof_callback_))
1065                 return die_ss_("returned false", decoder);
1066         printf("OK\n");
1067
1068         printf("testing FLAC__seekable_stream_decoder_set_write_callback()... ");
1069         if(!FLAC__seekable_stream_decoder_set_write_callback(decoder, seekable_stream_decoder_write_callback_))
1070                 return die_ss_("returned false", decoder);
1071         printf("OK\n");
1072
1073         printf("testing FLAC__seekable_stream_decoder_set_metadata_callback()... ");
1074         if(!FLAC__seekable_stream_decoder_set_metadata_callback(decoder, seekable_stream_decoder_metadata_callback_))
1075                 return die_ss_("returned false", decoder);
1076         printf("OK\n");
1077
1078         printf("testing FLAC__seekable_stream_decoder_set_error_callback()... ");
1079         if(!FLAC__seekable_stream_decoder_set_error_callback(decoder, seekable_stream_decoder_error_callback_))
1080                 return die_ss_("returned false", decoder);
1081         printf("OK\n");
1082
1083         printf("testing FLAC__seekable_stream_decoder_set_client_data()... ");
1084         if(!FLAC__seekable_stream_decoder_set_client_data(decoder, &decoder_client_data))
1085                 return die_ss_("returned false", decoder);
1086         printf("OK\n");
1087
1088         printf("testing FLAC__seekable_stream_decoder_set_md5_checking()... ");
1089         if(!FLAC__seekable_stream_decoder_set_md5_checking(decoder, true))
1090                 return die_ss_("returned false", decoder);
1091         printf("OK\n");
1092
1093         printf("testing FLAC__seekable_stream_decoder_init()... ");
1094         if(FLAC__seekable_stream_decoder_init(decoder) != FLAC__SEEKABLE_STREAM_DECODER_OK)
1095                 return die_ss_(0, decoder);
1096         printf("OK\n");
1097
1098         printf("testing FLAC__seekable_stream_decoder_get_state()... ");
1099         state = FLAC__seekable_stream_decoder_get_state(decoder);
1100         printf("returned state = %u (%s)... OK\n", state, FLAC__SeekableStreamDecoderStateString[state]);
1101
1102         decoder_client_data.current_metadata_number = 0;
1103         decoder_client_data.ignore_errors = false;
1104         decoder_client_data.error_occurred = false;
1105
1106         printf("opening FLAC file... ");
1107         decoder_client_data.file = fopen(flacfilename_, "rb");
1108         if(0 == decoder_client_data.file) {
1109                 printf("ERROR\n");
1110                 return false;
1111         }
1112         printf("OK\n");
1113
1114         printf("testing FLAC__seekable_stream_decoder_get_md5_checking()... ");
1115         if(!FLAC__seekable_stream_decoder_get_md5_checking(decoder)) {
1116                 printf("FAILED, returned false, expected true\n");
1117                 return false;
1118         }
1119         printf("OK\n");
1120
1121         printf("testing FLAC__seekable_stream_decoder_process_metadata()... ");
1122         if(!FLAC__seekable_stream_decoder_process_metadata(decoder))
1123                 return die_ss_("returned false", decoder);
1124         printf("OK\n");
1125
1126         printf("testing FLAC__seekable_stream_decoder_process_one_frame()... ");
1127         if(!FLAC__seekable_stream_decoder_process_one_frame(decoder))
1128                 return die_ss_("returned false", decoder);
1129         printf("OK\n");
1130
1131         printf("testing FLAC__seekable_stream_decoder_flush()... ");
1132         if(!FLAC__seekable_stream_decoder_flush(decoder))
1133                 return die_ss_("returned false", decoder);
1134         printf("OK\n");
1135
1136         decoder_client_data.ignore_errors = true;
1137         printf("testing FLAC__seekable_stream_decoder_process_one_frame()... ");
1138         if(!FLAC__seekable_stream_decoder_process_one_frame(decoder))
1139                 return die_ss_("returned false", decoder);
1140         printf("OK\n");
1141         decoder_client_data.ignore_errors = false;
1142
1143         printf("testing FLAC__seekable_stream_decoder_seek_absolute()... ");
1144         if(!FLAC__seekable_stream_decoder_seek_absolute(decoder, 0))
1145                 return die_ss_("returned false", decoder);
1146         printf("OK\n");
1147
1148         printf("testing FLAC__seekable_stream_decoder_process_remaining_frames()... ");
1149         if(!FLAC__seekable_stream_decoder_process_remaining_frames(decoder))
1150                 return die_ss_("returned false", decoder);
1151         printf("OK\n");
1152
1153         printf("testing FLAC__seekable_stream_decoder_get_channels()... ");
1154         {
1155                 unsigned channels = FLAC__seekable_stream_decoder_get_channels(decoder);
1156                 if(channels != streaminfo_.data.stream_info.channels) {
1157                         printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
1158                         return false;
1159                 }
1160         }
1161         printf("OK\n");
1162
1163         printf("testing FLAC__seekable_stream_decoder_get_bits_per_sample()... ");
1164         {
1165                 unsigned bits_per_sample = FLAC__seekable_stream_decoder_get_bits_per_sample(decoder);
1166                 if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
1167                         printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
1168                         return false;
1169                 }
1170         }
1171         printf("OK\n");
1172
1173         printf("testing FLAC__seekable_stream_decoder_get_sample_rate()... ");
1174         {
1175                 unsigned sample_rate = FLAC__seekable_stream_decoder_get_sample_rate(decoder);
1176                 if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
1177                         printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
1178                         return false;
1179                 }
1180         }
1181         printf("OK\n");
1182
1183         printf("testing FLAC__seekable_stream_decoder_get_blocksize()... ");
1184         {
1185                 unsigned blocksize = FLAC__seekable_stream_decoder_get_blocksize(decoder);
1186                 /* value could be anything since we're at the last block, so accept any answer */
1187                 printf("returned %u... OK\n", blocksize);
1188         }
1189
1190         printf("testing FLAC__seekable_stream_decoder_get_channel_assignment()... ");
1191         {
1192                 FLAC__ChannelAssignment ca = FLAC__seekable_stream_decoder_get_channel_assignment(decoder);
1193                 printf("returned %u (%s)... OK\n", (unsigned)ca, FLAC__ChannelAssignmentString[ca]);
1194         }
1195
1196         printf("testing FLAC__seekable_stream_decoder_reset()... ");
1197         if(!FLAC__seekable_stream_decoder_reset(decoder))
1198                 return die_ss_("returned false", decoder);
1199         printf("OK\n");
1200
1201         decoder_client_data.current_metadata_number = 0;
1202
1203         printf("rewinding input... ");
1204         if(fseek(decoder_client_data.file, 0, SEEK_SET) < 0) {
1205                 printf("FAILED, errno = %d\n", errno);
1206                 return false;
1207         }
1208         printf("OK\n");
1209
1210         printf("testing FLAC__seekable_stream_decoder_process_whole_stream()... ");
1211         if(!FLAC__seekable_stream_decoder_process_whole_stream(decoder))
1212                 return die_ss_("returned false", decoder);
1213         printf("OK\n");
1214
1215         printf("testing FLAC__seekable_stream_decoder_finish()... ");
1216         (void) FLAC__seekable_stream_decoder_finish(decoder);
1217         printf("OK\n");
1218
1219         /*
1220          * respond all
1221          */
1222
1223         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
1224         if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
1225                 return die_ss_("returned false", decoder);
1226         printf("OK\n");
1227
1228         num_expected_ = 0;
1229         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1230         expected_metadata_sequence_[num_expected_++] = &padding_;
1231         expected_metadata_sequence_[num_expected_++] = &seektable_;
1232         expected_metadata_sequence_[num_expected_++] = &application1_;
1233         expected_metadata_sequence_[num_expected_++] = &application2_;
1234         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1235
1236         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1237                 return false;
1238
1239         /*
1240          * ignore all
1241          */
1242
1243         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
1244         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
1245                 return die_ss_("returned false", decoder);
1246         printf("OK\n");
1247
1248         num_expected_ = 0;
1249
1250         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1251                 return false;
1252
1253         /*
1254          * respond all, ignore VORBIS_COMMENT
1255          */
1256
1257         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
1258         if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
1259                 return die_ss_("returned false", decoder);
1260         printf("OK\n");
1261
1262         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore(VORBIS_COMMENT)... ");
1263         if(!FLAC__seekable_stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
1264                 return die_ss_("returned false", decoder);
1265         printf("OK\n");
1266
1267         num_expected_ = 0;
1268         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1269         expected_metadata_sequence_[num_expected_++] = &padding_;
1270         expected_metadata_sequence_[num_expected_++] = &seektable_;
1271         expected_metadata_sequence_[num_expected_++] = &application1_;
1272         expected_metadata_sequence_[num_expected_++] = &application2_;
1273
1274         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1275                 return false;
1276
1277         /*
1278          * respond all, ignore APPLICATION
1279          */
1280
1281         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
1282         if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
1283                 return die_ss_("returned false", decoder);
1284         printf("OK\n");
1285
1286         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore(APPLICATION)... ");
1287         if(!FLAC__seekable_stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
1288                 return die_ss_("returned false", decoder);
1289         printf("OK\n");
1290
1291         num_expected_ = 0;
1292         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1293         expected_metadata_sequence_[num_expected_++] = &padding_;
1294         expected_metadata_sequence_[num_expected_++] = &seektable_;
1295         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1296
1297         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1298                 return false;
1299
1300         /*
1301          * respond all, ignore APPLICATION id of app#1
1302          */
1303
1304         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
1305         if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
1306                 return die_ss_("returned false", decoder);
1307         printf("OK\n");
1308
1309         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_application(of app block #1)... ");
1310         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
1311                 return die_ss_("returned false", decoder);
1312         printf("OK\n");
1313
1314         num_expected_ = 0;
1315         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1316         expected_metadata_sequence_[num_expected_++] = &padding_;
1317         expected_metadata_sequence_[num_expected_++] = &seektable_;
1318         expected_metadata_sequence_[num_expected_++] = &application2_;
1319         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1320
1321         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1322                 return false;
1323
1324         /*
1325          * respond all, ignore APPLICATION id of app#1 & app#2
1326          */
1327
1328         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
1329         if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
1330                 return die_ss_("returned false", decoder);
1331         printf("OK\n");
1332
1333         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_application(of app block #1)... ");
1334         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
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 #2)... ");
1339         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder, application2_.data.application.id))
1340                 return die_ss_("returned false", decoder);
1341         printf("OK\n");
1342
1343         num_expected_ = 0;
1344         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1345         expected_metadata_sequence_[num_expected_++] = &padding_;
1346         expected_metadata_sequence_[num_expected_++] = &seektable_;
1347         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1348
1349         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1350                 return false;
1351
1352         /*
1353          * ignore all, respond VORBIS_COMMENT
1354          */
1355
1356         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
1357         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
1358                 return die_ss_("returned false", decoder);
1359         printf("OK\n");
1360
1361         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond(VORBIS_COMMENT)... ");
1362         if(!FLAC__seekable_stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
1363                 return die_ss_("returned false", decoder);
1364         printf("OK\n");
1365
1366         num_expected_ = 0;
1367         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1368
1369         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1370                 return false;
1371
1372         /*
1373          * ignore all, respond APPLICATION
1374          */
1375
1376         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
1377         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
1378                 return die_ss_("returned false", decoder);
1379         printf("OK\n");
1380
1381         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond(APPLICATION)... ");
1382         if(!FLAC__seekable_stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
1383                 return die_ss_("returned false", decoder);
1384         printf("OK\n");
1385
1386         num_expected_ = 0;
1387         expected_metadata_sequence_[num_expected_++] = &application1_;
1388         expected_metadata_sequence_[num_expected_++] = &application2_;
1389
1390         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1391                 return false;
1392
1393         /*
1394          * ignore all, respond APPLICATION id of app#1
1395          */
1396
1397         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
1398         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
1399                 return die_ss_("returned false", decoder);
1400         printf("OK\n");
1401
1402         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_application(of app block #1)... ");
1403         if(!FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
1404                 return die_ss_("returned false", decoder);
1405         printf("OK\n");
1406
1407         num_expected_ = 0;
1408         expected_metadata_sequence_[num_expected_++] = &application1_;
1409
1410         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1411                 return false;
1412
1413         /*
1414          * ignore all, respond APPLICATION id of app#1 & app#2
1415          */
1416
1417         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
1418         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
1419                 return die_ss_("returned false", decoder);
1420         printf("OK\n");
1421
1422         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_application(of app block #1)... ");
1423         if(!FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
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 #2)... ");
1428         if(!FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder, application2_.data.application.id))
1429                 return die_ss_("returned false", decoder);
1430         printf("OK\n");
1431
1432         num_expected_ = 0;
1433         expected_metadata_sequence_[num_expected_++] = &application1_;
1434         expected_metadata_sequence_[num_expected_++] = &application2_;
1435
1436         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1437                 return false;
1438
1439         /*
1440          * respond all, ignore APPLICATION, respond APPLICATION id of app#1
1441          */
1442
1443         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_all()... ");
1444         if(!FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder))
1445                 return die_ss_("returned false", decoder);
1446         printf("OK\n");
1447
1448         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore(APPLICATION)... ");
1449         if(!FLAC__seekable_stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
1450                 return die_ss_("returned false", decoder);
1451         printf("OK\n");
1452
1453         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond_application(of app block #1)... ");
1454         if(!FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
1455                 return die_ss_("returned false", decoder);
1456         printf("OK\n");
1457
1458         num_expected_ = 0;
1459         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1460         expected_metadata_sequence_[num_expected_++] = &padding_;
1461         expected_metadata_sequence_[num_expected_++] = &seektable_;
1462         expected_metadata_sequence_[num_expected_++] = &application1_;
1463         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1464
1465         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1466                 return false;
1467
1468         /*
1469          * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1470          */
1471
1472         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_all()... ");
1473         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder))
1474                 return die_ss_("returned false", decoder);
1475         printf("OK\n");
1476
1477         printf("testing FLAC__seekable_stream_decoder_set_metadata_respond(APPLICATION)... ");
1478         if(!FLAC__seekable_stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
1479                 return die_ss_("returned false", decoder);
1480         printf("OK\n");
1481
1482         printf("testing FLAC__seekable_stream_decoder_set_metadata_ignore_application(of app block #1)... ");
1483         if(!FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
1484                 return die_ss_("returned false", decoder);
1485         printf("OK\n");
1486
1487         num_expected_ = 0;
1488         expected_metadata_sequence_[num_expected_++] = &application2_;
1489
1490         if(!seekable_stream_decoder_test_respond_(decoder, &decoder_client_data))
1491                 return false;
1492
1493         /* done, now leave the sequence the way we found it... */
1494         num_expected_ = 0;
1495         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1496         expected_metadata_sequence_[num_expected_++] = &padding_;
1497         expected_metadata_sequence_[num_expected_++] = &seektable_;
1498         expected_metadata_sequence_[num_expected_++] = &application1_;
1499         expected_metadata_sequence_[num_expected_++] = &application2_;
1500         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1501
1502         printf("testing FLAC__seekable_stream_decoder_delete()... ");
1503         FLAC__seekable_stream_decoder_delete(decoder);
1504         printf("OK\n");
1505
1506         fclose(decoder_client_data.file);
1507
1508         printf("\nPASSED!\n");
1509
1510         return true;
1511 }
1512
1513 static FLAC__StreamDecoderWriteStatus file_decoder_write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
1514 {
1515         (void)decoder;
1516         return stream_decoder_write_callback_(0, frame, buffer, client_data);
1517 }
1518
1519 static void file_decoder_metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
1520 {
1521         (void)decoder;
1522         stream_decoder_metadata_callback_(0, metadata, client_data);
1523 }
1524
1525 static void file_decoder_error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
1526 {
1527         (void)decoder;
1528         stream_decoder_error_callback_(0, status, client_data);
1529 }
1530
1531 static FLAC__bool file_decoder_test_respond_(FLAC__FileDecoder *decoder, file_decoder_client_data_struct *dcd)
1532 {
1533         if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
1534                 return die_f_("at FLAC__file_decoder_set_write_callback(), returned false", decoder);
1535
1536         if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
1537                 return die_f_("at FLAC__file_decoder_set_metadata_callback(), returned false", decoder);
1538
1539         if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
1540                 return die_f_("at FLAC__file_decoder_set_error_callback(), returned false", decoder);
1541
1542         if(!FLAC__file_decoder_set_client_data(decoder, dcd))
1543                 return die_f_("at FLAC__file_decoder_set_client_data(), returned false", decoder);
1544
1545         if(!FLAC__file_decoder_set_filename(decoder, flacfilename_))
1546                 return die_f_("at FLAC__file_decoder_set_filename(), returned false", decoder);
1547
1548         if(!FLAC__file_decoder_set_md5_checking(decoder, true))
1549                 return die_f_("at FLAC__file_decoder_set_md5_checking(), returned false", decoder);
1550
1551         printf("testing FLAC__file_decoder_init()... ");
1552         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
1553                 return die_f_(0, decoder);
1554         printf("OK\n");
1555
1556         dcd->current_metadata_number = 0;
1557
1558         printf("testing FLAC__file_decoder_process_whole_file()... ");
1559         if(!FLAC__file_decoder_process_whole_file(decoder))
1560                 return die_f_("returned false", decoder);
1561         printf("OK\n");
1562
1563         printf("testing FLAC__file_decoder_finish()... ");
1564         (void) FLAC__file_decoder_finish(decoder);
1565         printf("OK\n");
1566
1567         return true;
1568 }
1569
1570 static FLAC__bool test_file_decoder()
1571 {
1572         FLAC__FileDecoder *decoder;
1573         FLAC__FileDecoderState state;
1574         seekable_stream_decoder_client_data_struct decoder_client_data;
1575
1576         printf("\n+++ libFLAC unit test: FLAC__FileDecoder\n\n");
1577
1578         printf("testing FLAC__file_decoder_new()... ");
1579         decoder = FLAC__file_decoder_new();
1580         if(0 == decoder) {
1581                 printf("FAILED, returned NULL\n");
1582                 return false;
1583         }
1584         printf("OK\n");
1585
1586         printf("testing FLAC__file_decoder_delete()... ");
1587         FLAC__file_decoder_delete(decoder);
1588         printf("OK\n");
1589
1590         printf("testing FLAC__file_decoder_new()... ");
1591         decoder = FLAC__file_decoder_new();
1592         if(0 == decoder) {
1593                 printf("FAILED, returned NULL\n");
1594                 return false;
1595         }
1596         printf("OK\n");
1597
1598         printf("testing FLAC__file_decoder_init()... ");
1599         if(FLAC__file_decoder_init(decoder) == FLAC__FILE_DECODER_OK)
1600                 return die_f_(0, decoder);
1601         printf("OK\n");
1602
1603         printf("testing FLAC__file_decoder_delete()... ");
1604         FLAC__file_decoder_delete(decoder);
1605         printf("OK\n");
1606
1607         num_expected_ = 0;
1608         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1609
1610         printf("testing FLAC__file_decoder_new()... ");
1611         decoder = FLAC__file_decoder_new();
1612         if(0 == decoder) {
1613                 printf("FAILED, returned NULL\n");
1614                 return false;
1615         }
1616         printf("OK\n");
1617
1618         printf("testing FLAC__file_decoder_set_write_callback()... ");
1619         if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_))
1620                 return die_f_("returned false", decoder);
1621         printf("OK\n");
1622
1623         printf("testing FLAC__file_decoder_set_metadata_callback()... ");
1624         if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_))
1625                 return die_f_("returned false", decoder);
1626         printf("OK\n");
1627
1628         printf("testing FLAC__file_decoder_set_error_callback()... ");
1629         if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_))
1630                 return die_f_("returned false", decoder);
1631         printf("OK\n");
1632
1633         printf("testing FLAC__file_decoder_set_client_data()... ");
1634         if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data))
1635                 return die_f_("returned false", decoder);
1636         printf("OK\n");
1637
1638         printf("testing FLAC__file_decoder_set_filename()... ");
1639         if(!FLAC__file_decoder_set_filename(decoder, flacfilename_))
1640                 return die_f_("returned false", decoder);
1641         printf("OK\n");
1642
1643         printf("testing FLAC__file_decoder_set_md5_checking()... ");
1644         if(!FLAC__file_decoder_set_md5_checking(decoder, true))
1645                 return die_f_("returned false", decoder);
1646         printf("OK\n");
1647
1648         printf("testing FLAC__file_decoder_init()... ");
1649         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
1650                 return die_f_(0, decoder);
1651         printf("OK\n");
1652
1653         printf("testing FLAC__file_decoder_get_state()... ");
1654         state = FLAC__file_decoder_get_state(decoder);
1655         printf("returned state = %u (%s)... OK\n", state, FLAC__FileDecoderStateString[state]);
1656
1657         decoder_client_data.current_metadata_number = 0;
1658         decoder_client_data.ignore_errors = false;
1659         decoder_client_data.error_occurred = false;
1660
1661         printf("testing FLAC__file_decoder_get_md5_checking()... ");
1662         if(!FLAC__file_decoder_get_md5_checking(decoder)) {
1663                 printf("FAILED, returned false, expected true\n");
1664                 return false;
1665         }
1666         printf("OK\n");
1667
1668         printf("testing FLAC__file_decoder_process_metadata()... ");
1669         if(!FLAC__file_decoder_process_metadata(decoder))
1670                 return die_f_("returned false", decoder);
1671         printf("OK\n");
1672
1673         printf("testing FLAC__file_decoder_process_one_frame()... ");
1674         if(!FLAC__file_decoder_process_one_frame(decoder))
1675                 return die_f_("returned false", decoder);
1676         printf("OK\n");
1677
1678         printf("testing FLAC__file_decoder_seek_absolute()... ");
1679         if(!FLAC__file_decoder_seek_absolute(decoder, 0))
1680                 return die_f_("returned false", decoder);
1681         printf("OK\n");
1682
1683         printf("testing FLAC__file_decoder_process_remaining_frames()... ");
1684         if(!FLAC__file_decoder_process_remaining_frames(decoder))
1685                 return die_f_("returned false", decoder);
1686         printf("OK\n");
1687
1688         printf("testing FLAC__file_decoder_get_channels()... ");
1689         {
1690                 unsigned channels = FLAC__file_decoder_get_channels(decoder);
1691                 if(channels != streaminfo_.data.stream_info.channels) {
1692                         printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
1693                         return false;
1694                 }
1695         }
1696         printf("OK\n");
1697
1698         printf("testing FLAC__file_decoder_get_bits_per_sample()... ");
1699         {
1700                 unsigned bits_per_sample = FLAC__file_decoder_get_bits_per_sample(decoder);
1701                 if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
1702                         printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
1703                         return false;
1704                 }
1705         }
1706         printf("OK\n");
1707
1708         printf("testing FLAC__file_decoder_get_sample_rate()... ");
1709         {
1710                 unsigned sample_rate = FLAC__file_decoder_get_sample_rate(decoder);
1711                 if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
1712                         printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
1713                         return false;
1714                 }
1715         }
1716         printf("OK\n");
1717
1718         printf("testing FLAC__file_decoder_get_blocksize()... ");
1719         {
1720                 unsigned blocksize = FLAC__file_decoder_get_blocksize(decoder);
1721                 /* value could be anything since we're at the last block, so accept any answer */
1722                 printf("returned %u... OK\n", blocksize);
1723         }
1724
1725         printf("testing FLAC__file_decoder_get_channel_assignment()... ");
1726         {
1727                 FLAC__ChannelAssignment ca = FLAC__file_decoder_get_channel_assignment(decoder);
1728                 printf("returned %u (%s)... OK\n", (unsigned)ca, FLAC__ChannelAssignmentString[ca]);
1729         }
1730
1731         printf("testing FLAC__file_decoder_finish()... ");
1732         (void) FLAC__file_decoder_finish(decoder);
1733         printf("OK\n");
1734
1735         /*
1736          * respond all
1737          */
1738
1739         printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
1740         if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
1741                 return die_f_("returned false", decoder);
1742         printf("OK\n");
1743
1744         num_expected_ = 0;
1745         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1746         expected_metadata_sequence_[num_expected_++] = &padding_;
1747         expected_metadata_sequence_[num_expected_++] = &seektable_;
1748         expected_metadata_sequence_[num_expected_++] = &application1_;
1749         expected_metadata_sequence_[num_expected_++] = &application2_;
1750         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1751
1752         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1753                 return false;
1754
1755         /*
1756          * ignore all
1757          */
1758
1759         printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
1760         if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
1761                 return die_f_("returned false", decoder);
1762         printf("OK\n");
1763
1764         num_expected_ = 0;
1765
1766         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1767                 return false;
1768
1769         /*
1770          * respond all, ignore VORBIS_COMMENT
1771          */
1772
1773         printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
1774         if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
1775                 return die_f_("returned false", decoder);
1776         printf("OK\n");
1777
1778         printf("testing FLAC__file_decoder_set_metadata_ignore(VORBIS_COMMENT)... ");
1779         if(!FLAC__file_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
1780                 return die_f_("returned false", decoder);
1781         printf("OK\n");
1782
1783         num_expected_ = 0;
1784         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1785         expected_metadata_sequence_[num_expected_++] = &padding_;
1786         expected_metadata_sequence_[num_expected_++] = &seektable_;
1787         expected_metadata_sequence_[num_expected_++] = &application1_;
1788         expected_metadata_sequence_[num_expected_++] = &application2_;
1789
1790         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1791                 return false;
1792
1793         /*
1794          * respond all, ignore APPLICATION
1795          */
1796
1797         printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
1798         if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
1799                 return die_f_("returned false", decoder);
1800         printf("OK\n");
1801
1802         printf("testing FLAC__file_decoder_set_metadata_ignore(APPLICATION)... ");
1803         if(!FLAC__file_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
1804                 return die_f_("returned false", decoder);
1805         printf("OK\n");
1806
1807         num_expected_ = 0;
1808         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1809         expected_metadata_sequence_[num_expected_++] = &padding_;
1810         expected_metadata_sequence_[num_expected_++] = &seektable_;
1811         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1812
1813         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1814                 return false;
1815
1816         /*
1817          * respond all, ignore APPLICATION id of app#1
1818          */
1819
1820         printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
1821         if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
1822                 return die_f_("returned false", decoder);
1823         printf("OK\n");
1824
1825         printf("testing FLAC__file_decoder_set_metadata_ignore_application(of app block #1)... ");
1826         if(!FLAC__file_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
1827                 return die_f_("returned false", decoder);
1828         printf("OK\n");
1829
1830         num_expected_ = 0;
1831         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1832         expected_metadata_sequence_[num_expected_++] = &padding_;
1833         expected_metadata_sequence_[num_expected_++] = &seektable_;
1834         expected_metadata_sequence_[num_expected_++] = &application2_;
1835         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1836
1837         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1838                 return false;
1839
1840         /*
1841          * respond all, ignore APPLICATION id of app#1 & app#2
1842          */
1843
1844         printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
1845         if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
1846                 return die_f_("returned false", decoder);
1847         printf("OK\n");
1848
1849         printf("testing FLAC__file_decoder_set_metadata_ignore_application(of app block #1)... ");
1850         if(!FLAC__file_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
1851                 return die_f_("returned false", decoder);
1852         printf("OK\n");
1853
1854         printf("testing FLAC__file_decoder_set_metadata_ignore_application(of app block #2)... ");
1855         if(!FLAC__file_decoder_set_metadata_ignore_application(decoder, application2_.data.application.id))
1856                 return die_f_("returned false", decoder);
1857         printf("OK\n");
1858
1859         num_expected_ = 0;
1860         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1861         expected_metadata_sequence_[num_expected_++] = &padding_;
1862         expected_metadata_sequence_[num_expected_++] = &seektable_;
1863         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1864
1865         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1866                 return false;
1867
1868         /*
1869          * ignore all, respond VORBIS_COMMENT
1870          */
1871
1872         printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
1873         if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
1874                 return die_f_("returned false", decoder);
1875         printf("OK\n");
1876
1877         printf("testing FLAC__file_decoder_set_metadata_respond(VORBIS_COMMENT)... ");
1878         if(!FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT))
1879                 return die_f_("returned false", decoder);
1880         printf("OK\n");
1881
1882         num_expected_ = 0;
1883         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1884
1885         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1886                 return false;
1887
1888         /*
1889          * ignore all, respond APPLICATION
1890          */
1891
1892         printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
1893         if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
1894                 return die_f_("returned false", decoder);
1895         printf("OK\n");
1896
1897         printf("testing FLAC__file_decoder_set_metadata_respond(APPLICATION)... ");
1898         if(!FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
1899                 return die_f_("returned false", decoder);
1900         printf("OK\n");
1901
1902         num_expected_ = 0;
1903         expected_metadata_sequence_[num_expected_++] = &application1_;
1904         expected_metadata_sequence_[num_expected_++] = &application2_;
1905
1906         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1907                 return false;
1908
1909         /*
1910          * ignore all, respond APPLICATION id of app#1
1911          */
1912
1913         printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
1914         if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
1915                 return die_f_("returned false", decoder);
1916         printf("OK\n");
1917
1918         printf("testing FLAC__file_decoder_set_metadata_respond_application(of app block #1)... ");
1919         if(!FLAC__file_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
1920                 return die_f_("returned false", decoder);
1921         printf("OK\n");
1922
1923         num_expected_ = 0;
1924         expected_metadata_sequence_[num_expected_++] = &application1_;
1925
1926         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1927                 return false;
1928
1929         /*
1930          * ignore all, respond APPLICATION id of app#1 & app#2
1931          */
1932
1933         printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
1934         if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
1935                 return die_f_("returned false", decoder);
1936         printf("OK\n");
1937
1938         printf("testing FLAC__file_decoder_set_metadata_respond_application(of app block #1)... ");
1939         if(!FLAC__file_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
1940                 return die_f_("returned false", decoder);
1941         printf("OK\n");
1942
1943         printf("testing FLAC__file_decoder_set_metadata_respond_application(of app block #2)... ");
1944         if(!FLAC__file_decoder_set_metadata_respond_application(decoder, application2_.data.application.id))
1945                 return die_f_("returned false", decoder);
1946         printf("OK\n");
1947
1948         num_expected_ = 0;
1949         expected_metadata_sequence_[num_expected_++] = &application1_;
1950         expected_metadata_sequence_[num_expected_++] = &application2_;
1951
1952         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1953                 return false;
1954
1955         /*
1956          * respond all, ignore APPLICATION, respond APPLICATION id of app#1
1957          */
1958
1959         printf("testing FLAC__file_decoder_set_metadata_respond_all()... ");
1960         if(!FLAC__file_decoder_set_metadata_respond_all(decoder))
1961                 return die_f_("returned false", decoder);
1962         printf("OK\n");
1963
1964         printf("testing FLAC__file_decoder_set_metadata_ignore(APPLICATION)... ");
1965         if(!FLAC__file_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION))
1966                 return die_f_("returned false", decoder);
1967         printf("OK\n");
1968
1969         printf("testing FLAC__file_decoder_set_metadata_respond_application(of app block #1)... ");
1970         if(!FLAC__file_decoder_set_metadata_respond_application(decoder, application1_.data.application.id))
1971                 return die_f_("returned false", decoder);
1972         printf("OK\n");
1973
1974         num_expected_ = 0;
1975         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1976         expected_metadata_sequence_[num_expected_++] = &padding_;
1977         expected_metadata_sequence_[num_expected_++] = &seektable_;
1978         expected_metadata_sequence_[num_expected_++] = &application1_;
1979         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1980
1981         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
1982                 return false;
1983
1984         /*
1985          * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1986          */
1987
1988         printf("testing FLAC__file_decoder_set_metadata_ignore_all()... ");
1989         if(!FLAC__file_decoder_set_metadata_ignore_all(decoder))
1990                 return die_f_("returned false", decoder);
1991         printf("OK\n");
1992
1993         printf("testing FLAC__file_decoder_set_metadata_respond(APPLICATION)... ");
1994         if(!FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION))
1995                 return die_f_("returned false", decoder);
1996         printf("OK\n");
1997
1998         printf("testing FLAC__file_decoder_set_metadata_ignore_application(of app block #1)... ");
1999         if(!FLAC__file_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id))
2000                 return die_f_("returned false", decoder);
2001         printf("OK\n");
2002
2003         num_expected_ = 0;
2004         expected_metadata_sequence_[num_expected_++] = &application2_;
2005
2006         if(!file_decoder_test_respond_(decoder, &decoder_client_data))
2007                 return false;
2008
2009         /* done, now leave the sequence the way we found it... */
2010         num_expected_ = 0;
2011         expected_metadata_sequence_[num_expected_++] = &streaminfo_;
2012         expected_metadata_sequence_[num_expected_++] = &padding_;
2013         expected_metadata_sequence_[num_expected_++] = &seektable_;
2014         expected_metadata_sequence_[num_expected_++] = &application1_;
2015         expected_metadata_sequence_[num_expected_++] = &application2_;
2016         expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
2017
2018         printf("testing FLAC__file_decoder_delete()... ");
2019         FLAC__file_decoder_delete(decoder);
2020         printf("OK\n");
2021
2022         printf("\nPASSED!\n");
2023
2024         return true;
2025 }
2026
2027 FLAC__bool test_decoders()
2028 {
2029         init_metadata_blocks_();
2030         if(!generate_file_())
2031                 return false;
2032
2033         if(!test_stream_decoder())
2034                 return false;
2035
2036         if(!test_seekable_stream_decoder())
2037                 return false;
2038
2039         if(!test_file_decoder())
2040                 return false;
2041
2042         (void) file_utils__remove_file(flacfilename_);
2043         free_metadata_blocks_();
2044
2045         return true;
2046 }