#!/bin/sh
#
# This autopkgtest runs only specific test files instead of the full
# suite, as upstream test suite is very time consuming.

set -e -u

# use XSIMD acceleration on supported architectures
CXXFLAGS=
DEB_HOST_ARCH=$( dpkg-architecture -qDEB_HOST_ARCH )
case " amd64 arm64 i386 " in
  *\ ${DEB_HOST_ARCH}\ *)  export CXXFLAGS=-DUSE_XSIMD;;
esac

TEST_FILES="test_cases.py"
# * test_cases: suggested by upstream [1].
# [1] https://github.com/serge-sans-paille/pythran/issues/908#issuecomment-945378742

# Additionally, some tests are excluded manually:
# * due to its duration, based on the output of "pytest --durations=0".
# * loopy_jacob is excluded due to failing, seemingly related to:
# https://github.com/serge-sans-paille/pythran/issues/1963

# test_case test_multitype_run* fails with gcc-12, cf Bug#1016155,
# see https://github.com/serge-sans-paille/pythran/issues/2011
TEST_SELECTOR="not test_rbfinterp and not test_shallow_water and not test_compute_subpix_2d_gaussian2 and not loopy_jacob and not test_multitype_run"

DEB_HOST_ARCH=$(dpkg-architecture -qDEB_HOST_ARCH)
if [ "${DEB_HOST_ARCH}" = "i386" ] ; then
    export CXXFLAGS="${CXXFLAGS} -march=native"
fi

# Copy upstream "pythran.tests" to a new package, as it is not part of
# the installed packages but the test files import from it.
cp -r pythran/tests $AUTOPKGTEST_TMP/pythran_tests
cd $AUTOPKGTEST_TMP/pythran_tests
find . -type f -name "*.py" -print0 | xargs -0 sed -i 's|pythran\.tests|pythran_tests|g'

# test_rosetta contains hard-coded references to a file relative to the
# package directory.
sed -i 's|pythran/tests/rosetta/read_conf.cfg|rosetta/read_conf.cfg|g' rosetta/*

for py in $(py3versions -s 2>/dev/null); do
    echo "Running testsuite with $py using CXXFLAGS=${CXXFLAGS}"
    for test_file in $TEST_FILES; do
        $py -m pytest $test_file -k "${TEST_SELECTOR}"
    done
done
