#!/bin/bash

# testing on 5 processes is recommended by upstream where possible
# and is safe to oversubscribe with OpenMPI
# but oversubscribing might be a problem (runs slow) with mpich
# 32-bit arm is fine, but there are problems with i386

DEB_HOST_ARCH=`dpkg-architecture -qDEB_HOST_ARCH`
DEB_HOST_ARCH_BITS=`dpkg-architecture -qDEB_HOST_ARCH_BITS`

nproc=`nproc`
MIN_PROC=$(( nproc > 2 ? nproc : 2 ))
NUM_PROC=$(( MIN_PROC > 5 ? 5 : MIN_PROC ))

echo -e "nproc reports ${nproc} processing units available (using ${NUM_PROC})\n"

ARCH_DEFAULT_MPI_IMPL=`grep ARCH_DEFAULT_MPI_IMPL /usr/share/mpi-default-dev/debian_defaults | sed "s/^.*=//"`

declare -a DISABLED_TESTS

# testContiguousBigMPI fails on 32-bit systems
# https://github.com/mpi4py/mpi4py/issues/670
if [ "x${DEB_HOST_ARCH_BITS}" = "x32" ]; then
    DISABLED_TESTS=("${DISABLED_TESTS[@]}" testContiguousBigMPI)
fi

# test_spawn fails with openmpi, see Bug#1100120
if [ "x${ARCH_DEFAULT_MPI_IMPL}" = "xopenmpi" ]; then
    DISABLED_TESTS=("${DISABLED_TESTS[@]}" test_spawn)
fi

if [ "${DEB_HOST_ARCH}" = "s390x" ]; then
    DISABLED_TESTS=("${DISABLED_TESTS[@]}"
        # segfault via PMIX (PMIX_ERR_BAD_PARAM) in test_dynproc.TestDPM
	TestDPM

	# failing RMA tests from test_rma_nb
	testAccumulate testGetAccumulate
    )
fi

TESTS_SEPARATOR=" -x "
DISABLED_TESTS_STRING=$(printf "${TESTS_SEPARATOR}%s" "${DISABLED_TESTS[@]}")
if [ "${DISABLED_TESTS_STRING}" = "${TESTS_SEPARATOR}" ]; then
    DISABLED_TESTS_STRING=""
fi

for pyver in `py3versions -vs`; do
  PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe \
  OMPI_MCA_btl_tcp_if_include=lo \
  GITHUB_ACTIONS=true MPI4PY_TEST_SPAWN=false \
  mpiexec -v -n $NUM_PROC python$pyver test/runtests.py --verbose ${DISABLED_TESTS_STRING}
done
