-
-
Notifications
You must be signed in to change notification settings - Fork 417
/
test
executable file
·191 lines (154 loc) · 4.59 KB
/
test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env bash
set -Eeuo pipefail
error() { printf '\033[37;41m%s\033[0m\n' "$*" >&2; }
green() { printf '\033[0;32m%s\033[0m\n' "$*"; }
path() { printf '\033[0;35m%s\033[0m\n' "$*"; }
red() { printf '\033[0;31m%s\033[0m\n' "$*"; }
yellow() { printf '\033[0;33m%s\033[0m\n' "$*"; }
usage() {
cat <<USAGE
Run a test for a specific implementation.
$(yellow Usage:)
test [option]... <solution>...
$(yellow Arguments:)
$(green solution) path(s) to the solution(s) that should be tested
$(yellow Options:)
$(green -h, --help) display this help and exit
$(yellow Examples:)
The following compiles and executes the Rust solution for the problem in the
given path for all inputs in the corresponding data directory:
$(green './test problems/problem/solution.rs')
The following compiles and executes all solutions for the given problem:
$(green './test problems/problem/solution.*')
However, you can also run tests for different problems at once:
$(green './test problems/p1/solution.* problems/p2/solution.*')
USAGE
}
if (($# == 0)); then
error 'Missing required path argument.'
usage >&2
exit 64
fi
for arg in "$@"; do
if [[ "$arg" =~ ^-(h|-help)$ ]]; then
usage
exit 0
fi
done
require-command() {
for cmd in "$@"; do
if ! command -v "$cmd" &>/dev/null; then
error "Could not find $cmd in your PATH, make sure it is available."
exit 69
fi
done
}
failure() {
echo "$(red '✘') $*"
fail=true
}
success() {
echo "$(green '✓') $*"
}
fail=false
for solution in "$@"; do
ext=${solution##*.}
# Sanity check: this might happen if the user used a glob on an unclean
# directory that contains data from a previous run, we just ignore it…
case "$ext" in
exe | iml | log)
continue 2
;;
esac
if [[ ! -f "$solution" ]]; then
error "Path does not exist: $solution"
usage >&2
exit 65
fi
dir=$(dirname "$solution")
# Here we handle all programming languages that require a compilation step
# before we can execute the program.
case "$ext" in
c)
require-command gcc
gcc -pedantic -std=c18 -Wall -Wextra -Werror -o "$solution.exe" "$solution"
cmd=("$solution.exe")
;;
cpp)
require-command g++
g++ -std=c++17 -Wall -Wextra -Werror -o "$solution.exe" "$solution"
cmd=("$solution.exe")
;;
cs)
require-command mcs mono
mcs -langversion:Experimental -optimize+ -out:"$solution.exe" "$solution"
cmd=(mono "$solution.exe")
;;
go)
require-command go
cmd=(go run "$solution")
;;
java)
require-command java
version=$(java --version | head -n1 | cut -d' ' -f2)
if ((${version%%.*} < 11)); then
error "At least Java 11 is required, found $version"
exit 64
fi
cmd=(java "$solution")
;;
rs)
require-command rustc
rustc -o "$solution.exe" "$solution"
cmd=("./$solution.exe")
;;
scala)
require-command scala
cmd=(scala "$solution")
;;
*)
cmd=("./$solution")
if [[ ! -x "${cmd[0]}" ]]; then
error "${cmd[0]} is not executable, did you forget the executable bit?"
exit 69
fi
;;
esac
echo "Running tests for $(path "$solution")"
data=$dir/data
has_data=false
shopt -s nullglob
for in in "$data"/*.in; do
has_data=true
name=$(basename "$in" .in)
out=$solution.$name.out.log
err=$solution.$name.err.log
if ! "${cmd[@]}" "$(cat "$in")" >"$out" 2>"$err"; then
failure "$name (non-zero exit status)"
fi
if [[ "$(cat "$data/$name.out")" == "$(cat "$out")" ]]; then
success "$name"
rm -f "$out" "$err"
else
failure "$name"
fi
done
if [[ $has_data == false ]]; then
error "Not test data in: $data"
continue
fi
if [[ $fail == true ]]; then
nr=$(yellow \$nr)
cat <<HELP
--------------------------------------------------------------------------------
One or more test cases failed.
$(green input) $(path "$data/")$nr$(path .in)
$(green stdout) $(path "$solution.")$nr$(path .out.log)
$(green stderr) $(path "$solution.")$nr$(path .err.log)
$(green expected) $(path "$data/")$nr$(path .out)
Replace $nr with the number of the test case that failed.
HELP
# We abort at this point and do not execute any more tests.
exit 1
fi
done