#!/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_NONTERMINALS=2
RAND_DENSITY="50"
RAND_TERMINALS=4
STRING_LENGHTS=6

EXECUTABLES="arand2 atrim2 anormalize2 agenerate2 aepsilon2"
TESTS_DIR="`pwd`/examples2/grammar"

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 generateCFG {
	./arand2 -t CFG --density $RAND_DENSITY --nonterminals $(( $RANDOM % $RAND_NONTERMINALS + 1 )) --terminals $(( $RANDOM % $RAND_TERMINALS + 1 )) 2>/dev/null | ./aepsilon2
}

# $1 = command for conversion. Output of such command must be a grammar !!
# $2 = original grammar
function runTest2 {
	GENERATED_STRINGS="./agenerate2 -t upTo --upto $STRING_LENGHTS"

	OUT=`timeout $TESTCASE_TIMEOUT bash -c "diff <(cat $2 | $1 | $GENERATED_STRINGS ) <(cat $2 | $GENERATED_STRINGS)"`
	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/anormalization.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
		CFG_FILE=$(mktemp)

		cat <(generateCFG) > $CFG_FILE

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

	bgxwait

	outputResults
	clearResults
}

# Chomsky normal form and Greibach normal form
runTest "./anormalize2 -f GNF"
runTest "./anormalize2 -f CNF"
