diff options
Diffstat (limited to '')
36 files changed, 16134 insertions, 0 deletions
diff --git a/src/test_libFLAC++/CMakeLists.txt b/src/test_libFLAC++/CMakeLists.txt new file mode 100644 index 0000000..2fa7b1e --- /dev/null +++ b/src/test_libFLAC++/CMakeLists.txt @@ -0,0 +1,10 @@ +add_executable(test_libFLAC++ + decoders.cpp + encoders.cpp + main.cpp + metadata.cpp + metadata_manip.cpp + metadata_object.cpp + $<$<BOOL:${WIN32}>:../../include/share/win_utf8_io.h> + $<$<BOOL:${WIN32}>:../share/win_utf8_io/win_utf8_io.c>) +target_link_libraries(test_libFLAC++ FLAC++ test_libs_common grabbag) diff --git a/src/test_libFLAC++/Makefile.am b/src/test_libFLAC++/Makefile.am new file mode 100644 index 0000000..8bf746f --- /dev/null +++ b/src/test_libFLAC++/Makefile.am @@ -0,0 +1,50 @@ +# test_libFLAC++ - Unit tester for libFLAC++ +# Copyright (C) 2002-2009 Josh Coalson +# Copyright (C) 2011-2023 Xiph.Org Foundation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +EXTRA_DIST = \ + CMakeLists.txt + +AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include +check_PROGRAMS = test_libFLAC++ + +if OS_IS_WINDOWS +win_utf8_lib = $(top_builddir)/src/share/win_utf8_io/libwin_utf8_io.la +endif + +test_libFLAC___LDADD = \ + $(top_builddir)/src/share/grabbag/libgrabbag.la \ + $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ + $(top_builddir)/src/test_libs_common/libtest_libs_common.la \ + $(top_builddir)/src/libFLAC++/libFLAC++.la \ + $(top_builddir)/src/libFLAC/libFLAC.la \ + $(win_utf8_lib) \ + @OGG_LIBS@ \ + -lm + +test_libFLAC___SOURCES = \ + decoders.cpp \ + encoders.cpp \ + main.cpp \ + metadata.cpp \ + metadata_manip.cpp \ + metadata_object.cpp \ + decoders.h \ + encoders.h \ + metadata.h + +CLEANFILES = test_libFLAC++.exe diff --git a/src/test_libFLAC++/Makefile.in b/src/test_libFLAC++/Makefile.in new file mode 100644 index 0000000..825dad6 --- /dev/null +++ b/src/test_libFLAC++/Makefile.in @@ -0,0 +1,720 @@ +# Makefile.in generated by automake 1.16.5 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2021 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +# test_libFLAC++ - Unit tester for libFLAC++ +# Copyright (C) 2002-2009 Josh Coalson +# Copyright (C) 2011-2023 Xiph.Org Foundation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +VPATH = @srcdir@ +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +check_PROGRAMS = test_libFLAC++$(EXEEXT) +subdir = src/test_libFLAC++ +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/add_cflags.m4 \ + $(top_srcdir)/m4/add_cxxflags.m4 \ + $(top_srcdir)/m4/ax_add_fortify_source.m4 \ + $(top_srcdir)/m4/ax_check_compile_flag.m4 \ + $(top_srcdir)/m4/ax_check_enable_debug.m4 \ + $(top_srcdir)/m4/bswap.m4 $(top_srcdir)/m4/clang.m4 \ + $(top_srcdir)/m4/codeset.m4 $(top_srcdir)/m4/gcc_version.m4 \ + $(top_srcdir)/m4/iconv.m4 $(top_srcdir)/m4/lib-ld.m4 \ + $(top_srcdir)/m4/lib-link.m4 $(top_srcdir)/m4/lib-prefix.m4 \ + $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltoptions.m4 \ + $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ + $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/m4/ogg.m4 \ + $(top_srcdir)/m4/really_gcc.m4 \ + $(top_srcdir)/m4/stack_protect.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am_test_libFLAC___OBJECTS = decoders.$(OBJEXT) encoders.$(OBJEXT) \ + main.$(OBJEXT) metadata.$(OBJEXT) metadata_manip.$(OBJEXT) \ + metadata_object.$(OBJEXT) +test_libFLAC___OBJECTS = $(am_test_libFLAC___OBJECTS) +test_libFLAC___DEPENDENCIES = \ + $(top_builddir)/src/share/grabbag/libgrabbag.la \ + $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ + $(top_builddir)/src/test_libs_common/libtest_libs_common.la \ + $(top_builddir)/src/libFLAC++/libFLAC++.la \ + $(top_builddir)/src/libFLAC/libFLAC.la $(win_utf8_lib) +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__maybe_remake_depfiles = depfiles +am__depfiles_remade = ./$(DEPDIR)/decoders.Po ./$(DEPDIR)/encoders.Po \ + ./$(DEPDIR)/main.Po ./$(DEPDIR)/metadata.Po \ + ./$(DEPDIR)/metadata_manip.Po ./$(DEPDIR)/metadata_object.Po +am__mv = mv -f +CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) +LTCXXCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CXXFLAGS) $(CXXFLAGS) +AM_V_CXX = $(am__v_CXX_@AM_V@) +am__v_CXX_ = $(am__v_CXX_@AM_DEFAULT_V@) +am__v_CXX_0 = @echo " CXX " $@; +am__v_CXX_1 = +CXXLD = $(CXX) +CXXLINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \ + $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CXXLD = $(am__v_CXXLD_@AM_V@) +am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@) +am__v_CXXLD_0 = @echo " CXXLD " $@; +am__v_CXXLD_1 = +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = $(test_libFLAC___SOURCES) +DIST_SOURCES = $(test_libFLAC___SOURCES) +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +AS = @AS@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCAS = @CCAS@ +CCASDEPMODE = @CCASDEPMODE@ +CCASFLAGS = @CCASFLAGS@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CSCOPE = @CSCOPE@ +CTAGS = @CTAGS@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DOXYGEN = @DOXYGEN@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +ENABLE_64_BIT_WORDS = @ENABLE_64_BIT_WORDS@ +ETAGS = @ETAGS@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +FLAC__HAS_OGG = @FLAC__HAS_OGG@ +FLAC__TEST_LEVEL = @FLAC__TEST_LEVEL@ +FLAC__TEST_WITH_VALGRIND = @FLAC__TEST_WITH_VALGRIND@ +GCC_MAJOR_VERSION = @GCC_MAJOR_VERSION@ +GCC_MINOR_VERSION = @GCC_MINOR_VERSION@ +GCC_VERSION = @GCC_VERSION@ +GIT_COMMIT_VERSION_HASH = @GIT_COMMIT_VERSION_HASH@ +GIT_FOUND = @GIT_FOUND@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBICONV = @LIBICONV@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIB_CLOCK_GETTIME = @LIB_CLOCK_GETTIME@ +LIB_FUZZING_ENGINE = @LIB_FUZZING_ENGINE@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBICONV = @LTLIBICONV@ +LTLIBOBJS = @LTLIBOBJS@ +LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MKDIR_P = @MKDIR_P@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OBJ_FORMAT = @OBJ_FORMAT@ +OGG_CFLAGS = @OGG_CFLAGS@ +OGG_LIBS = @OGG_LIBS@ +OGG_PACKAGE = @OGG_PACKAGE@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PANDOC = @PANDOC@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +RANLIB = @RANLIB@ +RC = @RC@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_CXX = @ac_ct_CXX@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +runstatedir = @runstatedir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +EXTRA_DIST = \ + CMakeLists.txt + +AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include +@OS_IS_WINDOWS_TRUE@win_utf8_lib = $(top_builddir)/src/share/win_utf8_io/libwin_utf8_io.la +test_libFLAC___LDADD = \ + $(top_builddir)/src/share/grabbag/libgrabbag.la \ + $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ + $(top_builddir)/src/test_libs_common/libtest_libs_common.la \ + $(top_builddir)/src/libFLAC++/libFLAC++.la \ + $(top_builddir)/src/libFLAC/libFLAC.la \ + $(win_utf8_lib) \ + @OGG_LIBS@ \ + -lm + +test_libFLAC___SOURCES = \ + decoders.cpp \ + encoders.cpp \ + main.cpp \ + metadata.cpp \ + metadata_manip.cpp \ + metadata_object.cpp \ + decoders.h \ + encoders.h \ + metadata.h + +CLEANFILES = test_libFLAC++.exe +all: all-am + +.SUFFIXES: +.SUFFIXES: .cpp .lo .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/test_libFLAC++/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign src/test_libFLAC++/Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-checkPROGRAMS: + @list='$(check_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list + +test_libFLAC++$(EXEEXT): $(test_libFLAC___OBJECTS) $(test_libFLAC___DEPENDENCIES) $(EXTRA_test_libFLAC___DEPENDENCIES) + @rm -f test_libFLAC++$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(test_libFLAC___OBJECTS) $(test_libFLAC___LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/decoders.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/encoders.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/metadata.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/metadata_manip.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/metadata_object.Po@am__quote@ # am--include-marker + +$(am__depfiles_remade): + @$(MKDIR_P) $(@D) + @echo '# dummy' >$@-t && $(am__mv) $@-t $@ + +am--depfiles: $(am__depfiles_remade) + +.cpp.o: +@am__fastdepCXX_TRUE@ $(AM_V_CXX)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ +@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCXX_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXXCOMPILE) -c -o $@ $< + +.cpp.obj: +@am__fastdepCXX_TRUE@ $(AM_V_CXX)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.obj$$||'`;\ +@am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ `$(CYGPATH_W) '$<'` &&\ +@am__fastdepCXX_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.cpp.lo: +@am__fastdepCXX_TRUE@ $(AM_V_CXX)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.lo$$||'`;\ +@am__fastdepCXX_TRUE@ $(LTCXXCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCXX_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Plo +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LTCXXCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags +distdir: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) distdir-am + +distdir-am: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) $(check_PROGRAMS) +check: check-am +all-am: Makefile +installdirs: +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-checkPROGRAMS clean-generic clean-libtool \ + mostlyclean-am + +distclean: distclean-am + -rm -f ./$(DEPDIR)/decoders.Po + -rm -f ./$(DEPDIR)/encoders.Po + -rm -f ./$(DEPDIR)/main.Po + -rm -f ./$(DEPDIR)/metadata.Po + -rm -f ./$(DEPDIR)/metadata_manip.Po + -rm -f ./$(DEPDIR)/metadata_object.Po + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f ./$(DEPDIR)/decoders.Po + -rm -f ./$(DEPDIR)/encoders.Po + -rm -f ./$(DEPDIR)/main.Po + -rm -f ./$(DEPDIR)/metadata.Po + -rm -f ./$(DEPDIR)/metadata_manip.Po + -rm -f ./$(DEPDIR)/metadata_object.Po + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: check-am install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am am--depfiles check check-am clean \ + clean-checkPROGRAMS clean-generic clean-libtool cscopelist-am \ + ctags ctags-am distclean distclean-compile distclean-generic \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-html install-html-am install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags tags-am uninstall uninstall-am + +.PRECIOUS: Makefile + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/src/test_libFLAC++/decoders.cpp b/src/test_libFLAC++/decoders.cpp new file mode 100644 index 0000000..9f375f3 --- /dev/null +++ b/src/test_libFLAC++/decoders.cpp @@ -0,0 +1,1179 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "decoders.h" +#include "FLAC/assert.h" +#include "FLAC/metadata.h" // for ::FLAC__metadata_object_is_equal() +#include "FLAC++/decoder.h" +#include "share/grabbag.h" +#include "share/compat.h" +extern "C" { +#include "test_libs_common/file_utils_flac.h" +#include "test_libs_common/metadata_utils.h" +} + +#ifdef _MSC_VER +// warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning) +#pragma warning ( disable : 4800 ) +#endif + +typedef enum { + LAYER_STREAM = 0, /* FLAC__stream_decoder_init_stream() without seeking */ + LAYER_SEEKABLE_STREAM, /* FLAC__stream_decoder_init_stream() with seeking */ + LAYER_FILE, /* FLAC__stream_decoder_init_FILE() */ + LAYER_FILENAME /* FLAC__stream_decoder_init_file() */ +} Layer; + +static const char * const LayerString[] = { + "Stream", + "Seekable Stream", + "FILE*", + "Filename" +}; + +static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_; +static ::FLAC__StreamMetadata *expected_metadata_sequence_[9]; +static uint32_t num_expected_; +static FLAC__off_t flacfilesize_; + +static const char *flacfilename(bool is_ogg) +{ + return is_ogg? "metadata.oga" : "metadata.flac"; +} + +static bool die_(const char *msg) +{ + printf("ERROR: %s\n", msg); + return false; +} + +static FLAC__bool die_s_(const char *msg, const FLAC::Decoder::Stream *decoder) +{ + FLAC::Decoder::Stream::State state = decoder->get_state(); + + if(msg) + printf("FAILED, %s", msg); + else + printf("FAILED"); + + printf(", state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring()); + + return false; +} + +static void init_metadata_blocks_() +{ + mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_); +} + +static void free_metadata_blocks_() +{ + mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_); +} + +static bool generate_file_(FLAC__bool is_ogg) +{ + printf("\n\ngenerating %sFLAC file for decoder tests...\n", is_ogg? "Ogg ":""); + + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + /* WATCHOUT: for Ogg FLAC the encoder should move the VORBIS_COMMENT block to the front, right after STREAMINFO */ + + if(!file_utils__generate_flacfile(is_ogg, flacfilename(is_ogg), &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_)) + return die_("creating the encoded file"); + + return true; +} + + +class DecoderCommon { +public: + Layer layer_; + uint32_t current_metadata_number_; + bool ignore_errors_; + bool error_occurred_; + + DecoderCommon(Layer layer): layer_(layer), current_metadata_number_(0), ignore_errors_(false), error_occurred_(false) { } + virtual ~DecoderCommon(void) { } + ::FLAC__StreamDecoderWriteStatus common_write_callback_(const ::FLAC__Frame *frame); + void common_metadata_callback_(const ::FLAC__StreamMetadata *metadata); + void common_error_callback_(::FLAC__StreamDecoderErrorStatus status); +}; + +::FLAC__StreamDecoderWriteStatus DecoderCommon::common_write_callback_(const ::FLAC__Frame *frame) +{ + if(error_occurred_) + return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + + if( + (frame->header.number_type == ::FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) || + (frame->header.number_type == ::FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0) + ) { + printf("content... "); + fflush(stdout); + } + + return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; +} + +void DecoderCommon::common_metadata_callback_(const ::FLAC__StreamMetadata *metadata) +{ + if(error_occurred_) + return; + + printf("%u... ", current_metadata_number_); + fflush(stdout); + + if(current_metadata_number_ >= num_expected_) { + (void)die_("got more metadata blocks than expected"); + error_occurred_ = true; + } + else { + if(!::FLAC__metadata_object_is_equal(expected_metadata_sequence_[current_metadata_number_], metadata)) { + (void)die_("metadata block mismatch"); + error_occurred_ = true; + } + } + current_metadata_number_++; +} + +void DecoderCommon::common_error_callback_(::FLAC__StreamDecoderErrorStatus status) +{ + if(!ignore_errors_) { + printf("ERROR: got error callback: err = %u (%s)\n", (uint32_t)status, ::FLAC__StreamDecoderErrorStatusString[status]); + error_occurred_ = true; + } +} + +class StreamDecoder : public FLAC::Decoder::Stream, public DecoderCommon { +public: + FILE *file_; + + StreamDecoder(Layer layer): FLAC::Decoder::Stream(), DecoderCommon(layer), file_(0) { } + ~StreamDecoder() { } + + // from FLAC::Decoder::Stream + ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes); + ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset); + ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset); + ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length); + bool eof_callback(); + ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]); + void metadata_callback(const ::FLAC__StreamMetadata *metadata); + void error_callback(::FLAC__StreamDecoderErrorStatus status); + + bool test_respond(bool is_ogg); +private: + StreamDecoder(const StreamDecoder&); + StreamDecoder&operator=(const StreamDecoder&); +}; + +::FLAC__StreamDecoderReadStatus StreamDecoder::read_callback(FLAC__byte buffer[], size_t *bytes) +{ + const size_t requested_bytes = *bytes; + + if(error_occurred_) + return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; + + if(feof(file_)) { + *bytes = 0; + return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; + } + else if(requested_bytes > 0) { + *bytes = ::fread(buffer, 1, requested_bytes, file_); + if(*bytes == 0) { + if(feof(file_)) + return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; + else + return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; + } + else { + return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; + } + } + else + return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */ +} + +::FLAC__StreamDecoderSeekStatus StreamDecoder::seek_callback(FLAC__uint64 absolute_byte_offset) +{ + if(layer_ == LAYER_STREAM) + return ::FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED; + + if(error_occurred_) + return ::FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; + + if(fseeko(file_, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) { + error_occurred_ = true; + return ::FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; + } + + return ::FLAC__STREAM_DECODER_SEEK_STATUS_OK; +} + +::FLAC__StreamDecoderTellStatus StreamDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset) +{ + if(layer_ == LAYER_STREAM) + return ::FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED; + + if(error_occurred_) + return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR; + + FLAC__off_t offset = ftello(file_); + *absolute_byte_offset = (FLAC__uint64)offset; + + if(offset < 0) { + error_occurred_ = true; + return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR; + } + + return ::FLAC__STREAM_DECODER_TELL_STATUS_OK; +} + +::FLAC__StreamDecoderLengthStatus StreamDecoder::length_callback(FLAC__uint64 *stream_length) +{ + if(layer_ == LAYER_STREAM) + return ::FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED; + + if(error_occurred_) + return ::FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; + + *stream_length = (FLAC__uint64)flacfilesize_; + return ::FLAC__STREAM_DECODER_LENGTH_STATUS_OK; +} + +bool StreamDecoder::eof_callback() +{ + if(layer_ == LAYER_STREAM) + return false; + + if(error_occurred_) + return true; + + return (bool)feof(file_); +} + +::FLAC__StreamDecoderWriteStatus StreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) +{ + (void)buffer; + + return common_write_callback_(frame); +} + +void StreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata) +{ + common_metadata_callback_(metadata); +} + +void StreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status) +{ + common_error_callback_(status); +} + +bool StreamDecoder::test_respond(bool is_ogg) +{ + ::FLAC__StreamDecoderInitStatus init_status; + + if(!set_md5_checking(true)) { + printf("FAILED at set_md5_checking(), returned false\n"); + return false; + } + + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? init_ogg() : init(); + if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK) + return die_s_(0, this); + printf("OK\n"); + + current_metadata_number_ = 0; + + if(fseeko(file_, 0, SEEK_SET) < 0) { + printf("FAILED rewinding input, errno = %d\n", errno); + return false; + } + + printf("testing process_until_end_of_stream()... "); + if(!process_until_end_of_stream()) { + State state = get_state(); + printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring()); + return false; + } + printf("OK\n"); + + printf("testing finish()... "); + if(!finish()) { + State state = get_state(); + printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring()); + return false; + } + printf("OK\n"); + + return true; +} + +class FileDecoder : public FLAC::Decoder::File, public DecoderCommon { +public: + FileDecoder(Layer layer): FLAC::Decoder::File(), DecoderCommon(layer) { } + ~FileDecoder() { } + + // from FLAC::Decoder::Stream + ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]); + void metadata_callback(const ::FLAC__StreamMetadata *metadata); + void error_callback(::FLAC__StreamDecoderErrorStatus status); + + bool test_respond(bool is_ogg); +}; + +::FLAC__StreamDecoderWriteStatus FileDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) +{ + (void)buffer; + return common_write_callback_(frame); +} + +void FileDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata) +{ + common_metadata_callback_(metadata); +} + +void FileDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status) +{ + common_error_callback_(status); +} + +bool FileDecoder::test_respond(bool is_ogg) +{ + ::FLAC__StreamDecoderInitStatus init_status; + + if(!set_md5_checking(true)) { + printf("FAILED at set_md5_checking(), returned false\n"); + return false; + } + + switch(layer_) { + case LAYER_FILE: + { + printf("opening %sFLAC file... ", is_ogg? "Ogg ":""); + FILE *file = ::flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == file) { + printf("ERROR (%s)\n", strerror(errno)); + return false; + } + printf("OK\n"); + + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? init_ogg(file) : init(file); + } + break; + case LAYER_FILENAME: + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? init_ogg(flacfilename(is_ogg)) : init(flacfilename(is_ogg)); + break; + default: + die_("internal error 001"); + return false; + } + if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK) + return die_s_(0, this); + printf("OK\n"); + + current_metadata_number_ = 0; + + printf("testing process_until_end_of_stream()... "); + if(!process_until_end_of_stream()) { + State state = get_state(); + printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring()); + return false; + } + printf("OK\n"); + + printf("testing finish()... "); + if(!finish()) { + State state = get_state(); + printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring()); + return false; + } + printf("OK\n"); + + return true; +} + + +static FLAC::Decoder::Stream *new_by_layer(Layer layer) +{ + if(layer < LAYER_FILE) + return new StreamDecoder(layer); + else + return new FileDecoder(layer); +} + +static bool test_stream_decoder(Layer layer, bool is_ogg) +{ + FLAC::Decoder::Stream *decoder; + ::FLAC__StreamDecoderInitStatus init_status; + bool expect; + + printf("\n+++ libFLAC++ unit test: FLAC::Decoder::%s (layer: %s, format: %s)\n\n", layer<LAYER_FILE? "Stream":"File", LayerString[layer], is_ogg? "Ogg FLAC" : "FLAC"); + + // + // test new -> delete + // + printf("allocating decoder instance... "); + decoder = new_by_layer(layer); + if(0 == decoder) { + printf("FAILED, new returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing is_valid()... "); + if(!decoder->is_valid()) { + printf("FAILED, returned false\n"); + delete decoder; + return false; + } + printf("OK\n"); + + printf("freeing decoder instance... "); + delete decoder; + printf("OK\n"); + + // + // test new -> init -> delete + // + printf("allocating decoder instance... "); + decoder = new_by_layer(layer); + if(0 == decoder) { + printf("FAILED, new returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing is_valid()... "); + if(!decoder->is_valid()) { + printf("FAILED, returned false\n"); + delete decoder; + return false; + } + printf("OK\n"); + + printf("testing init%s()... ", is_ogg? "_ogg":""); + switch(layer) { + case LAYER_STREAM: + case LAYER_SEEKABLE_STREAM: + dynamic_cast<StreamDecoder*>(decoder)->file_ = stdin; + init_status = is_ogg? decoder->init_ogg() : decoder->init(); + break; + case LAYER_FILE: + init_status = is_ogg? + dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(stdin) : + dynamic_cast<FLAC::Decoder::File*>(decoder)->init(stdin); + break; + case LAYER_FILENAME: + init_status = is_ogg? + dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(flacfilename(is_ogg)) : + dynamic_cast<FLAC::Decoder::File*>(decoder)->init(flacfilename(is_ogg)); + break; + default: + die_("internal error 006"); + delete decoder; + return false; + } + if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK) + return die_s_(0, decoder); + printf("OK\n"); + + printf("freeing decoder instance... "); + delete decoder; + printf("OK\n"); + + // + // test normal usage + // + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + + printf("allocating decoder instance... "); + decoder = new_by_layer(layer); + if(0 == decoder) { + printf("FAILED, new returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing is_valid()... "); + if(!decoder->is_valid()) { + printf("FAILED, returned false\n"); + delete decoder; + return false; + } + printf("OK\n"); + + if(is_ogg) { + printf("testing set_ogg_serial_number()... "); + if(!decoder->set_ogg_serial_number(file_utils__ogg_serial_number)) + return die_s_("returned false", decoder); + printf("OK\n"); + } + + if(!decoder->set_md5_checking(true)) { + printf("FAILED at set_md5_checking(), returned false\n"); + return false; + } + + switch(layer) { + case LAYER_STREAM: + case LAYER_SEEKABLE_STREAM: + printf("opening %sFLAC file... ", is_ogg? "Ogg ":""); + dynamic_cast<StreamDecoder*>(decoder)->file_ = ::flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == dynamic_cast<StreamDecoder*>(decoder)->file_) { + printf("ERROR (%s)\n", strerror(errno)); + return false; + } + printf("OK\n"); + + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? decoder->init_ogg() : decoder->init(); + break; + case LAYER_FILE: + { + printf("opening FLAC file... "); + FILE *file = ::flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == file) { + printf("ERROR (%s)\n", strerror(errno)); + return false; + } + printf("OK\n"); + + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? + dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(file) : + dynamic_cast<FLAC::Decoder::File*>(decoder)->init(file); + } + break; + case LAYER_FILENAME: + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? + dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(flacfilename(is_ogg)) : + dynamic_cast<FLAC::Decoder::File*>(decoder)->init(flacfilename(is_ogg)); + break; + default: + die_("internal error 009"); + return false; + } + if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK) + return die_s_(0, decoder); + printf("OK\n"); + + printf("testing get_state()... "); + FLAC::Decoder::Stream::State state = decoder->get_state(); + printf("returned state = %u (%s)... OK\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring()); + + dynamic_cast<DecoderCommon*>(decoder)->current_metadata_number_ = 0; + dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = false; + dynamic_cast<DecoderCommon*>(decoder)->error_occurred_ = false; + + printf("testing get_md5_checking()... "); + if(!decoder->get_md5_checking()) { + printf("FAILED, returned false, expected true\n"); + return false; + } + printf("OK\n"); + + printf("testing process_until_end_of_metadata()... "); + if(!decoder->process_until_end_of_metadata()) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing process_single()... "); + if(!decoder->process_single()) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing skip_single_frame()... "); + if(!decoder->skip_single_frame()) + return die_s_("returned false", decoder); + printf("OK\n"); + + if(layer < LAYER_FILE) { + printf("testing flush()... "); + if(!decoder->flush()) + return die_s_("returned false", decoder); + printf("OK\n"); + + dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = true; + printf("testing process_single()... "); + if(!decoder->process_single()) + return die_s_("returned false", decoder); + printf("OK\n"); + dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = false; + } + + expect = (layer != LAYER_STREAM); + printf("testing seek_absolute()... "); + if(decoder->seek_absolute(0) != expect) + return die_s_(expect? "returned false" : "returned true", decoder); + printf("OK\n"); + + printf("testing process_until_end_of_stream()... "); + if(!decoder->process_until_end_of_stream()) + return die_s_("returned false", decoder); + printf("OK\n"); + + expect = (layer != LAYER_STREAM); + printf("testing seek_absolute()... "); + if(decoder->seek_absolute(0) != expect) + return die_s_(expect? "returned false" : "returned true", decoder); + printf("OK\n"); + + printf("testing get_channels()... "); + { + uint32_t channels = decoder->get_channels(); + if(channels != streaminfo_.data.stream_info.channels) { + printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels); + return false; + } + } + printf("OK\n"); + + printf("testing get_bits_per_sample()... "); + { + uint32_t bits_per_sample = decoder->get_bits_per_sample(); + if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) { + printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample); + return false; + } + } + printf("OK\n"); + + printf("testing get_sample_rate()... "); + { + uint32_t sample_rate = decoder->get_sample_rate(); + if(sample_rate != streaminfo_.data.stream_info.sample_rate) { + printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate); + return false; + } + } + printf("OK\n"); + + printf("testing get_blocksize()... "); + { + uint32_t blocksize = decoder->get_blocksize(); + /* value could be anything since we're at the last block, so accept any reasonable answer */ + printf("returned %u... %s\n", blocksize, blocksize>0? "OK" : "FAILED"); + if(blocksize == 0) + return false; + } + + printf("testing get_channel_assignment()... "); + { + ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment(); + printf("returned %u (%s)... OK\n", (uint32_t)ca, ::FLAC__ChannelAssignmentString[ca]); + } + + if(layer < LAYER_FILE) { + printf("testing reset()... "); + if(!decoder->reset()) + return die_s_("returned false", decoder); + printf("OK\n"); + + if(layer == LAYER_STREAM) { + /* after a reset() we have to rewind the input ourselves */ + printf("rewinding input... "); + if(fseeko(dynamic_cast<StreamDecoder*>(decoder)->file_, 0, SEEK_SET) < 0) { + printf("FAILED, errno = %d\n", errno); + return false; + } + printf("OK\n"); + } + + dynamic_cast<DecoderCommon*>(decoder)->current_metadata_number_ = 0; + + printf("testing process_until_end_of_stream()... "); + if(!decoder->process_until_end_of_stream()) + return die_s_("returned false", decoder); + printf("OK\n"); + } + + printf("testing finish()... "); + if(!decoder->finish()) { + state = decoder->get_state(); + printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring()); + return false; + } + printf("OK\n"); + + /* + * respond all + */ + + printf("testing set_metadata_respond_all()... "); + if(!decoder->set_metadata_respond_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &application1_; + expected_metadata_sequence_[num_expected_++] = &application2_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * ignore all + */ + + printf("testing set_metadata_ignore_all()... "); + if(!decoder->set_metadata_ignore_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * respond all, ignore VORBIS_COMMENT + */ + + printf("testing set_metadata_respond_all()... "); + if(!decoder->set_metadata_respond_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_ignore(VORBIS_COMMENT)... "); + if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &padding_; + if(!is_ogg) /* encoder removes seektable for ogg */ + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * respond all, ignore APPLICATION + */ + + printf("testing set_metadata_respond_all()... "); + if(!decoder->set_metadata_respond_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_ignore(APPLICATION)... "); + if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * respond all, ignore APPLICATION id of app#1 + */ + + printf("testing set_metadata_respond_all()... "); + if(!decoder->set_metadata_respond_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_ignore_application(of app block #1)... "); + if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &application2_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &padding_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * respond all, ignore APPLICATION id of app#1 & app#2 + */ + + printf("testing set_metadata_respond_all()... "); + if(!decoder->set_metadata_respond_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_ignore_application(of app block #1)... "); + if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_ignore_application(of app block #2)... "); + if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * ignore all, respond VORBIS_COMMENT + */ + + printf("testing set_metadata_ignore_all()... "); + if(!decoder->set_metadata_ignore_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_respond(VORBIS_COMMENT)... "); + if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * ignore all, respond APPLICATION + */ + + printf("testing set_metadata_ignore_all()... "); + if(!decoder->set_metadata_ignore_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_respond(APPLICATION)... "); + if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &application1_; + expected_metadata_sequence_[num_expected_++] = &application2_; + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * ignore all, respond APPLICATION id of app#1 + */ + + printf("testing set_metadata_ignore_all()... "); + if(!decoder->set_metadata_ignore_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_respond_application(of app block #1)... "); + if(!decoder->set_metadata_respond_application(application1_.data.application.id)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &application1_; + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * ignore all, respond APPLICATION id of app#1 & app#2 + */ + + printf("testing set_metadata_ignore_all()... "); + if(!decoder->set_metadata_ignore_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_respond_application(of app block #1)... "); + if(!decoder->set_metadata_respond_application(application1_.data.application.id)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_respond_application(of app block #2)... "); + if(!decoder->set_metadata_respond_application(application2_.data.application.id)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &application1_; + expected_metadata_sequence_[num_expected_++] = &application2_; + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * respond all, ignore APPLICATION, respond APPLICATION id of app#1 + */ + + printf("testing set_metadata_respond_all()... "); + if(!decoder->set_metadata_respond_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_ignore(APPLICATION)... "); + if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_respond_application(of app block #1)... "); + if(!decoder->set_metadata_respond_application(application1_.data.application.id)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &application1_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &padding_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + /* + * ignore all, respond APPLICATION, ignore APPLICATION id of app#1 + */ + + printf("testing set_metadata_ignore_all()... "); + if(!decoder->set_metadata_ignore_all()) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_respond(APPLICATION)... "); + if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing set_metadata_ignore_application(of app block #1)... "); + if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &application2_; + + if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg))) + return false; + + if(layer < LAYER_FILE) /* for LAYER_FILE, FLAC__stream_decoder_finish() closes the file */ + ::fclose(dynamic_cast<StreamDecoder*>(decoder)->file_); + + printf("freeing decoder instance... "); + delete decoder; + printf("OK\n"); + + printf("\nPASSED!\n"); + + return true; +} + +bool test_decoders() +{ + FLAC__bool is_ogg = false; + + while(1) { + init_metadata_blocks_(); + + if(!generate_file_(is_ogg)) + return false; + + if(!test_stream_decoder(LAYER_STREAM, is_ogg)) + return false; + + if(!test_stream_decoder(LAYER_SEEKABLE_STREAM, is_ogg)) + return false; + + if(!test_stream_decoder(LAYER_FILE, is_ogg)) + return false; + + if(!test_stream_decoder(LAYER_FILENAME, is_ogg)) + return false; + + (void) grabbag__file_remove_file(flacfilename(is_ogg)); + + free_metadata_blocks_(); + + if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg) + break; + is_ogg = true; + } + + return true; +} diff --git a/src/test_libFLAC++/decoders.h b/src/test_libFLAC++/decoders.h new file mode 100644 index 0000000..4bbce13 --- /dev/null +++ b/src/test_libFLAC++/decoders.h @@ -0,0 +1,25 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLACPP_DECODERS_H +#define FLAC__TEST_LIBFLACPP_DECODERS_H + +bool test_decoders(); + +#endif diff --git a/src/test_libFLAC++/encoders.cpp b/src/test_libFLAC++/encoders.cpp new file mode 100644 index 0000000..0e356af --- /dev/null +++ b/src/test_libFLAC++/encoders.cpp @@ -0,0 +1,576 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "encoders.h" +#include "FLAC/assert.h" +#include "FLAC++/encoder.h" +#include "share/grabbag.h" +extern "C" { +#include "test_libs_common/file_utils_flac.h" +#include "test_libs_common/metadata_utils.h" +} +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "share/compat.h" + +#ifdef _MSC_VER +// warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning) +#pragma warning ( disable : 4800 ) +#endif + +typedef enum { + LAYER_STREAM = 0, /* FLAC__stream_encoder_init_stream() without seeking */ + LAYER_SEEKABLE_STREAM, /* FLAC__stream_encoder_init_stream() with seeking */ + LAYER_FILE, /* FLAC__stream_encoder_init_FILE() */ + LAYER_FILENAME /* FLAC__stream_encoder_init_file() */ +} Layer; + +static const char * const LayerString[] = { + "Stream", + "Seekable Stream", + "FILE*", + "Filename" +}; + +static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_; +static ::FLAC__StreamMetadata *metadata_sequence_[] = { &vorbiscomment_, &padding_, &seektable_, &application1_, &application2_, &cuesheet_, &picture_, &unknown_ }; +static const uint32_t num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]); + +static const char *flacfilename(bool is_ogg) +{ + return is_ogg? "metadata.oga" : "metadata.flac"; +} + +static bool die_(const char *msg) +{ + printf("ERROR: %s\n", msg); + return false; +} + +static bool die_s_(const char *msg, const FLAC::Encoder::Stream *encoder) +{ + FLAC::Encoder::Stream::State state = encoder->get_state(); + + if(msg) + printf("FAILED, %s", msg); + else + printf("FAILED"); + + printf(", state = %u (%s)\n", (uint32_t)((::FLAC__StreamEncoderState)state), state.as_cstring()); + if(state == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) { + FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state(); + printf(" verify decoder state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring()); + } + + return false; +} + +static void init_metadata_blocks_() +{ + mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_); +} + +static void free_metadata_blocks_() +{ + mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_); +} + +class StreamEncoder : public FLAC::Encoder::Stream { +public: + Layer layer_; + FILE *file_; + + StreamEncoder(Layer layer): FLAC::Encoder::Stream(), layer_(layer), file_(0) { } + ~StreamEncoder() { } + + // from FLAC::Encoder::Stream + ::FLAC__StreamEncoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes); + ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame); + ::FLAC__StreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset); + ::FLAC__StreamEncoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset); + void metadata_callback(const ::FLAC__StreamMetadata *metadata); +private: + StreamEncoder(const StreamEncoder&); + StreamEncoder&operator=(const StreamEncoder&); +}; + +::FLAC__StreamEncoderReadStatus StreamEncoder::read_callback(FLAC__byte buffer[], size_t *bytes) +{ + if(*bytes > 0) { + *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file_); + if(ferror(file_)) + return ::FLAC__STREAM_ENCODER_READ_STATUS_ABORT; + else if(*bytes == 0) + return ::FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM; + else + return ::FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE; + } + else + return ::FLAC__STREAM_ENCODER_READ_STATUS_ABORT; +} + +::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame) +{ + (void)samples, (void)current_frame; + + if(fwrite(buffer, 1, bytes, file_) != bytes) + return ::FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; + else + return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK; +} + +::FLAC__StreamEncoderSeekStatus StreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset) +{ + if(layer_==LAYER_STREAM) + return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED; + else if(fseeko(file_, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) + return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR; + else + return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; +} + +::FLAC__StreamEncoderTellStatus StreamEncoder::tell_callback(FLAC__uint64 *absolute_byte_offset) +{ + FLAC__off_t pos; + if(layer_==LAYER_STREAM) + return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED; + else if((pos = ftello(file_)) < 0) + return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR; + else { + *absolute_byte_offset = (FLAC__uint64)pos; + return FLAC__STREAM_ENCODER_TELL_STATUS_OK; + } +} + +void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata) +{ + (void)metadata; +} + +class FileEncoder : public FLAC::Encoder::File { +public: + Layer layer_; + + FileEncoder(Layer layer): FLAC::Encoder::File(), layer_(layer) { } + ~FileEncoder() { } + + // from FLAC::Encoder::File + void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, uint32_t frames_written, uint32_t total_frames_estimate); +}; + +void FileEncoder::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, uint32_t frames_written, uint32_t total_frames_estimate) +{ + (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate; +} + +static FLAC::Encoder::Stream *new_by_layer(Layer layer) +{ + if(layer < LAYER_FILE) + return new StreamEncoder(layer); + else + return new FileEncoder(layer); +} + +static bool test_stream_encoder(Layer layer, bool is_ogg) +{ + FLAC::Encoder::Stream *encoder; + ::FLAC__StreamEncoderInitStatus init_status; + FILE *file = 0; + FLAC__int32 samples[1024]; + FLAC__int32 *samples_array[1] = { samples }; + uint32_t i; + + printf("\n+++ libFLAC++ unit test: FLAC::Encoder::%s (layer: %s, format: %s)\n\n", layer<LAYER_FILE? "Stream":"File", LayerString[layer], is_ogg? "Ogg FLAC":"FLAC"); + + printf("allocating encoder instance... "); + encoder = new_by_layer(layer); + if(0 == encoder) { + printf("FAILED, new returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing is_valid()... "); + if(!encoder->is_valid()) { + printf("FAILED, returned false\n"); + delete encoder; + return false; + } + printf("OK\n"); + + if(is_ogg) { + printf("testing set_ogg_serial_number()... "); + if(!encoder->set_ogg_serial_number(file_utils__ogg_serial_number)) + return die_s_("returned false", encoder); + printf("OK\n"); + } + + printf("testing set_verify()... "); + if(!encoder->set_verify(true)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_streamable_subset()... "); + if(!encoder->set_streamable_subset(true)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_channels()... "); + if(!encoder->set_channels(streaminfo_.data.stream_info.channels)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_bits_per_sample()... "); + if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_sample_rate()... "); + if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_compression_level()... "); + if(!encoder->set_compression_level((uint32_t)(-1))) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_blocksize()... "); + if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_do_mid_side_stereo()... "); + if(!encoder->set_do_mid_side_stereo(false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_loose_mid_side_stereo()... "); + if(!encoder->set_loose_mid_side_stereo(false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_max_lpc_order()... "); + if(!encoder->set_max_lpc_order(0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_qlp_coeff_precision()... "); + if(!encoder->set_qlp_coeff_precision(0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_do_qlp_coeff_prec_search()... "); + if(!encoder->set_do_qlp_coeff_prec_search(false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_do_escape_coding()... "); + if(!encoder->set_do_escape_coding(false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_do_exhaustive_model_search()... "); + if(!encoder->set_do_exhaustive_model_search(false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_min_residual_partition_order()... "); + if(!encoder->set_min_residual_partition_order(0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_max_residual_partition_order()... "); + if(!encoder->set_max_residual_partition_order(0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_rice_parameter_search_dist()... "); + if(!encoder->set_rice_parameter_search_dist(0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_total_samples_estimate()... "); + if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_metadata()... "); + if(!encoder->set_metadata(metadata_sequence_, num_metadata_)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing set_limit_min_bitrate()... "); + if(!encoder->set_limit_min_bitrate(true)) + return die_s_("returned false", encoder); + printf("OK\n"); + + if(layer < LAYER_FILENAME) { + printf("opening file for FLAC output... "); + file = ::flac_fopen(flacfilename(is_ogg), "w+b"); + if(0 == file) { + printf("ERROR (%s)\n", strerror(errno)); + return false; + } + printf("OK\n"); + if(layer < LAYER_FILE) + dynamic_cast<StreamEncoder*>(encoder)->file_ = file; + } + + switch(layer) { + case LAYER_STREAM: + case LAYER_SEEKABLE_STREAM: + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? encoder->init_ogg() : encoder->init(); + break; + case LAYER_FILE: + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? + dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(file) : + dynamic_cast<FLAC::Encoder::File*>(encoder)->init(file); + break; + case LAYER_FILENAME: + printf("testing init%s()... ", is_ogg? "_ogg":""); + init_status = is_ogg? + dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(flacfilename(is_ogg)) : + dynamic_cast<FLAC::Encoder::File*>(encoder)->init(flacfilename(is_ogg)); + break; + default: + die_("internal error 001"); + return false; + } + if(init_status != ::FLAC__STREAM_ENCODER_INIT_STATUS_OK) + return die_s_(0, encoder); + printf("OK\n"); + + printf("testing get_state()... "); + FLAC::Encoder::Stream::State state = encoder->get_state(); + printf("returned state = %u (%s)... OK\n", (uint32_t)((::FLAC__StreamEncoderState)state), state.as_cstring()); + + printf("testing get_verify_decoder_state()... "); + FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state(); + printf("returned state = %u (%s)... OK\n", (uint32_t)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring()); + + { + FLAC__uint64 absolute_sample; + uint32_t frame_number; + uint32_t channel; + uint32_t sample; + FLAC__int32 expected; + FLAC__int32 got; + + printf("testing get_verify_decoder_error_stats()... "); + encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got); + printf("OK\n"); + } + + printf("testing get_verify()... "); + if(encoder->get_verify() != true) { + printf("FAILED, expected true, got false\n"); + return false; + } + printf("OK\n"); + + printf("testing get_streamable_subset()... "); + if(encoder->get_streamable_subset() != true) { + printf("FAILED, expected true, got false\n"); + return false; + } + printf("OK\n"); + + printf("testing get_do_mid_side_stereo()... "); + if(encoder->get_do_mid_side_stereo() != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing get_loose_mid_side_stereo()... "); + if(encoder->get_loose_mid_side_stereo() != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing get_channels()... "); + if(encoder->get_channels() != streaminfo_.data.stream_info.channels) { + printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels()); + return false; + } + printf("OK\n"); + + printf("testing get_bits_per_sample()... "); + if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) { + printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample()); + return false; + } + printf("OK\n"); + + printf("testing get_sample_rate()... "); + if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) { + printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate()); + return false; + } + printf("OK\n"); + + printf("testing get_blocksize()... "); + if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) { + printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize()); + return false; + } + printf("OK\n"); + + printf("testing get_max_lpc_order()... "); + if(encoder->get_max_lpc_order() != 0) { + printf("FAILED, expected %d, got %u\n", 0, encoder->get_max_lpc_order()); + return false; + } + printf("OK\n"); + + printf("testing get_qlp_coeff_precision()... "); + (void)encoder->get_qlp_coeff_precision(); + /* we asked the encoder to auto select this so we accept anything */ + printf("OK\n"); + + printf("testing get_do_qlp_coeff_prec_search()... "); + if(encoder->get_do_qlp_coeff_prec_search() != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing get_do_escape_coding()... "); + if(encoder->get_do_escape_coding() != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing get_do_exhaustive_model_search()... "); + if(encoder->get_do_exhaustive_model_search() != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing get_min_residual_partition_order()... "); + if(encoder->get_min_residual_partition_order() != 0) { + printf("FAILED, expected %d, got %u\n", 0, encoder->get_min_residual_partition_order()); + return false; + } + printf("OK\n"); + + printf("testing get_max_residual_partition_order()... "); + if(encoder->get_max_residual_partition_order() != 0) { + printf("FAILED, expected %d, got %u\n", 0, encoder->get_max_residual_partition_order()); + return false; + } + printf("OK\n"); + + printf("testing get_rice_parameter_search_dist()... "); + if(encoder->get_rice_parameter_search_dist() != 0) { + printf("FAILED, expected %d, got %u\n", 0, encoder->get_rice_parameter_search_dist()); + return false; + } + printf("OK\n"); + + printf("testing get_total_samples_estimate()... "); + if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) { + printf("FAILED, expected %" PRIu64 ", got %" PRIu64 "\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate()); + return false; + } + printf("OK\n"); + + printf("testing get_limit_min_bitrate()... "); + if(encoder->get_limit_min_bitrate() != true) { + printf("FAILED, expected true, got false\n"); + return false; + } + printf("OK\n"); + + /* init the dummy sample buffer */ + for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++) + samples[i] = i & 7; + + printf("testing process()... "); + if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32))) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing process_interleaved()... "); + if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32))) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing finish()... "); + if(!encoder->finish()) { + state = encoder->get_state(); + printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamEncoderState)state), state.as_cstring()); + return false; + } + printf("OK\n"); + + if(layer < LAYER_FILE) + ::fclose(dynamic_cast<StreamEncoder*>(encoder)->file_); + + printf("freeing encoder instance... "); + delete encoder; + printf("OK\n"); + + printf("\nPASSED!\n"); + + return true; +} + +bool test_encoders() +{ + FLAC__bool is_ogg = false; + + while(1) { + init_metadata_blocks_(); + + if(!test_stream_encoder(LAYER_STREAM, is_ogg)) + return false; + + if(!test_stream_encoder(LAYER_SEEKABLE_STREAM, is_ogg)) + return false; + + if(!test_stream_encoder(LAYER_FILE, is_ogg)) + return false; + + if(!test_stream_encoder(LAYER_FILENAME, is_ogg)) + return false; + + (void) grabbag__file_remove_file(flacfilename(is_ogg)); + + free_metadata_blocks_(); + + if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg) + break; + is_ogg = true; + } + + return true; +} diff --git a/src/test_libFLAC++/encoders.h b/src/test_libFLAC++/encoders.h new file mode 100644 index 0000000..9da415c --- /dev/null +++ b/src/test_libFLAC++/encoders.h @@ -0,0 +1,25 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLACPP_ENCODERS_H +#define FLAC__TEST_LIBFLACPP_ENCODERS_H + +bool test_encoders(); + +#endif diff --git a/src/test_libFLAC++/main.cpp b/src/test_libFLAC++/main.cpp new file mode 100644 index 0000000..7183def --- /dev/null +++ b/src/test_libFLAC++/main.cpp @@ -0,0 +1,42 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "decoders.h" +#include "encoders.h" +#include "metadata.h" + +int main(int argc, char *argv[]) +{ + (void)argc, (void)argv; + + if(!test_encoders()) + return 1; + + if(!test_decoders()) + return 1; + + if(!test_metadata()) + return 1; + + return 0; +} diff --git a/src/test_libFLAC++/metadata.cpp b/src/test_libFLAC++/metadata.cpp new file mode 100644 index 0000000..a31031b --- /dev/null +++ b/src/test_libFLAC++/metadata.cpp @@ -0,0 +1,41 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "metadata.h" +#include <stdio.h> + +extern bool test_metadata_object(); +extern bool test_metadata_file_manipulation(); + +bool test_metadata() +{ + if(!test_metadata_object()) + return false; + + if(!test_metadata_file_manipulation()) + return false; + + printf("\nPASSED!\n"); + + return true; +} diff --git a/src/test_libFLAC++/metadata.h b/src/test_libFLAC++/metadata.h new file mode 100644 index 0000000..8c45bc0 --- /dev/null +++ b/src/test_libFLAC++/metadata.h @@ -0,0 +1,25 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLACPP_METADATA_H +#define FLAC__TEST_LIBFLACPP_METADATA_H + +bool test_metadata(); + +#endif diff --git a/src/test_libFLAC++/metadata_manip.cpp b/src/test_libFLAC++/metadata_manip.cpp new file mode 100644 index 0000000..5d395db --- /dev/null +++ b/src/test_libFLAC++/metadata_manip.cpp @@ -0,0 +1,2241 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> /* for malloc() */ +#include <string.h> /* for memcpy()/memset() */ +#include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */ +#ifdef _MSC_VER +#include <sys/utime.h> +#endif +#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__ +#include <unistd.h> /* for chown(), unlink() */ +#endif +#include <sys/stat.h> /* for stat(), maybe chmod() */ +#include "FLAC/assert.h" +#include "FLAC++/decoder.h" +#include "FLAC++/metadata.h" +#include "share/grabbag.h" +#include "share/compat.h" +#include "share/macros.h" +#include "share/safe_str.h" +extern "C" { +#include "test_libs_common/file_utils_flac.h" +} + +/****************************************************************************** + The general strategy of these tests (for interface levels 1 and 2) is + to create a dummy FLAC file with a known set of initial metadata + blocks, then keep a mirror locally of what we expect the metadata to be + after each operation. Then testing becomes a simple matter of running + a FLAC::Decoder::File over the dummy file after each operation, comparing + the decoded metadata to what's in our local copy. If there are any + differences in the metadata, or the actual audio data is corrupted, we + will catch it while decoding. +******************************************************************************/ + +class OurFileDecoder: public FLAC::Decoder::File { +public: + inline OurFileDecoder(bool ignore_metadata): ignore_metadata_(ignore_metadata), error_occurred_(false) { } + + bool ignore_metadata_; + bool error_occurred_; +protected: + ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]); + void metadata_callback(const ::FLAC__StreamMetadata *metadata); + void error_callback(::FLAC__StreamDecoderErrorStatus status); +}; + +struct OurMetadata { + FLAC::Metadata::Prototype *blocks[64]; + uint32_t num_blocks; +}; + +/* our copy of the metadata in flacfilename() */ +static OurMetadata our_metadata_; + +/* the current block number that corresponds to the position of the iterator we are testing */ +static uint32_t mc_our_block_number_ = 0; + +static const char *flacfilename(bool is_ogg) +{ + return is_ogg? "metadata.oga" : "metadata.flac"; +} + +static bool die_(const char *msg) +{ + printf("ERROR: %s\n", msg); + return false; +} + +static bool die_c_(const char *msg, FLAC::Metadata::Chain::Status status) +{ + printf("ERROR: %s\n", msg); + printf(" status=%u (%s)\n", (uint32_t)((::FLAC__Metadata_ChainStatus)status), status.as_cstring()); + return false; +} + +static bool die_ss_(const char *msg, FLAC::Metadata::SimpleIterator &iterator) +{ + const FLAC::Metadata::SimpleIterator::Status status = iterator.status(); + printf("ERROR: %s\n", msg); + printf(" status=%u (%s)\n", (uint32_t)((::FLAC__Metadata_SimpleIteratorStatus)status), status.as_cstring()); + 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", (uint32_t)size); + exit(1); + } + return x; +} + +static char *strdup_or_die_(const char *s) +{ + char *x = strdup(s); + if(0 == x) { + fprintf(stderr, "ERROR: out of memory copying string \"%s\"\n", s); + exit(1); + } + return x; +} + +/* functions for working with our metadata copy */ + +static bool replace_in_our_metadata_(FLAC::Metadata::Prototype *block, uint32_t position, bool copy) +{ + uint32_t i; + FLAC::Metadata::Prototype *obj = block; + FLAC__ASSERT(position < our_metadata_.num_blocks); + if(copy) { + if(0 == (obj = FLAC::Metadata::clone(block))) + return die_("during FLAC::Metadata::clone()"); + } + delete our_metadata_.blocks[position]; + our_metadata_.blocks[position] = obj; + + /* set the is_last flags */ + for(i = 0; i < our_metadata_.num_blocks - 1; i++) + our_metadata_.blocks[i]->set_is_last(false); + our_metadata_.blocks[i]->set_is_last(true); + + return true; +} + +static bool insert_to_our_metadata_(FLAC::Metadata::Prototype *block, uint32_t position, bool copy) +{ + uint32_t i; + FLAC::Metadata::Prototype *obj = block; + if(copy) { + if(0 == (obj = FLAC::Metadata::clone(block))) + return die_("during FLAC::Metadata::clone()"); + } + if(position > our_metadata_.num_blocks) { + position = our_metadata_.num_blocks; + } + else { + for(i = our_metadata_.num_blocks; i > position; i--) + our_metadata_.blocks[i] = our_metadata_.blocks[i-1]; + } + our_metadata_.blocks[position] = obj; + our_metadata_.num_blocks++; + + /* set the is_last flags */ + for(i = 0; i < our_metadata_.num_blocks - 1; i++) + our_metadata_.blocks[i]->set_is_last(false); + our_metadata_.blocks[i]->set_is_last(true); + + return true; +} + +static void delete_from_our_metadata_(uint32_t position) +{ + uint32_t i; + FLAC__ASSERT(position < our_metadata_.num_blocks); + delete our_metadata_.blocks[position]; + for(i = position; i < our_metadata_.num_blocks - 1; i++) + our_metadata_.blocks[i] = our_metadata_.blocks[i+1]; + our_metadata_.num_blocks--; + + /* set the is_last flags */ + if(our_metadata_.num_blocks > 0) { + for(i = 0; i < our_metadata_.num_blocks - 1; i++) + our_metadata_.blocks[i]->set_is_last(false); + our_metadata_.blocks[i]->set_is_last(true); + } +} + +void add_to_padding_length_(uint32_t indx, int delta) +{ + FLAC::Metadata::Padding *padding = dynamic_cast<FLAC::Metadata::Padding *>(our_metadata_.blocks[indx]); + FLAC__ASSERT(0 != padding); + padding->set_length((uint32_t)((int)padding->get_length() + delta)); +} + +/* + * This wad of functions supports filename- and callback-based chain reading/writing. + * Everything up to set_file_stats_() is copied from libFLAC/metadata_iterators.c + */ +bool open_tempfile_(const char *filename, FILE **tempfile, char **tempfilename) +{ + static const char *tempfile_suffix = ".metadata_edit"; + size_t destlen = strlen(filename) + strlen(tempfile_suffix) + 1; + + *tempfilename = (char*)malloc(destlen); + if (*tempfilename == 0) + return false; + flac_snprintf(*tempfilename, destlen, "%s%s", filename, tempfile_suffix); + + *tempfile = flac_fopen(*tempfilename, "wb"); + if (*tempfile == 0) + return false; + + return true; +} + +void cleanup_tempfile_(FILE **tempfile, char **tempfilename) +{ + if (*tempfile != 0) { + (void)fclose(*tempfile); + *tempfile = 0; + } + + if (*tempfilename != 0) { + (void)flac_unlink(*tempfilename); + free(*tempfilename); + *tempfilename = 0; + } +} + +bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename) +{ + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != tempfile); + FLAC__ASSERT(0 != tempfilename); + FLAC__ASSERT(0 != *tempfilename); + + if(0 != *tempfile) { + (void)fclose(*tempfile); + *tempfile = 0; + } + +#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__ + /* on some flavors of windows, flac_rename() will fail if the destination already exists */ + if(flac_unlink(filename) < 0) { + cleanup_tempfile_(tempfile, tempfilename); + return false; + } +#endif + + if(0 != flac_rename(*tempfilename, filename)) { + cleanup_tempfile_(tempfile, tempfilename); + return false; + } + + cleanup_tempfile_(tempfile, tempfilename); + + return true; +} + +bool get_file_stats_(const char *filename, struct flac_stat_s *stats) +{ + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != stats); + return (0 == flac_stat(filename, stats)); +} + +void set_file_stats_(const char *filename, struct flac_stat_s *stats) +{ + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != stats); + +#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) && !defined(_WIN32) + struct timespec srctime[2] = {}; + srctime[0].tv_sec = stats->st_atime; + srctime[1].tv_sec = stats->st_mtime; +#else + struct utimbuf srctime; + srctime.actime = stats->st_atime; + srctime.modtime = stats->st_mtime; +#endif + (void)flac_chmod(filename, stats->st_mode); + (void)flac_utime(filename, &srctime); +#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__ + FLAC_CHECK_RETURN(chown(filename, stats->st_uid, (gid_t)(-1))); + FLAC_CHECK_RETURN(chown(filename, (uid_t)(-1), stats->st_gid)); +#endif +} + +#ifdef FLAC__VALGRIND_TESTING +static size_t chain_write_cb_(const void *ptr, size_t size, size_t nmemb, ::FLAC__IOHandle handle) +{ + FILE *stream = (FILE*)handle; + size_t ret = fwrite(ptr, size, nmemb, stream); + if(!ferror(stream)) + fflush(stream); + return ret; +} +#endif + +static int chain_seek_cb_(::FLAC__IOHandle handle, FLAC__int64 offset, int whence) +{ + FLAC__off_t o = (FLAC__off_t)offset; + FLAC__ASSERT(offset == o); + return fseeko((FILE*)handle, o, whence); +} + +static FLAC__int64 chain_tell_cb_(::FLAC__IOHandle handle) +{ + return ftello((FILE*)handle); +} + +static int chain_eof_cb_(::FLAC__IOHandle handle) +{ + return feof((FILE*)handle); +} + +static bool write_chain_(FLAC::Metadata::Chain &chain, bool use_padding, bool preserve_file_stats, bool filename_based, const char *filename) +{ + if(filename_based) + return chain.write(use_padding, preserve_file_stats); + else { + ::FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (::FLAC__IOCallback_Read)fread; +#ifdef FLAC__VALGRIND_TESTING + callbacks.write = chain_write_cb_; +#else + callbacks.write = (::FLAC__IOCallback_Write)fwrite; +#endif + callbacks.seek = chain_seek_cb_; + callbacks.eof = chain_eof_cb_; + + if(chain.check_if_tempfile_needed(use_padding)) { + struct flac_stat_s stats; + FILE *file, *tempfile; + char *tempfilename; + if(preserve_file_stats) { + if(!get_file_stats_(filename, &stats)) + return false; + } + if(0 == (file = flac_fopen(filename, "rb"))) + return false; /*@@@@ chain status still says OK though */ + if(!open_tempfile_(filename, &tempfile, &tempfilename)) { + fclose(file); + cleanup_tempfile_(&tempfile, &tempfilename); + return false; /*@@@@ chain status still says OK though */ + } + if(!chain.write(use_padding, (::FLAC__IOHandle)file, callbacks, (::FLAC__IOHandle)tempfile, callbacks)) { + fclose(file); + fclose(tempfile); + return false; + } + fclose(file); + fclose(tempfile); + file = tempfile = 0; + if(!transport_tempfile_(filename, &tempfile, &tempfilename)) + return false; + if(preserve_file_stats) + set_file_stats_(filename, &stats); + } + else { + FILE *file = flac_fopen(filename, "r+b"); + if(0 == file) + return false; /*@@@@ chain status still says OK though */ + if(!chain.write(use_padding, (::FLAC__IOHandle)file, callbacks)) { + fclose(file); + return false; + } + fclose(file); + } + } + + return true; +} + +static bool read_chain_(FLAC::Metadata::Chain &chain, const char *filename, bool filename_based, bool is_ogg) +{ + if(filename_based) + return chain.read(filename, is_ogg); + else { + ::FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (::FLAC__IOCallback_Read)fread; + callbacks.seek = chain_seek_cb_; + callbacks.tell = chain_tell_cb_; + + { + bool ret; + FILE *file = flac_fopen(filename, "rb"); + if(0 == file) + return false; /*@@@@ chain status still says OK though */ + ret = chain.read((::FLAC__IOHandle)file, callbacks, is_ogg); + fclose(file); + return ret; + } + } +} + +/* function for comparing our metadata to a FLAC::Metadata::Chain */ + +static bool compare_chain_(FLAC::Metadata::Chain &chain, uint32_t current_position, FLAC::Metadata::Prototype *current_block) +{ + uint32_t i; + FLAC::Metadata::Iterator iterator; + bool next_ok = true; + + printf("\tcomparing chain... "); + fflush(stdout); + + if(!iterator.is_valid()) + return die_("allocating memory for iterator"); + + iterator.init(chain); + + i = 0; + do { + FLAC::Metadata::Prototype *block; + + printf("%u... ", i); + fflush(stdout); + + if(0 == (block = iterator.get_block())) + return die_("getting block from iterator"); + + if(*block != *our_metadata_.blocks[i]) + return die_("metadata block mismatch"); + + delete block; + i++; + next_ok = iterator.next(); + } while(i < our_metadata_.num_blocks && next_ok); + + if(next_ok) + return die_("chain has more blocks than expected"); + + if(i < our_metadata_.num_blocks) + return die_("short block count in chain"); + + if(0 != current_block) { + printf("CURRENT_POSITION... "); + fflush(stdout); + + if(*current_block != *our_metadata_.blocks[current_position]) + return die_("metadata block mismatch"); + } + + printf("PASSED\n"); + + return true; +} + +::FLAC__StreamDecoderWriteStatus OurFileDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) +{ + (void)buffer; + + if( + (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) || + (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0) + ) { + printf("content... "); + fflush(stdout); + } + + return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; +} + +void OurFileDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata) +{ + /* don't bother checking if we've already hit an error */ + if(error_occurred_) + return; + + printf("%u... ", mc_our_block_number_); + fflush(stdout); + + if(!ignore_metadata_) { + if(mc_our_block_number_ >= our_metadata_.num_blocks) { + (void)die_("got more metadata blocks than expected"); + error_occurred_ = true; + } + else { + if(*our_metadata_.blocks[mc_our_block_number_] != metadata) { + (void)die_("metadata block mismatch"); + error_occurred_ = true; + } + } + } + + mc_our_block_number_++; +} + +void OurFileDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status) +{ + error_occurred_ = true; + printf("ERROR: got error callback, status = %s (%u)\n", FLAC__StreamDecoderErrorStatusString[status], (uint32_t)status); +} + +static bool generate_file_(bool include_extras, bool is_ogg) +{ + ::FLAC__StreamMetadata streaminfo, vorbiscomment, *cuesheet, picture, padding; + ::FLAC__StreamMetadata *metadata[4]; + uint32_t i = 0, n = 0; + + printf("generating %sFLAC file for test\n", is_ogg? "Ogg " : ""); + + while(our_metadata_.num_blocks > 0) + delete_from_our_metadata_(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); + + { + const uint32_t vendor_string_length = (uint32_t)strlen(FLAC__VENDOR_STRING); + vorbiscomment.is_last = false; + vorbiscomment.type = ::FLAC__METADATA_TYPE_VORBIS_COMMENT; + vorbiscomment.length = (4 + vendor_string_length) + 4; + 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+1); + memcpy(vorbiscomment.data.vorbis_comment.vendor_string.entry, FLAC__VENDOR_STRING, vendor_string_length+1); + vorbiscomment.data.vorbis_comment.num_comments = 0; + vorbiscomment.data.vorbis_comment.comments = 0; + } + + { + if (0 == (cuesheet = ::FLAC__metadata_object_new(::FLAC__METADATA_TYPE_CUESHEET))) + return die_("priming our metadata"); + cuesheet->is_last = false; + safe_strncpy(cuesheet->data.cue_sheet.media_catalog_number, "bogo-MCN", sizeof(cuesheet->data.cue_sheet.media_catalog_number)); + cuesheet->data.cue_sheet.lead_in = 123; + cuesheet->data.cue_sheet.is_cd = false; + if (!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, 0)) + return die_("priming our metadata"); + cuesheet->data.cue_sheet.tracks[0].number = 1; + if (!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, 0, 0)) + return die_("priming our metadata"); + } + + { + picture.is_last = false; + picture.type = ::FLAC__METADATA_TYPE_PICTURE; + picture.length = + ( + FLAC__STREAM_METADATA_PICTURE_TYPE_LEN + + FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* will add the length for the string later */ + FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* will add the length for the string later */ + FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN + + FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN + + FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN + + FLAC__STREAM_METADATA_PICTURE_COLORS_LEN + + FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN /* will add the length for the data later */ + ) / 8 + ; + picture.data.picture.type = ::FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER; + picture.data.picture.mime_type = strdup_or_die_("image/jpeg"); + picture.length += strlen(picture.data.picture.mime_type); + picture.data.picture.description = (FLAC__byte*)strdup_or_die_("desc"); + picture.length += strlen((const char *)picture.data.picture.description); + picture.data.picture.width = 300; + picture.data.picture.height = 300; + picture.data.picture.depth = 24; + picture.data.picture.colors = 0; + picture.data.picture.data = (FLAC__byte*)strdup_or_die_("SOMEJPEGDATA"); + picture.data.picture.data_length = strlen((const char *)picture.data.picture.data); + picture.length += picture.data.picture.data_length; + } + + padding.is_last = true; + padding.type = ::FLAC__METADATA_TYPE_PADDING; + padding.length = 1234; + + metadata[n++] = &vorbiscomment; + if(include_extras) { + metadata[n++] = cuesheet; + metadata[n++] = &picture; + } + metadata[n++] = &padding; + + FLAC::Metadata::StreamInfo s(&streaminfo); + FLAC::Metadata::VorbisComment v(&vorbiscomment); + FLAC::Metadata::CueSheet c(cuesheet, /*copy=*/false); + FLAC::Metadata::Picture pi(&picture); + FLAC::Metadata::Padding p(&padding); + if( + !insert_to_our_metadata_(&s, i++, /*copy=*/true) || + !insert_to_our_metadata_(&v, i++, /*copy=*/true) || + (include_extras && !insert_to_our_metadata_(&c, i++, /*copy=*/true)) || + (include_extras && !insert_to_our_metadata_(&pi, i++, /*copy=*/true)) || + !insert_to_our_metadata_(&p, i++, /*copy=*/true) + ) + return die_("priming our metadata"); + + if(!file_utils__generate_flacfile(is_ogg, flacfilename(is_ogg), 0, 512 * 1024, &streaminfo, metadata, n)) + return die_("creating the encoded file"); + + free(vorbiscomment.data.vorbis_comment.vendor_string.entry); + free(picture.data.picture.mime_type); + free(picture.data.picture.description); + free(picture.data.picture.data); + + return true; +} + +static bool test_file_(bool is_ogg, bool ignore_metadata) +{ + const char *filename = flacfilename(is_ogg); + OurFileDecoder decoder(ignore_metadata); + + mc_our_block_number_ = 0; + decoder.error_occurred_ = false; + + printf("\ttesting '%s'... ", filename); + fflush(stdout); + + if(!decoder.is_valid()) + return die_("couldn't allocate decoder instance"); + + decoder.set_md5_checking(true); + decoder.set_metadata_respond_all(); + if((is_ogg? decoder.init_ogg(filename) : decoder.init(filename)) != ::FLAC__STREAM_DECODER_INIT_STATUS_OK) { + (void)decoder.finish(); + return die_("initializing decoder\n"); + } + if(!decoder.process_until_end_of_stream()) { + (void)decoder.finish(); + return die_("decoding file\n"); + } + + (void)decoder.finish(); + + if(decoder.error_occurred_) + return false; + + if(mc_our_block_number_ != our_metadata_.num_blocks) + return die_("short metadata block count"); + + printf("PASSED\n"); + return true; +} + +static bool change_stats_(const char *filename, bool read_only) +{ + if(!grabbag__file_change_stats(filename, read_only)) + return die_("during grabbag__file_change_stats()"); + + return true; +} + +static bool remove_file_(const char *filename) +{ + while(our_metadata_.num_blocks > 0) + delete_from_our_metadata_(0); + + if(!grabbag__file_remove_file(filename)) + return die_("removing file"); + + return true; +} + +static bool test_level_0_() +{ + FLAC::Metadata::StreamInfo streaminfo; + + printf("\n\n++++++ testing level 0 interface\n"); + + if(!generate_file_(/*include_extras=*/true, /*is_ogg=*/false)) + return false; + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/true)) + return false; + + printf("testing FLAC::Metadata::get_streaminfo()... "); + + if(!FLAC::Metadata::get_streaminfo(flacfilename(/*is_ogg=*/false), streaminfo)) + return die_("during FLAC::Metadata::get_streaminfo()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(streaminfo.get_channels() != 1) + return die_("mismatch in streaminfo.get_channels()"); + if(streaminfo.get_bits_per_sample() != 8) + return die_("mismatch in streaminfo.get_bits_per_sample()"); + if(streaminfo.get_sample_rate() != 44100) + return die_("mismatch in streaminfo.get_sample_rate()"); + if(streaminfo.get_min_blocksize() != 576) + return die_("mismatch in streaminfo.get_min_blocksize()"); + if(streaminfo.get_max_blocksize() != 576) + return die_("mismatch in streaminfo.get_max_blocksize()"); + + printf("OK\n"); + + { + printf("testing FLAC::Metadata::get_tags(VorbisComment *&)... "); + + FLAC::Metadata::VorbisComment *tags = 0; + + if(!FLAC::Metadata::get_tags(flacfilename(/*is_ogg=*/false), tags)) + return die_("during FLAC::Metadata::get_tags()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(tags->get_num_comments() != 0) + return die_("mismatch in tags->get_num_comments()"); + + printf("OK\n"); + + delete tags; + } + + { + printf("testing FLAC::Metadata::get_tags(VorbisComment &)... "); + + FLAC::Metadata::VorbisComment tags; + + if(!FLAC::Metadata::get_tags(flacfilename(/*is_ogg=*/false), tags)) + return die_("during FLAC::Metadata::get_tags()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(tags.get_num_comments() != 0) + return die_("mismatch in tags.get_num_comments()"); + + printf("OK\n"); + } + + { + printf("testing FLAC::Metadata::get_cuesheet(CueSheet *&)... "); + + FLAC::Metadata::CueSheet *cuesheet = 0; + + if(!FLAC::Metadata::get_cuesheet(flacfilename(/*is_ogg=*/false), cuesheet)) + return die_("during FLAC::Metadata::get_cuesheet()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(cuesheet->get_lead_in() != 123) + return die_("mismatch in cuesheet->get_lead_in()"); + + printf("OK\n"); + + delete cuesheet; + } + + { + printf("testing FLAC::Metadata::get_cuesheet(CueSheet &)... "); + + FLAC::Metadata::CueSheet cuesheet; + + if(!FLAC::Metadata::get_cuesheet(flacfilename(/*is_ogg=*/false), cuesheet)) + return die_("during FLAC::Metadata::get_cuesheet()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(cuesheet.get_lead_in() != 123) + return die_("mismatch in cuesheet.get_lead_in()"); + + printf("OK\n"); + } + + { + printf("testing FLAC::Metadata::get_picture(Picture *&)... "); + + FLAC::Metadata::Picture *picture = 0; + + if(!FLAC::Metadata::get_picture(flacfilename(/*is_ogg=*/false), picture, /*type=*/(::FLAC__StreamMetadata_Picture_Type)(-1), /*mime_type=*/0, /*description=*/0, /*max_width=*/(uint32_t)(-1), /*max_height=*/(uint32_t)(-1), /*max_depth=*/(uint32_t)(-1), /*max_colors=*/(uint32_t)(-1))) + return die_("during FLAC::Metadata::get_picture()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(picture->get_type () != ::FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER) + return die_("mismatch in picture->get_type ()"); + + printf("OK\n"); + + delete picture; + } + + { + printf("testing FLAC::Metadata::get_picture(Picture &)... "); + + FLAC::Metadata::Picture picture; + + if(!FLAC::Metadata::get_picture(flacfilename(/*is_ogg=*/false), picture, /*type=*/(::FLAC__StreamMetadata_Picture_Type)(-1), /*mime_type=*/0, /*description=*/0, /*max_width=*/(uint32_t)(-1), /*max_height=*/(uint32_t)(-1), /*max_depth=*/(uint32_t)(-1), /*max_colors=*/(uint32_t)(-1))) + return die_("during FLAC::Metadata::get_picture()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(picture.get_type () != ::FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER) + return die_("mismatch in picture->get_type ()"); + + printf("OK\n"); + } + + if(!remove_file_(flacfilename(/*is_ogg=*/false))) + return false; + + return true; +} + +static bool test_level_1_() +{ + FLAC::Metadata::Prototype *block; + FLAC::Metadata::StreamInfo *streaminfo; + FLAC::Metadata::Padding *padding; + FLAC::Metadata::Application *app; + FLAC__byte data[1000]; + uint32_t our_current_position = 0; + + // initialize 'data' to avoid Valgrind errors + memset(data, 0, sizeof(data)); + + printf("\n\n++++++ testing level 1 interface\n"); + + /************************************************************/ + { + printf("simple iterator on read-only file\n"); + + if(!generate_file_(/*include_extras=*/false, /*is_ogg=*/false)) + return false; + + if(!change_stats_(flacfilename(/*is_ogg=*/false), /*read_only=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/true)) + return false; + + FLAC::Metadata::SimpleIterator iterator; + + if(!iterator.is_valid()) + return die_("iterator.is_valid() returned false"); + + if(!iterator.init(flacfilename(/*is_ogg=*/false), /*read_only=*/false, /*preserve_file_stats=*/false)) + return die_("iterator.init() returned false"); + + printf("is writable = %u\n", (uint32_t)iterator.is_writable()); + if(iterator.is_writable()) + return die_("iterator claims file is writable when tester thinks it should not be; are you running as root?\n"); + + printf("iterate forwards\n"); + + if(iterator.get_block_type() != ::FLAC__METADATA_TYPE_STREAMINFO) + return die_("expected STREAMINFO type from iterator.get_block_type()"); + if(0 == (block = iterator.get_block())) + return die_("getting block 0"); + if(block->get_type() != ::FLAC__METADATA_TYPE_STREAMINFO) + return die_("expected STREAMINFO type"); + if(block->get_is_last()) + return die_("expected is_last to be false"); + if(block->get_length() != FLAC__STREAM_METADATA_STREAMINFO_LENGTH) + return die_("bad STREAMINFO length"); + /* check to see if some basic data matches (c.f. generate_file_()) */ + streaminfo = dynamic_cast<FLAC::Metadata::StreamInfo *>(block); + FLAC__ASSERT(0 != streaminfo); + if(streaminfo->get_channels() != 1) + return die_("mismatch in channels"); + if(streaminfo->get_bits_per_sample() != 8) + return die_("mismatch in bits_per_sample"); + if(streaminfo->get_sample_rate() != 44100) + return die_("mismatch in sample_rate"); + if(streaminfo->get_min_blocksize() != 576) + return die_("mismatch in min_blocksize"); + if(streaminfo->get_max_blocksize() != 576) + return die_("mismatch in max_blocksize"); + // we will delete streaminfo a little later when we're really done with it... + + if(!iterator.next()) + return die_("forward iterator ended early"); + our_current_position++; + + if(!iterator.next()) + return die_("forward iterator ended early"); + our_current_position++; + + if(iterator.get_block_type() != ::FLAC__METADATA_TYPE_PADDING) + return die_("expected PADDING type from iterator.get_block_type()"); + if(0 == (block = iterator.get_block())) + return die_("getting block 1"); + if(block->get_type() != ::FLAC__METADATA_TYPE_PADDING) + return die_("expected PADDING type"); + if(!block->get_is_last()) + return die_("expected is_last to be true"); + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(block->get_length() != 1234) + return die_("bad PADDING length"); + delete block; + + if(iterator.next()) + return die_("forward iterator returned true but should have returned false"); + + printf("iterate backwards\n"); + if(!iterator.prev()) + return die_("reverse iterator ended early"); + if(!iterator.prev()) + return die_("reverse iterator ended early"); + if(iterator.prev()) + return die_("reverse iterator returned true but should have returned false"); + + printf("testing iterator.set_block() on read-only file...\n"); + + if(!iterator.set_block(streaminfo, false)) + printf("PASSED. iterator.set_block() returned false like it should\n"); + else + return die_("iterator.set_block() returned true but shouldn't have"); + delete streaminfo; + } + + /************************************************************/ + { + printf("simple iterator on writable file\n"); + + if(!change_stats_(flacfilename(/*is_ogg=*/false), /*read-only=*/false)) + return false; + + printf("creating APPLICATION block\n"); + + if(0 == (app = new FLAC::Metadata::Application())) + return die_("new FLAC::Metadata::Application()"); + app->set_id((const uint8_t *)"duh"); + + printf("creating PADDING block\n"); + + if(0 == (padding = new FLAC::Metadata::Padding())) { + delete app; + return die_("new FLAC::Metadata::Padding()"); + } + padding->set_length(20); + + FLAC::Metadata::SimpleIterator iterator; + + if(!iterator.is_valid()) { + delete app; + delete padding; + return die_("iterator.is_valid() returned false"); + } + + if(!iterator.init(flacfilename(/*is_ogg=*/false), /*read_only=*/false, /*preserve_file_stats=*/false)) { + delete app; + delete padding; + return die_("iterator.init() returned false"); + } + our_current_position = 0; + + printf("is writable = %u\n", (uint32_t)iterator.is_writable()); + + printf("[S]VP\ttry to write over STREAMINFO block...\n"); + if(!iterator.set_block(app, false)) + printf("\titerator.set_block() returned false like it should\n"); + else { + delete app; + delete padding; + return die_("iterator.set_block() returned true but shouldn't have"); + } + + if(iterator.status() != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT) + return die_("iterator.status() should have been FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT"); + + printf("[S]VP\tnext\n"); + if(!iterator.next()) { + delete app; + delete padding; + return die_("iterator ended early\n"); + } + our_current_position++; + + printf("S[V]P\tnext\n"); + if(!iterator.next()) { + delete app; + delete padding; + return die_("iterator ended early\n"); + } + our_current_position++; + + printf("SV[P]\tinsert PADDING after, don't expand into padding\n"); + padding->set_length(25); + if(!iterator.insert_block_after(padding, false)) + return die_ss_("iterator.insert_block_after(padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + printf("SVP[P]\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SV[P]P\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]PP\tinsert PADDING after, don't expand into padding\n"); + padding->set_length(30); + if(!iterator.insert_block_after(padding, false)) + return die_ss_("iterator.insert_block_after(padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[P]PP\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]PPP\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("[S]VPPP\tdelete (STREAMINFO block), must fail\n"); + if(iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false) should have returned false", iterator); + + if(iterator.status() != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT) + return die_("iterator.status() should have been FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT"); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("[S]VPPP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]PPP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]PP\tdelete (middle block), replace with padding\n"); + if(!iterator.delete_block(true)) + return die_ss_("iterator.delete_block(true)", iterator); + our_current_position--; + + printf("S[V]PPP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]PP\tdelete (middle block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("S[V]PP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]P\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVP[P]\tdelete (last block), replace with padding\n"); + if(!iterator.delete_block(true)) + return die_ss_("iterator.delete_block(false)", iterator); + our_current_position--; + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[P]P\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVP[P]\tdelete (last block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[P]\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]P\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("[S]VP\tset STREAMINFO (change sample rate)\n"); + FLAC__ASSERT(our_current_position == 0); + block = iterator.get_block(); + streaminfo = dynamic_cast<FLAC::Metadata::StreamInfo *>(block); + FLAC__ASSERT(0 != streaminfo); + streaminfo->set_sample_rate(32000); + if(!replace_in_our_metadata_(block, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(block, false)) + return die_ss_("iterator.set_block(block, false)", iterator); + delete block; + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("[S]VP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]P\tinsert APPLICATION after, expand into padding of exceeding size\n"); + app->set_id((const uint8_t *)"euh"); /* twiddle the id so that our comparison doesn't miss transposition */ + if(!iterator.insert_block_after(app, true)) + return die_ss_("iterator.insert_block_after(app, true)", iterator); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return false; + add_to_padding_length_(our_current_position+1, -((int)(FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) + (int)app->get_length())); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]P\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVA[P]\tset APPLICATION, expand into padding of exceeding size\n"); + app->set_id((const uint8_t *)"fuh"); /* twiddle the id */ + if(!iterator.set_block(app, true)) + return die_ss_("iterator.set_block(app, true)", iterator); + if(!insert_to_our_metadata_(app, our_current_position, /*copy=*/true)) + return false; + add_to_padding_length_(our_current_position+1, -((int)(FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) + (int)app->get_length())); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVA[A]P\tset APPLICATION (grow), don't expand into padding\n"); + app->set_id((const uint8_t *)"guh"); /* twiddle the id */ + if(!app->set_data(data, sizeof(data), true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app, false)) + return die_ss_("iterator.set_block(app, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVA[A]P\tset APPLICATION (shrink), don't fill in with padding\n"); + app->set_id((const uint8_t *)"huh"); /* twiddle the id */ + if(!app->set_data(data, 12, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app, false)) + return die_ss_("iterator.set_block(app, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVA[A]P\tset APPLICATION (grow), expand into padding of exceeding size\n"); + app->set_id((const uint8_t *)"iuh"); /* twiddle the id */ + if(!app->set_data(data, sizeof(data), true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + add_to_padding_length_(our_current_position+1, -((int)sizeof(data) - 12)); + if(!iterator.set_block(app, true)) + return die_ss_("iterator.set_block(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVA[A]P\tset APPLICATION (shrink), fill in with padding\n"); + app->set_id((const uint8_t *)"juh"); /* twiddle the id */ + if(!app->set_data(data, 23, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!insert_to_our_metadata_(padding, our_current_position+1, /*copy=*/true)) + return die_("copying object"); + dynamic_cast<FLAC::Metadata::Padding *>(our_metadata_.blocks[our_current_position+1])->set_length(sizeof(data) - 23 - FLAC__STREAM_METADATA_HEADER_LENGTH); + if(!iterator.set_block(app, true)) + return die_ss_("iterator.set_block(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVA[A]PP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVAA[P]P\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVAAP[P]\tset PADDING (shrink), don't fill in with padding\n"); + padding->set_length(5); + if(!replace_in_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(padding, false)) + return die_ss_("iterator.set_block(padding, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVAAP[P]\tset APPLICATION (grow)\n"); + app->set_id((const uint8_t *)"kuh"); /* twiddle the id */ + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app, false)) + return die_ss_("iterator.set_block(app, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVAAP[A]\tset PADDING (equal)\n"); + padding->set_length(27); + if(!replace_in_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(padding, false)) + return die_ss_("iterator.set_block(padding, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVAAP[P]\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SVAA[P]P\tdelete (middle block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVA[A]P\tdelete (middle block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]P\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVA[P]\tinsert PADDING after\n"); + padding->set_length(5); + if(!iterator.insert_block_after(padding, false)) + return die_ss_("iterator.insert_block_after(padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVAP[P]\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SVA[P]P\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SV[A]PP\tset APPLICATION (grow), try to expand into padding which is too small\n"); + if(!app->set_data(data, 32, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app, true)) + return die_ss_("iterator.set_block(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]PP\tset APPLICATION (grow), try to expand into padding which is 'close' but still too small\n"); + if(!app->set_data(data, 60, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app, true)) + return die_ss_("iterator.set_block(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]PP\tset APPLICATION (grow), expand into padding which will leave 0-length pad\n"); + if(!app->set_data(data, 87, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + dynamic_cast<FLAC::Metadata::Padding *>(our_metadata_.blocks[our_current_position+1])->set_length(0); + if(!iterator.set_block(app, true)) + return die_ss_("iterator.set_block(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]PP\tset APPLICATION (grow), expand into padding which is exactly consumed\n"); + if(!app->set_data(data, 91, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + if(!iterator.set_block(app, true)) + return die_ss_("iterator.set_block(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]P\tset APPLICATION (grow), expand into padding which is exactly consumed\n"); + if(!app->set_data(data, 100, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + our_metadata_.blocks[our_current_position]->set_is_last(true); + if(!iterator.set_block(app, true)) + return die_ss_("iterator.set_block(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]\tset PADDING (equal size)\n"); + padding->set_length(app->get_length()); + if(!replace_in_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(padding, true)) + return die_ss_("iterator.set_block(padding, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[P]\tinsert PADDING after\n"); + if(!iterator.insert_block_after(padding, false)) + return die_ss_("iterator.insert_block_after(padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVP[P]\tinsert PADDING after\n"); + padding->set_length(5); + if(!iterator.insert_block_after(padding, false)) + return die_ss_("iterator.insert_block_after(padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SVPP[P]\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SVP[P]P\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SV[P]PP\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]PPP\tinsert APPLICATION after, try to expand into padding which is too small\n"); + if(!app->set_data(data, 101, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.insert_block_after(app, true)) + return die_ss_("iterator.insert_block_after(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]PPP\tdelete (middle block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("S[V]PPP\tinsert APPLICATION after, try to expand into padding which is 'close' but still too small\n"); + if(!app->set_data(data, 97, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.insert_block_after(app, true)) + return die_ss_("iterator.insert_block_after(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]PPP\tdelete (middle block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("S[V]PPP\tinsert APPLICATION after, expand into padding which is exactly consumed\n"); + if(!app->set_data(data, 100, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + if(!iterator.insert_block_after(app, true)) + return die_ss_("iterator.insert_block_after(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]PP\tdelete (middle block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("S[V]PP\tinsert APPLICATION after, expand into padding which will leave 0-length pad\n"); + if(!app->set_data(data, 96, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + dynamic_cast<FLAC::Metadata::Padding *>(our_metadata_.blocks[our_current_position+1])->set_length(0); + if(!iterator.insert_block_after(app, true)) + return die_ss_("iterator.insert_block_after(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]PP\tdelete (middle block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("S[V]PP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]P\tdelete (middle block), don't replace with padding\n"); + if(!iterator.delete_block(false)) + return die_ss_("iterator.delete_block(false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + + printf("S[V]P\tinsert APPLICATION after, expand into padding which is exactly consumed\n"); + if(!app->set_data(data, 1, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + if(!iterator.insert_block_after(app, true)) + return die_ss_("iterator.insert_block_after(app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false)) + return false; + } + + delete app; + delete padding; + + if(!remove_file_(flacfilename(/*is_ogg=*/false))) + return false; + + return true; +} + +static bool test_level_2_(bool filename_based, bool is_ogg) +{ + FLAC::Metadata::Prototype *block; + FLAC::Metadata::StreamInfo *streaminfo; + FLAC::Metadata::Application *app; + FLAC::Metadata::Padding *padding; + FLAC__byte data[2000]; + uint32_t our_current_position; + + // initialize 'data' to avoid Valgrind errors + memset(data, 0, sizeof(data)); + + printf("\n\n++++++ testing level 2 interface (%s-based, %s FLAC)\n", filename_based? "filename":"callback", is_ogg? "Ogg":"native"); + + printf("generate read-only file\n"); + + if(!generate_file_(/*include_extras=*/false, is_ogg)) + return false; + + if(!change_stats_(flacfilename(is_ogg), /*read_only=*/true)) + return false; + + printf("create chain\n"); + FLAC::Metadata::Chain chain; + if(!chain.is_valid()) + return die_("allocating memory for chain"); + + printf("read chain\n"); + + if(!read_chain_(chain, flacfilename(is_ogg), filename_based, is_ogg)) + return die_c_("reading chain", chain.status()); + + printf("[S]VP\ttest initial metadata\n"); + + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + if(is_ogg) + goto end; + + printf("switch file to read-write\n"); + + if(!change_stats_(flacfilename(is_ogg), /*read-only=*/false)) + return false; + + printf("create iterator\n"); + { + FLAC::Metadata::Iterator iterator; + if(!iterator.is_valid()) + return die_("allocating memory for iterator"); + + our_current_position = 0; + + iterator.init(chain); + + if(0 == (block = iterator.get_block())) + return die_("getting block from iterator"); + + FLAC__ASSERT(block->get_type() == FLAC__METADATA_TYPE_STREAMINFO); + + printf("[S]VP\tmodify STREAMINFO, write\n"); + + streaminfo = dynamic_cast<FLAC::Metadata::StreamInfo *>(block); + FLAC__ASSERT(0 != streaminfo); + streaminfo->set_sample_rate(32000); + if(!replace_in_our_metadata_(block, our_current_position, /*copy=*/true)) + return die_("copying object"); + delete block; + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/true, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(false, true)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("[S]VP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]P\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]\treplace PADDING with identical-size APPLICATION\n"); + if(0 == (block = iterator.get_block())) + return die_("getting block from iterator"); + if(0 == (app = new FLAC::Metadata::Application())) + return die_("new FLAC::Metadata::Application()"); + app->set_id((const uint8_t *)"duh"); + if(!app->set_data(data, block->get_length()-(FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), true)) + return die_("setting APPLICATION data"); + delete block; + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(false, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]\tshrink APPLICATION, don't use padding\n"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 26, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(false, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]\tgrow APPLICATION, don't use padding\n"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 28, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(false, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]\tgrow APPLICATION, use padding, but last block is not padding\n"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 36, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(false, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]\tshrink APPLICATION, use padding, last block is not padding, but delta is too small for new PADDING block\n"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 33, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(true, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]\tshrink APPLICATION, use padding, last block is not padding, delta is enough for new PADDING block\n"); + if(0 == (padding = new FLAC::Metadata::Padding())) + return die_("creating PADDING block"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 29, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + padding->set_length(0); + if(!insert_to_our_metadata_(padding, our_current_position+1, /*copy=*/false)) + return die_("internal error"); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(true, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]P\tshrink APPLICATION, use padding, last block is padding\n"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 16, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + dynamic_cast<FLAC::Metadata::Padding *>(our_metadata_.blocks[our_current_position+1])->set_length(13); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(true, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding, but delta is too small\n"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 50, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(true, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding of exceeding size\n"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 56, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + add_to_padding_length_(our_current_position+1, -(56 - 50)); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(true, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding of exact size\n"); + if(0 == (app = dynamic_cast<FLAC::Metadata::Application *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("copying object"); + if(!app->set_data(data, 67, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + if(!iterator.set_block(app)) + return die_c_("iterator.set_block(app)", chain.status()); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(true, false)", chain.status()); + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV[A]\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]A\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("[S]VA\tinsert PADDING before STREAMINFO (should fail)\n"); + if(0 == (padding = new FLAC::Metadata::Padding())) + return die_("creating PADDING block"); + padding->set_length(30); + if(!iterator.insert_block_before(padding)) + printf("\titerator.insert_block_before() returned false like it should\n"); + else + return die_("iterator.insert_block_before() should have returned false"); + + printf("[S]VA\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]A\tinsert PADDING after\n"); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!iterator.insert_block_after(padding)) + return die_("iterator.insert_block_after(padding)"); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + printf("SV[P]A\tinsert PADDING before\n"); + if(0 == (padding = dynamic_cast<FLAC::Metadata::Padding *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("creating PADDING block"); + padding->set_length(17); + if(!insert_to_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!iterator.insert_block_before(padding)) + return die_("iterator.insert_block_before(padding)"); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + printf("SV[P]PA\tinsert PADDING before\n"); + if(0 == (padding = dynamic_cast<FLAC::Metadata::Padding *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position])))) + return die_("creating PADDING block"); + padding->set_length(0); + if(!insert_to_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!iterator.insert_block_before(padding)) + return die_("iterator.insert_block_before(padding)"); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + printf("SV[P]PPA\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVP[P]PA\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVPP[P]A\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVPPP[A]\tinsert PADDING after\n"); + if(0 == (padding = dynamic_cast<FLAC::Metadata::Padding *>(FLAC::Metadata::clone(our_metadata_.blocks[2])))) + return die_("creating PADDING block"); + padding->set_length(57); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!iterator.insert_block_after(padding)) + return die_("iterator.insert_block_after(padding)"); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + printf("SVPPPA[P]\tinsert PADDING before\n"); + if(0 == (padding = dynamic_cast<FLAC::Metadata::Padding *>(FLAC::Metadata::clone(our_metadata_.blocks[2])))) + return die_("creating PADDING block"); + padding->set_length(99); + if(!insert_to_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!iterator.insert_block_before(padding)) + return die_("iterator.insert_block_before(padding)"); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + } + our_current_position = 0; + + printf("SVPPPAPP\tmerge padding\n"); + chain.merge_padding(); + add_to_padding_length_(2, FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[3]->get_length()); + add_to_padding_length_(2, FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[4]->get_length()); + add_to_padding_length_(6, FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[7]->get_length()); + delete_from_our_metadata_(7); + delete_from_our_metadata_(4); + delete_from_our_metadata_(3); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(true, false)", chain.status()); + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SVPAP\tsort padding\n"); + chain.sort_padding(); + add_to_padding_length_(4, FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[2]->get_length()); + delete_from_our_metadata_(2); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(true, false)", chain.status()); + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("create iterator\n"); + { + FLAC::Metadata::Iterator iterator; + if(!iterator.is_valid()) + return die_("allocating memory for iterator"); + + our_current_position = 0; + + iterator.init(chain); + + printf("[S]VAP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]AP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[A]P\tdelete middle block, replace with padding\n"); + if(0 == (padding = new FLAC::Metadata::Padding())) + return die_("creating PADDING block"); + padding->set_length(71); + if(!replace_in_our_metadata_(padding, our_current_position--, /*copy=*/false)) + return die_("copying object"); + if(!iterator.delete_block(/*replace_with_padding=*/true)) + return die_c_("iterator.delete_block(true)", chain.status()); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + printf("S[V]PP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]P\tdelete middle block, don't replace with padding\n"); + delete_from_our_metadata_(our_current_position--); + if(!iterator.delete_block(/*replace_with_padding=*/false)) + return die_c_("iterator.delete_block(false)", chain.status()); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + printf("S[V]P\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]\tdelete last block, replace with padding\n"); + if(0 == (padding = new FLAC::Metadata::Padding())) + return die_("creating PADDING block"); + padding->set_length(219); + if(!replace_in_our_metadata_(padding, our_current_position--, /*copy=*/false)) + return die_("copying object"); + if(!iterator.delete_block(/*replace_with_padding=*/true)) + return die_c_("iterator.delete_block(true)", chain.status()); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + printf("S[V]P\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]\tdelete last block, don't replace with padding\n"); + delete_from_our_metadata_(our_current_position--); + if(!iterator.delete_block(/*replace_with_padding=*/false)) + return die_c_("iterator.delete_block(false)", chain.status()); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + printf("S[V]\tprev\n"); + if(!iterator.prev()) + return die_("iterator ended early\n"); + our_current_position--; + + printf("[S]V\tdelete STREAMINFO block, should fail\n"); + if(iterator.delete_block(/*replace_with_padding=*/false)) + return die_("iterator.delete_block() on STREAMINFO should have failed but didn't"); + + block = iterator.get_block(); + if(!compare_chain_(chain, our_current_position, block)) + return false; + delete block; + + } // delete iterator + our_current_position = 0; + + printf("SV\tmerge padding\n"); + chain.merge_padding(); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(false, false)", chain.status()); + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + + printf("SV\tsort padding\n"); + chain.sort_padding(); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during chain.write(false, false)", chain.status()); + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, /*ignore_metadata=*/false)) + return false; + +end: + if(!remove_file_(flacfilename(is_ogg))) + return false; + + return true; +} + +static bool test_level_2_misc_(bool is_ogg) +{ + ::FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (::FLAC__IOCallback_Read)fread; +#ifdef FLAC__VALGRIND_TESTING + callbacks.write = chain_write_cb_; +#else + callbacks.write = (::FLAC__IOCallback_Write)fwrite; +#endif + callbacks.seek = chain_seek_cb_; + callbacks.tell = chain_tell_cb_; + callbacks.eof = chain_eof_cb_; + + printf("\n\n++++++ testing level 2 interface (mismatched read/write protections)\n"); + + printf("generate file\n"); + + if(!generate_file_(/*include_extras=*/false, is_ogg)) + return false; + + printf("create chain\n"); + FLAC::Metadata::Chain chain; + if(!chain.is_valid()) + return die_("allocating chain"); + + printf("read chain (filename-based)\n"); + + if(!chain.read(flacfilename(is_ogg))) + return die_c_("reading chain", chain.status()); + + printf("write chain with wrong method Chain::write(with callbacks)\n"); + { + if(chain.write(/*use_padding=*/false, 0, callbacks)) + return die_c_("mismatched write should have failed", chain.status()); + if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", chain.status()); + printf(" OK: Chain::write(with callbacks) returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (filename-based)\n"); + + if(!chain.read(flacfilename(is_ogg))) + return die_c_("reading chain", chain.status()); + + printf("write chain with wrong method Chain::write(with callbacks and tempfile)\n"); + { + if(chain.write(/*use_padding=*/false, 0, callbacks, 0, callbacks)) + return die_c_("mismatched write should have failed", chain.status()); + if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", chain.status()); + printf(" OK: Chain::write(with callbacks and tempfile) returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == file) + return die_("opening file"); + if(!chain.read((::FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", chain.status()); + } + fclose(file); + } + + printf("write chain with wrong method write()\n"); + { + if(chain.write(/*use_padding=*/false, /*preserve_file_stats=*/false)) + return die_c_("mismatched write should have failed", chain.status()); + if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", chain.status()); + printf(" OK: write() returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == file) + return die_("opening file"); + if(!chain.read((::FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", chain.status()); + } + fclose(file); + } + + printf("testing Chain::check_if_tempfile_needed()... "); + + if(!chain.check_if_tempfile_needed(/*use_padding=*/false)) + printf("OK: Chain::check_if_tempfile_needed() returned false like it should\n"); + else + return die_("Chain::check_if_tempfile_needed() returned true but shouldn't have"); + + printf("write chain with wrong method Chain::write(with callbacks and tempfile)\n"); + { + if(chain.write(/*use_padding=*/false, 0, callbacks, 0, callbacks)) + return die_c_("mismatched write should have failed", chain.status()); + if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL", chain.status()); + printf(" OK: Chain::write(with callbacks and tempfile) returned false,FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == file) + return die_("opening file"); + if(!chain.read((::FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", chain.status()); + } + fclose(file); + } + + printf("create iterator\n"); + { + FLAC::Metadata::Iterator iterator; + if(!iterator.is_valid()) + return die_("allocating memory for iterator"); + + iterator.init(chain); + + printf("[S]VP\tnext\n"); + if(!iterator.next()) + return die_("iterator ended early\n"); + + printf("S[V]P\tdelete VORBIS_COMMENT, write\n"); + if(!iterator.delete_block(/*replace_with_padding=*/false)) + return die_c_("block delete failed\n", chain.status()); + + printf("testing Chain::check_if_tempfile_needed()... "); + + if(chain.check_if_tempfile_needed(/*use_padding=*/false)) + printf("OK: Chain::check_if_tempfile_needed() returned true like it should\n"); + else + return die_("Chain::check_if_tempfile_needed() returned false but shouldn't have"); + + printf("write chain with wrong method Chain::write(with callbacks)\n"); + { + if(chain.write(/*use_padding=*/false, 0, callbacks)) + return die_c_("mismatched write should have failed", chain.status()); + if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL", chain.status()); + printf(" OK: Chain::write(with callbacks) returned false,FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL like it should\n"); + } + + } // delete iterator + + if(!remove_file_(flacfilename(is_ogg))) + return false; + + return true; +} + +bool test_metadata_file_manipulation() +{ + printf("\n+++ libFLAC++ unit test: metadata manipulation\n\n"); + + our_metadata_.num_blocks = 0; + + if(!test_level_0_()) + return false; + + if(!test_level_1_()) + return false; + + if(!test_level_2_(/*filename_based=*/true, /*is_ogg=*/false)) /* filename-based */ + return false; + if(!test_level_2_(/*filename_based=*/false, /*is_ogg=*/false)) /* callback-based */ + return false; + if(!test_level_2_misc_(/*is_ogg=*/false)) + return false; + + if(FLAC_API_SUPPORTS_OGG_FLAC) { + if(!test_level_2_(/*filename_based=*/true, /*is_ogg=*/true)) /* filename-based */ + return false; + if(!test_level_2_(/*filename_based=*/false, /*is_ogg=*/true)) /* callback-based */ + return false; +#if 0 + /* when ogg flac write is supported, will have to add this: */ + if(!test_level_2_misc_(/*is_ogg=*/true)) + return false; +#endif + } + + return true; +} diff --git a/src/test_libFLAC++/metadata_object.cpp b/src/test_libFLAC++/metadata_object.cpp new file mode 100644 index 0000000..ab4cfbf --- /dev/null +++ b/src/test_libFLAC++/metadata_object.cpp @@ -0,0 +1,2099 @@ +/* test_libFLAC++ - Unit tester for libFLAC++ + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <stdio.h> +#include <stdlib.h> /* for malloc() */ +#include <string.h> /* for memcmp() */ +#include "FLAC/assert.h" +#include "FLAC++/metadata.h" +#include "share/safe_str.h" + +static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application_, vorbiscomment_, cuesheet_, picture_; + +static bool die_(const char *msg) +{ + printf("FAILED, %s\n", 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", (uint32_t)size); + exit(1); + } + return x; +} + +static char *strdup_or_die_(const char *s) +{ + char *x = strdup(s); + if(0 == x) { + fprintf(stderr, "ERROR: out of memory copying string \"%s\"\n", s); + exit(1); + } + return x; +} + +static bool index_is_equal_(const ::FLAC__StreamMetadata_CueSheet_Index &indx, const ::FLAC__StreamMetadata_CueSheet_Index &indxcopy) +{ + if(indxcopy.offset != indx.offset) + return false; + if(indxcopy.number != indx.number) + return false; + return true; +} + +static bool track_is_equal_(const ::FLAC__StreamMetadata_CueSheet_Track *track, const ::FLAC__StreamMetadata_CueSheet_Track *trackcopy) +{ + uint32_t i; + + if(trackcopy->offset != track->offset) + return false; + if(trackcopy->number != track->number) + return false; + if(0 != strcmp(trackcopy->isrc, track->isrc)) + return false; + if(trackcopy->type != track->type) + return false; + if(trackcopy->pre_emphasis != track->pre_emphasis) + return false; + if(trackcopy->num_indices != track->num_indices) + return false; + if(0 == track->indices || 0 == trackcopy->indices) { + if(track->indices != trackcopy->indices) + return false; + } + else { + for(i = 0; i < track->num_indices; i++) { + if(!index_is_equal_(trackcopy->indices[i], track->indices[i])) + return false; + } + } + return true; +} + +static void init_metadata_blocks_() +{ + 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; + + application_.is_last = false; + application_.type = ::FLAC__METADATA_TYPE_APPLICATION; + application_.length = 8; + memcpy(application_.data.application.id, "\xfe\xdc\xba\x98", 4); + application_.data.application.data = (FLAC__byte*)malloc_or_die_(4); + memcpy(application_.data.application.data, "\xf0\xe1\xd2\xc3", 4); + + vorbiscomment_.is_last = false; + vorbiscomment_.type = ::FLAC__METADATA_TYPE_VORBIS_COMMENT; + vorbiscomment_.length = (4 + 5) + 4 + (4 + 12) + (4 + 12); + vorbiscomment_.data.vorbis_comment.vendor_string.length = 5; + vorbiscomment_.data.vorbis_comment.vendor_string.entry = (FLAC__byte*)malloc_or_die_(5+1); + memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, "name0", 5+1); + 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 = 12; + vorbiscomment_.data.vorbis_comment.comments[0].entry = (FLAC__byte*)malloc_or_die_(12+1); + memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "name2=value2", 12+1); + vorbiscomment_.data.vorbis_comment.comments[1].length = 12; + vorbiscomment_.data.vorbis_comment.comments[1].entry = (FLAC__byte*)malloc_or_die_(12+1); + memcpy(vorbiscomment_.data.vorbis_comment.comments[1].entry, "name3=value3", 12+1); + + cuesheet_.is_last = false; + cuesheet_.type = ::FLAC__METADATA_TYPE_CUESHEET; + cuesheet_.length = + /* cuesheet guts */ + ( + FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN + + FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN + + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN + + FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN + ) / 8 + + /* 2 tracks */ + 2 * ( + FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN + ) / 8 + + /* 3 index points */ + 3 * ( + FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN + + FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN + + FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN + ) / 8 + ; + memset(cuesheet_.data.cue_sheet.media_catalog_number, 0, sizeof(cuesheet_.data.cue_sheet.media_catalog_number)); + cuesheet_.data.cue_sheet.media_catalog_number[0] = 'j'; + cuesheet_.data.cue_sheet.media_catalog_number[1] = 'C'; + cuesheet_.data.cue_sheet.lead_in = 159; + cuesheet_.data.cue_sheet.is_cd = true; + cuesheet_.data.cue_sheet.num_tracks = 2; + cuesheet_.data.cue_sheet.tracks = (FLAC__StreamMetadata_CueSheet_Track*)malloc_or_die_(cuesheet_.data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track)); + cuesheet_.data.cue_sheet.tracks[0].offset = 1; + cuesheet_.data.cue_sheet.tracks[0].number = 1; + memcpy(cuesheet_.data.cue_sheet.tracks[0].isrc, "ACBDE1234567", sizeof(cuesheet_.data.cue_sheet.tracks[0].isrc)); + cuesheet_.data.cue_sheet.tracks[0].type = 0; + cuesheet_.data.cue_sheet.tracks[0].pre_emphasis = 1; + cuesheet_.data.cue_sheet.tracks[0].num_indices = 2; + cuesheet_.data.cue_sheet.tracks[0].indices = (FLAC__StreamMetadata_CueSheet_Index*)malloc_or_die_(cuesheet_.data.cue_sheet.tracks[0].num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index)); + cuesheet_.data.cue_sheet.tracks[0].indices[0].offset = 0; + cuesheet_.data.cue_sheet.tracks[0].indices[0].number = 0; + cuesheet_.data.cue_sheet.tracks[0].indices[1].offset = 1234567890; + cuesheet_.data.cue_sheet.tracks[0].indices[1].number = 1; + cuesheet_.data.cue_sheet.tracks[1].offset = 2345678901u; + cuesheet_.data.cue_sheet.tracks[1].number = 2; + memcpy(cuesheet_.data.cue_sheet.tracks[1].isrc, "ACBDE7654321", sizeof(cuesheet_.data.cue_sheet.tracks[1].isrc)); + cuesheet_.data.cue_sheet.tracks[1].type = 1; + cuesheet_.data.cue_sheet.tracks[1].pre_emphasis = 0; + cuesheet_.data.cue_sheet.tracks[1].num_indices = 1; + cuesheet_.data.cue_sheet.tracks[1].indices = (FLAC__StreamMetadata_CueSheet_Index*)malloc_or_die_(cuesheet_.data.cue_sheet.tracks[1].num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index)); + cuesheet_.data.cue_sheet.tracks[1].indices[0].offset = 0; + cuesheet_.data.cue_sheet.tracks[1].indices[0].number = 1; + + picture_.is_last = true; + picture_.type = FLAC__METADATA_TYPE_PICTURE; + picture_.length = + ( + FLAC__STREAM_METADATA_PICTURE_TYPE_LEN + + FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* will add the length for the string later */ + FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* will add the length for the string later */ + FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN + + FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN + + FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN + + FLAC__STREAM_METADATA_PICTURE_COLORS_LEN + + FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN /* will add the length for the data later */ + ) / 8 + ; + picture_.data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER; + picture_.data.picture.mime_type = strdup_or_die_("image/jpeg"); + picture_.length += strlen(picture_.data.picture.mime_type); + picture_.data.picture.description = (FLAC__byte*)strdup_or_die_("desc"); + picture_.length += strlen((const char *)picture_.data.picture.description); + picture_.data.picture.width = 300; + picture_.data.picture.height = 300; + picture_.data.picture.depth = 24; + picture_.data.picture.colors = 0; + picture_.data.picture.data = (FLAC__byte*)strdup_or_die_("SOMEJPEGDATA"); + picture_.data.picture.data_length = strlen((const char *)picture_.data.picture.data); + picture_.length += picture_.data.picture.data_length; +} + +static void free_metadata_blocks_() +{ + free(seektable_.data.seek_table.points); + free(application_.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[1].entry); + free(vorbiscomment_.data.vorbis_comment.comments); + free(cuesheet_.data.cue_sheet.tracks[0].indices); + free(cuesheet_.data.cue_sheet.tracks[1].indices); + free(cuesheet_.data.cue_sheet.tracks); + free(picture_.data.picture.mime_type); + free(picture_.data.picture.description); + free(picture_.data.picture.data); +} + +bool test_metadata_object_streaminfo() +{ + uint32_t expected_length; + + printf("testing class FLAC::Metadata::StreamInfo\n"); + + printf("testing StreamInfo::StreamInfo()... "); + FLAC::Metadata::StreamInfo block; + if(!block.is_valid()) + return die_("!block.is_valid()"); + expected_length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; + if(block.get_length() != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length()); + return false; + } + printf("OK\n"); + + printf("testing StreamInfo::StreamInfo(const StreamInfo &)... +\n"); + printf(" StreamInfo::operator!=(const StreamInfo &)... "); + { + FLAC::Metadata::StreamInfo blockcopy(block); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != block) + return die_("copy is not identical to original"); + printf("OK\n"); + + printf("testing StreamInfo::~StreamInfo()... "); + } + printf("OK\n"); + + printf("testing StreamInfo::StreamInfo(const ::FLAC__StreamMetadata &)... +\n"); + printf(" StreamInfo::operator!=(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::StreamInfo blockcopy(streaminfo_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != streaminfo_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::StreamInfo(const ::FLAC__StreamMetadata *)... +\n"); + printf(" StreamInfo::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::StreamInfo blockcopy(&streaminfo_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != streaminfo_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::StreamInfo(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" StreamInfo::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::StreamInfo blockcopy(&streaminfo_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != streaminfo_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::StreamInfo(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" StreamInfo::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&streaminfo_); + FLAC::Metadata::StreamInfo blockcopy(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != streaminfo_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::assign(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" StreamInfo::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::StreamInfo blockcopy; + blockcopy.assign(&streaminfo_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != streaminfo_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::assign(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" StreamInfo::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&streaminfo_); + FLAC::Metadata::StreamInfo blockcopy; + blockcopy.assign(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != streaminfo_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::operator=(const StreamInfo &)... +\n"); + printf(" StreamInfo::operator==(const StreamInfo &)... "); + { + FLAC::Metadata::StreamInfo blockcopy = block; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == block)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::operator=(const ::FLAC__StreamMetadata &)... +\n"); + printf(" StreamInfo::operator==(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::StreamInfo blockcopy = streaminfo_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == streaminfo_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::operator=(const ::FLAC__StreamMetadata *)... +\n"); + printf(" StreamInfo::operator==(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::StreamInfo blockcopy = &streaminfo_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == streaminfo_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing StreamInfo::set_min_blocksize()... "); + block.set_min_blocksize(streaminfo_.data.stream_info.min_blocksize); + printf("OK\n"); + + printf("testing StreamInfo::set_max_blocksize()... "); + block.set_max_blocksize(streaminfo_.data.stream_info.max_blocksize); + printf("OK\n"); + + printf("testing StreamInfo::set_min_framesize()... "); + block.set_min_framesize(streaminfo_.data.stream_info.min_framesize); + printf("OK\n"); + + printf("testing StreamInfo::set_max_framesize()... "); + block.set_max_framesize(streaminfo_.data.stream_info.max_framesize); + printf("OK\n"); + + printf("testing StreamInfo::set_sample_rate()... "); + block.set_sample_rate(streaminfo_.data.stream_info.sample_rate); + printf("OK\n"); + + printf("testing StreamInfo::set_channels()... "); + block.set_channels(streaminfo_.data.stream_info.channels); + printf("OK\n"); + + printf("testing StreamInfo::set_bits_per_sample()... "); + block.set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample); + printf("OK\n"); + + printf("testing StreamInfo::set_total_samples()... "); + block.set_total_samples(streaminfo_.data.stream_info.total_samples); + printf("OK\n"); + + printf("testing StreamInfo::set_md5sum()... "); + block.set_md5sum(streaminfo_.data.stream_info.md5sum); + printf("OK\n"); + + printf("testing StreamInfo::get_min_blocksize()... "); + if(block.get_min_blocksize() != streaminfo_.data.stream_info.min_blocksize) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing StreamInfo::get_max_blocksize()... "); + if(block.get_max_blocksize() != streaminfo_.data.stream_info.max_blocksize) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing StreamInfo::get_min_framesize()... "); + if(block.get_min_framesize() != streaminfo_.data.stream_info.min_framesize) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing StreamInfo::get_max_framesize()... "); + if(block.get_max_framesize() != streaminfo_.data.stream_info.max_framesize) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing StreamInfo::get_sample_rate()... "); + if(block.get_sample_rate() != streaminfo_.data.stream_info.sample_rate) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing StreamInfo::get_channels()... "); + if(block.get_channels() != streaminfo_.data.stream_info.channels) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing StreamInfo::get_bits_per_sample()... "); + if(block.get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing StreamInfo::get_total_samples()... "); + if(block.get_total_samples() != streaminfo_.data.stream_info.total_samples) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing StreamInfo::get_md5sum()... "); + if(0 != memcmp(block.get_md5sum(), streaminfo_.data.stream_info.md5sum, 16)) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... "); + FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block); + if(0 == clone_) + return die_("returned NULL"); + if(0 == dynamic_cast<FLAC::Metadata::StreamInfo *>(clone_)) + return die_("downcast is NULL"); + if(*dynamic_cast<FLAC::Metadata::StreamInfo *>(clone_) != block) + return die_("clone is not identical"); + printf("OK\n"); + printf("testing StreamInfo::~StreamInfo()... "); + delete clone_; + printf("OK\n"); + + + printf("PASSED\n\n"); + return true; +} + +bool test_metadata_object_padding() +{ + uint32_t expected_length; + + printf("testing class FLAC::Metadata::Padding\n"); + + printf("testing Padding::Padding()... "); + FLAC::Metadata::Padding block; + if(!block.is_valid()) + return die_("!block.is_valid()"); + expected_length = 0; + if(block.get_length() != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length()); + return false; + } + printf("OK\n"); + + printf("testing Padding::Padding(const Padding &)... +\n"); + printf(" Padding::operator!=(const Padding &)... "); + { + FLAC::Metadata::Padding blockcopy(block); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != block) + return die_("copy is not identical to original"); + printf("OK\n"); + + printf("testing Padding::~Padding()... "); + } + printf("OK\n"); + + printf("testing Padding::Padding(const ::FLAC__StreamMetadata &)... +\n"); + printf(" Padding::operator!=(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::Padding blockcopy(padding_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != padding_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::Padding(const ::FLAC__StreamMetadata *)... +\n"); + printf(" Padding::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Padding blockcopy(&padding_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != padding_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::Padding(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" Padding::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Padding blockcopy(&padding_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != padding_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::Padding(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" Padding::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&padding_); + FLAC::Metadata::Padding blockcopy(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != padding_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::assign(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" Padding::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Padding blockcopy; + blockcopy.assign(&padding_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != padding_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::assign(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" Padding::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&padding_); + FLAC::Metadata::Padding blockcopy; + blockcopy.assign(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != padding_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::operator=(const Padding &)... +\n"); + printf(" Padding::operator==(const Padding &)... "); + { + FLAC::Metadata::Padding blockcopy = block; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == block)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::operator=(const ::FLAC__StreamMetadata &)... +\n"); + printf(" Padding::operator==(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::Padding blockcopy = padding_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == padding_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::operator=(const ::FLAC__StreamMetadata *)... +\n"); + printf(" Padding::operator==(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Padding blockcopy = &padding_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == padding_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Padding::set_length()... "); + block.set_length(padding_.length); + printf("OK\n"); + + printf("testing Prototype::get_length()... "); + if(block.get_length() != padding_.length) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... "); + FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block); + if(0 == clone_) + return die_("returned NULL"); + if(0 == dynamic_cast<FLAC::Metadata::Padding *>(clone_)) + return die_("downcast is NULL"); + if(*dynamic_cast<FLAC::Metadata::Padding *>(clone_) != block) + return die_("clone is not identical"); + printf("OK\n"); + printf("testing Padding::~Padding()... "); + delete clone_; + printf("OK\n"); + + + printf("PASSED\n\n"); + return true; +} + +bool test_metadata_object_application() +{ + uint32_t expected_length; + + printf("testing class FLAC::Metadata::Application\n"); + + printf("testing Application::Application()... "); + FLAC::Metadata::Application block; + if(!block.is_valid()) + return die_("!block.is_valid()"); + expected_length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; + if(block.get_length() != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length()); + return false; + } + printf("OK\n"); + + printf("testing Application::Application(const Application &)... +\n"); + printf(" Application::operator!=(const Application &)... "); + { + FLAC::Metadata::Application blockcopy(block); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != block) + return die_("copy is not identical to original"); + printf("OK\n"); + + printf("testing Application::~Application()... "); + } + printf("OK\n"); + + printf("testing Application::Application(const ::FLAC__StreamMetadata &)... +\n"); + printf(" Application::operator!=(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::Application blockcopy(application_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != application_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::Application(const ::FLAC__StreamMetadata *)... +\n"); + printf(" Application::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Application blockcopy(&application_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != application_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::Application(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" Application::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Application blockcopy(&application_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != application_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::Application(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" Application::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&application_); + FLAC::Metadata::Application blockcopy(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != application_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::assign(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" Application::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Application blockcopy; + blockcopy.assign(&application_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != application_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::assign(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" Application::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&application_); + FLAC::Metadata::Application blockcopy; + blockcopy.assign(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != application_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::operator=(const Application &)... +\n"); + printf(" Application::operator==(const Application &)... "); + { + FLAC::Metadata::Application blockcopy = block; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == block)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::operator=(const ::FLAC__StreamMetadata &)... +\n"); + printf(" Application::operator==(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::Application blockcopy = application_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == application_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::operator=(const ::FLAC__StreamMetadata *)... +\n"); + printf(" Application::operator==(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Application blockcopy = &application_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == application_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Application::set_id()... "); + block.set_id(application_.data.application.id); + printf("OK\n"); + + printf("testing Application::set_data()... "); + block.set_data(application_.data.application.data, application_.length - sizeof(application_.data.application.id), /*copy=*/true); + printf("OK\n"); + + printf("testing Application::get_id()... "); + if(0 != memcmp(block.get_id(), application_.data.application.id, sizeof(application_.data.application.id))) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing Application::get_data()... "); + if(0 != memcmp(block.get_data(), application_.data.application.data, application_.length - sizeof(application_.data.application.id))) + return die_("value mismatch, doesn't match previously set value"); + printf("OK\n"); + + printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... "); + FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block); + if(0 == clone_) + return die_("returned NULL"); + if(0 == dynamic_cast<FLAC::Metadata::Application *>(clone_)) + return die_("downcast is NULL"); + if(*dynamic_cast<FLAC::Metadata::Application *>(clone_) != block) + return die_("clone is not identical"); + printf("OK\n"); + printf("testing Application::~Application()... "); + delete clone_; + printf("OK\n"); + + + printf("PASSED\n\n"); + return true; +} + +bool test_metadata_object_seektable() +{ + uint32_t expected_length; + + printf("testing class FLAC::Metadata::SeekTable\n"); + + printf("testing SeekTable::SeekTable()... "); + FLAC::Metadata::SeekTable block; + if(!block.is_valid()) + return die_("!block.is_valid()"); + expected_length = 0; + if(block.get_length() != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length()); + return false; + } + printf("OK\n"); + + printf("testing SeekTable::SeekTable(const SeekTable &)... +\n"); + printf(" SeekTable::operator!=(const SeekTable &)... "); + { + FLAC::Metadata::SeekTable blockcopy(block); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != block) + return die_("copy is not identical to original"); + printf("OK\n"); + + printf("testing SeekTable::~SeekTable()... "); + } + printf("OK\n"); + + printf("testing SeekTable::SeekTable(const ::FLAC__StreamMetadata &)... +\n"); + printf(" SeekTable::operator!=(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::SeekTable blockcopy(seektable_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != seektable_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::SeekTable(const ::FLAC__StreamMetadata *)... +\n"); + printf(" SeekTable::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::SeekTable blockcopy(&seektable_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != seektable_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::SeekTable(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" SeekTable::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::SeekTable blockcopy(&seektable_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != seektable_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::SeekTable(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" SeekTable::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&seektable_); + FLAC::Metadata::SeekTable blockcopy(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != seektable_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::assign(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" SeekTable::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::SeekTable blockcopy; + blockcopy.assign(&seektable_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != seektable_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::assign(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" SeekTable::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&seektable_); + FLAC::Metadata::SeekTable blockcopy; + blockcopy.assign(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != seektable_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::operator=(const SeekTable &)... +\n"); + printf(" SeekTable::operator==(const SeekTable &)... "); + { + FLAC::Metadata::SeekTable blockcopy = block; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == block)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::operator=(const ::FLAC__StreamMetadata &)... +\n"); + printf(" SeekTable::operator==(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::SeekTable blockcopy = seektable_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == seektable_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::operator=(const ::FLAC__StreamMetadata *)... +\n"); + printf(" SeekTable::operator==(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::SeekTable blockcopy = &seektable_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == seektable_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing SeekTable::insert_point() x 3... "); + if(!block.insert_point(0, seektable_.data.seek_table.points[1])) + return die_("returned false"); + if(!block.insert_point(0, seektable_.data.seek_table.points[1])) + return die_("returned false"); + if(!block.insert_point(1, seektable_.data.seek_table.points[0])) + return die_("returned false"); + printf("OK\n"); + + printf("testing SeekTable::is_legal()... "); + if(block.is_legal()) + return die_("returned true"); + printf("OK\n"); + + printf("testing SeekTable::set_point()... "); + block.set_point(0, seektable_.data.seek_table.points[0]); + printf("OK\n"); + + printf("testing SeekTable::delete_point()... "); + if(!block.delete_point(0)) + return die_("returned false"); + printf("OK\n"); + + printf("testing SeekTable::is_legal()... "); + if(!block.is_legal()) + return die_("returned false"); + printf("OK\n"); + + printf("testing SeekTable::get_num_points()... "); + if(block.get_num_points() != seektable_.data.seek_table.num_points) + return die_("number mismatch"); + printf("OK\n"); + + printf("testing SeekTable::operator!=(const ::FLAC__StreamMetadata &)... "); + if(block != seektable_) + return die_("data mismatch"); + printf("OK\n"); + + printf("testing SeekTable::get_point()... "); + if( + block.get_point(1).sample_number != seektable_.data.seek_table.points[1].sample_number || + block.get_point(1).stream_offset != seektable_.data.seek_table.points[1].stream_offset || + block.get_point(1).frame_samples != seektable_.data.seek_table.points[1].frame_samples + ) + return die_("point mismatch"); + printf("OK\n"); + + printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... "); + FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block); + if(0 == clone_) + return die_("returned NULL"); + if(0 == dynamic_cast<FLAC::Metadata::SeekTable *>(clone_)) + return die_("downcast is NULL"); + if(*dynamic_cast<FLAC::Metadata::SeekTable *>(clone_) != block) + return die_("clone is not identical"); + printf("OK\n"); + printf("testing SeekTable::~SeekTable()... "); + delete clone_; + printf("OK\n"); + + + printf("PASSED\n\n"); + return true; +} + +bool test_metadata_object_vorbiscomment() +{ + uint32_t expected_length; + + printf("testing class FLAC::Metadata::VorbisComment::Entry\n"); + + printf("testing Entry::Entry()... "); + { + FLAC::Metadata::VorbisComment::Entry entry1; + if(!entry1.is_valid()) + return die_("!is_valid()"); + printf("OK\n"); + + printf("testing Entry::~Entry()... "); + } + printf("OK\n"); + + printf("testing Entry::Entry(const char *field, uint32_t field_length)... "); + FLAC::Metadata::VorbisComment::Entry entry2("name2=value2", strlen("name2=value2")); + if(!entry2.is_valid()) + return die_("!is_valid()"); + printf("OK\n"); + + { + printf("testing Entry::Entry(const char *field)... "); + FLAC::Metadata::VorbisComment::Entry entry2z("name2=value2"); + if(!entry2z.is_valid()) + return die_("!is_valid()"); + if(strcmp(entry2.get_field(), entry2z.get_field())) + return die_("bad value"); + printf("OK\n"); + } + + printf("testing Entry::Entry(const char *field_name, const char *field_value, uint32_t field_value_length)... "); + FLAC::Metadata::VorbisComment::Entry entry3("name3", "value3", strlen("value3")); + if(!entry3.is_valid()) + return die_("!is_valid()"); + printf("OK\n"); + + { + printf("testing Entry::Entry(const char *field_name, const char *field_value)... "); + FLAC::Metadata::VorbisComment::Entry entry3z("name3", "value3"); + if(!entry3z.is_valid()) + return die_("!is_valid()"); + if(strcmp(entry3.get_field(), entry3z.get_field())) + return die_("bad value"); + printf("OK\n"); + } + + printf("testing Entry::Entry(const Entry &entry)... "); + { + FLAC::Metadata::VorbisComment::Entry entry2copy(entry2); + if(!entry2copy.is_valid()) + return die_("!is_valid()"); + printf("OK\n"); + + printf("testing Entry::~Entry()... "); + } + printf("OK\n"); + + printf("testing Entry::operator=(const Entry &entry)... "); + FLAC::Metadata::VorbisComment::Entry entry1 = entry2; + if(!entry2.is_valid()) + return die_("!is_valid()"); + printf("OK\n"); + + printf("testing Entry::get_field_length()... "); + if(entry1.get_field_length() != strlen("name2=value2")) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Entry::get_field_name_length()... "); + if(entry1.get_field_name_length() != strlen("name2")) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Entry::get_field_value_length()... "); + if(entry1.get_field_value_length() != strlen("value2")) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Entry::get_entry()... "); + { + ::FLAC__StreamMetadata_VorbisComment_Entry entry = entry1.get_entry(); + if(entry.length != strlen("name2=value2")) + return die_("entry length mismatch"); + if(0 != memcmp(entry.entry, "name2=value2", entry.length)) + return die_("entry value mismatch"); + } + printf("OK\n"); + + printf("testing Entry::get_field()... "); + if(0 != memcmp(entry1.get_field(), "name2=value2", strlen("name2=value2"))) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Entry::get_field_name()... "); + if(0 != memcmp(entry1.get_field_name(), "name2", strlen("name2"))) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Entry::get_field_value()... "); + if(0 != memcmp(entry1.get_field_value(), "value2", strlen("value2"))) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Entry::set_field_name()... "); + if(!entry1.set_field_name("name1")) + return die_("returned false"); + if(0 != memcmp(entry1.get_field_name(), "name1", strlen("name1"))) + return die_("value mismatch"); + if(0 != memcmp(entry1.get_field(), "name1=value2", strlen("name1=value2"))) + return die_("entry mismatch"); + printf("OK\n"); + + printf("testing Entry::set_field_value(const char *field_value, uint32_t field_value_length)... "); + if(!entry1.set_field_value("value1", strlen("value1"))) + return die_("returned false"); + if(0 != memcmp(entry1.get_field_value(), "value1", strlen("value1"))) + return die_("value mismatch"); + if(0 != memcmp(entry1.get_field(), "name1=value1", strlen("name1=value1"))) + return die_("entry mismatch"); + printf("OK\n"); + + printf("testing Entry::set_field_value(const char *field_value)... "); + if(!entry1.set_field_value("value1")) + return die_("returned false"); + if(0 != memcmp(entry1.get_field_value(), "value1", strlen("value1"))) + return die_("value mismatch"); + if(0 != memcmp(entry1.get_field(), "name1=value1", strlen("name1=value1"))) + return die_("entry mismatch"); + printf("OK\n"); + + printf("testing Entry::set_field(const char *field, uint32_t field_length)... "); + if(!entry1.set_field("name0=value0", strlen("name0=value0"))) + return die_("returned false"); + if(0 != memcmp(entry1.get_field_name(), "name0", strlen("name0"))) + return die_("value mismatch"); + if(0 != memcmp(entry1.get_field_value(), "value0", strlen("value0"))) + return die_("value mismatch"); + if(0 != memcmp(entry1.get_field(), "name0=value0", strlen("name0=value0"))) + return die_("entry mismatch"); + printf("OK\n"); + + printf("testing Entry::set_field(const char *field)... "); + if(!entry1.set_field("name0=value0")) + return die_("returned false"); + if(0 != memcmp(entry1.get_field_name(), "name0", strlen("name0"))) + return die_("value mismatch"); + if(0 != memcmp(entry1.get_field_value(), "value0", strlen("value0"))) + return die_("value mismatch"); + if(0 != memcmp(entry1.get_field(), "name0=value0", strlen("name0=value0"))) + return die_("entry mismatch"); + printf("OK\n"); + + printf("PASSED\n\n"); + + + printf("testing class FLAC::Metadata::VorbisComment\n"); + + printf("testing VorbisComment::VorbisComment()... "); + FLAC::Metadata::VorbisComment block; + if(!block.is_valid()) + return die_("!block.is_valid()"); + expected_length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + strlen(::FLAC__VENDOR_STRING) + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN/8); + if(block.get_length() != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length()); + return false; + } + printf("OK\n"); + + printf("testing VorbisComment::VorbisComment(const VorbisComment &)... +\n"); + printf(" VorbisComment::operator!=(const VorbisComment &)... "); + { + FLAC::Metadata::VorbisComment blockcopy(block); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != block) + return die_("copy is not identical to original"); + printf("OK\n"); + + printf("testing VorbisComment::~VorbisComment()... "); + } + printf("OK\n"); + + printf("testing VorbisComment::VorbisComment(const ::FLAC__StreamMetadata &)... +\n"); + printf(" VorbisComment::operator!=(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::VorbisComment blockcopy(vorbiscomment_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != vorbiscomment_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::VorbisComment(const ::FLAC__StreamMetadata *)... +\n"); + printf(" VorbisComment::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::VorbisComment blockcopy(&vorbiscomment_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != vorbiscomment_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::VorbisComment(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" VorbisComment::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::VorbisComment blockcopy(&vorbiscomment_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != vorbiscomment_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::VorbisComment(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" VorbisComment::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&vorbiscomment_); + FLAC::Metadata::VorbisComment blockcopy(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != vorbiscomment_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::assign(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" VorbisComment::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::VorbisComment blockcopy; + blockcopy.assign(&vorbiscomment_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != vorbiscomment_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::assign(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" VorbisComment::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&vorbiscomment_); + FLAC::Metadata::VorbisComment blockcopy; + blockcopy.assign(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != vorbiscomment_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::operator=(const VorbisComment &)... +\n"); + printf(" VorbisComment::operator==(const VorbisComment &)... "); + { + FLAC::Metadata::VorbisComment blockcopy = block; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == block)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::operator=(const ::FLAC__StreamMetadata &)... +\n"); + printf(" VorbisComment::operator==(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::VorbisComment blockcopy = vorbiscomment_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == vorbiscomment_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::operator=(const ::FLAC__StreamMetadata *)... +\n"); + printf(" VorbisComment::operator==(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::VorbisComment blockcopy = &vorbiscomment_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == vorbiscomment_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing VorbisComment::get_num_comments()... "); + if(block.get_num_comments() != 0) + return die_("value mismatch, expected 0"); + printf("OK\n"); + + printf("testing VorbisComment::set_vendor_string()... "); + if(!block.set_vendor_string((const FLAC__byte *)"mame0")) + return die_("returned false"); + printf("OK\n"); + vorbiscomment_.data.vorbis_comment.vendor_string.entry[0] = 'm'; + + printf("testing VorbisComment::get_vendor_string()... "); + if(strlen((const char *)block.get_vendor_string()) != vorbiscomment_.data.vorbis_comment.vendor_string.length) + return die_("length mismatch"); + if(0 != memcmp(block.get_vendor_string(), vorbiscomment_.data.vorbis_comment.vendor_string.entry, vorbiscomment_.data.vorbis_comment.vendor_string.length)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing VorbisComment::append_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.append_comment(entry3)) + return die_("returned false"); + if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[1].length) + return die_("length mismatch"); + if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[1].entry, vorbiscomment_.data.vorbis_comment.comments[1].length)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing VorbisComment::append_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.append_comment(entry2)) + return die_("returned false"); + if(block.get_comment(1).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[0].length) + return die_("length mismatch"); + if(0 != memcmp(block.get_comment(1).get_field(), vorbiscomment_.data.vorbis_comment.comments[0].entry, vorbiscomment_.data.vorbis_comment.comments[0].length)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing VorbisComment::delete_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.delete_comment(0)) + return die_("returned false"); + if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[0].length) + return die_("length[0] mismatch"); + if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[0].entry, vorbiscomment_.data.vorbis_comment.comments[0].length)) + return die_("value[0] mismatch"); + printf("OK\n"); + + printf("testing VorbisComment::delete_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.delete_comment(0)) + return die_("returned false"); + if(block.get_num_comments() != 0) + return die_("block mismatch, expected num_comments = 0"); + printf("OK\n"); + + printf("testing VorbisComment::insert_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.insert_comment(0, entry3)) + return die_("returned false"); + if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[1].length) + return die_("length mismatch"); + if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[1].entry, vorbiscomment_.data.vorbis_comment.comments[1].length)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing VorbisComment::insert_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.insert_comment(0, entry3)) + return die_("returned false"); + if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[1].length) + return die_("length mismatch"); + if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[1].entry, vorbiscomment_.data.vorbis_comment.comments[1].length)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing VorbisComment::insert_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.insert_comment(1, entry2)) + return die_("returned false"); + if(block.get_comment(1).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[0].length) + return die_("length mismatch"); + if(0 != memcmp(block.get_comment(1).get_field(), vorbiscomment_.data.vorbis_comment.comments[0].entry, vorbiscomment_.data.vorbis_comment.comments[0].length)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing VorbisComment::set_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.set_comment(0, entry2)) + return die_("returned false"); + if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[0].length) + return die_("length mismatch"); + if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[0].entry, vorbiscomment_.data.vorbis_comment.comments[0].length)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing VorbisComment::delete_comment()... +\n"); + printf(" VorbisComment::get_comment()... "); + if(!block.delete_comment(0)) + return die_("returned false"); + if(block.get_comment(0).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[0].length) + return die_("length[0] mismatch"); + if(0 != memcmp(block.get_comment(0).get_field(), vorbiscomment_.data.vorbis_comment.comments[0].entry, vorbiscomment_.data.vorbis_comment.comments[0].length)) + return die_("value[0] mismatch"); + if(block.get_comment(1).get_field_length() != vorbiscomment_.data.vorbis_comment.comments[1].length) + return die_("length[1] mismatch"); + if(0 != memcmp(block.get_comment(1).get_field(), vorbiscomment_.data.vorbis_comment.comments[1].entry, vorbiscomment_.data.vorbis_comment.comments[1].length)) + return die_("value[0] mismatch"); + printf("OK\n"); + + printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... "); + FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block); + if(0 == clone_) + return die_("returned NULL"); + if(0 == dynamic_cast<FLAC::Metadata::VorbisComment *>(clone_)) + return die_("downcast is NULL"); + if(*dynamic_cast<FLAC::Metadata::VorbisComment *>(clone_) != block) + return die_("clone is not identical"); + printf("OK\n"); + printf("testing VorbisComment::~VorbisComment()... "); + delete clone_; + printf("OK\n"); + + + printf("PASSED\n\n"); + return true; +} + +bool test_metadata_object_cuesheet() +{ + uint32_t expected_length; + + printf("testing class FLAC::Metadata::CueSheet::Track\n"); + + printf("testing Track::Track()... "); + FLAC::Metadata::CueSheet::Track track0; + if(!track0.is_valid()) + return die_("!is_valid()"); + printf("OK\n"); + + { + printf("testing Track::get_track()... "); + const ::FLAC__StreamMetadata_CueSheet_Track *trackp = track0.get_track(); + if(0 == trackp) + return die_("returned pointer is NULL"); + printf("OK\n"); + + printf("testing Track::Track(const ::FLAC__StreamMetadata_CueSheet_Track*)... "); + FLAC::Metadata::CueSheet::Track track2(trackp); + if(!track2.is_valid()) + return die_("!is_valid()"); + if(!track_is_equal_(track2.get_track(), trackp)) + return die_("copy is not equal"); + printf("OK\n"); + + printf("testing Track::~Track()... "); + } + printf("OK\n"); + + printf("testing Track::Track(const Track &track)... "); + { + FLAC::Metadata::CueSheet::Track track0copy(track0); + if(!track0copy.is_valid()) + return die_("!is_valid()"); + if(!track_is_equal_(track0copy.get_track(), track0.get_track())) + return die_("copy is not equal"); + printf("OK\n"); + + printf("testing Track::~Track()... "); + } + printf("OK\n"); + + printf("testing Track::operator=(const Track &track)... "); + FLAC::Metadata::CueSheet::Track track1 = track0; + if(!track0.is_valid()) + return die_("!is_valid()"); + if(!track_is_equal_(track1.get_track(), track0.get_track())) + return die_("copy is not equal"); + printf("OK\n"); + + printf("testing Track::get_offset()... "); + if(track1.get_offset() != 0) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::get_number()... "); + if(track1.get_number() != 0) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::get_isrc()... "); + if(0 != memcmp(track1.get_isrc(), "\0\0\0\0\0\0\0\0\0\0\0\0\0", 13)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::get_type()... "); + if(track1.get_type() != 0) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::get_pre_emphasis()... "); + if(track1.get_pre_emphasis() != 0) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::get_num_indices()... "); + if(track1.get_num_indices() != 0) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::set_offset()... "); + track1.set_offset(588); + if(track1.get_offset() != 588) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::set_number()... "); + track1.set_number(1); + if(track1.get_number() != 1) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::set_isrc()... "); + track1.set_isrc("ABCDE1234567"); + if(0 != memcmp(track1.get_isrc(), "ABCDE1234567", 13)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::set_type()... "); + track1.set_type(1); + if(track1.get_type() != 1) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Track::set_pre_emphasis()... "); + track1.set_pre_emphasis(1); + if(track1.get_pre_emphasis() != 1) + return die_("value mismatch"); + printf("OK\n"); + + printf("PASSED\n\n"); + + printf("testing class FLAC::Metadata::CueSheet\n"); + + printf("testing CueSheet::CueSheet()... "); + FLAC::Metadata::CueSheet block; + if(!block.is_valid()) + return die_("!block.is_valid()"); + expected_length = ( + FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN + + FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN + + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN + + FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN + ) / 8; + if(block.get_length() != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length()); + return false; + } + printf("OK\n"); + + printf("testing CueSheet::CueSheet(const CueSheet &)... +\n"); + printf(" CueSheet::operator!=(const CueSheet &)... "); + { + FLAC::Metadata::CueSheet blockcopy(block); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != block) + return die_("copy is not identical to original"); + printf("OK\n"); + + printf("testing CueSheet::~CueSheet()... "); + } + printf("OK\n"); + + printf("testing CueSheet::CueSheet(const ::FLAC__StreamMetadata &)... +\n"); + printf(" CueSheet::operator!=(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::CueSheet blockcopy(cuesheet_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != cuesheet_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::CueSheet(const ::FLAC__StreamMetadata *)... +\n"); + printf(" CueSheet::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::CueSheet blockcopy(&cuesheet_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != cuesheet_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::CueSheet(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" CueSheet::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::CueSheet blockcopy(&cuesheet_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != cuesheet_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::CueSheet(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" CueSheet::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&cuesheet_); + FLAC::Metadata::CueSheet blockcopy(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != cuesheet_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::assign(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" CueSheet::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::CueSheet blockcopy; + blockcopy.assign(&cuesheet_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != cuesheet_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::assign(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" CueSheet::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&cuesheet_); + FLAC::Metadata::CueSheet blockcopy; + blockcopy.assign(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != cuesheet_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::operator=(const CueSheet &)... +\n"); + printf(" CueSheet::operator==(const CueSheet &)... "); + { + FLAC::Metadata::CueSheet blockcopy = block; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == block)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::operator=(const ::FLAC__StreamMetadata &)... +\n"); + printf(" CueSheet::operator==(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::CueSheet blockcopy = cuesheet_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == cuesheet_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::operator=(const ::FLAC__StreamMetadata *)... +\n"); + printf(" CueSheet::operator==(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::CueSheet blockcopy = &cuesheet_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == cuesheet_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing CueSheet::get_media_catalog_number()... "); + if(0 != strcmp(block.get_media_catalog_number(), "")) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing CueSheet::get_lead_in()... "); + if(block.get_lead_in() != 0) + return die_("value mismatch, expected 0"); + printf("OK\n"); + + printf("testing CueSheet::get_is_cd()... "); + if(block.get_is_cd()) + return die_("value mismatch, expected false"); + printf("OK\n"); + + printf("testing CueSheet::get_num_tracks()... "); + if(block.get_num_tracks() != 0) + return die_("value mismatch, expected 0"); + printf("OK\n"); + + printf("testing CueSheet::set_media_catalog_number()... "); + { + char mcn[129]; + memset(mcn, 0, sizeof(mcn)); + safe_strncpy(mcn, "1234567890123", sizeof(mcn)); + block.set_media_catalog_number(mcn); + if(0 != memcmp(block.get_media_catalog_number(), mcn, sizeof(mcn))) + return die_("value mismatch"); + } + printf("OK\n"); + + printf("testing CueSheet::set_lead_in()... "); + block.set_lead_in(588); + if(block.get_lead_in() != 588) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing CueSheet::set_is_cd()... "); + block.set_is_cd(true); + if(!block.get_is_cd()) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing CueSheet::insert_track()... +\n"); + printf(" CueSheet::get_track()... "); + if(!block.insert_track(0, track0)) + return die_("returned false"); + if(!track_is_equal_(block.get_track(0).get_track(), track0.get_track())) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing CueSheet::insert_track()... +\n"); + printf(" CueSheet::get_track()... "); + if(!block.insert_track(1, track1)) + return die_("returned false"); + if(!track_is_equal_(block.get_track(1).get_track(), track1.get_track())) + return die_("value mismatch"); + printf("OK\n"); + + ::FLAC__StreamMetadata_CueSheet_Index index0; + index0.offset = 588*4; + index0.number = 1; + + printf("testing CueSheet::insert_index(0)... +\n"); + printf(" CueSheet::get_track()... +\n"); + printf(" CueSheet::Track::get_index()... "); + if(!block.insert_index(0, 0, index0)) + return die_("returned false"); + if(!index_is_equal_(block.get_track(0).get_index(0), index0)) + return die_("value mismatch"); + printf("OK\n"); + + index0.offset = 588*5; + printf("testing CueSheet::Track::set_index()... "); + { + FLAC::Metadata::CueSheet::Track track_ = block.get_track(0); + track_.set_index(0, index0); + if(!index_is_equal_(track_.get_index(0), index0)) + return die_("value mismatch"); + } + printf("OK\n"); + + index0.offset = 588*6; + printf("testing CueSheet::set_index()... "); + block.set_index(0, 0, index0); + if(!index_is_equal_(block.get_track(0).get_index(0), index0)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing CueSheet::delete_index()... "); + if(!block.delete_index(0, 0)) + return die_("returned false"); + if(block.get_track(0).get_num_indices() != 0) + return die_("num_indices mismatch"); + printf("OK\n"); + + + printf("testing CueSheet::set_track()... +\n"); + printf(" CueSheet::get_track()... "); + if(!block.set_track(0, track1)) + return die_("returned false"); + if(!track_is_equal_(block.get_track(0).get_track(), track1.get_track())) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing CueSheet::delete_track()... "); + if(!block.delete_track(0)) + return die_("returned false"); + if(block.get_num_tracks() != 1) + return die_("num_tracks mismatch"); + printf("OK\n"); + + printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... "); + FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block); + if(0 == clone_) + return die_("returned NULL"); + if(0 == dynamic_cast<FLAC::Metadata::CueSheet *>(clone_)) + return die_("downcast is NULL"); + if(*dynamic_cast<FLAC::Metadata::CueSheet *>(clone_) != block) + return die_("clone is not identical"); + printf("OK\n"); + printf("testing CueSheet::~CueSheet()... "); + delete clone_; + printf("OK\n"); + + printf("PASSED\n\n"); + return true; +} + +bool test_metadata_object_picture() +{ + uint32_t expected_length; + + printf("testing class FLAC::Metadata::Picture\n"); + + printf("testing Picture::Picture()... "); + FLAC::Metadata::Picture block; + if(!block.is_valid()) + return die_("!block.is_valid()"); + expected_length = ( + FLAC__STREAM_METADATA_PICTURE_TYPE_LEN + + FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + + FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + + FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN + + FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN + + FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN + + FLAC__STREAM_METADATA_PICTURE_COLORS_LEN + + FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN + ) / 8; + if(block.get_length() != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block.get_length()); + return false; + } + printf("OK\n"); + + printf("testing Picture::Picture(const Picture &)... +\n"); + printf(" Picture::operator!=(const Picture &)... "); + { + FLAC::Metadata::Picture blockcopy(block); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != block) + return die_("copy is not identical to original"); + printf("OK\n"); + + printf("testing Picture::~Picture()... "); + } + printf("OK\n"); + + printf("testing Picture::Picture(const ::FLAC__StreamMetadata &)... +\n"); + printf(" Picture::operator!=(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::Picture blockcopy(picture_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != picture_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::Picture(const ::FLAC__StreamMetadata *)... +\n"); + printf(" Picture::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Picture blockcopy(&picture_); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != picture_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::Picture(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" Picture::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Picture blockcopy(&picture_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != picture_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::Picture(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" Picture::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&picture_); + FLAC::Metadata::Picture blockcopy(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != picture_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::assign(const ::FLAC__StreamMetadata *, copy=true)... +\n"); + printf(" Picture::operator!=(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Picture blockcopy; + blockcopy.assign(&picture_, /*copy=*/true); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != picture_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::assign(const ::FLAC__StreamMetadata *, copy=false)... +\n"); + printf(" Picture::operator!=(const ::FLAC__StreamMetadata *)... "); + { + ::FLAC__StreamMetadata *copy = ::FLAC__metadata_object_clone(&picture_); + FLAC::Metadata::Picture blockcopy; + blockcopy.assign(copy, /*copy=*/false); + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(blockcopy != picture_) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::operator=(const Picture &)... +\n"); + printf(" Picture::operator==(const Picture &)... "); + { + FLAC::Metadata::Picture blockcopy = block; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == block)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::operator=(const ::FLAC__StreamMetadata &)... +\n"); + printf(" Picture::operator==(const ::FLAC__StreamMetadata &)... "); + { + FLAC::Metadata::Picture blockcopy = picture_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == picture_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::operator=(const ::FLAC__StreamMetadata *)... +\n"); + printf(" Picture::operator==(const ::FLAC__StreamMetadata *)... "); + { + FLAC::Metadata::Picture blockcopy = &picture_; + if(!blockcopy.is_valid()) + return die_("!blockcopy.is_valid()"); + if(!(blockcopy == picture_)) + return die_("copy is not identical to original"); + printf("OK\n"); + } + + printf("testing Picture::get_type()... "); + if(block.get_type() != ::FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER) + return die_("value mismatch, expected ::FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER"); + printf("OK\n"); + + printf("testing Picture::set_type()... +\n"); + printf(" Picture::get_type()... "); + block.set_type(::FLAC__STREAM_METADATA_PICTURE_TYPE_MEDIA); + if(block.get_type() != ::FLAC__STREAM_METADATA_PICTURE_TYPE_MEDIA) + return die_("value mismatch, expected ::FLAC__STREAM_METADATA_PICTURE_TYPE_MEDIA"); + printf("OK\n"); + + printf("testing Picture::set_mime_type()... "); + if(!block.set_mime_type("qmage/jpeg")) + return die_("returned false"); + printf("OK\n"); + picture_.data.picture.mime_type[0] = 'q'; + + printf("testing Picture::get_mime_type()... "); + if(0 != strcmp(block.get_mime_type(), picture_.data.picture.mime_type)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Picture::set_description()... "); + if(!block.set_description((const FLAC__byte*)"qesc")) + return die_("returned false"); + printf("OK\n"); + picture_.data.picture.description[0] = 'q'; + + printf("testing Picture::get_description()... "); + if(0 != strcmp((const char *)block.get_description(), (const char *)picture_.data.picture.description)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing Picture::get_width()... "); + if(block.get_width() != 0) + return die_("value mismatch, expected 0"); + printf("OK\n"); + + printf("testing Picture::set_width()... +\n"); + printf(" Picture::get_width()... "); + block.set_width(400); + if(block.get_width() != 400) + return die_("value mismatch, expected 400"); + printf("OK\n"); + + printf("testing Picture::get_height()... "); + if(block.get_height() != 0) + return die_("value mismatch, expected 0"); + printf("OK\n"); + + printf("testing Picture::set_height()... +\n"); + printf(" Picture::get_height()... "); + block.set_height(200); + if(block.get_height() != 200) + return die_("value mismatch, expected 200"); + printf("OK\n"); + + printf("testing Picture::get_depth()... "); + if(block.get_depth() != 0) + return die_("value mismatch, expected 0"); + printf("OK\n"); + + printf("testing Picture::set_depth()... +\n"); + printf(" Picture::get_depth()... "); + block.set_depth(16); + if(block.get_depth() != 16) + return die_("value mismatch, expected 16"); + printf("OK\n"); + + printf("testing Picture::get_colors()... "); + if(block.get_colors() != 0) + return die_("value mismatch, expected 0"); + printf("OK\n"); + + printf("testing Picture::set_colors()... +\n"); + printf(" Picture::get_colors()... "); + block.set_colors(1u>16); + if(block.get_colors() != (1u>16)) + return die_("value mismatch, expected 2^16"); + printf("OK\n"); + + printf("testing Picture::get_data_length()... "); + if(block.get_data_length() != 0) + return die_("value mismatch, expected 0"); + printf("OK\n"); + + printf("testing Picture::set_data()... "); + if(!block.set_data((const FLAC__byte*)"qOMEJPEGDATA", strlen("qOMEJPEGDATA"))) + return die_("returned false"); + printf("OK\n"); + picture_.data.picture.data[0] = 'q'; + + printf("testing Picture::get_data()... "); + if(block.get_data_length() != picture_.data.picture.data_length) + return die_("length mismatch"); + if(0 != memcmp(block.get_data(), picture_.data.picture.data, picture_.data.picture.data_length)) + return die_("value mismatch"); + printf("OK\n"); + + printf("testing FLAC::Metadata::clone(const FLAC::Metadata::Prototype *)... "); + FLAC::Metadata::Prototype *clone_ = FLAC::Metadata::clone(&block); + if(0 == clone_) + return die_("returned NULL"); + if(0 == dynamic_cast<FLAC::Metadata::Picture *>(clone_)) + return die_("downcast is NULL"); + if(*dynamic_cast<FLAC::Metadata::Picture *>(clone_) != block) + return die_("clone is not identical"); + printf("OK\n"); + printf("testing Picture::~Picture()... "); + delete clone_; + printf("OK\n"); + + + printf("PASSED\n\n"); + return true; +} + +bool test_metadata_object() +{ + printf("\n+++ libFLAC++ unit test: metadata objects\n\n"); + + init_metadata_blocks_(); + + if(!test_metadata_object_streaminfo()) + return false; + + if(!test_metadata_object_padding()) + return false; + + if(!test_metadata_object_application()) + return false; + + if(!test_metadata_object_seektable()) + return false; + + if(!test_metadata_object_vorbiscomment()) + return false; + + if(!test_metadata_object_cuesheet()) + return false; + + if(!test_metadata_object_picture()) + return false; + + free_metadata_blocks_(); + + return true; +} diff --git a/src/test_libFLAC/CMakeLists.txt b/src/test_libFLAC/CMakeLists.txt new file mode 100644 index 0000000..36a5820 --- /dev/null +++ b/src/test_libFLAC/CMakeLists.txt @@ -0,0 +1,25 @@ +add_executable(test_libFLAC + bitreader.c + bitwriter.c + crc.c + decoders.c + encoders.c + endswap.c + format.c + main.c + metadata.c + metadata_manip.c + metadata_object.c + md5.c + "$<TARGET_PROPERTY:FLAC,SOURCE_DIR>/bitreader.c" + "$<TARGET_PROPERTY:FLAC,SOURCE_DIR>/bitwriter.c" + "$<TARGET_PROPERTY:FLAC,SOURCE_DIR>/crc.c" + "$<TARGET_PROPERTY:FLAC,SOURCE_DIR>/md5.c" + $<$<BOOL:${WIN32}>:../../include/share/win_utf8_io.h> + $<$<BOOL:${WIN32}>:../share/win_utf8_io/win_utf8_io.c>) + +target_compile_definitions(test_libFLAC PRIVATE + $<$<BOOL:${ENABLE_64_BIT_WORDS}>:ENABLE_64_BIT_WORDS>) +target_include_directories(test_libFLAC PRIVATE + "$<TARGET_PROPERTY:FLAC,SOURCE_DIR>/include") +target_link_libraries(test_libFLAC FLAC grabbag test_libs_common) diff --git a/src/test_libFLAC/Makefile.am b/src/test_libFLAC/Makefile.am new file mode 100644 index 0000000..c77f87e --- /dev/null +++ b/src/test_libFLAC/Makefile.am @@ -0,0 +1,63 @@ +# test_libFLAC - Unit tester for libFLAC +# Copyright (C) 2000-2009 Josh Coalson +# Copyright (C) 2011-2023 Xiph.Org Foundation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +EXTRA_DIST = \ + CMakeLists.txt + +check_PROGRAMS = test_libFLAC + +if OS_IS_WINDOWS +win_utf8_lib = $(top_builddir)/src/share/win_utf8_io/libwin_utf8_io.la +flac__no_dll = -DFLAC__NO_DLL +endif + +AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include -I$(top_srcdir)/src/libFLAC/include $(flac__no_dll) + +test_libFLAC_LDADD = \ + $(top_builddir)/src/share/grabbag/libgrabbag.la \ + $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ + $(top_builddir)/src/test_libs_common/libtest_libs_common.la \ + $(top_builddir)/src/libFLAC/libFLAC-static.la \ + $(win_utf8_lib) \ + @OGG_LIBS@ \ + -lm + +test_libFLAC_SOURCES = \ + bitreader.c \ + bitwriter.c \ + crc.c \ + decoders.c \ + encoders.c \ + endswap.c \ + format.c \ + main.c \ + metadata.c \ + metadata_manip.c \ + metadata_object.c \ + md5.c \ + bitreader.h \ + bitwriter.h \ + crc.h \ + decoders.h \ + encoders.h \ + endswap.h \ + format.h \ + metadata.h \ + md5.h + +CLEANFILES = test_libFLAC.exe diff --git a/src/test_libFLAC/Makefile.in b/src/test_libFLAC/Makefile.in new file mode 100644 index 0000000..ee0dcf8 --- /dev/null +++ b/src/test_libFLAC/Makefile.in @@ -0,0 +1,736 @@ +# Makefile.in generated by automake 1.16.5 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2021 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +# test_libFLAC - Unit tester for libFLAC +# Copyright (C) 2000-2009 Josh Coalson +# Copyright (C) 2011-2023 Xiph.Org Foundation +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +VPATH = @srcdir@ +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +check_PROGRAMS = test_libFLAC$(EXEEXT) +subdir = src/test_libFLAC +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/add_cflags.m4 \ + $(top_srcdir)/m4/add_cxxflags.m4 \ + $(top_srcdir)/m4/ax_add_fortify_source.m4 \ + $(top_srcdir)/m4/ax_check_compile_flag.m4 \ + $(top_srcdir)/m4/ax_check_enable_debug.m4 \ + $(top_srcdir)/m4/bswap.m4 $(top_srcdir)/m4/clang.m4 \ + $(top_srcdir)/m4/codeset.m4 $(top_srcdir)/m4/gcc_version.m4 \ + $(top_srcdir)/m4/iconv.m4 $(top_srcdir)/m4/lib-ld.m4 \ + $(top_srcdir)/m4/lib-link.m4 $(top_srcdir)/m4/lib-prefix.m4 \ + $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltoptions.m4 \ + $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ + $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/m4/ogg.m4 \ + $(top_srcdir)/m4/really_gcc.m4 \ + $(top_srcdir)/m4/stack_protect.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am_test_libFLAC_OBJECTS = bitreader.$(OBJEXT) bitwriter.$(OBJEXT) \ + crc.$(OBJEXT) decoders.$(OBJEXT) encoders.$(OBJEXT) \ + endswap.$(OBJEXT) format.$(OBJEXT) main.$(OBJEXT) \ + metadata.$(OBJEXT) metadata_manip.$(OBJEXT) \ + metadata_object.$(OBJEXT) md5.$(OBJEXT) +test_libFLAC_OBJECTS = $(am_test_libFLAC_OBJECTS) +test_libFLAC_DEPENDENCIES = \ + $(top_builddir)/src/share/grabbag/libgrabbag.la \ + $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ + $(top_builddir)/src/test_libs_common/libtest_libs_common.la \ + $(top_builddir)/src/libFLAC/libFLAC-static.la $(win_utf8_lib) +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__maybe_remake_depfiles = depfiles +am__depfiles_remade = ./$(DEPDIR)/bitreader.Po \ + ./$(DEPDIR)/bitwriter.Po ./$(DEPDIR)/crc.Po \ + ./$(DEPDIR)/decoders.Po ./$(DEPDIR)/encoders.Po \ + ./$(DEPDIR)/endswap.Po ./$(DEPDIR)/format.Po \ + ./$(DEPDIR)/main.Po ./$(DEPDIR)/md5.Po ./$(DEPDIR)/metadata.Po \ + ./$(DEPDIR)/metadata_manip.Po ./$(DEPDIR)/metadata_object.Po +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = $(test_libFLAC_SOURCES) +DIST_SOURCES = $(test_libFLAC_SOURCES) +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +AS = @AS@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCAS = @CCAS@ +CCASDEPMODE = @CCASDEPMODE@ +CCASFLAGS = @CCASFLAGS@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CSCOPE = @CSCOPE@ +CTAGS = @CTAGS@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DOXYGEN = @DOXYGEN@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +ENABLE_64_BIT_WORDS = @ENABLE_64_BIT_WORDS@ +ETAGS = @ETAGS@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +FLAC__HAS_OGG = @FLAC__HAS_OGG@ +FLAC__TEST_LEVEL = @FLAC__TEST_LEVEL@ +FLAC__TEST_WITH_VALGRIND = @FLAC__TEST_WITH_VALGRIND@ +GCC_MAJOR_VERSION = @GCC_MAJOR_VERSION@ +GCC_MINOR_VERSION = @GCC_MINOR_VERSION@ +GCC_VERSION = @GCC_VERSION@ +GIT_COMMIT_VERSION_HASH = @GIT_COMMIT_VERSION_HASH@ +GIT_FOUND = @GIT_FOUND@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBICONV = @LIBICONV@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIB_CLOCK_GETTIME = @LIB_CLOCK_GETTIME@ +LIB_FUZZING_ENGINE = @LIB_FUZZING_ENGINE@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBICONV = @LTLIBICONV@ +LTLIBOBJS = @LTLIBOBJS@ +LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MKDIR_P = @MKDIR_P@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OBJ_FORMAT = @OBJ_FORMAT@ +OGG_CFLAGS = @OGG_CFLAGS@ +OGG_LIBS = @OGG_LIBS@ +OGG_PACKAGE = @OGG_PACKAGE@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PANDOC = @PANDOC@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +RANLIB = @RANLIB@ +RC = @RC@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_CXX = @ac_ct_CXX@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +runstatedir = @runstatedir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +EXTRA_DIST = \ + CMakeLists.txt + +@OS_IS_WINDOWS_TRUE@win_utf8_lib = $(top_builddir)/src/share/win_utf8_io/libwin_utf8_io.la +@OS_IS_WINDOWS_TRUE@flac__no_dll = -DFLAC__NO_DLL +AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include -I$(top_srcdir)/src/libFLAC/include $(flac__no_dll) +test_libFLAC_LDADD = \ + $(top_builddir)/src/share/grabbag/libgrabbag.la \ + $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ + $(top_builddir)/src/test_libs_common/libtest_libs_common.la \ + $(top_builddir)/src/libFLAC/libFLAC-static.la \ + $(win_utf8_lib) \ + @OGG_LIBS@ \ + -lm + +test_libFLAC_SOURCES = \ + bitreader.c \ + bitwriter.c \ + crc.c \ + decoders.c \ + encoders.c \ + endswap.c \ + format.c \ + main.c \ + metadata.c \ + metadata_manip.c \ + metadata_object.c \ + md5.c \ + bitreader.h \ + bitwriter.h \ + crc.h \ + decoders.h \ + encoders.h \ + endswap.h \ + format.h \ + metadata.h \ + md5.h + +CLEANFILES = test_libFLAC.exe +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/test_libFLAC/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign src/test_libFLAC/Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-checkPROGRAMS: + @list='$(check_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list + +test_libFLAC$(EXEEXT): $(test_libFLAC_OBJECTS) $(test_libFLAC_DEPENDENCIES) $(EXTRA_test_libFLAC_DEPENDENCIES) + @rm -f test_libFLAC$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(test_libFLAC_OBJECTS) $(test_libFLAC_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bitreader.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bitwriter.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/crc.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/decoders.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/encoders.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/endswap.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/format.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/md5.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/metadata.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/metadata_manip.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/metadata_object.Po@am__quote@ # am--include-marker + +$(am__depfiles_remade): + @$(MKDIR_P) $(@D) + @echo '# dummy' >$@-t && $(am__mv) $@-t $@ + +am--depfiles: $(am__depfiles_remade) + +.c.o: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< + +.c.obj: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.obj$$||'`;\ +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ `$(CYGPATH_W) '$<'` &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.lo$$||'`;\ +@am__fastdepCC_TRUE@ $(LTCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags +distdir: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) distdir-am + +distdir-am: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) $(check_PROGRAMS) +check: check-am +all-am: Makefile +installdirs: +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-checkPROGRAMS clean-generic clean-libtool \ + mostlyclean-am + +distclean: distclean-am + -rm -f ./$(DEPDIR)/bitreader.Po + -rm -f ./$(DEPDIR)/bitwriter.Po + -rm -f ./$(DEPDIR)/crc.Po + -rm -f ./$(DEPDIR)/decoders.Po + -rm -f ./$(DEPDIR)/encoders.Po + -rm -f ./$(DEPDIR)/endswap.Po + -rm -f ./$(DEPDIR)/format.Po + -rm -f ./$(DEPDIR)/main.Po + -rm -f ./$(DEPDIR)/md5.Po + -rm -f ./$(DEPDIR)/metadata.Po + -rm -f ./$(DEPDIR)/metadata_manip.Po + -rm -f ./$(DEPDIR)/metadata_object.Po + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f ./$(DEPDIR)/bitreader.Po + -rm -f ./$(DEPDIR)/bitwriter.Po + -rm -f ./$(DEPDIR)/crc.Po + -rm -f ./$(DEPDIR)/decoders.Po + -rm -f ./$(DEPDIR)/encoders.Po + -rm -f ./$(DEPDIR)/endswap.Po + -rm -f ./$(DEPDIR)/format.Po + -rm -f ./$(DEPDIR)/main.Po + -rm -f ./$(DEPDIR)/md5.Po + -rm -f ./$(DEPDIR)/metadata.Po + -rm -f ./$(DEPDIR)/metadata_manip.Po + -rm -f ./$(DEPDIR)/metadata_object.Po + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: check-am install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am am--depfiles check check-am clean \ + clean-checkPROGRAMS clean-generic clean-libtool cscopelist-am \ + ctags ctags-am distclean distclean-compile distclean-generic \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-html install-html-am install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags tags-am uninstall uninstall-am + +.PRECIOUS: Makefile + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/src/test_libFLAC/bitreader.c b/src/test_libFLAC/bitreader.c new file mode 100644 index 0000000..d40bd1f --- /dev/null +++ b/src/test_libFLAC/bitreader.c @@ -0,0 +1,355 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2000-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "FLAC/assert.h" +#include "share/compat.h" +#include "private/bitreader.h" /* from the libFLAC private include area */ +#include "bitreader.h" +#include <stdio.h> +#include <string.h> /* for memcpy() */ + +/* + * WATCHOUT! Since FLAC__BitReader is a private structure, we use a copy of + * the definition here to get at the internals. Make sure this is kept up + * to date with what is in ../libFLAC/bitreader.c + */ +#if (ENABLE_64_BIT_WORDS == 0) + +typedef FLAC__uint32 brword; +#define FLAC__BYTES_PER_WORD 4 +#define FLAC__BITS_PER_WORD 32 + +#else + +typedef FLAC__uint64 brword; +#define FLAC__BYTES_PER_WORD 8 +#define FLAC__BITS_PER_WORD 64 + +#endif + +struct FLAC__BitReader { + /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */ + /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */ + brword *buffer; + uint32_t capacity; /* in words */ + uint32_t words; /* # of completed words in buffer */ + uint32_t bytes; /* # of bytes in incomplete word at buffer[words] */ + uint32_t consumed_words; /* #words ... */ + uint32_t consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */ + uint32_t read_crc16; /* the running frame CRC */ + uint32_t crc16_offset; /* the number of words in the current buffer that should not be CRC'd */ + uint32_t crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */ + FLAC__bool read_limit_set; /* whether reads are limited */ + uint32_t read_limit; /* the remaining size of what can be read */ + uint32_t last_seen_framesync; /* the location of the last seen framesync, if it is in the buffer, in bits from front of buffer */ + FLAC__BitReaderReadCallback read_callback; + void *client_data; +}; + +static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data); + +static void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out) +{ + uint32_t i, j; + if(br == 0) { + fprintf(out, "bitreader is NULL\n"); + } + else { + fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits); + + for(i = 0; i < br->words; i++) { + fprintf(out, "%08X: ", i); + for(j = 0; j < FLAC__BITS_PER_WORD; j++) + if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits)) + fprintf(out, "."); + else + fprintf(out, "%01d", br->buffer[i] & ((brword)1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0); + fprintf(out, "\n"); + } + if(br->bytes > 0) { + fprintf(out, "%08X: ", i); + for(j = 0; j < br->bytes*8; j++) + if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits)) + fprintf(out, "."); + else + fprintf(out, "%01d", br->buffer[i] & ((brword)1 << (br->bytes*8-j-1)) ? 1:0); + fprintf(out, "\n"); + } + } +} + +FLAC__bool test_bitreader(void) +{ + FLAC__BitReader *br; + FLAC__bool ok; + uint32_t i; + uint32_t words, bits; /* what we think br->consumed_words and br->consumed_bits should be */ + + FLAC__uint16 crc,expected_crcs[4] = { 0x5e4c, 0x7f6b, 0x2272, 0x42bf }; + FLAC__byte data[32]; + + FLAC__uint32 val_uint32; + FLAC__uint64 val_uint64; + + for (i = 0; i < 32; i++) + data[i] = i * 8 + 7; + + printf("\n+++ libFLAC unit test: bitreader\n\n"); + + /* + * test new -> delete + */ + printf("testing new... "); + br = FLAC__bitreader_new(); + if(0 == br) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing delete... "); + FLAC__bitreader_delete(br); + printf("OK\n"); + + /* + * test new -> init -> delete + */ + printf("testing new... "); + br = FLAC__bitreader_new(); + if(0 == br) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing init... "); + if(!FLAC__bitreader_init(br, read_callback, data)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing delete... "); + FLAC__bitreader_delete(br); + printf("OK\n"); + + /* + * test new -> init -> clear -> delete + */ + printf("testing new... "); + br = FLAC__bitreader_new(); + if(0 == br) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing init... "); + if(!FLAC__bitreader_init(br, read_callback, data)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing clear... "); + if(!FLAC__bitreader_clear(br)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing delete... "); + FLAC__bitreader_delete(br); + printf("OK\n"); + + /* + * test normal usage + */ + printf("testing new... "); + br = FLAC__bitreader_new(); + if(0 == br) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing init... "); + if(!FLAC__bitreader_init(br, read_callback, data)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing clear... "); + if(!FLAC__bitreader_clear(br)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + words = bits = 0; + + printf("capacity = %u\n", br->capacity); + + printf("testing raw reads... "); + ok = + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 10) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) && + FLAC__bitreader_read_raw_uint64(br, &val_uint64, 64) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 12) + ; + if(!ok) { + printf("FAILED\n"); + FLAC__bitreader_dump(br, stdout); + return false; + } + /* we read 152 bits (=19 bytes) from the bitreader */ + words = 152 / FLAC__BITS_PER_WORD; + bits = 152 - words*FLAC__BITS_PER_WORD; + + if(br->consumed_words != words) { + printf("FAILED word count %u != %u\n", br->consumed_words, words); + FLAC__bitreader_dump(br, stdout); + return false; + } + if(br->consumed_bits != bits) { + printf("FAILED bit count %u != %u\n", br->consumed_bits, bits); + FLAC__bitreader_dump(br, stdout); + return false; + } + crc = FLAC__bitreader_get_read_crc16(br); + if(crc != expected_crcs[0]) { + printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[0]); + FLAC__bitreader_dump(br, stdout); + return false; + } + printf("OK\n"); + FLAC__bitreader_dump(br, stdout); + + printf("testing CRC reset... "); + FLAC__bitreader_clear(br); + FLAC__bitreader_reset_read_crc16(br, 0xFFFF); + crc = FLAC__bitreader_get_read_crc16(br); + if(crc != 0xFFFF) { + printf("FAILED reported CRC 0x%04x does not match expected 0xFFFF\n", crc); + FLAC__bitreader_dump(br, stdout); + return false; + } + FLAC__bitreader_reset_read_crc16(br, 0); + crc = FLAC__bitreader_get_read_crc16(br); + if(crc != 0) { + printf("FAILED reported CRC 0x%04x does not match expected 0x0000\n", crc); + FLAC__bitreader_dump(br, stdout); + return false; + } + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 16); + FLAC__bitreader_reset_read_crc16(br, 0); + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32); + crc = FLAC__bitreader_get_read_crc16(br); + if(crc != expected_crcs[1]) { + printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[1]); + FLAC__bitreader_dump(br, stdout); + return false; + } + printf("OK\n"); + + printf("testing unaligned < 32 bit reads... "); + FLAC__bitreader_clear(br); + FLAC__bitreader_skip_bits_no_crc(br, 8); + FLAC__bitreader_reset_read_crc16(br, 0); + ok = + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) + ; + if(!ok) { + printf("FAILED\n"); + FLAC__bitreader_dump(br, stdout); + return false; + } + crc = FLAC__bitreader_get_read_crc16(br); + if(crc != expected_crcs[2]) { + printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[2]); + FLAC__bitreader_dump(br, stdout); + return false; + } + printf("OK\n"); + FLAC__bitreader_dump(br, stdout); + + printf("testing unaligned < 64 bit reads... "); + FLAC__bitreader_clear(br); + FLAC__bitreader_skip_bits_no_crc(br, 8); + FLAC__bitreader_reset_read_crc16(br, 0); + ok = + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) && + FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32) + ; + if(!ok) { + printf("FAILED\n"); + FLAC__bitreader_dump(br, stdout); + return false; + } + crc = FLAC__bitreader_get_read_crc16(br); + if(crc != expected_crcs[3]) { + printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[3]); + FLAC__bitreader_dump(br, stdout); + return false; + } + printf("OK\n"); + FLAC__bitreader_dump(br, stdout); + + printf("testing free... "); + FLAC__bitreader_free(br); + printf("OK\n"); + + printf("testing delete... "); + FLAC__bitreader_delete(br); + printf("OK\n"); + + printf("\nPASSED!\n"); + return true; +} + +/*----------------------------------------------------------------------------*/ + +static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data) +{ + if (*bytes > 32) + *bytes = 32; + + memcpy(buffer, data, *bytes); + + return true; +} diff --git a/src/test_libFLAC/bitreader.h b/src/test_libFLAC/bitreader.h new file mode 100644 index 0000000..f497acf --- /dev/null +++ b/src/test_libFLAC/bitreader.h @@ -0,0 +1,27 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2000-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_BITREADER_H +#define FLAC__TEST_LIBFLAC_BITREADER_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_bitreader(void); + +#endif diff --git a/src/test_libFLAC/bitwriter.c b/src/test_libFLAC/bitwriter.c new file mode 100644 index 0000000..0aafff2 --- /dev/null +++ b/src/test_libFLAC/bitwriter.c @@ -0,0 +1,688 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2000-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "FLAC/assert.h" +#include "share/compat.h" +#include "private/bitwriter.h" /* from the libFLAC private include area */ +#include "bitwriter.h" +#include <stdio.h> +#include <string.h> /* for memcmp() */ + +/* + * WATCHOUT! Since FLAC__BitWriter is a private structure, we use a copy of + * the definition here to get at the internals. Make sure this is kept up + * to date with what is in ../libFLAC/bitwriter.c + */ +#if (ENABLE_64_BIT_WORDS == 0) + +typedef FLAC__uint32 bwword; +#define FLAC__BYTES_PER_WORD 4 +#define FLAC__BITS_PER_WORD 32 +#define PRI_BWWORD "08x" + +#else + +typedef FLAC__uint64 bwword; +#define FLAC__BYTES_PER_WORD 8 +#define FLAC__BITS_PER_WORD 64 +#define PRI_BWWORD "016" PRIx64 + +#endif + +struct FLAC__BitWriter { + bwword *buffer; + bwword accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */ + uint32_t capacity; /* capacity of buffer in words */ + uint32_t words; /* # of complete words in buffer */ + uint32_t bits; /* # of used bits in accum */ +}; + +#define WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD) +#define TOTAL_BITS(bw) (WORDS_TO_BITS((bw)->words) + (bw)->bits) + +static void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out) +{ + uint32_t i, j; + if(bw == 0) { + fprintf(out, "bitwriter is NULL\n"); + } + else { + fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, TOTAL_BITS(bw)); + + for(i = 0; i < bw->words; i++) { + fprintf(out, "%08X: ", i); + for(j = 0; j < FLAC__BITS_PER_WORD; j++) + fprintf(out, "%01d", bw->buffer[i] & ((bwword)1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0); + fprintf(out, "\n"); + } + if(bw->bits > 0) { + fprintf(out, "%08X: ", i); + for(j = 0; j < bw->bits; j++) + fprintf(out, "%01d", bw->accum & ((bwword)1 << (bw->bits-j-1)) ? 1:0); + fprintf(out, "\n"); + } + } +} + +FLAC__bool test_bitwriter(void) +{ + FLAC__BitWriter *bw; + FLAC__bool ok; + uint32_t i, j; +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + static bwword test_pattern1[5] = { 0xaaf0aabe, 0xaaaaaaa8, 0x300aaaaa, 0xaaadeadb, 0x00eeface }; +#else + static bwword test_pattern1[5] = { 0xbeaaf0aa, 0xa8aaaaaa, 0xaaaa0a30, 0xdbeaadaa, 0x00eeface }; +#endif +#elif FLAC__BYTES_PER_WORD == 8 +#if WORDS_BIGENDIAN + static bwword test_pattern1[3] = { FLAC__U64L(0xaaf0aabeaaaaaaa8), FLAC__U64L(0x300aaaaaaaadeadb), FLAC__U64L(0x0000000000eeface) }; +#else + static bwword test_pattern1[3] = { FLAC__U64L(0xa8aaaaaabeaaf0aa), FLAC__U64L(0xdbeaadaaaaaa0a30), FLAC__U64L(0x0000000000eeface) }; +#endif +#else +#error FLAC__BYTES_PER_WORD is neither 4 nor 8 -- not implemented +#endif + uint32_t words, bits; /* what we think bw->words and bw->bits should be */ + + printf("\n+++ libFLAC unit test: bitwriter\n\n"); + + /* + * test new -> delete + */ + printf("testing new... "); + bw = FLAC__bitwriter_new(); + if(0 == bw) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing delete... "); + FLAC__bitwriter_delete(bw); + printf("OK\n"); + + /* + * test new -> init -> delete + */ + printf("testing new... "); + bw = FLAC__bitwriter_new(); + if(0 == bw) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing init... "); + if(!FLAC__bitwriter_init(bw)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing delete... "); + FLAC__bitwriter_delete(bw); + printf("OK\n"); + + /* + * test new -> init -> clear -> delete + */ + printf("testing new... "); + bw = FLAC__bitwriter_new(); + if(0 == bw) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing init... "); + if(!FLAC__bitwriter_init(bw)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing clear... "); + FLAC__bitwriter_clear(bw); + printf("OK\n"); + + printf("testing delete... "); + FLAC__bitwriter_delete(bw); + printf("OK\n"); + + /* + * test normal usage + */ + printf("testing new... "); + bw = FLAC__bitwriter_new(); + if(0 == bw) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing init... "); + if(!FLAC__bitwriter_init(bw)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing clear... "); + FLAC__bitwriter_clear(bw); + printf("OK\n"); + + words = bits = 0; + + printf("capacity = %u\n", bw->capacity); + + printf("testing zeroes, raw_uint32*... "); + ok = + FLAC__bitwriter_write_raw_uint32(bw, 0x1, 1) && + FLAC__bitwriter_write_raw_uint32(bw, 0x1, 2) && + FLAC__bitwriter_write_raw_uint32(bw, 0xa, 5) && + FLAC__bitwriter_write_raw_uint32(bw, 0xf0, 8) && + FLAC__bitwriter_write_raw_uint32(bw, 0x2aa, 10) && + FLAC__bitwriter_write_raw_uint32(bw, 0xf, 4) && + FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32) && + FLAC__bitwriter_write_zeroes(bw, 4) && + FLAC__bitwriter_write_raw_uint32(bw, 0x3, 2) && + FLAC__bitwriter_write_zeroes(bw, 8) && + FLAC__bitwriter_write_raw_uint64(bw, FLAC__U64L(0xaaaaaaaadeadbeef), 64) && + FLAC__bitwriter_write_raw_uint32(bw, 0xace, 12) + ; + if(!ok) { + printf("FAILED\n"); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + /* we wrote 152 bits (=19 bytes) to the bitwriter */ + words = 152 / FLAC__BITS_PER_WORD; + bits = 152 - words*FLAC__BITS_PER_WORD; + + if(bw->words != words) { + printf("FAILED word count %u != %u\n", bw->words, words); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + if(bw->bits != bits) { + printf("FAILED bit count %u != %u\n", bw->bits, bits); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) { + printf("FAILED pattern match (buffer)\n"); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + if((bw->accum & 0x00ffffff) != test_pattern1[words]) { + printf("FAILED pattern match (bw->accum=%" PRI_BWWORD " != %" PRI_BWWORD ")\n", bw->accum&0x00ffffff, test_pattern1[words]); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + printf("OK\n"); + FLAC__bitwriter_dump(bw, stdout); + + printf("testing raw_uint32 some more... "); + ok = FLAC__bitwriter_write_raw_uint32(bw, 0x3d, 6); + if(!ok) { + printf("FAILED\n"); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + bits += 6; + test_pattern1[words] <<= 6; + test_pattern1[words] |= 0x3d; + if(bw->words != words) { + printf("FAILED word count %u != %u\n", bw->words, words); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + if(bw->bits != bits) { + printf("FAILED bit count %u != %u\n", bw->bits, bits); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) { + printf("FAILED pattern match (buffer)\n"); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + if((bw->accum & 0x3fffffff) != test_pattern1[words]) { + printf("FAILED pattern match (bw->accum=%" PRI_BWWORD " != %" PRI_BWWORD ")\n", bw->accum&0x3fffffff, test_pattern1[words]); + FLAC__bitwriter_dump(bw, stdout); + return false; + } + printf("OK\n"); + FLAC__bitwriter_dump(bw, stdout); + + printf("testing utf8_uint32(0x00000000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x00000000); + ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x0000007F)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x0000007F); + ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x00000080)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x00000080); + ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x000007FF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x000007FF); + ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x00000800)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x00000800); + ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x0000FFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x0000FFFF); + ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x00010000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x00010000); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080; +#else + ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF0908080; +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x001FFFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x001FFFFF); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF; +#else + ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF7BFBFBF; +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x00200000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x00200000); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80; +#else + ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xF888808080); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x03FFFFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x03FFFFFF); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF; +#else + ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xFBBFBFBFBF); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x04000000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x04000000); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080; +#else + ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFC8480808080); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint32(0x7FFFFFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint32(bw, 0x7FFFFFFF); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF; +#else + ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFDBFBFBFBFBF); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000000000000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000000)); + ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x000000000000007F)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000000000007F)); + ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000000000080)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000080)); + ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x00000000000007FF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x00000000000007FF)); + ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000000000800)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000000800)); + ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x000000000000FFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000000000FFFF)); + ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF; + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000000010000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000010000)); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080; +#else + ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF0908080; +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x00000000001FFFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x00000000001FFFFF)); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF; +#else + ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 32 && (bw->accum & 0xffffffff) == 0xF7BFBFBF; +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000000200000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000000200000)); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80; +#else + ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xF888808080); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000003FFFFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000003FFFFFF)); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF; +#else + ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 40 && (bw->accum & FLAC__U64L(0xffffffffff)) == FLAC__U64L(0xFBBFBFBFBF); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000004000000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000004000000)); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080; +#else + ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFC8480808080); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x000000007FFFFFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x000000007FFFFFFF)); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF; +#else + ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 48 && (bw->accum & FLAC__U64L(0xffffffffffff)) == FLAC__U64L(0xFDBFBFBFBFBF); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000080000000)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000080000000)); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFE828080 && (bw->accum & 0xffffff) == 0x808080; +#else + ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0x808082FE && (bw->accum & 0xffffff) == 0x808080; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 56 && (bw->accum & FLAC__U64L(0xffffffffffffff)) == FLAC__U64L(0xFE828080808080); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing utf8_uint64(0x0000000FFFFFFFFF)... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000FFFFFFFFF)); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFEBFBFBF && (bw->accum & 0xffffff) == 0xBFBFBF; +#else + ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xBFBFBFFE && (bw->accum & 0xffffff) == 0xBFBFBF; +#endif +#elif FLAC__BYTES_PER_WORD == 8 + ok = TOTAL_BITS(bw) == 56 && (bw->accum & FLAC__U64L(0xffffffffffffff)) == FLAC__U64L(0xFEBFBFBFBFBFBF); +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + + printf("testing grow... "); + FLAC__bitwriter_clear(bw); + FLAC__bitwriter_write_raw_uint32(bw, 0x5, 4); + j = bw->capacity; + for(i = 0; i < j; i++) + FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32); +#if FLAC__BYTES_PER_WORD == 4 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0x5aaaaaaa && (bw->accum & 0xf) == 0xa; +#else + ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0xaaaaaa5a && (bw->accum & 0xf) == 0xa; +#endif +#elif FLAC__BYTES_PER_WORD == 8 +#if WORDS_BIGENDIAN + ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == FLAC__U64L(0x5aaaaaaaaaaaaaaa) && (bw->accum & 0xf) == 0xa; +#else + ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == FLAC__U64L(0xaaaaaaaaaaaaaa5a) && (bw->accum & 0xf) == 0xa; +#endif +#endif + printf("%s\n", ok?"OK":"FAILED"); + if(!ok) { + FLAC__bitwriter_dump(bw, stdout); + return false; + } + printf("capacity = %u\n", bw->capacity); + + printf("testing free... "); + FLAC__bitwriter_free(bw); + printf("OK\n"); + + printf("testing delete... "); + FLAC__bitwriter_delete(bw); + printf("OK\n"); + + printf("\nPASSED!\n"); + return true; +} diff --git a/src/test_libFLAC/bitwriter.h b/src/test_libFLAC/bitwriter.h new file mode 100644 index 0000000..9b6a824 --- /dev/null +++ b/src/test_libFLAC/bitwriter.h @@ -0,0 +1,27 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2000-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_BITBUFFER_H +#define FLAC__TEST_LIBFLAC_BITBUFFER_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_bitwriter(void); + +#endif diff --git a/src/test_libFLAC/crc.c b/src/test_libFLAC/crc.c new file mode 100644 index 0000000..8b87671 --- /dev/null +++ b/src/test_libFLAC/crc.c @@ -0,0 +1,274 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2014-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> + +#include "FLAC/assert.h" +#include "share/compat.h" +#include "private/crc.h" +#include "crc.h" + +static FLAC__uint8 crc8_update_ref(FLAC__byte byte, FLAC__uint8 crc); +static FLAC__uint16 crc16_update_ref(FLAC__byte byte, FLAC__uint16 crc); + +static FLAC__bool test_crc8(const FLAC__byte *data, size_t size); +static FLAC__bool test_crc16(const FLAC__byte *data, size_t size); +static FLAC__bool test_crc16_update(const FLAC__byte *data, size_t size); +static FLAC__bool test_crc16_32bit_words(const FLAC__uint32 *words, size_t size); +static FLAC__bool test_crc16_64bit_words(const FLAC__uint64 *words, size_t size); + +#define DATA_SIZE 32768 + +FLAC__bool test_crc(void) +{ + uint32_t i; + FLAC__byte data[DATA_SIZE] = { 0 }; + + /* Initialize data reproducibly with pseudo-random values. */ + for (i = 1; i < DATA_SIZE; i++) + data[i] = crc8_update_ref(i % 256, data[i - 1]); + + printf("\n+++ libFLAC unit test: crc\n\n"); + + if (! test_crc8(data, DATA_SIZE)) + return false; + + if (! test_crc16(data, DATA_SIZE)) + return false; + + if (! test_crc16_update(data, DATA_SIZE)) + return false; + + if (! test_crc16_32bit_words((FLAC__uint32 *)data, DATA_SIZE / 4)) + return false; + + if (! test_crc16_64bit_words((FLAC__uint64 *)data, DATA_SIZE / 8)) + return false; + + printf("\nPASSED!\n"); + return true; +} + +/*----------------------------------------------------------------------------*/ + +/* Reference implementations of CRC-8 and CRC-16 to check against. */ + +#define CRC8_POLYNOMIAL 0x07 + +static FLAC__uint8 crc8_update_ref(FLAC__byte byte, FLAC__uint8 crc) +{ + uint32_t i; + + crc ^= byte; + + for (i = 0; i < 8; i++) { + crc = (crc << 1) ^ ((crc >> 7) ? CRC8_POLYNOMIAL : 0); + } + + return crc; +} + +#define CRC16_POLYNOMIAL 0x8005 + +static FLAC__uint16 crc16_update_ref(FLAC__byte byte, FLAC__uint16 crc) +{ + uint32_t i; + + crc ^= byte << 8; + + for (i = 0; i < 8; i++) { + crc = (crc << 1) ^ ((crc >> 15) ? CRC16_POLYNOMIAL : 0); + } + + return crc; +} + +/*----------------------------------------------------------------------------*/ + +static FLAC__bool test_crc8(const FLAC__byte *data, size_t size) +{ + uint32_t i; + FLAC__uint8 crc0,crc1; + + printf("testing FLAC__crc8 ... "); + + crc0 = 0; + crc1 = FLAC__crc8(data, 0); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc8 returned non-zero CRC for zero bytes of data\n"); + return false; + } + + for (i = 0; i < size; i++) { + crc0 = crc8_update_ref(data[i], crc0); + crc1 = FLAC__crc8(data, i + 1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc8 result did not match reference CRC for %u bytes of test data\n", i + 1); + return false; + } + } + + printf("OK\n"); + + return true; +} + +static FLAC__bool test_crc16(const FLAC__byte *data, size_t size) +{ + uint32_t i; + FLAC__uint16 crc0,crc1; + + printf("testing FLAC__crc16 ... "); + + crc0 = 0; + crc1 = FLAC__crc16(data, 0); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc16 returned non-zero CRC for zero bytes of data\n"); + return false; + } + + for (i = 0; i < size; i++) { + crc0 = crc16_update_ref(data[i], crc0); + crc1 = FLAC__crc16(data, i + 1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc16 result did not match reference CRC for %u bytes of test data\n", i + 1); + return false; + } + } + + printf("OK\n"); + + return true; +} + +static FLAC__bool test_crc16_update(const FLAC__byte *data, size_t size) +{ + uint32_t i; + FLAC__uint16 crc0,crc1; + + printf("testing FLAC__CRC16_UPDATE macro ... "); + + crc0 = 0; + crc1 = 0; + + for (i = 0; i < size; i++) { + crc0 = crc16_update_ref(data[i], crc0); + crc1 = FLAC__CRC16_UPDATE(data[i], crc1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__CRC16_UPDATE result did not match reference CRC after %u bytes of test data\n", i + 1); + return false; + } + } + + printf("OK\n"); + + return true; +} + +static FLAC__bool test_crc16_32bit_words(const FLAC__uint32 *words, size_t size) +{ + uint32_t n,i,k; + FLAC__uint16 crc0,crc1; + + for (n = 1; n <= 16; n++) { + printf("testing FLAC__crc16_update_words32 (length=%i) ... ", n); + + crc0 = 0; + crc1 = 0; + + for (i = 0; i <= size - n; i += n) { + for (k = 0; k < n; k++) { + crc0 = crc16_update_ref( words[i + k] >> 24, crc0); + crc0 = crc16_update_ref((words[i + k] >> 16) & 0xFF, crc0); + crc0 = crc16_update_ref((words[i + k] >> 8) & 0xFF, crc0); + crc0 = crc16_update_ref( words[i + k] & 0xFF, crc0); + } + + crc1 = FLAC__crc16_update_words32(words + i, n, crc1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc16_update_words32 result did not match reference CRC after %u words of test data\n", i + n); + return false; + } + } + + crc1 = FLAC__crc16_update_words32(words, 0, crc1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc16_update_words32 called with zero bytes changed CRC value\n"); + return false; + } + + printf("OK\n"); + } + + return true; +} + +static FLAC__bool test_crc16_64bit_words(const FLAC__uint64 *words, size_t size) +{ + uint32_t n,i,k; + FLAC__uint16 crc0,crc1; + + for (n = 1; n <= 16; n++) { + printf("testing FLAC__crc16_update_words64 (length=%i) ... ", n); + + crc0 = 0; + crc1 = 0; + + for (i = 0; i <= size - n; i += n) { + for (k = 0; k < n; k++) { + crc0 = crc16_update_ref( words[i + k] >> 56, crc0); + crc0 = crc16_update_ref((words[i + k] >> 48) & 0xFF, crc0); + crc0 = crc16_update_ref((words[i + k] >> 40) & 0xFF, crc0); + crc0 = crc16_update_ref((words[i + k] >> 32) & 0xFF, crc0); + crc0 = crc16_update_ref((words[i + k] >> 24) & 0xFF, crc0); + crc0 = crc16_update_ref((words[i + k] >> 16) & 0xFF, crc0); + crc0 = crc16_update_ref((words[i + k] >> 8) & 0xFF, crc0); + crc0 = crc16_update_ref( words[i + k] & 0xFF, crc0); + } + + crc1 = FLAC__crc16_update_words64(words + i, n, crc1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc16_update_words64 result did not match reference CRC after %u words of test data\n", i + n); + return false; + } + } + + crc1 = FLAC__crc16_update_words64(words, 0, crc1); + + if (crc1 != crc0) { + printf("FAILED, FLAC__crc16_update_words64 called with zero bytes changed CRC value\n"); + return false; + } + + printf("OK\n"); + } + + return true; +} diff --git a/src/test_libFLAC/crc.h b/src/test_libFLAC/crc.h new file mode 100644 index 0000000..11523cd --- /dev/null +++ b/src/test_libFLAC/crc.h @@ -0,0 +1,26 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2014-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_CRC_H +#define FLAC__TEST_LIBFLAC_CRC_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_crc(void); + +#endif diff --git a/src/test_libFLAC/decoders.c b/src/test_libFLAC/decoders.c new file mode 100644 index 0000000..ae114ce --- /dev/null +++ b/src/test_libFLAC/decoders.c @@ -0,0 +1,1054 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "decoders.h" +#include "FLAC/assert.h" +#include "FLAC/stream_decoder.h" +#include "share/grabbag.h" +#include "share/compat.h" +#include "share/safe_str.h" +#include "test_libs_common/file_utils_flac.h" +#include "test_libs_common/metadata_utils.h" + +typedef enum { + LAYER_STREAM = 0, /* FLAC__stream_decoder_init_[ogg_]stream() without seeking */ + LAYER_SEEKABLE_STREAM, /* FLAC__stream_decoder_init_[ogg_]stream() with seeking */ + LAYER_FILE, /* FLAC__stream_decoder_init_[ogg_]FILE() */ + LAYER_FILENAME /* FLAC__stream_decoder_init_[ogg_]file() */ +} Layer; + +static const char * const LayerString[] = { + "Stream", + "Seekable Stream", + "FILE*", + "Filename" +}; + +typedef struct { + Layer layer; + FILE *file; + char filename[512]; + uint32_t current_metadata_number; + FLAC__bool ignore_errors; + FLAC__bool error_occurred; +} StreamDecoderClientData; + +static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_; +static FLAC__StreamMetadata *expected_metadata_sequence_[9]; +static uint32_t num_expected_; +static FLAC__off_t flacfilesize_; + +static const char *flacfilename(FLAC__bool is_ogg) +{ + return is_ogg? "metadata.oga" : "metadata.flac"; +} + +static FLAC__bool die_(const char *msg) +{ + printf("ERROR: %s\n", msg); + return false; +} + +static FLAC__bool die_s_(const char *msg, const FLAC__StreamDecoder *decoder) +{ + FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(decoder); + + if(msg) + printf("FAILED, %s", msg); + else + printf("FAILED"); + + printf(", state = %u (%s)\n", (uint32_t)state, FLAC__StreamDecoderStateString[state]); + + return false; +} + +static void open_test_file(StreamDecoderClientData * pdcd, int is_ogg, const char * mode) +{ + pdcd->file = flac_fopen(flacfilename(is_ogg), mode); + safe_strncpy(pdcd->filename, flacfilename(is_ogg), sizeof (pdcd->filename)); +} + +static void init_metadata_blocks_(void) +{ + mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_); +} + +static void free_metadata_blocks_(void) +{ + mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_); +} + +static FLAC__bool generate_file_(FLAC__bool is_ogg) +{ + printf("\n\ngenerating %sFLAC file for decoder tests...\n", is_ogg? "Ogg ":""); + + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + /* WATCHOUT: for Ogg FLAC the encoder should move the VORBIS_COMMENT block to the front, right after STREAMINFO */ + + if(!file_utils__generate_flacfile(is_ogg, flacfilename(is_ogg), &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_)) + return die_("creating the encoded file"); + + return true; +} + +static FLAC__StreamDecoderReadStatus stream_decoder_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data) +{ + StreamDecoderClientData *dcd = (StreamDecoderClientData*)client_data; + const size_t requested_bytes = *bytes; + + (void)decoder; + + if(0 == dcd) { + printf("ERROR: client_data in read callback is NULL\n"); + return FLAC__STREAM_DECODER_READ_STATUS_ABORT; + } + + if(dcd->error_occurred) + return FLAC__STREAM_DECODER_READ_STATUS_ABORT; + + if(feof(dcd->file)) { + *bytes = 0; + return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; + } + else if(requested_bytes > 0) { + *bytes = fread(buffer, 1, requested_bytes, dcd->file); + if(*bytes == 0) { + if(feof(dcd->file)) + return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; + else + return FLAC__STREAM_DECODER_READ_STATUS_ABORT; + } + else { + return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; + } + } + else + return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */ +} + +static FLAC__StreamDecoderSeekStatus stream_decoder_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) +{ + StreamDecoderClientData *dcd = (StreamDecoderClientData*)client_data; + + (void)decoder; + + if(0 == dcd) { + printf("ERROR: client_data in seek callback is NULL\n"); + return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; + } + + if(dcd->error_occurred) + return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; + + if(fseeko(dcd->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) { + dcd->error_occurred = true; + return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR; + } + + return FLAC__STREAM_DECODER_SEEK_STATUS_OK; +} + +static FLAC__StreamDecoderTellStatus stream_decoder_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) +{ + StreamDecoderClientData *dcd = (StreamDecoderClientData*)client_data; + FLAC__off_t offset; + + (void)decoder; + + if(0 == dcd) { + printf("ERROR: client_data in tell callback is NULL\n"); + return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; + } + + if(dcd->error_occurred) + return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; + + offset = ftello(dcd->file); + *absolute_byte_offset = (FLAC__uint64)offset; + + if(offset < 0) { + dcd->error_occurred = true; + return FLAC__STREAM_DECODER_TELL_STATUS_ERROR; + } + + return FLAC__STREAM_DECODER_TELL_STATUS_OK; +} + +static FLAC__StreamDecoderLengthStatus stream_decoder_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) +{ + StreamDecoderClientData *dcd = (StreamDecoderClientData*)client_data; + + (void)decoder; + + if(0 == dcd) { + printf("ERROR: client_data in length callback is NULL\n"); + return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; + } + + if(dcd->error_occurred) + return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR; + + *stream_length = (FLAC__uint64)flacfilesize_; + return FLAC__STREAM_DECODER_LENGTH_STATUS_OK; +} + +static FLAC__bool stream_decoder_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data) +{ + StreamDecoderClientData *dcd = (StreamDecoderClientData*)client_data; + + (void)decoder; + + if(0 == dcd) { + printf("ERROR: client_data in eof callback is NULL\n"); + return true; + } + + if(dcd->error_occurred) + return true; + + return feof(dcd->file); +} + +static FLAC__StreamDecoderWriteStatus stream_decoder_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) +{ + StreamDecoderClientData *dcd = (StreamDecoderClientData*)client_data; + + (void)decoder, (void)buffer; + + if(0 == dcd) { + printf("ERROR: client_data in write callback is NULL\n"); + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + } + + if(dcd->error_occurred) + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + + if( + (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) || + (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0) + ) { + printf("content... "); + fflush(stdout); + } + + return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; +} + +static void stream_decoder_metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) +{ + StreamDecoderClientData *dcd = (StreamDecoderClientData*)client_data; + + (void)decoder; + + if(0 == dcd) { + printf("ERROR: client_data in metadata callback is NULL\n"); + return; + } + + if(dcd->error_occurred) + return; + + if (metadata->type == FLAC__METADATA_TYPE_APPLICATION) { + printf ("%u ('%c%c%c%c')... ", dcd->current_metadata_number, metadata->data.application.id [0], metadata->data.application.id [1], metadata->data.application.id [2], metadata->data.application.id [3]); + } + else { + printf("%u... ", dcd->current_metadata_number); + } + fflush(stdout); + + + if(dcd->current_metadata_number >= num_expected_) { + (void)die_("got more metadata blocks than expected"); + dcd->error_occurred = true; + } + else { + if(!mutils__compare_block(expected_metadata_sequence_[dcd->current_metadata_number], metadata)) { + (void)die_("metadata block mismatch"); + dcd->error_occurred = true; + } + } + dcd->current_metadata_number++; +} + +static void stream_decoder_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) +{ + StreamDecoderClientData *dcd = (StreamDecoderClientData*)client_data; + + (void)decoder; + + if(0 == dcd) { + printf("ERROR: client_data in error callback is NULL\n"); + return; + } + + if(!dcd->ignore_errors) { + printf("ERROR: got error callback: err = %u (%s)\n", (uint32_t)status, FLAC__StreamDecoderErrorStatusString[status]); + dcd->error_occurred = true; + } +} + +static FLAC__bool stream_decoder_test_respond_(FLAC__StreamDecoder *decoder, StreamDecoderClientData *dcd, FLAC__bool is_ogg) +{ + FLAC__StreamDecoderInitStatus init_status; + + if(!FLAC__stream_decoder_set_md5_checking(decoder, true)) + return die_s_("at FLAC__stream_decoder_set_md5_checking(), returned false", decoder); + + /* for FLAC__stream_encoder_init_FILE(), the FLAC__stream_encoder_finish() closes the file so we have to keep re-opening: */ + if(dcd->layer == LAYER_FILE) { + printf("opening %sFLAC file... ", is_ogg? "Ogg ":""); + open_test_file(dcd, is_ogg, "rb"); + if(0 == dcd->file) { + printf("ERROR (%s)\n", strerror(errno)); + return false; + } + printf("OK\n"); + } + + switch(dcd->layer) { + case LAYER_STREAM: + printf("testing FLAC__stream_decoder_init_%sstream()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_stream(decoder, stream_decoder_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, dcd) : + FLAC__stream_decoder_init_stream(decoder, stream_decoder_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, dcd) + ; + break; + case LAYER_SEEKABLE_STREAM: + printf("testing FLAC__stream_decoder_init_%sstream()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_stream(decoder, stream_decoder_read_callback_, stream_decoder_seek_callback_, stream_decoder_tell_callback_, stream_decoder_length_callback_, stream_decoder_eof_callback_, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, dcd) : + FLAC__stream_decoder_init_stream(decoder, stream_decoder_read_callback_, stream_decoder_seek_callback_, stream_decoder_tell_callback_, stream_decoder_length_callback_, stream_decoder_eof_callback_, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, dcd); + break; + case LAYER_FILE: + printf("testing FLAC__stream_decoder_init_%sFILE()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_FILE(decoder, dcd->file, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, dcd) : + FLAC__stream_decoder_init_FILE(decoder, dcd->file, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, dcd); + break; + case LAYER_FILENAME: + printf("testing FLAC__stream_decoder_init_%sfile()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_file(decoder, flacfilename(is_ogg), stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, dcd) : + FLAC__stream_decoder_init_file(decoder, flacfilename(is_ogg), stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, dcd); + break; + default: + die_("internal error 000"); + return false; + } + if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) + return die_s_(0, decoder); + printf("OK\n"); + + dcd->current_metadata_number = 0; + + if(dcd->layer < LAYER_FILE && fseeko(dcd->file, 0, SEEK_SET) < 0) { + printf("FAILED rewinding input, errno = %d\n", errno); + return false; + } + + printf("testing FLAC__stream_decoder_process_until_end_of_stream()... "); + if(!FLAC__stream_decoder_process_until_end_of_stream(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_finish()... "); + if(!FLAC__stream_decoder_finish(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + return true; +} + +static FLAC__bool test_stream_decoder(Layer layer, FLAC__bool is_ogg) +{ + FLAC__StreamDecoder *decoder; + FLAC__StreamDecoderInitStatus init_status; + FLAC__StreamDecoderState state; + StreamDecoderClientData decoder_client_data; + FLAC__bool expect; + + decoder_client_data.layer = layer; + + printf("\n+++ libFLAC unit test: FLAC__StreamDecoder (layer: %s, format: %s)\n\n", LayerString[layer], is_ogg? "Ogg FLAC" : "FLAC"); + + printf("testing FLAC__stream_decoder_new()... "); + decoder = FLAC__stream_decoder_new(); + if(0 == decoder) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_decoder_delete()... "); + FLAC__stream_decoder_delete(decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_new()... "); + decoder = FLAC__stream_decoder_new(); + if(0 == decoder) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + switch(layer) { + case LAYER_STREAM: + case LAYER_SEEKABLE_STREAM: + printf("testing FLAC__stream_decoder_init_%sstream()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_stream(decoder, 0, 0, 0, 0, 0, 0, 0, 0, 0) : + FLAC__stream_decoder_init_stream(decoder, 0, 0, 0, 0, 0, 0, 0, 0, 0); + break; + case LAYER_FILE: + printf("testing FLAC__stream_decoder_init_%sFILE()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_FILE(decoder, stdin, 0, 0, 0, 0) : + FLAC__stream_decoder_init_FILE(decoder, stdin, 0, 0, 0, 0); + break; + case LAYER_FILENAME: + printf("testing FLAC__stream_decoder_init_%sfile()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_file(decoder, flacfilename(is_ogg), 0, 0, 0, 0) : + FLAC__stream_decoder_init_file(decoder, flacfilename(is_ogg), 0, 0, 0, 0); + break; + default: + die_("internal error 003"); + return false; + } + if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS) + return die_s_(0, decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_delete()... "); + FLAC__stream_decoder_delete(decoder); + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + + printf("testing FLAC__stream_decoder_new()... "); + decoder = FLAC__stream_decoder_new(); + if(0 == decoder) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + if(is_ogg) { + printf("testing FLAC__stream_decoder_set_ogg_serial_number()... "); + if(!FLAC__stream_decoder_set_ogg_serial_number(decoder, file_utils__ogg_serial_number)) + return die_s_("returned false", decoder); + printf("OK\n"); + } + + printf("testing FLAC__stream_decoder_set_md5_checking()... "); + if(!FLAC__stream_decoder_set_md5_checking(decoder, true)) + return die_s_("returned false", decoder); + printf("OK\n"); + + if(layer < LAYER_FILENAME) { + printf("opening %sFLAC file... ", is_ogg? "Ogg ":""); + open_test_file(&decoder_client_data, is_ogg, "rb"); + if(0 == decoder_client_data.file) { + printf("ERROR (%s)\n", strerror(errno)); + return false; + } + printf("OK\n"); + } + + switch(layer) { + case LAYER_STREAM: + printf("testing FLAC__stream_decoder_init_%sstream()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_stream(decoder, stream_decoder_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, &decoder_client_data) : + FLAC__stream_decoder_init_stream(decoder, stream_decoder_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, &decoder_client_data); + break; + case LAYER_SEEKABLE_STREAM: + printf("testing FLAC__stream_decoder_init_%sstream()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_stream(decoder, stream_decoder_read_callback_, stream_decoder_seek_callback_, stream_decoder_tell_callback_, stream_decoder_length_callback_, stream_decoder_eof_callback_, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, &decoder_client_data) : + FLAC__stream_decoder_init_stream(decoder, stream_decoder_read_callback_, stream_decoder_seek_callback_, stream_decoder_tell_callback_, stream_decoder_length_callback_, stream_decoder_eof_callback_, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, &decoder_client_data); + break; + case LAYER_FILE: + printf("testing FLAC__stream_decoder_init_%sFILE()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_FILE(decoder, decoder_client_data.file, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, &decoder_client_data) : + FLAC__stream_decoder_init_FILE(decoder, decoder_client_data.file, stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, &decoder_client_data); + break; + case LAYER_FILENAME: + printf("testing FLAC__stream_decoder_init_%sfile()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_decoder_init_ogg_file(decoder, flacfilename(is_ogg), stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, &decoder_client_data) : + FLAC__stream_decoder_init_file(decoder, flacfilename(is_ogg), stream_decoder_write_callback_, stream_decoder_metadata_callback_, stream_decoder_error_callback_, &decoder_client_data); + break; + default: + die_("internal error 009"); + return false; + } + if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) + return die_s_(0, decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_get_state()... "); + state = FLAC__stream_decoder_get_state(decoder); + printf("returned state = %u (%s)... OK\n", state, FLAC__StreamDecoderStateString[state]); + + decoder_client_data.current_metadata_number = 0; + decoder_client_data.ignore_errors = false; + decoder_client_data.error_occurred = false; + + printf("testing FLAC__stream_decoder_get_md5_checking()... "); + if(!FLAC__stream_decoder_get_md5_checking(decoder)) { + printf("FAILED, returned false, expected true\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_decoder_process_until_end_of_metadata()... "); + if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_process_single()... "); + if(!FLAC__stream_decoder_process_single(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_skip_single_frame()... "); + if(!FLAC__stream_decoder_skip_single_frame(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + if(layer < LAYER_FILE) { + printf("testing FLAC__stream_decoder_flush()... "); + if(!FLAC__stream_decoder_flush(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + decoder_client_data.ignore_errors = true; + printf("testing FLAC__stream_decoder_process_single()... "); + if(!FLAC__stream_decoder_process_single(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + decoder_client_data.ignore_errors = false; + } + + expect = (layer != LAYER_STREAM); + printf("testing FLAC__stream_decoder_seek_absolute()... "); + if(FLAC__stream_decoder_seek_absolute(decoder, 0) != expect) + return die_s_(expect? "returned false" : "returned true", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_process_until_end_of_stream()... "); + if(!FLAC__stream_decoder_process_until_end_of_stream(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + expect = (layer != LAYER_STREAM); + printf("testing FLAC__stream_decoder_seek_absolute()... "); + if(FLAC__stream_decoder_seek_absolute(decoder, 0) != expect) + return die_s_(expect? "returned false" : "returned true", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_get_channels()... "); + { + uint32_t channels = FLAC__stream_decoder_get_channels(decoder); + if(channels != streaminfo_.data.stream_info.channels) { + printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels); + return false; + } + } + printf("OK\n"); + + printf("testing FLAC__stream_decoder_get_bits_per_sample()... "); + { + uint32_t bits_per_sample = FLAC__stream_decoder_get_bits_per_sample(decoder); + if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) { + printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample); + return false; + } + } + printf("OK\n"); + + printf("testing FLAC__stream_decoder_get_sample_rate()... "); + { + uint32_t sample_rate = FLAC__stream_decoder_get_sample_rate(decoder); + if(sample_rate != streaminfo_.data.stream_info.sample_rate) { + printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate); + return false; + } + } + printf("OK\n"); + + printf("testing FLAC__stream_decoder_get_blocksize()... "); + { + uint32_t blocksize = FLAC__stream_decoder_get_blocksize(decoder); + /* value could be anything since we're at the last block, so accept any reasonable answer */ + printf("returned %u... %s\n", blocksize, blocksize>0? "OK" : "FAILED"); + if(blocksize == 0) + return false; + } + + printf("testing FLAC__stream_decoder_get_channel_assignment()... "); + { + FLAC__ChannelAssignment ca = FLAC__stream_decoder_get_channel_assignment(decoder); + printf("returned %u (%s)... OK\n", (uint32_t)ca, FLAC__ChannelAssignmentString[ca]); + } + + if(layer < LAYER_FILE) { + printf("testing FLAC__stream_decoder_reset()... "); + if(!FLAC__stream_decoder_reset(decoder)) { + state = FLAC__stream_decoder_get_state(decoder); + printf("FAILED, returned false, state = %u (%s)\n", state, FLAC__StreamDecoderStateString[state]); + return false; + } + printf("OK\n"); + + if(layer == LAYER_STREAM) { + /* after a reset() we have to rewind the input ourselves */ + printf("rewinding input... "); + if(fseeko(decoder_client_data.file, 0, SEEK_SET) < 0) { + printf("FAILED, errno = %d\n", errno); + return false; + } + printf("OK\n"); + } + + decoder_client_data.current_metadata_number = 0; + + printf("testing FLAC__stream_decoder_process_until_end_of_stream()... "); + if(!FLAC__stream_decoder_process_until_end_of_stream(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + } + + printf("testing FLAC__stream_decoder_finish()... "); + if(!FLAC__stream_decoder_finish(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + /* + * respond all + */ + + printf("testing FLAC__stream_decoder_set_metadata_respond_all()... "); + if(!FLAC__stream_decoder_set_metadata_respond_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &application1_; + expected_metadata_sequence_[num_expected_++] = &application2_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * ignore all + */ + + printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... "); + if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * respond all, ignore VORBIS_COMMENT + */ + + printf("testing FLAC__stream_decoder_set_metadata_respond_all()... "); + if(!FLAC__stream_decoder_set_metadata_respond_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_ignore(VORBIS_COMMENT)... "); + if(!FLAC__stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &padding_; + if(!is_ogg) /* encoder removes seektable for ogg */ + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * respond all, ignore APPLICATION + */ + + printf("testing FLAC__stream_decoder_set_metadata_respond_all()... "); + if(!FLAC__stream_decoder_set_metadata_respond_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_ignore(APPLICATION)... "); + if(!FLAC__stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * respond all, ignore APPLICATION id of app#1 + */ + + printf("testing FLAC__stream_decoder_set_metadata_respond_all()... "); + if(!FLAC__stream_decoder_set_metadata_respond_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_ignore_application(of app block #1)... "); + if(!FLAC__stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &application2_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &padding_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * respond all, ignore APPLICATION id of app#1 & app#2 + */ + + printf("testing FLAC__stream_decoder_set_metadata_respond_all()... "); + if(!FLAC__stream_decoder_set_metadata_respond_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_ignore_application(of app block #1)... "); + if(!FLAC__stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_ignore_application(of app block #2)... "); + if(!FLAC__stream_decoder_set_metadata_ignore_application(decoder, application2_.data.application.id)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * ignore all, respond VORBIS_COMMENT + */ + + printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... "); + if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_respond(VORBIS_COMMENT)... "); + if(!FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * ignore all, respond APPLICATION + */ + + printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... "); + if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_respond(APPLICATION)... "); + if(!FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &application1_; + expected_metadata_sequence_[num_expected_++] = &application2_; + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * ignore all, respond APPLICATION id of app#1 + */ + + printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... "); + if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_respond_application(of app block #1)... "); + if(!FLAC__stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &application1_; + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * ignore all, respond APPLICATION id of app#1 & app#2 + */ + + printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... "); + if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_respond_application(of app block #1)... "); + if(!FLAC__stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_respond_application(of app block #2)... "); + if(!FLAC__stream_decoder_set_metadata_respond_application(decoder, application2_.data.application.id)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &application1_; + expected_metadata_sequence_[num_expected_++] = &application2_; + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * respond all, ignore APPLICATION, respond APPLICATION id of app#1 + */ + + printf("testing FLAC__stream_decoder_set_metadata_respond_all()... "); + if(!FLAC__stream_decoder_set_metadata_respond_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_ignore(APPLICATION)... "); + if(!FLAC__stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_APPLICATION)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_respond_application(of app block #1)... "); + if(!FLAC__stream_decoder_set_metadata_respond_application(decoder, application1_.data.application.id)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */ + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &vorbiscomment_; + expected_metadata_sequence_[num_expected_++] = &padding_; + expected_metadata_sequence_[num_expected_++] = &application1_; + expected_metadata_sequence_[num_expected_++] = &cuesheet_; + expected_metadata_sequence_[num_expected_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + else { + expected_metadata_sequence_[num_expected_++] = &streaminfo_; + expected_metadata_sequence_[num_expected_++] = &padding_; + 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_++] = &picture_; + expected_metadata_sequence_[num_expected_++] = &unknown_; + } + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + /* + * ignore all, respond APPLICATION, ignore APPLICATION id of app#1 + */ + + printf("testing FLAC__stream_decoder_set_metadata_ignore_all()... "); + if(!FLAC__stream_decoder_set_metadata_ignore_all(decoder)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_respond(APPLICATION)... "); + if(!FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_APPLICATION)) + return die_s_("returned false", decoder); + printf("OK\n"); + + printf("testing FLAC__stream_decoder_set_metadata_ignore_application(of app block #1)... "); + if(!FLAC__stream_decoder_set_metadata_ignore_application(decoder, application1_.data.application.id)) + return die_s_("returned false", decoder); + printf("OK\n"); + + num_expected_ = 0; + expected_metadata_sequence_[num_expected_++] = &application2_; + + if(!stream_decoder_test_respond_(decoder, &decoder_client_data, is_ogg)) + return false; + + if(layer < LAYER_FILE) /* for LAYER_FILE, FLAC__stream_decoder_finish() closes the file */ + fclose(decoder_client_data.file); + + printf("testing FLAC__stream_decoder_delete()... "); + FLAC__stream_decoder_delete(decoder); + printf("OK\n"); + + printf("\nPASSED!\n"); + + return true; +} + +FLAC__bool test_decoders(void) +{ + FLAC__bool is_ogg = false; + + while(1) { + init_metadata_blocks_(); + + if(!generate_file_(is_ogg)) + return false; + + if(!test_stream_decoder(LAYER_STREAM, is_ogg)) + return false; + + if(!test_stream_decoder(LAYER_SEEKABLE_STREAM, is_ogg)) + return false; + + if(!test_stream_decoder(LAYER_FILE, is_ogg)) + return false; + + if(!test_stream_decoder(LAYER_FILENAME, is_ogg)) + return false; + + (void) grabbag__file_remove_file(flacfilename(is_ogg)); + + free_metadata_blocks_(); + + if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg) + break; + is_ogg = true; + } + + return true; +} diff --git a/src/test_libFLAC/decoders.h b/src/test_libFLAC/decoders.h new file mode 100644 index 0000000..431eb17 --- /dev/null +++ b/src/test_libFLAC/decoders.h @@ -0,0 +1,27 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_DECODERS_H +#define FLAC__TEST_LIBFLAC_DECODERS_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_decoders(void); + +#endif diff --git a/src/test_libFLAC/encoders.c b/src/test_libFLAC/encoders.c new file mode 100644 index 0000000..d3fd39d --- /dev/null +++ b/src/test_libFLAC/encoders.c @@ -0,0 +1,530 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "encoders.h" +#include "FLAC/assert.h" +#include "FLAC/stream_encoder.h" +#include "share/grabbag.h" +#include "share/compat.h" +#include "test_libs_common/file_utils_flac.h" +#include "test_libs_common/metadata_utils.h" + +typedef enum { + LAYER_STREAM = 0, /* FLAC__stream_encoder_init_[ogg_]stream() without seeking */ + LAYER_SEEKABLE_STREAM, /* FLAC__stream_encoder_init_[ogg_]stream() with seeking */ + LAYER_FILE, /* FLAC__stream_encoder_init_[ogg_]FILE() */ + LAYER_FILENAME /* FLAC__stream_encoder_init_[ogg_]file() */ +} Layer; + +static const char * const LayerString[] = { + "Stream", + "Seekable Stream", + "FILE*", + "Filename" +}; + +static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_; +static FLAC__StreamMetadata *metadata_sequence_[] = { &vorbiscomment_, &padding_, &seektable_, &application1_, &application2_, &cuesheet_, &picture_, &unknown_ }; +static const uint32_t num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]); + +static const char *flacfilename(FLAC__bool is_ogg) +{ + return is_ogg? "metadata.oga" : "metadata.flac"; +} + +static FLAC__bool die_(const char *msg) +{ + printf("ERROR: %s\n", msg); + return false; +} + +static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder) +{ + FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder); + + if(msg) + printf("FAILED, %s", msg); + else + printf("FAILED"); + + printf(", state = %u (%s)\n", (uint32_t)state, FLAC__StreamEncoderStateString[state]); + if(state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) { + FLAC__StreamDecoderState dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder); + printf(" verify decoder state = %u (%s)\n", (uint32_t)dstate, FLAC__StreamDecoderStateString[dstate]); + } + + return false; +} + +static void init_metadata_blocks_(void) +{ + mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_); +} + +static void free_metadata_blocks_(void) +{ + mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_); +} + +static FLAC__StreamEncoderReadStatus stream_encoder_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data) +{ + FILE *f = (FILE*)client_data; + (void)encoder; + if(*bytes > 0) { + *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, f); + if(ferror(f)) + return FLAC__STREAM_ENCODER_READ_STATUS_ABORT; + else if(*bytes == 0) + return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM; + else + return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE; + } + else + return FLAC__STREAM_ENCODER_READ_STATUS_ABORT; +} + +static FLAC__StreamEncoderWriteStatus stream_encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data) +{ + FILE *f = (FILE*)client_data; + (void)encoder, (void)samples, (void)current_frame; + if(fwrite(buffer, 1, bytes, f) != bytes) + return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR; + else + return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; +} + +static FLAC__StreamEncoderSeekStatus stream_encoder_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) +{ + FILE *f = (FILE*)client_data; + (void)encoder; + if(fseeko(f, (long)absolute_byte_offset, SEEK_SET) < 0) + return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR; + else + return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; +} + +static FLAC__StreamEncoderTellStatus stream_encoder_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) +{ + FILE *f = (FILE*)client_data; + FLAC__off_t pos; + (void)encoder; + if((pos = ftello(f)) < 0) + return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR; + else { + *absolute_byte_offset = (FLAC__uint64)pos; + return FLAC__STREAM_ENCODER_TELL_STATUS_OK; + } +} + +static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data) +{ + (void)encoder, (void)metadata, (void)client_data; +} + +static void stream_encoder_progress_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, uint32_t frames_written, uint32_t total_frames_estimate, void *client_data) +{ + (void)encoder, (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate, (void)client_data; +} + +static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg) +{ + FLAC__StreamEncoder *encoder; + FLAC__StreamEncoderInitStatus init_status; + FLAC__StreamEncoderState state; + FLAC__StreamDecoderState dstate; + FILE *file = 0; + FLAC__int32 samples[1024]; + FLAC__int32 *samples_array[1]; + uint32_t i; + + samples_array[0] = samples; + + printf("\n+++ libFLAC unit test: FLAC__StreamEncoder (layer: %s, format: %s)\n\n", LayerString[layer], is_ogg? "Ogg FLAC":"FLAC"); + + printf("testing FLAC__stream_encoder_new()... "); + encoder = FLAC__stream_encoder_new(); + if(0 == encoder) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + if(is_ogg) { + printf("testing FLAC__stream_encoder_set_ogg_serial_number()... "); + if(!FLAC__stream_encoder_set_ogg_serial_number(encoder, file_utils__ogg_serial_number)) + return die_s_("returned false", encoder); + printf("OK\n"); + } + + printf("testing FLAC__stream_encoder_set_verify()... "); + if(!FLAC__stream_encoder_set_verify(encoder, true)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_streamable_subset()... "); + if(!FLAC__stream_encoder_set_streamable_subset(encoder, true)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_channels()... "); + if(!FLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_bits_per_sample()... "); + if(!FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_sample_rate()... "); + if(!FLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_compression_level()... "); + if(!FLAC__stream_encoder_set_compression_level(encoder, (uint32_t)(-1))) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_blocksize()... "); + if(!FLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... "); + if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... "); + if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_max_lpc_order()... "); + if(!FLAC__stream_encoder_set_max_lpc_order(encoder, 0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... "); + if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... "); + if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_do_escape_coding()... "); + if(!FLAC__stream_encoder_set_do_escape_coding(encoder, false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... "); + if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... "); + if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... "); + if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... "); + if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_total_samples_estimate()... "); + if(!FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_metadata()... "); + if(!FLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_)) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_set_limit_min_bitrate()... "); + if(!FLAC__stream_encoder_set_limit_min_bitrate(encoder, true)) + return die_s_("returned false", encoder); + printf("OK\n"); + + if(layer < LAYER_FILENAME) { + printf("opening file for FLAC output... "); + file = flac_fopen(flacfilename(is_ogg), "w+b"); + if(0 == file) { + printf("ERROR (%s)\n", strerror(errno)); + return false; + } + printf("OK\n"); + } + + switch(layer) { + case LAYER_STREAM: + printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_encoder_init_ogg_stream(encoder, /*read_callback=*/0, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/file) : + FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/file); + break; + case LAYER_SEEKABLE_STREAM: + printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_encoder_init_ogg_stream(encoder, stream_encoder_read_callback_, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/file) : + FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/file); + break; + case LAYER_FILE: + printf("testing FLAC__stream_encoder_init_%sFILE()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_encoder_init_ogg_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0) : + FLAC__stream_encoder_init_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0); + break; + case LAYER_FILENAME: + printf("testing FLAC__stream_encoder_init_%sfile()... ", is_ogg? "ogg_":""); + init_status = is_ogg? + FLAC__stream_encoder_init_ogg_file(encoder, flacfilename(is_ogg), stream_encoder_progress_callback_, /*client_data=*/0) : + FLAC__stream_encoder_init_file(encoder, flacfilename(is_ogg), stream_encoder_progress_callback_, /*client_data=*/0); + break; + default: + die_("internal error 001"); + return false; + } + if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) + return die_s_(0, encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_state()... "); + state = FLAC__stream_encoder_get_state(encoder); + printf("returned state = %u (%s)... OK\n", (uint32_t)state, FLAC__StreamEncoderStateString[state]); + + printf("testing FLAC__stream_encoder_get_verify_decoder_state()... "); + dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder); + printf("returned state = %u (%s)... OK\n", (uint32_t)dstate, FLAC__StreamDecoderStateString[dstate]); + + { + FLAC__uint64 absolute_sample; + uint32_t frame_number; + uint32_t channel; + uint32_t sample; + FLAC__int32 expected; + FLAC__int32 got; + + printf("testing FLAC__stream_encoder_get_verify_decoder_error_stats()... "); + FLAC__stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got); + printf("OK\n"); + } + + printf("testing FLAC__stream_encoder_get_verify()... "); + if(FLAC__stream_encoder_get_verify(encoder) != true) { + printf("FAILED, expected true, got false\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_streamable_subset()... "); + if(FLAC__stream_encoder_get_streamable_subset(encoder) != true) { + printf("FAILED, expected true, got false\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... "); + if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... "); + if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_channels()... "); + if(FLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) { + printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__stream_encoder_get_channels(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_bits_per_sample()... "); + if(FLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) { + printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__stream_encoder_get_bits_per_sample(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_sample_rate()... "); + if(FLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) { + printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__stream_encoder_get_sample_rate(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_blocksize()... "); + if(FLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) { + printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__stream_encoder_get_blocksize(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_max_lpc_order()... "); + if(FLAC__stream_encoder_get_max_lpc_order(encoder) != 0) { + printf("FAILED, expected %d, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... "); + (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder); + /* we asked the encoder to auto select this so we accept anything */ + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... "); + if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_do_escape_coding()... "); + if(FLAC__stream_encoder_get_do_escape_coding(encoder) != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... "); + if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) { + printf("FAILED, expected false, got true\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... "); + if(FLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) { + printf("FAILED, expected %d, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... "); + if(FLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) { + printf("FAILED, expected %d, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... "); + if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) { + printf("FAILED, expected %d, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_total_samples_estimate()... "); + if(FLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) { + printf("FAILED, expected %" PRIu64 ", got %" PRIu64 "\n", streaminfo_.data.stream_info.total_samples, FLAC__stream_encoder_get_total_samples_estimate(encoder)); + return false; + } + printf("OK\n"); + + printf("testing FLAC__stream_encoder_get_limit_min_bitrate()... "); + if(FLAC__stream_encoder_get_limit_min_bitrate(encoder) != true) { + printf("FAILED, expected true, got false\n"); + return false; + } + + /* init the dummy sample buffer */ + for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++) + samples[i] = i & 7; + + printf("testing FLAC__stream_encoder_process()... "); + if(!FLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32))) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_process_interleaved()... "); + if(!FLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32))) + return die_s_("returned false", encoder); + printf("OK\n"); + + printf("testing FLAC__stream_encoder_finish()... "); + if(!FLAC__stream_encoder_finish(encoder)) + return die_s_("returned false", encoder); + printf("OK\n"); + + if(layer < LAYER_FILE) + fclose(file); + + printf("testing FLAC__stream_encoder_delete()... "); + FLAC__stream_encoder_delete(encoder); + printf("OK\n"); + + printf("\nPASSED!\n"); + + return true; +} + +FLAC__bool test_encoders(void) +{ + FLAC__bool is_ogg = false; + + while(1) { + init_metadata_blocks_(); + + if(!test_stream_encoder(LAYER_STREAM, is_ogg)) + return false; + + if(!test_stream_encoder(LAYER_SEEKABLE_STREAM, is_ogg)) + return false; + + if(!test_stream_encoder(LAYER_FILE, is_ogg)) + return false; + + if(!test_stream_encoder(LAYER_FILENAME, is_ogg)) + return false; + + (void) grabbag__file_remove_file(flacfilename(is_ogg)); + + free_metadata_blocks_(); + + if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg) + break; + is_ogg = true; + } + + return true; +} diff --git a/src/test_libFLAC/encoders.h b/src/test_libFLAC/encoders.h new file mode 100644 index 0000000..7bdcaf5 --- /dev/null +++ b/src/test_libFLAC/encoders.h @@ -0,0 +1,27 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_ENCODERS_H +#define FLAC__TEST_LIBFLAC_ENCODERS_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_encoders(void); + +#endif diff --git a/src/test_libFLAC/endswap.c b/src/test_libFLAC/endswap.c new file mode 100644 index 0000000..808f81f --- /dev/null +++ b/src/test_libFLAC/endswap.c @@ -0,0 +1,111 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2014-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <string.h> + +#include "share/compat.h" +#include "FLAC/assert.h" +#include "share/endswap.h" +#include "private/md5.h" +#include "endswap.h" + + +FLAC__bool test_endswap(void) +{ + int16_t i16 = 0x1234; + uint16_t u16 = 0xabcd; + int32_t i32 = 0x12345678; + uint32_t u32 = 0xabcdef01; + + union { + uint8_t bytes[4]; + uint16_t u16; + uint32_t u32; + } data; + + printf("\n+++ libFLAC unit test: endswap (%s endian host)\n\n", CPU_IS_BIG_ENDIAN ? "big" : "little"); + + printf("testing ENDSWAP_16 on int16_t ... "); + if (((int16_t) ENDSWAP_16(i16)) == i16) { + printf("\nFAILED, ENDSWAP_16(0x%04x) -> 0x%04x == 0x%04x\n", i16, ENDSWAP_16(i16), i16); + return false; + } + if (((int16_t) ENDSWAP_16(ENDSWAP_16(i16))) != i16) { + printf("\nFAILED, ENDSWAP_16(ENDSWAP_16(0x%04x)) -> 0x%04x != 0x%04x\n", i16, ENDSWAP_16(ENDSWAP_16(i16)), i16); + return false; + } + puts("OK"); + + printf("testing ENDSWAP_16 on uint16_t ... "); + if (((uint16_t) ENDSWAP_16(u16)) == u16) { + printf("\nFAILED, ENDSWAP_16(0x%04x) -> 0x%04x == 0x%04x\n", u16, ENDSWAP_16(u16), u16); + return false; + } + if (((uint16_t) ENDSWAP_16(ENDSWAP_16(u16))) != u16) { + printf("\nFAILED, ENDSWAP_16(ENDSWAP_16(0x%04x)) -> 0x%04x != 0x%04x\n", u16, ENDSWAP_16(ENDSWAP_16(u16)), u16); + return false; + } + puts("OK"); + + printf("testing ENDSWAP_32 on int32_t ... "); + if (((int32_t) ENDSWAP_32 (i32)) == i32) { + printf("\nFAILED, ENDSWAP_32(0x%08x) -> 0x%08x == 0x%08x\n", i32, (uint32_t) ENDSWAP_32 (i32), i32); + return false; + } + if (((int32_t) ENDSWAP_32 (ENDSWAP_32 (i32))) != i32) { + printf("\nFAILED, ENDSWAP_32(ENDSWAP_32(0x%08x)) -> 0x%08x != 0x%08x\n", i32, (uint32_t) ENDSWAP_32(ENDSWAP_32 (i32)), i32); + return false; + } + puts("OK"); + + printf("testing ENDSWAP_32 on uint32_t ... "); + if (((uint32_t) ENDSWAP_32(u32)) == u32) { + printf("\nFAILED, ENDSWAP_32(0x%08x) -> 0x%08x == 0x%08x\n", u32, (uint32_t) ENDSWAP_32(u32), u32); + return false; + } + if (((uint32_t) ENDSWAP_32 (ENDSWAP_32(u32))) != u32) { + printf("\nFAILED, ENDSWAP_32(ENDSWAP_32(0x%08x)) -> 0x%08x != 0%08x\n", u32, (uint32_t) ENDSWAP_32(ENDSWAP_32(u32)), u32); + return false; + } + puts("OK"); + + printf("testing H2LE_16 on uint16_t ... "); + data.u16 = H2LE_16(0x1234); + if (data.bytes [0] != 0x34 || data.bytes [1] != 0x12) { + printf("\nFAILED, H2LE_16(0x%04x) -> { 0x%02x, 0x%02x }\n", data.u16, data.bytes [0] & 0xff, data.bytes [1] & 0xff); + return false; + } + puts("OK"); + + printf("testing H2LE_32 on uint32_t ... "); + data.u32 = H2LE_32(0x12345678); + if (data.bytes [0] != 0x78 || data.bytes [1] != 0x56 || data.bytes [2] != 0x34 || data.bytes [3] != 0x12) { + printf("\nFAILED, H2LE_32(0x%08x) -> { 0x%02x, 0x%02x, 0x%02x, 0x%02x }\n", + data.u32, data.bytes [0] & 0xff, data.bytes [1] & 0xff, data.bytes [2] & 0xff, data.bytes [3] & 0xff); + return false; + } + puts("OK"); + + printf("\nPASSED!\n"); + return true; +} diff --git a/src/test_libFLAC/endswap.h b/src/test_libFLAC/endswap.h new file mode 100644 index 0000000..952b17f --- /dev/null +++ b/src/test_libFLAC/endswap.h @@ -0,0 +1,26 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2014-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_ENDSWAP_H +#define FLAC__TEST_LIBFLAC_ENDSWAP_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_endswap(void); + +#endif diff --git a/src/test_libFLAC/format.c b/src/test_libFLAC/format.c new file mode 100644 index 0000000..c5e8bf2 --- /dev/null +++ b/src/test_libFLAC/format.c @@ -0,0 +1,260 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2004-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "FLAC/assert.h" +#include "FLAC/format.h" +#include "format.h" +#include <stdio.h> + +static const char *true_false_string_[2] = { "false", "true" }; + +static struct { + uint32_t rate; + FLAC__bool valid; + FLAC__bool subset; +} SAMPLE_RATES[] = { + { 0 , true , true }, + { 1 , true , true }, + { 9 , true , true }, + { 10 , true , true }, + { 4000 , true , true }, + { 8000 , true , true }, + { 11025 , true , true }, + { 12000 , true , true }, + { 16000 , true , true }, + { 22050 , true , true }, + { 24000 , true , true }, + { 32000 , true , true }, + { 32768 , true , true }, + { 44100 , true , true }, + { 48000 , true , true }, + { 65000 , true , true }, + { 65535 , true , true }, + { 65536 , true , false }, + { 65540 , true , true }, + { 65550 , true , true }, + { 65555 , true , false }, + { 66000 , true , true }, + { 66001 , true , false }, + { 96000 , true , true }, + { 100000 , true , true }, + { 100001 , true , false }, + { 192000 , true , true }, + { 500000 , true , true }, + { 500001 , true , false }, + { 500010 , true , true }, + { 655349 , true , false }, + { 655350 , true , true }, + { 655351 , true , false }, + { 655360 , true , false }, + { 700000 , true , false }, + { 700010 , true , false }, + { 705600 , true , false }, + { 768000 , true , false }, + { 1000000, true , false }, + { 1048575, true , false }, + { 1100000, false, false } +}; + +static struct { + const char *string; + FLAC__bool valid; +} VCENTRY_NAMES[] = { + { "" , true }, + { "a" , true }, + { "=" , false }, + { "a=" , false }, + { "\x01", false }, + { "\x1f", false }, + { "\x7d", true }, + { "\x7e", false }, + { "\xff", false } +}; + +static struct { + uint32_t length; + const FLAC__byte *string; + FLAC__bool valid; +} VCENTRY_VALUES[] = { + { 0, (const FLAC__byte*)"" , true }, + { 1, (const FLAC__byte*)"" , true }, + { 1, (const FLAC__byte*)"\x01" , true }, + { 1, (const FLAC__byte*)"\x7f" , true }, + { 1, (const FLAC__byte*)"\x80" , false }, + { 1, (const FLAC__byte*)"\x81" , false }, + { 1, (const FLAC__byte*)"\xc0" , false }, + { 1, (const FLAC__byte*)"\xe0" , false }, + { 1, (const FLAC__byte*)"\xf0" , false }, + { 2, (const FLAC__byte*)"\xc0\x41" , false }, + { 2, (const FLAC__byte*)"\xc1\x41" , false }, + { 2, (const FLAC__byte*)"\xc0\x85" , false }, /* non-shortest form */ + { 2, (const FLAC__byte*)"\xc1\x85" , false }, /* non-shortest form */ + { 2, (const FLAC__byte*)"\xc2\x85" , true }, + { 2, (const FLAC__byte*)"\xe0\x41" , false }, + { 2, (const FLAC__byte*)"\xe1\x41" , false }, + { 2, (const FLAC__byte*)"\xe0\x85" , false }, + { 2, (const FLAC__byte*)"\xe1\x85" , false }, + { 3, (const FLAC__byte*)"\xe0\x85\x41", false }, + { 3, (const FLAC__byte*)"\xe1\x85\x41", false }, + { 3, (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */ + { 3, (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */ + { 3, (const FLAC__byte*)"\xe0\xa5\x80", true }, + { 3, (const FLAC__byte*)"\xe1\x85\x80", true }, + { 3, (const FLAC__byte*)"\xe1\x95\x80", true }, + { 3, (const FLAC__byte*)"\xe1\xa5\x80", true } +}; + +static struct { + const FLAC__byte *string; + FLAC__bool valid; +} VCENTRY_VALUES_NT[] = { + { (const FLAC__byte*)"" , true }, + { (const FLAC__byte*)"\x01" , true }, + { (const FLAC__byte*)"\x7f" , true }, + { (const FLAC__byte*)"\x80" , false }, + { (const FLAC__byte*)"\x81" , false }, + { (const FLAC__byte*)"\xc0" , false }, + { (const FLAC__byte*)"\xe0" , false }, + { (const FLAC__byte*)"\xf0" , false }, + { (const FLAC__byte*)"\xc0\x41" , false }, + { (const FLAC__byte*)"\xc1\x41" , false }, + { (const FLAC__byte*)"\xc0\x85" , false }, /* non-shortest form */ + { (const FLAC__byte*)"\xc1\x85" , false }, /* non-shortest form */ + { (const FLAC__byte*)"\xc2\x85" , true }, + { (const FLAC__byte*)"\xe0\x41" , false }, + { (const FLAC__byte*)"\xe1\x41" , false }, + { (const FLAC__byte*)"\xe0\x85" , false }, + { (const FLAC__byte*)"\xe1\x85" , false }, + { (const FLAC__byte*)"\xe0\x85\x41", false }, + { (const FLAC__byte*)"\xe1\x85\x41", false }, + { (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */ + { (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */ + { (const FLAC__byte*)"\xe0\xa5\x80", true }, + { (const FLAC__byte*)"\xe1\x85\x80", true }, + { (const FLAC__byte*)"\xe1\x95\x80", true }, + { (const FLAC__byte*)"\xe1\xa5\x80", true } +}; + +static struct { + uint32_t length; + const FLAC__byte *string; + FLAC__bool valid; +} VCENTRIES[] = { + { 0, (const FLAC__byte*)"" , false }, + { 1, (const FLAC__byte*)"a" , false }, + { 1, (const FLAC__byte*)"=" , true }, + { 2, (const FLAC__byte*)"a=" , true }, + { 2, (const FLAC__byte*)"\x01=" , false }, + { 2, (const FLAC__byte*)"\x1f=" , false }, + { 2, (const FLAC__byte*)"\x7d=" , true }, + { 2, (const FLAC__byte*)"\x7e=" , false }, + { 2, (const FLAC__byte*)"\xff=" , false }, + { 3, (const FLAC__byte*)"a=\x01" , true }, + { 3, (const FLAC__byte*)"a=\x7f" , true }, + { 3, (const FLAC__byte*)"a=\x80" , false }, + { 3, (const FLAC__byte*)"a=\x81" , false }, + { 3, (const FLAC__byte*)"a=\xc0" , false }, + { 3, (const FLAC__byte*)"a=\xe0" , false }, + { 3, (const FLAC__byte*)"a=\xf0" , false }, + { 4, (const FLAC__byte*)"a=\xc0\x41" , false }, + { 4, (const FLAC__byte*)"a=\xc1\x41" , false }, + { 4, (const FLAC__byte*)"a=\xc0\x85" , false }, /* non-shortest form */ + { 4, (const FLAC__byte*)"a=\xc1\x85" , false }, /* non-shortest form */ + { 4, (const FLAC__byte*)"a=\xc2\x85" , true }, + { 4, (const FLAC__byte*)"a=\xe0\x41" , false }, + { 4, (const FLAC__byte*)"a=\xe1\x41" , false }, + { 4, (const FLAC__byte*)"a=\xe0\x85" , false }, + { 4, (const FLAC__byte*)"a=\xe1\x85" , false }, + { 5, (const FLAC__byte*)"a=\xe0\x85\x41", false }, + { 5, (const FLAC__byte*)"a=\xe1\x85\x41", false }, + { 5, (const FLAC__byte*)"a=\xe0\x85\x80", false }, /* non-shortest form */ + { 5, (const FLAC__byte*)"a=\xe0\x95\x80", false }, /* non-shortest form */ + { 5, (const FLAC__byte*)"a=\xe0\xa5\x80", true }, + { 5, (const FLAC__byte*)"a=\xe1\x85\x80", true }, + { 5, (const FLAC__byte*)"a=\xe1\x95\x80", true }, + { 5, (const FLAC__byte*)"a=\xe1\xa5\x80", true } +}; + +FLAC__bool test_format(void) +{ + uint32_t i; + + printf("\n+++ libFLAC unit test: format\n\n"); + + for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) { + printf("testing FLAC__format_sample_rate_is_valid(%u)... ", SAMPLE_RATES[i].rate); + if(FLAC__format_sample_rate_is_valid(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].valid) { + printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].valid], true_false_string_[!SAMPLE_RATES[i].valid]); + return false; + } + printf("OK\n"); + } + + for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) { + printf("testing FLAC__format_sample_rate_is_subset(%u)... ", SAMPLE_RATES[i].rate); + if(FLAC__format_sample_rate_is_subset(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].subset) { + printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].subset], true_false_string_[!SAMPLE_RATES[i].subset]); + return false; + } + printf("OK\n"); + } + + for(i = 0; i < sizeof(VCENTRY_NAMES)/sizeof(VCENTRY_NAMES[0]); i++) { + printf("testing FLAC__format_vorbiscomment_entry_name_is_legal(\"%s\")... ", VCENTRY_NAMES[i].string); + if(FLAC__format_vorbiscomment_entry_name_is_legal(VCENTRY_NAMES[i].string) != VCENTRY_NAMES[i].valid) { + printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_NAMES[i].valid], true_false_string_[!VCENTRY_NAMES[i].valid]); + return false; + } + printf("OK\n"); + } + + for(i = 0; i < sizeof(VCENTRY_VALUES)/sizeof(VCENTRY_VALUES[0]); i++) { + printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", %u)... ", VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length); + if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length) != VCENTRY_VALUES[i].valid) { + printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES[i].valid], true_false_string_[!VCENTRY_VALUES[i].valid]); + return false; + } + printf("OK\n"); + } + + for(i = 0; i < sizeof(VCENTRY_VALUES_NT)/sizeof(VCENTRY_VALUES_NT[0]); i++) { + printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", -1)... ", VCENTRY_VALUES_NT[i].string); + if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES_NT[i].string, (uint32_t)(-1)) != VCENTRY_VALUES_NT[i].valid) { + printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES_NT[i].valid], true_false_string_[!VCENTRY_VALUES_NT[i].valid]); + return false; + } + printf("OK\n"); + } + + for(i = 0; i < sizeof(VCENTRIES)/sizeof(VCENTRIES[0]); i++) { + printf("testing FLAC__format_vorbiscomment_entry_is_legal(\"%s\", %u)... ", VCENTRIES[i].string, VCENTRIES[i].length); + if(FLAC__format_vorbiscomment_entry_is_legal(VCENTRIES[i].string, VCENTRIES[i].length) != VCENTRIES[i].valid) { + printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRIES[i].valid], true_false_string_[!VCENTRIES[i].valid]); + return false; + } + printf("OK\n"); + } + + printf("\nPASSED!\n"); + return true; +} diff --git a/src/test_libFLAC/format.h b/src/test_libFLAC/format.h new file mode 100644 index 0000000..f78d55d --- /dev/null +++ b/src/test_libFLAC/format.h @@ -0,0 +1,27 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2004-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_FORMAT_H +#define FLAC__TEST_LIBFLAC_FORMAT_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_format(void); + +#endif diff --git a/src/test_libFLAC/main.c b/src/test_libFLAC/main.c new file mode 100644 index 0000000..a4be0fe --- /dev/null +++ b/src/test_libFLAC/main.c @@ -0,0 +1,64 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2000-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "bitreader.h" +#include "bitwriter.h" +#include "crc.h" +#include "decoders.h" +#include "encoders.h" +#include "endswap.h" +#include "format.h" +#include "metadata.h" +#include "md5.h" + +int main(void) +{ + if(!test_endswap()) + return 1; + + if(!test_crc()) + return 1; + + if(!test_md5()) + return 1; + + if(!test_bitreader()) + return 1; + + if(!test_bitwriter()) + return 1; + + if(!test_format()) + return 1; + + if(!test_encoders()) + return 1; + + if(!test_decoders()) + return 1; + + if(!test_metadata()) + return 1; + + return 0; +} diff --git a/src/test_libFLAC/md5.c b/src/test_libFLAC/md5.c new file mode 100644 index 0000000..bac4a74 --- /dev/null +++ b/src/test_libFLAC/md5.c @@ -0,0 +1,221 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2014-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <string.h> + +#include "FLAC/assert.h" +#include "share/compat.h" +#include "private/md5.h" +#include "md5.h" + + +static FLAC__bool test_md5_clear_context(void); +static FLAC__bool test_md5_codec(void); +static FLAC__bool test_md5_accumulate(const FLAC__int32 * const * signal,uint32_t channels, uint32_t samples, uint32_t bytes_per_sample, const FLAC__byte target_digest [16]); + +FLAC__bool test_md5(void) +{ + printf("\n+++ libFLAC unit test: md5\n\n"); + + if (! test_md5_clear_context()) + return false; + + if (! test_md5_codec()) + return false; + + printf("\nPASSED!\n"); + return true; +} + +/*----------------------------------------------------------------------------*/ + +static FLAC__bool test_md5_clear_context(void) +{ + FLAC__MD5Context ctx; + FLAC__byte digest[16]; + FLAC__byte target[16] = { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e }; + uint32_t k ; + char * cptr; + + printf("testing FLAC__MD5Init ... "); + FLAC__MD5Init (&ctx); + if (ctx.buf[0] != 0x67452301) { + printf("FAILED!\n"); + return false; + } + printf("OK\n"); + + printf("testing that FLAC__MD5Final clears the MD5Context ... "); + FLAC__MD5Final(digest, &ctx); + cptr = (char*) &ctx ; + for (k = 0 ; k < sizeof (ctx) ; k++) { + if (cptr [k]) { + printf("FAILED, MD5 ctx has not been cleared after FLAC__MD5Final\n"); + return false; + } + } + printf("OK\n"); + + printf("testing digest correct for zero data ... "); + if (memcmp(digest, target, sizeof (digest))) { + printf("\nFAILED, expected MD5 sum "); + for (k = 0 ; k < 16 ; k++) + printf("%02x", (target [k] & 0xff)); + printf (" but got "); + for (k = 0 ; k < 16 ; k++) + printf("%02x", (digest [k] & 0xff)); + puts("\n"); + return false; + } + puts("OK"); + + return true; +} + +static FLAC__byte target_digests [8][4][16] = +{ /* 1 channel */ + { /* 1 byte per sample */ + { 0xc1, 0x9a, 0x5b, 0xeb, 0x57, 0x8f, 0x26, 0xeb, 0xfb, 0x34, 0x7c, 0xef, 0x04, 0x31, 0x6d, 0x7d }, + /* 2 bytes per sample */ + { 0xd4, 0x78, 0x90, 0xd3, 0xa9, 0x17, 0x4e, 0x76, 0xca, 0x4d, 0x27, 0x20, 0x98, 0x36, 0x8b, 0x2e }, + /* 3 bytes per sample */ + { 0x5a, 0x4b, 0xd6, 0xac, 0xa1, 0x70, 0x84, 0x19, 0x7c, 0x0d, 0xfb, 0x5b, 0xa9, 0x7b, 0xcb, 0x54 }, + /* 4 bytes per sample */ + { 0x79, 0xd5, 0x7a, 0x32, 0x06, 0x0b, 0xfe, 0x46, 0xa3, 0xe7, 0xba, 0xc5, 0xf7, 0x48, 0x6f, 0x50 } + }, + + /* 2 channels */ + { + { 0x89, 0xac, 0xcf, 0x91, 0xf1, 0x8c, 0xea, 0xab, 0x46, 0x12, 0x74, 0xbc, 0x4e, 0x82, 0xbe, 0x7d }, + { 0xb9, 0x17, 0x16, 0x5b, 0xd8, 0x1c, 0xc8, 0x4e, 0x5a, 0x28, 0xfb, 0xba, 0x87, 0x74, 0x76, 0x44 }, + { 0xec, 0x63, 0x92, 0xca, 0x4f, 0x6b, 0x9e, 0xb1, 0x9f, 0xec, 0x3b, 0x2c, 0x15, 0x30, 0xfd, 0x2a }, + { 0x05, 0x4d, 0xfd, 0xb8, 0x9d, 0x8a, 0xa2, 0xdd, 0x26, 0x47, 0xc6, 0xfb, 0x4f, 0x23, 0x67, 0x6d } + }, + + /* 3 channels */ + { + { 0xad, 0x05, 0xda, 0xf3, 0x7a, 0xa1, 0x94, 0xdb, 0x0c, 0x61, 0x06, 0xb2, 0x94, 0x39, 0x6c, 0xa9 }, + { 0x8b, 0xcc, 0x41, 0x4d, 0xe9, 0xe3, 0xc2, 0x61, 0x61, 0x8a, 0x8b, 0x22, 0xc6, 0x4e, 0xac, 0xa7 }, + { 0x8a, 0xce, 0x97, 0xc1, 0x86, 0xae, 0xbc, 0x73, 0x88, 0x8b, 0x35, 0x5a, 0x37, 0x33, 0xf9, 0xcf }, + { 0x69, 0x59, 0xe8, 0x38, 0x29, 0x80, 0x80, 0x21, 0xb1, 0xd2, 0xba, 0xf6, 0x28, 0xd6, 0x6a, 0x83 } + }, + + /* 4 channels */ + { + { 0x61, 0x40, 0x75, 0xef, 0x22, 0xf1, 0x0f, 0xa6, 0x08, 0x6c, 0x88, 0xff, 0x2c, 0x4e, 0x98, 0x0b }, + { 0xa0, 0x77, 0x3a, 0x59, 0x4a, 0xbf, 0xd0, 0x5c, 0xcc, 0xe3, 0xb9, 0x83, 0x2b, 0xf3, 0xdf, 0x1a }, + { 0xdb, 0xd7, 0xf1, 0x82, 0x13, 0x60, 0x42, 0x7c, 0x84, 0xe6, 0xcf, 0x30, 0xab, 0xa2, 0x64, 0xf1 }, + { 0x4a, 0x9a, 0xad, 0x53, 0x05, 0x74, 0xb1, 0x1c, 0xb8, 0xd4, 0xae, 0x78, 0x13, 0xf6, 0x2a, 0x11 } + }, + + /* 5 channels */ + { + { 0xcc, 0xca, 0x44, 0xc0, 0x54, 0xe2, 0xc9, 0xba, 0x99, 0x32, 0xc9, 0x65, 0xf3, 0x3e, 0x44, 0x34}, + { 0x40, 0x38, 0x6a, 0xdd, 0xde, 0x89, 0x10, 0x3c, 0x8e, 0xec, 0xdf, 0x15, 0x53, 0x4c, 0x2c, 0x92 }, + { 0xc8, 0x95, 0x0a, 0x7c, 0x17, 0x30, 0xc0, 0xac, 0x8e, 0x34, 0xdb, 0x79, 0x76, 0x64, 0x7c, 0x6e }, + { 0x3f, 0x06, 0x11, 0x8a, 0x8d, 0x80, 0xb5, 0x4f, 0x8b, 0xb5, 0x8e, 0xb3, 0x27, 0x3e, 0x41, 0xe8 } + }, + + /* 6 channels */ + { + { 0x61, 0xe4, 0xbd, 0xb1, 0xc0, 0x2f, 0xf4, 0x4c, 0x6e, 0x09, 0x5a, 0xbd, 0x90, 0x18, 0x8b, 0x62 }, + { 0x47, 0xe7, 0x6e, 0x3b, 0x18, 0x86, 0x60, 0x1b, 0x09, 0x62, 0xc6, 0xc9, 0x7c, 0x4c, 0x03, 0xb5 }, + { 0x70, 0x57, 0xbf, 0x67, 0x66, 0x0f, 0xe3, 0x0a, 0x6c, 0xd2, 0x97, 0x66, 0xa2, 0xd2, 0xe4, 0x79 }, + { 0xaa, 0x3f, 0xc7, 0xf5, 0x7a, 0xa5, 0x46, 0xf7, 0xea, 0xe3, 0xd5, 0x1a, 0xa4, 0x62, 0xbe, 0xfa } + }, + + /* 7 channels */ + { + { 0x7c, 0x8d, 0xd2, 0x8c, 0xfd, 0x91, 0xbb, 0x77, 0x6f, 0x0e, 0xf0, 0x39, 0x1f, 0x39, 0xc4, 0xac }, + { 0xfb, 0xab, 0x18, 0x3f, 0x1e, 0x1d, 0xa5, 0x77, 0xe0, 0x5c, 0xea, 0x45, 0x6f, 0x64, 0xa4, 0x64 }, + { 0xe3, 0xac, 0x33, 0x50, 0xc1, 0xb1, 0x93, 0xfb, 0xca, 0x4b, 0x15, 0xcb, 0x2d, 0xcd, 0xd5, 0xef }, + { 0x10, 0xfb, 0x02, 0x83, 0x76, 0x0d, 0xe5, 0xd2, 0x3b, 0xb1, 0x4c, 0x78, 0x3b, 0x73, 0xf7, 0x1a } + }, + + /* 8 channels */ + { + { 0x65, 0x7b, 0xe5, 0x92, 0xe2, 0x1c, 0x95, 0x3e, 0xd7, 0x2f, 0x64, 0xa0, 0x86, 0xec, 0x1a, 0xed }, + { 0x9d, 0x04, 0x8f, 0xa4, 0xea, 0x10, 0xec, 0xb8, 0xa3, 0x88, 0xe2, 0x5d, 0x3c, 0xe2, 0xfb, 0x94 }, + { 0x5a, 0xd3, 0xd2, 0x75, 0x6a, 0xfa, 0xa7, 0x42, 0xf3, 0xbf, 0x0e, 0xbc, 0x90, 0x2a, 0xf8, 0x5f }, + { 0x76, 0xe1, 0xe5, 0xf6, 0xe3, 0x44, 0x08, 0x29, 0xae, 0x79, 0x19, 0xeb, 0xa8, 0x57, 0x16, 0x2a } + } +}; + +#define MAX_CHANNEL_COUNT 8 +#define MD5_SAMPLE_COUNT 64 + +static FLAC__bool test_md5_codec(void) +{ + FLAC__int32 arrays[MAX_CHANNEL_COUNT][MD5_SAMPLE_COUNT], *pointer[MAX_CHANNEL_COUNT], **signal; + uint32_t chan, byte_size, seed = 0x12345679; + + /* Set up signal data using a trivial Linear Congruent PRNG. */ + signal = &pointer[0]; + for (chan = 0 ; chan < MAX_CHANNEL_COUNT ; chan ++) { + uint32_t k; + pointer[chan] = arrays [chan]; + for (k = 0 ; k < MD5_SAMPLE_COUNT ; k++) { + seed = seed * 1103515245 + 12345; + arrays[chan][k] = seed; + } + } + + for (chan = 1 ; chan <= MAX_CHANNEL_COUNT ; chan ++) { + for (byte_size = 1 ; byte_size <= 4 ; byte_size ++) { + if (! test_md5_accumulate((const FLAC__int32 * const *) signal, chan, MD5_SAMPLE_COUNT, byte_size, target_digests[chan-1][byte_size-1])) + return false; + } + } + + return true; +} + +static FLAC__bool test_md5_accumulate(const FLAC__int32 * const * signal, uint32_t channels, uint32_t samples, uint32_t bytes_per_sample, const FLAC__byte target_digest [16]) +{ + FLAC__MD5Context ctx; + FLAC__byte digest[16]; + + memset(&ctx, 0, sizeof (ctx)); + + printf("testing FLAC__MD5Accumulate (samples=%u, channels=%u, bytes_per_sample=%u) ... ", samples, channels, bytes_per_sample); + + FLAC__MD5Init(&ctx); + FLAC__MD5Accumulate(&ctx, signal, channels, samples, bytes_per_sample); + FLAC__MD5Final(digest, &ctx); + + if (memcmp(digest, target_digest, sizeof (digest))) { + int k ; + + printf("\nFAILED, expected MD5 sum "); + for (k = 0 ; k < 16 ; k++) + printf("%02x", (target_digest [k] & 0xff)); + printf (" but got "); + for (k = 0 ; k < 16 ; k++) + printf("%02x", (digest [k] & 0xff)); + puts("\n"); + return false; + } + + printf("OK\n"); + return true; +} diff --git a/src/test_libFLAC/md5.h b/src/test_libFLAC/md5.h new file mode 100644 index 0000000..6863268 --- /dev/null +++ b/src/test_libFLAC/md5.h @@ -0,0 +1,26 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2014-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_MD5_H +#define FLAC__TEST_LIBFLAC_MD5_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_md5(void); + +#endif diff --git a/src/test_libFLAC/metadata.c b/src/test_libFLAC/metadata.c new file mode 100644 index 0000000..0347f6d --- /dev/null +++ b/src/test_libFLAC/metadata.c @@ -0,0 +1,41 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "metadata.h" +#include <stdio.h> + +extern FLAC__bool test_metadata_object(void); +extern FLAC__bool test_metadata_file_manipulation(void); + +FLAC__bool test_metadata(void) +{ + if(!test_metadata_object()) + return false; + + if(!test_metadata_file_manipulation()) + return false; + + printf("\nPASSED!\n"); + + return true; +} diff --git a/src/test_libFLAC/metadata.h b/src/test_libFLAC/metadata.h new file mode 100644 index 0000000..51bdf7a --- /dev/null +++ b/src/test_libFLAC/metadata.h @@ -0,0 +1,29 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef FLAC__TEST_LIBFLAC_METADATA_H +#define FLAC__TEST_LIBFLAC_METADATA_H + +#include "FLAC/ordinals.h" + +FLAC__bool test_metadata(void); +FLAC__bool test_metadata_file_manipulation(void); +FLAC__bool test_metadata_object(void); + +#endif diff --git a/src/test_libFLAC/metadata_manip.c b/src/test_libFLAC/metadata_manip.c new file mode 100644 index 0000000..334dc3a --- /dev/null +++ b/src/test_libFLAC/metadata_manip.c @@ -0,0 +1,2146 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> /* for malloc() */ +#include <string.h> /* for memcpy()/memset() */ +#if defined _MSC_VER || defined __MINGW32__ +#include <sys/utime.h> /* for utime() */ +#include <io.h> /* for chmod() */ +#else +#include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */ +#include <unistd.h> /* for chown(), unlink() */ +#endif +#include <sys/stat.h> /* for stat(), maybe chmod() */ +#include "FLAC/assert.h" +#include "FLAC/stream_decoder.h" +#include "FLAC/metadata.h" +#include "share/grabbag.h" +#include "share/compat.h" +#include "share/macros.h" +#include "share/safe_str.h" +#include "test_libs_common/file_utils_flac.h" +#include "test_libs_common/metadata_utils.h" +#include "metadata.h" + + +/****************************************************************************** + The general strategy of these tests (for interface levels 1 and 2) is + to create a dummy FLAC file with a known set of initial metadata + blocks, then keep a mirror locally of what we expect the metadata to be + after each operation. Then testing becomes a simple matter of running + a FLAC__StreamDecoder over the dummy file after each operation, comparing + the decoded metadata to what's in our local copy. If there are any + differences in the metadata, or the actual audio data is corrupted, we + will catch it while decoding. +******************************************************************************/ + +typedef struct { + FLAC__bool error_occurred; +} decoder_client_struct; + +typedef struct { + FLAC__StreamMetadata *blocks[64]; + uint32_t num_blocks; +} our_metadata_struct; + +/* our copy of the metadata in flacfilename() */ +static our_metadata_struct our_metadata_; + +/* the current block number that corresponds to the position of the iterator we are testing */ +static uint32_t mc_our_block_number_ = 0; + +static const char *flacfilename(FLAC__bool is_ogg) +{ + return is_ogg? "metadata.oga" : "metadata.flac"; +} + +static FLAC__bool die_(const char *msg) +{ + printf("ERROR: %s\n", msg); + return false; +} + +static FLAC__bool die_c_(const char *msg, FLAC__Metadata_ChainStatus status) +{ + printf("ERROR: %s\n", msg); + printf(" status=%s\n", FLAC__Metadata_ChainStatusString[status]); + return false; +} + +static FLAC__bool die_ss_(const char *msg, FLAC__Metadata_SimpleIterator *iterator) +{ + printf("ERROR: %s\n", msg); + printf(" status=%s\n", FLAC__Metadata_SimpleIteratorStatusString[FLAC__metadata_simple_iterator_status(iterator)]); + 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", (uint32_t)size); + exit(1); + } + return x; +} + +static char *strdup_or_die_(const char *s) +{ + char *x = strdup(s); + if(0 == x) { + fprintf(stderr, "ERROR: out of memory copying string \"%s\"\n", s); + exit(1); + } + return x; +} + +/* functions for working with our metadata copy */ + +static FLAC__bool replace_in_our_metadata_(FLAC__StreamMetadata *block, uint32_t position, FLAC__bool copy) +{ + uint32_t i; + FLAC__StreamMetadata *obj = block; + FLAC__ASSERT(position < our_metadata_.num_blocks); + if(copy) { + if(0 == (obj = FLAC__metadata_object_clone(block))) + return die_("during FLAC__metadata_object_clone()"); + } + FLAC__metadata_object_delete(our_metadata_.blocks[position]); + our_metadata_.blocks[position] = obj; + + /* set the is_last flags */ + for(i = 0; i < our_metadata_.num_blocks - 1; i++) + our_metadata_.blocks[i]->is_last = false; + our_metadata_.blocks[i]->is_last = true; + + return true; +} + +static FLAC__bool insert_to_our_metadata_(FLAC__StreamMetadata *block, uint32_t position, FLAC__bool copy) +{ + uint32_t i; + FLAC__StreamMetadata *obj = block; + if(copy) { + if(0 == (obj = FLAC__metadata_object_clone(block))) + return die_("during FLAC__metadata_object_clone()"); + } + if(position > our_metadata_.num_blocks) { + position = our_metadata_.num_blocks; + } + else { + for(i = our_metadata_.num_blocks; i > position; i--) + our_metadata_.blocks[i] = our_metadata_.blocks[i-1]; + } + our_metadata_.blocks[position] = obj; + our_metadata_.num_blocks++; + + /* set the is_last flags */ + for(i = 0; i < our_metadata_.num_blocks - 1; i++) + our_metadata_.blocks[i]->is_last = false; + our_metadata_.blocks[i]->is_last = true; + + return true; +} + +static void delete_from_our_metadata_(uint32_t position) +{ + uint32_t i; + FLAC__ASSERT(position < our_metadata_.num_blocks); + FLAC__metadata_object_delete(our_metadata_.blocks[position]); + for(i = position; i < our_metadata_.num_blocks - 1; i++) + our_metadata_.blocks[i] = our_metadata_.blocks[i+1]; + our_metadata_.num_blocks--; + + /* set the is_last flags */ + if(our_metadata_.num_blocks > 0) { + for(i = 0; i < our_metadata_.num_blocks - 1; i++) + our_metadata_.blocks[i]->is_last = false; + our_metadata_.blocks[i]->is_last = true; + } +} + +/* + * This wad of functions supports filename- and callback-based chain reading/writing. + * Everything up to set_file_stats_() is copied from libFLAC/metadata_iterators.c + */ +static FLAC__bool open_tempfile_(const char *filename, FILE **tempfile, char **tempfilename) +{ + static const char *tempfile_suffix = ".metadata_edit"; + size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1; + + *tempfilename = malloc(dest_len); + if (*tempfilename == NULL) + return false; + safe_strncpy(*tempfilename, filename, dest_len); + safe_strncat(*tempfilename, tempfile_suffix, dest_len); + + *tempfile = flac_fopen(*tempfilename, "wb"); + if (*tempfile == NULL) + return false; + + return true; +} + +static void cleanup_tempfile_(FILE **tempfile, char **tempfilename) +{ + if (*tempfile != NULL) { + (void)fclose(*tempfile); + *tempfile = 0; + } + + if (*tempfilename != NULL) { + (void)flac_unlink(*tempfilename); + free(*tempfilename); + *tempfilename = 0; + } +} + +static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename) +{ + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != tempfile); + FLAC__ASSERT(0 != tempfilename); + FLAC__ASSERT(0 != *tempfilename); + + if(0 != *tempfile) { + (void)fclose(*tempfile); + *tempfile = 0; + } + +#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__ + /* on some flavors of windows, flac_rename() will fail if the destination already exists */ + if(flac_unlink(filename) < 0) { + cleanup_tempfile_(tempfile, tempfilename); + return false; + } +#endif + + if(0 != flac_rename(*tempfilename, filename)) { + cleanup_tempfile_(tempfile, tempfilename); + return false; + } + + cleanup_tempfile_(tempfile, tempfilename); + + return true; +} + +static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats) +{ + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != stats); + return (0 == flac_stat(filename, stats)); +} + +static void set_file_stats_(const char *filename, struct flac_stat_s *stats) +{ +#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) && !defined(_WIN32) + struct timespec srctime[2] = {}; + srctime[0].tv_sec = stats->st_atime; + srctime[1].tv_sec = stats->st_mtime; +#else + struct utimbuf srctime; + srctime.actime = stats->st_atime; + srctime.modtime = stats->st_mtime; +#endif + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != stats); + + (void)flac_chmod(filename, stats->st_mode); + (void)flac_utime(filename, &srctime); +#if !defined _MSC_VER && !defined __MINGW32__ + FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1)); + FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid)); +#endif +} + +#ifdef FLAC__VALGRIND_TESTING +static size_t chain_write_cb_(const void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle) +{ + FILE *stream = (FILE*)handle; + size_t ret = fwrite(ptr, size, nmemb, stream); + if(!ferror(stream)) + fflush(stream); + return ret; +} +#endif + +static int chain_seek_cb_(FLAC__IOHandle handle, FLAC__int64 offset, int whence) +{ + FLAC__off_t o = (FLAC__off_t)offset; + FLAC__ASSERT(offset == o); + return fseeko((FILE*)handle, o, whence); +} + +static FLAC__int64 chain_tell_cb_(FLAC__IOHandle handle) +{ + return ftello((FILE*)handle); +} + +static int chain_eof_cb_(FLAC__IOHandle handle) +{ + return feof((FILE*)handle); +} + +static FLAC__bool write_chain_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats, FLAC__bool filename_based, const char *filename) +{ + if(filename_based) + return FLAC__metadata_chain_write(chain, use_padding, preserve_file_stats); + else { + FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (FLAC__IOCallback_Read)fread; +#ifdef FLAC__VALGRIND_TESTING + callbacks.write = chain_write_cb_; +#else + callbacks.write = (FLAC__IOCallback_Write)fwrite; +#endif + callbacks.seek = chain_seek_cb_; + callbacks.eof = chain_eof_cb_; + + if(FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) { + struct flac_stat_s stats; + FILE *file, *tempfile = 0; + char *tempfilename; + if(preserve_file_stats) { + if(!get_file_stats_(filename, &stats)) + return false; + } + if(0 == (file = flac_fopen(filename, "rb"))) + return false; /*@@@@ chain status still says OK though */ + if(!open_tempfile_(filename, &tempfile, &tempfilename)) { + fclose(file); + cleanup_tempfile_(&tempfile, &tempfilename); + return false; /*@@@@ chain status still says OK though */ + } + if(!FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain, use_padding, (FLAC__IOHandle)file, callbacks, (FLAC__IOHandle)tempfile, callbacks)) { + fclose(file); + fclose(tempfile); + return false; + } + fclose(file); + fclose(tempfile); + file = tempfile = 0; + if(!transport_tempfile_(filename, &tempfile, &tempfilename)) + return false; + if(preserve_file_stats) + set_file_stats_(filename, &stats); + } + else { + FILE *file = flac_fopen(filename, "r+b"); + if(0 == file) + return false; /*@@@@ chain status still says OK though */ + if(!FLAC__metadata_chain_write_with_callbacks(chain, use_padding, (FLAC__IOHandle)file, callbacks)) + return false; + fclose(file); + } + } + + return true; +} + +static FLAC__bool read_chain_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool filename_based, FLAC__bool is_ogg) +{ + if(filename_based) + return is_ogg? + FLAC__metadata_chain_read_ogg(chain, flacfilename(is_ogg)) : + FLAC__metadata_chain_read(chain, flacfilename(is_ogg)) + ; + else { + FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (FLAC__IOCallback_Read)fread; + callbacks.seek = chain_seek_cb_; + callbacks.tell = chain_tell_cb_; + + { + FLAC__bool ret; + FILE *file = flac_fopen(filename, "rb"); + if(0 == file) + return false; /*@@@@ chain status still says OK though */ + ret = is_ogg? + FLAC__metadata_chain_read_ogg_with_callbacks(chain, (FLAC__IOHandle)file, callbacks) : + FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks) + ; + fclose(file); + return ret; + } + } +} + +/* function for comparing our metadata to a FLAC__Metadata_Chain */ + +static FLAC__bool compare_chain_(FLAC__Metadata_Chain *chain, uint32_t current_position, FLAC__StreamMetadata *current_block) +{ + uint32_t i; + FLAC__Metadata_Iterator *iterator; + FLAC__StreamMetadata *block; + FLAC__bool next_ok = true; + + FLAC__ASSERT(0 != chain); + + printf("\tcomparing chain... "); + fflush(stdout); + + if(0 == (iterator = FLAC__metadata_iterator_new())) + return die_("allocating memory for iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + i = 0; + do { + printf("%u... ", i); + fflush(stdout); + + if(0 == (block = FLAC__metadata_iterator_get_block(iterator))) { + FLAC__metadata_iterator_delete(iterator); + return die_("getting block from iterator"); + } + + if(!mutils__compare_block(our_metadata_.blocks[i], block)) { + FLAC__metadata_iterator_delete(iterator); + return die_("metadata block mismatch"); + } + + i++; + next_ok = FLAC__metadata_iterator_next(iterator); + } while(i < our_metadata_.num_blocks && next_ok); + + FLAC__metadata_iterator_delete(iterator); + + if(next_ok) + return die_("chain has more blocks than expected"); + + if(i < our_metadata_.num_blocks) + return die_("short block count in chain"); + + if(0 != current_block) { + printf("CURRENT_POSITION... "); + fflush(stdout); + + if(!mutils__compare_block(our_metadata_.blocks[current_position], current_block)) + return die_("metadata block mismatch"); + } + + printf("PASSED\n"); + + return true; +} + +/* decoder callbacks for checking the file */ + +static FLAC__StreamDecoderWriteStatus decoder_write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) +{ + (void)decoder, (void)buffer, (void)client_data; + + if( + (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) || + (frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0) + ) { + printf("content... "); + fflush(stdout); + } + + return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; +} + +/* this version pays no attention to the metadata */ +static void decoder_metadata_callback_null_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) +{ + (void)decoder, (void)metadata, (void)client_data; + + printf("%u... ", mc_our_block_number_); + fflush(stdout); + + mc_our_block_number_++; +} + +/* this version is used when we want to compare to our metadata copy */ +static void decoder_metadata_callback_compare_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) +{ + decoder_client_struct *dcd = (decoder_client_struct*)client_data; + + (void)decoder; + + /* don't bother checking if we've already hit an error */ + if(dcd->error_occurred) + return; + + printf("%u... ", mc_our_block_number_); + fflush(stdout); + + if(mc_our_block_number_ >= our_metadata_.num_blocks) { + (void)die_("got more metadata blocks than expected"); + dcd->error_occurred = true; + } + else { + if(!mutils__compare_block(our_metadata_.blocks[mc_our_block_number_], metadata)) { + (void)die_("metadata block mismatch"); + dcd->error_occurred = true; + } + } + mc_our_block_number_++; +} + +static void decoder_error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) +{ + decoder_client_struct *dcd = (decoder_client_struct*)client_data; + (void)decoder; + + dcd->error_occurred = true; + printf("ERROR: got error callback, status = %s (%u)\n", FLAC__StreamDecoderErrorStatusString[status], (uint32_t)status); +} + +static FLAC__bool generate_file_(FLAC__bool include_extras, FLAC__bool is_ogg) +{ + FLAC__StreamMetadata streaminfo, vorbiscomment, *cuesheet, picture, padding; + FLAC__StreamMetadata *metadata[4]; + uint32_t i = 0, n = 0; + + printf("generating %sFLAC file for test\n", is_ogg? "Ogg " : ""); + + while(our_metadata_.num_blocks > 0) + delete_from_our_metadata_(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); + + { + const uint32_t vendor_string_length = (uint32_t)strlen(FLAC__VENDOR_STRING); + vorbiscomment.is_last = false; + vorbiscomment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT; + vorbiscomment.length = (4 + vendor_string_length) + 4; + vorbiscomment.data.vorbis_comment.vendor_string.length = vendor_string_length; + vorbiscomment.data.vorbis_comment.vendor_string.entry = malloc_or_die_(vendor_string_length+1); + memcpy(vorbiscomment.data.vorbis_comment.vendor_string.entry, FLAC__VENDOR_STRING, vendor_string_length+1); + vorbiscomment.data.vorbis_comment.num_comments = 0; + vorbiscomment.data.vorbis_comment.comments = 0; + } + + { + if (0 == (cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET))) + return die_("priming our metadata"); + cuesheet->is_last = false; + safe_strncpy(cuesheet->data.cue_sheet.media_catalog_number, "bogo-MCN", sizeof(cuesheet->data.cue_sheet.media_catalog_number)); + cuesheet->data.cue_sheet.lead_in = 123; + cuesheet->data.cue_sheet.is_cd = false; + if (!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, 0)) + return die_("priming our metadata"); + cuesheet->data.cue_sheet.tracks[0].number = 1; + if (!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, 0, 0)) + return die_("priming our metadata"); + } + + { + picture.is_last = false; + picture.type = FLAC__METADATA_TYPE_PICTURE; + picture.length = + ( + FLAC__STREAM_METADATA_PICTURE_TYPE_LEN + + FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* will add the length for the string later */ + FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* will add the length for the string later */ + FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN + + FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN + + FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN + + FLAC__STREAM_METADATA_PICTURE_COLORS_LEN + + FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN /* will add the length for the data later */ + ) / 8 + ; + picture.data.picture.type = FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER; + picture.data.picture.mime_type = strdup_or_die_("image/jpeg"); + picture.length += strlen(picture.data.picture.mime_type); + picture.data.picture.description = (FLAC__byte*)strdup_or_die_("desc"); + picture.length += strlen((const char *)picture.data.picture.description); + picture.data.picture.width = 300; + picture.data.picture.height = 300; + picture.data.picture.depth = 24; + picture.data.picture.colors = 0; + picture.data.picture.data = (FLAC__byte*)strdup_or_die_("SOMEJPEGDATA"); + picture.data.picture.data_length = strlen((const char *)picture.data.picture.data); + picture.length += picture.data.picture.data_length; + } + + padding.is_last = true; + padding.type = FLAC__METADATA_TYPE_PADDING; + padding.length = 1234; + + metadata[n++] = &vorbiscomment; + if(include_extras) { + metadata[n++] = cuesheet; + metadata[n++] = &picture; + } + metadata[n++] = &padding; + + if( + !insert_to_our_metadata_(&streaminfo, i++, /*copy=*/true) || + !insert_to_our_metadata_(&vorbiscomment, i++, /*copy=*/true) || + (include_extras && !insert_to_our_metadata_(cuesheet, i++, /*copy=*/false)) || + (include_extras && !insert_to_our_metadata_(&picture, i++, /*copy=*/true)) || + !insert_to_our_metadata_(&padding, i++, /*copy=*/true) + ) + return die_("priming our metadata"); + + if(!file_utils__generate_flacfile(is_ogg, flacfilename(is_ogg), 0, 512 * 1024, &streaminfo, metadata, n)) + return die_("creating the encoded file"); + + free(vorbiscomment.data.vorbis_comment.vendor_string.entry); + free(picture.data.picture.mime_type); + free(picture.data.picture.description); + free(picture.data.picture.data); + if(!include_extras) + FLAC__metadata_object_delete(cuesheet); + + return true; +} + +static FLAC__bool test_file_(FLAC__bool is_ogg, FLAC__StreamDecoderMetadataCallback metadata_callback) +{ + const char *filename = flacfilename(is_ogg); + FLAC__StreamDecoder *decoder; + decoder_client_struct decoder_client_data; + + FLAC__ASSERT(0 != metadata_callback); + + mc_our_block_number_ = 0; + decoder_client_data.error_occurred = false; + + printf("\ttesting '%s'... ", filename); + fflush(stdout); + + if(0 == (decoder = FLAC__stream_decoder_new())) + return die_("couldn't allocate decoder instance"); + + FLAC__stream_decoder_set_md5_checking(decoder, true); + FLAC__stream_decoder_set_metadata_respond_all(decoder); + if( + (is_ogg? + FLAC__stream_decoder_init_ogg_file(decoder, filename, decoder_write_callback_, metadata_callback, decoder_error_callback_, &decoder_client_data) : + FLAC__stream_decoder_init_file(decoder, filename, decoder_write_callback_, metadata_callback, decoder_error_callback_, &decoder_client_data) + ) != FLAC__STREAM_DECODER_INIT_STATUS_OK + ) { + (void)FLAC__stream_decoder_finish(decoder); + FLAC__stream_decoder_delete(decoder); + return die_("initializing decoder\n"); + } + if(!FLAC__stream_decoder_process_until_end_of_stream(decoder)) { + (void)FLAC__stream_decoder_finish(decoder); + FLAC__stream_decoder_delete(decoder); + return die_("decoding file\n"); + } + + (void)FLAC__stream_decoder_finish(decoder); + FLAC__stream_decoder_delete(decoder); + + if(decoder_client_data.error_occurred) + return false; + + if(mc_our_block_number_ != our_metadata_.num_blocks) + return die_("short metadata block count"); + + printf("PASSED\n"); + return true; +} + +static FLAC__bool change_stats_(const char *filename, FLAC__bool read_only) +{ + if(!grabbag__file_change_stats(filename, read_only)) + return die_("during grabbag__file_change_stats()"); + + return true; +} + +static FLAC__bool remove_file_(const char *filename) +{ + while(our_metadata_.num_blocks > 0) + delete_from_our_metadata_(0); + + if(!grabbag__file_remove_file(filename)) + return die_("removing file"); + + return true; +} + +static FLAC__bool test_level_0_(void) +{ + FLAC__StreamMetadata streaminfo; + FLAC__StreamMetadata *tags = 0; + FLAC__StreamMetadata *cuesheet = 0; + FLAC__StreamMetadata *picture = 0; + + printf("\n\n++++++ testing level 0 interface\n"); + + if(!generate_file_(/*include_extras=*/true, /*is_ogg=*/false)) + return false; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_null_)) + return false; + + printf("testing FLAC__metadata_get_streaminfo()... "); + + if(!FLAC__metadata_get_streaminfo(flacfilename(/*is_ogg=*/false), &streaminfo)) + return die_("during FLAC__metadata_get_streaminfo()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(streaminfo.data.stream_info.channels != 1) + return die_("mismatch in streaminfo.data.stream_info.channels"); + if(streaminfo.data.stream_info.bits_per_sample != 8) + return die_("mismatch in streaminfo.data.stream_info.bits_per_sample"); + if(streaminfo.data.stream_info.sample_rate != 44100) + return die_("mismatch in streaminfo.data.stream_info.sample_rate"); + if(streaminfo.data.stream_info.min_blocksize != 576) + return die_("mismatch in streaminfo.data.stream_info.min_blocksize"); + if(streaminfo.data.stream_info.max_blocksize != 576) + return die_("mismatch in streaminfo.data.stream_info.max_blocksize"); + + printf("OK\n"); + + printf("testing FLAC__metadata_get_tags()... "); + + if(!FLAC__metadata_get_tags(flacfilename(/*is_ogg=*/false), &tags)) + return die_("during FLAC__metadata_get_tags()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(tags->data.vorbis_comment.num_comments != 0) + return die_("mismatch in tags->data.vorbis_comment.num_comments"); + + printf("OK\n"); + + FLAC__metadata_object_delete(tags); + + printf("testing FLAC__metadata_get_cuesheet()... "); + + if(!FLAC__metadata_get_cuesheet(flacfilename(/*is_ogg=*/false), &cuesheet)) + return die_("during FLAC__metadata_get_cuesheet()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(cuesheet->data.cue_sheet.lead_in != 123) + return die_("mismatch in cuesheet->data.cue_sheet.lead_in"); + + printf("OK\n"); + + FLAC__metadata_object_delete(cuesheet); + + printf("testing FLAC__metadata_get_picture()... "); + + if(!FLAC__metadata_get_picture(flacfilename(/*is_ogg=*/false), &picture, /*type=*/(FLAC__StreamMetadata_Picture_Type)(-1), /*mime_type=*/0, /*description=*/0, /*max_width=*/(uint32_t)(-1), /*max_height=*/(uint32_t)(-1), /*max_depth=*/(uint32_t)(-1), /*max_colors=*/(uint32_t)(-1))) + return die_("during FLAC__metadata_get_picture()"); + + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(picture->data.picture.type != FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER) + return die_("mismatch in picture->data.picture.type"); + + printf("OK\n"); + + FLAC__metadata_object_delete(picture); + + if(!remove_file_(flacfilename(/*is_ogg=*/false))) + return false; + + return true; +} + +static FLAC__bool test_level_1_(void) +{ + FLAC__Metadata_SimpleIterator *iterator; + FLAC__StreamMetadata *block, *app, *padding; + FLAC__byte data[1000]; + uint32_t our_current_position = 0; + + /* initialize 'data' to avoid Valgrind errors */ + memset(data, 0, sizeof(data)); + + printf("\n\n++++++ testing level 1 interface\n"); + + /************************************************************/ + + printf("simple iterator on read-only file\n"); + + if(!generate_file_(/*include_extras=*/false, /*is_ogg=*/false)) + return false; + + if(!change_stats_(flacfilename(/*is_ogg=*/false), /*read_only=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_null_)) + return false; + + if(0 == (iterator = FLAC__metadata_simple_iterator_new())) + return die_("FLAC__metadata_simple_iterator_new()"); + + if(!FLAC__metadata_simple_iterator_init(iterator, flacfilename(/*is_ogg=*/false), /*read_only=*/false, /*preserve_file_stats=*/false)) + return die_("FLAC__metadata_simple_iterator_init() returned false"); + + printf("is writable = %u\n", (uint32_t)FLAC__metadata_simple_iterator_is_writable(iterator)); + if(FLAC__metadata_simple_iterator_is_writable(iterator)) + return die_("iterator claims file is writable when tester thinks it should not be; are you running as root?\n"); + + printf("iterate forwards\n"); + + if(FLAC__metadata_simple_iterator_get_block_type(iterator) != FLAC__METADATA_TYPE_STREAMINFO) + return die_("expected STREAMINFO type from FLAC__metadata_simple_iterator_get_block_type()"); + if(0 == (block = FLAC__metadata_simple_iterator_get_block(iterator))) + return die_("getting block 0"); + if(block->type != FLAC__METADATA_TYPE_STREAMINFO) + return die_("expected STREAMINFO type"); + if(block->is_last) + return die_("expected is_last to be false"); + if(block->length != FLAC__STREAM_METADATA_STREAMINFO_LENGTH) + return die_("bad STREAMINFO length"); + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(block->data.stream_info.channels != 1) + return die_("mismatch in channels"); + if(block->data.stream_info.bits_per_sample != 8) + return die_("mismatch in bits_per_sample"); + if(block->data.stream_info.sample_rate != 44100) + return die_("mismatch in sample_rate"); + if(block->data.stream_info.min_blocksize != 576) + return die_("mismatch in min_blocksize"); + if(block->data.stream_info.max_blocksize != 576) + return die_("mismatch in max_blocksize"); + FLAC__metadata_object_delete(block); + + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("forward iterator ended early"); + our_current_position++; + + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("forward iterator ended early"); + our_current_position++; + + if(FLAC__metadata_simple_iterator_get_block_type(iterator) != FLAC__METADATA_TYPE_PADDING) + return die_("expected PADDING type from FLAC__metadata_simple_iterator_get_block_type()"); + if(0 == (block = FLAC__metadata_simple_iterator_get_block(iterator))) + return die_("getting block 2"); + if(block->type != FLAC__METADATA_TYPE_PADDING) + return die_("expected PADDING type"); + if(!block->is_last) + return die_("expected is_last to be true"); + /* check to see if some basic data matches (c.f. generate_file_()) */ + if(block->length != 1234) + return die_("bad PADDING length"); + FLAC__metadata_object_delete(block); + + if(FLAC__metadata_simple_iterator_next(iterator)) + return die_("forward iterator returned true but should have returned false"); + + printf("iterate backwards\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("reverse iterator ended early"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("reverse iterator ended early"); + if(FLAC__metadata_simple_iterator_prev(iterator)) + return die_("reverse iterator returned true but should have returned false"); + + printf("testing FLAC__metadata_simple_iterator_set_block() on read-only file...\n"); + + if(!FLAC__metadata_simple_iterator_set_block(iterator, (FLAC__StreamMetadata*)99, false)) + printf("OK: FLAC__metadata_simple_iterator_set_block() returned false like it should\n"); + else + return die_("FLAC__metadata_simple_iterator_set_block() returned true but shouldn't have"); + + FLAC__metadata_simple_iterator_delete(iterator); + + /************************************************************/ + + printf("simple iterator on writable file\n"); + + if(!change_stats_(flacfilename(/*is_ogg=*/false), /*read-only=*/false)) + return false; + + printf("creating APPLICATION block\n"); + + if(0 == (app = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION))) + return die_("FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)"); + memcpy(app->data.application.id, "duh", (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); + + printf("creating PADDING block\n"); + + if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) + return die_("FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)"); + padding->length = 20; + + if(0 == (iterator = FLAC__metadata_simple_iterator_new())) + return die_("FLAC__metadata_simple_iterator_new()"); + + if(!FLAC__metadata_simple_iterator_init(iterator, flacfilename(/*is_ogg=*/false), /*read_only=*/false, /*preserve_file_stats=*/false)) + return die_("FLAC__metadata_simple_iterator_init() returned false"); + our_current_position = 0; + + printf("is writable = %u\n", (uint32_t)FLAC__metadata_simple_iterator_is_writable(iterator)); + + printf("[S]VP\ttry to write over STREAMINFO block...\n"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, false)) + printf("\tFLAC__metadata_simple_iterator_set_block() returned false like it should\n"); + else + return die_("FLAC__metadata_simple_iterator_set_block() returned true but shouldn't have"); + + if(FLAC__metadata_simple_iterator_status(iterator) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT) + return die_("FLAC__metadata_simple_iterator_status() should have been FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT"); + + printf("[S]VP\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]P\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]\tinsert PADDING after, don't expand into padding\n"); + padding->length = 25; + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + printf("SVP[P]\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SV[P]P\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]PP\tinsert PADDING after, don't expand into padding\n"); + padding->length = 30; + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[P]PP\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]PPP\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("[S]VPPP\tdelete (STREAMINFO block), must fail\n"); + if(FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false) should have returned false", iterator); + + if(FLAC__metadata_simple_iterator_status(iterator) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT) + return die_("FLAC__metadata_simple_iterator_status() should have been FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT"); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("[S]VPPP\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]PPP\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]PP\tdelete (middle block), replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, true)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, true)", iterator); + our_current_position--; + + printf("S[V]PPP\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]PP\tdelete (middle block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("S[V]PP\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]P\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVP[P]\tdelete (last block), replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, true)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + our_current_position--; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[P]P\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVP[P]\tdelete (last block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[P]\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]P\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("[S]VP\tset STREAMINFO (change sample rate)\n"); + FLAC__ASSERT(our_current_position == 0); + block = FLAC__metadata_simple_iterator_get_block(iterator); + block->data.stream_info.sample_rate = 32000; + if(!replace_in_our_metadata_(block, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, block, false)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, block, false)", iterator); + FLAC__metadata_object_delete(block); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("[S]VP\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]P\tinsert APPLICATION after, expand into padding of exceeding size\n"); + app->data.application.id[0] = 'e'; /* twiddle the id so that our comparison doesn't miss transposition */ + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)", iterator); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return false; + our_metadata_.blocks[our_current_position+1]->length -= (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) + app->length; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]P\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVA[P]\tset APPLICATION, expand into padding of exceeding size\n"); + app->data.application.id[0] = 'f'; /* twiddle the id */ + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, true)", iterator); + if(!insert_to_our_metadata_(app, our_current_position, /*copy=*/true)) + return false; + our_metadata_.blocks[our_current_position+1]->length -= (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) + app->length; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVA[A]P\tset APPLICATION (grow), don't expand into padding\n"); + app->data.application.id[0] = 'g'; /* twiddle the id */ + if(!FLAC__metadata_object_application_set_data(app, data, sizeof(data), true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, false)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVA[A]P\tset APPLICATION (shrink), don't fill in with padding\n"); + app->data.application.id[0] = 'h'; /* twiddle the id */ + if(!FLAC__metadata_object_application_set_data(app, data, 12, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, false)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVA[A]P\tset APPLICATION (grow), expand into padding of exceeding size\n"); + app->data.application.id[0] = 'i'; /* twiddle the id */ + if(!FLAC__metadata_object_application_set_data(app, data, sizeof(data), true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + our_metadata_.blocks[our_current_position+1]->length -= (sizeof(data) - 12); + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVA[A]P\tset APPLICATION (shrink), fill in with padding\n"); + app->data.application.id[0] = 'j'; /* twiddle the id */ + if(!FLAC__metadata_object_application_set_data(app, data, 23, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!insert_to_our_metadata_(padding, our_current_position+1, /*copy=*/true)) + return die_("copying object"); + our_metadata_.blocks[our_current_position+1]->length = sizeof(data) - 23 - FLAC__STREAM_METADATA_HEADER_LENGTH; + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVA[A]PP\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVAA[P]P\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVAAP[P]\tset PADDING (shrink), don't fill in with padding\n"); + padding->length = 5; + if(!replace_in_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, padding, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVAAP[P]\tset APPLICATION (grow)\n"); + app->data.application.id[0] = 'k'; /* twiddle the id */ + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, false)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVAAP[A]\tset PADDING (equal)\n"); + padding->length = 27; + if(!replace_in_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, padding, false)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVAAP[P]\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SVAA[P]P\tdelete (middle block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVA[A]P\tdelete (middle block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]P\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVA[P]\tinsert PADDING after\n"); + padding->length = 5; + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVAP[P]\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SVA[P]P\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SV[A]PP\tset APPLICATION (grow), try to expand into padding which is too small\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 32, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]PP\tset APPLICATION (grow), try to expand into padding which is 'close' but still too small\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 60, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]PP\tset APPLICATION (grow), expand into padding which will leave 0-length pad\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 87, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + our_metadata_.blocks[our_current_position+1]->length = 0; + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]PP\tset APPLICATION (grow), expand into padding which is exactly consumed\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 91, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]P\tset APPLICATION (grow), expand into padding which is exactly consumed\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 100, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + our_metadata_.blocks[our_current_position]->is_last = true; + if(!FLAC__metadata_simple_iterator_set_block(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]\tset PADDING (equal size)\n"); + padding->length = app->length; + if(!replace_in_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, true)) + return die_ss_("FLAC__metadata_simple_iterator_set_block(iterator, padding, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[P]\tinsert PADDING after\n"); + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVP[P]\tinsert PADDING after\n"); + padding->length = 5; + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, padding, false)", iterator); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return false; + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SVPP[P]\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SVP[P]P\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("SV[P]PP\tprev\n"); + if(!FLAC__metadata_simple_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]PPP\tinsert APPLICATION after, try to expand into padding which is too small\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 101, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]PPP\tdelete (middle block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("S[V]PPP\tinsert APPLICATION after, try to expand into padding which is 'close' but still too small\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 97, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]PPP\tdelete (middle block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("S[V]PPP\tinsert APPLICATION after, expand into padding which is exactly consumed\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 100, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]PP\tdelete (middle block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("S[V]PP\tinsert APPLICATION after, expand into padding which will leave 0-length pad\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 96, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + our_metadata_.blocks[our_current_position+1]->length = 0; + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]PP\tdelete (middle block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("S[V]PP\tnext\n"); + if(!FLAC__metadata_simple_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]P\tdelete (middle block), don't replace with padding\n"); + if(!FLAC__metadata_simple_iterator_delete_block(iterator, false)) + return die_ss_("FLAC__metadata_simple_iterator_delete_block(iterator, false)", iterator); + delete_from_our_metadata_(our_current_position--); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("S[V]P\tinsert APPLICATION after, expand into padding which is exactly consumed\n"); + if(!FLAC__metadata_object_application_set_data(app, data, 1, true)) + return die_("setting APPLICATION data"); + if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)) + return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(iterator, app, true)", iterator); + + if(!test_file_(/*is_ogg=*/false, decoder_metadata_callback_compare_)) + return false; + + printf("delete simple iterator\n"); + + FLAC__metadata_simple_iterator_delete(iterator); + + FLAC__metadata_object_delete(app); + FLAC__metadata_object_delete(padding); + + if(!remove_file_(flacfilename(/*is_ogg=*/false))) + return false; + + return true; +} + +static FLAC__bool test_level_2_(FLAC__bool filename_based, FLAC__bool is_ogg) +{ + FLAC__Metadata_Iterator *iterator; + FLAC__Metadata_Chain *chain; + FLAC__StreamMetadata *block, *app, *padding; + FLAC__byte data[2000]; + uint32_t our_current_position; + + /* initialize 'data' to avoid Valgrind errors */ + memset(data, 0, sizeof(data)); + + printf("\n\n++++++ testing level 2 interface (%s-based, %s FLAC)\n", filename_based? "filename":"callback", is_ogg? "Ogg":"native"); + + printf("generate read-only file\n"); + + if(!generate_file_(/*include_extras=*/false, is_ogg)) + return false; + + if(!change_stats_(flacfilename(is_ogg), /*read_only=*/true)) + return false; + + printf("create chain\n"); + + if(0 == (chain = FLAC__metadata_chain_new())) + return die_("allocating chain"); + + printf("read chain\n"); + + if(!read_chain_(chain, flacfilename(is_ogg), filename_based, is_ogg)) + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + + printf("[S]VP\ttest initial metadata\n"); + + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + if(is_ogg) + goto end; + + printf("switch file to read-write\n"); + + if(!change_stats_(flacfilename(is_ogg), /*read-only=*/false)) + return false; + + printf("create iterator\n"); + if(0 == (iterator = FLAC__metadata_iterator_new())) + return die_("allocating memory for iterator"); + + our_current_position = 0; + + FLAC__metadata_iterator_init(iterator, chain); + + if(0 == (block = FLAC__metadata_iterator_get_block(iterator))) + return die_("getting block from iterator"); + + FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_STREAMINFO); + + printf("[S]VP\tmodify STREAMINFO, write\n"); + + block->data.stream_info.sample_rate = 32000; + if(!replace_in_our_metadata_(block, our_current_position, /*copy=*/true)) + return die_("copying object"); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/true, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, false, true)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("[S]VP\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]P\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]\treplace PADDING with identical-size APPLICATION\n"); + if(0 == (block = FLAC__metadata_iterator_get_block(iterator))) + return die_("getting block from iterator"); + if(0 == (app = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION))) + return die_("FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)"); + memcpy(app->data.application.id, "duh", (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); + if(!FLAC__metadata_object_application_set_data(app, data, block->length-(FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]\tshrink APPLICATION, don't use padding\n"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 26, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]\tgrow APPLICATION, don't use padding\n"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 28, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]\tgrow APPLICATION, use padding, but last block is not padding\n"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 36, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]\tshrink APPLICATION, use padding, last block is not padding, but delta is too small for new PADDING block\n"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 33, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]\tshrink APPLICATION, use padding, last block is not padding, delta is enough for new PADDING block\n"); + if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) + return die_("creating PADDING block"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 29, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + padding->length = 0; + if(!insert_to_our_metadata_(padding, our_current_position+1, /*copy=*/false)) + return die_("internal error"); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]P\tshrink APPLICATION, use padding, last block is padding\n"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 16, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + our_metadata_.blocks[our_current_position+1]->length = 13; + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding, but delta is too small\n"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 50, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding of exceeding size\n"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 56, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + our_metadata_.blocks[our_current_position+1]->length -= (56 - 50); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding of exact size\n"); + if(0 == (app = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("copying object"); + if(!FLAC__metadata_object_application_set_data(app, data, 67, true)) + return die_("setting APPLICATION data"); + if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true)) + return die_("copying object"); + delete_from_our_metadata_(our_current_position+1); + if(!FLAC__metadata_iterator_set_block(iterator, app)) + return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV[A]\tprev\n"); + if(!FLAC__metadata_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("S[V]A\tprev\n"); + if(!FLAC__metadata_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("[S]VA\tinsert PADDING before STREAMINFO (should fail)\n"); + if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) + return die_("creating PADDING block"); + padding->length = 30; + if(!FLAC__metadata_iterator_insert_block_before(iterator, padding)) + printf("\tFLAC__metadata_iterator_insert_block_before() returned false like it should\n"); + else + return die_("FLAC__metadata_iterator_insert_block_before() should have returned false"); + + printf("[S]VP\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]A\tinsert PADDING after\n"); + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) + return die_("FLAC__metadata_iterator_insert_block_after(iterator, padding)"); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("SV[P]A\tinsert PADDING before\n"); + if(0 == (padding = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("creating PADDING block"); + padding->length = 17; + if(!insert_to_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!FLAC__metadata_iterator_insert_block_before(iterator, padding)) + return die_("FLAC__metadata_iterator_insert_block_before(iterator, padding)"); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("SV[P]PA\tinsert PADDING before\n"); + if(0 == (padding = FLAC__metadata_object_clone(our_metadata_.blocks[our_current_position]))) + return die_("creating PADDING block"); + padding->length = 0; + if(!insert_to_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!FLAC__metadata_iterator_insert_block_before(iterator, padding)) + return die_("FLAC__metadata_iterator_insert_block_before(iterator, padding)"); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("SV[P]PPA\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVP[P]PA\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVPP[P]A\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SVPPP[A]\tinsert PADDING after\n"); + if(0 == (padding = FLAC__metadata_object_clone(our_metadata_.blocks[2]))) + return die_("creating PADDING block"); + padding->length = 57; + if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) + return die_("FLAC__metadata_iterator_insert_block_after(iterator, padding)"); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("SVPPPA[P]\tinsert PADDING before\n"); + if(0 == (padding = FLAC__metadata_object_clone(our_metadata_.blocks[2]))) + return die_("creating PADDING block"); + padding->length = 99; + if(!insert_to_our_metadata_(padding, our_current_position, /*copy=*/true)) + return die_("copying metadata"); + if(!FLAC__metadata_iterator_insert_block_before(iterator, padding)) + return die_("FLAC__metadata_iterator_insert_block_before(iterator, padding)"); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("delete iterator\n"); + FLAC__metadata_iterator_delete(iterator); + our_current_position = 0; + + printf("SVPPPAPP\tmerge padding\n"); + FLAC__metadata_chain_merge_padding(chain); + our_metadata_.blocks[2]->length += (FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[3]->length); + our_metadata_.blocks[2]->length += (FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[4]->length); + our_metadata_.blocks[6]->length += (FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[7]->length); + delete_from_our_metadata_(7); + delete_from_our_metadata_(4); + delete_from_our_metadata_(3); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SVPAP\tsort padding\n"); + FLAC__metadata_chain_sort_padding(chain); + our_metadata_.blocks[4]->length += (FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[2]->length); + delete_from_our_metadata_(2); + + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("create iterator\n"); + if(0 == (iterator = FLAC__metadata_iterator_new())) + return die_("allocating memory for iterator"); + + our_current_position = 0; + + FLAC__metadata_iterator_init(iterator, chain); + + printf("[S]VAP\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("S[V]AP\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[A]P\tdelete middle block, replace with padding\n"); + if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) + return die_("creating PADDING block"); + padding->length = 71; + if(!replace_in_our_metadata_(padding, our_current_position--, /*copy=*/false)) + return die_("copying object"); + if(!FLAC__metadata_iterator_delete_block(iterator, /*replace_with_padding=*/true)) + return die_c_("FLAC__metadata_iterator_delete_block(iterator, true)", FLAC__metadata_chain_status(chain)); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("S[V]PP\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]P\tdelete middle block, don't replace with padding\n"); + delete_from_our_metadata_(our_current_position--); + if(!FLAC__metadata_iterator_delete_block(iterator, /*replace_with_padding=*/false)) + return die_c_("FLAC__metadata_iterator_delete_block(iterator, false)", FLAC__metadata_chain_status(chain)); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("S[V]P\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]\tdelete last block, replace with padding\n"); + if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) + return die_("creating PADDING block"); + padding->length = 219; + if(!replace_in_our_metadata_(padding, our_current_position--, /*copy=*/false)) + return die_("copying object"); + if(!FLAC__metadata_iterator_delete_block(iterator, /*replace_with_padding=*/true)) + return die_c_("FLAC__metadata_iterator_delete_block(iterator, true)", FLAC__metadata_chain_status(chain)); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("S[V]P\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + our_current_position++; + + printf("SV[P]\tdelete last block, don't replace with padding\n"); + delete_from_our_metadata_(our_current_position--); + if(!FLAC__metadata_iterator_delete_block(iterator, /*replace_with_padding=*/false)) + return die_c_("FLAC__metadata_iterator_delete_block(iterator, false)", FLAC__metadata_chain_status(chain)); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("S[V]\tprev\n"); + if(!FLAC__metadata_iterator_prev(iterator)) + return die_("iterator ended early\n"); + our_current_position--; + + printf("[S]V\tdelete STREAMINFO block, should fail\n"); + if(FLAC__metadata_iterator_delete_block(iterator, /*replace_with_padding=*/false)) + return die_("FLAC__metadata_iterator_delete_block() on STREAMINFO should have failed but didn't"); + + if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) + return false; + + printf("delete iterator\n"); + FLAC__metadata_iterator_delete(iterator); + our_current_position = 0; + + printf("SV\tmerge padding\n"); + FLAC__metadata_chain_merge_padding(chain); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + + printf("SV\tsort padding\n"); + FLAC__metadata_chain_sort_padding(chain); + + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg))) + return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); + if(!compare_chain_(chain, 0, 0)) + return false; + if(!test_file_(is_ogg, decoder_metadata_callback_compare_)) + return false; + +end: + printf("delete chain\n"); + + FLAC__metadata_chain_delete(chain); + + if(!remove_file_(flacfilename(is_ogg))) + return false; + + return true; +} + +static FLAC__bool test_level_2_misc_(FLAC__bool is_ogg) +{ + FLAC__Metadata_Iterator *iterator; + FLAC__Metadata_Chain *chain; + FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (FLAC__IOCallback_Read)fread; +#ifdef FLAC__VALGRIND_TESTING + callbacks.write = chain_write_cb_; +#else + callbacks.write = (FLAC__IOCallback_Write)fwrite; +#endif + callbacks.seek = chain_seek_cb_; + callbacks.tell = chain_tell_cb_; + callbacks.eof = chain_eof_cb_; + + printf("\n\n++++++ testing level 2 interface (mismatched read/write protections)\n"); + + printf("generate file\n"); + + if(!generate_file_(/*include_extras=*/false, is_ogg)) + return false; + + printf("create chain\n"); + + if(0 == (chain = FLAC__metadata_chain_new())) + return die_("allocating chain"); + + printf("read chain (filename-based)\n"); + + if(!FLAC__metadata_chain_read(chain, flacfilename(is_ogg))) + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + + printf("write chain with wrong method FLAC__metadata_chain_write_with_callbacks()\n"); + { + if(FLAC__metadata_chain_write_with_callbacks(chain, /*use_padding=*/false, 0, callbacks)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write_with_callbacks() returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (filename-based)\n"); + + if(!FLAC__metadata_chain_read(chain, flacfilename(is_ogg))) + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + + printf("write chain with wrong method FLAC__metadata_chain_write_with_callbacks_and_tempfile()\n"); + { + if(FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain, /*use_padding=*/false, 0, callbacks, 0, callbacks)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write_with_callbacks_and_tempfile() returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == file) + return die_("opening file"); + if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + } + fclose(file); + } + + printf("write chain with wrong method FLAC__metadata_chain_write()\n"); + { + if(FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/false)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write() returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == file) + return die_("opening file"); + if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + } + fclose(file); + } + + printf("testing FLAC__metadata_chain_check_if_tempfile_needed()... "); + + if(!FLAC__metadata_chain_check_if_tempfile_needed(chain, /*use_padding=*/false)) + printf("OK: FLAC__metadata_chain_check_if_tempfile_needed() returned false like it should\n"); + else + return die_("FLAC__metadata_chain_check_if_tempfile_needed() returned true but shouldn't have"); + + printf("write chain with wrong method FLAC__metadata_chain_write_with_callbacks_and_tempfile()\n"); + { + if(FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain, /*use_padding=*/false, 0, callbacks, 0, callbacks)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write_with_callbacks_and_tempfile() returned false,FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = flac_fopen(flacfilename(is_ogg), "rb"); + if(0 == file) + return die_("opening file"); + if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + } + fclose(file); + } + + printf("create iterator\n"); + if(0 == (iterator = FLAC__metadata_iterator_new())) + return die_("allocating memory for iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + printf("[S]VP\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + + printf("S[V]P\tdelete VORBIS_COMMENT, write\n"); + if(!FLAC__metadata_iterator_delete_block(iterator, /*replace_with_padding=*/false)) + return die_c_("block delete failed\n", FLAC__metadata_chain_status(chain)); + + printf("testing FLAC__metadata_chain_check_if_tempfile_needed()... "); + + if(FLAC__metadata_chain_check_if_tempfile_needed(chain, /*use_padding=*/false)) + printf("OK: FLAC__metadata_chain_check_if_tempfile_needed() returned true like it should\n"); + else + return die_("FLAC__metadata_chain_check_if_tempfile_needed() returned false but shouldn't have"); + + printf("write chain with wrong method FLAC__metadata_chain_write_with_callbacks()\n"); + { + if(FLAC__metadata_chain_write_with_callbacks(chain, /*use_padding=*/false, 0, callbacks)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write_with_callbacks() returned false,FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL like it should\n"); + } + + printf("delete iterator\n"); + + FLAC__metadata_iterator_delete(iterator); + + printf("delete chain\n"); + + FLAC__metadata_chain_delete(chain); + + if(!remove_file_(flacfilename(is_ogg))) + return false; + + return true; +} + +FLAC__bool test_metadata_file_manipulation(void) +{ + printf("\n+++ libFLAC unit test: metadata manipulation\n\n"); + + our_metadata_.num_blocks = 0; + + if(!test_level_0_()) + return false; + + if(!test_level_1_()) + return false; + + if(!test_level_2_(/*filename_based=*/true, /*is_ogg=*/false)) /* filename-based */ + return false; + if(!test_level_2_(/*filename_based=*/false, /*is_ogg=*/false)) /* callback-based */ + return false; + if(!test_level_2_misc_(/*is_ogg=*/false)) + return false; + + if(FLAC_API_SUPPORTS_OGG_FLAC) { + if(!test_level_2_(/*filename_based=*/true, /*is_ogg=*/true)) /* filename-based */ + return false; + if(!test_level_2_(/*filename_based=*/false, /*is_ogg=*/true)) /* callback-based */ + return false; +#if 0 + /* when ogg flac write is supported, will have to add this: */ + if(!test_level_2_misc_(/*is_ogg=*/true)) + return false; +#endif + } + + return true; +} diff --git a/src/test_libFLAC/metadata_object.c b/src/test_libFLAC/metadata_object.c new file mode 100644 index 0000000..ea6b69f --- /dev/null +++ b/src/test_libFLAC/metadata_object.c @@ -0,0 +1,2291 @@ +/* test_libFLAC - Unit tester for libFLAC + * Copyright (C) 2002-2009 Josh Coalson + * Copyright (C) 2011-2023 Xiph.Org Foundation + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "FLAC/assert.h" +#include "FLAC/metadata.h" +#include "test_libs_common/metadata_utils.h" +#include "share/compat.h" +#include "metadata.h" +#include <stdio.h> +#include <stdlib.h> /* for malloc() */ +#include <string.h> /* for memcmp() */ + +static FLAC__byte *make_dummydata_(FLAC__byte *dummydata, uint32_t len) +{ + FLAC__byte *ret; + + if(0 == (ret = malloc(len))) { + printf("FAILED, malloc error\n"); + exit(1); + } + else + memcpy(ret, dummydata, len); + + return ret; +} + +static FLAC__bool compare_track_(const FLAC__StreamMetadata_CueSheet_Track *from, const FLAC__StreamMetadata_CueSheet_Track *to) +{ + uint32_t i; + + if(from->offset != to->offset) { + printf("FAILED, track offset mismatch, expected %" PRIu64 ", got %" PRIu64 "\n", to->offset, from->offset); + return false; + } + if(from->number != to->number) { + printf("FAILED, track number mismatch, expected %u, got %u\n", (uint32_t)to->number, (uint32_t)from->number); + return false; + } + if(0 != strcmp(from->isrc, to->isrc)) { + printf("FAILED, track number mismatch, expected %s, got %s\n", to->isrc, from->isrc); + return false; + } + if(from->type != to->type) { + printf("FAILED, track type mismatch, expected %u, got %u\n", (uint32_t)to->type, (uint32_t)from->type); + return false; + } + if(from->pre_emphasis != to->pre_emphasis) { + printf("FAILED, track pre_emphasis mismatch, expected %u, got %u\n", (uint32_t)to->pre_emphasis, (uint32_t)from->pre_emphasis); + return false; + } + if(from->num_indices != to->num_indices) { + printf("FAILED, track num_indices mismatch, expected %u, got %u\n", (uint32_t)to->num_indices, (uint32_t)from->num_indices); + return false; + } + if(0 == to->indices || 0 == from->indices) { + if(to->indices != from->indices) { + printf("FAILED, track indices mismatch\n"); + return false; + } + } + else { + for(i = 0; i < to->num_indices; i++) { + if(from->indices[i].offset != to->indices[i].offset) { + printf("FAILED, track indices[%u].offset mismatch, expected %" PRIu64 ", got %" PRIu64 "\n", i, to->indices[i].offset, from->indices[i].offset); + return false; + } + if(from->indices[i].number != to->indices[i].number) { + printf("FAILED, track indices[%u].number mismatch, expected %u, got %u\n", i, (uint32_t)to->indices[i].number, (uint32_t)from->indices[i].number); + return false; + } + } + } + + return true; +} + +static FLAC__bool compare_seekpoint_array_(const FLAC__StreamMetadata_SeekPoint *from, const FLAC__StreamMetadata_SeekPoint *to, uint32_t n) +{ + uint32_t i; + + FLAC__ASSERT(0 != from); + FLAC__ASSERT(0 != to); + + for(i = 0; i < n; i++) { + if(from[i].sample_number != to[i].sample_number) { + printf("FAILED, point[%u].sample_number mismatch, expected %" PRIu64 ", got %" PRIu64 "\n", i, to[i].sample_number, from[i].sample_number); + return false; + } + if(from[i].stream_offset != to[i].stream_offset) { + printf("FAILED, point[%u].stream_offset mismatch, expected %" PRIu64 ", got %" PRIu64 "\n", i, to[i].stream_offset, from[i].stream_offset); + return false; + } + if(from[i].frame_samples != to[i].frame_samples) { + printf("FAILED, point[%u].frame_samples mismatch, expected %u, got %u\n", i, to[i].frame_samples, from[i].frame_samples); + return false; + } + } + + return true; +} + +static FLAC__bool check_seektable_(const FLAC__StreamMetadata *block, uint32_t num_points, const FLAC__StreamMetadata_SeekPoint *array) +{ + const uint32_t expected_length = num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; + + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + if(block->data.seek_table.num_points != num_points) { + printf("FAILED, expected %u point, got %u\n", num_points, block->data.seek_table.num_points); + return false; + } + if(0 == array) { + if(0 != block->data.seek_table.points) { + printf("FAILED, 'points' pointer is not null\n"); + return false; + } + } + else { + if(!compare_seekpoint_array_(block->data.seek_table.points, array, num_points)) + return false; + } + printf("OK\n"); + + return true; +} + +static void entry_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, const char *field) +{ + entry->length = strlen(field); + entry->entry = malloc(entry->length+1); + FLAC__ASSERT(0 != entry->entry); + memcpy(entry->entry, field, entry->length); + entry->entry[entry->length] = '\0'; +} + +static void entry_clone_(FLAC__StreamMetadata_VorbisComment_Entry *entry) +{ + FLAC__byte *x = malloc(entry->length+1); + FLAC__ASSERT(0 != x); + memcpy(x, entry->entry, entry->length); + x[entry->length] = '\0'; + entry->entry = x; +} + +static void vc_calc_len_(FLAC__StreamMetadata *block) +{ + const FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment; + uint32_t i; + + block->length = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8; + block->length += vc->vendor_string.length; + block->length += FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8; + for(i = 0; i < vc->num_comments; i++) { + block->length += FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8; + block->length += vc->comments[i].length; + } +} + +static void vc_resize_(FLAC__StreamMetadata *block, uint32_t num) +{ + FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment; + + if(vc->num_comments != 0) { + FLAC__ASSERT(0 != vc->comments); + if(num < vc->num_comments) { + uint32_t i; + for(i = num; i < vc->num_comments; i++) { + if(0 != vc->comments[i].entry) + free(vc->comments[i].entry); + } + } + } + if(num == 0) { + if(0 != vc->comments) { + free(vc->comments); + vc->comments = 0; + } + } + else { + uint32_t i; + vc->comments = realloc(vc->comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*num); + FLAC__ASSERT(0 != vc->comments); + for(i = vc->num_comments; i < num; i++) { + vc->comments[i].length = 0; + vc->comments[i].entry = malloc(1); + vc->comments[i].entry[0] = '\0'; + } + } + + vc->num_comments = num; + vc_calc_len_(block); +} + +static int vc_find_from_(FLAC__StreamMetadata *block, const char *name, uint32_t start) +{ + const uint32_t n = strlen(name); + uint32_t i; + for(i = start; i < block->data.vorbis_comment.num_comments; i++) { + const FLAC__StreamMetadata_VorbisComment_Entry *entry = &block->data.vorbis_comment.comments[i]; + if(entry->length > n && 0 == strncmp((const char *)entry->entry, name, n) && entry->entry[n] == '=') + return (int)i; + } + return -1; +} + +static void vc_set_vs_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, const char *field) +{ + if(0 != block->data.vorbis_comment.vendor_string.entry) + free(block->data.vorbis_comment.vendor_string.entry); + entry_new_(entry, field); + block->data.vorbis_comment.vendor_string = *entry; + vc_calc_len_(block); +} + +static void vc_set_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, uint32_t pos, const char *field) +{ + if(0 != block->data.vorbis_comment.comments[pos].entry) + free(block->data.vorbis_comment.comments[pos].entry); + entry_new_(entry, field); + block->data.vorbis_comment.comments[pos] = *entry; + vc_calc_len_(block); +} + +static void vc_insert_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, uint32_t pos, const char *field) +{ + FLAC__StreamMetadata_VorbisComment_Entry temp; + vc_resize_(block, block->data.vorbis_comment.num_comments+1); + temp = block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1]; + memmove(&block->data.vorbis_comment.comments[pos+1], &block->data.vorbis_comment.comments[pos], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(block->data.vorbis_comment.num_comments-1-pos)); + block->data.vorbis_comment.comments[pos] = temp; + vc_set_new_(entry, block, pos, field); + vc_calc_len_(block); +} + +static void vc_delete_(FLAC__StreamMetadata *block, uint32_t pos) +{ + if(0 != block->data.vorbis_comment.comments[pos].entry) + free(block->data.vorbis_comment.comments[pos].entry); + memmove(&block->data.vorbis_comment.comments[pos], &block->data.vorbis_comment.comments[pos+1], sizeof(FLAC__StreamMetadata_VorbisComment_Entry)*(block->data.vorbis_comment.num_comments-pos-1)); + block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1].entry = 0; + block->data.vorbis_comment.comments[block->data.vorbis_comment.num_comments-1].length = 0; + vc_resize_(block, block->data.vorbis_comment.num_comments-1); + vc_calc_len_(block); +} + +static void vc_replace_new_(FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__StreamMetadata *block, const char *field, FLAC__bool all) +{ + int indx; + char field_name[256]; + const char *eq = strchr(field, '='); + FLAC__ASSERT(eq>field && (uint32_t)(eq-field) < sizeof(field_name)); + memcpy(field_name, field, eq-field); + field_name[eq-field]='\0'; + + indx = vc_find_from_(block, field_name, 0); + if(indx < 0) + vc_insert_new_(entry, block, block->data.vorbis_comment.num_comments, field); + else { + vc_set_new_(entry, block, (uint32_t)indx, field); + if(all) { + for(indx = indx+1; indx >= 0 && (uint32_t)indx < block->data.vorbis_comment.num_comments; ) + if((indx = vc_find_from_(block, field_name, (uint32_t)indx)) >= 0) + vc_delete_(block, (uint32_t)indx); + } + } + + vc_calc_len_(block); +} + +static void track_new_(FLAC__StreamMetadata_CueSheet_Track *track, FLAC__uint64 offset, FLAC__byte number, const char *isrc, FLAC__bool data, FLAC__bool pre_em) +{ + track->offset = offset; + track->number = number; + memcpy(track->isrc, isrc, sizeof(track->isrc)); + track->type = data; + track->pre_emphasis = pre_em; + track->num_indices = 0; + track->indices = 0; +} + +static void track_clone_(FLAC__StreamMetadata_CueSheet_Track *track) +{ + if(track->num_indices > 0) { + size_t bytes = sizeof(FLAC__StreamMetadata_CueSheet_Index) * track->num_indices; + FLAC__StreamMetadata_CueSheet_Index *x = malloc(bytes); + FLAC__ASSERT(0 != x); + memcpy(x, track->indices, bytes); + track->indices = x; + } +} + +static void cs_calc_len_(FLAC__StreamMetadata *block) +{ + const FLAC__StreamMetadata_CueSheet *cs = &block->data.cue_sheet; + uint32_t i; + + block->length = ( + FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN + + FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN + + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN + + FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN + ) / 8; + block->length += cs->num_tracks * ( + FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN + + FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN + ) / 8; + for(i = 0; i < cs->num_tracks; i++) { + block->length += cs->tracks[i].num_indices * ( + FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN + + FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN + + FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN + ) / 8; + } +} + +static void tr_resize_(FLAC__StreamMetadata *block, uint32_t track_num, uint32_t num) +{ + FLAC__StreamMetadata_CueSheet_Track *tr; + + FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks); + + tr = &block->data.cue_sheet.tracks[track_num]; + + if(tr->num_indices != 0) { + FLAC__ASSERT(0 != tr->indices); + } + if(num == 0) { + if(0 != tr->indices) { + free(tr->indices); + tr->indices = 0; + } + } + else { + tr->indices = realloc(tr->indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)*num); + FLAC__ASSERT(0 != tr->indices); + if(num > tr->num_indices) + memset(tr->indices+tr->num_indices, 0, sizeof(FLAC__StreamMetadata_CueSheet_Index)*(num-tr->num_indices)); + } + + tr->num_indices = num; + cs_calc_len_(block); +} + +static void tr_set_new_(FLAC__StreamMetadata *block, uint32_t track_num, uint32_t pos, FLAC__StreamMetadata_CueSheet_Index indx) +{ + FLAC__StreamMetadata_CueSheet_Track *tr; + + FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks); + + tr = &block->data.cue_sheet.tracks[track_num]; + + FLAC__ASSERT(pos < tr->num_indices); + + tr->indices[pos] = indx; + + cs_calc_len_(block); +} + +static void tr_insert_new_(FLAC__StreamMetadata *block, uint32_t track_num, uint32_t pos, FLAC__StreamMetadata_CueSheet_Index indx) +{ + FLAC__StreamMetadata_CueSheet_Track *tr; + + FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks); + + tr = &block->data.cue_sheet.tracks[track_num]; + + FLAC__ASSERT(pos <= tr->num_indices); + + tr_resize_(block, track_num, tr->num_indices+1); + memmove(&tr->indices[pos+1], &tr->indices[pos], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(tr->num_indices-1-pos)); + tr_set_new_(block, track_num, pos, indx); + cs_calc_len_(block); +} + +static void tr_delete_(FLAC__StreamMetadata *block, uint32_t track_num, uint32_t pos) +{ + FLAC__StreamMetadata_CueSheet_Track *tr; + + FLAC__ASSERT(track_num < block->data.cue_sheet.num_tracks); + + tr = &block->data.cue_sheet.tracks[track_num]; + + FLAC__ASSERT(pos <= tr->num_indices); + + memmove(&tr->indices[pos], &tr->indices[pos+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(tr->num_indices-pos-1)); + tr_resize_(block, track_num, tr->num_indices-1); + cs_calc_len_(block); +} + +static void cs_resize_(FLAC__StreamMetadata *block, uint32_t num) +{ + FLAC__StreamMetadata_CueSheet *cs = &block->data.cue_sheet; + + if(cs->num_tracks != 0) { + FLAC__ASSERT(0 != cs->tracks); + if(num < cs->num_tracks) { + uint32_t i; + for(i = num; i < cs->num_tracks; i++) { + if(0 != cs->tracks[i].indices) + free(cs->tracks[i].indices); + } + } + } + if(num == 0) { + if(0 != cs->tracks) { + free(cs->tracks); + cs->tracks = 0; + } + } + else { + cs->tracks = realloc(cs->tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)*num); + FLAC__ASSERT(0 != cs->tracks); + if(num > cs->num_tracks) + memset(cs->tracks+cs->num_tracks, 0, sizeof(FLAC__StreamMetadata_CueSheet_Track)*(num-cs->num_tracks)); + } + + cs->num_tracks = num; + cs_calc_len_(block); +} + +static void cs_set_new_(FLAC__StreamMetadata_CueSheet_Track *track, FLAC__StreamMetadata *block, uint32_t pos, FLAC__uint64 offset, FLAC__byte number, const char *isrc, FLAC__bool data, FLAC__bool pre_em) +{ + track_new_(track, offset, number, isrc, data, pre_em); + block->data.cue_sheet.tracks[pos] = *track; + cs_calc_len_(block); +} + +static void cs_insert_new_(FLAC__StreamMetadata_CueSheet_Track *track, FLAC__StreamMetadata *block, uint32_t pos, FLAC__uint64 offset, FLAC__byte number, const char *isrc, FLAC__bool data, FLAC__bool pre_em) +{ + cs_resize_(block, block->data.cue_sheet.num_tracks+1); + memmove(&block->data.cue_sheet.tracks[pos+1], &block->data.cue_sheet.tracks[pos], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(block->data.cue_sheet.num_tracks-1-pos)); + cs_set_new_(track, block, pos, offset, number, isrc, data, pre_em); + cs_calc_len_(block); +} + +static void cs_delete_(FLAC__StreamMetadata *block, uint32_t pos) +{ + if(0 != block->data.cue_sheet.tracks[pos].indices) + free(block->data.cue_sheet.tracks[pos].indices); + memmove(&block->data.cue_sheet.tracks[pos], &block->data.cue_sheet.tracks[pos+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(block->data.cue_sheet.num_tracks-pos-1)); + block->data.cue_sheet.tracks[block->data.cue_sheet.num_tracks-1].indices = 0; + block->data.cue_sheet.tracks[block->data.cue_sheet.num_tracks-1].num_indices = 0; + cs_resize_(block, block->data.cue_sheet.num_tracks-1); + cs_calc_len_(block); +} + +static void pi_set_mime_type(FLAC__StreamMetadata *block, const char *s) +{ + if(block->data.picture.mime_type) { + block->length -= strlen(block->data.picture.mime_type); + free(block->data.picture.mime_type); + } + block->data.picture.mime_type = strdup(s); + FLAC__ASSERT(block->data.picture.mime_type); + block->length += strlen(block->data.picture.mime_type); +} + +static void pi_set_description(FLAC__StreamMetadata *block, const FLAC__byte *s) +{ + if(block->data.picture.description) { + block->length -= strlen((const char *)block->data.picture.description); + free(block->data.picture.description); + } + block->data.picture.description = (FLAC__byte*)strdup((const char *)s); + FLAC__ASSERT(block->data.picture.description); + block->length += strlen((const char *)block->data.picture.description); +} + +static void pi_set_data(FLAC__StreamMetadata *block, const FLAC__byte *data, FLAC__uint32 len) +{ + if(block->data.picture.data) { + block->length -= block->data.picture.data_length; + free(block->data.picture.data); + } + block->data.picture.data = (FLAC__byte*)strdup((const char *)data); + FLAC__ASSERT(block->data.picture.data); + block->data.picture.data_length = len; + block->length += len; +} + +FLAC__bool test_metadata_object(void) +{ + FLAC__StreamMetadata *block, *blockcopy, *vorbiscomment, *cuesheet, *picture; + FLAC__StreamMetadata_SeekPoint seekpoint_array[14]; + FLAC__StreamMetadata_VorbisComment_Entry entry; + FLAC__StreamMetadata_CueSheet_Index indx; + FLAC__StreamMetadata_CueSheet_Track track; + uint32_t i, expected_length, seekpoints; + int j; + static FLAC__byte dummydata[4] = { 'a', 'b', 'c', 'd' }; + + printf("\n+++ libFLAC unit test: metadata objects\n\n"); + + + printf("testing STREAMINFO\n"); + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_STREAMINFO); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + expected_length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH; + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + printf("testing PADDING\n"); + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + expected_length = 0; + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + printf("testing APPLICATION\n"); + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + expected_length = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8; + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + printf("testing FLAC__metadata_object_application_set_data(copy)... "); + if(!FLAC__metadata_object_application_set_data(block, dummydata, sizeof(dummydata), true/*copy*/)) { + printf("FAILED, returned false\n"); + return false; + } + expected_length = (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) + sizeof(dummydata); + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + if(0 != memcmp(block->data.application.data, dummydata, sizeof(dummydata))) { + printf("FAILED, data mismatch\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + printf("testing FLAC__metadata_object_application_set_data(own)... "); + if(!FLAC__metadata_object_application_set_data(block, make_dummydata_(dummydata, sizeof(dummydata)), sizeof(dummydata), false/*own*/)) { + printf("FAILED, returned false\n"); + return false; + } + expected_length = (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8) + sizeof(dummydata); + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + if(0 != memcmp(block->data.application.data, dummydata, sizeof(dummydata))) { + printf("FAILED, data mismatch\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + printf("testing SEEKTABLE\n"); + + for(i = 0; i < sizeof(seekpoint_array) / sizeof(FLAC__StreamMetadata_SeekPoint); i++) { + seekpoint_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; + seekpoint_array[i].stream_offset = 0; + seekpoint_array[i].frame_samples = 0; + } + + seekpoints = 0; + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!check_seektable_(block, seekpoints, 0)) + return false; + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + seekpoints = 2; + printf("testing FLAC__metadata_object_seektable_resize_points(grow to %u)...", seekpoints); + if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoints = 1; + printf("testing FLAC__metadata_object_seektable_resize_points(shrink to %u)...", seekpoints); + if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + printf("testing FLAC__metadata_object_seektable_is_legal()..."); + if(!FLAC__metadata_object_seektable_is_legal(block)) { + printf("FAILED, returned false\n"); + return false; + } + printf("OK\n"); + + seekpoints = 0; + printf("testing FLAC__metadata_object_seektable_resize_points(shrink to %u)...", seekpoints); + if(!FLAC__metadata_object_seektable_resize_points(block, seekpoints)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, 0)) + return false; + + seekpoints++; + printf("testing FLAC__metadata_object_seektable_insert_point() on empty array..."); + if(!FLAC__metadata_object_seektable_insert_point(block, 0, seekpoint_array[0])) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoint_array[0].sample_number = 1; + seekpoints++; + printf("testing FLAC__metadata_object_seektable_insert_point() on beginning of non-empty array..."); + if(!FLAC__metadata_object_seektable_insert_point(block, 0, seekpoint_array[0])) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoint_array[1].sample_number = 2; + seekpoints++; + printf("testing FLAC__metadata_object_seektable_insert_point() on middle of non-empty array..."); + if(!FLAC__metadata_object_seektable_insert_point(block, 1, seekpoint_array[1])) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoint_array[3].sample_number = 3; + seekpoints++; + printf("testing FLAC__metadata_object_seektable_insert_point() on end of non-empty array..."); + if(!FLAC__metadata_object_seektable_insert_point(block, 3, seekpoint_array[3])) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + seekpoint_array[2].sample_number = seekpoint_array[3].sample_number; + seekpoints--; + printf("testing FLAC__metadata_object_seektable_delete_point() on middle of array..."); + if(!FLAC__metadata_object_seektable_delete_point(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoints--; + printf("testing FLAC__metadata_object_seektable_delete_point() on end of array..."); + if(!FLAC__metadata_object_seektable_delete_point(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoints--; + printf("testing FLAC__metadata_object_seektable_delete_point() on beginning of array..."); + if(!FLAC__metadata_object_seektable_delete_point(block, 0)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array+1)) + return false; + + printf("testing FLAC__metadata_object_seektable_set_point()..."); + FLAC__metadata_object_seektable_set_point(block, 0, seekpoint_array[0]); + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + /* seektable template functions */ + + for(i = 0; i < sizeof(seekpoint_array) / sizeof(FLAC__StreamMetadata_SeekPoint); i++) { + seekpoint_array[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; + seekpoint_array[i].stream_offset = 0; + seekpoint_array[i].frame_samples = 0; + } + + seekpoints = 0; + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!check_seektable_(block, seekpoints, 0)) + return false; + + seekpoints += 2; + printf("testing FLAC__metadata_object_seekpoint_template_append_placeholders()... "); + if(!FLAC__metadata_object_seektable_template_append_placeholders(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoint_array[seekpoints++].sample_number = 7; + printf("testing FLAC__metadata_object_seekpoint_template_append_point()... "); + if(!FLAC__metadata_object_seektable_template_append_point(block, 7)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + { + FLAC__uint64 nums[2] = { 3, 7 }; + seekpoint_array[seekpoints++].sample_number = nums[0]; + seekpoint_array[seekpoints++].sample_number = nums[1]; + printf("testing FLAC__metadata_object_seekpoint_template_append_points()... "); + if(!FLAC__metadata_object_seektable_template_append_points(block, nums, sizeof(nums)/sizeof(FLAC__uint64))) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + } + + seekpoint_array[seekpoints++].sample_number = 0; + seekpoint_array[seekpoints++].sample_number = 10; + seekpoint_array[seekpoints++].sample_number = 20; + printf("testing FLAC__metadata_object_seekpoint_template_append_spaced_points()... "); + if(!FLAC__metadata_object_seektable_template_append_spaced_points(block, 3, 30)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoints--; + seekpoint_array[0].sample_number = 0; + seekpoint_array[1].sample_number = 3; + seekpoint_array[2].sample_number = 7; + seekpoint_array[3].sample_number = 10; + seekpoint_array[4].sample_number = 20; + seekpoint_array[5].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; + seekpoint_array[6].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER; + printf("testing FLAC__metadata_object_seekpoint_template_sort(compact=true)... "); + if(!FLAC__metadata_object_seektable_template_sort(block, /*compact=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!FLAC__metadata_object_seektable_is_legal(block)) { + printf("FAILED, seek table is illegal\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + printf("testing FLAC__metadata_object_seekpoint_template_sort(compact=false)... "); + if(!FLAC__metadata_object_seektable_template_sort(block, /*compact=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!FLAC__metadata_object_seektable_is_legal(block)) { + printf("FAILED, seek table is illegal\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoint_array[seekpoints++].sample_number = 0; + seekpoint_array[seekpoints++].sample_number = 10; + seekpoint_array[seekpoints++].sample_number = 20; + printf("testing FLAC__metadata_object_seekpoint_template_append_spaced_points_by_samples()... "); + if(!FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(block, 10, 30)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + seekpoint_array[seekpoints++].sample_number = 0; + seekpoint_array[seekpoints++].sample_number = 11; + seekpoint_array[seekpoints++].sample_number = 22; + printf("testing FLAC__metadata_object_seekpoint_template_append_spaced_points_by_samples()... "); + if(!FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(block, 11, 30)) { + printf("FAILED, returned false\n"); + return false; + } + if(!check_seektable_(block, seekpoints, seekpoint_array)) + return false; + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + printf("testing VORBIS_COMMENT\n"); + + { + FLAC__StreamMetadata_VorbisComment_Entry entry_; + char *field_name, *field_value; + + printf("testing FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair()... "); + if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry_, "name", "value")) { + printf("FAILED, returned false\n"); + return false; + } + if(strcmp((const char *)entry_.entry, "name=value")) { + printf("FAILED, field mismatch\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair()... "); + if(!FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(entry_, &field_name, &field_value)) { + printf("FAILED, returned false\n"); + return false; + } + if(strcmp(field_name, "name")) { + printf("FAILED, field name mismatch\n"); + return false; + } + if(strcmp(field_value, "value")) { + printf("FAILED, field value mismatch\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_entry_matches()... "); + if(!FLAC__metadata_object_vorbiscomment_entry_matches(entry_, field_name, strlen(field_name))) { + printf("FAILED, expected true, returned false\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_entry_matches()... "); + if(FLAC__metadata_object_vorbiscomment_entry_matches(entry_, "blah", strlen("blah"))) { + printf("FAILED, expected false, returned true\n"); + return false; + } + printf("OK\n"); + + free(entry_.entry); + free(field_name); + free(field_value); + } + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + expected_length = (FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN/8 + strlen(FLAC__VENDOR_STRING) + FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN/8); + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + vorbiscomment = FLAC__metadata_object_clone(block); + if(0 == vorbiscomment) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + vc_resize_(vorbiscomment, 2); + printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(grow to %u)...", vorbiscomment->data.vorbis_comment.num_comments); + if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + vc_resize_(vorbiscomment, 1); + printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments); + if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + vc_resize_(vorbiscomment, 0); + printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments); + if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_append_comment(copy) on empty array..."); + vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1"); + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_append_comment(copy) on non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 1, "name2=field2"); + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + vc_resize_(vorbiscomment, 0); + printf("testing FLAC__metadata_object_vorbiscomment_resize_comments(shrink to %u)...", vorbiscomment->data.vorbis_comment.num_comments); + if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, vorbiscomment->data.vorbis_comment.num_comments)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on empty array..."); + vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1"); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on beginning of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 0, "name2=field2"); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on middle of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 1, "name3=field3"); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 1, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on end of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 3, "name4=field4"); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 3, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on end of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 4, "name3=field3dup1"); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 4, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(copy) on end of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 5, "name3=field3dup1"); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 5, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_find_entry_from()..."); + if((j = FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0, "name3")) != 1) { + printf("FAILED, expected 1, got %d\n", j); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_find_entry_from()..."); + if((j = FLAC__metadata_object_vorbiscomment_find_entry_from(block, j+1, "name3")) != 4) { + printf("FAILED, expected 4, got %d\n", j); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_find_entry_from()..."); + if((j = FLAC__metadata_object_vorbiscomment_find_entry_from(block, j+1, "name3")) != 5) { + printf("FAILED, expected 5, got %d\n", j); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_find_entry_from()..."); + if((j = FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0, "name2")) != 0) { + printf("FAILED, expected 0, got %d\n", j); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_find_entry_from()..."); + if((j = FLAC__metadata_object_vorbiscomment_find_entry_from(block, j+1, "name2")) != -1) { + printf("FAILED, expected -1, got %d\n", j); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_find_entry_from()..."); + if((j = FLAC__metadata_object_vorbiscomment_find_entry_from(block, 0, "blah")) != -1) { + printf("FAILED, expected -1, got %d\n", j); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_replace_comment(first, copy)..."); + vc_replace_new_(&entry, vorbiscomment, "name3=field3new1", /*all=*/false); + if(!FLAC__metadata_object_vorbiscomment_replace_comment(block, entry, /*all=*/false, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + if(block->data.vorbis_comment.num_comments != 6) { + printf("FAILED, expected 6 comments, got %u\n", block->data.vorbis_comment.num_comments); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_replace_comment(all, copy)..."); + vc_replace_new_(&entry, vorbiscomment, "name3=field3new2", /*all=*/true); + if(!FLAC__metadata_object_vorbiscomment_replace_comment(block, entry, /*all=*/true, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + if(block->data.vorbis_comment.num_comments != 4) { + printf("FAILED, expected 4 comments, got %u\n", block->data.vorbis_comment.num_comments); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on middle of array..."); + vc_delete_(vorbiscomment, 2); + if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on end of array..."); + vc_delete_(vorbiscomment, 2); + if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on beginning of array..."); + vc_delete_(vorbiscomment, 0); + if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 0)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_append_comment(copy) on non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 1, "rem0=val0"); + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_append_comment(copy) on non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 2, "rem0=val1"); + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_append_comment(copy) on non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 3, "rem0=val2"); + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_remove_entry_matching(\"blah\")..."); + if((j = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, "blah")) != 0) { + printf("FAILED, expected 0, got %d\n", j); + return false; + } + if(block->data.vorbis_comment.num_comments != 4) { + printf("FAILED, expected 4 comments, got %u\n", block->data.vorbis_comment.num_comments); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_remove_entry_matching(\"rem0\")..."); + vc_delete_(vorbiscomment, 1); + if((j = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, "rem0")) != 1) { + printf("FAILED, expected 1, got %d\n", j); + return false; + } + if(block->data.vorbis_comment.num_comments != 3) { + printf("FAILED, expected 3 comments, got %u\n", block->data.vorbis_comment.num_comments); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_remove_entries_matching(\"blah\")..."); + if((j = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, "blah")) != 0) { + printf("FAILED, expected 0, got %d\n", j); + return false; + } + if(block->data.vorbis_comment.num_comments != 3) { + printf("FAILED, expected 3 comments, got %u\n", block->data.vorbis_comment.num_comments); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_remove_entries_matching(\"rem0\")..."); + vc_delete_(vorbiscomment, 1); + vc_delete_(vorbiscomment, 1); + if((j = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, "rem0")) != 2) { + printf("FAILED, expected 2, got %d\n", j); + return false; + } + if(block->data.vorbis_comment.num_comments != 1) { + printf("FAILED, expected 1 comments, got %u\n", block->data.vorbis_comment.num_comments); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_set_comment(copy)..."); + vc_set_new_(&entry, vorbiscomment, 0, "name5=field5"); + FLAC__metadata_object_vorbiscomment_set_comment(block, 0, entry, /*copy=*/true); + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_set_vendor_string(copy)..."); + vc_set_vs_new_(&entry, vorbiscomment, "name6=field6"); + FLAC__metadata_object_vorbiscomment_set_vendor_string(block, entry, /*copy=*/true); + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(vorbiscomment); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + vorbiscomment = FLAC__metadata_object_clone(block); + if(0 == vorbiscomment) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_append_comment(own) on empty array..."); + vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1"); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_append_comment(own) on non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 1, "name2=field2"); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(vorbiscomment); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + vorbiscomment = FLAC__metadata_object_clone(block); + if(0 == vorbiscomment) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on empty array..."); + vc_insert_new_(&entry, vorbiscomment, 0, "name1=field1"); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on beginning of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 0, "name2=field2"); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 0, entry, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on middle of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 1, "name3=field3"); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 1, entry, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on end of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 3, "name4=field4"); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 3, entry, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on end of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 4, "name3=field3dup1"); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 4, entry, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_insert_comment(own) on end of non-empty array..."); + vc_insert_new_(&entry, vorbiscomment, 5, "name3=field3dup1"); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, 5, entry, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_replace_comment(first, own)..."); + vc_replace_new_(&entry, vorbiscomment, "name3=field3new1", /*all=*/false); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_replace_comment(block, entry, /*all=*/false, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + if(block->data.vorbis_comment.num_comments != 6) { + printf("FAILED, expected 6 comments, got %u\n", block->data.vorbis_comment.num_comments); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_replace_comment(all, own)..."); + vc_replace_new_(&entry, vorbiscomment, "name3=field3new2", /*all=*/true); + entry_clone_(&entry); + if(!FLAC__metadata_object_vorbiscomment_replace_comment(block, entry, /*all=*/true, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + if(block->data.vorbis_comment.num_comments != 4) { + printf("FAILED, expected 4 comments, got %u\n", block->data.vorbis_comment.num_comments); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on middle of array..."); + vc_delete_(vorbiscomment, 2); + if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on end of array..."); + vc_delete_(vorbiscomment, 2); + if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_delete_comment() on beginning of array..."); + vc_delete_(vorbiscomment, 0); + if(!FLAC__metadata_object_vorbiscomment_delete_comment(block, 0)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_set_comment(own)..."); + vc_set_new_(&entry, vorbiscomment, 0, "name5=field5"); + entry_clone_(&entry); + FLAC__metadata_object_vorbiscomment_set_comment(block, 0, entry, /*copy=*/false); + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_vorbiscomment_set_vendor_string(own)..."); + vc_set_vs_new_(&entry, vorbiscomment, "name6=field6"); + entry_clone_(&entry); + FLAC__metadata_object_vorbiscomment_set_vendor_string(block, entry, /*copy=*/false); + if(!mutils__compare_block(vorbiscomment, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(vorbiscomment); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + printf("testing CUESHEET\n"); + + { + FLAC__StreamMetadata_CueSheet_Track *track_, *trackcopy_; + + printf("testing FLAC__metadata_object_cuesheet_track_new()... "); + track_ = FLAC__metadata_object_cuesheet_track_new(); + if(0 == track_) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_track_clone()... "); + trackcopy_ = FLAC__metadata_object_cuesheet_track_clone(track_); + if(0 == trackcopy_) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!compare_track_(trackcopy_, track_)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_track_delete()... "); + FLAC__metadata_object_cuesheet_track_delete(trackcopy_); + FLAC__metadata_object_cuesheet_track_delete(track_); + printf("OK\n"); + } + + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + expected_length = ( + FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN + + FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN + + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN + + FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN + ) / 8; + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + cuesheet = FLAC__metadata_object_clone(block); + if(0 == cuesheet) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + cs_resize_(cuesheet, 2); + printf("testing FLAC__metadata_object_cuesheet_resize_tracks(grow to %u)...", cuesheet->data.cue_sheet.num_tracks); + if(!FLAC__metadata_object_cuesheet_resize_tracks(block, cuesheet->data.cue_sheet.num_tracks)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + cs_resize_(cuesheet, 1); + printf("testing FLAC__metadata_object_cuesheet_resize_tracks(shrink to %u)...", cuesheet->data.cue_sheet.num_tracks); + if(!FLAC__metadata_object_cuesheet_resize_tracks(block, cuesheet->data.cue_sheet.num_tracks)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + cs_resize_(cuesheet, 0); + printf("testing FLAC__metadata_object_cuesheet_resize_tracks(shrink to %u)...", cuesheet->data.cue_sheet.num_tracks); + if(!FLAC__metadata_object_cuesheet_resize_tracks(block, cuesheet->data.cue_sheet.num_tracks)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_track(copy) on empty array..."); + cs_insert_new_(&track, cuesheet, 0, 0, 1, "ABCDE1234567", false, false); + if(!FLAC__metadata_object_cuesheet_insert_track(block, 0, &track, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_track(copy) on beginning of non-empty array..."); + cs_insert_new_(&track, cuesheet, 0, 10, 2, "BBCDE1234567", false, false); + if(!FLAC__metadata_object_cuesheet_insert_track(block, 0, &track, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_track(copy) on middle of non-empty array..."); + cs_insert_new_(&track, cuesheet, 1, 20, 3, "CBCDE1234567", false, false); + if(!FLAC__metadata_object_cuesheet_insert_track(block, 1, &track, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_track(copy) on end of non-empty array..."); + cs_insert_new_(&track, cuesheet, 3, 30, 4, "DBCDE1234567", false, false); + if(!FLAC__metadata_object_cuesheet_insert_track(block, 3, &track, /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_blank_track() on end of non-empty array..."); + cs_insert_new_(&track, cuesheet, 4, 0, 0, "\0\0\0\0\0\0\0\0\0\0\0\0", false, false); + if(!FLAC__metadata_object_cuesheet_insert_blank_track(block, 4)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_delete_track() on end of array..."); + cs_delete_(cuesheet, 4); + if(!FLAC__metadata_object_cuesheet_delete_track(block, 4)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_delete_track() on middle of array..."); + cs_delete_(cuesheet, 2); + if(!FLAC__metadata_object_cuesheet_delete_track(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_delete_track() on end of array..."); + cs_delete_(cuesheet, 2); + if(!FLAC__metadata_object_cuesheet_delete_track(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_delete_track() on beginning of array..."); + cs_delete_(cuesheet, 0); + if(!FLAC__metadata_object_cuesheet_delete_track(block, 0)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_set_track(copy)..."); + cs_set_new_(&track, cuesheet, 0, 40, 5, "EBCDE1234567", false, false); + FLAC__metadata_object_cuesheet_set_track(block, 0, &track, /*copy=*/true); + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + tr_resize_(cuesheet, 0, 2); + printf("testing FLAC__metadata_object_cuesheet_track_resize_indices(grow to %u)...", cuesheet->data.cue_sheet.tracks[0].num_indices); + if(!FLAC__metadata_object_cuesheet_track_resize_indices(block, 0, cuesheet->data.cue_sheet.tracks[0].num_indices)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + tr_resize_(cuesheet, 0, 1); + printf("testing FLAC__metadata_object_cuesheet_track_resize_indices(shrink to %u)...", cuesheet->data.cue_sheet.tracks[0].num_indices); + if(!FLAC__metadata_object_cuesheet_track_resize_indices(block, 0, cuesheet->data.cue_sheet.tracks[0].num_indices)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + tr_resize_(cuesheet, 0, 0); + printf("testing FLAC__metadata_object_cuesheet_track_resize_indices(shrink to %u)...", cuesheet->data.cue_sheet.tracks[0].num_indices); + if(!FLAC__metadata_object_cuesheet_track_resize_indices(block, 0, cuesheet->data.cue_sheet.tracks[0].num_indices)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + indx.offset = 0; + indx.number = 1; + printf("testing FLAC__metadata_object_cuesheet_track_insert_index() on empty array..."); + tr_insert_new_(cuesheet, 0, 0, indx); + if(!FLAC__metadata_object_cuesheet_track_insert_index(block, 0, 0, indx)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + indx.offset = 10; + indx.number = 2; + printf("testing FLAC__metadata_object_cuesheet_track_insert_index() on beginning of non-empty array..."); + tr_insert_new_(cuesheet, 0, 0, indx); + if(!FLAC__metadata_object_cuesheet_track_insert_index(block, 0, 0, indx)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + indx.offset = 20; + indx.number = 3; + printf("testing FLAC__metadata_object_cuesheet_track_insert_index() on middle of non-empty array..."); + tr_insert_new_(cuesheet, 0, 1, indx); + if(!FLAC__metadata_object_cuesheet_track_insert_index(block, 0, 1, indx)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + indx.offset = 30; + indx.number = 4; + printf("testing FLAC__metadata_object_cuesheet_track_insert_index() on end of non-empty array..."); + tr_insert_new_(cuesheet, 0, 3, indx); + if(!FLAC__metadata_object_cuesheet_track_insert_index(block, 0, 3, indx)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + indx.offset = 0; + indx.number = 0; + printf("testing FLAC__metadata_object_cuesheet_track_insert_blank_index() on end of non-empty array..."); + tr_insert_new_(cuesheet, 0, 4, indx); + if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(block, 0, 4)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_track_delete_index() on end of array..."); + tr_delete_(cuesheet, 0, 4); + if(!FLAC__metadata_object_cuesheet_track_delete_index(block, 0, 4)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_track_delete_index() on middle of array..."); + tr_delete_(cuesheet, 0, 2); + if(!FLAC__metadata_object_cuesheet_track_delete_index(block, 0, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_track_delete_index() on end of array..."); + tr_delete_(cuesheet, 0, 2); + if(!FLAC__metadata_object_cuesheet_track_delete_index(block, 0, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_track_delete_index() on beginning of array..."); + tr_delete_(cuesheet, 0, 0); + if(!FLAC__metadata_object_cuesheet_track_delete_index(block, 0, 0)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(cuesheet); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + cuesheet = FLAC__metadata_object_clone(block); + if(0 == cuesheet) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_track(own) on empty array..."); + cs_insert_new_(&track, cuesheet, 0, 60, 7, "GBCDE1234567", false, false); + track_clone_(&track); + if(!FLAC__metadata_object_cuesheet_insert_track(block, 0, &track, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_track(own) on beginning of non-empty array..."); + cs_insert_new_(&track, cuesheet, 0, 70, 8, "HBCDE1234567", false, false); + track_clone_(&track); + if(!FLAC__metadata_object_cuesheet_insert_track(block, 0, &track, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_track(own) on middle of non-empty array..."); + cs_insert_new_(&track, cuesheet, 1, 80, 9, "IBCDE1234567", false, false); + track_clone_(&track); + if(!FLAC__metadata_object_cuesheet_insert_track(block, 1, &track, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_insert_track(own) on end of non-empty array..."); + cs_insert_new_(&track, cuesheet, 3, 90, 10, "JBCDE1234567", false, false); + track_clone_(&track); + if(!FLAC__metadata_object_cuesheet_insert_track(block, 3, &track, /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_delete_track() on middle of array..."); + cs_delete_(cuesheet, 2); + if(!FLAC__metadata_object_cuesheet_delete_track(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_delete_track() on end of array..."); + cs_delete_(cuesheet, 2); + if(!FLAC__metadata_object_cuesheet_delete_track(block, 2)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_delete_track() on beginning of array..."); + cs_delete_(cuesheet, 0); + if(!FLAC__metadata_object_cuesheet_delete_track(block, 0)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_set_track(own)..."); + cs_set_new_(&track, cuesheet, 0, 100, 11, "KBCDE1234567", false, false); + track_clone_(&track); + FLAC__metadata_object_cuesheet_set_track(block, 0, &track, /*copy=*/false); + if(!mutils__compare_block(cuesheet, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_cuesheet_is_legal()..."); + { + const char *violation; + if(FLAC__metadata_object_cuesheet_is_legal(block, /*check_cd_da_subset=*/true, &violation)) { + printf("FAILED, returned true when expecting false\n"); + return false; + } + printf("returned false as expected, violation=\"%s\" OK\n", violation); + } + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(cuesheet); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + printf("testing PICTURE\n"); + + printf("testing FLAC__metadata_object_new()... "); + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PICTURE); + if(0 == block) { + printf("FAILED, returned NULL\n"); + return false; + } + expected_length = ( + FLAC__STREAM_METADATA_PICTURE_TYPE_LEN + + FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + + FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + + FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN + + FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN + + FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN + + FLAC__STREAM_METADATA_PICTURE_COLORS_LEN + + FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN + ) / 8; + if(block->length != expected_length) { + printf("FAILED, bad length, expected %u, got %u\n", expected_length, block->length); + return false; + } + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + picture = FLAC__metadata_object_clone(block); + if(0 == picture) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + pi_set_mime_type(picture, "image/png\t"); + printf("testing FLAC__metadata_object_picture_set_mime_type(copy)..."); + if(!FLAC__metadata_object_picture_set_mime_type(block, "image/png\t", /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_picture_is_legal()..."); + { + const char *violation; + if(FLAC__metadata_object_picture_is_legal(block, &violation)) { + printf("FAILED, returned true when expecting false\n"); + return false; + } + printf("returned false as expected, violation=\"%s\" OK\n", violation); + } + + pi_set_mime_type(picture, "image/png"); + printf("testing FLAC__metadata_object_picture_set_mime_type(copy)..."); + if(!FLAC__metadata_object_picture_set_mime_type(block, "image/png", /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_picture_is_legal()..."); + { + const char *violation; + if(!FLAC__metadata_object_picture_is_legal(block, &violation)) { + printf("FAILED, returned false, violation=\"%s\"\n", violation); + return false; + } + printf("OK\n"); + } + + pi_set_description(picture, (const FLAC__byte *)"DESCRIPTION\xff"); + printf("testing FLAC__metadata_object_picture_set_description(copy)..."); + if(!FLAC__metadata_object_picture_set_description(block, (FLAC__byte *)"DESCRIPTION\xff", /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_picture_is_legal()..."); + { + const char *violation; + if(FLAC__metadata_object_picture_is_legal(block, &violation)) { + printf("FAILED, returned true when expecting false\n"); + return false; + } + printf("returned false as expected, violation=\"%s\" OK\n", violation); + } + + pi_set_description(picture, (const FLAC__byte *)"DESCRIPTION"); + printf("testing FLAC__metadata_object_picture_set_description(copy)..."); + if(!FLAC__metadata_object_picture_set_description(block, (FLAC__byte *)"DESCRIPTION", /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_picture_is_legal()..."); + { + const char *violation; + if(!FLAC__metadata_object_picture_is_legal(block, &violation)) { + printf("FAILED, returned false, violation=\"%s\"\n", violation); + return false; + } + printf("OK\n"); + } + + + pi_set_data(picture, (const FLAC__byte*)"PNGDATA", strlen("PNGDATA")); + printf("testing FLAC__metadata_object_picture_set_data(copy)..."); + if(!FLAC__metadata_object_picture_set_data(block, (FLAC__byte*)"PNGDATA", strlen("PNGDATA"), /*copy=*/true)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + pi_set_mime_type(picture, "image/png\t"); + printf("testing FLAC__metadata_object_picture_set_mime_type(own)..."); + if(!FLAC__metadata_object_picture_set_mime_type(block, strdup("image/png\t"), /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_picture_is_legal()..."); + { + const char *violation; + if(FLAC__metadata_object_picture_is_legal(block, &violation)) { + printf("FAILED, returned true when expecting false\n"); + return false; + } + printf("returned false as expected, violation=\"%s\" OK\n", violation); + } + + pi_set_mime_type(picture, "image/png"); + printf("testing FLAC__metadata_object_picture_set_mime_type(own)..."); + if(!FLAC__metadata_object_picture_set_mime_type(block, strdup("image/png"), /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_picture_is_legal()..."); + { + const char *violation; + if(!FLAC__metadata_object_picture_is_legal(block, &violation)) { + printf("FAILED, returned false, violation=\"%s\"\n", violation); + return false; + } + printf("OK\n"); + } + + pi_set_description(picture, (const FLAC__byte *)"DESCRIPTION\xff"); + printf("testing FLAC__metadata_object_picture_set_description(own)..."); + if(!FLAC__metadata_object_picture_set_description(block, (FLAC__byte *)strdup("DESCRIPTION\xff"), /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_picture_is_legal()..."); + { + const char *violation; + if(FLAC__metadata_object_picture_is_legal(block, &violation)) { + printf("FAILED, returned true when expecting false\n"); + return false; + } + printf("returned false as expected, violation=\"%s\" OK\n", violation); + } + + pi_set_description(picture, (const FLAC__byte *)"DESCRIPTION"); + printf("testing FLAC__metadata_object_picture_set_description(own)..."); + if(!FLAC__metadata_object_picture_set_description(block, (FLAC__byte *)strdup("DESCRIPTION"), /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_picture_is_legal()..."); + { + const char *violation; + if(!FLAC__metadata_object_picture_is_legal(block, &violation)) { + printf("FAILED, returned false, violation=\"%s\"\n", violation); + return false; + } + printf("OK\n"); + } + + pi_set_data(picture, (const FLAC__byte*)"PNGDATA", strlen("PNGDATA")); + printf("testing FLAC__metadata_object_picture_set_data(own)..."); + if(!FLAC__metadata_object_picture_set_data(block, (FLAC__byte*)strdup("PNGDATA"), strlen("PNGDATA"), /*copy=*/false)) { + printf("FAILED, returned false\n"); + return false; + } + if(!mutils__compare_block(picture, block)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_clone()... "); + blockcopy = FLAC__metadata_object_clone(block); + if(0 == blockcopy) { + printf("FAILED, returned NULL\n"); + return false; + } + if(!mutils__compare_block(block, blockcopy)) + return false; + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(blockcopy); + printf("OK\n"); + + printf("testing FLAC__metadata_object_delete()... "); + FLAC__metadata_object_delete(picture); + FLAC__metadata_object_delete(block); + printf("OK\n"); + + + return true; +} |