From 2481fe32a6bca24bb15c52dd12b669e1a30303ca Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Mon, 19 Jan 2015 13:49:58 +0100
Subject: [PATCH] add example xml readibility test +fix issues found

---
 examples2/automaton/DFSM1IS.xml |   4 +-
 examples2/automaton/FSM.xml     |  12 +--
 examples2/regexp/regexp.xml     |   4 +-
 examples2/regexp/regexp2.xml    |   4 +-
 examples2/regexp/regexp4.xml    |   2 +-
 makefile                        |   2 +
 tests.aconversion.sh            |   4 +-
 tests.aderivation.aintegral.sh  |  30 +++----
 tests.examples.sh               | 133 ++++++++++++++++++++++++++++++++
 9 files changed, 167 insertions(+), 28 deletions(-)
 create mode 100755 tests.examples.sh

diff --git a/examples2/automaton/DFSM1IS.xml b/examples2/automaton/DFSM1IS.xml
index ded0536a98..f96552bbb9 100644
--- a/examples2/automaton/DFSM1IS.xml
+++ b/examples2/automaton/DFSM1IS.xml
@@ -10,9 +10,9 @@
 		<LabeledSymbol><PrimitiveLabel><Character>0</Character></PrimitiveLabel></LabeledSymbol>
 		<LabeledSymbol><PrimitiveLabel><Character>1</Character></PrimitiveLabel></LabeledSymbol>
 	</inputAlphabet>
-	<initialStates>
+	<initialState>
 		<PrimitiveLabel><String>['S']</String></PrimitiveLabel>
-	</initialStates>
+	</initialState>
 	<finalStates>
 		<PrimitiveLabel><String>['A', 'C', 'S']</String></PrimitiveLabel>
 	</finalStates>
diff --git a/examples2/automaton/FSM.xml b/examples2/automaton/FSM.xml
index 0e48fb790f..bbd2b2250f 100644
--- a/examples2/automaton/FSM.xml
+++ b/examples2/automaton/FSM.xml
@@ -9,6 +9,12 @@
 		<LabeledSymbol><PrimitiveLabel><Character>0</Character></PrimitiveLabel></LabeledSymbol>
 		<LabeledSymbol><PrimitiveLabel><Character>1</Character></PrimitiveLabel></LabeledSymbol>
 	</inputAlphabet>
+	<initialState>
+		<PrimitiveLabel><String>one</String></PrimitiveLabel>
+	</initialState>
+	<finalStates>
+		<PrimitiveLabel><String>one</String></PrimitiveLabel>
+	</finalStates>
 	<transitions>
 		<transition>
 			<from><PrimitiveLabel><String>four</String></PrimitiveLabel></from>
@@ -51,10 +57,4 @@
 			<to><PrimitiveLabel><String>two</String></PrimitiveLabel></to>
 		</transition>
 	</transitions>
-	<initialStates>
-		<PrimitiveLabel><String>one</String></PrimitiveLabel>
-	</initialStates>
-	<finalStates>
-		<PrimitiveLabel><String>one</String></PrimitiveLabel>
-	</finalStates>
 </DFA>
diff --git a/examples2/regexp/regexp.xml b/examples2/regexp/regexp.xml
index d23d63e18f..0f186abdf9 100644
--- a/examples2/regexp/regexp.xml
+++ b/examples2/regexp/regexp.xml
@@ -2,6 +2,8 @@
 	<alphabet>
 		<LabeledSymbol><PrimitiveLabel><Integer>0</Integer></PrimitiveLabel></LabeledSymbol>
 		<LabeledSymbol><PrimitiveLabel><Integer>1</Integer></PrimitiveLabel></LabeledSymbol>
+		<LabeledSymbol><PrimitiveLabel><Integer>11</Integer></PrimitiveLabel></LabeledSymbol>
+		<LabeledSymbol><PrimitiveLabel><Integer>10</Integer></PrimitiveLabel></LabeledSymbol>
 	</alphabet>
 	<concatenation>
 		<LabeledSymbol><PrimitiveLabel><Integer>0</Integer></PrimitiveLabel></LabeledSymbol>
