xref: /freebsd/contrib/bc/tests/all.sh (revision a970610a3af63b3f4df5b69d91c6b4093a00ed8f)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2024 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 ; 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	time_tests=0
107	check_bool_arg "$time_tests"
108else
109	time_tests="$1"
110	shift
111	check_bool_arg "$time_tests"
112fi
113
114if [ "$#" -lt 1 ]; then
115	exe="$testdir/../bin/$d"
116	check_exec_arg "$exe"
117else
118	exe="$1"
119	shift
120	check_exec_arg "$exe"
121fi
122
123stars="***********************************************************************"
124printf '%s\n' "$stars"
125
126# Set stuff for the correct calculator.
127if [ "$d" = "bc" ]; then
128	halt="quit"
129else
130	halt="q"
131fi
132
133# I use these, so unset them to make the tests work.
134unset BC_ENV_ARGS
135unset BC_LINE_LENGTH
136unset DC_ENV_ARGS
137unset DC_LINE_LENGTH
138
139# Get the list of tests that require extra math.
140extra_required=$(cat "$testdir/extra_required.txt")
141
142pids=""
143
144printf '\nRunning %s tests...\n\n' "$d"
145
146# Run the tests one at a time.
147while read t; do
148
149	# If it requires extra, then skip if we don't have it.
150	if [ "$extra" -eq 0 ]; then
151		if [ -z "${extra_required##*$t*}" ]; then
152			printf 'Skipping %s %s\n' "$d" "$t"
153			continue
154		fi
155	fi
156
157	if [ "$pll" -ne 0 ]; then
158		sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$time_tests" "$exe" "$@" &
159		pids="$pids $!"
160	else
161		sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$time_tests" "$exe" "$@"
162	fi
163
164done < "$testdir/$d/all.txt"
165
166# stdin tests.
167if [ "$pll" -ne 0 ]; then
168	sh "$testdir/stdin.sh" "$d" "$exe" "$@" &
169	pids="$pids $!"
170else
171	sh "$testdir/stdin.sh" "$d" "$exe" "$@"
172fi
173
174# Script tests.
175if [ "$pll" -ne 0 ]; then
176	sh "$testdir/scripts.sh" "$d" "$extra" "$run_stack_tests" "$generate_tests" \
177		"$time_tests" "$exe" "$@" &
178	pids="$pids $!"
179else
180	sh "$testdir/scripts.sh" -n "$d" "$extra" "$run_stack_tests" "$generate_tests" \
181		"$time_tests" "$exe" "$@"
182fi
183
184# Read tests.
185if [ "$pll" -ne 0 ]; then
186	sh "$testdir/read.sh" "$d" "$exe" "$@" &
187	pids="$pids $!"
188else
189	sh "$testdir/read.sh" "$d" "$exe" "$@"
190fi
191
192# Error tests.
193if [ "$pll" -ne 0 ]; then
194	sh "$testdir/errors.sh" "$d" "$exe" "$@" &
195	pids="$pids $!"
196else
197	sh "$testdir/errors.sh" "$d" "$exe" "$@"
198fi
199
200# Test all the files in the errors directory. While the other error test (in
201# tests/errors.sh) does a test for every line, this does one test per file, but
202# it runs the file through stdin and as a file on the command-line.
203for testfile in $testdir/$d/errors/*.txt; do
204
205	b=$(basename "$testfile")
206
207	if [ "$pll" -ne 0 ]; then
208		sh "$testdir/error.sh" "$d" "$b" "$problematic_tests" "$@" &
209		pids="$pids $!"
210	else
211		sh "$testdir/error.sh" "$d" "$b" "$problematic_tests" "$@"
212	fi
213
214done
215
216# Other tests.
217if [ "$pll" -ne 0 ]; then
218	sh "$testdir/other.sh" "$d" "$extra" "$exe" "$@" &
219	pids="$pids $!"
220else
221	sh "$testdir/other.sh" "$d" "$extra" "$exe" "$@"
222fi
223
224if [ "$pll" -ne 0 ]; then
225
226	exit_err=0
227
228	for p in $pids; do
229
230		wait "$p"
231		err="$?"
232
233		if [ "$err" -ne 0 ]; then
234			printf 'A test failed!\n'
235			exit_err=1
236		fi
237	done
238
239	if [ "$exit_err" -ne 0 ]; then
240		exit 1
241	fi
242
243fi
244
245printf '\nAll %s tests passed.\n' "$d"
246
247printf '\n%s\n' "$stars"
248