1234949Sbapt#!/bin/sh
2272953Srodrigc# $Id: run_test.sh,v 1.24 2014/07/15 19:21:10 tom Exp $
3234949Sbapt# vi:ts=4 sw=4:
4234949Sbapt
5272953Srodrigcerrors=0
6272953Srodrigc
7268899Sbapt# NEW is the file created by the testcase
8268899Sbapt# REF is the reference file against which to compare
9268899Sbapttest_diffs() {
10268899Sbapt	# echo "...test_diffs $NEW vs $REF"
11268899Sbapt	mv -f $NEW ${REF_DIR}/
12268899Sbapt	CMP=${REF_DIR}/${NEW}
13268899Sbapt	if test ! -f $CMP
14268899Sbapt	then
15268899Sbapt		echo "...not found $CMP"
16272953Srodrigc		errors=1
17268899Sbapt	else
18268899Sbapt		sed	-e s,$NEW,$REF, \
19272953Srodrigc			-e "s%$YACC_escaped%YACC%" \
20268899Sbapt			-e '/YYPATCH/s/[0-9][0-9]*/"yyyymmdd"/' \
21268899Sbapt			-e '/#define YYPATCH/s/PATCH/CHECK/' \
22268899Sbapt			-e 's,#line \([1-9][0-9]*\) "'$REF_DIR'/,#line \1 ",' \
23268899Sbapt			-e 's,#line \([1-9][0-9]*\) "'$TEST_DIR'/,#line \1 ",' \
24272953Srodrigc			-e 's,\(YACC:.* line [0-9][0-9]* of "\)'$TEST_DIR/',\1./,' \
25268899Sbapt			< $CMP >$tmpfile \
26268899Sbapt			&& mv $tmpfile $CMP
27268899Sbapt		if test ! -f $REF
28268899Sbapt		then
29268899Sbapt			mv $CMP $REF
30268899Sbapt			echo "...saved $REF"
31268899Sbapt		elif ( cmp -s $REF $CMP )
32268899Sbapt		then
33268899Sbapt			echo "...ok $REF"
34268899Sbapt			rm -f $CMP
35268899Sbapt		else
36268899Sbapt			echo "...diff $REF"
37268899Sbapt			diff -u $REF $CMP
38272953Srodrigc			errors=1
39268899Sbapt		fi
40268899Sbapt	fi
41268899Sbapt}
42268899Sbapt
43268899Sbapttest_flags() {
44268899Sbapt	echo "** testing flags $*"
45268899Sbapt	root=$1
46268899Sbapt	ROOT=test-$root
47268899Sbapt	shift 1
48268899Sbapt	$YACC $* >$ROOT.output \
49268899Sbapt	    2>&1 >$ROOT.error
50268899Sbapt	for type in .output .error
51268899Sbapt	do
52268899Sbapt		NEW=$ROOT$type
53268899Sbapt		REF=$REF_DIR/$root$type
54268899Sbapt		test_diffs
55268899Sbapt	done
56268899Sbapt}
57268899Sbapt
58234949Sbaptif test $# = 1
59234949Sbaptthen
60234949Sbapt	PROG_DIR=`pwd`
61234949Sbapt	TEST_DIR=$1
62268899Sbapt	PROG_DIR=`echo "$PROG_DIR" | sed -e 's/ /\\\\ /g'`
63268899Sbapt	TEST_DIR=`echo "$TEST_DIR" | sed -e 's/ /\\\\ /g'`
64234949Sbaptelse
65234949Sbapt	PROG_DIR=..
66234949Sbapt	TEST_DIR=.
67234949Sbaptfi
68234949Sbapt
69234949SbaptYACC=$PROG_DIR/yacc
70272953SrodrigcYACC_escaped=`echo "$PROG_DIR/yacc" | sed -e 's/\./\\\./g'`
71234949Sbapt
72234949Sbapttmpfile=temp$$
73234949Sbapt
74272953SrodrigcifBTYACC=`fgrep -l 'define YYBTYACC' $PROG_DIR/config.h > /dev/null; test $? != 0; echo $?`
75268899Sbapt
76268899Sbaptif test $ifBTYACC = 0; then
77268899Sbapt	REF_DIR=${TEST_DIR}/yacc
78268899Sbaptelse
79268899Sbapt	REF_DIR=${TEST_DIR}/btyacc
80268899Sbaptfi
81268899Sbapt
82268899Sbaptrm -f ${REF_DIR}/test-*
83268899Sbapt
84234949Sbaptecho '** '`date`
85268899Sbapt
86268899Sbapt# Tests which do not need files
87268899SbaptMYFILE=nosuchfile
88268899Sbapttest_flags help -z
89268899Sbapttest_flags big_b -B
90268899Sbapttest_flags big_l -L
91268899Sbapt
92268899Sbapt# Test attempts to read non-existent file
93268899Sbaptrm -f $MYFILE.*
94268899Sbapttest_flags nostdin - $MYFILE.y
95268899Sbapttest_flags no_opts -- $MYFILE.y
96268899Sbapt
97268899Sbapt# Test attempts to write to readonly file
98268899Sbapttouch $MYFILE.y
99268899Sbapt
100268899Sbapttouch $MYFILE.c
101268899Sbaptchmod 444 $MYFILE.*
102268899Sbapttest_flags no_b_opt   -b
103268899Sbapttest_flags no_b_opt1  -bBASE -o $MYFILE.c $MYFILE.y
104268899Sbapt
105268899Sbapttouch $MYFILE.c
106268899Sbaptchmod 444 $MYFILE.*
107268899Sbapttest_flags no_p_opt   -p
108268899Sbapttest_flags no_p_opt1  -pBASE -o $MYFILE.c $MYFILE.y
109268899Sbaptrm -f BASE$MYFILE.c
110268899Sbapt
111268899Sbapttouch $MYFILE.dot
112268899Sbaptchmod 444 $MYFILE.*
113268899Sbapttest_flags no_graph   -g -o $MYFILE.c $MYFILE.y
114268899Sbaptrm -f $MYFILE.dot
115268899Sbapt
116268899Sbapttouch $MYFILE.output
117268899Sbaptchmod 444 $MYFILE.*
118268899Sbapttest_flags no_verbose -v -o $MYFILE.c $MYFILE.y
119268899Sbapttest_flags no_output  -o $MYFILE.output $MYFILE.y
120268899Sbapttest_flags no_output1  -o$MYFILE.output $MYFILE.y
121268899Sbapttest_flags no_output2  -o
122268899Sbaptrm -f $MYFILE.output
123268899Sbapt
124268899Sbapttouch $MYFILE.h
125268899Sbaptchmod 444 $MYFILE.*
126268899Sbapttest_flags no_defines -d -o $MYFILE.c $MYFILE.y
127268899Sbaptrm -f $MYFILE.h
128268899Sbapt
129268899Sbapttouch $MYFILE.i
130268899Sbaptchmod 444 $MYFILE.*
131268899Sbapttest_flags no_include -i -o $MYFILE.c $MYFILE.y
132268899Sbaptrm -f $MYFILE.i
133268899Sbapt
134268899Sbapttouch $MYFILE.code.c
135268899Sbaptchmod 444 $MYFILE.*
136268899Sbapttest_flags no_code_c -r -o $MYFILE.c $MYFILE.y
137268899Sbaptrm -f $MYFILE.code.c
138268899Sbapt
139268899Sbaptrm -f $MYFILE.*
140268899Sbapt
141234949Sbaptfor input in ${TEST_DIR}/*.y
142234949Sbaptdo
143234949Sbapt	case $input in
144268899Sbapt	test-*)
145234949Sbapt		echo "?? ignored $input"
146234949Sbapt		;;
147234949Sbapt	*)
148234949Sbapt		root=`basename $input .y`
149234949Sbapt		ROOT="test-$root"
150234949Sbapt		prefix=${root}_
151234949Sbapt
152234949Sbapt		OPTS=
153234949Sbapt		OPT2=
154268899Sbapt		OOPT=
155268899Sbapt		TYPE=".error .output .tab.c .tab.h"
156234949Sbapt		case $input in
157268899Sbapt		${TEST_DIR}/btyacc_*)
158268899Sbapt			if test $ifBTYACC = 0; then continue; fi
159268899Sbapt			OPTS="$OPTS -B"
160268899Sbapt			prefix=`echo "$prefix" | sed -e 's/^btyacc_//'`
161268899Sbapt			;;
162268899Sbapt		${TEST_DIR}/grammar*)
163268899Sbapt			OPTS="$OPTS -g"
164268899Sbapt			TYPE="$TYPE .dot"
165268899Sbapt			;;
166268899Sbapt		${TEST_DIR}/code_debug*)
167268899Sbapt			OPTS="$OPTS -t -i"
168268899Sbapt			OOPT=rename_debug.c
169268899Sbapt			TYPE="$TYPE .i"
170268899Sbapt			prefix=
171268899Sbapt			;;
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			;;
184268899Sbapt		${TEST_DIR}/inherit*|\
185268899Sbapt		${TEST_DIR}/err_inherit*)
186268899Sbapt			if test $ifBTYACC = 0; then continue; fi
187268899Sbapt			;;
188234949Sbapt		esac
189234949Sbapt
190268899Sbapt		echo "** testing $input"
191268899Sbapt
192268899Sbapt		test -n "$prefix" && prefix="-p $prefix"
193268899Sbapt
194234949Sbapt		for opt2 in "" $OPT2
195234949Sbapt		do
196268899Sbapt			output=$OOPT
197268899Sbapt			if test -n "$output"
198268899Sbapt			then
199268899Sbapt				output="-o $output"
200268899Sbapt				error=`basename $OOPT .c`.error
201268899Sbapt			else
202268899Sbapt				error=${ROOT}${opt2}.error
203268899Sbapt			fi
204268899Sbapt
205268899Sbapt			$YACC $OPTS $opt2 -v -d $output $prefix -b $ROOT${opt2} $input 2>$error
206234949Sbapt			for type in $TYPE
207234949Sbapt			do
208268899Sbapt				REF=${REF_DIR}/${root}${opt2}${type}
209268899Sbapt
210268899Sbapt				# handle renaming due to "-o" option
211268899Sbapt				if test -n "$OOPT"
212234949Sbapt				then
213268899Sbapt					case $type in
214268899Sbapt					*.tab.c)
215268899Sbapt						type=.c
216268899Sbapt						;;
217268899Sbapt					*.tab.h)
218268899Sbapt						type=.h
219268899Sbapt						;;
220268899Sbapt					*)
221268899Sbapt						;;
222268899Sbapt					esac
223268899Sbapt					NEW=`basename $OOPT .c`${type}
224268899Sbapt					case $NEW in
225268899Sbapt					test-*)
226268899Sbapt						;;
227268899Sbapt					*)
228268899Sbapt						if test -f "$NEW"
229268899Sbapt						then
230268899Sbapt							REF=${REF_DIR}/$NEW
231268899Sbapt							mv $NEW test-$NEW
232268899Sbapt							NEW=test-$NEW
233268899Sbapt						fi
234268899Sbapt						;;
235268899Sbapt					esac
236234949Sbapt				else
237268899Sbapt					NEW=${ROOT}${opt2}${type}
238234949Sbapt				fi
239268899Sbapt				test_diffs
240234949Sbapt			done
241234949Sbapt		done
242234949Sbapt		;;
243234949Sbapt	esac
244234949Sbaptdone
245272953Srodrigc
246272953Srodrigcexit $errors
247