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