xref: /freebsd/contrib/bc/tests/errors.sh (revision ccb59683b98360afaf5b5bb641a68fea22c68d0b)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2023 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"
92	read_call="read()"
93	read_expr="${read_call}\n5+5;"
94else
95	opts="-x"
96	halt="q"
97fi
98
99printf 'Running %s command-line error tests...' "$d"
100
101printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -e "2+2" 2> "$out" > /dev/null
102err="$?"
103
104checkerrtest "$d" "$err" "command-line -e test" "$out" "$exebase"
105
106printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -f "$testdir/$d/decimal.txt" 2> "$out" > /dev/null
107err="$?"
108
109checkerrtest "$d" "$err" "command-line -f test" "$out" "$exebase"
110
111printf 'pass\n'
112
113# Now test the error files in the standard tests directory.
114for testfile in $testdir/$d/*errors.txt; do
115
116	if [ -z "${testfile##*$read_errors*}" ]; then
117		# We don't test read errors here. Skip.
118		continue
119	fi
120
121	# Test bc POSIX errors and warnings.
122	if [ -z "${testfile##*$posix*}" ]; then
123
124		# Just test warnings.
125		line="last"
126		printf '%s\n' "$line" 2> /dev/null | "$exe" "$@" "-lw"  2> "$out" > /dev/null
127		err="$?"
128
129		if [ "$err" -ne 0 ]; then
130			die "$d" "returned an error ($err)" "POSIX warning" 1
131		fi
132
133		checkerrtest "$d" "1" "$line" "$out" "$exebase"
134
135		# Set the options for standard mode.
136		options="-ls"
137
138	else
139		options="$opts"
140	fi
141
142	# Output something pretty.
143	base=$(basename "$testfile")
144	base="${base%.*}"
145	printf 'Running %s %s...' "$d" "$base"
146
147	# Test errors on each line of the file. Yes, each line has a separate error
148	# case.
149	while read -r line; do
150
151		rm -f "$out"
152
153		printf '%s\n' "$line" 2> /dev/null | "$exe" "$@" "$options" 2> "$out" > /dev/null
154		err="$?"
155
156		checkerrtest "$d" "$err" "$line" "$out" "$exebase"
157
158	done < "$testfile"
159
160	printf 'pass\n'
161
162done
163