xref: /aosp_15_r20/external/bc/tests/test.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 test [generate_tests] [time_tests] [exe [args...]]\n' "$0"
48*5a6e8488SAndroid Build Coastguard Worker	printf 'valid dirs are:\n'
49*5a6e8488SAndroid Build Coastguard Worker	printf '\n'
50*5a6e8488SAndroid Build Coastguard Worker	cat "$testdir/all.txt"
51*5a6e8488SAndroid Build Coastguard Worker	printf '\n'
52*5a6e8488SAndroid Build Coastguard Worker	exit 1
53*5a6e8488SAndroid Build Coastguard Worker}
54*5a6e8488SAndroid Build Coastguard Worker
55*5a6e8488SAndroid Build Coastguard Worker# Command-line processing.
56*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 2 ]; then
57*5a6e8488SAndroid Build Coastguard Worker	usage "Need at least 2 arguments"
58*5a6e8488SAndroid Build Coastguard Workerfi
59*5a6e8488SAndroid Build Coastguard Worker
60*5a6e8488SAndroid Build Coastguard Workerd="$1"
61*5a6e8488SAndroid Build Coastguard Workershift
62*5a6e8488SAndroid Build Coastguard Workercheck_d_arg "$d"
63*5a6e8488SAndroid Build Coastguard Worker
64*5a6e8488SAndroid Build Coastguard Worker# We don't use check_file_arg on the test or the result because they might be
65*5a6e8488SAndroid Build Coastguard Worker# generated.
66*5a6e8488SAndroid Build Coastguard Workert="$1"
67*5a6e8488SAndroid Build Coastguard Workername="$testdir/$d/$t.txt"
68*5a6e8488SAndroid Build Coastguard Workerresults="$testdir/$d/${t}_results.txt"
69*5a6e8488SAndroid Build Coastguard Workershift
70*5a6e8488SAndroid Build Coastguard Worker
71*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
72*5a6e8488SAndroid Build Coastguard Worker	generate_tests="$1"
73*5a6e8488SAndroid Build Coastguard Worker	shift
74*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$generate_tests"
75*5a6e8488SAndroid Build Coastguard Workerelse
76*5a6e8488SAndroid Build Coastguard Worker	generate_tests=1
77*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$generate_tests"
78*5a6e8488SAndroid Build Coastguard Workerfi
79*5a6e8488SAndroid Build Coastguard Worker
80*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
81*5a6e8488SAndroid Build Coastguard Worker	time_tests="$1"
82*5a6e8488SAndroid Build Coastguard Worker	shift
83*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$time_tests"
84*5a6e8488SAndroid Build Coastguard Workerelse
85*5a6e8488SAndroid Build Coastguard Worker	time_tests=0
86*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$time_tests"
87*5a6e8488SAndroid Build Coastguard Workerfi
88*5a6e8488SAndroid Build Coastguard Worker
89*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
90*5a6e8488SAndroid Build Coastguard Worker	exe="$1"
91*5a6e8488SAndroid Build Coastguard Worker	shift
92*5a6e8488SAndroid Build Coastguard Worker	check_exec_arg "$exe"
93*5a6e8488SAndroid Build Coastguard Workerelse
94*5a6e8488SAndroid Build Coastguard Worker	exe="$testdir/../bin/$d"
95*5a6e8488SAndroid Build Coastguard Worker	check_exec_arg "$exe"
96*5a6e8488SAndroid Build Coastguard Workerfi
97*5a6e8488SAndroid Build Coastguard Worker
98*5a6e8488SAndroid Build Coastguard Workerout="$outputdir/${d}_outputs/${t}_results.txt"
99*5a6e8488SAndroid Build Coastguard Workeroutdir=$(dirname "$out")
100*5a6e8488SAndroid Build Coastguard Worker
101*5a6e8488SAndroid Build Coastguard Worker# Make sure the directory exists.
102*5a6e8488SAndroid Build Coastguard Workerif [ ! -d "$outdir" ]; then
103*5a6e8488SAndroid Build Coastguard Worker	mkdir -p "$outdir"
104*5a6e8488SAndroid Build Coastguard Workerfi
105*5a6e8488SAndroid Build Coastguard Worker
106*5a6e8488SAndroid Build Coastguard Worker# I use these, so unset them to make the tests work.
107*5a6e8488SAndroid Build Coastguard Workerunset BC_ENV_ARGS
108*5a6e8488SAndroid Build Coastguard Workerunset BC_LINE_LENGTH
109*5a6e8488SAndroid Build Coastguard Workerunset DC_ENV_ARGS
110*5a6e8488SAndroid Build Coastguard Workerunset DC_LINE_LENGTH
111*5a6e8488SAndroid Build Coastguard Worker
112*5a6e8488SAndroid Build Coastguard Worker# Set stuff for the correct calculator.
113*5a6e8488SAndroid Build Coastguard Workerif [ "$d" = "bc" ]; then
114*5a6e8488SAndroid Build Coastguard Worker	options="-lq"
115*5a6e8488SAndroid Build Coastguard Worker	var="BC_LINE_LENGTH"
116*5a6e8488SAndroid Build Coastguard Worker	halt="halt"
117*5a6e8488SAndroid Build Coastguard Workerelse
118*5a6e8488SAndroid Build Coastguard Worker	options=""
119*5a6e8488SAndroid Build Coastguard Worker	var="DC_LINE_LENGTH"
120*5a6e8488SAndroid Build Coastguard Worker	halt="q"
121*5a6e8488SAndroid Build Coastguard Workerfi
122*5a6e8488SAndroid Build Coastguard Worker
123*5a6e8488SAndroid Build Coastguard Worker# If the test does not exist...
124*5a6e8488SAndroid Build Coastguard Workerif [ ! -f "$name" ]; then
125*5a6e8488SAndroid Build Coastguard Worker
126*5a6e8488SAndroid Build Coastguard Worker	# Skip if we can't generate.
127*5a6e8488SAndroid Build Coastguard Worker	if [ "$generate_tests" -eq 0 ]; then
128*5a6e8488SAndroid Build Coastguard Worker		printf 'Skipping %s %s test\n' "$d" "$t"
129*5a6e8488SAndroid Build Coastguard Worker		exit 0
130*5a6e8488SAndroid Build Coastguard Worker	fi
131*5a6e8488SAndroid Build Coastguard Worker
132*5a6e8488SAndroid Build Coastguard Worker	# Generate.
133*5a6e8488SAndroid Build Coastguard Worker	printf 'Generating %s %s...' "$d" "$t"
134*5a6e8488SAndroid Build Coastguard Worker	"$d" "$testdir/$d/scripts/$t.$d" > "$name"
135*5a6e8488SAndroid Build Coastguard Worker	printf 'done\n'
136*5a6e8488SAndroid Build Coastguard Workerfi
137*5a6e8488SAndroid Build Coastguard Worker
138*5a6e8488SAndroid Build Coastguard Worker# If the results do not exist, generate..
139*5a6e8488SAndroid Build Coastguard Workerif [ ! -f "$results" ]; then
140*5a6e8488SAndroid Build Coastguard Worker	printf 'Generating %s %s results...' "$d" "$t"
141*5a6e8488SAndroid Build Coastguard Worker	printf '%s\n' "$halt" 2> /dev/null | "$d" $options "$name" > "$results"
142*5a6e8488SAndroid Build Coastguard Worker	printf 'done\n'
143*5a6e8488SAndroid Build Coastguard Workerfi
144*5a6e8488SAndroid Build Coastguard Worker
145*5a6e8488SAndroid Build Coastguard Worker# We set this here because GNU bc and dc does not have these options.
146*5a6e8488SAndroid Build Coastguard Workerif [ "$d" = "bc" ]; then
147*5a6e8488SAndroid Build Coastguard Worker	options="-lqc"
148*5a6e8488SAndroid Build Coastguard Workerelse
149*5a6e8488SAndroid Build Coastguard Worker	options="-xc"
150*5a6e8488SAndroid Build Coastguard Workerfi
151*5a6e8488SAndroid Build Coastguard Worker
152*5a6e8488SAndroid Build Coastguard Workerexport $var=string
153*5a6e8488SAndroid Build Coastguard Worker
154*5a6e8488SAndroid Build Coastguard Workerset +e
155*5a6e8488SAndroid Build Coastguard Worker
156*5a6e8488SAndroid Build Coastguard Workerprintf 'Running %s %s...' "$d" "$t"
157*5a6e8488SAndroid Build Coastguard Worker
158*5a6e8488SAndroid Build Coastguard Workerif [ "$time_tests" -ne 0 ]; then
159*5a6e8488SAndroid Build Coastguard Worker	printf '\n'
160*5a6e8488SAndroid Build Coastguard Worker	printf '%s\n' "$halt" 2> /dev/null | /usr/bin/time -p "$exe" "$@" $options "$name" > "$out"
161*5a6e8488SAndroid Build Coastguard Worker	err="$?"
162*5a6e8488SAndroid Build Coastguard Worker	printf '\n'
163*5a6e8488SAndroid Build Coastguard Workerelse
164*5a6e8488SAndroid Build Coastguard Worker	printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $options "$name" > "$out"
165*5a6e8488SAndroid Build Coastguard Worker	err="$?"
166*5a6e8488SAndroid Build Coastguard Workerfi
167*5a6e8488SAndroid Build Coastguard Worker
168*5a6e8488SAndroid Build Coastguard Workerchecktest "$d" "$err" "$t" "$results" "$out"
169*5a6e8488SAndroid Build Coastguard Worker
170*5a6e8488SAndroid Build Coastguard Workerrm -f "$out"
171*5a6e8488SAndroid Build Coastguard Worker
172*5a6e8488SAndroid Build Coastguard Workerexec printf 'pass\n'
173