#!/usr/bin/env sh

# Copyright (C) 2008-2017 Erik de Castro Lopo <erikd@mega-nerd.com>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#     * Neither the author nor the names of any contributors may be used
#       to endorse or promote products derived from this software without
#       specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


HOST_TRIPLET=@HOST_TRIPLET@
PACKAGE_VERSION=@PACKAGE_VERSION@
LIB_VERSION=$(echo $PACKAGE_VERSION | sed "s/[a-z].*//")
ABS_TOP_SRCDIR=@abs_top_srcdir@
PYTHON=@PYTHON@

sfversion=$(./tests/sfversion@EXEEXT@ | grep libsndfile | sed "s/-exp$//")

if test "$sfversion" != libsndfile-$PACKAGE_VERSION ; then
	echo "Error : sfversion ($sfversion) and PACKAGE_VERSION ($PACKAGE_VERSION) don't match."
	exit 1
	fi

# Force exit on errors.
set -e

# Check the header file.
/usr/bin/env sh tests/pedantic-header-test.sh

# Need this for when we're running from files collected into the
# libsndfile-testsuite-@PACKAGE_VERSION@ tarball.
echo "Running unit tests from src/ directory of source code tree."
./src/test_main@EXEEXT@

echo
echo "Running end-to-end tests from tests/ directory."

./tests/error_test@EXEEXT@
./tests/pcm_test@EXEEXT@
./tests/ulaw_test@EXEEXT@
./tests/alaw_test@EXEEXT@
./tests/dwvw_test@EXEEXT@
./tests/command_test@EXEEXT@ ver
./tests/command_test@EXEEXT@ norm
./tests/command_test@EXEEXT@ format
./tests/command_test@EXEEXT@ peak
./tests/command_test@EXEEXT@ trunc
./tests/command_test@EXEEXT@ inst
./tests/command_test@EXEEXT@ cue
./tests/command_test@EXEEXT@ current_sf_info
./tests/command_test@EXEEXT@ bext
./tests/command_test@EXEEXT@ bextch
./tests/command_test@EXEEXT@ chanmap
./tests/command_test@EXEEXT@ cart
./tests/floating_point_test@EXEEXT@
./tests/checksum_test@EXEEXT@
./tests/scale_clip_test@EXEEXT@
./tests/headerless_test@EXEEXT@
./tests/rdwr_test@EXEEXT@
./tests/locale_test@EXEEXT@
./tests/win32_ordinal_test@EXEEXT@
./tests/external_libs_test@EXEEXT@
./tests/format_check_test@EXEEXT@
./tests/channel_test@EXEEXT@

# The w64 G++ compiler requires an extra runtime DLL which we don't have,
# so skip this test.
case "$HOST_TRIPLET" in
	x86_64-w64-mingw32)
		;;
	i686-w64-mingw32)
		;;
	*)
		./tests/cpp_test@EXEEXT@
		;;
	esac

echo "----------------------------------------------------------------------"
echo "  $sfversion passed common tests."
echo "----------------------------------------------------------------------"

# aiff-tests
./tests/write_read_test@EXEEXT@ aiff
./tests/lossy_comp_test@EXEEXT@ aiff_ulaw
./tests/lossy_comp_test@EXEEXT@ aiff_alaw
./tests/lossy_comp_test@EXEEXT@ aiff_gsm610
echo "----------------------------------------------------------------------"
echo "  lossy_comp_test@EXEEXT@ aiff_ima"
echo "----------------------------------------------------------------------"

./tests/peak_chunk_test@EXEEXT@ aiff
./tests/header_test@EXEEXT@ aiff
./tests/misc_test@EXEEXT@ aiff
./tests/string_test@EXEEXT@ aiff
./tests/multi_file_test@EXEEXT@ aiff
./tests/aiff_rw_test@EXEEXT@
./tests/chunk_test@EXEEXT@ aiff
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on AIFF files."
echo "----------------------------------------------------------------------"

# au-tests
./tests/write_read_test@EXEEXT@ au
./tests/lossy_comp_test@EXEEXT@ au_ulaw
./tests/lossy_comp_test@EXEEXT@ au_alaw
./tests/lossy_comp_test@EXEEXT@ au_g721
./tests/lossy_comp_test@EXEEXT@ au_g723
./tests/header_test@EXEEXT@ au
./tests/misc_test@EXEEXT@ au
./tests/multi_file_test@EXEEXT@ au
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on AU files."
echo "----------------------------------------------------------------------"

