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