xref: /freebsd/contrib/bc/tests/error.sh (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
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
30script="$0"
31testdir=$(dirname "$script")
32
33. "$testdir/../scripts/functions.sh"
34
35outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}
36
37# Just print the usage and exit with an error. This can receive a message to
38# print.
39# @param 1  A message to print.
40usage() {
41	if [ $# -eq 1 ]; then
42		printf '%s\n\n' "$1"
43	fi
44	printf 'usage: %s dir test problematic_tests [exec args...]\n' "$script"
45	exit 1
46}
47
48# Command-line processing.
49if [ "$#" -lt 3 ]; then
50	usage "Not enough arguments"
51else
52
53	d="$1"
54	shift
55	check_d_arg "$d"
56
57	t="$1"
58	shift
59
60	problematic="$1"
61	shift
62	check_bool_arg "$problematic"
63
64fi
65
66testfile="$testdir/$d/errors/$t"
67check_file_arg "$testfile"
68
69if [ "$#" -lt 1 ]; then
70	exe="$testdir/../bin/$d"
71else
72	exe="$1"
73	shift
74fi
75
76# Just skip tests that are problematic on FreeBSD. These tests can cause FreeBSD
77# to kill bc from memory exhaustion because of overcommit.
78if [ "$d" = "bc" ] && [ "$problematic" -eq 0 ]; then
79	if [ "$t" = "33.txt" ]; then
80		printf 'Skipping problematic %s error file %s...\n' "$d" "$t"
81		exit 0
82	fi
83fi
84
85# I use these, so unset them to make the tests work.
86unset BC_ENV_ARGS
87unset BC_LINE_LENGTH
88unset DC_ENV_ARGS
89unset DC_LINE_LENGTH
90
91out="$outputdir/${d}_outputs/error_results_${t}"
92outdir=$(dirname "$out")
93
94# Make sure the directory exists.
95if [ ! -d "$outdir" ]; then
96	mkdir -p "$outdir"
97fi
98
99# Set stuff for the correct calculator.
100if [ "$d" = "bc" ]; then
101	opts="-l"
102	halt="halt"
103else
104	opts="-x"
105	halt="q"
106fi
107
108printf 'Running %s error file %s with clamping...' "$d" "$t"
109
110printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $opts -c "$testfile" 2> "$out" > /dev/null
111err="$?"
112
113checkerrtest "$d" "$err" "$testfile" "$out" "$exebase" > /dev/null
114
115printf 'pass\n'
116
117printf 'Running %s error file %s without clamping...' "$d" "$t"
118
119printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $opts -C "$testfile" 2> "$out" > /dev/null
120err="$?"
121
122checkerrtest "$d" "$err" "$testfile" "$out" "$exebase" > /dev/null
123
124printf 'pass\n'
125
126printf 'Running %s error file %s through cat with clamping...' "$d" "$t"
127
128cat "$testfile" 2> /dev/null | "$exe" "$@" $opts -c 2> "$out" > /dev/null
129err="$?"
130
131checkerrtest "$d" "$err" "$testfile" "$out" "$exebase"
132
133printf 'pass\n'
134
135printf 'Running %s error file %s through cat without clamping...' "$d" "$t"
136
137cat "$testfile" 2> /dev/null | "$exe" "$@" $opts -C 2> "$out" > /dev/null
138err="$?"
139
140checkerrtest "$d" "$err" "$testfile" "$out" "$exebase"
141
142printf 'pass\n'
143