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