xref: /freebsd/contrib/bc/tests/read.sh (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
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/../scripts/functions.sh"
36
37# Command-line processing.
38if [ "$#" -lt 1 ]; then
39	printf 'usage: %s dir [exe [args...]]\n' "$0"
40	printf 'valid dirs are:\n'
41	printf '\n'
42	cat "$testdir/all.txt"
43	printf '\n'
44	exit 1
45fi
46
47d="$1"
48shift
49
50if [ "$#" -gt 0 ]; then
51	exe="$1"
52	shift
53else
54	exe="$testdir/../bin/$d"
55fi
56
57name="$testdir/$d/read.txt"
58results="$testdir/$d/read_results.txt"
59errors="$testdir/$d/read_errors.txt"
60
61out="$testdir/${d}_outputs/read_results.txt"
62outdir=$(dirname "$out")
63
64# Make sure the directory exists.
65if [ ! -d "$outdir" ]; then
66	mkdir -p "$outdir"
67fi
68
69exebase=$(basename "$exe")
70
71# Set stuff for the correct calculator.
72if [ "$d" = "bc" ]; then
73	options="-lq"
74	halt="halt"
75	read_call="read()"
76	read_expr="${read_call}\n5+5;"
77else
78	options="-x"
79	halt="q"
80	read_call="?"
81	read_expr="${read_call}"
82fi
83
84# I use these, so unset them to make the tests work.
85unset BC_ENV_ARGS
86unset BC_LINE_LENGTH
87unset DC_ENV_ARGS
88unset DC_LINE_LENGTH
89
90printf 'Running %s read...' "$d"
91
92set +e
93
94# Run read() on every line.
95while read line; do
96
97	printf '%s\n%s\n' "$read_call" "$line" | "$exe" "$@" "$options" > "$out"
98	checktest "$d" "$?" 'read' "$results" "$out"
99
100done < "$name"
101
102printf 'pass\n'
103
104printf 'Running %s read errors...' "$d"
105
106# Run read on every line.
107while read line; do
108
109	printf '%s\n%s\n' "$read_call" "$line" | "$exe" "$@" "$options" 2> "$out" > /dev/null
110	err="$?"
111
112	checkerrtest "$d" "$err" "$line" "$out" "$exebase"
113
114done < "$errors"
115
116printf 'pass\n'
117
118printf 'Running %s empty read...' "$d"
119
120read_test=$(printf '%s\n' "$read_call")
121
122printf '%s\n' "$read_test" | "$exe" "$@" "$opts" 2> "$out" > /dev/null
123err="$?"
124
125checkerrtest "$d" "$err" "$read_test" "$out" "$exebase"
126
127printf 'pass\n'
128
129printf 'Running %s read EOF...' "$d"
130
131read_test=$(printf '%s' "$read_call")
132
133printf '%s' "$read_test" | "$exe" "$@" "$opts" 2> "$out" > /dev/null
134err="$?"
135
136checkerrtest "$d" "$err" "$read_test" "$out" "$exebase"
137
138exec printf 'pass\n'
139