xref: /freebsd/contrib/bc/tests/errors.sh (revision 7cc42f6d)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2020 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/../functions.sh"
36
37if [ "$#" -eq 0 ]; then
38	printf 'usage: %s dir [exec args...]\n' "$script"
39	exit 1
40else
41	d="$1"
42	shift
43fi
44
45if [ "$#" -lt 1 ]; then
46	exe="$testdir/../bin/$d"
47else
48	exe="$1"
49	shift
50fi
51
52out="$testdir/../.log_${d}_test.txt"
53
54exebase=$(basename "$exe")
55
56posix="posix_errors"
57read_errors="read_errors"
58
59if [ "$d" = "bc" ]; then
60	opts="-l"
61	halt="halt"
62	read_call="read()"
63	read_expr="${read_call}\n5+5;"
64else
65	opts="-x"
66	halt="q"
67fi
68
69for testfile in $testdir/$d/*errors.txt; do
70
71	if [ -z "${testfile##*$read_errors*}" ]; then
72		# We don't test read errors here. Skip.
73		continue
74	fi
75
76	if [ -z "${testfile##*$posix*}" ]; then
77
78		line="last"
79		printf '%s\n' "$line" | "$exe" "$@" "-lw"  2> "$out" > /dev/null
80		err="$?"
81
82		if [ "$err" -ne 0 ]; then
83			die "$d" "returned an error ($err)" "POSIX warning" 1
84		fi
85
86		checktest "$d" "1" "$line" "$out" "$exebase"
87
88		options="-ls"
89	else
90		options="$opts"
91	fi
92
93	base=$(basename "$testfile")
94	base="${base%.*}"
95	printf 'Running %s %s...' "$d" "$base"
96
97	while read -r line; do
98
99		rm -f "$out"
100
101		printf '%s\n' "$line" | "$exe" "$@" "$options" 2> "$out" > /dev/null
102		err="$?"
103
104		checktest "$d" "$err" "$line" "$out" "$exebase"
105
106	done < "$testfile"
107
108	printf 'pass\n'
109
110done
111
112for testfile in $testdir/$d/errors/*.txt; do
113
114	printf 'Running %s error file %s...' "$d" "$testfile"
115
116	printf '%s\n' "$halt" | "$exe" "$@" $opts "$testfile" 2> "$out" > /dev/null
117	err="$?"
118
119	checktest "$d" "$err" "$testfile" "$out" "$exebase"
120
121	printf 'pass\n'
122
123	printf 'Running %s error file %s through cat...' "$d" "$testfile"
124
125	cat "$testfile" | "$exe" "$@" $opts 2> "$out" > /dev/null
126	err="$?"
127
128	checkcrash "$d" "$err" "$testfile"
129
130	printf 'pass\n'
131
132done
133