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" 33 34testdir=$(dirname "${script}") 35 36. "$testdir/../scripts/functions.sh" 37 38outputdir=${BC_TEST_OUTPUT_DIR:-$testdir} 39 40# Just print the usage and exit with an error. This can receive a message to 41# print. 42# @param 1 A message to print. 43usage() { 44 if [ $# -eq 1 ]; then 45 printf '%s\n\n' "$1" 46 fi 47 printf 'usage: %s dir script [run_extra_tests] [run_stack_tests] [generate_tests] [time_tests] [exec args...]\n' "$script" 48 exit 1 49} 50 51# Command-line processing. 52if [ "$#" -lt 2 ]; then 53 usage "Not enough arguments; expect 2 arguments" 54fi 55 56d="$1" 57shift 58check_d_arg "$d" 59 60scriptdir="$testdir/$d/scripts" 61 62f="$1" 63shift 64check_file_arg "$scriptdir/$f" 65 66if [ "$#" -gt 0 ]; then 67 run_extra_tests="$1" 68 shift 69 check_bool_arg "$run_extra_tests" 70else 71 run_extra_tests=1 72 check_bool_arg "$run_extra_tests" 73fi 74 75if [ "$#" -gt 0 ]; then 76 run_stack_tests="$1" 77 shift 78 check_bool_arg "$run_stack_tests" 79else 80 run_stack_tests=1 81 check_bool_arg "$run_stack_tests" 82fi 83 84if [ "$#" -gt 0 ]; then 85 generate="$1" 86 shift 87 check_bool_arg "$generate" 88else 89 generate=1 90 check_bool_arg "$generate" 91fi 92 93if [ "$#" -gt 0 ]; then 94 time_tests="$1" 95 shift 96 check_bool_arg "$time_tests" 97else 98 time_tests=0 99 check_bool_arg "$generate" 100fi 101 102if [ "$#" -gt 0 ]; then 103 exe="$1" 104 shift 105 check_exec_arg "$exe" 106else 107 exe="$testdir/../bin/$d" 108fi 109 110# Set stuff for the correct calculator. 111if [ "$d" = "bc" ]; then 112 113 if [ "$run_stack_tests" -ne 0 ]; then 114 options="-lgqC" 115 else 116 options="-lqC" 117 fi 118 119 halt="halt" 120 121else 122 options="-xC" 123 halt="q" 124fi 125 126name="${f%.*}" 127 128# We specifically want to skip this because it is handled specially. 129if [ "$f" = "timeconst.bc" ]; then 130 exit 0 131fi 132 133# Skip the tests that require extra math if we don't have it. 134if [ "$run_extra_tests" -eq 0 ]; then 135 if [ "$f" = "rand.bc" ] || [ "$f" = "root.bc" ]; then 136 printf 'Skipping %s script: %s\n' "$d" "$f" 137 exit 0 138 fi 139fi 140 141# Skip the tests that require global stacks flag if we are not allowed to run 142# them. 143if [ "$run_stack_tests" -eq 0 ]; then 144 145 if [ "$f" = "globals.bc" ] || [ "$f" = "references.bc" ] || [ "$f" = "rand.bc" ]; then 146 printf 'Skipping %s script: %s\n' "$d" "$f" 147 exit 0 148 fi 149 150fi 151 152out="$outputdir/${d}_outputs/${name}_script_results.txt" 153outdir=$(dirname "$out") 154 155# Make sure the directory exists. 156if [ ! -d "$outdir" ]; then 157 mkdir -p "$outdir" 158fi 159 160# I use these, so unset them to make the tests work. 161unset BC_ENV_ARGS 162unset BC_LINE_LENGTH 163unset DC_ENV_ARGS 164unset DC_LINE_LENGTH 165 166s="$scriptdir/$f" 167orig="$testdir/$name.txt" 168results="$scriptdir/$name.txt" 169 170if [ -f "$orig" ]; then 171 res="$orig" 172elif [ -f "$results" ]; then 173 res="$results" 174elif [ "$generate" -eq 0 ]; then 175 printf 'Skipping %s script %s\n' "$d" "$f" 176 exit 0 177else 178 179 set +e 180 181 # This is to check that the command exists. If not, we should not try to 182 # generate the test. Instead, we should just skip. 183 command -v "$d" 1>/dev/null 2>&1 184 err="$?" 185 186 set -e 187 188 if [ "$err" -ne 0 ]; then 189 printf 'Could not find %s to generate results; skipping %s script %s\n' "$d" "$d" "$f" 190 exit 0 191 fi 192 193 printf 'Generating %s results...' "$f" 194 195 # This particular test needs to be generated straight. 196 if [ "$d" = "dc" ] && [ "$f" = "stream.dc" ]; then 197 printf '%s\n' "$halt" 2> /dev/null | "$d" "$s" > "$results" 198 else 199 # This sed, and the script, are to remove an incompatibility with GNU 200 # bc, where GNU bc is wrong. See the development manual 201 # (manuals/development.md#script-tests) for more information. 202 printf '%s\n' "$halt" 2> /dev/null | "$d" "$s" | sed -n -f "$testdir/script.sed" > "$results" 203 fi 204 printf 'done\n' 205 res="$results" 206fi 207 208set +e 209 210printf 'Running %s script %s...' "$d" "$f" 211 212# Yes this is poor timing, but it works. 213if [ "$time_tests" -ne 0 ]; then 214 printf '\n' 215 printf '%s\n' "$halt" 2> /dev/null | /usr/bin/time -p "$exe" "$@" $options "$s" > "$out" 216 err="$?" 217 printf '\n' 218else 219 printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $options "$s" > "$out" 220 err="$?" 221fi 222 223checktest "$d" "$err" "script $f" "$res" "$out" 224 225rm -f "$out" 226 227exec printf 'pass\n' 228