xref: /aosp_15_r20/external/bc/tests/scripts.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 Workerscript="$0"
31*5a6e8488SAndroid Build Coastguard Worker
32*5a6e8488SAndroid Build Coastguard Workertestdir=$(dirname "${script}")
33*5a6e8488SAndroid Build Coastguard Worker
34*5a6e8488SAndroid Build Coastguard Worker. "$testdir/../scripts/functions.sh"
35*5a6e8488SAndroid Build Coastguard Worker
36*5a6e8488SAndroid Build Coastguard Worker# Just print the usage and exit with an error. This can receive a message to
37*5a6e8488SAndroid Build Coastguard Worker# print.
38*5a6e8488SAndroid Build Coastguard Worker# @param 1  A message to print.
39*5a6e8488SAndroid Build Coastguard Workerusage() {
40*5a6e8488SAndroid Build Coastguard Worker	if [ $# -eq 1 ]; then
41*5a6e8488SAndroid Build Coastguard Worker		printf '%s\n\n' "$1"
42*5a6e8488SAndroid Build Coastguard Worker	fi
43*5a6e8488SAndroid Build Coastguard Worker	printf 'usage: %s [-n] dir [run_extra_tests] [run_stack_tests] [generate_tests] [time_tests] [exec args...]\n' "$script"
44*5a6e8488SAndroid Build Coastguard Worker	exit 1
45*5a6e8488SAndroid Build Coastguard Worker}
46*5a6e8488SAndroid Build Coastguard Worker
47*5a6e8488SAndroid Build Coastguard Workerpids=""
48*5a6e8488SAndroid Build Coastguard Worker
49*5a6e8488SAndroid Build Coastguard Worker# We need to figure out if we should run stuff in parallel.
50*5a6e8488SAndroid Build Coastguard Workerpll=1
51*5a6e8488SAndroid Build Coastguard Worker
52*5a6e8488SAndroid Build Coastguard Workerwhile getopts "n" opt; do
53*5a6e8488SAndroid Build Coastguard Worker
54*5a6e8488SAndroid Build Coastguard Worker	case "$opt" in
55*5a6e8488SAndroid Build Coastguard Worker		n) pll=0 ; set -e ;;
56*5a6e8488SAndroid Build Coastguard Worker		?) usage "Invalid option: $opt" ;;
57*5a6e8488SAndroid Build Coastguard Worker	esac
58*5a6e8488SAndroid Build Coastguard Worker
59*5a6e8488SAndroid Build Coastguard Workerdone
60*5a6e8488SAndroid Build Coastguard Workershift $(($OPTIND - 1))
61*5a6e8488SAndroid Build Coastguard Worker
62*5a6e8488SAndroid Build Coastguard Worker# Command-line processing.
63*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -eq 0 ]; then
64*5a6e8488SAndroid Build Coastguard Worker	usage "Need at least 1 argument"
65*5a6e8488SAndroid Build Coastguard Workerelse
66*5a6e8488SAndroid Build Coastguard Worker	d="$1"
67*5a6e8488SAndroid Build Coastguard Worker	shift
68*5a6e8488SAndroid Build Coastguard Worker	check_d_arg "$d"
69*5a6e8488SAndroid Build Coastguard Workerfi
70*5a6e8488SAndroid Build Coastguard Worker
71*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
72*5a6e8488SAndroid Build Coastguard Worker	run_extra_tests="$1"
73*5a6e8488SAndroid Build Coastguard Worker	shift
74*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$run_extra_tests"
75*5a6e8488SAndroid Build Coastguard Workerelse
76*5a6e8488SAndroid Build Coastguard Worker	run_extra_tests=1
77*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$run_extra_tests"
78*5a6e8488SAndroid Build Coastguard Workerfi
79*5a6e8488SAndroid Build Coastguard Worker
80*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
81*5a6e8488SAndroid Build Coastguard Worker	run_stack_tests="$1"
82*5a6e8488SAndroid Build Coastguard Worker	shift
83*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$run_stack_tests"
84*5a6e8488SAndroid Build Coastguard Workerelse
85*5a6e8488SAndroid Build Coastguard Worker	run_stack_tests=1
86*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$run_stack_tests"
87*5a6e8488SAndroid Build Coastguard Workerfi
88*5a6e8488SAndroid Build Coastguard Worker
89*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
90*5a6e8488SAndroid Build Coastguard Worker	generate="$1"
91*5a6e8488SAndroid Build Coastguard Worker	shift
92*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$generate"
93*5a6e8488SAndroid Build Coastguard Workerelse
94*5a6e8488SAndroid Build Coastguard Worker	generate=1
95*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$generate"
96*5a6e8488SAndroid Build Coastguard Workerfi
97*5a6e8488SAndroid Build Coastguard Worker
98*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
99*5a6e8488SAndroid Build Coastguard Worker	time_tests="$1"
100*5a6e8488SAndroid Build Coastguard Worker	shift
101*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$time_tests"
102*5a6e8488SAndroid Build Coastguard Workerelse
103*5a6e8488SAndroid Build Coastguard Worker	time_tests=0
104*5a6e8488SAndroid Build Coastguard Worker	check_bool_arg "$time_tests"
105*5a6e8488SAndroid Build Coastguard Workerfi
106*5a6e8488SAndroid Build Coastguard Worker
107*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -gt 0 ]; then
108*5a6e8488SAndroid Build Coastguard Worker	exe="$1"
109*5a6e8488SAndroid Build Coastguard Worker	shift
110*5a6e8488SAndroid Build Coastguard Worker	check_exec_arg "$exe"
111*5a6e8488SAndroid Build Coastguard Workerelse
112*5a6e8488SAndroid Build Coastguard Worker	exe="$testdir/../bin/$d"
113*5a6e8488SAndroid Build Coastguard Worker	check_exec_arg "$exe"
114*5a6e8488SAndroid Build Coastguard Workerfi
115*5a6e8488SAndroid Build Coastguard Worker
116*5a6e8488SAndroid Build Coastguard Workerscriptdir="$testdir/$d/scripts"
117*5a6e8488SAndroid Build Coastguard Worker
118*5a6e8488SAndroid Build Coastguard Workerscripts=$(cat "$scriptdir/all.txt")
119*5a6e8488SAndroid Build Coastguard Worker
120*5a6e8488SAndroid Build Coastguard Worker# Run each script test individually.
121*5a6e8488SAndroid Build Coastguard Workerfor s in $scripts; do
122*5a6e8488SAndroid Build Coastguard Worker
123*5a6e8488SAndroid Build Coastguard Worker	f=$(basename "$s")
124*5a6e8488SAndroid Build Coastguard Worker
125*5a6e8488SAndroid Build Coastguard Worker	if [ "$pll" -ne 0 ]; then
126*5a6e8488SAndroid Build Coastguard Worker		sh "$testdir/script.sh" "$d" "$f" "$run_extra_tests" "$run_stack_tests" \
127*5a6e8488SAndroid Build Coastguard Worker			"$generate" "$time_tests" "$exe" "$@" &
128*5a6e8488SAndroid Build Coastguard Worker		pids="$pids $!"
129*5a6e8488SAndroid Build Coastguard Worker	else
130*5a6e8488SAndroid Build Coastguard Worker		sh "$testdir/script.sh" "$d" "$f" "$run_extra_tests" "$run_stack_tests" \
131*5a6e8488SAndroid Build Coastguard Worker			"$generate" "$time_tests" "$exe" "$@"
132*5a6e8488SAndroid Build Coastguard Worker	fi
133*5a6e8488SAndroid Build Coastguard Worker
134*5a6e8488SAndroid Build Coastguard Workerdone
135*5a6e8488SAndroid Build Coastguard Worker
136*5a6e8488SAndroid Build Coastguard Workerif [ "$pll" -ne 0 ]; then
137*5a6e8488SAndroid Build Coastguard Worker
138*5a6e8488SAndroid Build Coastguard Worker	exit_err=0
139*5a6e8488SAndroid Build Coastguard Worker
140*5a6e8488SAndroid Build Coastguard Worker	for p in $pids; do
141*5a6e8488SAndroid Build Coastguard Worker
142*5a6e8488SAndroid Build Coastguard Worker		wait "$p"
143*5a6e8488SAndroid Build Coastguard Worker		err="$?"
144*5a6e8488SAndroid Build Coastguard Worker
145*5a6e8488SAndroid Build Coastguard Worker		if [ "$err" -ne 0 ]; then
146*5a6e8488SAndroid Build Coastguard Worker			printf 'A script failed!\n'
147*5a6e8488SAndroid Build Coastguard Worker			exit_err=1
148*5a6e8488SAndroid Build Coastguard Worker		fi
149*5a6e8488SAndroid Build Coastguard Worker
150*5a6e8488SAndroid Build Coastguard Worker	done
151*5a6e8488SAndroid Build Coastguard Worker
152*5a6e8488SAndroid Build Coastguard Worker	if [ "$exit_err" -ne 0 ]; then
153*5a6e8488SAndroid Build Coastguard Worker		exit 1
154*5a6e8488SAndroid Build Coastguard Worker	fi
155*5a6e8488SAndroid Build Coastguard Worker
156*5a6e8488SAndroid Build Coastguard Workerfi
157