1234949Sbapt#!/bin/sh
2268811Sbapt# $Id: run_test.sh,v 1.24 2014/07/15 19:21:10 tom Exp $
3234949Sbapt# vi:ts=4 sw=4:
4234949Sbapt
5268811Sbapterrors=0
6268811Sbapt
7264803Sbapt# NEW is the file created by the testcase
8264803Sbapt# REF is the reference file against which to compare
9264803Sbapttest_diffs() {
10264803Sbapt	# echo "...test_diffs $NEW vs $REF"
11264803Sbapt	mv -f $NEW ${REF_DIR}/
12264803Sbapt	CMP=${REF_DIR}/${NEW}
13264803Sbapt	if test ! -f $CMP
14264803Sbapt	then
15264803Sbapt		echo "...not found $CMP"
16268811Sbapt		errors=1
17264803Sbapt	else
18264803Sbapt		sed	-e s,$NEW,$REF, \
19268811Sbapt			-e "s%$YACC_escaped%YACC%" \
20264803Sbapt			-e '/YYPATCH/s/[0-9][0-9]*/"yyyymmdd"/' \
21264803Sbapt			-e '/#define YYPATCH/s/PATCH/CHECK/' \
22264803Sbapt			-e 's,#line \([1-9][0-9]*\) "'$REF_DIR'/,#line \1 ",' \
23264803Sbapt			-e 's,#line \([1-9][0-9]*\) "'$TEST_DIR'/,#line \1 ",' \
24268811Sbapt			-e 's,\(YACC:.* line [0-9][0-9]* of "\)'$TEST_DIR/',\1./,' \
25264803Sbapt			< $CMP >$tmpfile \
26264803Sbapt			&& mv $tmpfile $CMP
27264803Sbapt		if test ! -f $REF
28264803Sbapt		then
29264803Sbapt			mv $CMP $REF
30264803Sbapt			echo "...saved $REF"
31264803Sbapt		elif ( cmp -s $REF $CMP )
32264803Sbapt		then
33264803Sbapt			echo "...ok $REF"
34264803Sbapt			rm -f $CMP
35264803Sbapt		else
36264803Sbapt			echo "...diff $REF"
37264803Sbapt			diff -u $REF $CMP
38268811Sbapt			errors=1
39264803Sbapt		fi
40264803Sbapt	fi
41264803Sbapt}
42264803Sbapt
43264803Sbapttest_flags() {
44264803Sbapt	echo "** testing flags $*"
45264803Sbapt	root=$1
46264803Sbapt	ROOT=test-$root
47264803Sbapt	shift 1
48264803Sbapt	$YACC $* >$ROOT.output \
49264803Sbapt	    2>&1 >$ROOT.error
50264803Sbapt	for type in .output .error
51264803Sbapt	do
52264803Sbapt		NEW=$ROOT$type
53264803Sbapt		REF=$REF_DIR/$root$type
54264803Sbapt		test_diffs
55264803Sbapt	done
56264803Sbapt}
57264803Sbapt
58234949Sbaptif test $# = 1
59234949Sbaptthen
60234949Sbapt	PROG_DIR=`pwd`
61234949Sbapt	TEST_DIR=$1
62264803Sbapt	PROG_DIR=`echo "$PROG_DIR" | sed -e 's/ /\\\\ /g'`
63264803Sbapt	TEST_DIR=`echo "$TEST_DIR" | sed -e 's/ /\\\\ /g'`
64234949Sbaptelse
65234949Sbapt	PROG_DIR=..
66234949Sbapt	TEST_DIR=.
67234949Sbaptfi
68234949Sbapt
69234949SbaptYACC=$PROG_DIR/yacc
70268811SbaptYACC_escaped=`echo "$PROG_DIR/yacc" | sed -e 's/\./\\\./g'`
71234949Sbapt
72234949Sbapttmpfile=temp$$
73234949Sbapt
74268811SbaptifBTYACC=`fgrep -l 'define YYBTYACC' $PROG_DIR/config.h > /dev/null; test $? != 0; echo $?`
75264803Sbapt
76264803Sbaptif test $ifBTYACC = 0; then
77264803Sbapt	REF_DIR=${TEST_DIR}/yacc
78264803Sbaptelse
79264803Sbapt	REF_DIR=${TEST_DIR}/btyacc
80264803Sbaptfi
81264803Sbapt
82264803Sbaptrm -f ${REF_DIR}/test-*
83264803Sbapt
84234949Sbaptecho '** '`date`
85264803Sbapt
86264803Sbapt# Tests which do not need files
87264803SbaptMYFILE=nosuchfile
88264803Sbapttest_flags help -z
89264803Sbapttest_flags big_b -B
90264803Sbapttest_flags big_l -L
91264803Sbapt
92264803Sbapt# Test attempts to read non-existent file
93264803Sbaptrm -f $MYFILE.*
94264803Sbapttest_flags nostdin - $MYFILE.y
95264803Sbapttest_flags no_opts -- $MYFILE.y
96264803Sbapt
97264803Sbapt# Test attempts to write to readonly file
98264803Sbapttouch $MYFILE.y
99264803Sbapt
100264803Sbapttouch $MYFILE.c
101264803Sbaptchmod 444 $MYFILE.*
102264803Sbapttest_flags no_b_opt   -b
103264803Sbapttest_flags no_b_opt1  -bBASE -o $MYFILE.c $MYFILE.y
104264803Sbapt
105264803Sbapttouch $MYFILE.c
106264803Sbaptchmod 444 $MYFILE.*
107264803Sbapttest_flags no_p_opt   -p
108264803Sbapttest_flags no_p_opt1  -pBASE -o $MYFILE.c $MYFILE.y
109264803Sbaptrm -f BASE$MYFILE.c
110264803Sbapt
111264803Sbapttouch $MYFILE.dot
112264803Sbaptchmod 444 $MYFILE.*
113264803Sbapttest_flags no_graph   -g -o $MYFILE.c $MYFILE.y
114264803Sbaptrm -f $MYFILE.dot
115264803Sbapt
116264803Sbapttouch $MYFILE.output
117264803Sbaptchmod 444 $MYFILE.*
118264803Sbapttest_flags no_verbose -v -o $MYFILE.c $MYFILE.y
119264803Sbapttest_flags no_output  -o $MYFILE.output $MYFILE.y
120264803Sbapttest_flags no_output1  -o$MYFILE.output $MYFILE.y
121264803Sbapttest_flags no_output2  -o
122264803Sbaptrm -f $MYFILE.output
123264803Sbapt
124264803Sbapttouch $MYFILE.h
125264803Sbaptchmod 444 $MYFILE.*
126264803Sbapttest_flags no_defines -d -o $MYFILE.c $MYFILE.y
127264803Sbaptrm -f $MYFILE.h
128264803Sbapt
129264803Sbapttouch $MYFILE.i
130264803Sbaptchmod 444 $MYFILE.*
131264803Sbapttest_flags no_include -i -o $MYFILE.c $MYFILE.y
132264803Sbaptrm -f $MYFILE.i
133264803Sbapt
134264803Sbapttouch $MYFILE.code.c
135264803Sbaptchmod 444 $MYFILE.*
136264803Sbapttest_flags no_code_c -r -o $MYFILE.c $MYFILE.y
137264803Sbaptrm -f $MYFILE.code.c
138264803Sbapt
139264803Sbaptrm -f $MYFILE.*
140264803Sbapt
141234949Sbaptfor input in ${TEST_DIR}/*.y
142234949Sbaptdo
143234949Sbapt	case $input in
144264803Sbapt	test-*)
145234949Sbapt		echo "?? ignored $input"
146234949Sbapt		;;
147234949Sbapt	*)
148234949Sbapt		root=`basename $input .y`
149234949Sbapt		ROOT="test-$root"
150234949Sbapt		prefix=${root}_
151234949Sbapt
152234949Sbapt		OPTS=
153234949Sbapt		OPT2=
154264803Sbapt		OOPT=
155264803Sbapt		TYPE=".error .output .tab.c .tab.h"
156234949Sbapt		case $input in
157264803Sbapt		${TEST_DIR}/btyacc_*)
158264803Sbapt			if test $ifBTYACC = 0; then continue; fi
159264803Sbapt			OPTS="$OPTS -B"
160264803Sbapt			prefix=`echo "$prefix" | sed -e 's/^btyacc_//'`
161264803Sbapt			;;
162264803Sbapt		${TEST_DIR}/grammar*)
163264803Sbapt			OPTS="$OPTS -g"
164264803Sbapt			TYPE="$TYPE .dot"
165264803Sbapt			;;
166264803Sbapt		${TEST_DIR}/code_debug*)
167264803Sbapt			OPTS="$OPTS -t -i"
168264803Sbapt			OOPT=rename_debug.c
169264803Sbapt			TYPE="$TYPE .i"
170264803Sbapt			prefix=
171264803Sbapt			;;
172234949Sbapt		${TEST_DIR}/code_*)
173234949Sbapt			OPTS="$OPTS -r"
174234949Sbapt			TYPE="$TYPE .code.c"
175234949Sbapt			prefix=`echo "$prefix" | sed -e 's/^code_//'`
176234949Sbapt			;;
177234949Sbapt		${TEST_DIR}/pure_*)
178234949Sbapt			OPTS="$OPTS -P"
179234949Sbapt			prefix=`echo "$prefix" | sed -e 's/^pure_//'`
180234949Sbapt			;;
181234949Sbapt		${TEST_DIR}/quote_*)
182234949Sbapt			OPT2="-s"
183234949Sbapt			;;
184264803Sbapt		${TEST_DIR}/inherit*|\
185264803Sbapt		${TEST_DIR}/err_inherit*)
186264803Sbapt			if test $ifBTYACC = 0; then continue; fi
187264803Sbapt			;;
188234949Sbapt		esac
189234949Sbapt
190264803Sbapt		echo "** testing $input"
191264803Sbapt
192264803Sbapt		test -n "$prefix" && prefix="-p $prefix"
193264803Sbapt
194234949Sbapt		for opt2 in "" $OPT2
195234949Sbapt		do
196264803Sbapt			output=$OOPT
197264803Sbapt			if test -n "$output"
198264803Sbapt			then
199264803Sbapt				output="-o $output"
200264803Sbapt				error=`basename $OOPT .c`.error
201264803Sbapt			else
202264803Sbapt				error=${ROOT}${opt2}.error
203264803Sbapt			fi
204264803Sbapt
205264803Sbapt			$YACC $OPTS $opt2 -v -d $output $prefix -b $ROOT${opt2} $input 2>$error
206234949Sbapt			for type in $TYPE
207234949Sbapt			do
208264803Sbapt				REF=${REF_DIR}/${root}${opt2}${type}
209264803Sbapt
210264803Sbapt				# handle renaming due to "-o" option
211264803Sbapt				if test -n "$OOPT"
212234949Sbapt				then
213264803Sbapt					case $type in
214264803Sbapt					*.tab.c)
215264803Sbapt						type=.c
216264803Sbapt						;;
217264803Sbapt					*.tab.h)
218264803Sbapt						type=.h
219264803Sbapt						;;
220264803Sbapt					*)
221264803Sbapt						;;
222264803Sbapt					esac
223264803Sbapt					NEW=`basename $OOPT .c`${type}
224264803Sbapt					case $NEW in
225264803Sbapt					test-*)
226264803Sbapt						;;
227264803Sbapt					*)
228264803Sbapt						if test -f "$NEW"
229264803Sbapt						then
230264803Sbapt							REF=${REF_DIR}/$NEW
231264803Sbapt							mv $NEW test-$NEW
232264803Sbapt							NEW=test-$NEW
233264803Sbapt						fi
234264803Sbapt						;;
235264803Sbapt					esac
236234949Sbapt				else
237264803Sbapt					NEW=${ROOT}${opt2}${type}
238234949Sbapt				fi
239264803Sbapt				test_diffs
240234949Sbapt			done
241234949Sbapt		done
242234949Sbapt		;;
243234949Sbapt	esac
244234949Sbaptdone
245268811Sbapt
246268811Sbaptexit $errors
247