#!/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_SIZE_SUBJECT=100
RAND_SIZE_PATTERN=4
RAND_ALPHABET=4

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

RES_GOOD=
RES_FAIL=
RES_TIME=
RES_SEGV=
RES_UNKN=
MATCHES=


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

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

function generatePattern {
	./arand2 -t ST --length $RAND_SIZE_PATTERN --terminals $(( $RANDOM % $RAND_ALPHABET + 1 )) 2>/dev/null | ./anormalize2 -a string
}

function generateSubject {
	./arand2 -t ST --length $RAND_SIZE_SUBJECT --terminals $(( $RANDOM % $RAND_ALPHABET + 1 )) 2>/dev/null | ./anormalize2 -a string
}

# $1 = command to compute expected number of occurrences
# $2 = command to test
# $3 = subject
# $4 = pattern
function runTest2 {
	OUT1=`bash -c "SUBJECT_FILE=\"$3\"; PATTERN_FILE=\"$4\"; $1"`
	OUT2=`timeout $TESTCASE_TIMEOUT bash -c "SUBJECT_FILE=\"$3\"; PATTERN_FILE=\"$4\"; $2"`
	RET=$?
	if [ $RET == 0 ]; then # ok
		OUT=`test $OUT1 -eq $OUT2`
	fi
	RET2=$?

	if [ $RET != 0 ] || [ $RET2 != 0 ]; then # fail
		log "$2" $RET "$3" "$4" "$OUT"
	fi

	rm $3
	rm $4

	if [ $RET == 124 ]; then # timeout
		registerResult 2
		return 2
	elif [ $RET -ge 124 ]; then #segv
		registerResult 3
		return 3
	elif [ $RET != 0 ] || [ $RET2 != 0 ]; then # fail
		registerResult 1
		return 1
	else
		echo -n "+$OUT2" >> $MATCHES
		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
	MATCHES=$(mktemp)
	echo -n "0" > $MATCHES
}

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

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

	# 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), "MATCHES:" $(bc < $MATCHES)
	echo ""
}

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

	initResults
	bgxgrp=""

	Occs="./astringology2 -a exactFactorMatch -s \"\$SUBJECT_FILE\" -p \"\$PATTERN_FILE\" | ./astat2 -p size"

	# predefined tests first
	for SUBJECT_FILE in `ls $TESTS_DIR/astringology.test*.subject.xml`; do
		PATTERN_FILE=${SUBJECT_FILE%.subject.xml}.pattern.xml
		if [ -f $PATTERN_FILE ]; then
			SUBJECT_FILE_COPY=$(mktemp)
			PATTERN_FILE_COPY=$(mktemp)

			cat $SUBJECT_FILE > $SUBJECT_FILE_COPY
			cat $PATTERN_FILE > $PATTERN_FILE_COPY

			bgxlimit ${JOBS} runTest2 "$Occs" "$2" "$SUBJECT_FILE_COPY" "$PATTERN_FILE_COPY"
		fi
	done

	bgxwait

	echo -n " | "

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

		cat <(generateSubject) > $SUBJECT_FILE
		cat <(generatePattern) > $PATTERN_FILE

		bgxlimit ${JOBS} runTest2 "$Occs" "$2" "$SUBJECT_FILE" "$PATTERN_FILE"
	done

	bgxwait

	outputResults
	clearResults
}

runTest "Exact Boyer Moore Horspool" "./astringology2 -a boyerMooreHorspool -s \"\$SUBJECT_FILE\" -p <(./alphabetManip2 -o add -i \"\$PATTERN_FILE\" -a <(./alphabetManip2 -o get -i \"\$SUBJECT_FILE\")) | ./astat2 -p size"
runTest "Exact Reversed Boyer Moore Horspool" "./astringology2 -a reversedBoyerMooreHorspool -s \"\$SUBJECT_FILE\" -p <(./alphabetManip2 -o add -i \"\$PATTERN_FILE\" -a <(./alphabetManip2 -o get -i \"\$SUBJECT_FILE\")) | ./astat2 -p size"
runTest "Exact Matching Automaton" "./arun2 -t occurrences -a <(./astringology2 -a exactMatchingAutomaton -p <(./alphabetManip2 -o add -i \"\$PATTERN_FILE\" -a <(./alphabetManip2 -o get -i \"\$SUBJECT_FILE\")) | ./adeterminize2) -i \"\$SUBJECT_FILE\" | ./astat2 -p size"
runTest "Exact Dead Zone Using Bad Character Shift" "./astringology2 -a deadZoneUsingBadCharacterShift -s \"\$SUBJECT_FILE\" -p <(./alphabetManip2 -o add -i \"\$PATTERN_FILE\" -a <(./alphabetManip2 -o get -i \"\$SUBJECT_FILE\")) | ./astat2 -p size"

