Gavin Howard | 926d2be | 2018-03-30 11:52:51 -0600 | [diff] [blame] | 1 | #! /bin/sh |
Gavin Howard | 64f113c | 2018-09-25 09:38:57 -0600 | [diff] [blame] | 2 | # |
Gavin Howard | 29e00ba | 2020-06-30 09:25:21 -0600 | [diff] [blame] | 3 | # SPDX-License-Identifier: BSD-2-Clause |
| 4 | # |
Gavin Howard | 4feb708 | 2021-01-26 01:19:25 -0700 | [diff] [blame] | 5 | # Copyright (c) 2018-2021 Gavin D. Howard and contributors. |
Gavin Howard | 64f113c | 2018-09-25 09:38:57 -0600 | [diff] [blame] | 6 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 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. |
Gavin Howard | 64f113c | 2018-09-25 09:38:57 -0600 | [diff] [blame] | 28 | # |
Gavin Howard | 85d68e3 | 2018-02-15 14:28:50 -0700 | [diff] [blame] | 29 | |
Gavin Howard | d43ae75 | 2018-09-06 19:36:18 -0600 | [diff] [blame] | 30 | set -e |
| 31 | |
Gavin Howard | 164620e | 2018-02-21 19:00:45 -0700 | [diff] [blame] | 32 | script="$0" |
| 33 | |
| 34 | testdir=$(dirname "$script") |
| 35 | |
Gavin Howard | 781e6eb | 2021-06-15 11:01:29 -0600 | [diff] [blame] | 36 | . "$testdir/../scripts/functions.sh" |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 37 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 38 | # Command-line processing. |
Gavin Howard | 9c4ce07 | 2018-10-03 13:50:46 -0600 | [diff] [blame] | 39 | if [ "$#" -lt 2 ]; then |
Gavin Howard | c1735e6 | 2019-05-10 11:23:59 -0600 | [diff] [blame] | 40 | printf 'usage: %s dir test [generate_tests] [time_tests] [exe [args...]]\n' "$0" |
Gavin Howard | 0a9611b | 2018-12-31 20:11:39 -0700 | [diff] [blame] | 41 | printf 'valid dirs are:\n' |
| 42 | printf '\n' |
Gavin Howard | d568b65 | 2018-02-22 11:22:21 -0700 | [diff] [blame] | 43 | cat "$testdir/all.txt" |
Gavin Howard | 0a9611b | 2018-12-31 20:11:39 -0700 | [diff] [blame] | 44 | printf '\n' |
Gavin Howard | d568b65 | 2018-02-22 11:22:21 -0700 | [diff] [blame] | 45 | exit 1 |
| 46 | fi |
| 47 | |
Gavin Howard | 9c4ce07 | 2018-10-03 13:50:46 -0600 | [diff] [blame] | 48 | d="$1" |
| 49 | shift |
| 50 | |
Gavin Howard | 5f3edec | 2018-03-12 15:34:59 -0600 | [diff] [blame] | 51 | t="$1" |
Gavin Howard | 9c4ce07 | 2018-10-03 13:50:46 -0600 | [diff] [blame] | 52 | name="$testdir/$d/$t.txt" |
| 53 | results="$testdir/$d/${t}_results.txt" |
Gavin Howard | 85d68e3 | 2018-02-15 14:28:50 -0700 | [diff] [blame] | 54 | shift |
| 55 | |
Gavin Howard | d568b65 | 2018-02-22 11:22:21 -0700 | [diff] [blame] | 56 | if [ "$#" -gt 0 ]; then |
Gavin Howard | cc33af6 | 2019-01-05 19:54:44 -0700 | [diff] [blame] | 57 | generate_tests="$1" |
| 58 | shift |
| 59 | else |
| 60 | generate_tests=1 |
| 61 | fi |
| 62 | |
| 63 | if [ "$#" -gt 0 ]; then |
Gavin Howard | c1735e6 | 2019-05-10 11:23:59 -0600 | [diff] [blame] | 64 | time_tests="$1" |
| 65 | shift |
| 66 | else |
| 67 | time_tests=0 |
| 68 | fi |
| 69 | |
| 70 | if [ "$#" -gt 0 ]; then |
Gavin Howard | 9c4ce07 | 2018-10-03 13:50:46 -0600 | [diff] [blame] | 71 | exe="$1" |
Gavin Howard | d568b65 | 2018-02-22 11:22:21 -0700 | [diff] [blame] | 72 | shift |
| 73 | else |
Gavin Howard | 2bc50a9 | 2018-10-12 16:59:44 -0600 | [diff] [blame] | 74 | exe="$testdir/../bin/$d" |
Gavin Howard | d568b65 | 2018-02-22 11:22:21 -0700 | [diff] [blame] | 75 | fi |
Gavin Howard | 85d68e3 | 2018-02-15 14:28:50 -0700 | [diff] [blame] | 76 | |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 77 | out="$testdir/${d}_outputs/${t}_results.txt" |
| 78 | outdir=$(dirname "$out") |
| 79 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 80 | # Make sure the directory exists. |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 81 | if [ ! -d "$outdir" ]; then |
| 82 | mkdir -p "$outdir" |
| 83 | fi |
| 84 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 85 | # I use these, so unset them to make the tests work. |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 86 | unset BC_ENV_ARGS |
| 87 | unset BC_LINE_LENGTH |
| 88 | unset DC_ENV_ARGS |
| 89 | unset DC_LINE_LENGTH |
Gavin Howard | 85d68e3 | 2018-02-15 14:28:50 -0700 | [diff] [blame] | 90 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 91 | # Set stuff for the correct calculator. |
Gavin Howard | 9c4ce07 | 2018-10-03 13:50:46 -0600 | [diff] [blame] | 92 | if [ "$d" = "bc" ]; then |
| 93 | options="-lq" |
Gavin Howard | 4863d28 | 2018-10-25 15:48:23 -0600 | [diff] [blame] | 94 | var="BC_LINE_LENGTH" |
Gavin Howard | 9c4ce07 | 2018-10-03 13:50:46 -0600 | [diff] [blame] | 95 | halt="halt" |
| 96 | else |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 97 | options="" |
Gavin Howard | 4863d28 | 2018-10-25 15:48:23 -0600 | [diff] [blame] | 98 | var="DC_LINE_LENGTH" |
Gavin Howard | 9c4ce07 | 2018-10-03 13:50:46 -0600 | [diff] [blame] | 99 | halt="q" |
| 100 | fi |
| 101 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 102 | # If the test does not exist... |
Gavin Howard | 3169062 | 2018-08-30 09:56:37 -0600 | [diff] [blame] | 103 | if [ ! -f "$name" ]; then |
Gavin Howard | cc33af6 | 2019-01-05 19:54:44 -0700 | [diff] [blame] | 104 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 105 | # Skip if we can't generate. |
Gavin Howard | cc33af6 | 2019-01-05 19:54:44 -0700 | [diff] [blame] | 106 | if [ "$generate_tests" -eq 0 ]; then |
Gavin Howard | cc33af6 | 2019-01-05 19:54:44 -0700 | [diff] [blame] | 107 | printf 'Skipping %s %s test\n' "$d" "$t" |
| 108 | exit 0 |
| 109 | fi |
| 110 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 111 | # Generate. |
Gavin Howard | c86470e | 2019-07-31 08:13:34 -0600 | [diff] [blame] | 112 | printf 'Generating %s %s...' "$d" "$t" |
Gavin Howard | 8617704 | 2021-06-21 21:43:13 -0600 | [diff] [blame] | 113 | "$d" "$testdir/$d/scripts/$t.$d" > "$name" |
Gavin Howard | c86470e | 2019-07-31 08:13:34 -0600 | [diff] [blame] | 114 | printf 'done\n' |
Gavin Howard | 5f3edec | 2018-03-12 15:34:59 -0600 | [diff] [blame] | 115 | fi |
| 116 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 117 | # If the results do not exist, generate.. |
Gavin Howard | 3169062 | 2018-08-30 09:56:37 -0600 | [diff] [blame] | 118 | if [ ! -f "$results" ]; then |
Gavin Howard | c86470e | 2019-07-31 08:13:34 -0600 | [diff] [blame] | 119 | printf 'Generating %s %s results...' "$d" "$t" |
Gavin Howard | 0a9611b | 2018-12-31 20:11:39 -0700 | [diff] [blame] | 120 | printf '%s\n' "$halt" | "$d" $options "$name" > "$results" |
Gavin Howard | c86470e | 2019-07-31 08:13:34 -0600 | [diff] [blame] | 121 | printf 'done\n' |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 122 | fi |
| 123 | |
Gavin Howard | 982249a | 2021-07-16 23:54:41 -0600 | [diff] [blame] | 124 | # We set this here because GNU dc does not have it. |
Gavin Howard | dcaf230 | 2018-10-17 16:30:38 -0600 | [diff] [blame] | 125 | if [ "$d" = "dc" ]; then |
| 126 | options="-x" |
Gavin Howard | 3169062 | 2018-08-30 09:56:37 -0600 | [diff] [blame] | 127 | fi |
Gavin Howard | acbd69b | 2018-08-29 11:35:25 -0600 | [diff] [blame] | 128 | |
Gavin Howard | 4863d28 | 2018-10-25 15:48:23 -0600 | [diff] [blame] | 129 | export $var=string |
| 130 | |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 131 | set +e |
| 132 | |
Gavin Howard | d01a3ff | 2019-07-14 21:21:07 -0600 | [diff] [blame] | 133 | printf 'Running %s %s...' "$d" "$t" |
Gavin Howard | 3169062 | 2018-08-30 09:56:37 -0600 | [diff] [blame] | 134 | |
Gavin Howard | c1735e6 | 2019-05-10 11:23:59 -0600 | [diff] [blame] | 135 | if [ "$time_tests" -ne 0 ]; then |
| 136 | printf '\n' |
Gavin Howard | bf2f5eb | 2020-05-01 17:45:35 -0600 | [diff] [blame] | 137 | printf '%s\n' "$halt" | /usr/bin/time -p "$exe" "$@" $options "$name" > "$out" |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 138 | err="$?" |
Gavin Howard | c1735e6 | 2019-05-10 11:23:59 -0600 | [diff] [blame] | 139 | printf '\n' |
| 140 | else |
| 141 | printf '%s\n' "$halt" | "$exe" "$@" $options "$name" > "$out" |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 142 | err="$?" |
Gavin Howard | c1735e6 | 2019-05-10 11:23:59 -0600 | [diff] [blame] | 143 | fi |
Gavin Howard | 3169062 | 2018-08-30 09:56:37 -0600 | [diff] [blame] | 144 | |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 145 | checktest "$d" "$err" "$t" "$results" "$out" |
Gavin Howard | 9aaa3b2 | 2018-03-17 21:44:34 -0600 | [diff] [blame] | 146 | |
Gavin Howard | 7d4736e | 2019-05-26 08:31:41 -0600 | [diff] [blame] | 147 | rm -f "$out" |
Gavin Howard | d01a3ff | 2019-07-14 21:21:07 -0600 | [diff] [blame] | 148 | |
Gavin Howard | b0c6bf8 | 2021-01-29 13:02:21 -0700 | [diff] [blame] | 149 | exec printf 'pass\n' |