#!/bin/sh
#
# A script to test all the test cases in this directory using the setup
# (runtime, library, compiler etc) from a given workspace. The workspace
# is specified via the mmc command to execute to create the test case
# executables; it is intended that this command be a wrapper around tools/lmc.

usage="usage: test_tabling [-s] [-g grade] mmc_cmd"

simple_only=false
grade="asm_fast.gc.mm"
while test $# -gt 0
do
	case "$1" in
		-s)	simple_only=true
			grade="asm_fast.gc"
			;;
		-g)	grade="$2"
			shift
			;;
		-*)	echo "$usage"
			exit 1
			;;
		*)	break
			;;
	esac
	shift
done

case $# in
	1)	mmc_cmd=$1
		;;
	*)	echo "$usage"
		exit 1
		;;
esac

if $simple_only
then
	testcases=`mmake echo_simple_nonloop_progs`
else
	testcases=`mmake echo_minimal_nonloop_progs echo_simple_nonloop_progs`
fi

status=0
failed=""

for testcase in $testcases
do
	echo "testing $testcase"

	/bin/rm $testcase $testcase.mmake $testcase.res > /dev/null 2>&1
	if $mmc_cmd --grade "$grade" $testcase.m
	then
		mmake $testcase.res > $testcase.mmake 2>&1
		if test -f $testcase.res
		then
			if test -s $testcase.res
			then
				echo "unexpected output from $testcase"
				failed="$failed $testcase"
				cat $testcase.res
			else
				mmake $testcase.depend > /dev/null 2>&1
				mmake $testcase.realclean > /dev/null 2>&1
				/bin/rm $testcase $testcase.out $testcase.res $testcase.res1 > /dev/null 2>&1
			fi
		else
			echo "execution of $testcase failed"
			failed="$failed $testcase"
			cat $testcase.mmake
		fi
	else
		echo "compilation of $testcase failed"
		failed="$failed $testcase"
	fi
done

if test "$failed" != ""
then
	status=1
	echo "failed test cases: $failed"
fi

exit $status
