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 test [generate_tests] [time_tests] [exe [args...]]\n' "$0" 48 printf 'valid dirs are:\n' 49 printf '\n' 50 cat "$testdir/all.txt" 51 printf '\n' 52 exit 1 53} 54 55# Command-line processing. 56if [ "$#" -lt 2 ]; then 57 usage "Need at least 2 arguments" 58fi 59 60d="$1" 61shift 62check_d_arg "$d" 63 64# We don't use check_file_arg on the test or the result because they might be 65# generated. 66t="$1" 67name="$testdir/$d/$t.txt" 68results="$testdir/$d/${t}_results.txt" 69shift 70 71if [ "$#" -gt 0 ]; then 72 generate_tests="$1" 73 shift 74 check_bool_arg "$generate_tests" 75else 76 generate_tests=1 77 check_bool_arg "$generate_tests" 78fi 79 80if [ "$#" -gt 0 ]; then 81 time_tests="$1" 82 shift 83 check_bool_arg "$time_tests" 84else 85 time_tests=0 86 check_bool_arg "$time_tests" 87fi 88 89if [ "$#" -gt 0 ]; then 90 exe="$1" 91 shift 92 check_exec_arg "$exe" 93else 94 exe="$testdir/../bin/$d" 95 check_exec_arg "$exe" 96fi 97 98out="$outputdir/${d}_outputs/${t}_results.txt" 99outdir=$(dirname "$out") 100 101# Make sure the directory exists. 102if [ ! -d "$outdir" ]; then 103 mkdir -p "$outdir" 104fi 105 106# I use these, so unset them to make the tests work. 107unset BC_ENV_ARGS 108unset BC_LINE_LENGTH 109unset DC_ENV_ARGS 110unset DC_LINE_LENGTH 111 112# Set stuff for the correct calculator. 113if [ "$d" = "bc" ]; then 114 options="-lq" 115 var="BC_LINE_LENGTH" 116 halt="halt" 117else 118 options="" 119 var="DC_LINE_LENGTH" 120 halt="q" 121fi 122 123# If the test does not exist... 124if [ ! -f "$name" ]; then 125 126 # Skip if we can't generate. 127 if [ "$generate_tests" -eq 0 ]; then 128 printf 'Skipping %s %s test\n' "$d" "$t" 129 exit 0 130 fi 131 132 # Generate. 133 printf 'Generating %s %s...' "$d" "$t" 134 "$d" "$testdir/$d/scripts/$t.$d" > "$name" 135 printf 'done\n' 136fi 137 138# If the results do not exist, generate.. 139if [ ! -f "$results" ]; then 140 printf 'Generating %s %s results...' "$d" "$t" 141 printf '%s\n' "$halt" | "$d" $options "$name" > "$results" 142 printf 'done\n' 143fi 144 145# We set this here because GNU bc and dc does not have these options. 146if [ "$d" = "bc" ]; then 147 options="-lqc" 148else 149 options="-xc" 150fi 151 152export $var=string 153 154set +e 155 156printf 'Running %s %s...' "$d" "$t" 157 158if [ "$time_tests" -ne 0 ]; then 159 printf '\n' 160 printf '%s\n' "$halt" | /usr/bin/time -p "$exe" "$@" $options "$name" > "$out" 161 err="$?" 162 printf '\n' 163else 164 printf '%s\n' "$halt" | "$exe" "$@" $options "$name" > "$out" 165 err="$?" 166fi 167 168checktest "$d" "$err" "$t" "$results" "$out" 169 170rm -f "$out" 171 172exec printf 'pass\n' 173