#! /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#2829# WARNING: Test files cannot have empty lines!3031script="$0"32testdir=$(dirname "$script")3334. "$testdir/../scripts/functions.sh"3536outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}3738# Just print the usage and exit with an error. This can receive a message to39# print.40# @param 1 A message to print.41usage() {42if [ $# -eq 1 ]; then43printf '%s\n\n' "$1"44fi45printf 'usage: %s dir [exec args...]\n' "$script"46exit 147}4849# Command-line processing.50if [ "$#" -eq 0 ]; then51usage "Not enough arguments"52else53d="$1"54shift55check_d_arg "$d"56fi5758if [ "$#" -lt 1 ]; then59exe="$testdir/../bin/$d"60check_exec_arg "$exe"61else62exe="$1"63shift64check_exec_arg "$exe"65fi6667# I use these, so unset them to make the tests work.68unset BC_ENV_ARGS69unset BC_LINE_LENGTH70unset DC_ENV_ARGS71unset DC_LINE_LENGTH7273out="$outputdir/${d}_outputs/errors_results.txt"74outdir=$(dirname "$out")7576# Make sure the directory exists.77if [ ! -d "$outdir" ]; then78mkdir -p "$outdir"79fi8081exebase=$(basename "$exe")8283# These are the filenames for the extra tests.84posix="posix_errors"85read_errors="read_errors"8687# Set stuff for the correct calculator.88if [ "$d" = "bc" ]; then89opts="-l"90halt="halt"91else92opts="-x"93halt="q"94fi9596printf 'Running %s command-line error tests...' "$d"9798printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -e "2+2" 2> "$out" > /dev/null99err="$?"100101checkerrtest "$d" "$err" "command-line -e test" "$out" "$exebase"102103printf '%s\n' "$halt" 2> /dev/null | "$exe" "$@" -e "1+1" -f- -f "$testdir/$d/decimal.txt" 2> "$out" > /dev/null104err="$?"105106checkerrtest "$d" "$err" "command-line -f test" "$out" "$exebase"107108printf 'pass\n'109110# Now test the error files in the standard tests directory.111for testfile in $testdir/$d/*errors.txt; do112113if [ -z "${testfile##*$read_errors*}" ]; then114# We don't test read errors here. Skip.115continue116fi117118# Test bc POSIX errors and warnings.119if [ -z "${testfile##*$posix*}" ]; then120121# Just test warnings.122line="last"123printf '%s\n' "$line" 2> /dev/null | "$exe" "$@" "-lw" 2> "$out" > /dev/null124err="$?"125126if [ "$err" -ne 0 ]; then127die "$d" "returned an error ($err)" "POSIX warning" 1128fi129130checkerrtest "$d" "1" "$line" "$out" "$exebase"131132# Set the options for standard mode.133options="-ls"134135else136options="$opts"137fi138139# Output something pretty.140base=$(basename "$testfile")141base="${base%.*}"142printf 'Running %s %s...' "$d" "$base"143144# Test errors on each line of the file. Yes, each line has a separate error145# case.146while read -r line; do147148rm -f "$out"149150printf '%s\n' "$line" 2> /dev/null | "$exe" "$@" "$options" 2> "$out" > /dev/null151err="$?"152153checkerrtest "$d" "$err" "$line" "$out" "$exebase"154155done < "$testfile"156157printf 'pass\n'158159done160161162