#! /bin/sh
#
# Perform a standard set of test on the Kaffe system.  This script should
# be built up as we add more test and so, hopefully, avoid re-releasing
# bugs which have been fixed.
#
# This script contains tests for the new internal threading system
#
# Copyright (c) 1996, 1997
#	Transvirtual Technologies, Inc.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution 
# of this file. 

RESULT=/tmp/kaffer.$$
CHECK=/tmp/kaffec.$$
JAVA=kaffe
JAVAC=javac
overallerror=0
error=0

# If we have a destination directory, clean it and make sure we use it
# for later compilations.
if test "$DESTDIR" != "" ; then
	rm -f $DESTDIR/*.class
else
	DESTDIR="."
fi

compile()
{
	echo ERROR > $RESULT
	$JAVAC -d $DESTDIR $1.java > $RESULT 2>&1
	if test "`cat $RESULT`" != "" ; then
		echo "error compiling"
		cat $RESULT
		error=1
	fi
	rm -f $RESULT $CHECK
}

run()
{
	echo "$2" > $CHECK
	echo ERROR > $RESULT
	(cd $DESTDIR ; $JAVA $1 > $RESULT 2>&1)
	if cmp -s $RESULT $CHECK ; then
		:
	else
		echo "error running"
		echo "  Should have got:"
		cat $CHECK
		echo "  But got instead:"
		cat $RESULT
		error=1
	fi
	rm -f $RESULT $CHECK
}

runtest()
{
	error=0
	echo -n "$1 ... " 1>&2
	compile $1
	if test $error = 0 ; then
		run $1 "$2"
	fi
	if test $error = 0 ; then
		echo "passed" 1>&2
	else
		overallerror=1
	fi
}

ignoretest()
{
	error=0
	echo "$1 ... ignored" 1>&2
}

# Test threadedWaitfor, basically
runtest ExecTest "Okay
Okay"

# Test catching our own death
ignoretest CatchDeath "Caught java.lang.ThreadDeath
Caught java.lang.ThreadDeath
Caught java.lang.ThreadDeath"

# Test various ways of stopping threads
ignoretest ThreadStop "Test 1: Stop a thread that's blocked on itself
Target (BlockThread) running...
 Locked ...
 Success. Target is dead.
 Sleeping while still holding on to target
 Releasing target lock
 Handling my own: java.lang.ThreadDeath
 Am I alive? Answer: false
Test 2: Stop a thread that's blocked in a monitor
Target (MonitorThread) running...
 Success. Target is dead.
Test 3: Stop a thread that's running
Target (RunThread) running...
 Success. Target is dead.
Test 4: Stop a thread that's done
 Success. Target is dead.
Test 6: Have a thread stop itself
 Success. Target is dead.
All tests completed"

runtest DeadThread "1 true
true 1"

runtest tthrd1 "main start
true 1
main end"

# All done okay
exit $overallerror
