#!/bin/csh -f
#
# Test program for Wild-LIFE
#
#	test *.lf
#   or
#	test program
#
# This program run Wild-LIFE over one or several test programs and records the
# differences in the standard and error outputs.
#
#
# Read-only files:
#	program.lf	Life source code
#	program.in	Life user input
#	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 Wild-LIFE to use:
set WLIFE = "~/bin/wild_life"


# clear
#echo "Testing interpreter: $WLIFE"
#echo "Date: `date`"
#echo ""
# echo ""
# echo "                       Wild-LIFE test suite"
# echo "                       --------------------"
# echo ""
# echo "interpreter: $WLIFE"
# echo ""
# echo ""
# echo ""


    set life_tests = 0
    set life_tests_match = 0
    set life_tests_differ = 0
    set life_tests_out_differ = 0
    set life_tests_err_differ = 0
# Run the test suite
foreach I ($*)
    @ life_tests ++

    # Make sure ref files exist
    touch REFOUT_UPDATED/$I:r.refout
    touch REFERR_UPDATED/$I:r.referr

if(-e LF/$I:r.lf) then

# Remove any core dumps
   rm -f core

# Run Wild-LIFE
   (echo 'load("LF/' $I:r.lf '")?' | tr -d ' ' ;echo "" ; \
	cat IN/$I:r.in  ; echo "halt?") | \
	($WLIFE |  \
egrep -v '(Loading|already loaded|Version|customizing|Copyright|Garbage|Exiting|X interface)' > OUT/$I:r.out.tmp \
        ) |& \
        sed "s/.......s cpu (.*)//" > ERR/$I:r.err.tmp

# Check for core dump
   if(-e core) then
	echo "          ***    C O R E   D U M P  ! !       ***"
   endif

    (tr --delete '[:space:]' < OUT/$I:r.out.tmp > OUT/$I:r.out2.tmp)
    (tr --delete '[:space:]' < REFOUT_UPDATED/$I:r.refout >REFOUT_UPDATED/$I:r.refout2.tmp)
    (tr --delete '[:space:]' < ERR/$I:r.err.tmp > ERR/$I:r.err2.tmp)
    (tr --delete '[:space:]' < REFERR_UPDATED/$I:r.referr >REFERR_UPDATED/$I:r.referr2.tmp)
    

# 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  OUT/$I:r.out.tmp REFOUT_UPDATED/$I:r.refout > REFDIFF/$I:r.refdiff.tmp) >& /dev/null
    (diff  OUT/$I:r.out2.tmp REFOUT_UPDATED/$I:r.refout2 > REFDIFF/$I:r.refdiff2.tmp) >& /dev/null
#    (diff  ERR/$I:r.err.tmp REFERR_UPDATED/$I:r.referr > ERRDIFF/$I:r.errdiff.tmp) >& /dev/null
    (diff  ERR/$I:r.err2.tmp REFERR_UPDATED/$I:r.referr2 > ERRDIFF/$I:r.errdiff2.tmp) >& /dev/null
    
# Report them to the developer
    if((`wc -c < REFDIFF/$I:r.refdiff2.tmp` != 0) || (`wc -c < ERRDIFF/$I:r.errdiff2.tmp` != 0)) then
    echo "./work.sh $I:r"
    @ life_tests_differ ++
    else
    echo "echo ./$I:r -- Matches Perfectly"
    @ life_tests_match ++
    endif
    if (`wc -c < REFDIFF/$I:r.refdiff2.tmp` == 0) then
    rm -f REFDIFF/$I:r.refdiff.tmp
    rm -f REFDIFF/$I:r.refdiff2.tmp
    else
    @ life_tests_out_differ ++
    endif
    if (`wc -c < ERRDIFF/$I:r.errdiff2.tmp` == 0) then
    rm -f ERRDIFF/$I:r.errdiff.tmp
    rm -f ERRDIFF/$I:r.errdiff2.tmp
    else
	@ life_tests_err_differ ++
	endif
	end
    echo "echo $life_tests Total Tests completed"
    echo "echo $life_tests_match Total Tests match"
    echo "echo $life_tests_differ Total Tests differ"
    echo "echo $life_tests_out_differ Total Tests out differ"
    echo "echo $life_tests_err_differ Total Tests err differ"
	    endif
