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