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