• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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' | head -c3 > /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 | head -c12 > "$easter_out"
160		printf '\n' >> "$easter_out"
161		err="$?"
162
163		checktest "$d" "$err" "Easter script" "$easter_res" "$easter_out"
164
165		printf 'pass\n'
166	fi
167
168fi
169
170out1="$testdir/../.log_$d.txt"
171out2="$testdir/../.log_${d}_test.txt"
172
173printf 'Running %s line length tests...' "$d"
174
175printf '%s\n' "$numres" > "$out1"
176
177export "$line_var"=80
178printf '%s\n' "$num" | "$exe" "$@" > "$out2"
179
180checktest "$d" "$?" "environment var" "$out1" "$out2"
181
182printf '%s\n' "$num70" > "$out1"
183
184export "$line_var"=2147483647
185printf '%s\n' "$num" | "$exe" "$@" > "$out2"
186
187checktest "$d" "$?" "environment var" "$out1" "$out2"
188
189printf 'pass\n'
190
191printf 'Running %s arg tests...' "$d"
192
193f="$testdir/$d/add.txt"
194exprs=$(cat "$f")
195results=$(cat "$testdir/$d/add_results.txt")
196
197printf '%s\n%s\n%s\n%s\n' "$results" "$results" "$results" "$results" > "$out1"
198
199"$exe" "$@" -e "$exprs" -f "$f" --expression "$exprs" --file "$f" -e "$halt" > "$out2"
200
201checktest "$d" "$?" "arg" "$out1" "$out2"
202
203printf '%s\n' "$halt" | "$exe" "$@" -- "$f" "$f" "$f" "$f" > "$out2"
204
205checktest "$d" "$?" "arg" "$out1" "$out2"
206
207if [ "$d" = "bc" ]; then
208	printf '%s\n' "$halt" | "$exe" "$@" -i > /dev/null 2>&1
209fi
210
211printf '%s\n' "$halt" | "$exe" "$@" -h > /dev/null
212checktest_retcode "$d" "$?" "arg"
213printf '%s\n' "$halt" | "$exe" "$@" -P > /dev/null
214checktest_retcode "$d" "$?" "arg"
215printf '%s\n' "$halt" | "$exe" "$@" -v > /dev/null
216checktest_retcode "$d" "$?" "arg"
217printf '%s\n' "$halt" | "$exe" "$@" -V > /dev/null
218checktest_retcode "$d" "$?" "arg"
219
220"$exe" "$@" -f "saotehasotnehasthistohntnsahxstnhalcrgxgrlpyasxtsaosysxsatnhoy.txt" > /dev/null 2> "$out2"
221err="$?"
222
223checkerrtest "$d" "$err" "invalid file argument" "$out2" "$d"
224
225"$exe" "$@" "-$opt" -e "$exprs" > /dev/null 2> "$out2"
226err="$?"
227
228checkerrtest "$d" "$err" "invalid option argument" "$out2" "$d"
229
230"$exe" "$@" "--$lopt" -e "$exprs" > /dev/null 2> "$out2"
231err="$?"
232
233checkerrtest "$d" "$err" "invalid long option argument" "$out2" "$d"
234
235"$exe" "$@" "-u" -e "$exprs" > /dev/null 2> "$out2"
236err="$?"
237
238checkerrtest "$d" "$err" "unrecognized option argument" "$out2" "$d"
239
240"$exe" "$@" "--uniform" -e "$exprs" > /dev/null 2> "$out2"
241err="$?"
242
243checkerrtest "$d" "$err" "unrecognized long option argument" "$out2" "$d"
244
245"$exe" "$@" -f > /dev/null 2> "$out2"
246err="$?"
247
248checkerrtest "$d" "$err" "missing required argument to short option" "$out2" "$d"
249
250"$exe" "$@" --file > /dev/null 2> "$out2"
251err="$?"
252
253checkerrtest "$d" "$err" "missing required argument to long option" "$out2" "$d"
254
255"$exe" "$@" --version=5 > /dev/null 2> "$out2"
256err="$?"
257
258checkerrtest "$d" "$err" "given argument to long option with no argument" "$out2" "$d"
259
260printf 'pass\n'
261
262printf 'Running %s directory test...' "$d"
263
264"$exe" "$@" "$testdir" > /dev/null 2> "$out2"
265err="$?"
266
267checkerrtest "$d" "$err" "directory" "$out2" "$d"
268
269printf 'pass\n'
270
271printf 'Running %s binary file test...' "$d"
272
273bin="/bin/sh"
274
275"$exe" "$@" "$bin" > /dev/null 2> "$out2"
276err="$?"
277
278checkerrtest "$d" "$err" "binary file" "$out2" "$d"
279
280printf 'pass\n'
281
282printf 'Running %s binary stdin test...' "$d"
283
284cat "$bin" | "$exe" "$@" > /dev/null 2> "$out2"
285err="$?"
286
287checkerrtest "$d" "$err" "binary stdin" "$out2" "$d"
288
289printf 'pass\n'
290
291if [ "$d" = "bc" ]; then
292
293	printf 'Running %s limits tests...' "$d"
294	printf 'limits\n' | "$exe" "$@" > "$out2" /dev/null 2>&1
295
296	checktest_retcode "$d" "$?" "limits"
297
298	if [ ! -s "$out2" ]; then
299		err_exit "$d did not produce output on the limits test" 1
300	fi
301
302	exec printf 'pass\n'
303
304fi
305