#!/usr/bin/env bash

source bash_multithread.sh

# $1 test dir suffix (debug / release)

# SETTINGS
TESTCASE_ITERATIONS=100
TESTCASE_TIMEOUT=10
LOGFILE="log_tests.txt"

RAND_STATES=18
RAND_DENSITY="2.5"
RAND_ALPHABET=4

EXECUTABLES="arand2 aepsilon2 atrim2 adeterminize2 aminimize2 anormalize2 acompare2 aconversions2"
TESTS_DIR="`pwd`/examples2/automaton"

RES_GOOD=
RES_FAIL=
RES_TIME=
RES_SEGV=
RES_UNKN=


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

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

cd bin-$1/
rm -f $LOGFILE

JOBS=$2
if [ -z "$JOBS" ]; then
	JOBS=1
fi

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

# $1 = conv command
# $2 = return code
# $3 = input automaton
# $4 = output of conversion
function log {
	echo "----------------------------------------------------------" >> $LOGFILE
	echo "conv: " $1 >> $LOGFILE
	echo "ret: " $2 >> $LOGFILE
	echo "input automaton:" >> $LOGFILE
	cat  "$3" >> $LOGFILE
	echo "command out:" >> $LOGFILE
	echo "$4" >> $LOGFILE
}

function generateNFA {
	./arand2 -t FSM --density $RAND_DENSITY --states $(( $RANDOM % $RAND_STATES + 1 )) --terminals $(( $RANDOM % $RAND_ALPHABET + 1 )) 2>/dev/null
}

# $1 = command for conversion. Output of such command must be (eps-)NFA !!
# $2 = automaton
function runTest2 {
	MDFA="./aepsilon2 -e | ./adeterminize2 | ./atrim2 -e | ./aminimize2 | ./anormalize2 --labels automaton"

	OUT=`timeout $TESTCASE_TIMEOUT bash -c "./acompare2 <(cat $2 | $1 | $MDFA ) <(cat $2 | $MDFA)"`
	RET=$?

	if [ $RET != 0 ]; then # fail
		log "$1" $RET "$2" "$OUT"
	fi

	rm $2

	if [ $RET == 124 ]; then # timeout
		registerResult 2
		return 2
	elif [ $RET -ge 124 ]; then #segv
		registerResult 3
		return 3
	elif [ $RET != 0 ]; then # fail
		registerResult 1
		return 1
	else
		registerResult 0
		return 0
	fi
}

function registerResult {
	case $1 in
		0)
			echo -n "."
			echo -n "+1" >> $RES_GOOD
			;;
		1)
			echo -n "x"
			echo -n "+1" >> $RES_FAIL
			;;
		2)
			echo -n "T"
			echo -n "+1" >> $RES_TIME
			;;
		3)
			echo -n "E"
			echo -n "+1" >> $RES_SEGV
			;;
		*)
			echo -n "?"
			echo -n "+1" >> $RES_UNKN
			;;
	esac
}

function initResults {
	RES_GOOD=$(mktemp)
	echo -n "0" > $RES_GOOD
	RES_FAIL=$(mktemp)
	echo -n "0" > $RES_FAIL
	RES_TIME=$(mktemp)
	echo -n "0" > $RES_TIME
	RES_SEGV=$(mktemp)
	echo -n "0" > $RES_SEGV
	RES_UNKN=$(mktemp)
	echo -n "0" > $RES_UNKN
}

function clearResults {
	rm $RES_GOOD
	rm $RES_FAIL
	rm $RES_TIME
	rm $RES_SEGV
	rm $RES_UNKN
}

function outputResults {
	echo "" >> $RES_GOOD
	echo "" >> $RES_FAIL
	echo "" >> $RES_TIME
	echo "" >> $RES_SEGV
	echo "" >> $RES_UNKN

	# summary
	echo -ne "\n\t"
	echo "RES: GOOD:" $(bc < $RES_GOOD) ", FAIL:" $(bc < $RES_FAIL) ", TIME:" $(bc < $RES_TIME) ", SEGV:" $(bc < $RES_SEGV), "UNKN:" $(bc < $RES_UNKN)
	echo ""
}

