-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkst.sh
executable file
·298 lines (274 loc) · 5.26 KB
/
kst.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#! /bin/bash
declare -r RELEASE_NUM="1"
declare -r FEATURE_NUM="5"
declare -r HOTFIX_NUM="1"
declare -r VERSION="$RELEASE_NUM.$FEATURE_NUM.$HOTFIX_NUM"
declare -r AUTHOR="Kjartan Óli Ágústsson"
help()
{
cat <<- _EOF_
Usage kst [OPTIONS] PROBLEM SOLUTION
Test a solution against the samples provided on Kattis
OPTIONS:
-V, --version Print version number
-i, --iceland Problem on iceland.kattis
-h, --help Print this help and exit
-o, --open Problem on open.kattis
-s domain, --subdomain domain Problem on subdomain.kattis.com
-v, --verbose Print the output the solution being tested
-p2, --python2 Use Python2 instead of Python3
_EOF_
}
parse_json()
{
get_data | sed -e 's/\\u0026lt;/</g' | jq '.results[].text' | sed 's/^"//g' | sed 's/"$//g'
}
get_data()
{
if [[ $subdomain == "iceland" ]]
then
cat <<- _EOF_
{
"results": $(curl -s "https://$subdomain.kattis.com/problems/iceland.$problem" | pup 'table.sample pre json{}')
}
_EOF_
else
cat <<- _EOF_
{
"results": $(curl -s "https://$subdomain.kattis.com/problems/$problem" | pup 'table.sample pre json{}')
}
_EOF_
fi
}
version()
{
cat <<- _EOF_
kst (Kattis Solution Tester) $VERSION
Written by $AUTHOR
_EOF_
}
strip_whitespace()
{
sed 's/ *$//'
}
determine_interpreter()
{
if ((verbose))
then
echo "Determining filetype"
fi
case $solution in
*.py)
run="python$python"
if ((verbose))
then
echo -e "File is python. Setting interpreter to python$python\n"
fi
;;
*.js)
run="node"
if ((verbose))
then
echo -e "File is javascript. Setting interpreter to node\n"
fi
;;
*.java)
run="java"
compiled=1
compiler="javac"
if ((verbose))
then
echo -e"File is Java. Setting compiler to javac\n"
fi
;;
*.cpp)
compiled=1
compiler="g++"
if ((verbose))
then
echo -e "File is C++. Setting compiler to g++\n"
fi
;;
*.c)
compiled=1
compiler="gcc"
if ((verbose))
then
echo -e "File is C. Setting compiler to gcc\n"
fi
;;
esac
}
python=3
compiled=0
verbose=0
while [ $1 ]
do
case $1 in
-i | --iceland)
subdomain="iceland"
;;
-o | --open)
subdomain="open"
;;
-V | --version)
version
exit 0
;;
-h | --help)
help
exit 0
;;
-s | --subdomain)
shift
subdomain="$1"
;;
-v | --verbose)
verbose=1
;;
-p2 | --python2)
python=2
;;
*)
if (($# == 2))
then
problem=$1
elif (($# == 1))
then
solution=$1
fi
;;
esac
shift
done
c=0
# Replace space characters so that each sample is its own line
for i in $(parse_json | sed -e 's/ /\d26/g')
do
# Replace '\n' characters in the json.
i=$(echo -e $i)
for x in $i
do
# Add space characters back in so the samples are correct
x=$(echo $x | sed -e 's/\d26/ /g')
echo $x
done > "test$c"
# Increment argc and assign to a garbage variable to avoid Command not found errors.
t=$((c++))
done
i=0
while ((i < c))
do
# Odd numbered files are output, even numbered files are input
if ((i % 2 == 1))
then
mv "test$i" "out$((i / 2))"
else
mv "test$i" "in$((i / 2))"
fi
# Increment argc and assign to a garbage variable to avoid Command not found errors.
t=$((i++))
done
samples=$(find -type f -name "in*" | wc -l)
determine_interpreter
if ((compiled))
then
if [[ $compiler == "javac" ]]
then
java=1
fi
if ((verbose))
then
echo -e "Compiling\n"
fi
$compiler $solution
fi
i=0
while ((i < samples))
do
if ((compiled))
then
if ((java))
then
if ((verbose))
then
echo -e "Testing solution against Sample Input $((i + 1))\nInput:"
cat "in$i"
echo "Output:"
output=$($run $(echo $solution | sed 's/.java//g') | strip_whitespace)
echo "$output"
echo "Expected output:"
cat "out$i"
if [[ $output == $(cat "out$i") ]]
then
result="passed"
else
result="failed"
fi
else
if [[ $($run $(echo $solution | sed 's/\.java//g') < "in$i" | strip_whitespace) == $(cat "out$i") ]]
then
result="passed"
else
result="failed"
fi
fi
else
if ((verbose))
then
echo -e "Testing solution against Sample Input $((i + 1))\nInput:"
cat "in$i"
echo "Output:"
output=$(./a.out < "in$i" | strip_whitespace)
echo "$output"
echo "Expected output:"
cat "out$i"
if [[ $output == $(cat "out$i") ]]
then
result="passed"
else
result="failed"
fi
else
if [[ $(./a.out < "in$i" | strip_whitespace) == $(cat "out$i") ]]
then
result="passed"
else
result="failed"
fi
fi
fi
else
if ((verbose))
then
echo -e "Testing solution against Sample Input $((i + 1))\nInput:"
cat "in$i"
echo "Output:"
output=$($run $solution < "in$i" | strip_whitespace)
echo -e "$output\nExpected output:"
cat "out$i"
if [[ $output == $(cat "out$i") ]]
then
result="passed"
else
result="failed"
fi
else
if [[ $($run $solution < "in$i" | strip_whitespace) == $(cat "out$i") ]]
then
result="passed"
else
result="failed"
fi
fi
fi
echo "Test $((i + 1)): $result"
# Increment argc and assign to a garbage variable to avoid Command not found errors.
t=$((i++))
if ((verbose && i != (c / 2)))
then
echo
fi
done
# Clean up temporary files
find ./ -type f \( -name "in*" -o -name "out*" \) -delete