fix uninitialized variable error turned up by valgrind
[flac.git] / src / test_libFLAC++ / encoders.cpp
1 /* test_libFLAC++ - Unit tester for libFLAC++
2  * Copyright (C) 2002,2003,2004  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 "encoders.h"
20 extern "C" {
21 #include "file_utils.h"
22 #include "metadata_utils.h"
23 }
24 #include "FLAC/assert.h"
25 #include "FLAC++/encoder.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, unknown_;
31 static ::FLAC__StreamMetadata *metadata_sequence_[] = { &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_ };
32 static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
33 static const char *flacfilename_ = "metadata.flac";
34
35 static void init_metadata_blocks_()
36 {
37         mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
38 }
39
40 static void free_metadata_blocks_()
41 {
42         mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
43 }
44
45 class StreamEncoder : public FLAC::Encoder::Stream {
46 public:
47         StreamEncoder(): FLAC::Encoder::Stream() { }
48         ~StreamEncoder() { }
49
50         // from FLAC::Encoder::Stream
51         ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame);
52         void metadata_callback(const ::FLAC__StreamMetadata *metadata);
53
54         bool die(const char *msg = 0) const;
55 };
56
57 ::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
58 {
59         (void)buffer, (void)bytes, (void)samples, (void)current_frame;
60
61         return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
62 }
63
64 void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
65 {
66         (void)metadata;
67 }
68
69 bool StreamEncoder::die(const char *msg) const
70 {
71         State state = get_state();
72
73         if(msg)
74                 printf("FAILED, %s", msg);
75         else
76                 printf("FAILED");
77
78         printf(", state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
79         if(state == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
80                 FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
81                 printf("      verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
82         }
83
84         return false;
85 }
86
87 static bool test_stream_encoder()
88 {
89         StreamEncoder *encoder;
90         FLAC__int32 samples[1024];
91         FLAC__int32 *samples_array[1] = { samples };
92         unsigned i;
93
94         printf("\n+++ libFLAC++ unit test: FLAC::Encoder::Stream\n\n");
95
96         printf("allocating encoder instance... ");
97         encoder = new StreamEncoder();
98         if(0 == encoder) {
99                 printf("FAILED, new returned NULL\n");
100                 return false;
101         }
102         printf("OK\n");
103
104         printf("testing is_valid()... ");
105         if(!encoder->is_valid()) {
106                 printf("FAILED, returned false\n");
107                 return false;
108         }
109         printf("OK\n");
110
111         printf("testing set_verify()... ");
112         if(!encoder->set_verify(true))
113                 return encoder->die("returned false");
114         printf("OK\n");
115
116         printf("testing set_streamable_subset()... ");
117         if(!encoder->set_streamable_subset(true))
118                 return encoder->die("returned false");
119         printf("OK\n");
120
121         printf("testing set_do_mid_side_stereo()... ");
122         if(!encoder->set_do_mid_side_stereo(false))
123                 return encoder->die("returned false");
124         printf("OK\n");
125
126         printf("testing set_loose_mid_side_stereo()... ");
127         if(!encoder->set_loose_mid_side_stereo(false))
128                 return encoder->die("returned false");
129         printf("OK\n");
130
131         printf("testing set_channels()... ");
132         if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
133                 return encoder->die("returned false");
134         printf("OK\n");
135
136         printf("testing set_bits_per_sample()... ");
137         if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
138                 return encoder->die("returned false");
139         printf("OK\n");
140
141         printf("testing set_sample_rate()... ");
142         if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
143                 return encoder->die("returned false");
144         printf("OK\n");
145
146         printf("testing set_blocksize()... ");
147         if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
148                 return encoder->die("returned false");
149         printf("OK\n");
150
151         printf("testing set_max_lpc_order()... ");
152         if(!encoder->set_max_lpc_order(0))
153                 return encoder->die("returned false");
154         printf("OK\n");
155
156         printf("testing set_qlp_coeff_precision()... ");
157         if(!encoder->set_qlp_coeff_precision(0))
158                 return encoder->die("returned false");
159         printf("OK\n");
160
161         printf("testing set_do_qlp_coeff_prec_search()... ");
162         if(!encoder->set_do_qlp_coeff_prec_search(false))
163                 return encoder->die("returned false");
164         printf("OK\n");
165
166         printf("testing set_do_escape_coding()... ");
167         if(!encoder->set_do_escape_coding(false))
168                 return encoder->die("returned false");
169         printf("OK\n");
170
171         printf("testing set_do_exhaustive_model_search()... ");
172         if(!encoder->set_do_exhaustive_model_search(false))
173                 return encoder->die("returned false");
174         printf("OK\n");
175
176         printf("testing set_min_residual_partition_order()... ");
177         if(!encoder->set_min_residual_partition_order(0))
178                 return encoder->die("returned false");
179         printf("OK\n");
180
181         printf("testing set_max_residual_partition_order()... ");
182         if(!encoder->set_max_residual_partition_order(0))
183                 return encoder->die("returned false");
184         printf("OK\n");
185
186         printf("testing set_rice_parameter_search_dist()... ");
187         if(!encoder->set_rice_parameter_search_dist(0))
188                 return encoder->die("returned false");
189         printf("OK\n");
190
191         printf("testing set_total_samples_estimate()... ");
192         if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
193                 return encoder->die("returned false");
194         printf("OK\n");
195
196         printf("testing set_metadata()... ");
197         if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
198                 return encoder->die("returned false");
199         printf("OK\n");
200
201         printf("testing init()... ");
202         if(encoder->init() != ::FLAC__STREAM_ENCODER_OK)
203                 return encoder->die();
204         printf("OK\n");
205
206         printf("testing get_state()... ");
207         FLAC::Encoder::Stream::State state = encoder->get_state();
208         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
209
210         printf("testing get_verify_decoder_state()... ");
211         FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
212         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
213
214         {
215                 FLAC__uint64 absolute_sample;
216                 unsigned frame_number;
217                 unsigned channel;
218                 unsigned sample;
219                 FLAC__int32 expected;
220                 FLAC__int32 got;
221
222                 printf("testing get_verify_decoder_error_stats()... ");
223                 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
224                 printf("OK\n");
225         }
226
227         printf("testing get_verify()... ");
228         if(encoder->get_verify() != true) {
229                 printf("FAILED, expected true, got false\n");
230                 return false;
231         }
232         printf("OK\n");
233
234         printf("testing get_streamable_subset()... ");
235         if(encoder->get_streamable_subset() != true) {
236                 printf("FAILED, expected true, got false\n");
237                 return false;
238         }
239         printf("OK\n");
240
241         printf("testing get_do_mid_side_stereo()... ");
242         if(encoder->get_do_mid_side_stereo() != false) {
243                 printf("FAILED, expected false, got true\n");
244                 return false;
245         }
246         printf("OK\n");
247
248         printf("testing get_loose_mid_side_stereo()... ");
249         if(encoder->get_loose_mid_side_stereo() != false) {
250                 printf("FAILED, expected false, got true\n");
251                 return false;
252         }
253         printf("OK\n");
254
255         printf("testing get_channels()... ");
256         if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
257                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
258                 return false;
259         }
260         printf("OK\n");
261
262         printf("testing get_bits_per_sample()... ");
263         if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
264                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
265                 return false;
266         }
267         printf("OK\n");
268
269         printf("testing get_sample_rate()... ");
270         if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
271                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
272                 return false;
273         }
274         printf("OK\n");
275
276         printf("testing get_blocksize()... ");
277         if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
278                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
279                 return false;
280         }
281         printf("OK\n");
282
283         printf("testing get_max_lpc_order()... ");
284         if(encoder->get_max_lpc_order() != 0) {
285                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
286                 return false;
287         }
288         printf("OK\n");
289
290         printf("testing get_qlp_coeff_precision()... ");
291         (void)encoder->get_qlp_coeff_precision();
292         /* we asked the encoder to auto select this so we accept anything */
293         printf("OK\n");
294
295         printf("testing get_do_qlp_coeff_prec_search()... ");
296         if(encoder->get_do_qlp_coeff_prec_search() != false) {
297                 printf("FAILED, expected false, got true\n");
298                 return false;
299         }
300         printf("OK\n");
301
302         printf("testing get_do_escape_coding()... ");
303         if(encoder->get_do_escape_coding() != false) {
304                 printf("FAILED, expected false, got true\n");
305                 return false;
306         }
307         printf("OK\n");
308
309         printf("testing get_do_exhaustive_model_search()... ");
310         if(encoder->get_do_exhaustive_model_search() != false) {
311                 printf("FAILED, expected false, got true\n");
312                 return false;
313         }
314         printf("OK\n");
315
316         printf("testing get_min_residual_partition_order()... ");
317         if(encoder->get_min_residual_partition_order() != 0) {
318                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
319                 return false;
320         }
321         printf("OK\n");
322
323         printf("testing get_max_residual_partition_order()... ");
324         if(encoder->get_max_residual_partition_order() != 0) {
325                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
326                 return false;
327         }
328         printf("OK\n");
329
330         printf("testing get_rice_parameter_search_dist()... ");
331         if(encoder->get_rice_parameter_search_dist() != 0) {
332                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
333                 return false;
334         }
335         printf("OK\n");
336
337         printf("testing get_total_samples_estimate()... ");
338         if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
339                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
340                 return false;
341         }
342         printf("OK\n");
343
344         /* init the dummy sample buffer */
345         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
346                 samples[i] = i & 7;
347
348         printf("testing process()... ");
349         if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
350                 return encoder->die("returned false");
351         printf("OK\n");
352
353         printf("testing process_interleaved()... ");
354         if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
355                 return encoder->die("returned false");
356         printf("OK\n");
357
358         printf("testing finish()... ");
359         encoder->finish();
360         printf("OK\n");
361
362         printf("freeing encoder instance... ");
363         delete encoder;
364         printf("OK\n");
365
366         printf("\nPASSED!\n");
367
368         return true;
369 }
370
371 class SeekableStreamEncoder : public FLAC::Encoder::SeekableStream {
372 public:
373         SeekableStreamEncoder(): FLAC::Encoder::SeekableStream() { }
374         ~SeekableStreamEncoder() { }
375
376         // from FLAC::Encoder::SeekableStream
377         ::FLAC__SeekableStreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
378         ::FLAC__SeekableStreamEncoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
379         ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame);
380
381         bool die(const char *msg = 0) const;
382 };
383
384 ::FLAC__SeekableStreamEncoderSeekStatus SeekableStreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset)
385 {
386         (void)absolute_byte_offset;
387
388         return ::FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
389 }
390
391 ::FLAC__SeekableStreamEncoderTellStatus SeekableStreamEncoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
392 {
393         *absolute_byte_offset = 0;
394
395         return ::FLAC__SEEKABLE_STREAM_ENCODER_TELL_STATUS_OK;
396 }
397
398 ::FLAC__StreamEncoderWriteStatus SeekableStreamEncoder::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
399 {
400         (void)buffer, (void)bytes, (void)samples, (void)current_frame;
401
402         return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
403 }
404
405 bool SeekableStreamEncoder::die(const char *msg) const
406 {
407         State state = get_state();
408
409         if(msg)
410                 printf("FAILED, %s", msg);
411         else
412                 printf("FAILED");
413
414         printf(", state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamEncoderState)state), state.as_cstring());
415         if(state == ::FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
416                 FLAC::Encoder::Stream::State state_ = get_stream_encoder_state();
417                 printf("      stream encoder state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
418                 if(state_ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
419                         FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
420                         printf("      verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
421                 }
422         }
423
424         return false;
425 }
426
427 static bool test_seekable_stream_encoder()
428 {
429         SeekableStreamEncoder *encoder;
430         FLAC__int32 samples[1024];
431         FLAC__int32 *samples_array[1] = { samples };
432         unsigned i;
433
434         printf("\n+++ libFLAC++ unit test: FLAC::Encoder::SeekableStream\n\n");
435
436         printf("allocating encoder instance... ");
437         encoder = new SeekableStreamEncoder();
438         if(0 == encoder) {
439                 printf("FAILED, new returned NULL\n");
440                 return false;
441         }
442         printf("OK\n");
443
444         printf("testing is_valid()... ");
445         if(!encoder->is_valid()) {
446                 printf("FAILED, returned false\n");
447                 return false;
448         }
449         printf("OK\n");
450
451         printf("testing set_verify()... ");
452         if(!encoder->set_verify(true))
453                 return encoder->die("returned false");
454         printf("OK\n");
455
456         printf("testing set_streamable_subset()... ");
457         if(!encoder->set_streamable_subset(true))
458                 return encoder->die("returned false");
459         printf("OK\n");
460
461         printf("testing set_do_mid_side_stereo()... ");
462         if(!encoder->set_do_mid_side_stereo(false))
463                 return encoder->die("returned false");
464         printf("OK\n");
465
466         printf("testing set_loose_mid_side_stereo()... ");
467         if(!encoder->set_loose_mid_side_stereo(false))
468                 return encoder->die("returned false");
469         printf("OK\n");
470
471         printf("testing set_channels()... ");
472         if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
473                 return encoder->die("returned false");
474         printf("OK\n");
475
476         printf("testing set_bits_per_sample()... ");
477         if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
478                 return encoder->die("returned false");
479         printf("OK\n");
480
481         printf("testing set_sample_rate()... ");
482         if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
483                 return encoder->die("returned false");
484         printf("OK\n");
485
486         printf("testing set_blocksize()... ");
487         if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
488                 return encoder->die("returned false");
489         printf("OK\n");
490
491         printf("testing set_max_lpc_order()... ");
492         if(!encoder->set_max_lpc_order(0))
493                 return encoder->die("returned false");
494         printf("OK\n");
495
496         printf("testing set_qlp_coeff_precision()... ");
497         if(!encoder->set_qlp_coeff_precision(0))
498                 return encoder->die("returned false");
499         printf("OK\n");
500
501         printf("testing set_do_qlp_coeff_prec_search()... ");
502         if(!encoder->set_do_qlp_coeff_prec_search(false))
503                 return encoder->die("returned false");
504         printf("OK\n");
505
506         printf("testing set_do_escape_coding()... ");
507         if(!encoder->set_do_escape_coding(false))
508                 return encoder->die("returned false");
509         printf("OK\n");
510
511         printf("testing set_do_exhaustive_model_search()... ");
512         if(!encoder->set_do_exhaustive_model_search(false))
513                 return encoder->die("returned false");
514         printf("OK\n");
515
516         printf("testing set_min_residual_partition_order()... ");
517         if(!encoder->set_min_residual_partition_order(0))
518                 return encoder->die("returned false");
519         printf("OK\n");
520
521         printf("testing set_max_residual_partition_order()... ");
522         if(!encoder->set_max_residual_partition_order(0))
523                 return encoder->die("returned false");
524         printf("OK\n");
525
526         printf("testing set_rice_parameter_search_dist()... ");
527         if(!encoder->set_rice_parameter_search_dist(0))
528                 return encoder->die("returned false");
529         printf("OK\n");
530
531         printf("testing set_total_samples_estimate()... ");
532         if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
533                 return encoder->die("returned false");
534         printf("OK\n");
535
536         printf("testing set_metadata()... ");
537         if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
538                 return encoder->die("returned false");
539         printf("OK\n");
540
541         printf("testing init()... ");
542         if(encoder->init() != ::FLAC__SEEKABLE_STREAM_ENCODER_OK)
543                 return encoder->die();
544         printf("OK\n");
545
546         printf("testing get_state()... ");
547         FLAC::Encoder::SeekableStream::State state = encoder->get_state();
548         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__SeekableStreamEncoderState)state), state.as_cstring());
549
550         printf("testing get_stream_encoder_state()... ");
551         FLAC::Encoder::Stream::State state_ = encoder->get_stream_encoder_state();
552         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
553
554         printf("testing get_verify_decoder_state()... ");
555         FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
556         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
557
558         {
559                 FLAC__uint64 absolute_sample;
560                 unsigned frame_number;
561                 unsigned channel;
562                 unsigned sample;
563                 FLAC__int32 expected;
564                 FLAC__int32 got;
565
566                 printf("testing get_verify_decoder_error_stats()... ");
567                 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
568                 printf("OK\n");
569         }
570
571         printf("testing get_verify()... ");
572         if(encoder->get_verify() != true) {
573                 printf("FAILED, expected true, got false\n");
574                 return false;
575         }
576         printf("OK\n");
577
578         printf("testing get_streamable_subset()... ");
579         if(encoder->get_streamable_subset() != true) {
580                 printf("FAILED, expected true, got false\n");
581                 return false;
582         }
583         printf("OK\n");
584
585         printf("testing get_do_mid_side_stereo()... ");
586         if(encoder->get_do_mid_side_stereo() != false) {
587                 printf("FAILED, expected false, got true\n");
588                 return false;
589         }
590         printf("OK\n");
591
592         printf("testing get_loose_mid_side_stereo()... ");
593         if(encoder->get_loose_mid_side_stereo() != false) {
594                 printf("FAILED, expected false, got true\n");
595                 return false;
596         }
597         printf("OK\n");
598
599         printf("testing get_channels()... ");
600         if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
601                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
602                 return false;
603         }
604         printf("OK\n");
605
606         printf("testing get_bits_per_sample()... ");
607         if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
608                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
609                 return false;
610         }
611         printf("OK\n");
612
613         printf("testing get_sample_rate()... ");
614         if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
615                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
616                 return false;
617         }
618         printf("OK\n");
619
620         printf("testing get_blocksize()... ");
621         if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
622                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
623                 return false;
624         }
625         printf("OK\n");
626
627         printf("testing get_max_lpc_order()... ");
628         if(encoder->get_max_lpc_order() != 0) {
629                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
630                 return false;
631         }
632         printf("OK\n");
633
634         printf("testing get_qlp_coeff_precision()... ");
635         (void)encoder->get_qlp_coeff_precision();
636         /* we asked the encoder to auto select this so we accept anything */
637         printf("OK\n");
638
639         printf("testing get_do_qlp_coeff_prec_search()... ");
640         if(encoder->get_do_qlp_coeff_prec_search() != false) {
641                 printf("FAILED, expected false, got true\n");
642                 return false;
643         }
644         printf("OK\n");
645
646         printf("testing get_do_escape_coding()... ");
647         if(encoder->get_do_escape_coding() != false) {
648                 printf("FAILED, expected false, got true\n");
649                 return false;
650         }
651         printf("OK\n");
652
653         printf("testing get_do_exhaustive_model_search()... ");
654         if(encoder->get_do_exhaustive_model_search() != false) {
655                 printf("FAILED, expected false, got true\n");
656                 return false;
657         }
658         printf("OK\n");
659
660         printf("testing get_min_residual_partition_order()... ");
661         if(encoder->get_min_residual_partition_order() != 0) {
662                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
663                 return false;
664         }
665         printf("OK\n");
666
667         printf("testing get_max_residual_partition_order()... ");
668         if(encoder->get_max_residual_partition_order() != 0) {
669                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
670                 return false;
671         }
672         printf("OK\n");
673
674         printf("testing get_rice_parameter_search_dist()... ");
675         if(encoder->get_rice_parameter_search_dist() != 0) {
676                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
677                 return false;
678         }
679         printf("OK\n");
680
681         printf("testing get_total_samples_estimate()... ");
682         if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
683                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
684                 return false;
685         }
686         printf("OK\n");
687
688         /* init the dummy sample buffer */
689         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
690                 samples[i] = i & 7;
691
692         printf("testing process()... ");
693         if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
694                 return encoder->die("returned false");
695         printf("OK\n");
696
697         printf("testing process_interleaved()... ");
698         if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
699                 return encoder->die("returned false");
700         printf("OK\n");
701
702         printf("testing finish()... ");
703         encoder->finish();
704         printf("OK\n");
705
706         printf("freeing encoder instance... ");
707         delete encoder;
708         printf("OK\n");
709
710         printf("\nPASSED!\n");
711
712         return true;
713 }
714
715 class FileEncoder : public FLAC::Encoder::File {
716 public:
717         FileEncoder(): FLAC::Encoder::File() { }
718         ~FileEncoder() { }
719
720         // from FLAC::Encoder::File
721         void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
722
723         bool die(const char *msg = 0) const;
724 };
725
726 void FileEncoder::progress_callback(FLAC__uint64, FLAC__uint64, unsigned, unsigned)
727 {
728 }
729
730 bool FileEncoder::die(const char *msg) const
731 {
732         State state = get_state();
733
734         if(msg)
735                 printf("FAILED, %s", msg);
736         else
737                 printf("FAILED");
738
739         printf(", state = %u (%s)\n", (unsigned)((::FLAC__FileEncoderState)state), state.as_cstring());
740         if(state == ::FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR) {
741                 FLAC::Encoder::SeekableStream::State state_ = get_seekable_stream_encoder_state();
742                 printf("      seekable stream encoder state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamEncoderState)state_), state_.as_cstring());
743                 if(state_ == ::FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
744                         FLAC::Encoder::Stream::State state__ = get_stream_encoder_state();
745                         printf("      stream encoder state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state__), state__.as_cstring());
746                         if(state__ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
747                                 FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
748                                 printf("      verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
749                         }
750                 }
751         }
752
753         return false;
754 }
755
756 static bool test_file_encoder()
757 {
758         FileEncoder *encoder;
759         FLAC__int32 samples[1024];
760         FLAC__int32 *samples_array[1] = { samples };
761         unsigned i;
762
763         printf("\n+++ libFLAC++ unit test: FLAC::Encoder::File\n\n");
764
765         printf("allocating encoder instance... ");
766         encoder = new FileEncoder();
767         if(0 == encoder) {
768                 printf("FAILED, new returned NULL\n");
769                 return false;
770         }
771         printf("OK\n");
772
773         printf("testing is_valid()... ");
774         if(!encoder->is_valid()) {
775                 printf("FAILED, returned false\n");
776                 return false;
777         }
778         printf("OK\n");
779
780         printf("testing set_verify()... ");
781         if(!encoder->set_verify(true))
782                 return encoder->die("returned false");
783         printf("OK\n");
784
785         printf("testing set_streamable_subset()... ");
786         if(!encoder->set_streamable_subset(true))
787                 return encoder->die("returned false");
788         printf("OK\n");
789
790         printf("testing set_do_mid_side_stereo()... ");
791         if(!encoder->set_do_mid_side_stereo(false))
792                 return encoder->die("returned false");
793         printf("OK\n");
794
795         printf("testing set_loose_mid_side_stereo()... ");
796         if(!encoder->set_loose_mid_side_stereo(false))
797                 return encoder->die("returned false");
798         printf("OK\n");
799
800         printf("testing set_channels()... ");
801         if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
802                 return encoder->die("returned false");
803         printf("OK\n");
804
805         printf("testing set_bits_per_sample()... ");
806         if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
807                 return encoder->die("returned false");
808         printf("OK\n");
809
810         printf("testing set_sample_rate()... ");
811         if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
812                 return encoder->die("returned false");
813         printf("OK\n");
814
815         printf("testing set_blocksize()... ");
816         if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
817                 return encoder->die("returned false");
818         printf("OK\n");
819
820         printf("testing set_max_lpc_order()... ");
821         if(!encoder->set_max_lpc_order(0))
822                 return encoder->die("returned false");
823         printf("OK\n");
824
825         printf("testing set_qlp_coeff_precision()... ");
826         if(!encoder->set_qlp_coeff_precision(0))
827                 return encoder->die("returned false");
828         printf("OK\n");
829
830         printf("testing set_do_qlp_coeff_prec_search()... ");
831         if(!encoder->set_do_qlp_coeff_prec_search(false))
832                 return encoder->die("returned false");
833         printf("OK\n");
834
835         printf("testing set_do_escape_coding()... ");
836         if(!encoder->set_do_escape_coding(false))
837                 return encoder->die("returned false");
838         printf("OK\n");
839
840         printf("testing set_do_exhaustive_model_search()... ");
841         if(!encoder->set_do_exhaustive_model_search(false))
842                 return encoder->die("returned false");
843         printf("OK\n");
844
845         printf("testing set_min_residual_partition_order()... ");
846         if(!encoder->set_min_residual_partition_order(0))
847                 return encoder->die("returned false");
848         printf("OK\n");
849
850         printf("testing set_max_residual_partition_order()... ");
851         if(!encoder->set_max_residual_partition_order(0))
852                 return encoder->die("returned false");
853         printf("OK\n");
854
855         printf("testing set_rice_parameter_search_dist()... ");
856         if(!encoder->set_rice_parameter_search_dist(0))
857                 return encoder->die("returned false");
858         printf("OK\n");
859
860         printf("testing set_total_samples_estimate()... ");
861         if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
862                 return encoder->die("returned false");
863         printf("OK\n");
864
865         printf("testing set_metadata()... ");
866         if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
867                 return encoder->die("returned false");
868         printf("OK\n");
869
870         printf("testing set_filename()... ");
871         if(!encoder->set_filename(flacfilename_))
872                 return encoder->die("returned false");
873         printf("OK\n");
874
875         printf("testing init()... ");
876         if(encoder->init() != ::FLAC__FILE_ENCODER_OK)
877                 return encoder->die();
878         printf("OK\n");
879
880         printf("testing get_state()... ");
881         FLAC::Encoder::File::State state = encoder->get_state();
882         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__FileEncoderState)state), state.as_cstring());
883
884         printf("testing get_seekable_stream_encoder_state()... ");
885         FLAC::Encoder::SeekableStream::State state_ = encoder->get_seekable_stream_encoder_state();
886         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__SeekableStreamEncoderState)state_), state_.as_cstring());
887
888         printf("testing get_stream_encoder_state()... ");
889         FLAC::Encoder::Stream::State state__ = encoder->get_stream_encoder_state();
890         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state__), state__.as_cstring());
891
892         printf("testing get_verify_decoder_state()... ");
893         FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
894         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
895
896         {
897                 FLAC__uint64 absolute_sample;
898                 unsigned frame_number;
899                 unsigned channel;
900                 unsigned sample;
901                 FLAC__int32 expected;
902                 FLAC__int32 got;
903
904                 printf("testing get_verify_decoder_error_stats()... ");
905                 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
906                 printf("OK\n");
907         }
908
909         printf("testing get_verify()... ");
910         if(encoder->get_verify() != true) {
911                 printf("FAILED, expected true, got false\n");
912                 return false;
913         }
914         printf("OK\n");
915
916         printf("testing get_streamable_subset()... ");
917         if(encoder->get_streamable_subset() != true) {
918                 printf("FAILED, expected true, got false\n");
919                 return false;
920         }
921         printf("OK\n");
922
923         printf("testing get_do_mid_side_stereo()... ");
924         if(encoder->get_do_mid_side_stereo() != false) {
925                 printf("FAILED, expected false, got true\n");
926                 return false;
927         }
928         printf("OK\n");
929
930         printf("testing get_loose_mid_side_stereo()... ");
931         if(encoder->get_loose_mid_side_stereo() != false) {
932                 printf("FAILED, expected false, got true\n");
933                 return false;
934         }
935         printf("OK\n");
936
937         printf("testing get_channels()... ");
938         if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
939                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
940                 return false;
941         }
942         printf("OK\n");
943
944         printf("testing get_bits_per_sample()... ");
945         if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
946                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
947                 return false;
948         }
949         printf("OK\n");
950
951         printf("testing get_sample_rate()... ");
952         if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
953                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
954                 return false;
955         }
956         printf("OK\n");
957
958         printf("testing get_blocksize()... ");
959         if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
960                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
961                 return false;
962         }
963         printf("OK\n");
964
965         printf("testing get_max_lpc_order()... ");
966         if(encoder->get_max_lpc_order() != 0) {
967                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
968                 return false;
969         }
970         printf("OK\n");
971
972         printf("testing get_qlp_coeff_precision()... ");
973         (void)encoder->get_qlp_coeff_precision();
974         /* we asked the encoder to auto select this so we accept anything */
975         printf("OK\n");
976
977         printf("testing get_do_qlp_coeff_prec_search()... ");
978         if(encoder->get_do_qlp_coeff_prec_search() != false) {
979                 printf("FAILED, expected false, got true\n");
980                 return false;
981         }
982         printf("OK\n");
983
984         printf("testing get_do_escape_coding()... ");
985         if(encoder->get_do_escape_coding() != false) {
986                 printf("FAILED, expected false, got true\n");
987                 return false;
988         }
989         printf("OK\n");
990
991         printf("testing get_do_exhaustive_model_search()... ");
992         if(encoder->get_do_exhaustive_model_search() != false) {
993                 printf("FAILED, expected false, got true\n");
994                 return false;
995         }
996         printf("OK\n");
997
998         printf("testing get_min_residual_partition_order()... ");
999         if(encoder->get_min_residual_partition_order() != 0) {
1000                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
1001                 return false;
1002         }
1003         printf("OK\n");
1004
1005         printf("testing get_max_residual_partition_order()... ");
1006         if(encoder->get_max_residual_partition_order() != 0) {
1007                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
1008                 return false;
1009         }
1010         printf("OK\n");
1011
1012         printf("testing get_rice_parameter_search_dist()... ");
1013         if(encoder->get_rice_parameter_search_dist() != 0) {
1014                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
1015                 return false;
1016         }
1017         printf("OK\n");
1018
1019         printf("testing get_total_samples_estimate()... ");
1020         if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
1021                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
1022                 return false;
1023         }
1024         printf("OK\n");
1025
1026         /* init the dummy sample buffer */
1027         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
1028                 samples[i] = i & 7;
1029
1030         printf("testing process()... ");
1031         if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
1032                 return encoder->die("returned false");
1033         printf("OK\n");
1034
1035         printf("testing process_interleaved()... ");
1036         if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
1037                 return encoder->die("returned false");
1038         printf("OK\n");
1039
1040         printf("testing finish()... ");
1041         encoder->finish();
1042         printf("OK\n");
1043
1044         printf("freeing encoder instance... ");
1045         delete encoder;
1046         printf("OK\n");
1047
1048         printf("\nPASSED!\n");
1049
1050         return true;
1051 }
1052
1053 bool test_encoders()
1054 {
1055         init_metadata_blocks_();
1056
1057         if(!test_stream_encoder())
1058                 return false;
1059
1060         if(!test_seekable_stream_encoder())
1061                 return false;
1062
1063         if(!test_file_encoder())
1064                 return false;
1065
1066         free_metadata_blocks_();
1067
1068         return true;
1069 }