xref: /freebsd/contrib/bc/tests/script.sh (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2021 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
30set -e
31
32script="$0"
33
34testdir=$(dirname "${script}")
35
36. "$testdir/../scripts/functions.sh"
37
38outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}
39
40# Command-line processing.
41if [ "$#" -lt 2 ]; then
42	printf 'usage: %s dir script [run_extra_tests] [run_stack_tests] [generate_tests] [time_tests] [exec args...]\n' "$script"
43	exit 1
44fi
45
46d="$1"
47shift
48
49f="$1"
50shift
51
52if [ "$#" -gt 0 ]; then
53	run_extra_tests="$1"
54	shift
55else
56	run_extra_tests=1
57fi
58
59if [ "$#" -gt 0 ]; then
60	run_stack_tests="$1"
61	shift
62else
63	run_stack_tests=1
64fi
65
66if [ "$#" -gt 0 ]; then
67	generate="$1"
68	shift
69else
70	generate=1
71fi
72
73if [ "$#" -gt 0 ]; then
74	time_tests="$1"
75	shift
76else
77	time_tests=0
78fi
79
80if [ "$#" -gt 0 ]; then
81	exe="$1"
82	shift
83else
84	exe="$testdir/../bin/$d"
85fi
86
87# Set stuff for the correct calculator.
88if [ "$d" = "bc" ]; then
89
90	if [ "$run_stack_tests" -ne 0 ]; then
91		options="-lgq"
92	else
93		options="-lq"
94	fi
95
96	halt="halt"
97
98else
99	options="-x"
100	halt="q"
101fi
102
103scriptdir="$testdir/$d/scripts"
104
105name="${f%.*}"
106
107# We specifically want to skip this because it is handled specially.
108if [ "$f" = "timeconst.bc" ]; then
109	exit 0
110fi
111
112# Skip the tests that require extra math if we don't have it.
113if [ "$run_extra_tests" -eq 0 ]; then
114	if [ "$f" = "rand.bc" ]; then
115		printf 'Skipping %s script: %s\n' "$d" "$f"
116		exit 0
117	fi
118fi
119
120# Skip the tests that require global stacks flag if we are not allowed to run
121# them.
122if [ "$run_stack_tests" -eq 0 ]; then
123
124	if [ "$f" = "globals.bc" ] || [ "$f" = "references.bc" ] || [ "$f" = "rand.bc" ]; then
125		printf 'Skipping %s script: %s\n' "$d" "$f"
126		exit 0
127	fi
128
129fi
130
131out="$outputdir/${d}_outputs/${name}_script_results.txt"
132outdir=$(dirname "$out")
133
134# Make sure the directory exists.
135if [ ! -d "$outdir" ]; then
136	mkdir -p "$outdir"
137fi
138
139# I use these, so unset them to make the tests work.
140unset BC_ENV_ARGS
141unset BC_LINE_LENGTH
142unset DC_ENV_ARGS
143unset DC_LINE_LENGTH
144
145s="$scriptdir/$f"
146orig="$testdir/$name.txt"
147results="$scriptdir/$name.txt"
148
149if [ -f "$orig" ]; then
150	res="$orig"
151elif [ -f "$results" ]; then
152	res="$results"
153elif [ "$generate" -eq 0 ]; then
154	printf 'Skipping %s script %s\n' "$d" "$f"
155	exit 0
156else
157
158	set +e
159
160	# This is to check that the command exists. If not, we should not try to
161	# generate the test. Instead, we should just skip.
162	command -v "$d"
163	err="$?"
164
165	set -e
166
167	if [ "$err" -ne 0 ]; then
168		printf 'Could not find %s to generate results; skipping %s script %s\n' "$d" "$d" "$f"
169		exit 0
170	fi
171
172	# This sed, and the script, are to remove an incompatibility with GNU bc,
173	# where GNU bc is wrong. See the development manual
174	# (manuals/development.md#script-tests) for more information.
175	printf 'Generating %s results...' "$f"
176	printf '%s\n' "$halt" | "$d" "$s" | sed -n -f "$testdir/script.sed" > "$results"
177	printf 'done\n'
178	res="$results"
179fi
180
181set +e
182
183printf 'Running %s script %s...' "$d" "$f"
184
185# Yes this is poor timing, but it works.
186if [ "$time_tests" -ne 0 ]; then
187	printf '\n'
188	printf '%s\n' "$halt" | /usr/bin/time -p "$exe" "$@" $options "$s" > "$out"
189	err="$?"
190	printf '\n'
191else
192	printf '%s\n' "$halt" | "$exe" "$@" $options "$s" > "$out"
193	err="$?"
194fi
195
196checktest "$d" "$err" "script $f" "$res" "$out"
197
198rm -f "$out"
199
200exec printf 'pass\n'
201