]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/tests/other.sh
usr.bin/gh-bc, contrib/bc: update to version 5.0.0
[FreeBSD/FreeBSD.git] / contrib / bc / tests / other.sh
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
30 set -e
31
32 script="$0"
33 testdir=$(dirname "$script")
34
35 . "$testdir/../scripts/functions.sh"
36
37 outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}
38
39 # Command-line processing.
40 if [ "$#" -ge 2 ]; then
41
42         d="$1"
43         shift
44
45         extra_math="$1"
46         shift
47
48 else
49         err_exit "usage: $script dir extra_math [exec args...]" 1
50 fi
51
52 if [ "$#" -lt 1 ]; then
53         exe="$testdir/../bin/$d"
54 else
55         exe="$1"
56         shift
57 fi
58
59 if [ "$d" = "bc" ]; then
60         halt="quit"
61 else
62         halt="q"
63 fi
64
65 # For tests later.
66 num=100000000000000000000000000000000000000000000000000000000000000000000000000000
67 num2="$num"
68 numres="$num"
69 num70="10000000000000000000000000000000000000000000000000000000000000000000\\
70 0000000000"
71
72 # Set stuff for the correct calculator.
73 if [ "$d" = "bc" ]; then
74         halt="halt"
75         opt="x"
76         lopt="extended-register"
77         line_var="BC_LINE_LENGTH"
78         lltest="line_length()"
79 else
80         halt="q"
81         opt="l"
82         lopt="mathlib"
83         line_var="DC_LINE_LENGTH"
84         num="$num pR"
85         lltest="glpR"
86 fi
87
88 # I use these, so unset them to make the tests work.
89 unset BC_ENV_ARGS
90 unset BC_LINE_LENGTH
91 unset DC_ENV_ARGS
92 unset DC_LINE_LENGTH
93
94 set +e
95
96 printf '\nRunning %s quit test...' "$d"
97
98 printf '%s\n' "$halt" | "$exe" "$@" > /dev/null 2>&1
99
100 checktest_retcode "$d" "$?" "quit"
101
102 # bc has two halt or quit commands, so test the second as well.
103 if [ "$d" = bc ]; then
104
105         printf '%s\n' "quit" | "$exe" "$@" > /dev/null 2>&1
106
107         checktest_retcode "$d" "$?" quit
108
109         two=$("$exe" "$@" -e 1+1 -e quit)
110
111         checktest_retcode "$d" "$?" quit
112
113         if [ "$two" != "2" ]; then
114                 err_exit "$d failed test quit" 1
115         fi
116 fi
117
118 printf 'pass\n'
119
120 base=$(basename "$exe")
121
122 printf 'Running %s environment var tests...' "$d"
123
124 if [ "$d" = "bc" ]; then
125
126         export BC_ENV_ARGS=" '-l' '' -q"
127
128         printf 's(.02893)\n' | "$exe" "$@" > /dev/null
129
130         checktest_retcode "$d" "$?" "environment var"
131
132         "$exe" "$@" -e 4 > /dev/null
133
134         err="$?"
135         checktest_retcode "$d" "$?" "environment var"
136
137         printf 'pass\n'
138
139         printf 'Running keyword redefinition test...'
140
141         unset BC_ENV_ARGS
142
143         redefine_res="$outputdir/bc_outputs/redefine.txt"
144         redefine_out="$outputdir/bc_outputs/redefine_results.txt"
145
146         outdir=$(dirname "$easter_out")
147
148         if [ ! -d "$outdir" ]; then
149                 mkdir -p "$outdir"
150         fi
151
152         printf '5\n0\n' > "$redefine_res"
153
154         "$exe" "$@" --redefine=print -e 'define print(x) { x }' -e 'print(5)' > "$redefine_out"
155
156         checktest "$d" "$err" "keyword redefinition" "$redefine_res" "$redefine_out"
157
158         "$exe" "$@" -r "abs" -r "else" -e 'abs = 5;else = 0' -e 'abs;else' > "$redefine_out"
159
160         checktest "$d" "$err" "keyword redefinition" "$redefine_res" "$redefine_out"
161
162         if [ "$extra_math" -ne 0 ]; then
163
164                 "$exe" "$@" -lr abs -e "perm(5, 1)" -e "0" > "$redefine_out"
165
166                 checktest "$d" "$err" "keyword not redefined in builtin library" "$redefine_res" "$redefine_out"
167
168         fi
169
170         "$exe" "$@" -r "break" -e 'define break(x) { x }' 2> "$redefine_out"
171         err="$?"
172
173         checkerrtest "$d" "$err" "keyword redefinition error" "$redefine_out" "$d"
174
175         "$exe" "$@" -e 'define read(x) { x }' 2> "$redefine_out"
176         err="$?"
177
178         checkerrtest "$d" "$err" "Keyword redefinition error without BC_REDEFINE_KEYWORDS" "$redefine_out" "$d"
179
180         printf 'pass\n'
181
182 else
183
184         export DC_ENV_ARGS="'-x'"
185         export DC_EXPR_EXIT="1"
186
187         printf '4s stuff\n' | "$exe" "$@" > /dev/null
188
189         checktest_retcode "$d" "$?" "environment var"
190
191         "$exe" "$@" -e 4pR > /dev/null
192
193         checktest_retcode "$d" "$?" "environment var"
194
195         printf 'pass\n'
196
197         set +e
198
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
201         # regress.
202         printf 'three\n' | cut -c1-3 > /dev/null
203         err=$?
204
205         if [ "$err" -eq 0 ]; then
206
207                 printf 'Running dc Easter script...'
208
209                 easter_res="$outputdir/dc_outputs/easter.txt"
210                 easter_out="$outputdir/dc_outputs/easter_results.txt"
211
212                 outdir=$(dirname "$easter_out")
213
214                 if [ ! -d "$outdir" ]; then
215                         mkdir -p "$outdir"
216                 fi
217
218                 printf '4 April 2021\n' > "$easter_res"
219
220                 "$testdir/dc/scripts/easter.sh" "$exe" 2021 "$@" | cut -c1-12 > "$easter_out"
221                 err="$?"
222
223                 checktest "$d" "$err" "Easter script" "$easter_res" "$easter_out"
224
225                 printf 'pass\n'
226         fi
227
228 fi
229
230 out1="$outputdir/${d}_outputs/${d}_other.txt"
231 out2="$outputdir/${d}_outputs/${d}_other_test.txt"
232
233 printf 'Running %s line length tests...' "$d"
234
235 printf '%s\n' "$numres" > "$out1"
236
237 export "$line_var"=80
238 printf '%s\n' "$num" | "$exe" "$@" > "$out2"
239
240 checktest "$d" "$?" "line length" "$out1" "$out2"
241
242 printf '%s\n' "$num70" > "$out1"
243
244 export "$line_var"=2147483647
245 printf '%s\n' "$num" | "$exe" "$@" > "$out2"
246
247 checktest "$d" "$?" "line length 2" "$out1" "$out2"
248
249 printf '%s\n' "$num2" > "$out1"
250
251 export "$line_var"=62
252 printf '%s\n' "$num" | "$exe" "$@" -L > "$out2"
253
254 checktest "$d" "$?" "line length 3" "$out1" "$out2"
255
256 printf '0\n' > "$out1"
257 printf '%s\n' "$lltest" | "$exe" "$@" -L > "$out2"
258
259 checktest "$d" "$?" "line length 3" "$out1" "$out2"
260
261 printf 'pass\n'
262
263 printf '%s\n' "$numres" > "$out1"
264 export "$line_var"=2147483647
265
266 printf 'Running %s arg tests...' "$d"
267
268 f="$testdir/$d/add.txt"
269 exprs=$(cat "$f")
270 results=$(cat "$testdir/$d/add_results.txt")
271
272 printf '%s\n%s\n%s\n%s\n' "$results" "$results" "$results" "$results" > "$out1"
273
274 "$exe" "$@" -e "$exprs" -f "$f" --expression "$exprs" --file "$f" -e "$halt" > "$out2"
275
276 checktest "$d" "$?" "arg" "$out1" "$out2"
277
278 printf '%s\n' "$halt" | "$exe" "$@" -- "$f" "$f" "$f" "$f" > "$out2"
279
280 checktest "$d" "$?" "arg" "$out1" "$out2"
281
282 if [ "$d" = "bc" ]; then
283         printf '%s\n' "$halt" | "$exe" "$@" -i > /dev/null 2>&1
284 fi
285
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"
296
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"
299
300 if [ "$d" = "bc" ]; then
301         data=$(printf '0.1\n-0.1\n1.1\n-1.1\n.1\n-.1\n')
302 else
303         data=$(printf '0.1pR\n_0.1pR\n1.1pR\n_1.1pR\n.1pR\n_.1pR\n')
304 fi
305
306 printf '%s\n' "$data" | "$exe" "$@" -z > "$out2"
307 checktest "$d" "$?" "leading zero" "$out1" "$out2"
308
309 if [ "$d" = "bc" ] && [ "$extra_math" -ne 0 ]; then
310
311         printf '%s\n' "$halt" | "$exe" "$@" -lz "$testdir/bc/leadingzero.txt" > "$out2"
312
313         checktest "$d" "$?" "leading zero script" "$testdir/bc/leadingzero_results.txt" "$out2"
314
315 fi
316
317 "$exe" "$@" -f "saotehasotnehasthistohntnsahxstnhalcrgxgrlpyasxtsaosysxsatnhoy.txt" > /dev/null 2> "$out2"
318 err="$?"
319
320 checkerrtest "$d" "$err" "invalid file argument" "$out2" "$d"
321
322 "$exe" "$@" "-$opt" -e "$exprs" > /dev/null 2> "$out2"
323 err="$?"
324
325 checkerrtest "$d" "$err" "invalid option argument" "$out2" "$d"
326
327 "$exe" "$@" "--$lopt" -e "$exprs" > /dev/null 2> "$out2"
328 err="$?"
329
330 checkerrtest "$d" "$err" "invalid long option argument" "$out2" "$d"
331
332 "$exe" "$@" "-u" -e "$exprs" > /dev/null 2> "$out2"
333 err="$?"
334
335 checkerrtest "$d" "$err" "unrecognized option argument" "$out2" "$d"
336
337 "$exe" "$@" "--uniform" -e "$exprs" > /dev/null 2> "$out2"
338 err="$?"
339
340 checkerrtest "$d" "$err" "unrecognized long option argument" "$out2" "$d"
341
342 "$exe" "$@" -f > /dev/null 2> "$out2"
343 err="$?"
344
345 checkerrtest "$d" "$err" "missing required argument to short option" "$out2" "$d"
346
347 "$exe" "$@" --file > /dev/null 2> "$out2"
348 err="$?"
349
350 checkerrtest "$d" "$err" "missing required argument to long option" "$out2" "$d"
351
352 "$exe" "$@" --version=5 > /dev/null 2> "$out2"
353 err="$?"
354
355 checkerrtest "$d" "$err" "given argument to long option with no argument" "$out2" "$d"
356
357 "$exe" "$@" -: > /dev/null 2> "$out2"
358 err="$?"
359
360 checkerrtest "$d" "$err" "colon short option" "$out2" "$d"
361
362 "$exe" "$@" --: > /dev/null 2> "$out2"
363 err="$?"
364
365 checkerrtest "$d" "$err" "colon long option" "$out2" "$d"
366
367 printf 'pass\n'
368
369 printf 'Running %s directory test...' "$d"
370
371 "$exe" "$@" "$testdir" > /dev/null 2> "$out2"
372 err="$?"
373
374 checkerrtest "$d" "$err" "directory" "$out2" "$d"
375
376 printf 'pass\n'
377
378 printf 'Running %s binary file test...' "$d"
379
380 bin="/bin/sh"
381
382 "$exe" "$@" "$bin" > /dev/null 2> "$out2"
383 err="$?"
384
385 checkerrtest "$d" "$err" "binary file" "$out2" "$d"
386
387 printf 'pass\n'
388
389 printf 'Running %s binary stdin test...' "$d"
390
391 cat "$bin" | "$exe" "$@" > /dev/null 2> "$out2"
392 err="$?"
393
394 checkerrtest "$d" "$err" "binary stdin" "$out2" "$d"
395
396 printf 'pass\n'
397
398 if [ "$d" = "bc" ]; then
399
400         printf 'Running %s limits tests...' "$d"
401         printf 'limits\n' | "$exe" "$@" > "$out2" /dev/null 2>&1
402
403         checktest_retcode "$d" "$?" "limits"
404
405         if [ ! -s "$out2" ]; then
406                 err_exit "$d did not produce output on the limits test" 1
407         fi
408
409         exec printf 'pass\n'
410
411 fi