xref: /freebsd/contrib/bc/tests/history.sh (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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
30script="$0"
31
32testdir=$(dirname "$script")
33
34. "$testdir/../scripts/functions.sh"
35
36# Just print the usage and exit with an error. This can receive a message to
37# print.
38# @param 1  A message to print.
39usage() {
40	if [ $# -eq 1 ]; then
41		printf '%s\n\n' "$1"
42	fi
43	printf 'usage: %s dir -a|idx [exe args...]\n' "$script"
44	exit 1
45}
46
47# If Python does not exist, then just skip.
48py=$(command -v python3)
49err=$?
50
51if [ "$err" -ne 0 ]; then
52
53	py=$(command -v python)
54	err=$?
55
56	if [ "$err" -ne 0 ]; then
57		printf 'Could not find Python 3.\n'
58		printf 'Skipping %s history tests...\n' "$d"
59		exit 0
60	fi
61fi
62
63if [ "$#" -lt 2 ]; then
64	usage "Not enough arguments; expect 2 arguments"
65fi
66
67# d is "bc" or "dc"
68d="$1"
69shift
70check_d_arg "$d"
71
72# idx is either an index of the test to run or "-a". If it is "-a", then all
73# tests are run.
74idx="$1"
75shift
76
77if [ "$#" -gt 0 ]; then
78
79	# exe is the executable to run.
80	exe="$1"
81	shift
82	check_exec_arg "$exe"
83
84else
85	exe="$testdir/../bin/$d"
86	check_exec_arg "$exe"
87fi
88
89if [ "$d" = "bc" ]; then
90	flip="! %s"
91	addone="%s + 1"
92else
93	flip="%s Np"
94	addone="%s 1+p"
95fi
96
97# Set the test range correctly for all tests or one test. st is the start index.
98if [ "$idx" = "-a" ]; then
99	idx=$("$py" "$testdir/history.py" "$d" -a)
100	idx=$(printf '%s - 1\n' "$idx" | bc)
101	st=0
102else
103	st="$idx"
104fi
105
106# Run all of the tests.
107for i in $(seq "$st" "$idx"); do
108
109	printf 'Running %s history test %d...' "$d" "$i"
110
111	for j in $(seq 1 5); do
112
113		"$py" "$testdir/history.py" "$d" "$i" "$exe" "$@"
114		err="$?"
115
116		if [ "$err" -eq 0 ]; then
117			break
118		fi
119
120	done
121
122	checktest_retcode "$d" "$err" "$d history test $i"
123
124	printf 'pass\n'
125
126done
127