1#! /bin/sh 2# 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright (c) 2018-2024 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" 77multiple_res="$outputdir/${d}_outputs/read_multiple_results.txt" 78outdir=$(dirname "$out") 79 80# Make sure the directory exists. 81if [ ! -d "$outdir" ]; then 82 mkdir -p "$outdir" 83fi 84 85exebase=$(basename "$exe") 86 87# Set stuff for the correct calculator. 88if [ "$d" = "bc" ]; then 89 options="-lq" 90 halt="halt" 91 read_call="read()" 92 read_expr="${read_call}\n5+5;" 93 read_multiple=$(printf '%s\n%s\n%s\n' "3" "2" "1") 94else 95 options="-x" 96 halt="q" 97 read_call="?" 98 read_expr="${read_call}" 99 read_multiple=$(printf '%spR\n%spR\n%spR\n' "3" "2" "1") 100fi 101 102# I use these, so unset them to make the tests work. 103unset BC_ENV_ARGS 104unset BC_LINE_LENGTH 105unset DC_ENV_ARGS 106unset DC_LINE_LENGTH 107 108printf 'Running %s read...' "$d" 109 110set +e 111 112# Run read() on every line. 113while read line; do 114 115 printf '%s\n%s\n' "$read_call" "$line" | "$exe" "$@" "$options" > "$out" 116 checktest "$d" "$?" 'read' "$results" "$out" 117 118done < "$name" 119 120printf 'pass\n' 121 122printf 'Running %s read multiple...' "$d" 123 124printf '3\n2\n1\n' > "$multiple_res" 125 126# Run multiple read() calls. 127printf '%s\n' "$read_multiple" | "$exe" "$@" "$options" -e "$read_call" -e "$read_call" -e "$read_call" > "$out" 128checktest "$d" "$?" 'read multiple' "$multiple_res" "$out" 129 130printf 'pass\n' 131 132printf 'Running %s read errors...' "$d" 133 134# Run read on every line. 135while read line; do 136 137 printf '%s\n%s\n' "$read_call" "$line" | "$exe" "$@" "$options" 2> "$out" > /dev/null 138 err="$?" 139 140 checkerrtest "$d" "$err" "$line" "$out" "$exebase" 141 142done < "$errors" 143 144printf 'pass\n' 145 146printf 'Running %s empty read...' "$d" 147 148read_test=$(printf '%s\n' "$read_call") 149 150printf '%s\n' "$read_test" | "$exe" "$@" "$opts" 2> "$out" > /dev/null 151err="$?" 152 153checkerrtest "$d" "$err" "$read_test" "$out" "$exebase" 154 155printf 'pass\n' 156 157printf 'Running %s read EOF...' "$d" 158 159read_test=$(printf '%s' "$read_call") 160 161printf '%s' "$read_test" | "$exe" "$@" "$opts" 2> "$out" > /dev/null 162err="$?" 163 164checkerrtest "$d" "$err" "$read_test" "$out" "$exebase" 165 166exec printf 'pass\n' 167