#!/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, no preferences file, and the libraries below
# testcases/libroot, with the installed copies of the library files
# that make test copies there.
export HOME="$AUTOPKGTEST_TMP"
libdir=$(apl --show_lib_dir)
cp "$libdir/wslib5/ASSERT.apl" testcases/libroot/workspaces/
cp "$libdir/wslib5/FILE_IO.apl" "$libdir/wslib5/FILE_IO1.apl" \
   testcases/libroot/wslib5/

# Some testcases use paths in the build tree: APnnn.tc starts a second
# interpreter as ./apl, and ZZ64_Regression.tc loads
# ./native/.libs/lib_template_F0.so.  Point them at the installed files.
ln -s "$(command -v apl)" apl
mkdir -p native/.libs
ln -s "$libdir"/lib_template_*.so native/.libs/

# Testcases that cannot work against the installed package:
# Quad_FIO.tc and Quad_FIO1.tc need tools/TLV_server, which is not
# installed, and ZZZZ.tc checks for leaks after all other testcases ran
# in the same interpreter.
skip="
Quad_FIO.tc Quad_FIO1.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 --noPrefs --libroot testcases/libroot \
            --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; }
