xref: /freebsd/contrib/byacc/test/run_test.sh (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1#!/bin/sh
2# $Id: run_test.sh,v 1.24 2014/07/15 19:21:10 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 '/YYPATCH/s/[0-9][0-9]*/"yyyymmdd"/' \
21			-e '/#define YYPATCH/s/PATCH/CHECK/' \
22			-e 's,#line \([1-9][0-9]*\) "'$REF_DIR'/,#line \1 ",' \
23			-e 's,#line \([1-9][0-9]*\) "'$TEST_DIR'/,#line \1 ",' \
24			-e 's,\(YACC:.* line [0-9][0-9]* of "\)'$TEST_DIR/',\1./,' \
25			< $CMP >$tmpfile \
26			&& mv $tmpfile $CMP
27		if test ! -f $REF
28		then
29			mv $CMP $REF
30			echo "...saved $REF"
31		elif ( cmp -s $REF $CMP )
32		then
33			echo "...ok $REF"
34			rm -f $CMP
35		else
36			echo "...diff $REF"
37			diff -u $REF $CMP
38			errors=1
39		fi
40	fi
41}
42
43test_flags() {
44	echo "** testing flags $*"
45	root=$1
46	ROOT=test-$root
47	shift 1
48	$YACC $* >$ROOT.output \
49	    2>&1 >$ROOT.error
50	for type in .output .error
51	do
52		NEW=$ROOT$type
53		REF=$REF_DIR/$root$type
54		test_diffs
55	done
56}
57
58if test $# = 1
59then
60	PROG_DIR=`pwd`
61	TEST_DIR=$1
62	PROG_DIR=`echo "$PROG_DIR" | sed -e 's/ /\\\\ /g'`
63	TEST_DIR=`echo "$TEST_DIR" | sed -e 's/ /\\\\ /g'`
64else
65	PROG_DIR=..
66	TEST_DIR=.
67fi
68
69YACC=$PROG_DIR/yacc
70YACC_escaped=`echo "$PROG_DIR/yacc" | sed -e 's/\./\\\./g'`
71
72tmpfile=temp$$
73
74ifBTYACC=`fgrep -l 'define YYBTYACC' $PROG_DIR/config.h > /dev/null; test $? != 0; echo $?`
75
76if test $ifBTYACC = 0; then
77	REF_DIR=${TEST_DIR}/yacc
78else
79	REF_DIR=${TEST_DIR}/btyacc
80fi
81
82rm -f ${REF_DIR}/test-*
83
84echo '** '`date`
85
86# Tests which do not need files
87MYFILE=nosuchfile
88test_flags help -z
89test_flags big_b -B
90test_flags big_l -L
91
92# Test attempts to read non-existent file
93rm -f $MYFILE.*
94test_flags nostdin - $MYFILE.y
95test_flags no_opts -- $MYFILE.y
96
97# Test attempts to write to readonly file
98touch $MYFILE.y
99
100touch $MYFILE.c
101chmod 444 $MYFILE.*
102test_flags no_b_opt   -b
103test_flags no_b_opt1  -bBASE -o $MYFILE.c $MYFILE.y
104
105touch $MYFILE.c
106chmod 444 $MYFILE.*
107test_flags no_p_opt   -p
108test_flags no_p_opt1  -pBASE -o $MYFILE.c $MYFILE.y
109rm -f BASE$MYFILE.c
110
111touch $MYFILE.dot
112chmod 444 $MYFILE.*
113test_flags no_graph   -g -o $MYFILE.c $MYFILE.y
114rm -f $MYFILE.dot
115
116touch $MYFILE.output
117chmod 444 $MYFILE.*
118test_flags no_verbose -v -o $MYFILE.c $MYFILE.y
119test_flags no_output  -o $MYFILE.output $MYFILE.y
120test_flags no_output1  -o$MYFILE.output $MYFILE.y
121test_flags no_output2  -o
122rm -f $MYFILE.output
123
124touch $MYFILE.h
125chmod 444 $MYFILE.*
126test_flags no_defines -d -o $MYFILE.c $MYFILE.y
127rm -f $MYFILE.h
128
129touch $MYFILE.i
130chmod 444 $MYFILE.*
131test_flags no_include -i -o $MYFILE.c $MYFILE.y
132rm -f $MYFILE.i
133
134touch $MYFILE.code.c
135chmod 444 $MYFILE.*
136test_flags no_code_c -r -o $MYFILE.c $MYFILE.y
137rm -f $MYFILE.code.c
138
139rm -f $MYFILE.*
140
141for input in ${TEST_DIR}/*.y
142do
143	case $input in
144	test-*)
145		echo "?? ignored $input"
146		;;
147	*)
148		root=`basename $input .y`
149		ROOT="test-$root"
150		prefix=${root}_
151
152		OPTS=
153		OPT2=
154		OOPT=
155		TYPE=".error .output .tab.c .tab.h"
156		case $input in
157		${TEST_DIR}/btyacc_*)
158			if test $ifBTYACC = 0; then continue; fi
159			OPTS="$OPTS -B"
160			prefix=`echo "$prefix" | sed -e 's/^btyacc_//'`
161			;;
162		${TEST_DIR}/grammar*)
163			OPTS="$OPTS -g"
164			TYPE="$TYPE .dot"
165			;;
166		${TEST_DIR}/code_debug*)
167			OPTS="$OPTS -t -i"
168			OOPT=rename_debug.c
169			TYPE="$TYPE .i"
170			prefix=
171			;;
172		${TEST_DIR}/code_*)
173			OPTS="$OPTS -r"
174			TYPE="$TYPE .code.c"
175			prefix=`echo "$prefix" | sed -e 's/^code_//'`
176			;;
177		${TEST_DIR}/pure_*)
178			OPTS="$OPTS -P"
179			prefix=`echo "$prefix" | sed -e 's/^pure_//'`
180			;;
181		${TEST_DIR}/quote_*)
182			OPT2="-s"
183			;;
184		${TEST_DIR}/inherit*|\
185		${TEST_DIR}/err_inherit*)
186			if test $ifBTYACC = 0; then continue; fi
187			;;
188		esac
189
190		echo "** testing $input"
191
192		test -n "$prefix" && prefix="-p $prefix"
193
194		for opt2 in "" $OPT2
195		do
196			output=$OOPT
197			if test -n "$output"
198			then
199				output="-o $output"
200				error=`basename $OOPT .c`.error
201			else
202				error=${ROOT}${opt2}.error
203			fi
204
205			$YACC $OPTS $opt2 -v -d $output $prefix -b $ROOT${opt2} $input 2>$error
206			for type in $TYPE
207			do
208				REF=${REF_DIR}/${root}${opt2}${type}
209
210				# handle renaming due to "-o" option
211				if test -n "$OOPT"
212				then
213					case $type in
214					*.tab.c)
215						type=.c
216						;;
217					*.tab.h)
218						type=.h
219						;;
220					*)
221						;;
222					esac
223					NEW=`basename $OOPT .c`${type}
224					case $NEW in
225					test-*)
226						;;
227					*)
228						if test -f "$NEW"
229						then
230							REF=${REF_DIR}/$NEW
231							mv $NEW test-$NEW
232							NEW=test-$NEW
233						fi
234						;;
235					esac
236				else
237					NEW=${ROOT}${opt2}${type}
238				fi
239				test_diffs
240			done
241		done
242		;;
243	esac
244done
245
246exit $errors
247