xref: /freebsd/contrib/bc/tests/test.sh (revision df21a004be237a1dccd03c7b47254625eea62fa9)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2025 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"
33
34testdir=$(dirname "$script")
35
36. "$testdir/../scripts/functions.sh"
37
38outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}
39
40# Just print the usage and exit with an error. This can receive a message to
41# print.
42# @param 1  A message to print.
43usage() {
44	if [ $# -eq 1 ]; then
45		printf '%s\n\n' "$1"
46	fi
47	printf 'usage: %s dir test [generate_tests] [exe [args...]]\n' "$0"
48	printf 'valid dirs are:\n'
49	printf '\n'
50	cat "$testdir/all.txt"
51	printf '\n'
52	exit 1
53}
54
55# Command-line processing.
56if [ "$#" -lt 2 ]; then
57	usage "Need at least 2 arguments"
58fi
59
60d="$1"
61shift
62check_d_arg "$d"
63
64# We don't use check_file_arg on the test or the result because they might be
65# generated.
66t="$1"
67name="$testdir/$d/$t.txt"
68results="$testdir/$d/${t}_results.txt"
69shift
70
71if [ "$#" -gt 0 ]; then
72	generate_tests="$1"
73	shift
74	check_bool_arg "$generate_tests"
75else
76	generate_tests=1
77	check_bool_arg "$generate_tests"
78fi
79
80if [ "$#" -gt 0 ]; then
81	exe="$1"
82	shift
83	check_exec_arg "$exe"
84else
85	exe="$testdir/../bin/$d"
86	check_exec_arg "$exe"
87fi
88
89out="$outputdir/${d}_outputs/${t}_results.txt"
90outdir=$(dirname "$out")
91
92# Make sure the directory exists.
93if [ ! -d "$outdir" ]; then
94	mkdir -p "$outdir"
95fi
96
97# I use these, so unset them to make the tests work.
98unset BC_ENV_ARGS
99unset BC_LINE_LENGTH
100unset DC_ENV_ARGS
101unset DC_LINE_LENGTH
102
103# Set stuff for the correct calculator.
104if [ "$d" = "bc" ]; then
105	options="-lq"
106	var="BC_LINE_LENGTH"
107	halt="halt"
108else
109	options=""
110	var="DC_LINE_LENGTH"
111	halt="q"
112fi
113
114# If the test does not exist...
115if [ ! -f "$name" ]; then
116
117	# Skip if we can't generate.
118	if [ "$generate_tests" -eq 0 ]; then
119		printf 'Skipping %s %s test\n' "$d" "$t"
120		exit 0
121	fi
122
123	# Generate.
124	printf 'Generating %s %s...' "$d" "$t"
125	"$d" "$testdir/$d/scripts/$t.$d" > "$name"
126	printf 'done\n'
127fi
128
129# If the results do not exist, generate..
130if [ ! -f "$results" ]; then
131	printf 'Generating %s %s results...' "$d" "$t"
132	printf '%s\n' "$halt" 2> /dev/null | "$d" $options "$name" > "$results"
133	printf 'done\n'
134fi
135
136# We set this here because GNU bc and dc does not have these options.
137if [ "$d" = "bc" ]; then
138	options="-lqc"
139else
140	options="-xc"
141fi
142
143export $var=string
144
145set +e
146
147printf 'Running %s %s...' "$d" "$t"
148
149printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $options "$name" > "$out"
150err="$?"
151
152checktest "$d" "$err" "$t" "$results" "$out"
153
154rm -f "$out"
155
156exec printf 'pass\n'
157