#! /usr/bin/env bash

# For running Larceny's R7RS test suite (derived from Racket's R6RS tests).
#
# Usage: ./testall

R7PATH="`pwd`"
SRC="./tests/scheme/run"

KAWA=${KAWA:-"kawa"}
KAWA_OPTIONS=${KAWA_OPTIONS-"--r7rs --no-warn-unused"}

VERBOSE=yes
export VERBOSE

TESTS="                            \
    case-lambda                \
    char                       \
    complex                    \
    cxr                        \
    eval                       \
    file                       \
    inexact                    \
    lazy                       \
    load                       \
    read                       \
    repl                       \
    time                       \
    write                      \
    process-context"

# missing from the list above:
#
# r5rs
# run
# test
#
# base

echo "${KAWA} --version" > testresults
echo >> testresults

runtest ()
{
    sleep 1
    {
        echo "${SRC}/$1"
        time ${KAWA} ${KAWA_OPTIONS} -f tests/scheme/$1.sld -f "${SRC}/$1.sps"
    } 2>&1 | tee -a testresults
}

for program in ${TESTS} ; do
{
    echo $program
    runtest $program
}
done

runtest_with_options ()
{
    sleep 1
    {
        echo "${SRC}/$1 $2"
        time ${KAWA} ${KAWA_OPTIONS} -f tests/scheme/$1.sld "${SRC}/$1.sps" $2
        if [ $? = $3 ]; then
            echo "1 tests passed"
        else
            echo "1 tests failed:"
            echo "Incorrect exit status from exit or emergency-exit:"
            echo $?
            echo "1 of 1 tests failed."
        fi
    } 2>&1 | tee -a testresults
}

export TEMPORARY_ENV_VAR=2776e8e
runtest_with_options process-context "-- --test-getenv TEMPORARY_ENV_VAR 2776e8e" 0

runtest_with_options process-context "-- --test-emergency-exit" 0

runtest_with_options process-context "-- --test-emergency-exit 37" 37

runtest_with_options process-context "-- --test-exit" 0

runtest_with_options process-context "-- --test-exit 46" 46
