xref: /freebsd/contrib/bc/tests/scripts.sh (revision 9768746b)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2023 Gavin D. Howard and contributors.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11#   list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright notice,
14#   this list of conditions and the following disclaimer in the documentation
15#   and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30script="$0"
31
32testdir=$(dirname "${script}")
33
34# Just print the usage and exit with an error. This can receive a message to
35# print.
36# @param 1  A message to print.
37usage() {
38	if [ $# -eq 1 ]; then
39		printf '%s\n\n' "$1"
40	fi
41	printf 'usage: %s [-n] dir [run_extra_tests] [run_stack_tests] [generate_tests] [time_tests] [exec args...]\n' "$script"
42	exit 1
43}
44
45pids=""
46
47# We need to figure out if we should run stuff in parallel.
48pll=1
49
50while getopts "n" opt; do
51
52	case "$opt" in
53		n) pll=0 ; shift ; set -e ;;
54		?) usage "Invalid option: $opt" ;;
55	esac
56
57done
58
59# Command-line processing.
60if [ "$#" -eq 0 ]; then
61	usage "Need at least 1 argument"
62else
63	d="$1"
64	shift
65	check_d_arg "$d"
66fi
67
68if [ "$#" -gt 0 ]; then
69	run_extra_tests="$1"
70	shift
71	check_bool_arg "$run_extra_tests"
72else
73	run_extra_tests=1
74	check_bool_arg "$run_extra_tests"
75fi
76
77if [ "$#" -gt 0 ]; then
78	run_stack_tests="$1"
79	shift
80	check_bool_arg "$run_stack_tests"
81else
82	run_stack_tests=1
83	check_bool_arg "$run_stack_tests"
84fi
85
86if [ "$#" -gt 0 ]; then
87	generate="$1"
88	shift
89	check_bool_arg "$generate"
90else
91	generate=1
92	check_bool_arg "$generate"
93fi
94
95if [ "$#" -gt 0 ]; then
96	time_tests="$1"
97	shift
98	check_bool_arg "$time_tests"
99else
100	time_tests=0
101	check_bool_arg "$time_tests"
102fi
103
104if [ "$#" -gt 0 ]; then
105	exe="$1"
106	shift
107	check_exec_arg "$exe"
108else
109	exe="$testdir/../bin/$d"
110	check_exec_arg "$exe"
111fi
112
113scriptdir="$testdir/$d/scripts"
114
115scripts=$(cat "$scriptdir/all.txt")
116
117# Run each script test individually.
118for s in $scripts; do
119
120	f=$(basename "$s")
121
122	if [ "$pll" -ne 0 ]; then
123		sh "$testdir/script.sh" "$d" "$f" "$run_extra_tests" "$run_stack_tests" \
124			"$generate" "$time_tests" "$exe" "$@" &
125		pids="$pids $!"
126	else
127		sh "$testdir/script.sh" "$d" "$f" "$run_extra_tests" "$run_stack_tests" \
128			"$generate" "$time_tests" "$exe" "$@"
129	fi
130
131done
132
133if [ "$pll" -ne 0 ]; then
134
135	exit_err=0
136
137	for p in $pids; do
138
139		wait "$p"
140		err="$?"
141
142		if [ "$err" -ne 0 ]; then
143			printf 'A script failed!\n'
144			exit_err=1
145		fi
146
147	done
148
149	if [ "$exit_err" -ne 0 ]; then
150		exit 1
151	fi
152
153fi
154