#! /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 test [generate_tests] [exe [args...]]\n' "$0"47printf 'valid dirs are:\n'48printf '\n'49cat "$testdir/all.txt"50printf '\n'51exit 152}5354# Command-line processing.55if [ "$#" -lt 2 ]; then56usage "Need at least 2 arguments"57fi5859d="$1"60shift61check_d_arg "$d"6263# We don't use check_file_arg on the test or the result because they might be64# generated.65t="$1"66name="$testdir/$d/$t.txt"67results="$testdir/$d/${t}_results.txt"68shift6970if [ "$#" -gt 0 ]; then71generate_tests="$1"72shift73check_bool_arg "$generate_tests"74else75generate_tests=176check_bool_arg "$generate_tests"77fi7879if [ "$#" -gt 0 ]; then80exe="$1"81shift82check_exec_arg "$exe"83else84exe="$testdir/../bin/$d"85check_exec_arg "$exe"86fi8788out="$outputdir/${d}_outputs/${t}_results.txt"89outdir=$(dirname "$out")9091# Make sure the directory exists.92if [ ! -d "$outdir" ]; then93mkdir -p "$outdir"94fi9596# I use these, so unset them to make the tests work.97unset BC_ENV_ARGS98unset BC_LINE_LENGTH99unset DC_ENV_ARGS100unset DC_LINE_LENGTH101102# Set stuff for the correct calculator.103if [ "$d" = "bc" ]; then104options="-lq"105var="BC_LINE_LENGTH"106halt="halt"107else108options=""109var="DC_LINE_LENGTH"110halt="q"111fi112113# If the test does not exist...114if [ ! -f "$name" ]; then115116# Skip if we can't generate.117if [ "$generate_tests" -eq 0 ]; then118printf 'Skipping %s %s test\n' "$d" "$t"119exit 0120fi121122# Generate.123printf 'Generating %s %s...' "$d" "$t"124"$d" "$testdir/$d/scripts/$t.$d" > "$name"125printf 'done\n'126fi127128# If the results do not exist, generate..129if [ ! -f "$results" ]; then130printf 'Generating %s %s results...' "$d" "$t"131printf '%s\n' "$halt" 2> /dev/null | "$d" $options "$name" > "$results"132printf 'done\n'133fi134135# We set this here because GNU bc and dc does not have these options.136if [ "$d" = "bc" ]; then137options="-lqc"138else139options="-xc"140fi141142export $var=string143144set +e145146printf 'Running %s %s...' "$d" "$t"147148printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" $options "$name" > "$out"149err="$?"150151checktest "$d" "$err" "$t" "$results" "$out"152153rm -f "$out"154155exec printf 'pass\n'156157158