#!/bin/csh -f
#
# Test program for Bertrand
#
#
# This program runs Bertrand over one or several test programs and records the
# differences in the standard and error outputs.
#
#
# Read-only files:
#	program.exp	Bertrand export source code
#	program.loc	Bertrand local source code
#	program.in	Input script to Prolog
#	program.ref	Reference standard output
#	program.referr	Reference error output
#
# Output files:
#	program.out	Most recent standard output
#	program.err	Most recent error output
#	program.refdiff Difference between .out and .ref files
#	program.errdiff Difference between .err and .referr files
#


# Make all files rw by everyone
umask 000


# Version of STICtus to use:
set BERTRAND = "../src/bert"


# clear
#echo "Testing interpreter: Bertrand"
#echo "Date: `date`"
#echo ""
# echo ""
# echo "                       Bertrand test suite"
# echo "                       --------------------"
# echo ""
# echo "interpreter: Bertrand"
# echo ""
# echo ""
# echo ""


    set bertrand_tests = 0
    set bertrand_tests_match = 0
    set bertrand_tests_differ = 0
    set bertrand_tests_out_differ = 0
    set bertrand_tests_err_differ = 0
# Run the test suite
foreach I ($*)
    @ bertrand_tests ++

    # Make sure ref files exist
    touch ../Tests/REFOUT/$I:r.refout
    touch ../Tests/REFERR/$I:r.referr

if(-e $I:r.in) then

# Run Bertrand
#    echo ../Tests/OUT/$I:r.out
#    echo ../Tests/ERR/$I:r.err
    ($BERTRAND $I:r <$I:r.in >../Tests/OUT/$I:r.out.tmp) |& \
        sed "s/[0-9]* msec//"> ../Tests/ERR/$I:r.err.tmp


#	|& \
#        sed "s/.......s cpu (.*)//"
#    echo "Testing $I:r.in"
    
# Check for core dump
   if(-e core) then
	echo "          ***    C O R E   D U M P  ! !       ***"
   endif

# Calculate the differences
# files ending with 2 have whitepace removed (by tr above).
# Whitespace is different in c++ version - appears more correct.
# difference is from "void pretty_output()" in print.c

    (diff  ../Tests/OUT/$I:r.out.tmp ../Tests/REFOUT/$I:r.refout > ../Tests/REFDIFF/$I:r.refdiff.tmp) >& /dev/null
    (diff  ../Tests/ERR/$I:r.err.tmp ../Tests/REFERR/$I:r.referr > ../Tests/ERRDIFF/$I:r.errdiff.tmp) >& /dev/null
#
# Report them to the developer
    if((`wc -c < ../Tests/REFDIFF/$I:r.refdiff.tmp` != 0) || (`wc -c < ../Tests/ERRDIFF/$I:r.errdiff.tmp` != 0)) then
    echo "./work.sh $I:r"
    @ bertrand_tests_differ ++
    else
    echo "echo ./$I:r -- Matches Perfectly"
    @ bertrand_tests_match ++
    endif
    if (`wc -c < ../Tests/REFDIFF/$I:r.refdiff.tmp` == 0) then
    rm -f ../Tests/REFDIFF/$I:r.refdiff.tmp
    else
    @ bertrand_tests_out_differ ++
    endif
    if (`wc -c < ../Tests/ERRDIFF/$I:r.errdiff.tmp` == 0) then
    rm -f ../Tests/ERRDIFF/$I:r.errdiff.tmp
    else
	@ bertrand_tests_err_differ ++
	endif
	end
    echo "echo $bertrand_tests Total Tests completed"
    echo "echo $bertrand_tests_match Total Tests match"
    echo "echo $bertrand_tests_differ Total Tests differ"
    echo "echo $bertrand_tests_out_differ Total Tests out differ"
    echo "echo $bertrand_tests_err_differ Total Tests err differ"
	    endif
