#!/bin/bash
set -e

# MPI tests are set up to run on 3 processes.
N_MPI=3
export OMPI_MCA_plm_rsh_agent=/bin/false
export OMPI_MCA_rmaps_base_oversubscribe=1
export OMPI_MCA_btl_base_warn_component_unused=0

DEB_HOST_ARCH=$( dpkg-architecture -qDEB_HOST_ARCH )

# store tests to be skipped in this array variable
declare -a SKIP_TEST_LIST

case " arm64 armhf i386 ppc64el s390x " in
  *\ ${DEB_HOST_ARCH}\ *) SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} test_compute_entity_collisions_2d test_compute_entity_collisions_3d \
                                         test_meshes_on_diagonal test_meshes_with_boundary_edge_overlap_2d test_volume_2d)
esac

case " armel armhf i386 s390x " in
     *\ ${DEB_HOST_ARCH}\ *) SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} test_mixed_assembly_rank0);;
esac

# armhf debci started running out of disk space compiling some forms
case " armhf " in
     *\ ${DEB_HOST_ARCH}\ *) SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]}  test_form  \
	    test_evaluate_dofs_manifolds_affine  test_butcher_schemes_vector[ForwardEuler-True]);;
esac

SKIP_TESTS=""
list_initialised=0
for t in ${SKIP_TEST_LIST[@]}; do
    if [ ${list_initialised} = 0 ]; then
        SKIP_TESTS=$t
        list_initialised=1
    else
        SKIP_TESTS="${SKIP_TESTS} or $t"
    fi
done
if [ "x${SKIP_TESTS}" != "x" ]; then
    SKIP_TESTS="not ( ${SKIP_TESTS} )"
fi

echo "=== python unit test (serial) ==="
if [ "x${SKIP_TESTS}" != "x" ]; then
    echo "skipping tests with SKIP_TESTS=${SKIP_TESTS}"
fi
python3 -m pytest --durations=20 -k "${SKIP_TESTS}" python/test/unit/


declare -a MPI_SKIP_TEST_LIST
MPI_SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} test_function.py test_function_assigner.py test_shared_entities[shared_vertex-mesh_factory8])

MPI_SKIP_TESTS=""
list_initialised=0
for t in ${MPI_SKIP_TEST_LIST[@]}; do
    if [ ${list_initialised} = 0 ]; then
        MPI_SKIP_TESTS=$t
        list_initialised=1
    else
        MPI_SKIP_TESTS="${MPI_SKIP_TESTS} or $t"
    fi
done
if [ "x${MPI_SKIP_TESTS}" != "x" ]; then
    MPI_SKIP_TESTS="not ( ${MPI_SKIP_TESTS} )"
fi

echo "=== python unit test (MPI) ==="
if [ "x${MPI_SKIP_TESTS}" != "x" ]; then
    echo "skipping MPI tests with MPI_SKIP_TESTS=${MPI_SKIP_TESTS}"
fi
mpirun -n ${N_MPI} python3 -m pytest --durations=20 -k "${MPI_SKIP_TESTS}" python/test/unit/ --color=no
