Imported Upstream version 20220101
[platform/upstream/byacc.git] / test / run_test.sh
1 #!/bin/sh
2 # $Id: run_test.sh,v 1.35 2022/01/01 22:28:17 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%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 -u "$REF" "$CMP"
43                         errors=1
44                 fi
45         fi
46 }
47
48 test_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
62 test_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
84 test_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
114 if test $# = 1
115 then
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'`
120 else
121         PROG_DIR=..
122         TEST_DIR=.
123 fi
124
125 YACC=$PROG_DIR/yacc
126 YACC_escaped=`echo "$PROG_DIR/yacc" | sed -e 's/\./\\\./g'`
127
128 tmpfile=temp$$
129
130 ifBTYACC=`grep -F -l 'define YYBTYACC' $PROG_DIR/config.h > /dev/null; test $? != 0; echo $?`
131
132 if test "$ifBTYACC" = 0; then
133         REF_DIR=${TEST_DIR}/yacc
134 else
135         REF_DIR=${TEST_DIR}/btyacc
136 fi
137
138 rm -f ${REF_DIR}/test-*
139
140 echo "** `date`"
141
142 # Tests which do not need files
143 MYFILE=nosuchfile
144 test_flags help -z
145 test_flags big_b -B
146 test_flags big_l -L
147
148 # Test attempts to read non-existent file
149 rm -f $MYFILE.*
150 test_flags nostdin - $MYFILE.y
151 test_flags no_opts -- $MYFILE.y
152
153 # Test attempts to write to readonly file
154 touch $MYFILE.y
155
156 touch $MYFILE.c
157 chmod 444 $MYFILE.*
158 test_flags no_b_opt   -b
159 test_flags no_b_opt1  -bBASE -o $MYFILE.c $MYFILE.y
160
161 touch $MYFILE.c
162 chmod 444 $MYFILE.*
163 test_flags no_p_opt   -p
164 test_flags no_p_opt1  -pBASE -o $MYFILE.c $MYFILE.y
165 rm -f BASE$MYFILE.c
166
167 touch $MYFILE.dot
168 chmod 444 $MYFILE.*
169 test_flags no_graph   -g -o $MYFILE.c $MYFILE.y
170 rm -f $MYFILE.dot
171
172 touch $MYFILE.output
173 chmod 444 $MYFILE.*
174 test_flags no_verbose -v -o $MYFILE.c $MYFILE.y
175 test_flags no_output  -o $MYFILE.output $MYFILE.y
176 test_flags no_output1  -o$MYFILE.output $MYFILE.y
177 test_flags no_output2  -o
178 rm -f $MYFILE.output
179
180 touch $MYFILE.h
181 chmod 444 $MYFILE.*
182 test_flags no_defines -d -o $MYFILE.c $MYFILE.y
183 rm -f $MYFILE.h
184
185 touch $MYFILE.i
186 chmod 444 $MYFILE.*
187 test_flags no_include -i -o $MYFILE.c $MYFILE.y
188 rm -f $MYFILE.i
189
190 touch $MYFILE.code.c
191 chmod 444 $MYFILE.*
192 test_flags no_code_c -r -o $MYFILE.c $MYFILE.y
193 rm -f $MYFILE.code.c
194
195 rm -f $MYFILE.*
196
197 # Test special cases
198 test_stdin stdin1 - ${TEST_DIR}/calc.y
199 test_stdin stdin2 -- ${TEST_DIR}/calc.y
200
201 test_defines defines1 ${TEST_DIR}/calc.y
202 test_defines defines2 -d ${TEST_DIR}/calc.y
203 test_defines defines3 -b prefix ${TEST_DIR}/calc.y
204
205 for input in "${TEST_DIR}"/*.y
206 do
207         case $input in
208         test-*)
209                 echo "?? ignored $input"
210                 ;;
211         *)
212                 root=`basename "$input" .y`
213                 ROOT="test-$root"
214                 prefix=`echo "${root}_" | sed -e 's/[.]/_/g'`
215
216                 OPTS=
217                 OPT2=
218                 OOPT=
219                 TYPE=".error .output .tab.c .tab.h"
220                 case $input in
221                 ${TEST_DIR}/btyacc_*)
222                         if test "$ifBTYACC" = 0; then continue; fi
223                         OPTS="$OPTS -B"
224                         prefix=`echo "$prefix" | sed -e 's/^btyacc_//'`
225                         ;;
226                 ${TEST_DIR}/grammar*)
227                         OPTS="$OPTS -g"
228                         TYPE="$TYPE .dot"
229                         ;;
230                 ${TEST_DIR}/code_debug*)
231                         OPTS="$OPTS -t -i"
232                         OOPT=rename_debug.c
233                         TYPE="$TYPE .i"
234                         prefix=
235                         ;;
236                 ${TEST_DIR}/code_*)
237                         OPTS="$OPTS -r"
238                         TYPE="$TYPE .code.c"
239                         prefix=`echo "$prefix" | sed -e 's/^code_//'`
240                         ;;
241                 ${TEST_DIR}/pure_*)
242                         OPTS="$OPTS -P"
243                         prefix=`echo "$prefix" | sed -e 's/^pure_//'`
244                         ;;
245                 ${TEST_DIR}/quote_*)
246                         OPT2="-s"
247                         ;;
248                 ${TEST_DIR}/inherit*|\
249                 ${TEST_DIR}/err_inherit*)
250                         if test "$ifBTYACC" = 0; then continue; fi
251                         ;;
252                 esac
253
254                 echo "** testing $input"
255
256                 test -n "$prefix" && prefix="-p $prefix"
257
258                 for opt2 in "" $OPT2
259                 do
260                         output=$OOPT
261                         if test -n "$output"
262                         then
263                                 output="-o $output"
264                                 error=`basename "$OOPT" .c`.error
265                         else
266                                 error=${ROOT}${opt2}.error
267                         fi
268
269                         $YACC $OPTS $opt2 -v -d $output $prefix -b "$ROOT${opt2}" "$input" 2>"$error"
270                         for type in $TYPE
271                         do
272                                 REF=${REF_DIR}/${root}${opt2}${type}
273
274                                 # handle renaming due to "-o" option
275                                 if test -n "$OOPT"
276                                 then
277                                         case $type in
278                                         *.tab.c)
279                                                 type=.c
280                                                 ;;
281                                         *.tab.h)
282                                                 type=.h
283                                                 ;;
284                                         *)
285                                                 ;;
286                                         esac
287                                         NEW=`basename "$OOPT" .c`${type}
288                                         case $NEW in
289                                         test-*)
290                                                 ;;
291                                         *)
292                                                 if test -f "$NEW"
293                                                 then
294                                                         REF=${REF_DIR}/$NEW
295                                                         mv "$NEW" "test-$NEW"
296                                                         NEW="test-$NEW"
297                                                 fi
298                                                 ;;
299                                         esac
300                                 else
301                                         NEW="${ROOT}${opt2}${type}"
302                                 fi
303                                 test_diffs
304                         done
305                 done
306                 ;;
307         esac
308 done
309
310 exit $errors