#!/bin/sh
# Run the upstream test suites below src/testsuites against the installed
# interpreter: the active testcases (those listed in zzzz_selection) of
# the suites that upstream's "make test" runs, plus the invariants, each
# in a fresh interpreter so one failure does not cascade into the
# following ones.
set -u
cd "$AUTOPKGTEST_TMP"
cp -r "$OLDPWD/src/testcases" "$OLDPWD/src/testsuites" .

# 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="
main/Quad_FIO.tc main/Quad_FIO1.tc regression/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 main/Format_by_spec.tc regression/ZZZ0_Standard_20x.tc" ;;
esac

pass=0; fail=0; skipped=0; failed=""
for suite in extra main regression invariants; do
    # zzzz_selection: one testcase per line; blank lines and lines
    # starting with # or ⍝ are ignored, and so are surrounding blanks
    for name in $(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' \
                      -e '/^#/d' -e '/^⍝/d' -e '/^$/d' \
                      "testsuites/$suite/zzzz_selection"); do
        tc="testsuites/$suite/$name"
        case " $(echo $skip) " in
            *" $suite/$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 $suite/$name"
            echo "FAIL: $suite/$name"
            cat "$tc.log" 2>/dev/null | tail -n 40
        fi
    done
done

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