# $1 - aconversions2 sequence
function runTest {
	echo $1
	echo -ne "\t"

	initResults
	bgxgrp=""

	# predefined tests first
	for FILE in `ls $TESTS_DIR/aconversion.test*`; do
		FILE_COPY=$(mktemp)

		cat $FILE > $FILE_COPY

		bgxlimit ${JOBS} runTest2 "$1" "$FILE_COPY"
	done

	bgxwait

	echo -n " | "

	# random tests
	for i in $(seq 1 $TESTCASE_ITERATIONS );
	do
		NFA_FILE=$(mktemp)

		cat <(generateNFA) > $NFA_FILE

		bgxlimit ${JOBS} runTest2 "$1" "$NFA_FILE"
	done

	bgxwait

	outputResults
	clearResults
}

# FA -> RG -> FA
# covers: FA -> LRG, FA -> RRG, RRG <-> LRG, RRG -> FA, LRG -> FA
runTest "./aconversions2 -t rg -a outgoing  | ./anormalize2 --form leftRG  | ./aconversions2 -t fa"
runTest "./aconversions2 -t rg -a incoming | ./anormalize2 --form rightRG | ./aconversions2 -t fa"

# FA -> RE -> FA
# covers: FA -> RE (Brzozowski algebraic, elimination), RE -> FA (Brzozowski derivation, Thompson, Glushkov)
runTest "./aconversions2 -t re -a algebraic   | ./aconversions2 -t fa -a brzozowski"
runTest "./aconversions2 -t re -a algebraic   | ./aconversions2 -t fa -a thompson"
runTest "./aconversions2 -t re -a algebraic   | ./aconversions2 -t fa -a glushkov"
runTest "./aconversions2 -t re -a elimination | ./aconversions2 -t fa -a brzozowski"
runTest "./aconversions2 -t re -a elimination | ./aconversions2 -t fa -a thompson"
runTest "./aconversions2 -t re -a elimination | ./aconversions2 -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 "./aconversions2 -t re -a algebraic   | ./aconversions2 -t rg -a brzozowski | ./anormalize2 --form leftRG | ./aconversions2 -t fa"
runTest "./aconversions2 -t re -a algebraic   | ./aconversions2 -t rg -a glushkov   | ./anormalize2 --form leftRG | ./aconversions2 -t fa"
runTest "./aconversions2 -t re -a elimination | ./aconversions2 -t rg -a brzozowski | ./anormalize2 --form leftRG | ./aconversions2 -t fa"
runTest "./aconversions2 -t re -a elimination | ./aconversions2 -t rg -a glushkov   | ./anormalize2 --form leftRG | ./aconversions2 -t fa"

# FA -> RRG -> RE -> FA
# covers: FA -> RRG, FA -> LRG, RRG -> RE, LRG -> RE, RE -> FA (Brz. derivation, Thompson, Glushkov)
runTest "./aconversions2 -t rg -a outgoing  | ./aconversions2 -t re | ./aconversions2 -t fa -a brzozowski"
runTest "./aconversions2 -t rg -a incoming | ./aconversions2 -t re | ./aconversions2 -t fa -a brzozowski"
runTest "./aconversions2 -t rg -a outgoing  | ./aconversions2 -t re | ./aconversions2 -t fa -a thompson"
runTest "./aconversions2 -t rg -a incoming | ./aconversions2 -t re | ./aconversions2 -t fa -a thompson"
runTest "./aconversions2 -t rg -a outgoing  | ./aconversions2 -t re | ./aconversions2 -t fa -a glushkov"
runTest "./aconversions2 -t rg -a incoming | ./aconversions2 -t re | ./aconversions2 -t fa -a glushkov"
