xref: /netbsd/external/bsd/byacc/dist/test/run_test.sh (revision e86eba8a)
1f7a86c12Schristos#!/bin/sh
2*e86eba8aSchristos# Id: run_test.sh,v 1.31 2019/11/03 23:44:07 tom Exp
37953eebeSchristos# vi:ts=4 sw=4:
47953eebeSchristos
563eba854Schristoserrors=0
67953eebeSchristos
763eba854Schristos# NEW is the file created by the testcase
863eba854Schristos# REF is the reference file against which to compare
963eba854Schristostest_diffs() {
1063eba854Schristos	# echo "...test_diffs $NEW vs $REF"
1163eba854Schristos	mv -f $NEW ${REF_DIR}/
1263eba854Schristos	CMP=${REF_DIR}/${NEW}
13e833cfb1Schristos	if test ! -f $CMP
14e833cfb1Schristos	then
15f7a86c12Schristos		echo "...not found $CMP"
1663eba854Schristos		errors=1
17e833cfb1Schristos	else
1863eba854Schristos		sed	-e s,$NEW,$REF, \
1963eba854Schristos			-e "s%$YACC_escaped%YACC%" \
20*e86eba8aSchristos			-e "s%^yacc\>%YACC%" \
21*e86eba8aSchristos			-e "s%YACC:.*option.*$%YACC: error message%" \
22*e86eba8aSchristos			-e "s%^Usage: yacc\>%Usage: YACC%" \
2363eba854Schristos			-e '/YYPATCH/s/[0-9][0-9]*/"yyyymmdd"/' \
2463eba854Schristos			-e '/#define YYPATCH/s/PATCH/CHECK/' \
2563eba854Schristos			-e 's,#line \([1-9][0-9]*\) "'$REF_DIR'/,#line \1 ",' \
267953eebeSchristos			-e 's,#line \([1-9][0-9]*\) "'$TEST_DIR'/,#line \1 ",' \
2763eba854Schristos			-e 's,\(YACC:.* line [0-9][0-9]* of "\)'$TEST_DIR/',\1./,' \
287953eebeSchristos			< $CMP >$tmpfile \
297953eebeSchristos			&& mv $tmpfile $CMP
307953eebeSchristos		if test ! -f $REF
317953eebeSchristos		then
327953eebeSchristos			mv $CMP $REF
337953eebeSchristos			echo "...saved $REF"
347953eebeSchristos		elif ( cmp -s $REF $CMP )
35f7a86c12Schristos		then
36f7a86c12Schristos			echo "...ok $REF"
37f7a86c12Schristos			rm -f $CMP
38f7a86c12Schristos		else
39f7a86c12Schristos			echo "...diff $REF"
407953eebeSchristos			diff -u $REF $CMP
4163eba854Schristos			errors=1
42f7a86c12Schristos		fi
43e833cfb1Schristos	fi
4463eba854Schristos}
4563eba854Schristos
4663eba854Schristostest_flags() {
4763eba854Schristos	echo "** testing flags $*"
4863eba854Schristos	root=$1
4963eba854Schristos	ROOT=test-$root
5063eba854Schristos	shift 1
51fd0b3accSchristos	$YACC "$@" >$ROOT.output 2>$ROOT.error
5263eba854Schristos	for type in .output .error
5363eba854Schristos	do
5463eba854Schristos		NEW=$ROOT$type
5563eba854Schristos		REF=$REF_DIR/$root$type
5663eba854Schristos		test_diffs
5763eba854Schristos	done
5863eba854Schristos}
5963eba854Schristos
60fd0b3accSchristostest_stdin() {
61fd0b3accSchristos	echo "** testing stdin $*"
62fd0b3accSchristos	root=$1
63fd0b3accSchristos	ROOT=test-$root
64fd0b3accSchristos	shift 1
65fd0b3accSchristos	opts="$1"
66fd0b3accSchristos	shift 1
67fd0b3accSchristos	code=`echo "$1"|sed -e 's/y$/c/' -e "s,${TEST_DIR}/,,"`
68fd0b3accSchristos	if test "x$opts" = "x-"
69fd0b3accSchristos	then
70fd0b3accSchristos		$YACC -o $ROOT.$code $opts <$1 >$ROOT.output 2>$ROOT.error
71fd0b3accSchristos	else
72fd0b3accSchristos		$YACC -o $ROOT.$code $opts  $1 >$ROOT.output 2>$ROOT.error
73fd0b3accSchristos	fi
74fd0b3accSchristos	for type in .output .error .$code
75fd0b3accSchristos	do
76fd0b3accSchristos		NEW=$ROOT$type
77fd0b3accSchristos		REF=$REF_DIR/$root$type
78fd0b3accSchristos		test_diffs
79fd0b3accSchristos	done
80fd0b3accSchristos}
81fd0b3accSchristos
82fd0b3accSchristostest_defines() {
83fd0b3accSchristos	echo "** testing defines $*"
84fd0b3accSchristos	root=$1
85fd0b3accSchristos	ROOT=test-$root
86fd0b3accSchristos	shift 1
87fd0b3accSchristos	opts=
88fd0b3accSchristos	while test $# != 1
89fd0b3accSchristos	do
90fd0b3accSchristos		opts="$opts $1"
91fd0b3accSchristos		shift 1
92fd0b3accSchristos	done
93fd0b3accSchristos	head=`echo "$1"|sed -e 's/y$/h/' -e "s,${TEST_DIR}/,,"`
94fd0b3accSchristos	code=`echo "$1"|sed -e 's/y$/c/' -e "s,${TEST_DIR}/,,"`
95fd0b3accSchristos	$YACC $opts -H $ROOT.$head $1 >$ROOT.output 2>$ROOT.error
96fd0b3accSchristos	for name in prefix.tab.c y.tab.c
97fd0b3accSchristos	do
98fd0b3accSchristos		if test -f $name
99fd0b3accSchristos		then
100fd0b3accSchristos			mv $name $ROOT.$code
101fd0b3accSchristos			break
102fd0b3accSchristos		fi
103fd0b3accSchristos	done
104fd0b3accSchristos	for name in .output .error .$head .$code
105fd0b3accSchristos	do
106fd0b3accSchristos		NEW=$ROOT$name
107fd0b3accSchristos		REF=$REF_DIR/$root$name
108fd0b3accSchristos		test_diffs
109fd0b3accSchristos	done
110fd0b3accSchristos}
111fd0b3accSchristos
11263eba854Schristosif test $# = 1
11363eba854Schristosthen
11463eba854Schristos	PROG_DIR=`pwd`
11563eba854Schristos	TEST_DIR=$1
11663eba854Schristos	PROG_DIR=`echo "$PROG_DIR" | sed -e 's/ /\\\\ /g'`
11763eba854Schristos	TEST_DIR=`echo "$TEST_DIR" | sed -e 's/ /\\\\ /g'`
11863eba854Schristoselse
11963eba854Schristos	PROG_DIR=..
12063eba854Schristos	TEST_DIR=.
12163eba854Schristosfi
12263eba854Schristos
12363eba854SchristosYACC=$PROG_DIR/yacc
12463eba854SchristosYACC_escaped=`echo "$PROG_DIR/yacc" | sed -e 's/\./\\\./g'`
12563eba854Schristos
12663eba854Schristostmpfile=temp$$
12763eba854Schristos
12863eba854SchristosifBTYACC=`fgrep -l 'define YYBTYACC' $PROG_DIR/config.h > /dev/null; test $? != 0; echo $?`
12963eba854Schristos
13063eba854Schristosif test $ifBTYACC = 0; then
13163eba854Schristos	REF_DIR=${TEST_DIR}/yacc
13263eba854Schristoselse
13363eba854Schristos	REF_DIR=${TEST_DIR}/btyacc
13463eba854Schristosfi
13563eba854Schristos
13663eba854Schristosrm -f ${REF_DIR}/test-*
13763eba854Schristos
13863eba854Schristosecho '** '`date`
13963eba854Schristos
14063eba854Schristos# Tests which do not need files
14163eba854SchristosMYFILE=nosuchfile
14263eba854Schristostest_flags help -z
14363eba854Schristostest_flags big_b -B
14463eba854Schristostest_flags big_l -L
14563eba854Schristos
14663eba854Schristos# Test attempts to read non-existent file
14763eba854Schristosrm -f $MYFILE.*
14863eba854Schristostest_flags nostdin - $MYFILE.y
14963eba854Schristostest_flags no_opts -- $MYFILE.y
15063eba854Schristos
15163eba854Schristos# Test attempts to write to readonly file
15263eba854Schristostouch $MYFILE.y
15363eba854Schristos
15463eba854Schristostouch $MYFILE.c
15563eba854Schristoschmod 444 $MYFILE.*
15663eba854Schristostest_flags no_b_opt   -b
15763eba854Schristostest_flags no_b_opt1  -bBASE -o $MYFILE.c $MYFILE.y
15863eba854Schristos
15963eba854Schristostouch $MYFILE.c
16063eba854Schristoschmod 444 $MYFILE.*
16163eba854Schristostest_flags no_p_opt   -p
16263eba854Schristostest_flags no_p_opt1  -pBASE -o $MYFILE.c $MYFILE.y
16363eba854Schristosrm -f BASE$MYFILE.c
16463eba854Schristos
16563eba854Schristostouch $MYFILE.dot
16663eba854Schristoschmod 444 $MYFILE.*
16763eba854Schristostest_flags no_graph   -g -o $MYFILE.c $MYFILE.y
16863eba854Schristosrm -f $MYFILE.dot
16963eba854Schristos
17063eba854Schristostouch $MYFILE.output
17163eba854Schristoschmod 444 $MYFILE.*
17263eba854Schristostest_flags no_verbose -v -o $MYFILE.c $MYFILE.y
17363eba854Schristostest_flags no_output  -o $MYFILE.output $MYFILE.y
17463eba854Schristostest_flags no_output1  -o$MYFILE.output $MYFILE.y
17563eba854Schristostest_flags no_output2  -o
17663eba854Schristosrm -f $MYFILE.output
17763eba854Schristos
17863eba854Schristostouch $MYFILE.h
17963eba854Schristoschmod 444 $MYFILE.*
18063eba854Schristostest_flags no_defines -d -o $MYFILE.c $MYFILE.y
18163eba854Schristosrm -f $MYFILE.h
18263eba854Schristos
18363eba854Schristostouch $MYFILE.i
18463eba854Schristoschmod 444 $MYFILE.*
18563eba854Schristostest_flags no_include -i -o $MYFILE.c $MYFILE.y
18663eba854Schristosrm -f $MYFILE.i
18763eba854Schristos
18863eba854Schristostouch $MYFILE.code.c
18963eba854Schristoschmod 444 $MYFILE.*
19063eba854Schristostest_flags no_code_c -r -o $MYFILE.c $MYFILE.y
19163eba854Schristosrm -f $MYFILE.code.c
19263eba854Schristos
19363eba854Schristosrm -f $MYFILE.*
19463eba854Schristos
195fd0b3accSchristos# Test special cases
196fd0b3accSchristostest_stdin stdin1 - ${TEST_DIR}/calc.y
197fd0b3accSchristostest_stdin stdin2 -- ${TEST_DIR}/calc.y
198fd0b3accSchristos
199fd0b3accSchristostest_defines defines1 ${TEST_DIR}/calc.y
200fd0b3accSchristostest_defines defines2 -d ${TEST_DIR}/calc.y
201fd0b3accSchristostest_defines defines3 -b prefix ${TEST_DIR}/calc.y
202fd0b3accSchristos
20363eba854Schristosfor input in ${TEST_DIR}/*.y
20463eba854Schristosdo
20563eba854Schristos	case $input in
20663eba854Schristos	test-*)
20763eba854Schristos		echo "?? ignored $input"
20863eba854Schristos		;;
20963eba854Schristos	*)
21063eba854Schristos		root=`basename $input .y`
21163eba854Schristos		ROOT="test-$root"
21263eba854Schristos		prefix=${root}_
21363eba854Schristos
21463eba854Schristos		OPTS=
21563eba854Schristos		OPT2=
21663eba854Schristos		OOPT=
21763eba854Schristos		TYPE=".error .output .tab.c .tab.h"
21863eba854Schristos		case $input in
21963eba854Schristos		${TEST_DIR}/btyacc_*)
22063eba854Schristos			if test $ifBTYACC = 0; then continue; fi
22163eba854Schristos			OPTS="$OPTS -B"
22263eba854Schristos			prefix=`echo "$prefix" | sed -e 's/^btyacc_//'`
22363eba854Schristos			;;
22463eba854Schristos		${TEST_DIR}/grammar*)
22563eba854Schristos			OPTS="$OPTS -g"
22663eba854Schristos			TYPE="$TYPE .dot"
22763eba854Schristos			;;
22863eba854Schristos		${TEST_DIR}/code_debug*)
22963eba854Schristos			OPTS="$OPTS -t -i"
23063eba854Schristos			OOPT=rename_debug.c
23163eba854Schristos			TYPE="$TYPE .i"
23263eba854Schristos			prefix=
23363eba854Schristos			;;
23463eba854Schristos		${TEST_DIR}/code_*)
23563eba854Schristos			OPTS="$OPTS -r"
23663eba854Schristos			TYPE="$TYPE .code.c"
23763eba854Schristos			prefix=`echo "$prefix" | sed -e 's/^code_//'`
23863eba854Schristos			;;
23963eba854Schristos		${TEST_DIR}/pure_*)
24063eba854Schristos			OPTS="$OPTS -P"
24163eba854Schristos			prefix=`echo "$prefix" | sed -e 's/^pure_//'`
24263eba854Schristos			;;
24363eba854Schristos		${TEST_DIR}/quote_*)
24463eba854Schristos			OPT2="-s"
24563eba854Schristos			;;
24663eba854Schristos		${TEST_DIR}/inherit*|\
24763eba854Schristos		${TEST_DIR}/err_inherit*)
24863eba854Schristos			if test $ifBTYACC = 0; then continue; fi
24963eba854Schristos			;;
25063eba854Schristos		esac
25163eba854Schristos
25263eba854Schristos		echo "** testing $input"
25363eba854Schristos
25463eba854Schristos		test -n "$prefix" && prefix="-p $prefix"
25563eba854Schristos
25663eba854Schristos		for opt2 in "" $OPT2
25763eba854Schristos		do
25863eba854Schristos			output=$OOPT
25963eba854Schristos			if test -n "$output"
26063eba854Schristos			then
26163eba854Schristos				output="-o $output"
26263eba854Schristos				error=`basename $OOPT .c`.error
26363eba854Schristos			else
26463eba854Schristos				error=${ROOT}${opt2}.error
26563eba854Schristos			fi
26663eba854Schristos
26763eba854Schristos			$YACC $OPTS $opt2 -v -d $output $prefix -b $ROOT${opt2} $input 2>$error
26863eba854Schristos			for type in $TYPE
26963eba854Schristos			do
27063eba854Schristos				REF=${REF_DIR}/${root}${opt2}${type}
27163eba854Schristos
27263eba854Schristos				# handle renaming due to "-o" option
27363eba854Schristos				if test -n "$OOPT"
27463eba854Schristos				then
27563eba854Schristos					case $type in
27663eba854Schristos					*.tab.c)
27763eba854Schristos						type=.c
27863eba854Schristos						;;
27963eba854Schristos					*.tab.h)
28063eba854Schristos						type=.h
28163eba854Schristos						;;
28263eba854Schristos					*)
28363eba854Schristos						;;
28463eba854Schristos					esac
28563eba854Schristos					NEW=`basename $OOPT .c`${type}
28663eba854Schristos					case $NEW in
28763eba854Schristos					test-*)
28863eba854Schristos						;;
28963eba854Schristos					*)
29063eba854Schristos						if test -f "$NEW"
29163eba854Schristos						then
29263eba854Schristos							REF=${REF_DIR}/$NEW
29363eba854Schristos							mv $NEW test-$NEW
29463eba854Schristos							NEW=test-$NEW
29563eba854Schristos						fi
29663eba854Schristos						;;
29763eba854Schristos					esac
29863eba854Schristos				else
29963eba854Schristos					NEW=${ROOT}${opt2}${type}
30063eba854Schristos				fi
30163eba854Schristos				test_diffs
302e833cfb1Schristos			done
303f7a86c12Schristos		done
304f7a86c12Schristos		;;
305f7a86c12Schristos	esac
306f7a86c12Schristosdone
30763eba854Schristos
30863eba854Schristosexit $errors
309