xref: /freebsd/contrib/bc/tests/all.sh (revision 81ad6265)
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"
31testdir=$(dirname "$script")
32
33. "$testdir/../scripts/functions.sh"
34
35# Just print the usage and exit with an error. This can receive a message to
36# print.
37# @param 1  A message to print.
38usage() {
39	if [ $# -eq 1 ]; then
40		printf '%s\n\n' "$1"
41	fi
42	print 'usage: %s [-n] dir [run_extra_tests] [run_stack_tests] [gen_tests] [run_problematic_tests] [time_tests] [exec args...]\n' \
43		"$script"
44	exit 1
45}
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 [ "$#" -ge 1 ]; then
61	d="$1"
62	shift
63	check_d_arg "$d"
64else
65	usage "Not enough arguments"
66fi
67
68if [ "$#" -lt 1 ]; then
69	extra=1
70	check_bool_arg "$extra"
71else
72	extra="$1"
73	shift
74	check_bool_arg "$extra"
75fi
76
77if [ "$#" -lt 1 ]; then
78	run_stack_tests=1
79	check_bool_arg "$run_stack_tests"
80else
81	run_stack_tests="$1"
82	shift
83	check_bool_arg "$run_stack_tests"
84fi
85
86if [ "$#" -lt 1 ]; then
87	generate_tests=1
88	check_bool_arg "$generate_tests"
89else
90	generate_tests="$1"
91	shift
92	check_bool_arg "$generate_tests"
93fi
94
95if [ "$#" -lt 1 ]; then
96	problematic_tests=1
97	check_bool_arg "$problematic_tests"
98else
99	problematic_tests="$1"
100	shift
101	check_bool_arg "$problematic_tests"
102fi
103
104if [ "$#" -lt 1 ]; then
105	time_tests=0
106	check_bool_arg "$time_tests"
107else
108	time_tests="$1"
109	shift
110	check_bool_arg "$time_tests"
111fi
112
113if [ "$#" -lt 1 ]; then
114	exe="$testdir/../bin/$d"
115	check_exec_arg "$exe"
116else
117	exe="$1"
118	shift
119	check_exec_arg "$exe"
120fi
121
122stars="***********************************************************************"
123printf '%s\n' "$stars"
124
125# Set stuff for the correct calculator.
126if [ "$d" = "bc" ]; then
127	halt="quit"
128else
129	halt="q"
130fi
131
132# I use these, so unset them to make the tests work.
133unset BC_ENV_ARGS
134unset BC_LINE_LENGTH
135unset DC_ENV_ARGS
136unset DC_LINE_LENGTH
137
138# Get the list of tests that require extra math.
139extra_required=$(cat "$testdir/extra_required.txt")
140
141pids=""
142
143printf '\nRunning %s tests...\n\n' "$d"
144
145# Run the tests one at a time.
146while read t; do
147
148	# If it requires extra, then skip if we don't have it.
149	if [ "$extra" -eq 0 ]; then
150		if [ -z "${extra_required##*$t*}" ]; then
151			printf 'Skipping %s %s\n' "$d" "$t"
152			continue
153		fi
154	fi
155
156	if [ "$pll" -ne 0 ]; then
157		sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$time_tests" "$exe" "$@" &
158		pids="$pids $!"
159	else
160		sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$time_tests" "$exe" "$@"
161	fi
162
163done < "$testdir/$d/all.txt"
164
165# stdin tests.
166if [ "$pll" -ne 0 ]; then
167	sh "$testdir/stdin.sh" "$d" "$exe" "$@" &
168	pids="$pids $!"
169else
170	sh "$testdir/stdin.sh" "$d" "$exe" "$@"
171fi
172
173# Script tests.
174if [ "$pll" -ne 0 ]; then
175	sh "$testdir/scripts.sh" "$d" "$extra" "$run_stack_tests" "$generate_tests" \
176		"$time_tests" "$exe" "$@" &
177	pids="$pids $!"
178else
179	sh "$testdir/scripts.sh" -n "$d" "$extra" "$run_stack_tests" "$generate_tests" \
180		"$time_tests" "$exe" "$@"
181fi
182
183# Read tests.
184if [ "$pll" -ne 0 ]; then
185	sh "$testdir/read.sh" "$d" "$exe" "$@" &
186	pids="$pids $!"
187else
188	sh "$testdir/read.sh" "$d" "$exe" "$@"
189fi
190
191# Error tests.
192if [ "$pll" -ne 0 ]; then
193	sh "$testdir/errors.sh" "$d" "$exe" "$@" &
194	pids="$pids $!"
195else
196	sh "$testdir/errors.sh" "$d" "$exe" "$@"
197fi
198
199# Test all the files in the errors directory. While the other error test (in
200# tests/errors.sh) does a test for every line, this does one test per file, but
201# it runs the file through stdin and as a file on the command-line.
202for testfile in $testdir/$d/errors/*.txt; do
203
204	b=$(basename "$testfile")
205
206	if [ "$pll" -ne 0 ]; then
207		sh "$testdir/error.sh" "$d" "$b" "$problematic_tests" "$@" &
208		pids="$pids $!"
209	else
210		sh "$testdir/error.sh" "$d" "$b" "$problematic_tests" "$@"
211	fi
212
213done
214
215# Other tests.
216if [ "$pll" -ne 0 ]; then
217	sh "$testdir/other.sh" "$d" "$extra" "$exe" "$@" &
218	pids="$pids $!"
219else
220	sh "$testdir/other.sh" "$d" "$extra" "$exe" "$@"
221fi
222
223if [ "$pll" -ne 0 ]; then
224
225	exit_err=0
226
227	for p in $pids; do
228
229		wait "$p"
230		err="$?"
231
232		if [ "$err" -ne 0 ]; then
233			printf 'A test failed!\n'
234			exit_err=1
235		fi
236	done
237
238	if [ "$exit_err" -ne 0 ]; then
239		exit 1
240	fi
241
242fi
243
244printf '\nAll %s tests passed.\n' "$d"
245
246printf '\n%s\n' "$stars"
247