@@ -13,4 +15,4 @@
 			<LabeledSymbol><PrimitiveLabel><Integer>10</Integer></PrimitiveLabel></LabeledSymbol>
 		</alternation>
 	</concatenation>
-</regexp>
+</UnboundedRegExp>
diff --git a/examples2/regexp/regexp2.xml b/examples2/regexp/regexp2.xml
index 416b2944d4..ea05ae8ae7 100644
--- a/examples2/regexp/regexp2.xml
+++ b/examples2/regexp/regexp2.xml
@@ -2,6 +2,8 @@
 	<alphabet>
 		<LabeledSymbol><PrimitiveLabel><Integer>0</Integer></PrimitiveLabel></LabeledSymbol>
 		<LabeledSymbol><PrimitiveLabel><Integer>1</Integer></PrimitiveLabel></LabeledSymbol>
+		<LabeledSymbol><PrimitiveLabel><Integer>11</Integer></PrimitiveLabel></LabeledSymbol>
+		<LabeledSymbol><PrimitiveLabel><Integer>10</Integer></PrimitiveLabel></LabeledSymbol>
 	</alphabet>
 	<concatenation>
 		<LabeledSymbol><PrimitiveLabel><Integer>0</Integer></PrimitiveLabel></LabeledSymbol>
@@ -21,4 +23,4 @@
 			</iteration>
 		</alternation>
 	</concatenation>
-</regexp>
+</UnboundedRegExp>
diff --git a/examples2/regexp/regexp4.xml b/examples2/regexp/regexp4.xml
index 66d982807e..0814207514 100644
--- a/examples2/regexp/regexp4.xml
+++ b/examples2/regexp/regexp4.xml
@@ -24,4 +24,4 @@
 			</iteration>
 		</alternation>
 	</concatenation>
