#!/bin/csh -f
#

#
# A regressions test for coral (template)
# In order to use this template, change just the list of tests specified
# in the variable testlist.
# This is the ONLY place in the template that needs to be modified !!!
# IF you want to mess with the command line options that coral is invoked
# with, then modify coralopts as well.
#

#
# Test script begins
#

#
# NOTE: If you wish to add new files to be tested, add them to this list.
#       Please keep the list of files ordered alpahbetically.
# 	For each test "testname", there must be a run file called "testname.X" in
#	the testrun directory.
#
set testlist =   ( \
	declac1 \
	declac12 \
	declac2 \
	declac4 \
	declad2 \
	declad3 \
	declad4 \
	declad5 \
	declad7 \
	declad8 \
	declne1 \
	declne2 \
	declne3 \
	declne4 \
	declne5 \
	declse1 \
	declse2 \
	declse3 \
	declse4 \
	declse5 \
	declse6 \
	declse7 \
	extens1 \
	mod3 \
	mod4 \
	mod5 \
	mod6 \
	mod7 \
	mod8 \
	mod9 \
	pipe1 \
	pipe2 \
	pipe3 \
	pipe4 \
		)


#	impmod3 \
#	impmod4 \
#	pipe12 \
#
# command line options to coral
set coralopts = "-a binding -C -I -M -P -q -V"

#
# Set a few varibles
#
##
set coral = $CORALROOT/bin/coral	# command to call coral
set rg = $cwd				# current working directory
set rundir = $rg/testrun		# where testing files are 
set ansdir = $rg/testans		# where benchmark files are.
set dffdir = $rg/testdiff		# where differences go.
set filter = "awk -f $CORALROOT/bin/strip.awk "	# filter out messages
set diff = diff				# set the diff program
set argc = $#argv			# the number of args from command line

# if sysV sort is available, use it
#set sort = sort5	               	
#
# if BSD sort is being used, some of the programs will produce output lines 
# that are too long for sort to handle.
#
set sort = sort
#
# set up the single output files.
#
@ x = 0
foreach i ($argv)			# for each i in 
	@ x++
	switch($argv[$x])
	case "-h":
	case "-help":
		goto usage
		breaksw
	case "-s":
		if($argc == 1) goto usage
		@ y = $x + 1
		set sfiles = "$argv[${y}]"
		goto next
		breaksw
	default:
		echo Error in specifying command line options
		goto usage
		breaksw
	endsw
end

next:
#
# now, if sfiles has not been set, set it to the defaults
#
if($?sfiles == 0) then
set sfiles = ($testlist)
endif

onintr end 				# exit gracefully on end

#
#
# If the answer directory has not been written, create it and put all the output into the
# answer directory.
#
#
if(!(-e $ansdir)) then
	mkdir $ansdir
	echo " "
	echo "Creating the answer directory."
	echo " "
	set createoutput
endif

if(!(-e $dffdir)) then
	mkdir $dffdir
	echo "Creating the diff directory."
endif

foreach i (`ls $dffdir`)
	set dffsexist
end

if($?dffsexist) then
	echo " "
	echo "Cleaning up old files."
	echo " "
	/bin/rm -f $dffdir/*.diff >& /dev/null
endif


####
#
#
#
# This will run a set of files to make sure that they are giving the right answers.
#
####
initial:
foreach i ($sfiles)
	echo -n "Testing the " $i " program..."
	if($?createoutput) then
		$coral $coralopts < $rundir/${i}.X |& $filter | $sort >& $ansdir/${i}.ans
		echo "done."
		grep -si error $ansdir/${i}.ans
		@ s = $status
		if ($s == 0) echo "*** Errors occurred creating" $ansdir/${i}.ans
	else
		$coral $coralopts < $rundir/${i}.X |& $filter | $sort |& $diff - $ansdir/${i}.ans > $dffdir/${i}.diff

		echo "done."
	endif
end

########
#
# Clean up.
#
#######
end:
if(!($?createoutput)) then
foreach i (`ls $dffdir`)
	set i = $dffdir/$i
	if(!(-z $i)) then
		set s = $i:t
		set s = $s:r
		echo "There are differences between expected output and actual ouput in the $s file."
	endif
end
endif
echo " "
echo End of tests.
exit 0

usage:

echo Usage: test_suite \[-help\] \[-s \"list of files to test\"\]
exit 1


