#!/bin/sh
#####
# NAME
#	test-and-compare - execute test program and check the result
# VERSION
#	$Id$
# CHANGELOG
#	$Log$
# USAGE
#	test-and-compare 
#
#	This program execute all "*.out" files in current directory.
#	This script execute the program and store the result to "*.out".
#	And compare the result with "./OK/*.result.OK" ;
#	

message(){
	echo "test-and-compare: $1" >&2 ;
}

error(){
	echo "test-and-compare: [ERROR] $1" >&2 ;
	exit 1 ;
}

TARGETS=`ls ./*.out` ;
for PROGRAM in $TARGETS ; do
	NAME=`echo $PROGRAM | sed 's/\.out$//'` ;
	EXPECTED_LIST=`ls ./OK/${NAME}.result.OK*` ;
	RESULT="./${NAME}.result" ;

	if [ ! -x $PROGRAM ] ; then
		error "test program \"$PROGRAM\" is not found." ;
	fi
	if [ $PROGRAM = $NAME ] ; then
		error "test program name \"$PROGRAM\" is not finished by \".out\"" ;
	fi
	if [ -z $EXPECTED_LIST ] ; then
		error "test expected value file such as \"./OK/${NAME}.result.OK\" is not found." ;
	fi

	message "executing $PROGRAM" ;
	if $PROGRAM | tee $RESULT ; then
		message "execution was succeeded" ;
	else
		message "execution was failed ($PROGRAM)" ;
		exit 1 ;
	fi

	TESTOK="NO" ;
	for EXPECTED in $EXPECTED_LIST ; do
		message "compareing $RESULT with $EXPECTED" ;
		if diff $RESULT $EXPECTED ; then
			message "no difference ($PROGRAM)" ;
			TESTOK="YES" ;
		else
			message "there are any difference ($PROGRAM)" ;
		fi
	done

	if [ $TESTOK = "NO" ] ; then
		error "not matched with $EXPECTED_LIST" ;
	fi
done

exit 0 ;

