#!/bin/sh
set -e

# Copy test harness settings from buildscripts/condarecipe.local/run_test.sh
# switch on developer mode
export NUMBA_DEVELOPER_MODE=1
# switch off color messages
export NUMBA_DISABLE_ERROR_MESSAGE_HIGHLIGHTING=1
# enable the fault handler
export PYTHONFAULTHANDLER=1

# enable new style error handling
export NUMBA_CAPTURED_ERRORS="new_style"

# Enable subprocess tests
export SUBPROC_TEST=1

# Let numba know where to cache precompiled results
export NUMBA_CACHE_DIR=$AUTOPKGTEST_TMP/.cache
export XDG_CACHE_HOME=$NUMBA_CACHE_DIR
mkdir -p $NUMBA_CACHE_DIR

export XDG_DATA_HOME=$AUTOPKGTEST_TMP/.local/share
mkdir -p $XDG_DATA_HOME

# Set a writable home directory for ipython tests
export HOME=$AUTOPKGTEST_TMP

# Make sure numba can find gdb
export NUMBA_GDB_BINARY=/usr/bin/gdb

# Make sure we enable multiprocessing for tests
TEST_NPROCS=`nproc`

# limit CPUs in use on PPC64LE, fork() issues
# occur on high core count systems
archstr=`uname -m`
if [ "$archstr" = "ppc64le" -a $TEST_NPROCS -gt 16 ]; then
    TEST_NPROCS=16
fi


for py in $(py3versions -d); do
    cd "$AUTOPKGTEST_TMP" # run on installed package

    # Disable NumPy dispatching to AVX512_SKX feature extensions if the chip is
    # reported to support the feature and NumPy >= 1.22 as this results in the use
    # of low accuracy SVML libm replacements in ufunc loops.
    _NPY_CMD='from numba.misc import numba_sysinfo;\
              sysinfo=numba_sysinfo.get_sysinfo();\
              print(sysinfo["NumPy AVX512_SKX detected"] and
                    sysinfo["NumPy Version"]>="1.22")'
    NUMPY_DETECTS_AVX512_SKX_NP_GT_122=`$py -c "$_NPY_CMD"`
    echo "NumPy >= 1.22 with AVX512_SKX detected: $NUMPY_DETECTS_AVX512_SKX_NP_GT_122"

    if [ "$NUMPY_DETECTS_AVX512_SKX_NP_GT_122" = "True" ]; then
        export NPY_DISABLE_CPU_FEATURES="AVX512_SKX"
    fi

    echo "[*] Testing with $py:"
    MPLBACKEND=Agg $py -m numba.runtests -b --exclude-tags='long-running,gdb,compiled_caching' -v -m $TEST_NPROCS 2>&1
done
