]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/tests/test.sh
usr.bin/gh-bc, contrib/bc: update to version 5.0.0
[FreeBSD/FreeBSD.git] / contrib / bc / tests / test.sh
1 #! /bin/sh
2 #
3 # SPDX-License-Identifier: BSD-2-Clause
4 #
5 # Copyright (c) 2018-2021 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
30 set -e
31
32 script="$0"
33
34 testdir=$(dirname "$script")
35
36 . "$testdir/../scripts/functions.sh"
37
38 outputdir=${BC_TEST_OUTPUT_DIR:-$testdir}
39
40 # Command-line processing.
41 if [ "$#" -lt 2 ]; then
42         printf 'usage: %s dir test [generate_tests] [time_tests] [exe [args...]]\n' "$0"
43         printf 'valid dirs are:\n'
44         printf '\n'
45         cat "$testdir/all.txt"
46         printf '\n'
47         exit 1
48 fi
49
50 d="$1"
51 shift
52
53 t="$1"
54 name="$testdir/$d/$t.txt"
55 results="$testdir/$d/${t}_results.txt"
56 shift
57
58 if [ "$#" -gt 0 ]; then
59         generate_tests="$1"
60         shift
61 else
62         generate_tests=1
63 fi
64
65 if [ "$#" -gt 0 ]; then
66         time_tests="$1"
67         shift
68 else
69         time_tests=0
70 fi
71
72 if [ "$#" -gt 0 ]; then
73         exe="$1"
74         shift
75 else
76         exe="$testdir/../bin/$d"
77 fi
78
79 out="$outputdir/${d}_outputs/${t}_results.txt"
80 outdir=$(dirname "$out")
81
82 # Make sure the directory exists.
83 if [ ! -d "$outdir" ]; then
84         mkdir -p "$outdir"
85 fi
86
87 # I use these, so unset them to make the tests work.
88 unset BC_ENV_ARGS
89 unset BC_LINE_LENGTH
90 unset DC_ENV_ARGS
91 unset DC_LINE_LENGTH
92
93 # Set stuff for the correct calculator.
94 if [ "$d" = "bc" ]; then
95         options="-lq"
96         var="BC_LINE_LENGTH"
97         halt="halt"
98 else
99         options=""
100         var="DC_LINE_LENGTH"
101         halt="q"
102 fi
103
104 # If the test does not exist...
105 if [ ! -f "$name" ]; then
106
107         # Skip if we can't generate.
108         if [ "$generate_tests" -eq 0 ]; then
109                 printf 'Skipping %s %s test\n' "$d" "$t"
110                 exit 0
111         fi
112
113         # Generate.
114         printf 'Generating %s %s...' "$d" "$t"
115         "$d" "$testdir/$d/scripts/$t.$d" > "$name"
116         printf 'done\n'
117 fi
118
119 # If the results do not exist, generate..
120 if [ ! -f "$results" ]; then
121         printf 'Generating %s %s results...' "$d" "$t"
122         printf '%s\n' "$halt" | "$d" $options "$name" > "$results"
123         printf 'done\n'
124 fi
125
126 # We set this here because GNU dc does not have it.
127 if [ "$d" = "dc" ]; then
128         options="-x"
129 fi
130
131 export $var=string
132
133 set +e
134
135 printf 'Running %s %s...' "$d" "$t"
136
137 if [ "$time_tests" -ne 0 ]; then
138         printf '\n'
139         printf '%s\n' "$halt" | /usr/bin/time -p "$exe" "$@" $options "$name" > "$out"
140         err="$?"
141         printf '\n'
142 else
143         printf '%s\n' "$halt" | "$exe" "$@" $options "$name" > "$out"
144         err="$?"
145 fi
146
147 checktest "$d" "$err" "$t" "$results" "$out"
148
149 rm -f "$out"
150
151 exec printf 'pass\n'