add 2206 to copyright notice
[platform/upstream/flac.git] / src / test_libFLAC++ / decoders.cpp
index 8e86ae1..d682cfd 100644 (file)
@@ -1,5 +1,5 @@
 /* test_libFLAC++ - Unit tester for libFLAC++
- * Copyright (C) 2002  Josh Coalson
+ * Copyright (C) 2002,2003,2004,2005,2006  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include "decoders.h"
 extern "C" {
 #include "file_utils.h"
+#include "metadata_utils.h"
 }
 #include "FLAC/assert.h"
 #include "FLAC/metadata.h" // for ::FLAC__metadata_object_is_equal()
 #include "FLAC++/decoder.h"
-#include "share/file_utils.h"
+#include "share/grabbag.h"
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_;
-static ::FLAC__StreamMetadata *expected_metadata_sequence_[6];
+#ifdef _MSC_VER
+// warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
+#pragma warning ( disable : 4800 )
+#endif
+
+static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, unknown_;
+static ::FLAC__StreamMetadata *expected_metadata_sequence_[8];
 static unsigned num_expected_;
 static const char *flacfilename_ = "metadata.flac";
 static unsigned flacfilesize_;
@@ -41,106 +47,28 @@ static bool die_(const char *msg)
        return false;
 }
 
-static void *malloc_or_die_(size_t size)
-{
-       void *x = malloc(size);
-       if(0 == x) {
-               fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
-               exit(1);
-       }
-       return x;
-}
-
 static void init_metadata_blocks_()
 {
-       /*
-               most of the actual numbers and data in the blocks don't matter,
-               we just want to make sure the decoder parses them correctly
-
-               remember, the metadata interface gets tested after the decoders,
-               so we do all the metadata manipulation here without it.
-       */
-
-       /* min/max_framesize and md5sum don't get written at first, so we have to leave them 0 */
-       streaminfo_.is_last = false;
-       streaminfo_.type = ::FLAC__METADATA_TYPE_STREAMINFO;
-       streaminfo_.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
-       streaminfo_.data.stream_info.min_blocksize = 576;
-       streaminfo_.data.stream_info.max_blocksize = 576;
-       streaminfo_.data.stream_info.min_framesize = 0;
-       streaminfo_.data.stream_info.max_framesize = 0;
-       streaminfo_.data.stream_info.sample_rate = 44100;
-       streaminfo_.data.stream_info.channels = 1;
-       streaminfo_.data.stream_info.bits_per_sample = 8;
-       streaminfo_.data.stream_info.total_samples = 0;
-       memset(streaminfo_.data.stream_info.md5sum, 0, 16);
-
-       padding_.is_last = false;
-       padding_.type = ::FLAC__METADATA_TYPE_PADDING;
-       padding_.length = 1234;
-
-       seektable_.is_last = false;
-       seektable_.type = ::FLAC__METADATA_TYPE_SEEKTABLE;
-       seektable_.data.seek_table.num_points = 2;
-       seektable_.length = seektable_.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
-       seektable_.data.seek_table.points = (::FLAC__StreamMetadata_SeekPoint*)malloc_or_die_(seektable_.data.seek_table.num_points * sizeof(::FLAC__StreamMetadata_SeekPoint));
-       seektable_.data.seek_table.points[0].sample_number = 0;
-       seektable_.data.seek_table.points[0].stream_offset = 0;
-       seektable_.data.seek_table.points[0].frame_samples = streaminfo_.data.stream_info.min_blocksize;
-       seektable_.data.seek_table.points[1].sample_number = ::FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
-       seektable_.data.seek_table.points[1].stream_offset = 1000;
-       seektable_.data.seek_table.points[1].frame_samples = streaminfo_.data.stream_info.min_blocksize;
-
-       application1_.is_last = false;
-       application1_.type = ::FLAC__METADATA_TYPE_APPLICATION;
-       application1_.length = 8;
-       memcpy(application1_.data.application.id, "\xfe\xdc\xba\x98", 4);
-       application1_.data.application.data = (FLAC__byte*)malloc_or_die_(4);
-       memcpy(application1_.data.application.data, "\xf0\xe1\xd2\xc3", 4);
-
-       application2_.is_last = false;
-       application2_.type = ::FLAC__METADATA_TYPE_APPLICATION;
-       application2_.length = 4;
-       memcpy(application2_.data.application.id, "\x76\x54\x32\x10", 4);
-       application2_.data.application.data = 0;
-
-       {
-               const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);
-               vorbiscomment_.is_last = true;
-               vorbiscomment_.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
-               vorbiscomment_.length = (4 + vendor_string_length) + 4 + (4 + 5) + (4 + 0);
-               vorbiscomment_.data.vorbis_comment.vendor_string.length = vendor_string_length;
-               vorbiscomment_.data.vorbis_comment.vendor_string.entry = (FLAC__byte*)malloc_or_die_(vendor_string_length);
-               memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, FLAC__VENDOR_STRING, vendor_string_length);
-               vorbiscomment_.data.vorbis_comment.num_comments = 2;
-               vorbiscomment_.data.vorbis_comment.comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc_or_die_(vorbiscomment_.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
-               vorbiscomment_.data.vorbis_comment.comments[0].length = 5;
-               vorbiscomment_.data.vorbis_comment.comments[0].entry = (FLAC__byte*)malloc_or_die_(5);
-               memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "ab=cd", 5);
-               vorbiscomment_.data.vorbis_comment.comments[1].length = 0;
-               vorbiscomment_.data.vorbis_comment.comments[1].entry = 0;
-       }
+       mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
 }
 
 static void free_metadata_blocks_()
 {
-       free(seektable_.data.seek_table.points);
-       free(application1_.data.application.data);
-       free(vorbiscomment_.data.vorbis_comment.vendor_string.entry);
-       free(vorbiscomment_.data.vorbis_comment.comments[0].entry);
-       free(vorbiscomment_.data.vorbis_comment.comments);
+       mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
 }
 
 static bool generate_file_()
 {
        printf("\n\ngenerating FLAC file for decoder tests...\n");
 
-       expected_metadata_sequence_[0] = &padding_;
-       expected_metadata_sequence_[1] = &seektable_;
-       expected_metadata_sequence_[2] = &application1_;
-       expected_metadata_sequence_[3] = &application2_;
-       expected_metadata_sequence_[4] = &vorbiscomment_;
-       num_expected_ = 5;
+       num_expected_ = 0;
+       expected_metadata_sequence_[num_expected_++] = &padding_;
+       expected_metadata_sequence_[num_expected_++] = &seektable_;
+       expected_metadata_sequence_[num_expected_++] = &application1_;
+       expected_metadata_sequence_[num_expected_++] = &application2_;
+       expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!file_utils__generate_flacfile(flacfilename_, &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_))
                return die_("creating the encoded file");
