xref: /freebsd/contrib/byacc/test/run_test.sh (revision c1d255d3)
1#!/bin/sh
2# $Id: run_test.sh,v 1.31 2019/11/03 23:44:07 tom Exp $
3# vi:ts=4 sw=4:
4
5errors=0
6
7# NEW is the file created by the testcase
8# REF is the reference file against which to compare
9test_diffs() {
10	# echo "...test_diffs $NEW vs $REF"
11	mv -f $NEW ${REF_DIR}/
12	CMP=${REF_DIR}/${NEW}
13	if test ! -f $CMP
14	then
15		echo "...not found $CMP"
16		errors=1
17	else
18		sed	-e s,$NEW,$REF, \
19			-e "s%$YACC_escaped%YACC%" \
20			-e "s%^yacc\>%YACC%" \
21			-e "s%YACC:.*option.*$%YACC: error message%" \
22			-e "s%^Usage: yacc\>%Usage: YACC%" \
23			-e '/YYPATCH/s/[0-9][0-9]*/"yyyymmdd"/' \
24			-e '/#define YYPATCH/s/PATCH/CHECK/' \
25			-e 's,#line \([1-9][0-9]*\) "'$REF_DIR'/,#line \1 ",' \
26			-e 's,#line \([1-9][0-9]*\) "'$TEST_DIR'/,#line \1 ",' \
27			-e 's,\(YACC:.* line [0-9][0-9]* of "\)'$TEST_DIR/',\1./,' \
28			< $CMP >$tmpfile \
29			&& mv $tmpfile $CMP
30		if test ! -f $REF
31		then
32			mv $CMP $REF
33			echo "...saved $REF"
34		elif ( cmp -s $REF $CMP )
35		then
36			echo "...ok $REF"
37			rm -f $CMP
38		else
39			echo "...diff $REF"
40			diff -u $REF $CMP
41			errors=1
42		fi
43	fi
44}
45
46test_flags() {
47	echo "** testing flags $*"
48	root=$1
49	ROOT=test-$root
50	shift 1
51	$YACC "$@" >$ROOT.output 2>$ROOT.error
52	for type in .output .error
53	do
54		NEW=$ROOT$type
55		REF=$REF_DIR/$root$type
56		test_diffs
57	done
58}
59
60test_stdin() {
61	echo "** testing stdin $*"
62	root=$1
63	ROOT=test-$root
64	shift 1
65	opts="$1"
66	shift 1
67	code=`echo "$1"|sed -e 's/y$/c/' -e "s,${TEST_DIR}/,,"`
68	if test "x$opts" = "x-"
69	then
70		$YACC -o $ROOT.$code $opts <$1 >$ROOT.output 2>$ROOT.error
71	else
72		$YACC -o $ROOT.$code $opts  $1 >$ROOT.output 2>$ROOT.error
73	fi
74	for type in .output .error .$code
75	do
76		NEW=$ROOT$type
77		REF=$REF_DIR/$root$type
78		test_diffs
79	done
80}
81
82test_defines() {
83	echo "** testing defines $*"
84	root=$1
85	ROOT=test-$root
86	shift 1
87	opts=
88	while test $# != 1
89	do
90		opts="$opts $1"
91		shift 1
92	done
93	head=`echo "$1"|sed -e 's/y$/h/' -e "s,${TEST_DIR}/,,"`
94	code=`echo "$1"|sed -e 's/y$/c/' -e "s,${TEST_DIR}/,,"`
95	$YACC $opts -H $ROOT.$head $1 >$ROOT.output 2>$ROOT.error
96	for name in prefix.tab.c y.tab.c
97	do
98		if test -f $name
99		then
100			mv $name $ROOT.$code
101			break
102		fi
103	done
104	for name in .output .error .$head .$code
105	do
106		NEW=$ROOT$name
107		REF=$REF_DIR/$root$name
108		test_diffs
109	done
110}
111
112if test $# = 1
113then
114	PROG_DIR=`pwd`
115	TEST_DIR=$1
116	PROG_DIR=`echo "$PROG_DIR" | sed -e 's/ /\\\\ /g'`
117	TEST_DIR=`echo "$TEST_DIR" | sed -e 's/ /\\\\ /g'`
118else
119	PROG_DIR=..
120	TEST_DIR=.
121fi
122
123YACC=$PROG_DIR/yacc
124YACC_escaped=`echo "$PROG_DIR/yacc" | sed -e 's/\./\\\./g'`
125
126tmpfile=temp$$
127
128ifBTYACC=`fgrep -l 'define YYBTYACC' $PROG_DIR/config.h > /dev/null; test $? != 0; echo $?`
129
130if test $ifBTYACC = 0; then
131	REF_DIR=${TEST_DIR}/yacc
132else
133	REF_DIR=${TEST_DIR}/btyacc
134fi
135
136rm -f ${REF_DIR}/test-*
137
138echo '** '`date`
139
140# Tests which do not need files
141MYFILE=nosuchfile
142test_flags help -z
143test_flags big_b -B
144test_flags big_l -L
145
146# Test attempts to read non-existent file
147rm -f $MYFILE.*
148test_flags nostdin - $MYFILE.y
149test_flags no_opts -- $MYFILE.y
150
151# Test attempts to write to readonly file
152touch $MYFILE.y
153
154touch $MYFILE.c
155chmod 444 $MYFILE.*
156test_flags no_b_opt   -b
157test_flags no_b_opt1  -bBASE -o $MYFILE.c $MYFILE.y
158
159touch $MYFILE.c
160chmod 444 $MYFILE.*
161test_flags no_p_opt   -p
162test_flags no_p_opt1  -pBASE -o $MYFILE.c $MYFILE.y
163rm -f BASE$MYFILE.c
164
165touch $MYFILE.dot
166chmod 444 $MYFILE.*
167test_flags no_graph   -g -o $MYFILE.c $MYFILE.y
168rm -f $MYFILE.dot
169
170touch $MYFILE.output
171chmod 444 $MYFILE.*
172test_flags no_verbose -v -o $MYFILE.c $MYFILE.y
173test_flags no_output  -o $MYFILE.output $MYFILE.y
174test_flags no_output1  -o$MYFILE.output $MYFILE.y
175test_flags no_output2  -o
176rm -f $MYFILE.output
177
178touch $MYFILE.h
179chmod 444 $MYFILE.*
180test_flags no_defines -d -o $MYFILE.c $MYFILE.y
181rm -f $MYFILE.h
182
183touch $MYFILE.i
184chmod 444 $MYFILE.*
185test_flags no_include -i -o $MYFILE.c $MYFILE.y
186rm -f $MYFILE.i
187
188touch $MYFILE.code.c
189chmod 444 $MYFILE.*
190test_flags no_code_c -r -o $MYFILE.c $MYFILE.y
191rm -f $MYFILE.code.c
192
193rm -f $MYFILE.*
194
195# Test special cases
196test_stdin stdin1 - ${TEST_DIR}/calc.y
197test_stdin stdin2 -- ${TEST_DIR}/calc.y
198
199test_defines defines1 ${TEST_DIR}/calc.y
200test_defines defines2 -d ${TEST_DIR}/calc.y
201test_defines defines3 -b prefix ${TEST_DIR}/calc.y
202
203for input in ${TEST_DIR}/*.y
204do
205	case $input in
206	test-*)
207		echo "?? ignored $input"
208		;;
209	*)
210		root=`basename $input .y`
211		ROOT="test-$root"
212		prefix=${root}_
213
214		OPTS=
215		OPT2=
216		OOPT=
217		TYPE=".error .output .tab.c .tab.h"
218		case $input in
219		${TEST_DIR}/btyacc_*)
220			if test $ifBTYACC = 0; then continue; fi
221			OPTS="$OPTS -B"
222			prefix=`echo "$prefix" | sed -e 's/^btyacc_//'`
223			;;
224		${TEST_DIR}/grammar*)
225			OPTS="$OPTS -g"
226			TYPE="$TYPE .dot"
227			;;
228		${TEST_DIR}/code_debug*)
229			OPTS="$OPTS -t -i"
230			OOPT=rename_debug.c
231			TYPE="$TYPE .i"
232			prefix=
233			;;
234		${TEST_DIR}/code_*)
235			OPTS="$OPTS -r"
236			TYPE="$TYPE .code.c"
237			prefix=`echo "$prefix" | sed -e 's/^code_//'`
238			;;
239		${TEST_DIR}/pure_*)
240			OPTS="$OPTS -P"
241			prefix=`echo "$prefix" | sed -e 's/^pure_//'`
242			;;
243		${TEST_DIR}/quote_*)
244			OPT2="-s"
245			;;
246		${TEST_DIR}/inherit*|\
247		${TEST_DIR}/err_inherit*)
248			if test $ifBTYACC = 0; then continue; fi
249			;;
250		esac
251
252		echo "** testing $input"
253
254		test -n "$prefix" && prefix="-p $prefix"
255
256		for opt2 in "" $OPT2
257		do
258			output=$OOPT
259			if test -n "$output"
260			then
261				output="-o $output"
262				error=`basename $OOPT .c`.error
263			else
264				error=${ROOT}${opt2}.error
265			fi
266
267			$YACC $OPTS $opt2 -v -d $output $prefix -b $ROOT${opt2} $input 2>$error
268			for type in $TYPE
269			do
270				REF=${REF_DIR}/${root}${opt2}${type}
271
272				# handle renaming due to "-o" option
273				if test -n "$OOPT"
274				then
275					case $type in
276					*.tab.c)
277						type=.c
278						;;
279					*.tab.h)
280						type=.h
281						;;
282					*)
283						;;
284					esac
285					NEW=`basename $OOPT .c`${type}
286					case $NEW in
287					test-*)
288						;;
289					*)
290						if test -f "$NEW"
291						then
292							REF=${REF_DIR}/$NEW
293							mv $NEW test-$NEW
294							NEW=test-$NEW
295						fi
296						;;
297					esac
298				else
299					NEW=${ROOT}${opt2}${type}
300				fi
301				test_diffs
302			done
303		done
304		;;
305	esac
306done
307
308exit $errors
309