-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathruntest.sh
executable file
·226 lines (193 loc) · 5.86 KB
/
runtest.sh
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
#set -x
#echo $* >> runtest-cmdline.txt
# program parameters
rootdir=$(dirname "$0")
contest=master
quiet=
number= # testcase number (default 0)
testsdir= # path for testcases files (<tests>/<contest>/<challenges>/input/...)
extract_tests=
# program usage
usage()
{
echo "Usage: runtest.sh [options]"
echo " -h,--help : help"
echo " -t,--testcase : testcase name"
echo " -n,--number : testcase number"
exit $1
}
# colors for term
set_colors()
{
if [ -t 1 -a -t 0 ]; then
COLOR_RED="\033[91m"
COLOR_GREEN="\033[92m"
COLOR_YELLOW="\033[93m"
COLOR_LIGHT_PURPLE="\033[94m"
COLOR_PURPLE="\033[95m"
COLOR_CYAN="\033[0;36m"
COLOR_END="\033[0m"
else
COLOR_RED=
COLOR_GREEN=
COLOR_YELLOW=
COLOR_LIGHT_PURPLE=
COLOR_PURPLE=
COLOR_END=
fi
}
# compare command
compare()
{
python3 "${rootdir}/compare.py" "$1" "$2"
}
close_std()
{
if [ $CTEST_INTERACTIVE_DEBUG_MODE ]; then
# close stdout and stderr
exec 1<&-
exec 2<&-
# open stdout as a file for read and write.
exec 1<>$testname.log
# redirect stderr to stdout
exec 2>&1
fi
}
##############################################################################
# read the options
if [ "$(uname)" = "Darwin" ]; then
ARGS=`getopt hqc:t:n:T:X: $*`
else
ARGS=`getopt -o hqc:t:n:T:X: --long help,quiet,contest:,test:,number: -n 'runtest.sh' -- "$@"`
fi
eval set -- "$ARGS"
[ $? != 0 ] && usage 2
# extract options and their arguments into variables.
for i ; do
case "$i" in
-h|--help) usage ;;
-q|--quiet) quiet=1 ; shift ;;
-c|--contest) contest=$2 ; shift 2 ;;
-t|--test) testname=$2 ; shift 2 ;;
-n|--number) number=$2 ; shift 2 ;;
-T) testsdir=$2 ; shift 2 ;;
-X) extract_tests=$2 ; shift 2;;
--) shift ; break ;;
esac
done
if [ $extract_tests ]; then
if [ -s "${rootdir}/testcases.tar.xz" ]; then
mkdir -p "${extract_tests}"
tar -C "${extract_tests}" -xJf "${rootdir}/testcases.tar.xz"
exit $?
fi
exit 1
fi
# alternate syntax
if [ -z "${testname}" ]; then
testname=$1
[ -z "${testname}" ] && usage 3
fi
# testcase file must exist
[ -f "${testname}" ] || usage 4
# for batch/interactive processing
set_colors
close_std
# extract the extension
extension="${testname##*.}"
if [ "${extension}" == "py" ]; then
exe="python3 ${testname}"
testname="${testname%.*}"
result=result${extension}
elif [ "${extension}" == "sh" ]; then
exe="bash ${testname}"
testname="${testname%.*}"
result=result${extension}
elif [ "${extension}" == "jar" ]; then
exe="java -jar ${testname}"
testname="${testname%.*}"
result=result${extension}
elif [ "${extension}" == "js" ]; then
exe="node ${testname}"
testname="${testname%.*}"
result=result${extension}
else
exe=./${testname}
result=result-${testname}
# special case for duplicated challenges (practice/contest)
testname=${testname%_*}
fi
##############################################################################
# trois considérations:
# - le répertoire <tests>/<testname>/input/ existe
# - le fichier <rootdir>/testcases/<contest>/<testname>-testcases.zip
# - le fichier <rootdir>/testcases2/<contest>/<testname>-testcases.zip
if [ "${testsdir}" != "" -a -d "${testsdir}/${contest}/${testname}" ]; then
testsdir="${testsdir}/${contest}"
else
testsdir=tests
mkdir -p tests
# si les fichiers de testcases existent: on les extrait
zip="${rootdir}/testcases/${contest}/${testname}-testcases.zip"
if [ -s "${zip}" ]; then
unzip -q -o -d tests/${testname} "${zip}"
fi
zip="${rootdir}/testcases2/${contest}/${testname}-testcases.zip"
if [ -s "${zip}" ]; then
unzip -q -o -d tests/${testname} "${zip}"
fi
zip="${rootdir}/offline/testcases/${contest}/${testname}-testcases.zip"
if [ -s "${zip}" ]; then
unzip -q -o -d tests/${testname} "${zip}"
fi
fi
# the root folder for inputs, outputs and results
testdir=${testsdir}/${testname}
# si on n'a pas le répertoire des testcases, c'est une erreur
if [ ! -d "${testdir}/input" ]; then
echo -e "${COLOR_RED}MISSING TESTCASES${COLOR_END}"
exit 1
fi
##############################################################################
failure=0
for input in "${testdir}/input/input"*.txt; do
n=${input##*input}
n=${n%%.txt}
if [ ! -z "$number" -a "$number" != "a" ]; then
if [ "$number" -ne "${n}" ]; then
continue
fi
fi
echo -e "${COLOR_YELLOW}${exe} < ${input}${COLOR_END}"
exec 3>&2 # fd 3 is stderr too
exec 2> "${testdir}/${result}${n}.time" # builtin time will write to a file, not stderr
TIMEFORMAT="${COLOR_CYAN}(real %2R user %2U sys %2S)${COLOR_END}"
# old templates use the environment variable OUTPUT_PATH
export OUTPUT_PATH=/dev/stdout
if [ $quiet ]; then
time ${exe} < "${input}" 2>&3 > "${testdir}/${result}${n}.txt"
else
time ${exe} < "${input}" 2>&3 | tee "${testdir}/${result}${n}.txt"
fi
exec 2>&3 # restore stderr
exec 3<&- # close fd 3
elapsed=$(< ${testdir}/${result}${n}.time)
echo -ne "${COLOR_PURPLE}"
compare "${testdir}/${result}${n}.txt" "${testdir}/output/output${n}.txt"
rc=$?
echo -ne "${COLOR_END}"
[ $rc -ne 0 ] && failure=1
if [ $rc -eq 0 ] ; then
echo -e "${COLOR_YELLOW}TESTCASE ${n} : ${COLOR_GREEN}SUCCESS${COLOR_END} ${elapsed}"
else
echo -e "${COLOR_YELLOW}TESTCASE ${n} : ${COLOR_RED}FAILURE${COLOR_END} ${elapsed}"
fi
echo
done
if [ $failure -eq 0 ] ; then
echo -e "${COLOR_GREEN}SUCCESS${COLOR_END}"
else
echo -e "${COLOR_RED}FAILURE${COLOR_END}"
fi
exit $failure