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