#!/bin/bash
make clean
make
FILE_DIR=./regression_test

echo -e "\n\n*****************\\n\\nSTART TEST\\n\\n*****************\\n\\n"

tests=0
errors=0

shouldfail=0
didnotfail=0



for i in $(ls $FILE_DIR); do
    # check if the name of the file contains bad
    bad=$(echo $i | grep -ic bad)
    if [ $bad -gt 0 ]
    then
	#echo "this is bad $i"
	# can be either a one liners file or an entire file of bad stuff
	BAD=$(echo $i | grep -c BAD)
	if [ $BAD -gt 0 ]
	then
	    #echo "is a single file"
	    #is a single file which should fail
	    shouldfail=$((shouldfail+1))
	    ./main $FILE_DIR/$i &> log	
	    if [ $? -eq 0 ]
	    then
		echo "did not fail (FILE) $i : $(less log)"
		didnotfail=$((didnotfail+1))
	    fi  
	else
	    #echo "one liners"
	    #is a file of one liners
	    cat $FILE_DIR/$i | while read p; do
		echo $p > bad_line
		./main bad_line &> log	
		if [ $? -eq 0 ]
		then
		    echo "did not fail (LINE): $(less bad_line)"
		    errors=$((errors+1))
		fi
	
	    done
	    rm bad_line
	fi
    else
	#it's a file which should not fail
	tests=$((tests+1))
	./main $FILE_DIR/$i &> log	
	if [ $? -gt 0 ]
	then
	    echo "failed $i : $(less log)"
	    errors=$((errors+1))
	fi
    fi
done

if [ $errors -eq 0 ]
then
    echo -e "\nCONGRATS you passed $tests tests!\n"
else
    echo -e "\nYou failed on $errors test cases out of $tests \nSHAME ON YOU"
fi

if [ $didnotfail -eq 0 ]
then
    echo -e "\nCONGRATS you passed $shouldfail bad tests!\n"
else
    echo -e "\nYou failed on $didnotfail bad files out of $shouldfail\nSHAME ON YOU"
fi