]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/byacc/test/run_test.sh
MFV: r362513
[FreeBSD/FreeBSD.git] / contrib / byacc / test / run_test.sh
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
5 errors=0
6
7 # NEW is the file created by the testcase
8 # REF is the reference file against which to compare
9 test_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
46 test_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
60 test_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
82 test_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
112 if test $# = 1
113 then
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'`
118 else
119         PROG_DIR=..
120         TEST_DIR=.
121 fi
122
123 YACC=$PROG_DIR/yacc
124 YACC_escaped=`echo "$PROG_DIR/yacc" | sed -e 's/\./\\\./g'`
125
126 tmpfile=temp$$
127
128 ifBTYACC=`fgrep -l 'define YYBTYACC' $PROG_DIR/config.h > /dev/null; test $? != 0; echo $?`
129
130 if test $ifBTYACC = 0; then
131         REF_DIR=${TEST_DIR}/yacc
132 else
133         REF_DIR=${TEST_DIR}/btyacc
134 fi
135
136 rm -f ${REF_DIR}/test-*
137
138 echo '** '`date`
139
140 # Tests which do not need files
141 MYFILE=nosuchfile
142 test_flags help -z
143 test_flags big_b -B
144 test_flags big_l -L
145
146 # Test attempts to read non-existent file
147 rm -f $MYFILE.*
148 test_flags nostdin - $MYFILE.y
149 test_flags no_opts -- $MYFILE.y
150
151 # Test attempts to write to readonly file
152 touch $MYFILE.y
153
154 touch $MYFILE.c
155 chmod 444 $MYFILE.*
156 test_flags no_b_opt   -b
157 test_flags no_b_opt1  -bBASE -o $MYFILE.c $MYFILE.y
158
159 touch $MYFILE.c
160 chmod 444 $MYFILE.*
161 test_flags no_p_opt   -p
162 test_flags no_p_opt1  -pBASE -o $MYFILE.c $MYFILE.y
163 rm -f BASE$MYFILE.c
164
165 touch $MYFILE.dot
166 chmod 444 $MYFILE.*
167 test_flags no_graph   -g -o $MYFILE.c $MYFILE.y
168 rm -f $MYFILE.dot
169
170 touch $MYFILE.output
171 chmod 444 $MYFILE.*
172 test_flags no_verbose -v -o $MYFILE.c $MYFILE.y
173 test_flags no_output  -o $MYFILE.output $MYFILE.y
174 test_flags no_output1  -o$MYFILE.output $MYFILE.y
175 test_flags no_output2  -o
176 rm -f $MYFILE.output
177
178 touch $MYFILE.h
179 chmod 444 $MYFILE.*
180 test_flags no_defines -d -o $MYFILE.c $MYFILE.y
181 rm -f $MYFILE.h
182
183 touch $MYFILE.i
184 chmod 444 $MYFILE.*
185 test_flags no_include -i -o $MYFILE.c $MYFILE.y
186 rm -f $MYFILE.i
187
188 touch $MYFILE.code.c
189 chmod 444 $MYFILE.*
190 test_flags no_code_c -r -o $MYFILE.c $MYFILE.y
191 rm -f $MYFILE.code.c
192
193 rm -f $MYFILE.*
194
195 # Test special cases
196 test_stdin stdin1 - ${TEST_DIR}/calc.y
197 test_stdin stdin2 -- ${TEST_DIR}/calc.y
198
199 test_defines defines1 ${TEST_DIR}/calc.y
200 test_defines defines2 -d ${TEST_DIR}/calc.y
201 test_defines defines3 -b prefix ${TEST_DIR}/calc.y
202
203 for input in ${TEST_DIR}/*.y
204 do
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
306 done
307
308 exit $errors