#!/bin/bash
set -e

# limit thread parallelisation to no more than 4 cpus
# but respect any existing value of QUTIP_NUM_PROCESSES
ncpu=${QUTIP_NUM_PROCESSES}
if [ "x${ncpu}" = "x" ]; then
    ncpu=$(nproc)
    ncpu=$(( ncpu > 4 ? 4 : ncpu ))
fi
export QUTIP_NUM_PROCESSES=${ncpu}

DEB_HOST_ARCH=$(dpkg-architecture -q DEB_HOST_ARCH)

PY_DEFAULT=$(py3versions -sd)

if [ "${DEB_HOST_ARCH}" = "amd64" ]; then
    cp -r qutip/tests "$AUTOPKGTEST_TMP"
    for py in $(py3versions -s 2>/dev/null); do
	cd "$AUTOPKGTEST_TMP"
	echo "Testing with $py:"
	if [ "$py" = "python3.12" ]; then
	    # we know cython compilation does not work on Python 3.12
	    # so ignore message in stderr about it
	    err_stream=/dev/stdout
	else
	    err_stream=/dev/stderr
	fi
	$py -c "import qutip.testing; qutip.testing.run(pytest_options=[
		    ['-k', 'not test_qubit[power-gmres]'],
		    ['--durations', '20'],
		    ['--verbosity', '0']
		])" 2>$err_stream
	echo -e "\n"
    done
else
    echo "qutip tests are performed for amd64 only"
fi
