maint: remove unnecessary casts before using gnulib functions
[bison.git] / examples / test
blob738dcae41c7d23937cbc7c4c7d9d35dd4d3989c7
1 #! /bin/sh
3 # Copyright (C) 2005-2015, 2018-2022 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 SHELL=/bin/sh
19 export SHELL
21 me=$(basename "$1" .test)
22 medir=$(dirname "$1" | sed -e 's,.*examples/,,')
24 # Number of the current test.
25 number=1
27 # Exit status of this script.
28 status=0
30 # top_builddir.
31 cwd=$(pwd)
33 # Whether to strip '> ...' lines from the expected output.
34 # See bistromathic.test.
35 strip_prompt=false
37 # If diff supports --strip-trailing-cr, use it, to avoid EOL issues
38 # when testing Java programs on Windows.
39 echo "checking for diff --strip-trailing-cr..."
40 diff_opts=
41 if diff --strip-trailing-cr "$1" "$1"; then
42 diff_opts=--strip-trailing-cr
44 echo "checking for diff --strip-trailing-cr... $diff_opts"
46 # The exercised program.
47 abs_medir=$cwd/examples/$medir
48 if test -x "$abs_medir/$me"; then
49 prog ()
51 "$abs_medir/$me" "$@"
53 elif test -f "$abs_medir/$me.class"; then
54 prog ()
56 "$SHELL" "$cwd/javaexec.sh" -cp "$abs_medir" "$me" "$@"
58 else
59 echo "$me: ERROR: cannot find program to exercise in:"
60 echo "$me: ERROR: $cwd/examples/$medir/$me"
61 exit 1
65 # cleanup
66 # -------
67 cleanup ()
69 status=$?
70 if test -z "$DEBUG"; then
71 cd "$cwd"
72 rm -rf $$.dir
74 exit $status
76 trap cleanup 0 1 2 13 15
77 mkdir $$.dir
78 cd $$.dir
81 # skip [MSG]
82 # ----------
83 # Skip this test.
84 skip ()
86 if test x"$1" != x; then
87 echo "SKIP: $1"
89 # See Autoconf's doc on 'trap'.
90 (exit 77); exit 77
94 # run [-n, -noerr, -t] EXPECTED-EXIT-STATUS EXPECTED-OUTPUT [PARSER-OPTIONS]
95 # --------------------------------------------------------------------------
96 # -n: no final end-of-line in expected-output
97 # -noerr: ignore stderr, otherwise merge it into effective output.
98 # -t: nuke the possible trailing white spaces in the effective output.
99 run ()
101 echo=echo
102 noerr=false
103 rstrip=false
104 while true; do
105 case $1 in
106 (-n) echo=printf; shift;;
107 (-noerr) noerr=true; shift;;
108 (-t) rstrip=true; shift;;
109 (*) break;;
110 esac
111 done
113 # Expected exit status.
114 sta_exp=$1
115 shift
117 # Expected output.
118 $echo "$1" |
119 sed -e 's/Reducing stack by rule .* (line .*):/Reducing stack by rule XX (line XXX):/g' |
120 if $strip_prompt; then
121 # An extra EOL added in bistromathic's main. Keep that empty line.
122 sed -e '/^> ./d;s/^> $//g'
123 else
125 fi >exp
126 shift
128 # Effective exit status.
129 sta_eff=0
131 prog "$@" - <input >out_eff 2>err_eff || sta_eff=$?
133 # Combine effective output and error streams.
135 if $rstrip; then
136 sed -e 's/ *$//g' out_eff
137 else
138 cat out_eff
140 # Ignore informational messages from the JVM.
141 if ! $noerr; then
142 sed \
143 -e '/^Picked up _JAVA_OPTIONS: /d' \
144 -e 's/Reducing stack by rule .* (line .*):/Reducing stack by rule XX (line XXX):/g' \
145 -e 's/^/err: /g' \
146 err_eff
148 } >eff
150 if test $sta_eff -eq $sta_exp; then
151 if diff $diff_opts eff exp >/dev/null 2>&1; then
152 echo "$me: PASS: $number"
153 else
154 echo "$me: FAIL: $number"
155 echo "$me: input:"
156 sed -e 's/^/ /' input
157 echo "$me: expected output:"
158 sed -e 's/^/ /' exp
159 echo "$me: effective output:"
160 sed -e 's/^/ /' eff
161 echo "$me: diff:"
162 diff $diff_opts -u exp eff | sed -e 's/^/ /'
163 status=1
165 else
166 echo "$me: FAIL: $number (expected status: $sta_exp, effective: $sta_eff)"
167 cat err_eff
168 status=1
170 number=$(expr $number + 1)
173 # We have cd'd one level deeper.
174 case $1 in
175 /*) . "$1" || status=2;;
176 *) . "../$1" || status=2;;
177 esac
179 exit $status