Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/bc/tests/errors.sh
39535 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
# WARNING: Test files cannot have empty lines!
31
32
script="$0"
33
testdir=$(dirname "$script")
34
35
. "$testdir/../scripts/functions.sh"
36
37
outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}
38
39
# Just print the usage and exit with an error. This can receive a message to
40
# print.
41
# @param 1 A message to print.
42
usage() {
43
if [ $# -eq 1 ]; then
44
printf '%s\n\n' "$1"
45
fi
46
printf 'usage: %s dir [exec args...]\n' "$script"
47
exit 1
48
}
49
50
# Command-line processing.
51
if [ "$#" -eq 0 ]; then
52
usage "Not enough arguments"
53
else
54
d="$1"
55
shift
56
check_d_arg "$d"
57
fi
58
59
if [ "$#" -lt 1 ]; then
60
exe="$testdir/../bin/$d"
61
check_exec_arg "$exe"
62
else
63
exe="$1"
64
shift
65
check_exec_arg "$exe"
66
fi
67
68
# I use these, so unset them to make the tests work.
69
unset BC_ENV_ARGS
70
unset BC_LINE_LENGTH
71
unset DC_ENV_ARGS
72
unset DC_LINE_LENGTH
73
74
out="$outputdir/${d}_outputs/errors_results.txt"
75
outdir=$(dirname "$out")
76
77
# Make sure the directory exists.
78
if [ ! -d "$outdir" ]; then
79
mkdir -p "$outdir"
80
fi
81
82
exebase=$(basename "$exe")
83
84
# These are the filenames for the extra tests.
85
posix="posix_errors"
86
read_errors="read_errors"
87
88
# Set stuff for the correct calculator.
89
if [ "$d" = "bc" ]; then
90
opts="-l"
91
halt="halt"
92
else
93
opts="-x"
94
halt="q"
95
fi
96
97
printf 'Running %s command-line error tests...' "$d"
98
99
printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -e "2+2" 2> "$out" > /dev/null
100
err="$?"
101
102
checkerrtest "$d" "$err" "command-line -e test" "$out" "$exebase"
103
104
printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -f "$testdir/$d/decimal.txt" 2> "$out" > /dev/null
105
err="$?"
106
107
checkerrtest "$d" "$err" "command-line -f test" "$out" "$exebase"
108
109
printf 'pass\n'
110
111
# Now test the error files in the standard tests directory.
112
for testfile in $testdir/$d/*errors.txt; do
113
114
if [ -z "${testfile##*$read_errors*}" ]; then
115
# We don't test read errors here. Skip.
116
continue
117
fi
118
119
# Test bc POSIX errors and warnings.
120
if [ -z "${testfile##*$posix*}" ]; then
121
122
# Just test warnings.
123
line="last"
124
printf '%s\n' "$line" 2> /dev/null | "$exe" "$@" "-lw" 2> "$out" > /dev/null
125
err="$?"
126
127
if [ "$err" -ne 0 ]; then
128
die "$d" "returned an error ($err)" "POSIX warning" 1
129
fi
130
131
checkerrtest "$d" "1" "$line" "$out" "$exebase"
132
133
# Set the options for standard mode.
134
options="-ls"
135
136
else
137
options="$opts"
138
fi
139
140
# Output something pretty.
141
base=$(basename "$testfile")
142
base="${base%.*}"
143
printf 'Running %s %s...' "$d" "$base"
144
145
# Test errors on each line of the file. Yes, each line has a separate error
146
# case.
147
while read -r line; do
148
149
rm -f "$out"
150
151
printf '%s\n' "$line" 2> /dev/null | "$exe" "$@" "$options" 2> "$out" > /dev/null
152
err="$?"
153
154
checkerrtest "$d" "$err" "$line" "$out" "$exebase"
155
156
done < "$testfile"
157
158
printf 'pass\n'
159
160
done
161
162