3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2018-2021 Gavin D. Howard and contributors.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
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.
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.
33 testdir=$(dirname "$script")
35 . "$testdir/../scripts/functions.sh"
37 outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}
39 # Command-line processing.
40 if [ "$#" -ge 2 ]; then
49 err_exit "usage: $script dir extra_math [exec args...]" 1
52 if [ "$#" -lt 1 ]; then
53 exe="$testdir/../bin/$d"
59 if [ "$d" = "bc" ]; then
66 num=100000000000000000000000000000000000000000000000000000000000000000000000000000
69 num70="10000000000000000000000000000000000000000000000000000000000000000000\\
72 # Set stuff for the correct calculator.
73 if [ "$d" = "bc" ]; then
76 lopt="extended-register"
77 line_var="BC_LINE_LENGTH"
78 lltest="line_length()"
83 line_var="DC_LINE_LENGTH"
88 # I use these, so unset them to make the tests work.
96 printf '\nRunning %s quit test...' "$d"
98 printf '%s\n' "$halt" | "$exe" "$@" > /dev/null 2>&1
100 checktest_retcode "$d" "$?" "quit"
102 # bc has two halt or quit commands, so test the second as well.
103 if [ "$d" = bc ]; then
105 printf '%s\n' "quit" | "$exe" "$@" > /dev/null 2>&1
107 checktest_retcode "$d" "$?" quit
109 two=$("$exe" "$@" -e 1+1 -e quit)
111 checktest_retcode "$d" "$?" quit
113 if [ "$two" != "2" ]; then
114 err_exit "$d failed test quit" 1
120 base=$(basename "$exe")
122 printf 'Running %s environment var tests...' "$d"
124 if [ "$d" = "bc" ]; then
126 export BC_ENV_ARGS=" '-l' '' -q"
128 printf 's(.02893)\n' | "$exe" "$@" > /dev/null
130 checktest_retcode "$d" "$?" "environment var"
132 "$exe" "$@" -e 4 > /dev/null
135 checktest_retcode "$d" "$?" "environment var"
139 printf 'Running keyword redefinition test...'
143 redefine_res="$outputdir/bc_outputs/redefine.txt"
144 redefine_out="$outputdir/bc_outputs/redefine_results.txt"
146 outdir=$(dirname "$easter_out")
148 if [ ! -d "$outdir" ]; then
152 printf '5\n0\n' > "$redefine_res"
154 "$exe" "$@" --redefine=print -e 'define print(x) { x }' -e 'print(5)' > "$redefine_out"
156 checktest "$d" "$err" "keyword redefinition" "$redefine_res" "$redefine_out"
158 "$exe" "$@" -r "abs" -r "else" -e 'abs = 5;else = 0' -e 'abs;else' > "$redefine_out"
160 checktest "$d" "$err" "keyword redefinition" "$redefine_res" "$redefine_out"
162 if [ "$extra_math" -ne 0 ]; then
164 "$exe" "$@" -lr abs -e "perm(5, 1)" -e "0" > "$redefine_out"
166 checktest "$d" "$err" "keyword not redefined in builtin library" "$redefine_res" "$redefine_out"
170 "$exe" "$@" -r "break" -e 'define break(x) { x }' 2> "$redefine_out"
173 checkerrtest "$d" "$err" "keyword redefinition error" "$redefine_out" "$d"
175 "$exe" "$@" -e 'define read(x) { x }' 2> "$redefine_out"
178 checkerrtest "$d" "$err" "Keyword redefinition error without BC_REDEFINE_KEYWORDS" "$redefine_out" "$d"
184 export DC_ENV_ARGS="'-x'"
185 export DC_EXPR_EXIT="1"
187 printf '4s stuff\n' | "$exe" "$@" > /dev/null
189 checktest_retcode "$d" "$?" "environment var"
191 "$exe" "$@" -e 4pR > /dev/null
193 checktest_retcode "$d" "$?" "environment var"
199 # dc has an extra test for a case that someone found running this easter.dc
200 # script. It went into an infinite loop, so we want to check that we did not
202 printf 'three\n' | cut -c1-3 > /dev/null
205 if [ "$err" -eq 0 ]; then
207 printf 'Running dc Easter script...'
209 easter_res="$outputdir/dc_outputs/easter.txt"
210 easter_out="$outputdir/dc_outputs/easter_results.txt"
212 outdir=$(dirname "$easter_out")
214 if [ ! -d "$outdir" ]; then
218 printf '4 April 2021\n' > "$easter_res"
220 "$testdir/dc/scripts/easter.sh" "$exe" 2021 "$@" | cut -c1-12 > "$easter_out"
223 checktest "$d" "$err" "Easter script" "$easter_res" "$easter_out"
230 out1="$outputdir/${d}_outputs/${d}_other.txt"
231 out2="$outputdir/${d}_outputs/${d}_other_test.txt"
233 printf 'Running %s line length tests...' "$d"
235 printf '%s\n' "$numres" > "$out1"
237 export "$line_var"=80
238 printf '%s\n' "$num" | "$exe" "$@" > "$out2"
240 checktest "$d" "$?" "line length" "$out1" "$out2"
242 printf '%s\n' "$num70" > "$out1"
244 export "$line_var"=2147483647
245 printf '%s\n' "$num" | "$exe" "$@" > "$out2"
247 checktest "$d" "$?" "line length 2" "$out1" "$out2"
249 printf '%s\n' "$num2" > "$out1"
251 export "$line_var"=62
252 printf '%s\n' "$num" | "$exe" "$@" -L > "$out2"
254 checktest "$d" "$?" "line length 3" "$out1" "$out2"
256 printf '0\n' > "$out1"
257 printf '%s\n' "$lltest" | "$exe" "$@" -L > "$out2"
259 checktest "$d" "$?" "line length 3" "$out1" "$out2"
263 printf '%s\n' "$numres" > "$out1"
264 export "$line_var"=2147483647
266 printf 'Running %s arg tests...' "$d"
268 f="$testdir/$d/add.txt"
270 results=$(cat "$testdir/$d/add_results.txt")
272 printf '%s\n%s\n%s\n%s\n' "$results" "$results" "$results" "$results" > "$out1"
274 "$exe" "$@" -e "$exprs" -f "$f" --expression "$exprs" --file "$f" -e "$halt" > "$out2"
276 checktest "$d" "$?" "arg" "$out1" "$out2"
278 printf '%s\n' "$halt" | "$exe" "$@" -- "$f" "$f" "$f" "$f" > "$out2"
280 checktest "$d" "$?" "arg" "$out1" "$out2"
282 if [ "$d" = "bc" ]; then
283 printf '%s\n' "$halt" | "$exe" "$@" -i > /dev/null 2>&1
286 printf '%s\n' "$halt" | "$exe" "$@" -h > /dev/null
287 checktest_retcode "$d" "$?" "arg"
288 printf '%s\n' "$halt" | "$exe" "$@" -P > /dev/null
289 checktest_retcode "$d" "$?" "arg"
290 printf '%s\n' "$halt" | "$exe" "$@" -R > /dev/null
291 checktest_retcode "$d" "$?" "arg"
292 printf '%s\n' "$halt" | "$exe" "$@" -v > /dev/null
293 checktest_retcode "$d" "$?" "arg"
294 printf '%s\n' "$halt" | "$exe" "$@" -V > /dev/null
295 checktest_retcode "$d" "$?" "arg"
297 out=$(printf '0.1\n-0.1\n1.1\n-1.1\n0.1\n-0.1\n')
298 printf '%s\n' "$out" > "$out1"
300 if [ "$d" = "bc" ]; then
301 data=$(printf '0.1\n-0.1\n1.1\n-1.1\n.1\n-.1\n')
303 data=$(printf '0.1pR\n_0.1pR\n1.1pR\n_1.1pR\n.1pR\n_.1pR\n')
306 printf '%s\n' "$data" | "$exe" "$@" -z > "$out2"
307 checktest "$d" "$?" "leading zero" "$out1" "$out2"
309 if [ "$d" = "bc" ] && [ "$extra_math" -ne 0 ]; then
311 printf '%s\n' "$halt" | "$exe" "$@" -lz "$testdir/bc/leadingzero.txt" > "$out2"
313 checktest "$d" "$?" "leading zero script" "$testdir/bc/leadingzero_results.txt" "$out2"
317 "$exe" "$@" -f "saotehasotnehasthistohntnsahxstnhalcrgxgrlpyasxtsaosysxsatnhoy.txt" > /dev/null 2> "$out2"
320 checkerrtest "$d" "$err" "invalid file argument" "$out2" "$d"
322 "$exe" "$@" "-$opt" -e "$exprs" > /dev/null 2> "$out2"
325 checkerrtest "$d" "$err" "invalid option argument" "$out2" "$d"
327 "$exe" "$@" "--$lopt" -e "$exprs" > /dev/null 2> "$out2"
330 checkerrtest "$d" "$err" "invalid long option argument" "$out2" "$d"
332 "$exe" "$@" "-u" -e "$exprs" > /dev/null 2> "$out2"
335 checkerrtest "$d" "$err" "unrecognized option argument" "$out2" "$d"
337 "$exe" "$@" "--uniform" -e "$exprs" > /dev/null 2> "$out2"
340 checkerrtest "$d" "$err" "unrecognized long option argument" "$out2" "$d"
342 "$exe" "$@" -f > /dev/null 2> "$out2"
345 checkerrtest "$d" "$err" "missing required argument to short option" "$out2" "$d"
347 "$exe" "$@" --file > /dev/null 2> "$out2"
350 checkerrtest "$d" "$err" "missing required argument to long option" "$out2" "$d"
352 "$exe" "$@" --version=5 > /dev/null 2> "$out2"
355 checkerrtest "$d" "$err" "given argument to long option with no argument" "$out2" "$d"
357 "$exe" "$@" -: > /dev/null 2> "$out2"
360 checkerrtest "$d" "$err" "colon short option" "$out2" "$d"
362 "$exe" "$@" --: > /dev/null 2> "$out2"
365 checkerrtest "$d" "$err" "colon long option" "$out2" "$d"
369 printf 'Running %s directory test...' "$d"
371 "$exe" "$@" "$testdir" > /dev/null 2> "$out2"
374 checkerrtest "$d" "$err" "directory" "$out2" "$d"
378 printf 'Running %s binary file test...' "$d"
382 "$exe" "$@" "$bin" > /dev/null 2> "$out2"
385 checkerrtest "$d" "$err" "binary file" "$out2" "$d"
389 printf 'Running %s binary stdin test...' "$d"
391 cat "$bin" | "$exe" "$@" > /dev/null 2> "$out2"
394 checkerrtest "$d" "$err" "binary stdin" "$out2" "$d"
398 if [ "$d" = "bc" ]; then
400 printf 'Running %s limits tests...' "$d"
401 printf 'limits\n' | "$exe" "$@" > "$out2" /dev/null 2>&1
403 checktest_retcode "$d" "$?" "limits"
405 if [ ! -s "$out2" ]; then
406 err_exit "$d did not produce output on the limits test" 1