#! /bin/sh1#2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2018-2025 Gavin D. Howard and contributors.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions are met:8#9# * Redistributions of source code must retain the above copyright notice, this10# list of conditions and the following disclaimer.11#12# * Redistributions in binary form must reproduce the above copyright notice,13# this list of conditions and the following disclaimer in the documentation14# and/or other materials provided with the distribution.15#16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE26# POSSIBILITY OF SUCH DAMAGE.27#2829set -e3031script="$0"3233testdir=$(dirname "${script}")3435. "$testdir/../scripts/functions.sh"3637outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}3839# Just print the usage and exit with an error. This can receive a message to40# print.41# @param 1 A message to print.42usage() {43if [ $# -eq 1 ]; then44printf '%s\n\n' "$1"45fi46printf 'usage: %s dir script [run_extra_tests] [run_stack_tests] [generate_tests] [exec args...]\n' "$script"47exit 148}4950# Command-line processing.51if [ "$#" -lt 2 ]; then52usage "Not enough arguments; expect 2 arguments"53fi5455d="$1"56shift57check_d_arg "$d"5859scriptdir="$testdir/$d/scripts"6061f="$1"62shift63check_file_arg "$scriptdir/$f"6465if [ "$#" -gt 0 ]; then66run_extra_tests="$1"67shift68check_bool_arg "$run_extra_tests"69else70run_extra_tests=171check_bool_arg "$run_extra_tests"72fi7374if [ "$#" -gt 0 ]; then75run_stack_tests="$1"76shift77check_bool_arg "$run_stack_tests"78else79run_stack_tests=180check_bool_arg "$run_stack_tests"81fi8283if [ "$#" -gt 0 ]; then84generate="$1"85shift86check_bool_arg "$generate"87else88generate=189check_bool_arg "$generate"90fi9192if [ "$#" -gt 0 ]; then93exe="$1"94shift95check_exec_arg "$exe"96else97exe="$testdir/../bin/$d"98fi99100# Set stuff for the correct calculator.101if [ "$d" = "bc" ]; then102103if [ "$run_stack_tests" -ne 0 ]; then104options="-lgqC"105else106options="-lqC"107fi108109halt="halt"110111else112options="-xC"113halt="q"114fi115116name="${f%.*}"117118# We specifically want to skip this because it is handled specially.119if [ "$f" = "timeconst.bc" ]; then120exit 0121fi122123# Skip the tests that require extra math if we don't have it.124if [ "$run_extra_tests" -eq 0 ]; then125if [ "$f" = "rand.bc" ] || [ "$f" = "root.bc" ] || [ "$f" = "i2rand.bc" ]; then126printf 'Skipping %s script: %s\n' "$d" "$f"127exit 0128fi129fi130131# Skip the tests that require global stacks flag if we are not allowed to run132# them.133if [ "$run_stack_tests" -eq 0 ]; then134135if [ "$f" = "globals.bc" ] || [ "$f" = "references.bc" ] || [ "$f" = "rand.bc" ]; then136printf 'Skipping %s script: %s\n' "$d" "$f"137exit 0138fi139140fi141142out="$outputdir/${d}_outputs/${name}_script_results.txt"143outdir=$(dirname "$out")144145# Make sure the directory exists.146if [ ! -d "$outdir" ]; then147mkdir -p "$outdir"148fi149150# I use these, so unset them to make the tests work.151unset BC_ENV_ARGS152unset BC_LINE_LENGTH153unset DC_ENV_ARGS154unset DC_LINE_LENGTH155156s="$scriptdir/$f"157orig="$testdir/$name.txt"158results="$scriptdir/$name.txt"159160if [ -f "$orig" ]; then161res="$orig"162elif [ -f "$results" ]; then163res="$results"164elif [ "$generate" -eq 0 ]; then165printf 'Skipping %s script %s\n' "$d" "$f"166exit 0167else168169set +e170171# This is to check that the command exists. If not, we should not try to172# generate the test. Instead, we should just skip.173command -v "$d" 1>/dev/null 2>&1174err="$?"175176set -e177178if [ "$err" -ne 0 ]; then179printf 'Could not find %s to generate results; skipping %s script %s\n' "$d" "$d" "$f"180exit 0181fi182183printf 'Generating %s results...' "$f"184185# This particular test needs to be generated straight.186if [ "$d" = "dc" ] && [ "$f" = "stream.dc" ]; then187printf '%s\n' "$halt" 2> /dev/null | "$d" "$s" > "$results"188else189# This sed, and the script, are to remove an incompatibility with GNU190# bc, where GNU bc is wrong. See the development manual191# (manuals/development.md#script-tests) for more information.192printf '%s\n' "$halt" 2> /dev/null | "$d" "$s" | sed -n -f "$testdir/script.sed" > "$results"193fi194printf 'done\n'195res="$results"196fi197198set +e199200printf 'Running %s script %s...' "$d" "$f"201202printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $options "$s" > "$out"203err="$?"204205checktest "$d" "$err" "script $f" "$res" "$out"206207rm -f "$out"208209exec printf 'pass\n'210211212