1#! /bin/sh 2# 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright (c) 2018-2025 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] [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 exe="$1" 95 shift 96 check_exec_arg "$exe" 97else 98 exe="$testdir/../bin/$d" 99fi 100 101# Set stuff for the correct calculator. 102if [ "$d" = "bc" ]; then 103 104 if [ "$run_stack_tests" -ne 0 ]; then 105 options="-lgqC" 106 else 107 options="-lqC" 108 fi 109 110 halt="halt" 111 112else 113 options="-xC" 114 halt="q" 115fi 116 117name="${f%.*}" 118 119# We specifically want to skip this because it is handled specially. 120if [ "$f" = "timeconst.bc" ]; then 121 exit 0 122fi 123 124# Skip the tests that require extra math if we don't have it. 125if [ "$run_extra_tests" -eq 0 ]; then 126 if [ "$f" = "rand.bc" ] || [ "$f" = "root.bc" ] || [ "$f" = "i2rand.bc" ]; then 127 printf 'Skipping %s script: %s\n' "$d" "$f" 128 exit 0 129 fi 130fi 131 132# Skip the tests that require global stacks flag if we are not allowed to run 133# them. 134if [ "$run_stack_tests" -eq 0 ]; then 135 136 if [ "$f" = "globals.bc" ] || [ "$f" = "references.bc" ] || [ "$f" = "rand.bc" ]; then 137 printf 'Skipping %s script: %s\n' "$d" "$f" 138 exit 0 139 fi 140 141fi 142 143out="$outputdir/${d}_outputs/${name}_script_results.txt" 144outdir=$(dirname "$out") 145 146# Make sure the directory exists. 147if [ ! -d "$outdir" ]; then 148 mkdir -p "$outdir" 149fi 150 151# I use these, so unset them to make the tests work. 152unset BC_ENV_ARGS 153unset BC_LINE_LENGTH 154unset DC_ENV_ARGS 155unset DC_LINE_LENGTH 156 157s="$scriptdir/$f" 158orig="$testdir/$name.txt" 159results="$scriptdir/$name.txt" 160 161if [ -f "$orig" ]; then 162 res="$orig" 163elif [ -f "$results" ]; then 164 res="$results" 165elif [ "$generate" -eq 0 ]; then 166 printf 'Skipping %s script %s\n' "$d" "$f" 167 exit 0 168else 169 170 set +e 171 172 # This is to check that the command exists. If not, we should not try to 173 # generate the test. Instead, we should just skip. 174 command -v "$d" 1>/dev/null 2>&1 175 err="$?" 176 177 set -e 178 179 if [ "$err" -ne 0 ]; then 180 printf 'Could not find %s to generate results; skipping %s script %s\n' "$d" "$d" "$f" 181 exit 0 182 fi 183 184 printf 'Generating %s results...' "$f" 185 186 # This particular test needs to be generated straight. 187 if [ "$d" = "dc" ] && [ "$f" = "stream.dc" ]; then 188 printf '%s\n' "$halt" 2> /dev/null | "$d" "$s" > "$results" 189 else 190 # This sed, and the script, are to remove an incompatibility with GNU 191 # bc, where GNU bc is wrong. See the development manual 192 # (manuals/development.md#script-tests) for more information. 193 printf '%s\n' "$halt" 2> /dev/null | "$d" "$s" | sed -n -f "$testdir/script.sed" > "$results" 194 fi 195 printf 'done\n' 196 res="$results" 197fi 198 199set +e 200 201printf 'Running %s script %s...' "$d" "$f" 202 203printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $options "$s" > "$out" 204err="$?" 205 206checktest "$d" "$err" "script $f" "$res" "$out" 207 208rm -f "$out" 209 210exec printf 'pass\n' 211