set -e

if [ "x${CC}" = "x" ]; then
  CC=gcc
fi
if [ "x${CXX}" = "x" ]; then
  CXX=g++
fi

# XTENSOR_FORCE_TEMPORARY_MEMORY_IN_ASSIGNMENTS is automatically set
# by the toplevel CMakeLists.txt, but for debci (autopkgtest)
# we run the tests standalone so the flag must be set explicitly.
# If it is not set then binding reference errors are triggered
# (upstream Issue #2887)
export CXXFLAGS="${CXXFLAGS} -DXTENSOR_FORCE_TEMPORARY_MEMORY_IN_ASSIGNMENTS"

#if dpkg-query -s libxsimd-dev >/dev/null 2>&1; then
#  BUILD_OPTIONS=-DXTENSOR_USE_XSIMD=ON
#fi

# mark i386 as having flakey tests (xreducer.average)
DEB_HOST_ARCH=`dpkg-architecture -q DEB_HOST_ARCH`
if [ "x$DEB_HOST_ARCH" = "xi386" ]; then
  CXXFLAGS="${CXXFLAGS} -DMARK_FLAKEY_TEST"
fi

cp -r ./test/* "$AUTOPKGTEST_TMP"
cd "$AUTOPKGTEST_TMP"

echo "Testing with ${CC} and ${CXX}:"
mkdir build && cd build
CC=${CC} CXX=${CXX} cmake .. ${BUILD_OPTIONS}
CTEST_OUTPUT_ON_FAILURE=1 make -j $(nproc) VERBOSE=1 xtest
