#!/usr/bin/env bash

# $1 test dir suffix (debug / release)

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

RAND_SIZE_SUBJECT=1000
RAND_SIZE_PATTERN=4
RAND_HEIGHT_SUBJECT=25
RAND_HEIGHT_PATTERN=2
RAND_ALPHABET=2

EXECUTABLES="arand2 atrim2 adeterminize2 anormalize2 "
TESTS_DIR="`pwd`/examples2/tree"

RES_GOOD=0
RES_FAIL=0
RES_TIME=0
RES_SEGV=0
RES_UNKN=0


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

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

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

# $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 "subject:" >> $LOGFILE
	cat  "$3" >> $LOGFILE
	echo "pattern:" >> $LOGFILE
	cat  "$4" >> $LOGFILE
	echo "command out:" >> $LOGFILE
	echo "$5" >> $LOGFILE
}

function generatePattern {
	./arand2 -t RP --nodes $RAND_SIZE_PATTERN --terminals $(( $RANDOM % $RAND_ALPHABET + 1 )) --height $RAND_HEIGHT_PATTERN 2>/dev/null
}

function generateSubtree {
	./arand2 -t RT --nodes $RAND_SIZE_PATTERN --terminals $(( $RANDOM % $RAND_ALPHABET + 1 )) --height $RAND_HEIGHT_PATTERN 2>/dev/null
}

function generateSubject {
	./arand2 -t RT --nodes $RAND_SIZE_SUBJECT --terminals $(( $RANDOM % $RAND_ALPHABET + 1 )) --height $RAND_HEIGHT_SUBJECT 2>/dev/null
}

# $1 = Occs number of occurrences expected
# $2 = command to test
# $3 = subject
# $4 = pattern
function runTest2 {
	OUT=`timeout $TESTCASE_TIMEOUT bash -c "SUBJECT_FILE=\"$3\"; PATTERN_FILE=\"$4\"; $2"`
	OUT=`test $1 -eq $OUT`
	RET=$?

	if [ $RET == 0 ]; then # ok
		return 0
	fi

	log "$2" $RET "$3" "$4" "$OUT"

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

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

function clearResults {
	RES_GOOD=0
	RES_FAIL=0
	RES_TIME=0
	RES_SEGV=0
	RES_UNKN=0
}

function outputResults {
	# summary
	echo -ne "\n\t"
	echo "RES: GOOD:" $RES_GOOD ", FAIL:" $RES_FAIL ", TIME:" $RES_TIME ", SEGV:" $RES_SEGV, "UNKN:" $RES_UNKN
	echo ""
}

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

	clearResults

	# predefined tests first
	for SUBJECT_FILE in `ls $TESTS_DIR/aarbology.test*.subject.xml`; do
		PATTERN_FILE=${SUBJECT_FILE%.subject.xml}.subtree.xml
		if [ -f  ]; then
			Occs=`./aarbology2 -a exactSubtreeMatch -s "$SUBJECT_FILE" -p "$PATTERN_FILE" | ./astat2 -p size --set`

			runTest2 "$Occs" "$2" "$SUBJECT_FILE" "$PATTERN_FILE"
			registerResult $?
		fi
	done

	echo -n " | "

	# random tests
	SUBJECT_FILE=s.xml
	PATTERN_FILE=p.xml
	for i in $(seq 1 $TESTCASE_ITERATIONS );
	do
		cat <(generateSubject) > $SUBJECT_FILE
		cat <(generateSubtree) > $PATTERN_FILE

		Occs=`./aarbology2 -a exactSubtreeMatch -s "$SUBJECT_FILE" -p "$PATTERN_FILE" | ./astat2 -p size --set`

		runTest2 "$Occs" "$2" "$SUBJECT_FILE" "$PATTERN_FILE"
		registerResult $?
	done

	rm $SUBJECT_FILE
	rm $PATTERN_FILE

	outputResults
}

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

	clearResults

	# predefined tests first
	for SUBJECT_FILE in `ls $TESTS_DIR/aarbology.test*.subject.xml`; do
		PATTERN_FILE=${SUBJECT_FILE%.subject.xml}.pattern.xml
		if [ -f  ]; then
			Occs=`./aarbology2 -a exactPatternMatch -s "$SUBJECT_FILE" -p "$PATTERN_FILE" | ./astat2 -p size --set`

			runTest2 "$Occs" "$2" "$SUBJECT_FILE" "$PATTERN_FILE"
			registerResult $?
		fi
	done

	echo -n " | "

	# random tests
	SUBJECT_FILE=s.xml
	PATTERN_FILE=p.xml
	for i in $(seq 1 $TESTCASE_ITERATIONS );
	do
		cat <(generateSubject) > $SUBJECT_FILE
		cat <(generatePattern) > $PATTERN_FILE

		Occs=`./aarbology2 -a exactPatternMatch -s "$SUBJECT_FILE" -p "$PATTERN_FILE" | ./astat2 -p size --set`

		runTest2 "$Occs" "$2" "$SUBJECT_FILE" "$PATTERN_FILE"
		registerResult $?
	done

	rm $SUBJECT_FILE
	rm $PATTERN_FILE

	outputResults
}

runTestSubtree "Exact Boyer Moore Horspool" "./aarbology2 -a boyerMooreHorspool -s <( ./acast2 -t PrefixRankedBarTree -i \"\$SUBJECT_FILE\" ) -p <( ./acast2 -t PrefixRankedBarTree -i <(./alphabetManip2 -o add -i \"\$PATTERN_FILE\" -a <(./alphabetManip2 -o get -i \"\$SUBJECT_FILE\"))) | ./astat2 -p size --set"
runTestSubtree "Exact Subtree Automaton" "./arun2 -t occurrences -a <(./aarbology2 -a exactSubtreeMatchingAutomaton -p \"\$PATTERN_FILE\" | ./adeterminize2) -i \"\$SUBJECT_FILE\" | ./astat2 -p size --set"

runTestPattern "Exact Boyer Moore Horspool" "./aarbology2 -a boyerMooreHorspool -s <( ./acast2 -t PrefixRankedBarTree -i \"\$SUBJECT_FILE\" ) -p <( ./acast2 -t PrefixRankedBarPattern -i <(./alphabetManip2 -o add -i \"\$PATTERN_FILE\" -a <(./alphabetManip2 -o get -i \"\$SUBJECT_FILE\"))) | ./astat2 -p size --set"
runTestPattern "Exact Knuth Morris Pratt" "./aarbology2 -a knuthMorrisPratt -s <( ./acast2 -t PrefixRankedBarTree -i \"\$SUBJECT_FILE\" ) -p <( ./acast2 -t PrefixRankedBarPattern -i \"\$PATTERN_FILE\" ) | ./astat2 -p size --set"
runTestPattern "Exact Knuth Morris Pratt" "./aarbology2 -a knuthMorrisPratt -s <( ./acast2 -t PrefixRankedTree -i \"\$SUBJECT_FILE\" ) -p <( ./acast2 -t PrefixRankedPattern -i \"\$PATTERN_FILE\" ) | ./astat2 -p size --set"
runTestPattern "Exact Pattern Automaton" "./arun2 -t occurrences -a <(./aarbology2 -a exactPatternMatchingAutomaton -p <(./alphabetManip2 -o add -i \"\$PATTERN_FILE\" -a <(./alphabetManip2 -o get -i \"\$SUBJECT_FILE\")) | ./adeterminize2) -i \"\$SUBJECT_FILE\" | ./astat2 -p size --set"