-</regexp>
+</UnboundedRegExp>
diff --git a/makefile b/makefile
index 0d9c6b4d22..81db1d4c6a 100644
--- a/makefile
+++ b/makefile
@@ -70,6 +70,7 @@ debug  : all-debug
 	for dir in $(SUBDIRS_BINS); do \
 	    cp $$dir/bin-debug/* $(addsuffix -debug, $(BINFOLDER)); \
 	done
+	./tests.examples.sh debug
 	./tests.aconversion.sh debug
 	./tests.aderivation.aintegral.sh debug
 
@@ -82,6 +83,7 @@ release: all-release
 	for dir in $(SUBDIRS_BINS); do \
 	    cp $$dir/bin-release/* $(addsuffix -release, $(BINFOLDER)); \
 	done
+	./tests.examples.sh release
 	./tests.aconversion.sh release
 	./tests.aderivation.aintegral.sh release
 
diff --git a/tests.aconversion.sh b/tests.aconversion.sh
index 1e7defbdd6..233819baf8 100755
--- a/tests.aconversion.sh
+++ b/tests.aconversion.sh
@@ -141,9 +141,9 @@ function runTest {
 	do
 		runTest2 "$1" "$(generateNFA)"
 		registerResult $?
-   done
+	done
 
-   outputResults
+	outputResults
 }
 
 # FA -> RG -> FA
diff --git a/tests.aderivation.aintegral.sh b/tests.aderivation.aintegral.sh
index a7b5b0ef6c..f904f66e8c 100755
--- a/tests.aderivation.aintegral.sh
+++ b/tests.aderivation.aintegral.sh
@@ -25,8 +25,8 @@ function regexpToMDFA {
 }
 
 function compareRegexp {
-    regexpToMDFA "$1" > tmp1.xml
-    regexpToMDFA "$2" > tmp2.xml
+	 regexpToMDFA "$1" > tmp1.xml
+	 regexpToMDFA "$2" > tmp2.xml
 
 	# relies on ret code by adiff.automaton
 	./acompare2 tmp1.xml tmp2.xml > /dev/null
@@ -40,7 +40,7 @@ function compareRegexp {
 # $1 - input regexp
 # $2 - output regexp
 function runTest {
-    compareRegexp "$1" "$2"
+	 compareRegexp "$1" "$2"
 
 	TEST_EXITVALUE=$?
 
@@ -59,24 +59,24 @@ function runTest {
 }
 
 function runTestD {
-    FILE_REGEXP_ORIG=$(echo "$1")
-    FILE_REGEXP_RESULT=$(echo "$2")
-    shift 2
+	 FILE_REGEXP_ORIG=$(echo "$1")
+	 FILE_REGEXP_RESULT=$(echo "$2")
+	 shift 2
 
-    REGEXP_DERIVATIVE=$(./aconvert2 --string_from_string <<< "$1" | ./aderivations2 -r $TESTS_DIR/$FILE_REGEXP_ORIG -s - )
-    REGEXP_RESULT=$(cat $TESTS_DIR/$FILE_REGEXP_RESULT)
-    runTest "$REGEXP_DERIVATIVE" "$REGEXP_RESULT"
+	 REGEXP_DERIVATIVE=$(./aconvert2 --string_from_string <<< "$1" | ./aderivations2 -r $TESTS_DIR/$FILE_REGEXP_ORIG -s - )
+	 REGEXP_RESULT=$(cat $TESTS_DIR/$FILE_REGEXP_RESULT)
+	 runTest "$REGEXP_DERIVATIVE" "$REGEXP_RESULT"
 }
 
 function runTestI {
-    FILE_REGEXP_ORIG=$(echo "$1")
-    FILE_REGEXP_RESULT=$(echo "$2")
-    shift 2
+	 FILE_REGEXP_ORIG=$(echo "$1")
+	 FILE_REGEXP_RESULT=$(echo "$2")
+	 shift 2
 
-    REGEXP_INTEGRAL=$(./aconvert2 --string_from_string <<< "$1" | ./aintegral2 -r $TESTS_DIR/$FILE_REGEXP_ORIG -s - )
-    REGEXP_RESULT=$(cat $TESTS_DIR/$FILE_REGEXP_RESULT)
+	 REGEXP_INTEGRAL=$(./aconvert2 --string_from_string <<< "$1" | ./aintegral2 -r $TESTS_DIR/$FILE_REGEXP_ORIG -s - )
+	 REGEXP_RESULT=$(cat $TESTS_DIR/$FILE_REGEXP_RESULT)
 
-    runTest "$REGEXP_INTEGRAL" "$REGEXP_RESULT"
+	 runTest "$REGEXP_INTEGRAL" "$REGEXP_RESULT"
 }
 
 
diff --git a/tests.examples.sh b/tests.examples.sh
new file mode 100755
index 0000000000..89b648ed41
--- /dev/null
+++ b/tests.examples.sh
@@ -0,0 +1,133 @@
+#!/usr/bin/env bash
+
+# $1 test dir suffix (debug / release)
+
+# SETTINGS
+TESTCASE_TIMEOUT=10
+LOGFILE="log_tests.txt"
+
+EXECUTABLES="aecho2"
+TESTS_DIR="../examples2"
+
+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 "input automaton:" >> $LOGFILE
+	echo "$3" >> $LOGFILE
+	echo "command out:" >> $LOGFILE
+	echo "$4" >> $LOGFILE
+}
+
+# $1 = command for conversion. Output of such command must be (eps-)NFA !!
+# $2 = automaton
+function runTest2 {
+	TMPNFA="nfa.xml"
+	echo "$2" > $TMPNFA
+
+	OUT=`timeout $TESTCASE_TIMEOUT bash -c "cat $TMPNFA | ./aecho2 "`
+	RET=$?
+
+	if [ $RET == 0 ]; then # ok
+		rm $TMPNFA
+		return 0
+	fi
+
+	log "$1" $RET "$(cat $TMPNFA)" "$OUT"
+	rm $TMPNFA
+
+	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 runTest {
+	echo $1
+	echo -ne "\t"
+
+	clearResults
+
+	# predefined tests first
+	for FILE in `ls $TESTS_DIR/$1/*.xml`; do
+		runTest2 "$1" "$(cat $FILE)"
+		registerResult $?
+	done
+
+	outputResults
+}
+
+runTest "automaton"
+runTest "grammar"
+runTest "regexp"
+runTest "string"
+
-- 
GitLab