1*5a6e8488SAndroid Build Coastguard Worker#! /bin/sh 2*5a6e8488SAndroid Build Coastguard Worker# 3*5a6e8488SAndroid Build Coastguard Worker# SPDX-License-Identifier: BSD-2-Clause 4*5a6e8488SAndroid Build Coastguard Worker# 5*5a6e8488SAndroid Build Coastguard Worker# Copyright (c) 2018-2024 Gavin D. Howard and contributors. 6*5a6e8488SAndroid Build Coastguard Worker# 7*5a6e8488SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without 8*5a6e8488SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met: 9*5a6e8488SAndroid Build Coastguard Worker# 10*5a6e8488SAndroid Build Coastguard Worker# * Redistributions of source code must retain the above copyright notice, this 11*5a6e8488SAndroid Build Coastguard Worker# list of conditions and the following disclaimer. 12*5a6e8488SAndroid Build Coastguard Worker# 13*5a6e8488SAndroid Build Coastguard Worker# * Redistributions in binary form must reproduce the above copyright notice, 14*5a6e8488SAndroid Build Coastguard Worker# this list of conditions and the following disclaimer in the documentation 15*5a6e8488SAndroid Build Coastguard Worker# and/or other materials provided with the distribution. 16*5a6e8488SAndroid Build Coastguard Worker# 17*5a6e8488SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18*5a6e8488SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*5a6e8488SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*5a6e8488SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21*5a6e8488SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*5a6e8488SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*5a6e8488SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*5a6e8488SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25*5a6e8488SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26*5a6e8488SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27*5a6e8488SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE. 28*5a6e8488SAndroid Build Coastguard Worker# 29*5a6e8488SAndroid Build Coastguard Worker 30*5a6e8488SAndroid Build Coastguard Worker# WARNING: Test files cannot have empty lines! 31*5a6e8488SAndroid Build Coastguard Worker 32*5a6e8488SAndroid Build Coastguard Workerscript="$0" 33*5a6e8488SAndroid Build Coastguard Workertestdir=$(dirname "$script") 34*5a6e8488SAndroid Build Coastguard Worker 35*5a6e8488SAndroid Build Coastguard Worker. "$testdir/../scripts/functions.sh" 36*5a6e8488SAndroid Build Coastguard Worker 37*5a6e8488SAndroid Build Coastguard Workeroutputdir=${BC_TEST_OUTPUT_DIR:-$testdir} 38*5a6e8488SAndroid Build Coastguard Worker 39*5a6e8488SAndroid Build Coastguard Worker# Just print the usage and exit with an error. This can receive a message to 40*5a6e8488SAndroid Build Coastguard Worker# print. 41*5a6e8488SAndroid Build Coastguard Worker# @param 1 A message to print. 42*5a6e8488SAndroid Build Coastguard Workerusage() { 43*5a6e8488SAndroid Build Coastguard Worker if [ $# -eq 1 ]; then 44*5a6e8488SAndroid Build Coastguard Worker printf '%s\n\n' "$1" 45*5a6e8488SAndroid Build Coastguard Worker fi 46*5a6e8488SAndroid Build Coastguard Worker printf 'usage: %s dir [exec args...]\n' "$script" 47*5a6e8488SAndroid Build Coastguard Worker exit 1 48*5a6e8488SAndroid Build Coastguard Worker} 49*5a6e8488SAndroid Build Coastguard Worker 50*5a6e8488SAndroid Build Coastguard Worker# Command-line processing. 51*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -eq 0 ]; then 52*5a6e8488SAndroid Build Coastguard Worker usage "Not enough arguments" 53*5a6e8488SAndroid Build Coastguard Workerelse 54*5a6e8488SAndroid Build Coastguard Worker d="$1" 55*5a6e8488SAndroid Build Coastguard Worker shift 56*5a6e8488SAndroid Build Coastguard Worker check_d_arg "$d" 57*5a6e8488SAndroid Build Coastguard Workerfi 58*5a6e8488SAndroid Build Coastguard Worker 59*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 1 ]; then 60*5a6e8488SAndroid Build Coastguard Worker exe="$testdir/../bin/$d" 61*5a6e8488SAndroid Build Coastguard Worker check_exec_arg "$exe" 62*5a6e8488SAndroid Build Coastguard Workerelse 63*5a6e8488SAndroid Build Coastguard Worker exe="$1" 64*5a6e8488SAndroid Build Coastguard Worker shift 65*5a6e8488SAndroid Build Coastguard Worker check_exec_arg "$exe" 66*5a6e8488SAndroid Build Coastguard Workerfi 67*5a6e8488SAndroid Build Coastguard Worker 68*5a6e8488SAndroid Build Coastguard Worker# I use these, so unset them to make the tests work. 69*5a6e8488SAndroid Build Coastguard Workerunset BC_ENV_ARGS 70*5a6e8488SAndroid Build Coastguard Workerunset BC_LINE_LENGTH 71*5a6e8488SAndroid Build Coastguard Workerunset DC_ENV_ARGS 72*5a6e8488SAndroid Build Coastguard Workerunset DC_LINE_LENGTH 73*5a6e8488SAndroid Build Coastguard Worker 74*5a6e8488SAndroid Build Coastguard Workerout="$outputdir/${d}_outputs/errors_results.txt" 75*5a6e8488SAndroid Build Coastguard Workeroutdir=$(dirname "$out") 76*5a6e8488SAndroid Build Coastguard Worker 77*5a6e8488SAndroid Build Coastguard Worker# Make sure the directory exists. 78*5a6e8488SAndroid Build Coastguard Workerif [ ! -d "$outdir" ]; then 79*5a6e8488SAndroid Build Coastguard Worker mkdir -p "$outdir" 80*5a6e8488SAndroid Build Coastguard Workerfi 81*5a6e8488SAndroid Build Coastguard Worker 82*5a6e8488SAndroid Build Coastguard Workerexebase=$(basename "$exe") 83*5a6e8488SAndroid Build Coastguard Worker 84*5a6e8488SAndroid Build Coastguard Worker# These are the filenames for the extra tests. 85*5a6e8488SAndroid Build Coastguard Workerposix="posix_errors" 86*5a6e8488SAndroid Build Coastguard Workerread_errors="read_errors" 87*5a6e8488SAndroid Build Coastguard Worker 88*5a6e8488SAndroid Build Coastguard Worker# Set stuff for the correct calculator. 89*5a6e8488SAndroid Build Coastguard Workerif [ "$d" = "bc" ]; then 90*5a6e8488SAndroid Build Coastguard Worker opts="-l" 91*5a6e8488SAndroid Build Coastguard Worker halt="halt" 92*5a6e8488SAndroid Build Coastguard Worker read_call="read()" 93*5a6e8488SAndroid Build Coastguard Worker read_expr="${read_call}\n5+5;" 94*5a6e8488SAndroid Build Coastguard Workerelse 95*5a6e8488SAndroid Build Coastguard Worker opts="-x" 96*5a6e8488SAndroid Build Coastguard Worker halt="q" 97*5a6e8488SAndroid Build Coastguard Workerfi 98*5a6e8488SAndroid Build Coastguard Worker 99*5a6e8488SAndroid Build Coastguard Workerprintf 'Running %s command-line error tests...' "$d" 100*5a6e8488SAndroid Build Coastguard Worker 101*5a6e8488SAndroid Build Coastguard Workerprintf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -e "2+2" 2> "$out" > /dev/null 102*5a6e8488SAndroid Build Coastguard Workererr="$?" 103*5a6e8488SAndroid Build Coastguard Worker 104*5a6e8488SAndroid Build Coastguard Workercheckerrtest "$d" "$err" "command-line -e test" "$out" "$exebase" 105*5a6e8488SAndroid Build Coastguard Worker 106*5a6e8488SAndroid Build Coastguard Workerprintf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -f "$testdir/$d/decimal.txt" 2> "$out" > /dev/null 107*5a6e8488SAndroid Build Coastguard Workererr="$?" 108*5a6e8488SAndroid Build Coastguard Worker 109*5a6e8488SAndroid Build Coastguard Workercheckerrtest "$d" "$err" "command-line -f test" "$out" "$exebase" 110*5a6e8488SAndroid Build Coastguard Worker 111*5a6e8488SAndroid Build Coastguard Workerprintf 'pass\n' 112*5a6e8488SAndroid Build Coastguard Worker 113*5a6e8488SAndroid Build Coastguard Worker# Now test the error files in the standard tests directory. 114*5a6e8488SAndroid Build Coastguard Workerfor testfile in $testdir/$d/*errors.txt; do 115*5a6e8488SAndroid Build Coastguard Worker 116*5a6e8488SAndroid Build Coastguard Worker if [ -z "${testfile##*$read_errors*}" ]; then 117*5a6e8488SAndroid Build Coastguard Worker # We don't test read errors here. Skip. 118*5a6e8488SAndroid Build Coastguard Worker continue 119*5a6e8488SAndroid Build Coastguard Worker fi 120*5a6e8488SAndroid Build Coastguard Worker 121*5a6e8488SAndroid Build Coastguard Worker # Test bc POSIX errors and warnings. 122*5a6e8488SAndroid Build Coastguard Worker if [ -z "${testfile##*$posix*}" ]; then 123*5a6e8488SAndroid Build Coastguard Worker 124*5a6e8488SAndroid Build Coastguard Worker # Just test warnings. 125*5a6e8488SAndroid Build Coastguard Worker line="last" 126*5a6e8488SAndroid Build Coastguard Worker printf '%s\n' "$line" 2> /dev/null | "$exe" "$@" "-lw" 2> "$out" > /dev/null 127*5a6e8488SAndroid Build Coastguard Worker err="$?" 128*5a6e8488SAndroid Build Coastguard Worker 129*5a6e8488SAndroid Build Coastguard Worker if [ "$err" -ne 0 ]; then 130*5a6e8488SAndroid Build Coastguard Worker die "$d" "returned an error ($err)" "POSIX warning" 1 131*5a6e8488SAndroid Build Coastguard Worker fi 132*5a6e8488SAndroid Build Coastguard Worker 133*5a6e8488SAndroid Build Coastguard Worker checkerrtest "$d" "1" "$line" "$out" "$exebase" 134*5a6e8488SAndroid Build Coastguard Worker 135*5a6e8488SAndroid Build Coastguard Worker # Set the options for standard mode. 136*5a6e8488SAndroid Build Coastguard Worker options="-ls" 137*5a6e8488SAndroid Build Coastguard Worker 138*5a6e8488SAndroid Build Coastguard Worker else 139*5a6e8488SAndroid Build Coastguard Worker options="$opts" 140*5a6e8488SAndroid Build Coastguard Worker fi 141*5a6e8488SAndroid Build Coastguard Worker 142*5a6e8488SAndroid Build Coastguard Worker # Output something pretty. 143*5a6e8488SAndroid Build Coastguard Worker base=$(basename "$testfile") 144*5a6e8488SAndroid Build Coastguard Worker base="${base%.*}" 145*5a6e8488SAndroid Build Coastguard Worker printf 'Running %s %s...' "$d" "$base" 146*5a6e8488SAndroid Build Coastguard Worker 147*5a6e8488SAndroid Build Coastguard Worker # Test errors on each line of the file. Yes, each line has a separate error 148*5a6e8488SAndroid Build Coastguard Worker # case. 149*5a6e8488SAndroid Build Coastguard Worker while read -r line; do 150*5a6e8488SAndroid Build Coastguard Worker 151*5a6e8488SAndroid Build Coastguard Worker rm -f "$out" 152*5a6e8488SAndroid Build Coastguard Worker 153*5a6e8488SAndroid Build Coastguard Worker printf '%s\n' "$line" 2> /dev/null | "$exe" "$@" "$options" 2> "$out" > /dev/null 154*5a6e8488SAndroid Build Coastguard Worker err="$?" 155*5a6e8488SAndroid Build Coastguard Worker 156*5a6e8488SAndroid Build Coastguard Worker checkerrtest "$d" "$err" "$line" "$out" "$exebase" 157*5a6e8488SAndroid Build Coastguard Worker 158*5a6e8488SAndroid Build Coastguard Worker done < "$testfile" 159*5a6e8488SAndroid Build Coastguard Worker 160*5a6e8488SAndroid Build Coastguard Worker printf 'pass\n' 161*5a6e8488SAndroid Build Coastguard Worker 162*5a6e8488SAndroid Build Coastguard Workerdone 163