Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/bc/tests/test.sh
39507 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 test [generate_tests] [exe [args...]]\n' "$0"
48
printf 'valid dirs are:\n'
49
printf '\n'
50
cat "$testdir/all.txt"
51
printf '\n'
52
exit 1
53
}
54
55
# Command-line processing.
56
if [ "$#" -lt 2 ]; then
57
usage "Need at least 2 arguments"
58
fi
59
60
d="$1"
61
shift
62
check_d_arg "$d"
63
64
# We don't use check_file_arg on the test or the result because they might be
65
# generated.
66
t="$1"
67
name="$testdir/$d/$t.txt"
68
results="$testdir/$d/${t}_results.txt"
69
shift
70
71
if [ "$#" -gt 0 ]; then
72
generate_tests="$1"
73
shift
74
check_bool_arg "$generate_tests"
75
else
76
generate_tests=1
77
check_bool_arg "$generate_tests"
78
fi
79
80
if [ "$#" -gt 0 ]; then
81
exe="$1"
82
shift
83
check_exec_arg "$exe"
84
else
85
exe="$testdir/../bin/$d"
86
check_exec_arg "$exe"
87
fi
88
89
out="$outputdir/${d}_outputs/${t}_results.txt"
90
outdir=$(dirname "$out")
91
92
# Make sure the directory exists.
93
if [ ! -d "$outdir" ]; then
94
mkdir -p "$outdir"
95
fi
96
97
# I use these, so unset them to make the tests work.
98
unset BC_ENV_ARGS
99
unset BC_LINE_LENGTH
100
unset DC_ENV_ARGS
101
unset DC_LINE_LENGTH
102
103
# Set stuff for the correct calculator.
104
if [ "$d" = "bc" ]; then
105
options="-lq"
106
var="BC_LINE_LENGTH"
107
halt="halt"
108
else
109
options=""
110
var="DC_LINE_LENGTH"
111
halt="q"
112
fi
113
114
# If the test does not exist...
115
if [ ! -f "$name" ]; then
116
117
# Skip if we can't generate.
118
if [ "$generate_tests" -eq 0 ]; then
119
printf 'Skipping %s %s test\n' "$d" "$t"
120
exit 0
121
fi
122
123
# Generate.
124
printf 'Generating %s %s...' "$d" "$t"
125
"$d" "$testdir/$d/scripts/$t.$d" > "$name"
126
printf 'done\n'
127
fi
128
129
# If the results do not exist, generate..
130
if [ ! -f "$results" ]; then
131
printf 'Generating %s %s results...' "$d" "$t"
132
printf '%s\n' "$halt" 2> /dev/null | "$d" $options "$name" > "$results"
133
printf 'done\n'
134
fi
135
136
# We set this here because GNU bc and dc does not have these options.
137
if [ "$d" = "bc" ]; then
138
options="-lqc"
139
else
140
options="-xc"
141
fi
142
143
export $var=string
144
145
set +e
146
147
printf 'Running %s %s...' "$d" "$t"
148
149
printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $options "$name" > "$out"
150
err="$?"
151
152
checktest "$d" "$err" "$t" "$results" "$out"
153
154
rm -f "$out"
155
156
exec printf 'pass\n'
157
158