xref: /freebsd/contrib/bc/tests/other.sh (revision 5def4c47d4bd90b209b9b4a4ba9faec15846d8fd)
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"
121
122	printf 'pass\n'
123
124else
125
126	export DC_ENV_ARGS="'-x'"
127	export DC_EXPR_EXIT="1"
128
129	printf '4s stuff\n' | "$exe" "$@" > /dev/null
130
131	checktest_retcode "$d" "$?" "environment var"
132
133	"$exe" -e 4pR "$@" > /dev/null
134
135	checktest_retcode "$d" "$?" "environment var"
136
137	printf 'pass\n'
138
139	set +e
140
141	printf 'three\n' | cut -c1-3 > /dev/null
142	err=$?
143
144	if [ "$err" -eq 0 ]; then
145
146		printf 'Running dc Easter script...'
147
148		easter_res="$testdir/dc_outputs/easter.txt"
149		easter_out="$testdir/dc_outputs/easter_results.txt"
150
151		outdir=$(dirname "$easter_out")
152
153		if [ ! -d "$outdir" ]; then
154			mkdir -p "$outdir"
155		fi
156
157		printf '4 April 2021\n' > "$easter_res"
158
159		"$testdir/dc/scripts/easter.sh" "$exe" 2021 | cut -c1-12 > "$easter_out"
160		err="$?"
161
162		checktest "$d" "$err" "Easter script" "$easter_res" "$easter_out"
163
164		printf 'pass\n'
165	fi
166
167fi
168
169out1="$testdir/../.log_$d.txt"
170out2="$testdir/../.log_${d}_test.txt"
171
172printf 'Running %s line length tests...' "$d"
173
174printf '%s\n' "$numres" > "$out1"
175
176export "$line_var"=80
177printf '%s\n' "$num" | "$exe" "$@" > "$out2"
178
179checktest "$d" "$?" "environment var" "$out1" "$out2"
180
181printf '%s\n' "$num70" > "$out1"
182
183export "$line_var"=2147483647
184printf '%s\n' "$num" | "$exe" "$@" > "$out2"
185
186checktest "$d" "$?" "environment var" "$out1" "$out2"
187
188printf 'pass\n'
189
190printf 'Running %s arg tests...' "$d"
191
192f="$testdir/$d/add.txt"
193exprs=$(cat "$f")
194results=$(cat "$testdir/$d/add_results.txt")
195
196printf '%s\n%s\n%s\n%s\n' "$results" "$results" "$results" "$results" > "$out1"
197
198"$exe" "$@" -e "$exprs" -f "$f" --expression "$exprs" --file "$f" -e "$halt" > "$out2"
199
200checktest "$d" "$?" "arg" "$out1" "$out2"
201
202printf '%s\n' "$halt" | "$exe" "$@" -- "$f" "$f" "$f" "$f" > "$out2"
203
204checktest "$d" "$?" "arg" "$out1" "$out2"
205
206if [ "$d" = "bc" ]; then
207	printf '%s\n' "$halt" | "$exe" "$@" -i > /dev/null 2>&1
208fi
209
210printf '%s\n' "$halt" | "$exe" "$@" -h > /dev/null
211checktest_retcode "$d" "$?" "arg"
212printf '%s\n' "$halt" | "$exe" "$@" -P > /dev/null
213checktest_retcode "$d" "$?" "arg"
214printf '%s\n' "$halt" | "$exe" "$@" -v > /dev/null
215checktest_retcode "$d" "$?" "arg"
216printf '%s\n' "$halt" | "$exe" "$@" -V > /dev/null
217checktest_retcode "$d" "$?" "arg"
218
219"$exe" "$@" -f "saotehasotnehasthistohntnsahxstnhalcrgxgrlpyasxtsaosysxsatnhoy.txt" > /dev/null 2> "$out2"
220err="$?"
221
222checkerrtest "$d" "$err" "invalid file argument" "$out2" "$d"
223
224"$exe" "$@" "-$opt" -e "$exprs" > /dev/null 2> "$out2"
225err="$?"
226
227checkerrtest "$d" "$err" "invalid option argument" "$out2" "$d"
228
229"$exe" "$@" "--$lopt" -e "$exprs" > /dev/null 2> "$out2"
230err="$?"
231
232checkerrtest "$d" "$err" "invalid long option argument" "$out2" "$d"
233
234"$exe" "$@" "-u" -e "$exprs" > /dev/null 2> "$out2"
235err="$?"
236
237checkerrtest "$d" "$err" "unrecognized option argument" "$out2" "$d"
238
239"$exe" "$@" "--uniform" -e "$exprs" > /dev/null 2> "$out2"
240err="$?"
241
242checkerrtest "$d" "$err" "unrecognized long option argument" "$out2" "$d"
243
244"$exe" "$@" -f > /dev/null 2> "$out2"
245err="$?"
246
247checkerrtest "$d" "$err" "missing required argument to short option" "$out2" "$d"
248
249"$exe" "$@" --file > /dev/null 2> "$out2"
250err="$?"
251
252checkerrtest "$d" "$err" "missing required argument to long option" "$out2" "$d"
253
254"$exe" "$@" --version=5 > /dev/null 2> "$out2"
255err="$?"
256
257checkerrtest "$d" "$err" "given argument to long option with no argument" "$out2" "$d"
258
259printf 'pass\n'
260
261printf 'Running %s directory test...' "$d"
262
263"$exe" "$@" "$testdir" > /dev/null 2> "$out2"
264err="$?"
265
266checkerrtest "$d" "$err" "directory" "$out2" "$d"
267
268printf 'pass\n'
269
270printf 'Running %s binary file test...' "$d"
271
272bin="/bin/sh"
273
274"$exe" "$@" "$bin" > /dev/null 2> "$out2"
275err="$?"
276
277checkerrtest "$d" "$err" "binary file" "$out2" "$d"
278
279printf 'pass\n'
280
281printf 'Running %s binary stdin test...' "$d"
282
283cat "$bin" | "$exe" "$@" > /dev/null 2> "$out2"
284err="$?"
285
286checkerrtest "$d" "$err" "binary stdin" "$out2" "$d"
287
288printf 'pass\n'
289
290if [ "$d" = "bc" ]; then
291
292	printf 'Running %s limits tests...' "$d"
293	printf 'limits\n' | "$exe" "$@" > "$out2" /dev/null 2>&1
294
295	checktest_retcode "$d" "$?" "limits"
296
297	if [ ! -s "$out2" ]; then
298		err_exit "$d did not produce output on the limits test" 1
299	fi
300
301	exec printf 'pass\n'
302
303fi
304