xref: /freebsd/contrib/bc/tests/error.sh (revision 6f80738b228c04e3ff3f2d14eea2161d2cf4f81c)
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
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"
103	read_call="read()"
104	read_expr="${read_call}\n5+5;"
105else
106	opts="-x"
107	halt="q"
108fi
109
110printf 'Running %s error file %s with clamping...' "$d" "$t"
111
112printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $opts -c "$testfile" 2> "$out" > /dev/null
113err="$?"
114
115checkerrtest "$d" "$err" "$testfile" "$out" "$exebase" > /dev/null
116
117printf 'pass\n'
118
119printf 'Running %s error file %s without clamping...' "$d" "$t"
120
121printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $opts -C "$testfile" 2> "$out" > /dev/null
122err="$?"
123
124checkerrtest "$d" "$err" "$testfile" "$out" "$exebase" > /dev/null
125
126printf 'pass\n'
127
128printf 'Running %s error file %s through cat with clamping...' "$d" "$t"
129
130cat "$testfile" 2> /dev/null | "$exe" "$@" $opts -c 2> "$out" > /dev/null
131err="$?"
132
133checkerrtest "$d" "$err" "$testfile" "$out" "$exebase"
134
135printf 'pass\n'
136
137printf 'Running %s error file %s through cat without clamping...' "$d" "$t"
138
139cat "$testfile" 2> /dev/null | "$exe" "$@" $opts -C 2> "$out" > /dev/null
140err="$?"
141
142checkerrtest "$d" "$err" "$testfile" "$out" "$exebase"
143
144printf 'pass\n'
145