#!/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 -q DEB_HOST_ARCH )

# test default python only to reduce test burden
PYVER=$(py3versions -dv)

# store demos to be skipped in this array variable
declare -a SKIP_DEMO_LIST

# skip slowest demos to reduce test burden
# leave stokes-taylor-hood and meshview-3D2D running as a test of functionality
SKIP_DEMO_LIST=("${SKIP_DEMO_LIST[@]}" matnest curl-curl stokes-iterative meshview-3D3D meshview-3D1D poisson-disc)

# the -k keyword option doesn't handle the hyphen in navier-stokes or cahn-hilliard with multiple tests skipped
# so need to identify by single name
SKIP_DEMO_LIST=("${SKIP_DEMO_LIST[@]}" cahn hyperelasticity elastodynamics navier elasticity block-assembly-3D buckling)

# matplotlib crashes with malloc(): unaligned tcache chunk detected on several arches
SKIP_DEMO_LIST=("${SKIP_DEMO_LIST[@]}" mesh-quality)

case " i386 armhf " in \
  *\ ${DEB_HOST_ARCH}\ *) SKIP_DEMO_LIST=("${SKIP_DEMO_LIST[@]}" iterative block meshview assignment);; \
esac

# skip slowest demos on riscv64
case " riscv64 " in \
  *\ ${DEB_HOST_ARCH}\ *) SKIP_DEMO_LIST=("${SKIP_DEMO_LIST[@]}" meshview-3D stokes-iterative);; \
esac

case " s390x " in \
  *\ ${DEB_HOST_ARCH}\ *) SKIP_DEMO_LIST=("${SKIP_DEMO_LIST[@]}" matnest);; \
esac

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

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

python3 python/demo/generate-demo-files.py

for pyver in $PYVER; do
    echo "=== python $pyver demo test (serial) ==="
    python$pyver -m pytest -v --durations=20 -k "${SKIP_DEMOS}" python/demo/test.py
done


# these demos sometimes hang (race condition) or fail in the MPI test environment
declare -a MPI_SKIP_DEMO_LIST
MPI_SKIP_DEMO_LIST=("${SKIP_DEMO_LIST[@]}" block-assembly-2D2D contact-vi-tao meshview-2D2D meshview-3D mixed-poisson)

# avoid timeout on riscv64
case " riscv64 " in \
  *\ ${DEB_HOST_ARCH}\ *) MPI_SKIP_DEMO_LIST=("${MPI_SKIP_DEMO_LIST[@]}" built-in-meshes);; \
esac


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

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

for pyver in $PYVER; do
    echo "=== python $pyver demo test (MPI) ==="
    python$pyver -m pytest -v --durations=20 -k "${MPI_SKIP_DEMOS}" python/demo/test.py --mpiexec=mpiexec --num-proc=${N_MPI}
done
