xref: /freebsd/contrib/bc/tests/all.sh (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
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
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 [run_extra_tests] [run_stack_tests] [gen_tests] [time_tests] [exec args...]" 1
42fi
43
44if [ "$#" -lt 1 ]; then
45	extra=1
46else
47	extra="$1"
48	shift
49fi
50
51if [ "$#" -lt 1 ]; then
52	run_stack_tests=1
53else
54	run_stack_tests="$1"
55	shift
56fi
57
58if [ "$#" -lt 1 ]; then
59	generate_tests=1
60else
61	generate_tests="$1"
62	shift
63fi
64
65if [ "$#" -lt 1 ]; then
66	time_tests=0
67else
68	time_tests="$1"
69	shift
70fi
71
72if [ "$#" -lt 1 ]; then
73	exe="$testdir/../bin/$d"
74else
75	exe="$1"
76	shift
77fi
78
79stars="***********************************************************************"
80printf '%s\n' "$stars"
81
82if [ "$d" = "bc" ]; then
83	halt="quit"
84else
85	halt="q"
86fi
87
88unset BC_ENV_ARGS
89unset BC_LINE_LENGTH
90unset DC_ENV_ARGS
91unset DC_LINE_LENGTH
92
93printf '\nRunning %s tests...\n\n' "$d"
94
95while read t; do
96
97	if [ "$extra" -eq 0  ]; then
98		if [ "$t" = "trunc" ] || [ "$t" = "places" ] || [ "$t" = "shift" ] || \
99		   [ "$t" = "lib2" ] || [ "$t" = "scientific" ] || [ "$t" = "rand" ] || \
100		   [ "$t" = "engineering" ]
101		then
102			printf 'Skipping %s %s\n' "$d" "$t"
103			continue
104		fi
105	fi
106
107	sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$time_tests" "$exe" "$@"
108
109done < "$testdir/$d/all.txt"
110
111sh "$testdir/stdin.sh" "$d" "$exe" "$@"
112
113sh "$testdir/scripts.sh" "$d" "$extra" "$run_stack_tests" "$generate_tests" "$time_tests" "$exe" "$@"
114sh "$testdir/read.sh" "$d" "$exe" "$@"
115sh "$testdir/errors.sh" "$d" "$exe" "$@"
116
117num=100000000000000000000000000000000000000000000000000000000000000000000000000000
118numres="$num"
119num70="10000000000000000000000000000000000000000000000000000000000000000000\\
1200000000000"
121
122if [ "$d" = "bc" ]; then
123	halt="halt"
124	opt="x"
125	lopt="extended-register"
126	line_var="BC_LINE_LENGTH"
127else
128	halt="q"
129	opt="l"
130	lopt="mathlib"
131	line_var="DC_LINE_LENGTH"
132	num="$num pR"
133fi
134
135printf '\nRunning %s quit test...' "$d"
136
137printf '%s\n' "$halt" | "$exe" "$@" > /dev/null 2>&1
138
139if [ "$d" = bc ]; then
140	printf '%s\n' "quit" | "$exe" "$@" > /dev/null 2>&1
141	two=$("$exe" "$@" -e 1+1 -e quit)
142	if [ "$two" != "2" ]; then
143		err_exit "$d failed a quit test" 1
144	fi
145fi
146
147printf 'pass\n'
148
149base=$(basename "$exe")
150
151if [ "$base" != "bc" -a "$base" != "dc" ]; then
152	exit 0
153fi
154
155printf 'Running %s environment var tests...' "$d"
156
157if [ "$d" = "bc" ]; then
158	export BC_ENV_ARGS=" '-l' '' -q"
159	export BC_EXPR_EXIT="1"
160	printf 's(.02893)\n' | "$exe" "$@" > /dev/null
161	"$exe" -e 4 "$@" > /dev/null
162else
163	export DC_ENV_ARGS="'-x'"
164	export DC_EXPR_EXIT="1"
165	printf '4s stuff\n' | "$exe" "$@" > /dev/null
166	"$exe" -e 4pR "$@" > /dev/null
167fi
168
169printf 'pass\n'
170
171out1="$testdir/../.log_$d.txt"
172out2="$testdir/../.log_${d}_test.txt"
173
174printf 'Running %s line length tests...' "$d"
175
176printf '%s\n' "$numres" > "$out1"
177
178export "$line_var"=80
179printf '%s\n' "$num" | "$exe" "$@" > "$out2"
180
181diff "$out1" "$out2"
182
183printf '%s\n' "$num70" > "$out1"
184
185export "$line_var"=2147483647
186printf '%s\n' "$num" | "$exe" "$@" > "$out2"
187
188diff "$out1" "$out2"
189
190printf 'pass\n'
191
192printf 'Running %s arg tests...' "$d"
193
194f="$testdir/$d/add.txt"
195exprs=$(cat "$f")
196results=$(cat "$testdir/$d/add_results.txt")
197
198printf '%s\n%s\n%s\n%s\n' "$results" "$results" "$results" "$results" > "$out1"
199
200"$exe" "$@" -e "$exprs" -f "$f" --expression "$exprs" --file "$f" -e "$halt" > "$out2"
201
202diff "$out1" "$out2"
203
204printf '%s\n' "$halt" | "$exe" "$@" -- "$f" "$f" "$f" "$f" > "$out2"
205
206diff "$out1" "$out2"
207
208if [ "$d" = "bc" ]; then
209	printf '%s\n' "$halt" | "$exe" "$@" -i > /dev/null 2>&1
210fi
211
212printf '%s\n' "$halt" | "$exe" "$@" -h > /dev/null
213printf '%s\n' "$halt" | "$exe" "$@" -P > /dev/null
214printf '%s\n' "$halt" | "$exe" "$@" -v > /dev/null
215printf '%s\n' "$halt" | "$exe" "$@" -V > /dev/null
216
217set +e
218
219"$exe" "$@" -f "saotehasotnehasthistohntnsahxstnhalcrgxgrlpyasxtsaosysxsatnhoy.txt" > /dev/null 2> "$out2"
220err="$?"
221
222checktest "$d" "$err" "invalid file argument" "$out2" "$d"
223
224"$exe" "$@" "-$opt" -e "$exprs" > /dev/null 2> "$out2"
225err="$?"
226
227checktest "$d" "$err" "invalid option argument" "$out2" "$d"
228
229"$exe" "$@" "--$lopt" -e "$exprs" > /dev/null 2> "$out2"
230err="$?"
231
232checktest "$d" "$err" "invalid long option argument" "$out2" "$d"
233
234"$exe" "$@" "-u" -e "$exprs" > /dev/null 2> "$out2"
235err="$?"
236
237checktest "$d" "$err" "unrecognized option argument" "$out2" "$d"
238
239"$exe" "$@" "--uniform" -e "$exprs" > /dev/null 2> "$out2"
240err="$?"
241
242checktest "$d" "$err" "unrecognized long option argument" "$out2" "$d"
243
244"$exe" "$@" -f > /dev/null 2> "$out2"
245err="$?"
246
247checktest "$d" "$err" "missing required argument to short option" "$out2" "$d"
248
249"$exe" "$@" --file > /dev/null 2> "$out2"
250err="$?"
251
252checktest "$d" "$err" "missing required argument to long option" "$out2" "$d"
253
254"$exe" "$@" --version=5 > /dev/null 2> "$out2"
255err="$?"
256
257checktest "$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
266checktest "$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
277checktest "$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
286checktest "$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	if [ ! -s "$out2" ]; then
296		err_exit "$d did not produce output on the limits test" 1
297	fi
298
299	printf 'pass\n'
300
301fi
302
303printf '\nAll %s tests passed.\n' "$d"
304
305printf '\n%s\n' "$stars"
306