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