blob: ec7f6ba920c903337c1511d5783c34bd817aab71 [file] [log] [blame]
Gavin Howard926d2be2018-03-30 11:52:51 -06001#! /bin/sh
Gavin Howard64f113c2018-09-25 09:38:57 -06002#
Gavin Howard29e00ba2020-06-30 09:25:21 -06003# SPDX-License-Identifier: BSD-2-Clause
4#
Gavin Howard4feb7082021-01-26 01:19:25 -07005# Copyright (c) 2018-2021 Gavin D. Howard and contributors.
Gavin Howard64f113c2018-09-25 09:38:57 -06006#
Gavin Howard7345cb92019-04-08 14:13:43 -06007# 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 Howard64f113c2018-09-25 09:38:57 -060028#
Gavin Howard85d68e32018-02-15 14:28:50 -070029
Gavin Howardd43ae752018-09-06 19:36:18 -060030set -e
31
Gavin Howard164620e2018-02-21 19:00:45 -070032script="$0"
33
34testdir=$(dirname "$script")
35
Gavin Howard781e6eb2021-06-15 11:01:29 -060036. "$testdir/../scripts/functions.sh"
Gavin Howardb0c6bf82021-01-29 13:02:21 -070037
Gavin Howard982249a2021-07-16 23:54:41 -060038# Command-line processing.
Gavin Howard9c4ce072018-10-03 13:50:46 -060039if [ "$#" -lt 2 ]; then
Gavin Howardc1735e62019-05-10 11:23:59 -060040 printf 'usage: %s dir test [generate_tests] [time_tests] [exe [args...]]\n' "$0"
Gavin Howard0a9611b2018-12-31 20:11:39 -070041 printf 'valid dirs are:\n'
42 printf '\n'
Gavin Howardd568b652018-02-22 11:22:21 -070043 cat "$testdir/all.txt"
Gavin Howard0a9611b2018-12-31 20:11:39 -070044 printf '\n'
Gavin Howardd568b652018-02-22 11:22:21 -070045 exit 1
46fi
47
Gavin Howard9c4ce072018-10-03 13:50:46 -060048d="$1"
49shift
50
Gavin Howard5f3edec2018-03-12 15:34:59 -060051t="$1"
Gavin Howard9c4ce072018-10-03 13:50:46 -060052name="$testdir/$d/$t.txt"
53results="$testdir/$d/${t}_results.txt"
Gavin Howard85d68e32018-02-15 14:28:50 -070054shift
55
Gavin Howardd568b652018-02-22 11:22:21 -070056if [ "$#" -gt 0 ]; then
Gavin Howardcc33af62019-01-05 19:54:44 -070057 generate_tests="$1"
58 shift
59else
60 generate_tests=1
61fi
62
63if [ "$#" -gt 0 ]; then
Gavin Howardc1735e62019-05-10 11:23:59 -060064 time_tests="$1"
65 shift
66else
67 time_tests=0
68fi
69
70if [ "$#" -gt 0 ]; then
Gavin Howard9c4ce072018-10-03 13:50:46 -060071 exe="$1"
Gavin Howardd568b652018-02-22 11:22:21 -070072 shift
73else
Gavin Howard2bc50a92018-10-12 16:59:44 -060074 exe="$testdir/../bin/$d"
Gavin Howardd568b652018-02-22 11:22:21 -070075fi
Gavin Howard85d68e32018-02-15 14:28:50 -070076
Gavin Howardb0c6bf82021-01-29 13:02:21 -070077out="$testdir/${d}_outputs/${t}_results.txt"
78outdir=$(dirname "$out")
79
Gavin Howard982249a2021-07-16 23:54:41 -060080# Make sure the directory exists.
Gavin Howardb0c6bf82021-01-29 13:02:21 -070081if [ ! -d "$outdir" ]; then
82 mkdir -p "$outdir"
83fi
84
Gavin Howard982249a2021-07-16 23:54:41 -060085# I use these, so unset them to make the tests work.
Gavin Howardb0c6bf82021-01-29 13:02:21 -070086unset BC_ENV_ARGS
87unset BC_LINE_LENGTH
88unset DC_ENV_ARGS
89unset DC_LINE_LENGTH
Gavin Howard85d68e32018-02-15 14:28:50 -070090
Gavin Howard982249a2021-07-16 23:54:41 -060091# Set stuff for the correct calculator.
Gavin Howard9c4ce072018-10-03 13:50:46 -060092if [ "$d" = "bc" ]; then
93 options="-lq"
Gavin Howard4863d282018-10-25 15:48:23 -060094 var="BC_LINE_LENGTH"
Gavin Howard9c4ce072018-10-03 13:50:46 -060095 halt="halt"
96else
Gavin Howarddcaf2302018-10-17 16:30:38 -060097 options=""
Gavin Howard4863d282018-10-25 15:48:23 -060098 var="DC_LINE_LENGTH"
Gavin Howard9c4ce072018-10-03 13:50:46 -060099 halt="q"
100fi
101
Gavin Howard982249a2021-07-16 23:54:41 -0600102# If the test does not exist...
Gavin Howard31690622018-08-30 09:56:37 -0600103if [ ! -f "$name" ]; then
Gavin Howardcc33af62019-01-05 19:54:44 -0700104
Gavin Howard982249a2021-07-16 23:54:41 -0600105 # Skip if we can't generate.
Gavin Howardcc33af62019-01-05 19:54:44 -0700106 if [ "$generate_tests" -eq 0 ]; then
Gavin Howardcc33af62019-01-05 19:54:44 -0700107 printf 'Skipping %s %s test\n' "$d" "$t"
108 exit 0
109 fi
110
Gavin Howard982249a2021-07-16 23:54:41 -0600111 # Generate.
Gavin Howardc86470e2019-07-31 08:13:34 -0600112 printf 'Generating %s %s...' "$d" "$t"
Gavin Howard86177042021-06-21 21:43:13 -0600113 "$d" "$testdir/$d/scripts/$t.$d" > "$name"
Gavin Howardc86470e2019-07-31 08:13:34 -0600114 printf 'done\n'
Gavin Howard5f3edec2018-03-12 15:34:59 -0600115fi
116
Gavin Howard982249a2021-07-16 23:54:41 -0600117# If the results do not exist, generate..
Gavin Howard31690622018-08-30 09:56:37 -0600118if [ ! -f "$results" ]; then
Gavin Howardc86470e2019-07-31 08:13:34 -0600119 printf 'Generating %s %s results...' "$d" "$t"
Gavin Howard0a9611b2018-12-31 20:11:39 -0700120 printf '%s\n' "$halt" | "$d" $options "$name" > "$results"
Gavin Howardc86470e2019-07-31 08:13:34 -0600121 printf 'done\n'
Gavin Howarddcaf2302018-10-17 16:30:38 -0600122fi
123
Gavin Howard982249a2021-07-16 23:54:41 -0600124# We set this here because GNU dc does not have it.
Gavin Howarddcaf2302018-10-17 16:30:38 -0600125if [ "$d" = "dc" ]; then
126 options="-x"
Gavin Howard31690622018-08-30 09:56:37 -0600127fi
Gavin Howardacbd69b2018-08-29 11:35:25 -0600128
Gavin Howard4863d282018-10-25 15:48:23 -0600129export $var=string
130
Gavin Howardb0c6bf82021-01-29 13:02:21 -0700131set +e
132
Gavin Howardd01a3ff2019-07-14 21:21:07 -0600133printf 'Running %s %s...' "$d" "$t"
Gavin Howard31690622018-08-30 09:56:37 -0600134
Gavin Howardc1735e62019-05-10 11:23:59 -0600135if [ "$time_tests" -ne 0 ]; then
136 printf '\n'
Gavin Howardbf2f5eb2020-05-01 17:45:35 -0600137 printf '%s\n' "$halt" | /usr/bin/time -p "$exe" "$@" $options "$name" > "$out"
Gavin Howardb0c6bf82021-01-29 13:02:21 -0700138 err="$?"
Gavin Howardc1735e62019-05-10 11:23:59 -0600139 printf '\n'
140else
141 printf '%s\n' "$halt" | "$exe" "$@" $options "$name" > "$out"
Gavin Howardb0c6bf82021-01-29 13:02:21 -0700142 err="$?"
Gavin Howardc1735e62019-05-10 11:23:59 -0600143fi
Gavin Howard31690622018-08-30 09:56:37 -0600144
Gavin Howardb0c6bf82021-01-29 13:02:21 -0700145checktest "$d" "$err" "$t" "$results" "$out"
Gavin Howard9aaa3b22018-03-17 21:44:34 -0600146
Gavin Howard7d4736e2019-05-26 08:31:41 -0600147rm -f "$out"
Gavin Howardd01a3ff2019-07-14 21:21:07 -0600148
Gavin Howardb0c6bf82021-01-29 13:02:21 -0700149exec printf 'pass\n'