#!/bin/sh

set -u
set -e

tests="\
xamax                   \
xasum                   \
xaxpy                   \
xaxpybatched            \
xcol2im                 \
xconvgemm               \
xcopy                   \
xdot                    \
xdotc                   \
xdotu                   \
xgbmv                   \
xgemm                   \
xgemmbatched            \
xgemmstridedbatched     \
xgemv                   \
xger                    \
xgerc                   \
xgeru                   \
xhad                    \
xhbmv                   \
xhemm                   \
xhemv                   \
xher                    \
xher2                   \
xher2k                  \
xherk                   \
xhpmv                   \
xhpr                    \
xhpr2                   \
xim2col                 \
xnrm2                   \
xomatcopy               \
xsbmv                   \
xscal                   \
xspmv                   \
xspr                    \
xspr2                   \
xswap                   \
xsymm                   \
xsymv                   \
xsyr                    \
xsyr2                   \
xsyr2k                  \
xsyrk                   \
xtbmv                   \
xtpmv                   \
xtrmm                   \
xtrmv                   \
xtrsm                   \
xtrsv"

gcc -std=c11 -Wall -pedantic -o ${AUTOPKGTEST_TMP}/find_pocl debian/tests/find_pocl.c -lOpenCL

pocl_platform_and_device=$(${AUTOPKGTEST_TMP}/find_pocl)
pocl_platform=$(echo $pocl_platform_and_device | awk '{ print $1 }')
pocl_device=$(echo $pocl_platform_and_device | awk '{ print $2 }')

echo "POCL is platform ${pocl_platform}, device ${pocl_device}."

set +e

errors=0

for test in $tests
do
    echo "--------------------------------------------------"
    echo "Running test set ${test}."
    echo "--------------------------------------------------"
    output=$(/usr/bin/clblast_test_${test} -platform $pocl_platform -device $pocl_device)
    if [ $? -ne 0 ]
    then
        errors=$(($errors+1))
    fi
    printf "%s\n" "$output" | ansi2txt
done

echo "--------------------------------------------------"
echo "$errors errors encounted."
echo "--------------------------------------------------"

if [ $errors -ne 0 ]
then
    exit 1
fi

