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 Workerscript="$0" 31*5a6e8488SAndroid Build Coastguard Workertestdir=$(dirname "$script") 32*5a6e8488SAndroid Build Coastguard Worker 33*5a6e8488SAndroid Build Coastguard Worker. "$testdir/../scripts/functions.sh" 34*5a6e8488SAndroid Build Coastguard Worker 35*5a6e8488SAndroid Build Coastguard Worker# Just print the usage and exit with an error. This can receive a message to 36*5a6e8488SAndroid Build Coastguard Worker# print. 37*5a6e8488SAndroid Build Coastguard Worker# @param 1 A message to print. 38*5a6e8488SAndroid Build Coastguard Workerusage() { 39*5a6e8488SAndroid Build Coastguard Worker if [ $# -eq 1 ]; then 40*5a6e8488SAndroid Build Coastguard Worker printf '%s\n\n' "$1" 41*5a6e8488SAndroid Build Coastguard Worker fi 42*5a6e8488SAndroid Build Coastguard Worker print 'usage: %s [-n] dir [run_extra_tests] [run_stack_tests] [gen_tests] [run_problematic_tests] [time_tests] [exec args...]\n' \ 43*5a6e8488SAndroid Build Coastguard Worker "$script" 44*5a6e8488SAndroid Build Coastguard Worker exit 1 45*5a6e8488SAndroid Build Coastguard Worker} 46*5a6e8488SAndroid Build Coastguard Worker 47*5a6e8488SAndroid Build Coastguard Worker# We need to figure out if we should run stuff in parallel. 48*5a6e8488SAndroid Build Coastguard Workerpll=1 49*5a6e8488SAndroid Build Coastguard Worker 50*5a6e8488SAndroid Build Coastguard Workerwhile getopts "n" opt; do 51*5a6e8488SAndroid Build Coastguard Worker 52*5a6e8488SAndroid Build Coastguard Worker case "$opt" in 53*5a6e8488SAndroid Build Coastguard Worker n) pll=0 ; set -e ;; 54*5a6e8488SAndroid Build Coastguard Worker ?) usage "Invalid option: $opt" ;; 55*5a6e8488SAndroid Build Coastguard Worker esac 56*5a6e8488SAndroid Build Coastguard Worker 57*5a6e8488SAndroid Build Coastguard Workerdone 58*5a6e8488SAndroid Build Coastguard Workershift $(($OPTIND - 1)) 59*5a6e8488SAndroid Build Coastguard Worker 60*5a6e8488SAndroid Build Coastguard Worker# Command-line processing. 61*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -ge 1 ]; then 62*5a6e8488SAndroid Build Coastguard Worker d="$1" 63*5a6e8488SAndroid Build Coastguard Worker shift 64*5a6e8488SAndroid Build Coastguard Worker check_d_arg "$d" 65*5a6e8488SAndroid Build Coastguard Workerelse 66*5a6e8488SAndroid Build Coastguard Worker usage "Not enough arguments" 67*5a6e8488SAndroid Build Coastguard Workerfi 68*5a6e8488SAndroid Build Coastguard Worker 69*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 1 ]; then 70*5a6e8488SAndroid Build Coastguard Worker extra=1 71*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$extra" 72*5a6e8488SAndroid Build Coastguard Workerelse 73*5a6e8488SAndroid Build Coastguard Worker extra="$1" 74*5a6e8488SAndroid Build Coastguard Worker shift 75*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$extra" 76*5a6e8488SAndroid Build Coastguard Workerfi 77*5a6e8488SAndroid Build Coastguard Worker 78*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 1 ]; then 79*5a6e8488SAndroid Build Coastguard Worker run_stack_tests=1 80*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$run_stack_tests" 81*5a6e8488SAndroid Build Coastguard Workerelse 82*5a6e8488SAndroid Build Coastguard Worker run_stack_tests="$1" 83*5a6e8488SAndroid Build Coastguard Worker shift 84*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$run_stack_tests" 85*5a6e8488SAndroid Build Coastguard Workerfi 86*5a6e8488SAndroid Build Coastguard Worker 87*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 1 ]; then 88*5a6e8488SAndroid Build Coastguard Worker generate_tests=1 89*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$generate_tests" 90*5a6e8488SAndroid Build Coastguard Workerelse 91*5a6e8488SAndroid Build Coastguard Worker generate_tests="$1" 92*5a6e8488SAndroid Build Coastguard Worker shift 93*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$generate_tests" 94*5a6e8488SAndroid Build Coastguard Workerfi 95*5a6e8488SAndroid Build Coastguard Worker 96*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 1 ]; then 97*5a6e8488SAndroid Build Coastguard Worker problematic_tests=1 98*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$problematic_tests" 99*5a6e8488SAndroid Build Coastguard Workerelse 100*5a6e8488SAndroid Build Coastguard Worker problematic_tests="$1" 101*5a6e8488SAndroid Build Coastguard Worker shift 102*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$problematic_tests" 103*5a6e8488SAndroid Build Coastguard Workerfi 104*5a6e8488SAndroid Build Coastguard Worker 105*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 1 ]; then 106*5a6e8488SAndroid Build Coastguard Worker time_tests=0 107*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$time_tests" 108*5a6e8488SAndroid Build Coastguard Workerelse 109*5a6e8488SAndroid Build Coastguard Worker time_tests="$1" 110*5a6e8488SAndroid Build Coastguard Worker shift 111*5a6e8488SAndroid Build Coastguard Worker check_bool_arg "$time_tests" 112*5a6e8488SAndroid Build Coastguard Workerfi 113*5a6e8488SAndroid Build Coastguard Worker 114*5a6e8488SAndroid Build Coastguard Workerif [ "$#" -lt 1 ]; then 115*5a6e8488SAndroid Build Coastguard Worker exe="$testdir/../bin/$d" 116*5a6e8488SAndroid Build Coastguard Worker check_exec_arg "$exe" 117*5a6e8488SAndroid Build Coastguard Workerelse 118*5a6e8488SAndroid Build Coastguard Worker exe="$1" 119*5a6e8488SAndroid Build Coastguard Worker shift 120*5a6e8488SAndroid Build Coastguard Worker check_exec_arg "$exe" 121*5a6e8488SAndroid Build Coastguard Workerfi 122*5a6e8488SAndroid Build Coastguard Worker 123*5a6e8488SAndroid Build Coastguard Workerstars="***********************************************************************" 124*5a6e8488SAndroid Build Coastguard Workerprintf '%s\n' "$stars" 125*5a6e8488SAndroid Build Coastguard Worker 126*5a6e8488SAndroid Build Coastguard Worker# Set stuff for the correct calculator. 127*5a6e8488SAndroid Build Coastguard Workerif [ "$d" = "bc" ]; then 128*5a6e8488SAndroid Build Coastguard Worker halt="quit" 129*5a6e8488SAndroid Build Coastguard Workerelse 130*5a6e8488SAndroid Build Coastguard Worker halt="q" 131*5a6e8488SAndroid Build Coastguard Workerfi 132*5a6e8488SAndroid Build Coastguard Worker 133*5a6e8488SAndroid Build Coastguard Worker# I use these, so unset them to make the tests work. 134*5a6e8488SAndroid Build Coastguard Workerunset BC_ENV_ARGS 135*5a6e8488SAndroid Build Coastguard Workerunset BC_LINE_LENGTH 136*5a6e8488SAndroid Build Coastguard Workerunset DC_ENV_ARGS 137*5a6e8488SAndroid Build Coastguard Workerunset DC_LINE_LENGTH 138*5a6e8488SAndroid Build Coastguard Worker 139*5a6e8488SAndroid Build Coastguard Worker# Get the list of tests that require extra math. 140*5a6e8488SAndroid Build Coastguard Workerextra_required=$(cat "$testdir/extra_required.txt") 141*5a6e8488SAndroid Build Coastguard Worker 142*5a6e8488SAndroid Build Coastguard Workerpids="" 143*5a6e8488SAndroid Build Coastguard Worker 144*5a6e8488SAndroid Build Coastguard Workerprintf '\nRunning %s tests...\n\n' "$d" 145*5a6e8488SAndroid Build Coastguard Worker 146*5a6e8488SAndroid Build Coastguard Worker# Run the tests one at a time. 147*5a6e8488SAndroid Build Coastguard Workerwhile read t; do 148*5a6e8488SAndroid Build Coastguard Worker 149*5a6e8488SAndroid Build Coastguard Worker # If it requires extra, then skip if we don't have it. 150*5a6e8488SAndroid Build Coastguard Worker if [ "$extra" -eq 0 ]; then 151*5a6e8488SAndroid Build Coastguard Worker if [ -z "${extra_required##*$t*}" ]; then 152*5a6e8488SAndroid Build Coastguard Worker printf 'Skipping %s %s\n' "$d" "$t" 153*5a6e8488SAndroid Build Coastguard Worker continue 154*5a6e8488SAndroid Build Coastguard Worker fi 155*5a6e8488SAndroid Build Coastguard Worker fi 156*5a6e8488SAndroid Build Coastguard Worker 157*5a6e8488SAndroid Build Coastguard Worker if [ "$pll" -ne 0 ]; then 158*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$time_tests" "$exe" "$@" & 159*5a6e8488SAndroid Build Coastguard Worker pids="$pids $!" 160*5a6e8488SAndroid Build Coastguard Worker else 161*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/test.sh" "$d" "$t" "$generate_tests" "$time_tests" "$exe" "$@" 162*5a6e8488SAndroid Build Coastguard Worker fi 163*5a6e8488SAndroid Build Coastguard Worker 164*5a6e8488SAndroid Build Coastguard Workerdone < "$testdir/$d/all.txt" 165*5a6e8488SAndroid Build Coastguard Worker 166*5a6e8488SAndroid Build Coastguard Worker# stdin tests. 167*5a6e8488SAndroid Build Coastguard Workerif [ "$pll" -ne 0 ]; then 168*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/stdin.sh" "$d" "$exe" "$@" & 169*5a6e8488SAndroid Build Coastguard Worker pids="$pids $!" 170*5a6e8488SAndroid Build Coastguard Workerelse 171*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/stdin.sh" "$d" "$exe" "$@" 172*5a6e8488SAndroid Build Coastguard Workerfi 173*5a6e8488SAndroid Build Coastguard Worker 174*5a6e8488SAndroid Build Coastguard Worker# Script tests. 175*5a6e8488SAndroid Build Coastguard Workerif [ "$pll" -ne 0 ]; then 176*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/scripts.sh" "$d" "$extra" "$run_stack_tests" "$generate_tests" \ 177*5a6e8488SAndroid Build Coastguard Worker "$time_tests" "$exe" "$@" & 178*5a6e8488SAndroid Build Coastguard Worker pids="$pids $!" 179*5a6e8488SAndroid Build Coastguard Workerelse 180*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/scripts.sh" -n "$d" "$extra" "$run_stack_tests" "$generate_tests" \ 181*5a6e8488SAndroid Build Coastguard Worker "$time_tests" "$exe" "$@" 182*5a6e8488SAndroid Build Coastguard Workerfi 183*5a6e8488SAndroid Build Coastguard Worker 184*5a6e8488SAndroid Build Coastguard Worker# Read tests. 185*5a6e8488SAndroid Build Coastguard Workerif [ "$pll" -ne 0 ]; then 186*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/read.sh" "$d" "$exe" "$@" & 187*5a6e8488SAndroid Build Coastguard Worker pids="$pids $!" 188*5a6e8488SAndroid Build Coastguard Workerelse 189*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/read.sh" "$d" "$exe" "$@" 190*5a6e8488SAndroid Build Coastguard Workerfi 191*5a6e8488SAndroid Build Coastguard Worker 192*5a6e8488SAndroid Build Coastguard Worker# Error tests. 193*5a6e8488SAndroid Build Coastguard Workerif [ "$pll" -ne 0 ]; then 194*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/errors.sh" "$d" "$exe" "$@" & 195*5a6e8488SAndroid Build Coastguard Worker pids="$pids $!" 196*5a6e8488SAndroid Build Coastguard Workerelse 197*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/errors.sh" "$d" "$exe" "$@" 198*5a6e8488SAndroid Build Coastguard Workerfi 199*5a6e8488SAndroid Build Coastguard Worker 200*5a6e8488SAndroid Build Coastguard Worker# Test all the files in the errors directory. While the other error test (in 201*5a6e8488SAndroid Build Coastguard Worker# tests/errors.sh) does a test for every line, this does one test per file, but 202*5a6e8488SAndroid Build Coastguard Worker# it runs the file through stdin and as a file on the command-line. 203*5a6e8488SAndroid Build Coastguard Workerfor testfile in $testdir/$d/errors/*.txt; do 204*5a6e8488SAndroid Build Coastguard Worker 205*5a6e8488SAndroid Build Coastguard Worker b=$(basename "$testfile") 206*5a6e8488SAndroid Build Coastguard Worker 207*5a6e8488SAndroid Build Coastguard Worker if [ "$pll" -ne 0 ]; then 208*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/error.sh" "$d" "$b" "$problematic_tests" "$@" & 209*5a6e8488SAndroid Build Coastguard Worker pids="$pids $!" 210*5a6e8488SAndroid Build Coastguard Worker else 211*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/error.sh" "$d" "$b" "$problematic_tests" "$@" 212*5a6e8488SAndroid Build Coastguard Worker fi 213*5a6e8488SAndroid Build Coastguard Worker 214*5a6e8488SAndroid Build Coastguard Workerdone 215*5a6e8488SAndroid Build Coastguard Worker 216*5a6e8488SAndroid Build Coastguard Worker# Other tests. 217*5a6e8488SAndroid Build Coastguard Workerif [ "$pll" -ne 0 ]; then 218*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/other.sh" "$d" "$extra" "$exe" "$@" & 219*5a6e8488SAndroid Build Coastguard Worker pids="$pids $!" 220*5a6e8488SAndroid Build Coastguard Workerelse 221*5a6e8488SAndroid Build Coastguard Worker sh "$testdir/other.sh" "$d" "$extra" "$exe" "$@" 222*5a6e8488SAndroid Build Coastguard Workerfi 223*5a6e8488SAndroid Build Coastguard Worker 224*5a6e8488SAndroid Build Coastguard Workerif [ "$pll" -ne 0 ]; then 225*5a6e8488SAndroid Build Coastguard Worker 226*5a6e8488SAndroid Build Coastguard Worker exit_err=0 227*5a6e8488SAndroid Build Coastguard Worker 228*5a6e8488SAndroid Build Coastguard Worker for p in $pids; do 229*5a6e8488SAndroid Build Coastguard Worker 230*5a6e8488SAndroid Build Coastguard Worker wait "$p" 231*5a6e8488SAndroid Build Coastguard Worker err="$?" 232*5a6e8488SAndroid Build Coastguard Worker 233*5a6e8488SAndroid Build Coastguard Worker if [ "$err" -ne 0 ]; then 234*5a6e8488SAndroid Build Coastguard Worker printf 'A test failed!\n' 235*5a6e8488SAndroid Build Coastguard Worker exit_err=1 236*5a6e8488SAndroid Build Coastguard Worker fi 237*5a6e8488SAndroid Build Coastguard Worker done 238*5a6e8488SAndroid Build Coastguard Worker 239*5a6e8488SAndroid Build Coastguard Worker if [ "$exit_err" -ne 0 ]; then 240*5a6e8488SAndroid Build Coastguard Worker exit 1 241*5a6e8488SAndroid Build Coastguard Worker fi 242*5a6e8488SAndroid Build Coastguard Worker 243*5a6e8488SAndroid Build Coastguard Workerfi 244*5a6e8488SAndroid Build Coastguard Worker 245*5a6e8488SAndroid Build Coastguard Workerprintf '\nAll %s tests passed.\n' "$d" 246*5a6e8488SAndroid Build Coastguard Worker 247*5a6e8488SAndroid Build Coastguard Workerprintf '\n%s\n' "$stars" 248