#!/bin/bash
# This code was written by Darren Hakimi and Keir Lauritzen

CARLC="./carlc"
CARL_ENGINE="carl_engine.o"
TMP_DIR="./tmp"
TEST_DIR="./tests"
files=`ls $TEST_DIR/test-*.carl`
PASS_COUNTER=0
FAIL_COUNTER=0

rm -rf ./tmp

if [ ! -d ./tmp ];
then
    mkdir ./tmp
fi

if [ ! -f $CARLC ] || [ ! -f $CARL_ENGINE ];
then
    ./buildcarl
fi

for file in $files
do
    basename=`basename $file`;
    basename=`echo $basename | sed 's/.carl$//'`
    ./buildcarlp $basename;
    
    if [ -f ./$TMP_DIR/$basename.test ];
	then
    	./$TMP_DIR/$basename.test $TEST_DIR/$basename.txt > $TMP_DIR/$basename.generated;
    	if diff -b $TEST_DIR/$basename.out $TMP_DIR/$basename.generated > $TMP_DIR/$basename.diff;
    	then
            let PASS_COUNTER=PASS_COUNTER+1
        	echo "PASS";
    	else
            let FAIL_COUNTER=FAIL_COUNTER+1
    		echo "FAIL";
    	fi
	else
        let FAIL_COUNTER=FAIL_COUNTER+1
        echo "FAIL";
    fi
    echo;

done

echo;
echo "number of passed tests:";
echo $PASS_COUNTER;
echo "number of failed tests:";
echo $FAIL_COUNTER;
echo;

files=`ls $TEST_DIR/fail-*.carl`
PASS_COUNTER=0
FAIL_COUNTER=0

for file in $files
do
    basename=`basename $file`;
    basename=`echo $basename | sed 's/.carl$//'`
    ./buildcarlp $basename;
    
    if [ -f ./$TMP_DIR/$basename.test ];
    then
        ./$TMP_DIR/$basename.test $TEST_DIR/$basename.txt > $TMP_DIR/$basename.generated;
        if diff -b $TEST_DIR/$basename.out $TMP_DIR/$basename.generated > $TMP_DIR/$basename.diff;
        then
            let FAIL_COUNTER=FAIL_COUNTER+1
            echo "FAIL";
        else
            let PASS_COUNTER=PASS_COUNTER+1
            echo "PASS";
        fi
    else
        let PASS_COUNTER=PASS_COUNTER+1
        echo "PASS";
    fi
    echo;

done

echo;
echo "number of passed FAIL tests:";
echo $PASS_COUNTER;
echo "number of failed FAIL tests:";
echo $FAIL_COUNTER;
echo;