#!/bin/bash

set -efu

# Check that OpenCL isn't totally broken (note that it isn't totally working either)
# Uses device 0 platform 0, i.e. to use a real GPU manually install its opencl-icd before running this
# Mark the test has flaky, the important part is the CPU computation.

# display the OpenCL platform info

export HOME="$AUTOPKGTEST_TMP"

clinfo

# display opencl devices seens via nabu.

# some tests are expected to fail
# so list tests to skip in array variable SKIP_TEST_LIST

declare -a SKIP_TEST_LIST

SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} \
                    test_2D_lossless \
                    test_2D_lossy \
                    test_3D \
                    test_halftomo_right_side[config1] \
                    test_opencl_unsharp)

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[@]}"

for py in $(py3versions -r 2>/dev/null)
do cd "$AUTOPKGTEST_TMP"
   echo "Testing with $py:"
   $py -c "import sys; import nabu.opencl.utils; print(nabu.opencl.utils.get_opencl_devices('all')); sys.exit(0)" 2>&1
   echo "$py -m pytest --pyargs nabu -v -k ${SKIP_TESTS}"
   $py -m pytest --pyargs nabu -v -k "${SKIP_TESTS}"
done
