#!/bin/sh
# Run the upstream src/testcases/*.tc files against the installed
# interpreter, each in a fresh interpreter so one failure does not
# cascade into the following files.
set -u
cd "$AUTOPKGTEST_TMP"
cp -r "$OLDPWD/src/testcases" .

# Set up the environment that upstream's "make test" provides:
# interpreter id 1010 as the first command line options (⎕AI, ⎕ARG),
# a private $HOME, and library 0 ($PWD/workspaces) for )SAVE and )LOAD,
# containing ASSERT.apl.
export HOME="$AUTOPKGTEST_TMP"
mkdir -p workspaces
cp "$(apl --show_lib_dir)/wslib5/ASSERT.apl" workspaces/

# Testcases that cannot work against the installed package:
# APnnn.tc runs a helper script that is not in the source tarball,
# NativeFunctions.tc and SaveLoad.tc need the lib_template_*.so native
# function examples and Quad_FIO.tc needs tools/TLV_server, none of
# which are installed, and ZZZZ.tc checks for leaks after all other
# testcases ran in the same interpreter.
skip="
APnnn.tc NativeFunctions.tc Quad_FIO.tc SaveLoad.tc ZZZZ.tc
"

# The expected output of these assumes x87 80-bit long double: with a
# wider long double (e.g. 128-bit on arm64, s390x, sparc64) large
# integers such as 2⋆70 are formatted with more correct digits.
case "$(dpkg --print-architecture)" in
    amd64|i386) ;;
    *) skip="$skip Format_by_spec.tc ZZZ0_Standard_20x.tc" ;;
esac

pass=0; fail=0; skipped=0; failed=""
for tc in testcases/*.tc; do
    name=${tc#testcases/}
    case " $(echo $skip) " in
        *" $name "*) skipped=$((skipped+1)); continue ;;
    esac
    if timeout 300 apl --id 1010 --noCIN --noColor -q -T "$tc" \
            > "$tc.out" 2>&1 < /dev/null; then
        pass=$((pass+1))
    else
        fail=$((fail+1)); failed="$failed $name"
        echo "FAIL: $name"
        cat "$tc.log" 2>/dev/null | tail -n 40
    fi
done

echo "passed: $pass  failed: $fail  skipped: $skipped"
[ $fail -eq 0 ] || { echo "failed:$failed"; exit 1; }
