xref: /freebsd/contrib/bc/tests/errors.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
30# WARNING: Test files cannot have empty lines!
31
32script="$0"
33testdir=$(dirname "$script")
34
35. "$testdir/../scripts/functions.sh"
36
37# Command-line processing.
38if [ "$#" -eq 0 ]; then
39	printf 'usage: %s dir [exec args...]\n' "$script"
40	exit 1
41else
42	d="$1"
43	shift
44fi
45
46if [ "$#" -lt 1 ]; then
47	exe="$testdir/../bin/$d"
48else
49	exe="$1"
50	shift
51fi
52
53# I use these, so unset them to make the tests work.
54unset BC_ENV_ARGS
55unset BC_LINE_LENGTH
56unset DC_ENV_ARGS
57unset DC_LINE_LENGTH
58
59out="$testdir/${d}_outputs/errors_results.txt"
60outdir=$(dirname "$out")
61
62# Make sure the directory exists.
63if [ ! -d "$outdir" ]; then
64	mkdir -p "$outdir"
65fi
66
67exebase=$(basename "$exe")
68
69# These are the filenames for the extra tests.
70posix="posix_errors"
71read_errors="read_errors"
72
73# Set stuff for the correct calculator.
74if [ "$d" = "bc" ]; then
75	opts="-l"
76	halt="halt"
77	read_call="read()"
78	read_expr="${read_call}\n5+5;"
79else
80	opts="-x"
81	halt="q"
82fi
83
84printf 'Running %s command-line error tests...' "$d"
85
86printf '%s\n' "$halt" | "$exe" "$@" -e "1+1" -f- -e "2+2" 2> "$out" > /dev/null
87err="$?"
88
89checkerrtest "$d" "$err" "command-line -e test" "$out" "$exebase"
90
91printf '%s\n' "$halt" | "$exe" "$@" -e "1+1" -f- -f "$testdir/$d/decimal.txt" 2> "$out" > /dev/null
92err="$?"
93
94checkerrtest "$d" "$err" "command-line -f test" "$out" "$exebase"
95
96printf 'pass\n'
97
98# Now test the error files in the standard tests directory.
99for testfile in $testdir/$d/*errors.txt; do
100
101	if [ -z "${testfile##*$read_errors*}" ]; then
102		# We don't test read errors here. Skip.
103		continue
104	fi
105
106	# Test bc POSIX errors and warnings.
107	if [ -z "${testfile##*$posix*}" ]; then
108
109		# Just test warnings.
110		line="last"
111		printf '%s\n' "$line" | "$exe" "$@" "-lw"  2> "$out" > /dev/null
112		err="$?"
113
114		if [ "$err" -ne 0 ]; then
115			die "$d" "returned an error ($err)" "POSIX warning" 1
116		fi
117
118		checkerrtest "$d" "1" "$line" "$out" "$exebase"
119
120		# Set the options for standard mode.
121		options="-ls"
122
123	else
124		options="$opts"
125	fi
126
127	# Output something pretty.
128	base=$(basename "$testfile")
129	base="${base%.*}"
130	printf 'Running %s %s...' "$d" "$base"
131
132	# Test errors on each line of the file. Yes, each line has a separate error
133	# case.
134	while read -r line; do
135
136		rm -f "$out"
137
138		printf '%s\n' "$line" | "$exe" "$@" "$options" 2> "$out" > /dev/null
139		err="$?"
140
141		checkerrtest "$d" "$err" "$line" "$out" "$exebase"
142
143	done < "$testfile"
144
145	printf 'pass\n'
146
147done
148
149# I need to skip a test here on FreeBSD.
150os=$(uname)
151
152# The list of files we need to skip.
153skip_files="
15433.txt
155"
156
157# Test all the files in the errors directory. While the loop above does one test
158# for every line, this does one test per file, but it runs the file through
159# stdin and as a file on the command-line.
160for testfile in $testdir/$d/errors/*.txt; do
161
162	# If we are on FreeBSD...
163	if [ "$os" = "FreeBSD" ] && [ "$d" = "dc" ]; then
164
165		b=$(basename "$testfile")
166
167		# If the file is one of the skip files...
168		if [ -z "${skip_files##*$b*}" ]; then
169
170			printf 'On FreeBSD; skipping %s...\n' "$testfile"
171			continue
172
173		fi
174	fi
175
176	printf 'Running %s error file %s...' "$d" "$testfile"
177
178	printf '%s\n' "$halt" | "$exe" "$@" $opts "$testfile" 2> "$out" > /dev/null
179	err="$?"
180
181	checkerrtest "$d" "$err" "$testfile" "$out" "$exebase"
182
183	printf 'pass\n'
184
185	printf 'Running %s error file %s through cat...' "$d" "$testfile"
186
187	cat "$testfile" | "$exe" "$@" $opts 2> "$out" > /dev/null
188	err="$?"
189
190	checkcrash "$d" "$err" "$testfile"
191
192	printf 'pass\n'
193
194done
195