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