Skip to content
Snippets Groups Projects
tests.astringology.sh 5.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jan Trávníček's avatar
    Jan Trávníček committed
    #!/usr/bin/env bash
    
    
    source bash_multithread.sh
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    # $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
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    RAND_ALPHABET=4
    
    
    EXECUTABLES="arand2 atrim2 adeterminize2 anormalize2 "
    
    TESTS_DIR="`pwd`/examples2/string"
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    
    
    RES_GOOD=
    RES_FAIL=
    RES_TIME=
    RES_SEGV=
    RES_UNKN=
    MATCHES=
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    
    
    # ----------------------------
    
    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
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    # ----------------------------
    
    # $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
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	cat  "$3" >> $LOGFILE
    
    	echo "pattern:" >> $LOGFILE
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	cat  "$4" >> $LOGFILE
    
    	echo "command out:" >> $LOGFILE
    	echo "$5" >> $LOGFILE
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    }
    
    
    function generatePattern {
    
    	./arand2 -t ST --length $RAND_SIZE_PATTERN --terminals $(( $RANDOM % $RAND_ALPHABET + 1 )) 2>/dev/null | ./anormalize2 -a string
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    }
    
    
    function generateSubject {
    
    	./arand2 -t ST --length $RAND_SIZE_SUBJECT --terminals $(( $RANDOM % $RAND_ALPHABET + 1 )) 2>/dev/null | ./anormalize2 -a string
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    
    
    # $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"`
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	RET=$?
    	if [ $RET == 0 ]; then # ok
    
    		OUT=`test $OUT1 -eq $OUT2`
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	fi
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    
    
    	if [ $RET != 0 ] || [ $RET2 != 0 ]; then # fail
    		log "$2" $RET "$3" "$4" "$OUT"
    	fi
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	if [ $RET == 124 ]; then # timeout
    
    		registerResult 2
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    		return 2
    	elif [ $RET -ge 124 ]; then #segv
    
    		registerResult 3
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    		return 3
    
    	elif [ $RET != 0 ] || [ $RET2 != 0 ]; then # fail
    		registerResult 1
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    		return 1
    
    		echo -n "+$OUT2" >> $MATCHES
    
    		registerResult 0
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	fi
    }
    
    function registerResult {
    	case $1 in
    		0)
    			echo -n "."
    
    			echo -n "+1" >> $RES_GOOD
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    			;;
    		1)
    			echo -n "x"
    
    			echo -n "+1" >> $RES_FAIL
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    			;;
    		2)
    			echo -n "T"
    
    			echo -n "+1" >> $RES_TIME
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    			;;
    		3)
    			echo -n "E"
    
    			echo -n "+1" >> $RES_SEGV
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    			;;
    		*)
    			echo -n "?"
    
    			echo -n "+1" >> $RES_UNKN
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    			;;
    	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
    }
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    function clearResults {
    
    	rm $RES_GOOD
    	rm $RES_FAIL
    	rm $RES_TIME
    	rm $RES_SEGV
    	rm $RES_UNKN
    	rm $MATCHES
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    }
    
    function outputResults {
    
    	echo "" >> $RES_GOOD
    	echo "" >> $RES_FAIL
    	echo "" >> $RES_TIME
    	echo "" >> $RES_SEGV
    	echo "" >> $RES_UNKN
    	echo "" >> $MATCHES
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	# 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)
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	echo ""
    }
    
    # $1 - aconversions2 sequence
    function runTest {
    	echo $1
    	echo -ne "\t"
    
    
    	initResults
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    
    
    	Occs="./astringology2 -a exactFactorMatch -s \"\$SUBJECT_FILE\" -p \"\$PATTERN_FILE\" | ./astat2 -p size"
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	# 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"
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	done
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	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"
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	done
    
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    	outputResults
    
    	clearResults
    
    Jan Trávníček's avatar
    Jan Trávníček committed
    }
    
    
    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"