#! /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#2829script="$0"3031testdir=$(dirname "$script")3233. "$testdir/../scripts/functions.sh"3435# Just print the usage and exit with an error. This can receive a message to36# print.37# @param 1 A message to print.38usage() {39if [ $# -eq 1 ]; then40printf '%s\n\n' "$1"41fi42printf 'usage: %s dir -a|idx [exe args...]\n' "$script"43exit 144}4546# If Python does not exist, then just skip.47py=$(command -v python3)48err=$?4950if [ "$err" -ne 0 ]; then5152py=$(command -v python)53err=$?5455if [ "$err" -ne 0 ]; then56printf 'Could not find Python 3.\n'57printf 'Skipping %s history tests...\n' "$d"58exit 059fi60fi6162if [ "$#" -lt 2 ]; then63usage "Not enough arguments; expect 2 arguments"64fi6566# d is "bc" or "dc"67d="$1"68shift69check_d_arg "$d"7071# idx is either an index of the test to run or "-a". If it is "-a", then all72# tests are run.73idx="$1"74shift7576if [ "$#" -gt 0 ]; then7778# exe is the executable to run.79exe="$1"80shift81check_exec_arg "$exe"8283else84exe="$testdir/../bin/$d"85check_exec_arg "$exe"86fi8788if [ "$d" = "bc" ]; then89flip="! %s"90addone="%s + 1"91else92flip="%s Np"93addone="%s 1+p"94fi9596# Set the test range correctly for all tests or one test. st is the start index.97if [ "$idx" = "-a" ]; then98idx=$("$py" "$testdir/history.py" "$d" -a)99idx=$(printf '%s - 1\n' "$idx" | bc)100st=0101else102st="$idx"103fi104105# Run all of the tests.106for i in $(seq "$st" "$idx"); do107108printf 'Running %s history test %d...' "$d" "$i"109110for j in $(seq 1 5); do111112"$py" "$testdir/history.py" "$d" "$i" "$exe" "$@"113err="$?"114115if [ "$err" -eq 0 ]; then116break117fi118119done120121checktest_retcode "$d" "$err" "$d history test $i"122123printf 'pass\n'124125done126127128