xref: /freebsd/contrib/bc/tests/stdin.sh (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2023 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 [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 1 ]; then
57	usage "Not enough arguments"
58fi
59
60d="$1"
61shift
62check_d_arg "$d"
63
64if [ "$#" -gt 0 ]; then
65	exe="$1"
66	shift
67	check_exec_arg "$exe"
68else
69	exe="$testdir/../bin/$d"
70	check_exec_arg "$exe"
71fi
72
73out="$outputdir/${d}_outputs/stdin_results.txt"
74outdir=$(dirname "$out")
75
76# Make sure the directory exists.
77if [ ! -d "$outdir" ]; then
78	mkdir -p "$outdir"
79fi
80
81# Set stuff for the correct calculator.
82if [ "$d" = "bc" ]; then
83	options="-lq"
84else
85	options="-x"
86fi
87
88rm -f "$out"
89
90# I use these, so unset them to make the tests work.
91unset BC_ENV_ARGS
92unset BC_LINE_LENGTH
93unset DC_ENV_ARGS
94unset DC_LINE_LENGTH
95
96set +e
97
98printf 'Running %s stdin tests...' "$d"
99
100# Run the file through stdin.
101cat "$testdir/$d/stdin.txt" | "$exe" "$@" "$options" > "$out" 2> /dev/null
102checktest "$d" "$?" "stdin" "$testdir/$d/stdin_results.txt" "$out"
103
104# bc has some more tests; run those.
105if [ "$d" = "bc" ]; then
106
107	cat "$testdir/$d/stdin1.txt" | "$exe" "$@" "$options" > "$out" 2> /dev/null
108	checktest "$d" "$?" "stdin1" "$testdir/$d/stdin1_results.txt" "$out"
109
110	cat "$testdir/$d/stdin2.txt" | "$exe" "$@" "$options" > "$out" 2> /dev/null
111	checktest "$d" "$?" "stdin2" "$testdir/$d/stdin2_results.txt" "$out"
112fi
113
114rm -f "$out"
115
116exec printf 'pass\n'
117