#!/usr/bin/env bash

TESTCASE_ITERATIONS=200
TESTCASE_TIMEOUT=5
LOGFILE="log_tests.txt"

RAND_STATES=18
RAND_DENSITY="2.5"
RAND_ALPHABET=4

EXECUTABLES="arand aepsilon atrim adeterminize aminimize anormalize adiff.automaton aconversion"

# ----------------------------

for FILE in $EXECUTABLES; do
	if [ ! -f bin/$FILE ]; then
		echo "Executable" $FILE "is required for testing. Make sure it is in bin folder."
		exit 1
	fi
done

cd bin/
rm -f $LOGFILE

# ----------------------------

function mDFA {
	echo "$1" | ./aepsilon | ./atrim | ./adeterminize -t FSM | ./aminimize | ./anormalize
}

function compare {
	echo "$1" > tmp1.xml
	echo "$2" > tmp2.xml

	# relies on ret code by adiff.automaton
	./adiff.automaton tmp1.xml tmp2.xml
	RET=$?
	rm tmp1.xml tmp2.xml

	return $RET
}

# $1 = command for conversion. Output of such command must be (eps-)NFA !!
function runTest2 {
	TMPNFA="nfa.xml"

	./arand -d $RAND_DENSITY -n $(( $RANDOM % $RAND_STATES + 1 )) -a $(( $RANDOM % $RAND_ALPHABET + 1 )) > $TMPNFA

	TMP=$(timeout $TESTCASE_TIMEOUT bash -c "cat $TMPNFA | $1")
	RETTMP=$?
	if [ $RETTMP -ge 123 ]; then
		return 2
	fi
	
	mDFA1=$(mDFA "$(cat $TMPNFA)")
	mDFA2=$(mDFA "$TMP")

	CMP=$(compare "$mDFA1" "$mDFA2")
	if [ $? == 0 ]; then 
		rm nfa.xml
		return 0
	else
		echo "-----------------------------------------" >> $LOGFILE
		echo "$1" >> $LOGFILE
		echo "$CMP" >> $LOGFILE
		echo $(cat nfa.xml) >> $LOGFILE 
		rm nfa.xml
		return 1
	fi
}

function runTest {
	ITERS=$TESTCASE_ITERATIONS
	RES_GOOD=0
	RES_BAD=0
	RES_TIMEOUT=0

	if [ $# == 0 ] || [ $# -gt 2 ]; then
		echo "Invalid runTest call."
		exit
	elif [ $# == 2 ]; then
		ITERS=$2
	fi


	echo $1
	echo -ne "\t"
	for i in $(seq 1 $ITERS );
	do

		runTest2 "$1"
		RETTEST=$?

		if [ $RETTEST == 0 ]; then
			echo -n "."
			RES_GOOD=$((RES_GOOD + 1))
		elif [ $RETTEST == 1 ]; then
			echo -n "x"
			RES_BAD=$((RES_BAD + 1))
		elif [ $RETTEST == 2 ]; then
			echo -n "T"
			RES_TIMEOUT=$((RES_TIMEOUT + 1))
		elif [ $RETTEST == 3 ]; then
			echo -n "F"
			RES_BAD=$((RES_BAD + 1))
		else
			echo -n "?"
		fi
	done

	echo -ne "\n\t"
	echo "RES: GOOD:" $RES_GOOD ", BAD:" $RES_BAD ", TIMEOUT:" $RES_TIMEOUT
	echo ""
}

# FA -> RG -> FA
# covers: FA -> LRG, FA -> RRG, RRG <-> LRG, RRG -> FA, LRG -> FA
runTest "./aconversion -t RRG | ./aconversion -t LRG | ./aconversion -t FA" 
runTest "./aconversion -t LRG | ./aconversion -t RRG | ./aconversion -t FA"

# FA -> RE -> FA
# covers: FA -> RE (Brzozowski algebraic, elimination), RE -> FA (Brzozowski derivation, Thompson, Glushkov)
runTest "./aconversion -t RE -a algebraic | ./aconversion -t FA -a brzozowski"
runTest "./aconversion -t RE -a algebraic | ./aconversion -t FA -a thompson"
runTest "./aconversion -t RE -a algebraic | ./aconversion -t FA -a glushkov "
runTest "./aconversion -t RE -a elimination | ./aconversion -t FA -a brzozowski"
runTest "./aconversion -t RE -a elimination | ./aconversion -t FA -a thompson"
runTest "./aconversion -t RE -a elimination | ./aconversion -t FA -a glushkov"

# FA -> RE -> RRG -> LRG -> FA
# covers: FA -> RE (Brz. algebraic, elimination), RE -> RRG ( Brz. derivation, Glushkov), RRG -> LRG, LRG -> FA
runTest "./aconversion -t RE -a algebraic | ./aconversion -t RRG -a brzozowski | ./aconversion -t LRG | ./aconversion -t FA"
runTest "./aconversion -t RE -a algebraic | ./aconversion -t RRG -a glushkov | ./aconversion -t LRG | ./aconversion -t FA"
runTest "./aconversion -t RE -a elimination | ./aconversion -t RRG -a brzozowski | ./aconversion -t LRG | ./aconversion -t FA"
runTest "./aconversion -t RE -a elimination | ./aconversion -t RRG -a glushkov | ./aconversion -t LRG | ./aconversion -t FA"

# FA -> RRG -> RE -> FA
# covers: FA -> RRG, FA -> LRG, RRG -> RE, LRG -> RE, RE -> FA (Brz. derivation, Thompson, Glushkov)
runTest "./aconversion -t RRG | ./aconversion -t RE | ./aconversion -t FA -a brzozowski"
runTest "./aconversion -t LRG | ./aconversion -t RE | ./aconversion -t FA -a brzozowski"
runTest "./aconversion -t RRG | ./aconversion -t RE | ./aconversion -t FA -a thompson"
runTest "./aconversion -t LRG | ./aconversion -t RE | ./aconversion -t FA -a thompson"
runTest "./aconversion -t RRG | ./aconversion -t RE | ./aconversion -t FA -a glushkov"
runTest "./aconversion -t LRG | ./aconversion -t RE | ./aconversion -t FA -a glushkov"
