#!/bin/bash
set -efu

arch=$(dpkg-architecture -qDEB_HOST_ARCH)

# list tests to skip in array variable SKIP_TEST_LIST
declare -a SKIP_TEST_LIST

# this subset of demo tests (test_tutorials.py::test_geoFiles)
# only passes on amd64 and armhf
if [[ "$arch" != "amd64" && "$arch" != "armhf" ]]; then
    SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} boxcyl.geo-mp11-5 \
        cylsphere.geo-mp58-4 cylsphere.geo-mp61-1 cylsphere.geo-mp64-4 \
        hinge.stl-mp93-5 hinge.stl-mp99-5 part1.stl-mp129-1 \
        part1.stl-mp123-1 part1.stl-mp126-4 part1.stl-mp132-4 \
        sculpture.geo-mp143-4 sculpture.geo-mp149-4 \
        sphereincube.geo-mp162-5 sphereincube.geo-mp168-5 \
        twobricks.geo-mp195-2 twobricks.geo-mp199-0 twobricks.geo-mp201-2 twobricks.geo-mp202-3 \
        twocubes.geo-mp201-2 twocubes.geo-mp205-0 twocubes.geo-mp207-2 twocubes.geo-mp208-3)
fi

if [ "$arch" = "armel" ]; then
    # ngsolve import-or-skip triggers segfaults on armel
    SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} test_mesh_elements_numpy_array_access \
        test_boundarylayer2 test_wrong_orientation test_splitted_surface test_pyramids \
        test_csg2d test_rect_with_two_holes test_unit_square test_box_and_cyl \
        test_2_polyhedra test_BBNDsave)

    # too many pytest segfaults on armel. Skip all test_geoFiles
    SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} test_geoFiles)
fi

if [ "$arch" = "i386" ]; then
    SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} boxcyl.geo-mp10-4 hinge.stl-mp95-1 lense square)
fi

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 "skipping tests with SKIP_TEST_LIST=${SKIP_TEST_LIST[@]}"


cd tests/pytest

echo "Run serial python test"
pytest-3 -k "${SKIP_TESTS}"

if [[ "$arch" == "i386" || "$arch" == "armel" || "$arch" == "armhf" ]]; then
  # the mpi test fails on 32 bit archs with mpich
  echo -e "\n\nSkipping MPI python test on $arch"
else
  echo -e "\n\nRun MPI python test"
  PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe mpirun -n 4 python3 -m pytest --with-mpi test_mpi4py.py
fi
