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