xref: /freebsd/contrib/bc/tests/other.sh (revision 69c5fa5cd1ec9b09ed88a086607a8a0993818db9)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2021 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
30set -e
31
32script="$0"
33testdir=$(dirname "$script")
34
35. "$testdir/../functions.sh"
36
37if [ "$#" -ge 1 ]; then
38	d="$1"
39	shift
40else
41	err_exit "usage: $script dir [exec args...]" 1
42fi
43
44if [ "$#" -lt 1 ]; then
45	exe="$testdir/../bin/$d"
46else
47	exe="$1"
48	shift
49fi
50
51if [ "$d" = "bc" ]; then
52	halt="quit"
53else
54	halt="q"
55fi
56
57num=100000000000000000000000000000000000000000000000000000000000000000000000000000
58numres="$num"
59num70="10000000000000000000000000000000000000000000000000000000000000000000\\
600000000000"
61
62if [ "$d" = "bc" ]; then
63	halt="halt"
64	opt="x"
65	lopt="extended-register"
66	line_var="BC_LINE_LENGTH"
67else
68	halt="q"
69	opt="l"
70	lopt="mathlib"
71	line_var="DC_LINE_LENGTH"
72	num="$num pR"
73fi
74
75set +e
76
77printf '\nRunning %s quit test...' "$d"
78
79printf '%s\n' "$halt" | "$exe" "$@" > /dev/null 2>&1
80
81checktest_retcode "$d" "$?" "quit"
82
83if [ "$d" = bc ]; then
84
85	printf '%s\n' "quit" | "$exe" "$@" > /dev/null 2>&1
86
87	checktest_retcode "$d" "$?" quit
88
89	two=$("$exe" "$@" -e 1+1 -e quit)
90
91	checktest_retcode "$d" "$?" quit
92
93	if [ "$two" != "2" ]; then
94		err_exit "$d failed test quit" 1
95	fi
96fi
97
98printf 'pass\n'
99
100base=$(basename "$exe")
101
102if [ "$base" != "bc" -a "$base" != "dc" ]; then
103	exit 0
104fi
105
106printf 'Running %s environment var tests...' "$d"
107
108if [ "$d" = "bc" ]; then
109
110	export BC_ENV_ARGS=" '-l' '' -q"
111	export BC_EXPR_EXIT="1"
112
113	printf 's(.02893)\n' | "$exe" "$@" > /dev/null
114
115	checktest_retcode "$d" "$?" "environment var"
116
117	"$exe" -e 4 "$@" > /dev/null
118
119	err="$?"
120	checktest_retcode "$d" "$?" "environment var"
121else
122
123	export DC_ENV_ARGS="'-x'"
124	export DC_EXPR_EXIT="1"
125
126	printf '4s stuff\n' | "$exe" "$@" > /dev/null
127
128	checktest_retcode "$d" "$?" "environment var"
129
130	"$exe" -e 4pR "$@" > /dev/null
131
132	checktest_retcode "$d" "$?" "environment var"
133fi
134
135printf 'pass\n'
136
137out1="$testdir/../.log_$d.txt"
138out2="$testdir/../.log_${d}_test.txt"
139
140printf 'Running %s line length tests...' "$d"
141
142printf '%s\n' "$numres" > "$out1"
143
144export "$line_var"=80
145printf '%s\n' "$num" | "$exe" "$@" > "$out2"
146
147checktest "$d" "$?" "environment var" "$out1" "$out2"
148
149printf '%s\n' "$num70" > "$out1"
150
151export "$line_var"=2147483647
152printf '%s\n' "$num" | "$exe" "$@" > "$out2"
153
154checktest "$d" "$?" "environment var" "$out1" "$out2"
155
156printf 'pass\n'
157
158printf 'Running %s arg tests...' "$d"
159
160f="$testdir/$d/add.txt"
161exprs=$(cat "$f")
162results=$(cat "$testdir/$d/add_results.txt")
163
164printf '%s\n%s\n%s\n%s\n' "$results" "$results" "$results" "$results" > "$out1"
165
166"$exe" "$@" -e "$exprs" -f "$f" --expression "$exprs" --file "$f" -e "$halt" > "$out2"
167
168checktest "$d" "$?" "arg" "$out1" "$out2"
169
170printf '%s\n' "$halt" | "$exe" "$@" -- "$f" "$f" "$f" "$f" > "$out2"
171
172checktest "$d" "$?" "arg" "$out1" "$out2"
173
174if [ "$d" = "bc" ]; then
175	printf '%s\n' "$halt" | "$exe" "$@" -i > /dev/null 2>&1
176fi
177
178printf '%s\n' "$halt" | "$exe" "$@" -h > /dev/null
179checktest_retcode "$d" "$?" "arg"
180printf '%s\n' "$halt" | "$exe" "$@" -P > /dev/null
181checktest_retcode "$d" "$?" "arg"
182printf '%s\n' "$halt" | "$exe" "$@" -v > /dev/null
183checktest_retcode "$d" "$?" "arg"
184printf '%s\n' "$halt" | "$exe" "$@" -V > /dev/null
185checktest_retcode "$d" "$?" "arg"
186
187"$exe" "$@" -f "saotehasotnehasthistohntnsahxstnhalcrgxgrlpyasxtsaosysxsatnhoy.txt" > /dev/null 2> "$out2"
188err="$?"
189
190checkerrtest "$d" "$err" "invalid file argument" "$out2" "$d"
191
192"$exe" "$@" "-$opt" -e "$exprs" > /dev/null 2> "$out2"
193err="$?"
194
195checkerrtest "$d" "$err" "invalid option argument" "$out2" "$d"
196
197"$exe" "$@" "--$lopt" -e "$exprs" > /dev/null 2> "$out2"
198err="$?"
199
200checkerrtest "$d" "$err" "invalid long option argument" "$out2" "$d"
201
202"$exe" "$@" "-u" -e "$exprs" > /dev/null 2> "$out2"
203err="$?"
204
205checkerrtest "$d" "$err" "unrecognized option argument" "$out2" "$d"
206
207"$exe" "$@" "--uniform" -e "$exprs" > /dev/null 2> "$out2"
208err="$?"
209
210checkerrtest "$d" "$err" "unrecognized long option argument" "$out2" "$d"
211
212"$exe" "$@" -f > /dev/null 2> "$out2"
213err="$?"
214
215checkerrtest "$d" "$err" "missing required argument to short option" "$out2" "$d"
216
217"$exe" "$@" --file > /dev/null 2> "$out2"
218err="$?"
219
220checkerrtest "$d" "$err" "missing required argument to long option" "$out2" "$d"
221
222"$exe" "$@" --version=5 > /dev/null 2> "$out2"
223err="$?"
224
225checkerrtest "$d" "$err" "given argument to long option with no argument" "$out2" "$d"
226
227printf 'pass\n'
228
229printf 'Running %s directory test...' "$d"
230
231"$exe" "$@" "$testdir" > /dev/null 2> "$out2"
232err="$?"
233
234checkerrtest "$d" "$err" "directory" "$out2" "$d"
235
236printf 'pass\n'
237
238printf 'Running %s binary file test...' "$d"
239
240bin="/bin/sh"
241
242"$exe" "$@" "$bin" > /dev/null 2> "$out2"
243err="$?"
244
245checkerrtest "$d" "$err" "binary file" "$out2" "$d"
246
247printf 'pass\n'
248
249printf 'Running %s binary stdin test...' "$d"
250
251cat "$bin" | "$exe" "$@" > /dev/null 2> "$out2"
252err="$?"
253
254checkerrtest "$d" "$err" "binary stdin" "$out2" "$d"
255
256printf 'pass\n'
257
258if [ "$d" = "bc" ]; then
259
260	printf 'Running %s limits tests...' "$d"
261	printf 'limits\n' | "$exe" "$@" > "$out2" /dev/null 2>&1
262
263	checktest_retcode "$d" "$?" "limits"
264
265	if [ ! -s "$out2" ]; then
266		err_exit "$d did not produce output on the limits test" 1
267	fi
268
269	exec printf 'pass\n'
270
271fi
272