#! /bin/sh

### Run tests

errors=0

[ -n "$RUBY" ] || RUBY=ruby

# run_test program expected_output
run_test() {
    printf '%s...' "$1"
    output=`$2`
    s=$?
    if [ $s -ne 0 ]
    then
        echo "FAIL: exit status is $s"
        errors=$((errors + 1))
    elif [ "$output" != "$3" ]
    then
        echo "FAIL: wrong output"
        errors=$((errors + 1))
    else
	echo pass
    fi
}

run_test2() {
    run_test "$1" "./$1" "$2"
}

run_test1() {
    run_test2 "$1" "`cat $1.out`"
}

run_test2 hello "Hello, world!"

run_test1 call

run_test1 if

run_test1 goto

run_test2 fact 479001600

run_test1 bitwise

run_test1 plusminus

run_test1 many-vars

run_test1 mul

run_test1 div

run_test1 mod

run_test1 set-byte

run_test1 set-word

run_test1 vtable

run_test2 tail-calls ''

run_test1 99bottles

run_test1 gcd

printf 'test_output_name...'
$RUBY test_output_name.rb
[ $? -eq 0 ] || errors=$((errors + 1))

if [ $errors -eq 0 ]
then
    echo All tests passed
else
    echo "$errors tests failed"
    exit 1
fi
