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