xref: /freebsd/contrib/bc/tests/all.sh (revision df21a004be237a1dccd03c7b47254625eea62fa9)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2025 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] [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 ; set -e ;;
54		?) usage "Invalid option: $opt" ;;
55	esac
56
57done
58shift $(($OPTIND - 1))
59
60# Command-line processing.
61if [ "$#" -ge 1 ]; then
62	d="$1"
63	shift
64	check_d_arg "$d"
65else
66	usage "Not enough arguments"
67fi
68
69if [ "$#" -lt 1 ]; then
70	extra=1
71	check_bool_arg "$extra"
72else
73	extra="$1"
74	shift
75	check_bool_arg "$extra"
76fi
77
78if [ "$#" -lt 1 ]; then
79	run_stack_tests=1
80	check_bool_arg "$run_stack_tests"
81else
82	run_stack_tests="$1"
83	shift
84	check_bool_arg "$run_stack_tests"
85fi
86
87if [ "$#" -lt 1 ]; then
88	generate_tests=1
89	check_bool_arg "$generate_tests"
90else
91	generate_tests="$1"
92	shift
93	check_bool_arg "$generate_tests"
94fi
95
96if [ "$#" -lt 1 ]; then
97	problematic_tests=1
98	check_bool_arg "$problematic_tests"
99else
100	problematic_tests="$1"
101	shift
102	check_bool_arg "$problematic_tests"
103fi
104
105if [ "$#" -lt 1 ]; then
106	exe="$testdir/../bin/$d"
107	check_exec_arg "$exe"
108else
109	exe="$1"
110	shift
111	check_exec_arg "$exe"
112fi
113
114stars="***********************************************************************"
115printf '%s\n' "$stars"
116
117# Set stuff for the correct calculator.
118if [ "$d" = "bc" ]; then
119	halt="quit"
120else
121	halt="q"
122fi
123
124# I use these, so unset them to make the tests work.
125unset BC_ENV_ARGS
126unset BC_LINE_LENGTH
127unset DC_ENV_ARGS
128unset DC_LINE_LENGTH
129
130# Get the list of tests that require extra math.
131extra_required=$(cat "$testdir/extra_required.txt")
132
133pids=""
134
135printf '\nRunning %s tests...\n\n' "$d"
136
137# Run the tests one at a time.
138while read t; do
139
140	# If it requires extra, then skip if we don't have it.
141	if [ "$extra" -eq 0 ]; then
142		if [ -z "${extra_required##*$t*}" ]; then
143			printf 'Skipping %s %s\n' "$d" "$t"
144			continue
145		fi
146	fi
147
148	if [ "$pll" -ne 0 ]; then
149		sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$exe" "$@" &
150		pids="$pids $!"
151	else
152		sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$exe" "$@"
153	fi
154
155done < "$testdir/$d/all.txt"
156
157# stdin tests.
158if [ "$pll" -ne 0 ]; then
159	sh "$testdir/stdin.sh" "$d" "$exe" "$@" &
160	pids="$pids $!"
161else
162	sh "$testdir/stdin.sh" "$d" "$exe" "$@"
163fi
164
165# Script tests.
166if [ "$pll" -ne 0 ]; then
167	sh "$testdir/scripts.sh" "$d" "$extra" "$run_stack_tests" "$generate_tests" \
168		"$exe" "$@" &
169	pids="$pids $!"
170else
171	sh "$testdir/scripts.sh" -n "$d" "$extra" "$run_stack_tests" "$generate_tests" \
172		"$exe" "$@"
173fi
174
175# Error tests.
176if [ "$pll" -ne 0 ]; then
177	sh "$testdir/errors.sh" "$d" "$exe" "$@" &
178	pids="$pids $!"
179else
180	sh "$testdir/errors.sh" "$d" "$exe" "$@"
181fi
182
183# Test all the files in the errors directory. While the other error test (in
184# tests/errors.sh) does a test for every line of certain error files in the main
185# directory, this does one test per file in the errors directory, but it runs
186# the file through stdin and as a file on the command-line.
187for testfile in $testdir/$d/errors/*.txt; do
188
189	b=$(basename "$testfile")
190
191	if [ "$pll" -ne 0 ]; then
192		sh "$testdir/error.sh" "$d" "$b" "$problematic_tests" "$@" &
193		pids="$pids $!"
194	else
195		sh "$testdir/error.sh" "$d" "$b" "$problematic_tests" "$@"
196	fi
197
198done
199
200if [ "$pll" -ne 0 ]; then
201
202	exit_err=0
203
204	for p in $pids; do
205
206		wait "$p"
207		err="$?"
208
209		if [ "$err" -ne 0 ]; then
210			printf 'A test failed!\n'
211			exit_err=1
212		fi
213	done
214
215	if [ "$exit_err" -ne 0 ]; then
216		exit 1
217	fi
218
219fi
220
221printf '\nAll %s tests passed.\n' "$d"
222
223printf '\n%s\n' "$stars"
224