#! /usr/bin/env bash

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

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

SAGITTARIUS=${SAGITTARIUS:-sagittarius}

TESTS="                            \
    base.sps                       \
    case-lambda.sps                \
    char.sps                       \
    complex.sps                    \
    cxr.sps                        \
    eval.sps                       \
    file.sps                       \
    inexact.sps                    \
    lazy.sps                       \
    load.sps                       \
    read.sps                       \
    repl.sps                       \
    time.sps                       \
    write.sps                      \
    process-context.sps            \
    box.sps                        \
    charset.sps                    \
    comparator.sps                 \
    ephemeron.sps                  \
    generator.sps                  \
    hash-table.sps                 \
    ideque.sps                     \
    ilist.sps                      \
    list-queue.sps                 \
    list.sps                       \
    lseq.sps                       \
    rlist.sps                      \
    set.sps                        \
    sort.sps                       \
    stream.sps                     \
    vector.sps                     \
    text.sps                       \
    "

# missing from the list above:
#
# r5rs.sps
# run.sps
# test.sps

echo ${SAGITTARIUS} > testresults
echo `${SAGITTARIUS} < /dev/null` >> testresults
echo >> testresults

runtest ()
{
    sleep 1
    {
        echo "${SRC}/$1"
        time ${SAGITTARIUS} -L "${R7PATH}" -r7 "${SRC}/$1"
    } 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 ${SAGITTARIUS} -L "${R7PATH}" -r7 -- "${SRC}/$1" $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.sps "-- --test-getenv TEMPORARY_ENV_VAR 2776e8e" 0

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

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

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

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