#!/bin/bash
set -e

# MPI tests are set up to run on 3 processes.
N_MPI=3
export PRTE_MCA_plm_ssh_agent=/bin/false
export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe
export OMPI_MCA_btl_base_warn_component_unused=0

DEB_HOST_ARCH=$( dpkg-architecture -qDEB_HOST_ARCH )

PYVER=$(py3versions -sv)
case " armel riscv64 " in \
  *\ ${DEB_HOST_ARCH}\ *) PYVER=$(py3versions -dv);; \
esac

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

case " arm64 armhf i386 ppc64el riscv64 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

# riscv64 debci fails due to time out: remove the slowest tests
case " riscv64 " in
     *\ ${DEB_HOST_ARCH}\ *) SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} \
	test_assembler \
	test_assembly_derivatives \
	test_coordinate_derivative \
	test_dirichlet_bc \
	test_dofmap.py \
	test_function_assigner \
	test_manifolds \
	test_mixedassembler \
	test_multimesh \
	test_nullspace \
	test_point_source \
	test_RK_solver \
	test_solve_result_against_reference \
	test_symbolic_geometry_assembly \
	test_XDMF.py \
	test_matrix \
	test_tensor_layout \
	test_vector \
	test_mesh);;
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

if [ "x${SKIP_TESTS}" != "x" ]; then
    echo "skipping tests with SKIP_TESTS=${SKIP_TESTS}"
fi

for pyver in $PYVER; do
    echo "=== python $pyver unit test (serial) ==="
    python$pyver -m pytest --durations=20 -k "${SKIP_TESTS}" python/test/unit/
done


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

if [ "x${MPI_SKIP_TESTS}" != "x" ]; then
    echo "skipping MPI tests with MPI_SKIP_TESTS=${MPI_SKIP_TESTS}"
fi

for pyver in $PYVER; do
    echo "=== python $pyver unit test (MPI) ==="
    mpirun -n ${N_MPI} python$pyver -m pytest --durations=20 -k "${MPI_SKIP_TESTS}" python/test/unit/ --color=no
done