# caf-tests
./tests/write_read_test@EXEEXT@ caf
./tests/lossy_comp_test@EXEEXT@ caf_ulaw
./tests/lossy_comp_test@EXEEXT@ caf_alaw
./tests/header_test@EXEEXT@ caf
./tests/peak_chunk_test@EXEEXT@ caf
./tests/misc_test@EXEEXT@ caf
./tests/chunk_test@EXEEXT@ caf
./tests/string_test@EXEEXT@ caf
./tests/long_read_write_test@EXEEXT@ alac
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on CAF files."
echo "----------------------------------------------------------------------"

# wav-tests
./tests/write_read_test@EXEEXT@ wav
./tests/lossy_comp_test@EXEEXT@ wav_pcm
./tests/lossy_comp_test@EXEEXT@ wav_ima
./tests/lossy_comp_test@EXEEXT@ wav_msadpcm
./tests/lossy_comp_test@EXEEXT@ wav_ulaw
./tests/lossy_comp_test@EXEEXT@ wav_alaw
./tests/lossy_comp_test@EXEEXT@ wav_gsm610
./tests/lossy_comp_test@EXEEXT@ wav_g721
./tests/peak_chunk_test@EXEEXT@ wav
./tests/header_test@EXEEXT@ wav
./tests/misc_test@EXEEXT@ wav
./tests/string_test@EXEEXT@ wav
./tests/multi_file_test@EXEEXT@ wav
./tests/chunk_test@EXEEXT@ wav
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on WAV files."
echo "----------------------------------------------------------------------"

# w64-tests
./tests/write_read_test@EXEEXT@ w64
./tests/lossy_comp_test@EXEEXT@ w64_ima
./tests/lossy_comp_test@EXEEXT@ w64_msadpcm
./tests/lossy_comp_test@EXEEXT@ w64_ulaw
./tests/lossy_comp_test@EXEEXT@ w64_alaw
./tests/lossy_comp_test@EXEEXT@ w64_gsm610
./tests/header_test@EXEEXT@ w64
./tests/misc_test@EXEEXT@ w64
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on W64 files."
echo "----------------------------------------------------------------------"

# rf64-tests
./tests/write_read_test@EXEEXT@ rf64
./tests/header_test@EXEEXT@ rf64
./tests/misc_test@EXEEXT@ rf64
./tests/string_test@EXEEXT@ rf64
./tests/peak_chunk_test@EXEEXT@ rf64
./tests/chunk_test@EXEEXT@ rf64
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on RF64 files."
echo "----------------------------------------------------------------------"

# raw-tests
./tests/write_read_test@EXEEXT@ raw
./tests/lossy_comp_test@EXEEXT@ raw_ulaw
./tests/lossy_comp_test@EXEEXT@ raw_alaw
./tests/lossy_comp_test@EXEEXT@ raw_gsm610
./tests/lossy_comp_test@EXEEXT@ vox_adpcm
./tests/raw_test@EXEEXT@
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on RAW (header-less) files."
echo "----------------------------------------------------------------------"

# paf-tests
./tests/write_read_test@EXEEXT@ paf
./tests/header_test@EXEEXT@ paf
./tests/misc_test@EXEEXT@ paf
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on PAF files."
echo "----------------------------------------------------------------------"

# svx-tests
./tests/write_read_test@EXEEXT@ svx
./tests/header_test@EXEEXT@ svx
./tests/misc_test@EXEEXT@ svx
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on SVX files."
echo "----------------------------------------------------------------------"

# nist-tests
./tests/write_read_test@EXEEXT@ nist
./tests/lossy_comp_test@EXEEXT@ nist_ulaw
./tests/lossy_comp_test@EXEEXT@ nist_alaw
./tests/header_test@EXEEXT@ nist
./tests/misc_test@EXEEXT@ nist
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on NIST files."
echo "----------------------------------------------------------------------"

# ircam-tests
./tests/write_read_test@EXEEXT@ ircam
./tests/lossy_comp_test@EXEEXT@ ircam_ulaw
./tests/lossy_comp_test@EXEEXT@ ircam_alaw
./tests/header_test@EXEEXT@ ircam
./tests/misc_test@EXEEXT@ ircam
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on IRCAM files."
echo "----------------------------------------------------------------------"

