xref: /freebsd/contrib/bc/tests/errors.sh (revision acd546f01e58354af049455472980c6c4a52e18b)
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
32script="$0"
33testdir=$(dirname "$script")
34
35. "$testdir/../scripts/functions.sh"
36
37outputdir=${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.
42usage() {
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.
51if [ "$#" -eq 0 ]; then
52	usage "Not enough arguments"
53else
54	d="$1"
55	shift
56	check_d_arg "$d"
57fi
58
59if [ "$#" -lt 1 ]; then
60	exe="$testdir/../bin/$d"
61	check_exec_arg "$exe"
62else
63	exe="$1"
64	shift
65	check_exec_arg "$exe"
66fi
67
68# I use these, so unset them to make the tests work.
69unset BC_ENV_ARGS
70unset BC_LINE_LENGTH
71unset DC_ENV_ARGS
72unset DC_LINE_LENGTH
73
74out="$outputdir/${d}_outputs/errors_results.txt"
75outdir=$(dirname "$out")
76
77# Make sure the directory exists.
78if [ ! -d "$outdir" ]; then
79	mkdir -p "$outdir"
80fi
81
82exebase=$(basename "$exe")
83
84# These are the filenames for the extra tests.
85posix="posix_errors"
86read_errors="read_errors"
87
88# Set stuff for the correct calculator.
89if [ "$d" = "bc" ]; then
90	opts="-l"
91	halt="halt"
92else
93	opts="-x"
94	halt="q"
95fi
96
97printf 'Running %s command-line error tests...' "$d"
98
99printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -e "2+2" 2> "$out" > /dev/null
100err="$?"
101
102checkerrtest "$d" "$err" "command-line -e test" "$out" "$exebase"
103
104printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -f "$testdir/$d/decimal.txt" 2> "$out" > /dev/null
105err="$?"
106
107checkerrtest "$d" "$err" "command-line -f test" "$out" "$exebase"
108
109printf 'pass\n'
110
111# Now test the error files in the standard tests directory.
112for 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
160done
161