#!/usr/bin/env bash

# $1 test dir suffix (debug / release)

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

EXECUTABLES="arun2 adeterminize2 aconversions2"
TESTS_DIR="`pwd`/examples2/rte"

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 = str
# $2 = automaton
# $3 = rte
function log {
	echo "----------------------------------------------------------" >> $LOGFILE
	echo "str: " >> $LOGFILE
	cat $1 >> $LOGFILE
	echo "aut: " >> $LOGFILE
	cat $2 >> $LOGFILE
	echo "rte: " >> $LOGFILE
	cat $3 >> $LOGFILE
}

# $1 = genfile
# $2 = automaton
# $3 = rte
function runAcceptTest {
	PATTERN=$(mktemp)
	OUT=`timeout $TESTCASE_TIMEOUT bash -c "./arun2 -i <($1 | tee $PATTERN | ./aconvert2 --string_from_string) -a $2"`
	RET=$?
	if [ $RET == 0 ]; then # ok
		echo $OUT | grep -q "<Bool>true</Bool>"
		if [ $? != 0 ]; then
			RET=1
		fi
	fi

	if [ $RET != 0 ]; then # fail
		log "$PATTERN" "$2" "$3"
		echo -n ""
	fi

	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 ""
}

function run {
	initResults

	# predefined tests first
	TESTS="rte1 rte2 rte3 rte4 rte5 rte6 rte7 rte8 rte9"
	for RTE in $TESTS; do
		echo -n "$RTE: "

		RTE_FILE="$TESTS_DIR/$RTE.xml"
		GEN_FILE="$TESTS_DIR/gen_$RTE.py"
		AUTOMATON_FILE=$(mktemp)

		#echo $RTE_FILE
		#echo $GEN_FILE
		#echo $AUTOMATON_FILE

		./aconversions2 -t pda -a glushkovrtenaive -i $RTE_FILE 2>/dev/null | ./adeterminize2 > $AUTOMATON_FILE

		for i in `seq 1 $TESTCASE_ITERATIONS`; do
			runAcceptTest $GEN_FILE $AUTOMATON_FILE $RTE_FILE
		done

		echo ""
	done

	# random tests
	# TODO

	wait

	outputResults
	clearResults
}

run