# voc-tests
./tests/write_read_test@EXEEXT@ voc
./tests/lossy_comp_test@EXEEXT@ voc_ulaw
./tests/lossy_comp_test@EXEEXT@ voc_alaw
./tests/header_test@EXEEXT@ voc
./tests/misc_test@EXEEXT@ voc
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on VOC files."
echo "----------------------------------------------------------------------"

# mat4-tests
./tests/write_read_test@EXEEXT@ mat4
./tests/header_test@EXEEXT@ mat4
./tests/misc_test@EXEEXT@ mat4
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on MAT4 files."
echo "----------------------------------------------------------------------"

# mat5-tests
./tests/write_read_test@EXEEXT@ mat5
./tests/header_test@EXEEXT@ mat5
./tests/misc_test@EXEEXT@ mat5
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on MAT5 files."
echo "----------------------------------------------------------------------"

# pvf-tests
./tests/write_read_test@EXEEXT@ pvf
./tests/header_test@EXEEXT@ pvf
./tests/misc_test@EXEEXT@ pvf
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on PVF files."
echo "----------------------------------------------------------------------"

# xi-tests
./tests/lossy_comp_test@EXEEXT@ xi_dpcm
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on XI files."
echo "----------------------------------------------------------------------"

# htk-tests
./tests/write_read_test@EXEEXT@ htk
./tests/header_test@EXEEXT@ htk
./tests/misc_test@EXEEXT@ htk
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on HTK files."
echo "----------------------------------------------------------------------"

# avr-tests
./tests/write_read_test@EXEEXT@ avr
./tests/header_test@EXEEXT@ avr
./tests/misc_test@EXEEXT@ avr
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on AVR files."
echo "----------------------------------------------------------------------"

# sds-tests
./tests/write_read_test@EXEEXT@ sds
./tests/header_test@EXEEXT@ sds
./tests/misc_test@EXEEXT@ sds
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on SDS files."
echo "----------------------------------------------------------------------"

# sd2-tests
./tests/write_read_test@EXEEXT@ sd2
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on SD2 files."
echo "----------------------------------------------------------------------"

# wve-tests
./tests/lossy_comp_test@EXEEXT@ wve
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on WVE files."
echo "----------------------------------------------------------------------"

# mpc2k-tests
./tests/write_read_test@EXEEXT@ mpc2k
./tests/header_test@EXEEXT@ mpc2k
./tests/misc_test@EXEEXT@ mpc2k
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on MPC 2000 files."
echo "----------------------------------------------------------------------"

# flac-tests
./tests/write_read_test@EXEEXT@ flac
./tests/compression_size_test@EXEEXT@ flac
./tests/string_test@EXEEXT@ flac
./tests/header_test@EXEEXT@ flac
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on FLAC files."
echo "----------------------------------------------------------------------"

# vorbis-tests
./tests/ogg_test@EXEEXT@
./tests/compression_size_test@EXEEXT@ vorbis
./tests/lossy_comp_test@EXEEXT@ ogg_vorbis
./tests/string_test@EXEEXT@ ogg
./tests/misc_test@EXEEXT@ ogg
echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on OGG/VORBIS files."
echo "----------------------------------------------------------------------"

# opus-tests
./tests/ogg_opus_test@EXEEXT@
./tests/compression_size_test@EXEEXT@ opus
./tests/lossy_comp_test@EXEEXT@ ogg_opus
./tests/string_test@EXEEXT@ opus

echo "----------------------------------------------------------------------"
echo "  $sfversion passed tests on OPUS files."
echo "----------------------------------------------------------------------"

# io-tests
./tests/stdio_test@EXEEXT@
./tests/pipe_test@EXEEXT@
./tests/virtual_io_test@EXEEXT@
echo "----------------------------------------------------------------------"
echo "  $sfversion passed stdio/pipe/vio tests."
echo "----------------------------------------------------------------------"

"${PYTHON}" "${ABS_TOP_SRCDIR}/src/binheader_writef_check.py" "${ABS_TOP_SRCDIR}/src"/*.c
echo "----------------------------------------------------------------------"
echo "  $sfversion passed binary header tests."
echo "----------------------------------------------------------------------"

"${PYTHON}" "${ABS_TOP_SRCDIR}/programs/test-sndfile-metadata-set.py" "${HOST_TRIPLET}"
echo "----------------------------------------------------------------------"
echo "  $sfversion passed sndfile metadata tests."
echo "----------------------------------------------------------------------"
