xref: /aosp_15_r20/external/bc/tests/script.sh (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker#! /bin/sh
2*5a6e8488SAndroid Build Coastguard Worker#
3*5a6e8488SAndroid Build Coastguard Worker# SPDX-License-Identifier: BSD-2-Clause
4*5a6e8488SAndroid Build Coastguard Worker#
5*5a6e8488SAndroid Build Coastguard Worker# Copyright (c) 2018-2024 Gavin D. Howard and contributors.
6*5a6e8488SAndroid Build Coastguard Worker#
7*5a6e8488SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without
8*5a6e8488SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met:
9*5a6e8488SAndroid Build Coastguard Worker#
10*5a6e8488SAndroid Build Coastguard Worker# * Redistributions of source code must retain the above copyright notice, this
11*5a6e8488SAndroid Build Coastguard Worker#   list of conditions and the following disclaimer.
12*5a6e8488SAndroid Build Coastguard Worker#
13*5a6e8488SAndroid Build Coastguard Worker# * Redistributions in binary form must reproduce the above copyright notice,
14*5a6e8488SAndroid Build Coastguard Worker#   this list of conditions and the following disclaimer in the documentation
15*5a6e8488SAndroid Build Coastguard Worker#   and/or other materials provided with the distribution.
16*5a6e8488SAndroid Build Coastguard Worker#
17*5a6e8488SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*5a6e8488SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5a6e8488SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5a6e8488SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*5a6e8488SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*5a6e8488SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*5a6e8488SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*5a6e8488SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*5a6e8488SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*5a6e8488SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*5a6e8488SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE.
28*5a6e8488SAndroid Build Coastguard Worker#
29*5a6e8488SAndroid Build Coastguard Worker
30*5a6e8488SAndroid Build Coastguard Workerset -e
31*5a6e8488SAndroid Build Coastguard Worker
32*5a6e8488SAndroid Build Coastguard Workerscript="$0"
33*5a6e8488SAndroid Build Coastguard Worker
34*5a6e8488SAndroid Build Coastguard Workertestdir=$(dirname "${script}")
35*5a6e8488SAndroid Build Coastguard Worker
36*5a6e8488SAndroid Build Coastguard Worker. "$testdir/../scripts/functions.sh"
37*5a6e8488SAndroid Build Coastguard Worker
38*5a6e8488SAndroid Build Coastguard Workeroutputdir=${BC_TEST_OUTPUT_DIR:-$testdir}
39*5a6e8488SAndroid Build Coastguard Worker
40*5a6e8488SAndroid Build Coastguard Worker# Just print the usage and exit with an error. This can receive a message to
41*5a6e8488SAndroid Build Coastguard Worker# print.
42*5a6e8488SAndroid Build Coastguard Worker# @param 1  A message to print.
43*5a6e8488SAndroid Build Coastguard Workerusage() {
44*5a6e8488SAndroid Build Coastguard Worker	if [ $# -eq 1 ]; then
45*5a6e8488SAndroid Build Coastguard Worker		printf '%s\n\n' "$1"
46*5a6e8488SAndroid Build Coastguard Worker	fi
47*5a6e8488SAndroid Build Coastguard Worker	printf 'usage: %s dir script [run_extra_tests] [run_stack_tests] [generate_tests] [time_tests] [exec args...]\n' "$script"
48*5a6e8488SAndroid Build Coastguard Worker	exit 1
49*5a6e8488SAndroid Build Coastguard Worker}
50*5a6e8488SAndroid Build Coastguard Worker
51*5a6e8488SAndroid Build Coastguard Worker# Command-line processing.
52*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 2 ]; then
53*5a6e8488SAndroid Build Coastguard Worker	usage "Not enough arguments; expect 2 arguments"
54*5a6e8488SAndroid Build Coastguard Workerfi
55*5a6e8488SAndroid Build Coastguard Worker
56*5a6e8488SAndroid Build Coastguard Workerd="$1"
57*5a6e8488SAndroid Build Coastguard Workershift
58*5a6e8488SAndroid Build Coastguard Workercheck_d_arg "$d"
59*5a6e8488SAndroid Build Coastguard Worker
60*5a6e8488SAndroid Build Coastguard Workerscriptdir="$testdir/$d/scripts"
61*5a6e8488SAndroid Build Coastguard Worker
62*5a6e8488SAndroid Build Coastguard Workerf="$1"
63*5a6e8488SAndroid Build Coastguard Workershift
64*5a6e8488SAndroid Build Coastguard Workercheck_file_arg "$scriptdir/$f"
65*5a6e8488SAndroid Build Coastguard Worker
66*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
67*5a6e8488SAndroid Build Coastguard Worker	run_extra_tests="$1"
68*5a6e8488SAndroid Build Coastguard Worker	shift
69*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$run_extra_tests"
70*5a6e8488SAndroid Build Coastguard Workerelse
71*5a6e8488SAndroid Build Coastguard Worker	run_extra_tests=1
72*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$run_extra_tests"
73*5a6e8488SAndroid Build Coastguard Workerfi
74*5a6e8488SAndroid Build Coastguard Worker
75*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
76*5a6e8488SAndroid Build Coastguard Worker	run_stack_tests="$1"
77*5a6e8488SAndroid Build Coastguard Worker	shift
78*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$run_stack_tests"
79*5a6e8488SAndroid Build Coastguard Workerelse
80*5a6e8488SAndroid Build Coastguard Worker	run_stack_tests=1
81*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$run_stack_tests"
82*5a6e8488SAndroid Build Coastguard Workerfi
83*5a6e8488SAndroid Build Coastguard Worker
84*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
85*5a6e8488SAndroid Build Coastguard Worker	generate="$1"
86*5a6e8488SAndroid Build Coastguard Worker	shift
87*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$generate"
88*5a6e8488SAndroid Build Coastguard Workerelse
89*5a6e8488SAndroid Build Coastguard Worker	generate=1
90*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$generate"
91*5a6e8488SAndroid Build Coastguard Workerfi
92*5a6e8488SAndroid Build Coastguard Worker
93*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
94*5a6e8488SAndroid Build Coastguard Worker	time_tests="$1"
95*5a6e8488SAndroid Build Coastguard Worker	shift
96*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$time_tests"
97*5a6e8488SAndroid Build Coastguard Workerelse
98*5a6e8488SAndroid Build Coastguard Worker	time_tests=0
99*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$generate"
100*5a6e8488SAndroid Build Coastguard Workerfi
101*5a6e8488SAndroid Build Coastguard Worker
102*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
103*5a6e8488SAndroid Build Coastguard Worker	exe="$1"
104*5a6e8488SAndroid Build Coastguard Worker	shift
105*5a6e8488SAndroid Build Coastguard Worker	check_exec_arg "$exe"
106*5a6e8488SAndroid Build Coastguard Workerelse
107*5a6e8488SAndroid Build Coastguard Worker	exe="$testdir/../bin/$d"
108*5a6e8488SAndroid Build Coastguard Workerfi
109*5a6e8488SAndroid Build Coastguard Worker
110*5a6e8488SAndroid Build Coastguard Worker# Set stuff for the correct calculator.
111*5a6e8488SAndroid Build Coastguard Workerif [ "$d" = "bc" ]; then
112*5a6e8488SAndroid Build Coastguard Worker
113*5a6e8488SAndroid Build Coastguard Worker	if [ "$run_stack_tests" -ne 0 ]; then
114*5a6e8488SAndroid Build Coastguard Worker		options="-lgqC"
115*5a6e8488SAndroid Build Coastguard Worker	else
116*5a6e8488SAndroid Build Coastguard Worker		options="-lqC"
117*5a6e8488SAndroid Build Coastguard Worker	fi
118*5a6e8488SAndroid Build Coastguard Worker
119*5a6e8488SAndroid Build Coastguard Worker	halt="halt"
120*5a6e8488SAndroid Build Coastguard Worker
121*5a6e8488SAndroid Build Coastguard Workerelse
122*5a6e8488SAndroid Build Coastguard Worker	options="-xC"
123*5a6e8488SAndroid Build Coastguard Worker	halt="q"
124*5a6e8488SAndroid Build Coastguard Workerfi
125*5a6e8488SAndroid Build Coastguard Worker
126*5a6e8488SAndroid Build Coastguard Workername="${f%.*}"
127*5a6e8488SAndroid Build Coastguard Worker
128*5a6e8488SAndroid Build Coastguard Worker# We specifically want to skip this because it is handled specially.
129*5a6e8488SAndroid Build Coastguard Workerif [ "$f" = "timeconst.bc" ]; then
130*5a6e8488SAndroid Build Coastguard Worker	exit 0
131*5a6e8488SAndroid Build Coastguard Workerfi
132*5a6e8488SAndroid Build Coastguard Worker
133*5a6e8488SAndroid Build Coastguard Worker# Skip the tests that require extra math if we don't have it.
134*5a6e8488SAndroid Build Coastguard Workerif [ "$run_extra_tests" -eq 0 ]; then
135*5a6e8488SAndroid Build Coastguard Worker	if [ "$f" = "rand.bc" ] || [ "$f" = "root.bc" ] || [ "$f" = "i2rand.bc" ]; then
136*5a6e8488SAndroid Build Coastguard Worker		printf 'Skipping %s script: %s\n' "$d" "$f"
137*5a6e8488SAndroid Build Coastguard Worker		exit 0
138*5a6e8488SAndroid Build Coastguard Worker	fi
139*5a6e8488SAndroid Build Coastguard Workerfi
140*5a6e8488SAndroid Build Coastguard Worker
141*5a6e8488SAndroid Build Coastguard Worker# Skip the tests that require global stacks flag if we are not allowed to run
142*5a6e8488SAndroid Build Coastguard Worker# them.
143*5a6e8488SAndroid Build Coastguard Workerif [ "$run_stack_tests" -eq 0 ]; then
144*5a6e8488SAndroid Build Coastguard Worker
145*5a6e8488SAndroid Build Coastguard Worker	if [ "$f" = "globals.bc" ] || [ "$f" = "references.bc" ] || [ "$f" = "rand.bc" ]; then
146*5a6e8488SAndroid Build Coastguard Worker		printf 'Skipping %s script: %s\n' "$d" "$f"
147*5a6e8488SAndroid Build Coastguard Worker		exit 0
148*5a6e8488SAndroid Build Coastguard Worker	fi
149*5a6e8488SAndroid Build Coastguard Worker
150*5a6e8488SAndroid Build Coastguard Workerfi
151*5a6e8488SAndroid Build Coastguard Worker
152*5a6e8488SAndroid Build Coastguard Workerout="$outputdir/${d}_outputs/${name}_script_results.txt"
153*5a6e8488SAndroid Build Coastguard Workeroutdir=$(dirname "$out")
154*5a6e8488SAndroid Build Coastguard Worker
155*5a6e8488SAndroid Build Coastguard Worker# Make sure the directory exists.
156*5a6e8488SAndroid Build Coastguard Workerif [ ! -d "$outdir" ]; then
157*5a6e8488SAndroid Build Coastguard Worker	mkdir -p "$outdir"
158*5a6e8488SAndroid Build Coastguard Workerfi
159*5a6e8488SAndroid Build Coastguard Worker
160*5a6e8488SAndroid Build Coastguard Worker# I use these, so unset them to make the tests work.
161*5a6e8488SAndroid Build Coastguard Workerunset BC_ENV_ARGS
162*5a6e8488SAndroid Build Coastguard Workerunset BC_LINE_LENGTH
163*5a6e8488SAndroid Build Coastguard Workerunset DC_ENV_ARGS
164*5a6e8488SAndroid Build Coastguard Workerunset DC_LINE_LENGTH
165*5a6e8488SAndroid Build Coastguard Worker
166*5a6e8488SAndroid Build Coastguard Workers="$scriptdir/$f"
167*5a6e8488SAndroid Build Coastguard Workerorig="$testdir/$name.txt"
168*5a6e8488SAndroid Build Coastguard Workerresults="$scriptdir/$name.txt"
169*5a6e8488SAndroid Build Coastguard Worker
170*5a6e8488SAndroid Build Coastguard Workerif [ -f "$orig" ]; then
171*5a6e8488SAndroid Build Coastguard Worker	res="$orig"
172*5a6e8488SAndroid Build Coastguard Workerelif [ -f "$results" ]; then
173*5a6e8488SAndroid Build Coastguard Worker	res="$results"
174*5a6e8488SAndroid Build Coastguard Workerelif [ "$generate" -eq 0 ]; then
175*5a6e8488SAndroid Build Coastguard Worker	printf 'Skipping %s script %s\n' "$d" "$f"
176*5a6e8488SAndroid Build Coastguard Worker	exit 0
177*5a6e8488SAndroid Build Coastguard Workerelse
178*5a6e8488SAndroid Build Coastguard Worker
179*5a6e8488SAndroid Build Coastguard Worker	set +e
180*5a6e8488SAndroid Build Coastguard Worker
181*5a6e8488SAndroid Build Coastguard Worker	# This is to check that the command exists. If not, we should not try to
182*5a6e8488SAndroid Build Coastguard Worker	# generate the test. Instead, we should just skip.
183*5a6e8488SAndroid Build Coastguard Worker	command -v "$d" 1>/dev/null 2>&1
184*5a6e8488SAndroid Build Coastguard Worker	err="$?"
185*5a6e8488SAndroid Build Coastguard Worker
186*5a6e8488SAndroid Build Coastguard Worker	set -e
187*5a6e8488SAndroid Build Coastguard Worker
188*5a6e8488SAndroid Build Coastguard Worker	if [ "$err" -ne 0 ]; then
189*5a6e8488SAndroid Build Coastguard Worker		printf 'Could not find %s to generate results; skipping %s script %s\n' "$d" "$d" "$f"
190*5a6e8488SAndroid Build Coastguard Worker		exit 0
191*5a6e8488SAndroid Build Coastguard Worker	fi
192*5a6e8488SAndroid Build Coastguard Worker
193*5a6e8488SAndroid Build Coastguard Worker	printf 'Generating %s results...' "$f"
194*5a6e8488SAndroid Build Coastguard Worker
195*5a6e8488SAndroid Build Coastguard Worker	# This particular test needs to be generated straight.
196*5a6e8488SAndroid Build Coastguard Worker	if [ "$d" = "dc" ] && [ "$f" = "stream.dc" ]; then
197*5a6e8488SAndroid Build Coastguard Worker		printf '%s\n' "$halt" 2> /dev/null | "$d" "$s" > "$results"
198*5a6e8488SAndroid Build Coastguard Worker	else
199*5a6e8488SAndroid Build Coastguard Worker		# This sed, and the script, are to remove an incompatibility with GNU
200*5a6e8488SAndroid Build Coastguard Worker		# bc, where GNU bc is wrong. See the development manual
201*5a6e8488SAndroid Build Coastguard Worker		# (manuals/development.md#script-tests) for more information.
202*5a6e8488SAndroid Build Coastguard Worker		printf '%s\n' "$halt" 2> /dev/null | "$d" "$s" | sed -n -f "$testdir/script.sed" > "$results"
203*5a6e8488SAndroid Build Coastguard Worker	fi
204*5a6e8488SAndroid Build Coastguard Worker	printf 'done\n'
205*5a6e8488SAndroid Build Coastguard Worker	res="$results"
206*5a6e8488SAndroid Build Coastguard Workerfi
207*5a6e8488SAndroid Build Coastguard Worker
208*5a6e8488SAndroid Build Coastguard Workerset +e
209*5a6e8488SAndroid Build Coastguard Worker
210*5a6e8488SAndroid Build Coastguard Workerprintf 'Running %s script %s...' "$d" "$f"
211*5a6e8488SAndroid Build Coastguard Worker
212*5a6e8488SAndroid Build Coastguard Worker# Yes this is poor timing, but it works.
213*5a6e8488SAndroid Build Coastguard Workerif [ "$time_tests" -ne 0 ]; then
214*5a6e8488SAndroid Build Coastguard Worker	printf '\n'
215*5a6e8488SAndroid Build Coastguard Worker	printf '%s\n' "$halt" 2> /dev/null | /usr/bin/time -p "$exe" "$@" $options "$s" > "$out"
216*5a6e8488SAndroid Build Coastguard Worker	err="$?"
217*5a6e8488SAndroid Build Coastguard Worker	printf '\n'
218*5a6e8488SAndroid Build Coastguard Workerelse
219*5a6e8488SAndroid Build Coastguard Worker	printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $options "$s" > "$out"
220*5a6e8488SAndroid Build Coastguard Worker	err="$?"
221*5a6e8488SAndroid Build Coastguard Workerfi
222*5a6e8488SAndroid Build Coastguard Worker
223*5a6e8488SAndroid Build Coastguard Workerchecktest "$d" "$err" "script $f" "$res" "$out"
224*5a6e8488SAndroid Build Coastguard Worker
225*5a6e8488SAndroid Build Coastguard Workerrm -f "$out"
226*5a6e8488SAndroid Build Coastguard Worker
227*5a6e8488SAndroid Build Coastguard Workerexec printf 'pass\n'
228