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