@@ -420,6 +348,11 @@ static bool test_stream_decoder()
                return decoder->die("returned false");
        printf("OK\n");
 
+       printf("testing skip_single_frame()... ");
+       if(!decoder->skip_single_frame())
+               return decoder->die("returned false");
+       printf("OK\n");
+
        printf("testing flush()... ");
        if(!decoder->flush())
                return decoder->die("returned false");
@@ -521,6 +454,8 @@ static bool test_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -565,6 +500,8 @@ static bool test_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -592,6 +529,8 @@ static bool test_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &padding_;
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -620,6 +559,8 @@ static bool test_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -654,6 +595,8 @@ static bool test_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &padding_;
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -794,6 +737,8 @@ static bool test_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -837,6 +782,8 @@ static bool test_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        ::fclose(decoder->file_);
 
@@ -1123,6 +1070,11 @@ static bool test_seekable_stream_decoder()
                return decoder->die("returned false");
        printf("OK\n");
 
+       printf("testing skip_single_frame()... ");
+       if(!decoder->skip_single_frame())
+               return decoder->die("returned false");
+       printf("OK\n");
+
        printf("testing flush()... ");
        if(!decoder->flush())
                return decoder->die("returned false");
@@ -1229,6 +1181,8 @@ static bool test_seekable_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1273,6 +1227,8 @@ static bool test_seekable_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1300,6 +1256,8 @@ static bool test_seekable_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &padding_;
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1328,6 +1286,8 @@ static bool test_seekable_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1362,6 +1322,8 @@ static bool test_seekable_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &padding_;
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1502,6 +1464,8 @@ static bool test_seekable_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1545,6 +1509,8 @@ static bool test_seekable_stream_decoder()
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        ::fclose(decoder->file_);
 
@@ -1691,7 +1657,7 @@ static bool test_file_decoder()
        printf("OK\n");
 
        printf("testing init()... ");
-       if(decoder->init() != ::FLAC__SEEKABLE_STREAM_DECODER_OK)
+       if(decoder->init() != ::FLAC__FILE_DECODER_OK)
                return decoder->die();
        printf("OK\n");
 
@@ -1772,6 +1738,11 @@ static bool test_file_decoder()
                return decoder->die("returned false");
        printf("OK\n");
 
+       printf("testing skip_single_frame()... ");
+       if(!decoder->skip_single_frame())
+               return decoder->die("returned false");
+       printf("OK\n");
+
        printf("testing seek_absolute()... ");
        if(!decoder->seek_absolute(0))
                return decoder->die("returned false");
@@ -1847,6 +1818,8 @@ static bool test_file_decoder()
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1891,6 +1864,8 @@ static bool test_file_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1918,6 +1893,8 @@ static bool test_file_decoder()
        expected_metadata_sequence_[num_expected_++] = &padding_;
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1946,6 +1923,8 @@ static bool test_file_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -1980,6 +1959,8 @@ static bool test_file_decoder()
        expected_metadata_sequence_[num_expected_++] = &padding_;
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -2120,6 +2101,8 @@ static bool test_file_decoder()
        expected_metadata_sequence_[num_expected_++] = &seektable_;
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        if(!decoder->test_respond())
                return false;
@@ -2163,6 +2146,8 @@ static bool test_file_decoder()
        expected_metadata_sequence_[num_expected_++] = &application1_;
        expected_metadata_sequence_[num_expected_++] = &application2_;
        expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
+       expected_metadata_sequence_[num_expected_++] = &cuesheet_;
+       expected_metadata_sequence_[num_expected_++] = &unknown_;
 
        printf("freeing decoder instance... ");
        delete decoder;
@@ -2176,6 +2161,7 @@ static bool test_file_decoder()
 bool test_decoders()
 {
        init_metadata_blocks_();
+
        if(!generate_file_())
                return false;
 
@@ -2188,7 +2174,8 @@ bool test_decoders()
        if(!test_file_decoder())
                return false;
 
-       (void) FLAC__file_utils_remove_file(flacfilename_);
+       (void) grabbag__file_remove_file(flacfilename_);
+
        free_metadata_blocks_();
 
        return true;