-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests
executable file
·162 lines (133 loc) · 3.05 KB
/
runtests
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
#!/bin/sh
base_loc="$XDG_DATA_HOME/test_cases"
exec_loc="/tmp/out.out"
red(){
printf "\033[91m$1\033[0m"
}
green(){
printf "\033[92m$1\033[0m"
}
error() {
if [ $? -eq "124" ]; then
error "Timelimit Exceeded"
fi
red "[ERROR]"
echo " $1"
[ -f "$base_loc/out" ] && cat "$base_loc/out"
exit
}
read_input() {
rm -f $1
while read line
do
# break if the line is empty
[ -z "$line" ] && break
echo "$line" | sed -e 's/^\s*//g' -e 's/\s*$//g' >> "$1"
done
}
add_test_case() {
counter=$(cat "$base_loc/counter")
loc="$base_loc/$counter"
mkdir -p "$loc"
echo "==== INPUT ===="
read_input "$loc/input.txt"
echo "==== OUTPUT ===="
read_input "$loc/output.txt"
expr $counter \+ 1 > "$base_loc/counter"
exit
}
init_test_cases() {
[ -d "$base_loc" ] && return
echo "init new test case"
mkdir -p "$base_loc"
echo "1" > "$base_loc/counter"
}
purge_test_cases() {
printf "are you sure? "
read null
rm -rf "$base_loc"
exit
}
view_test_cases() {
counter=$(cat "$base_loc/counter")
if [ "$counter" -eq "1" ]
then
echo "no test cases"
return
fi
for i in $(seq $(expr $counter \- 1))
do
green "CASE $i\n"
pr -m -t "$base_loc/$i/input.txt" "$base_loc/$i/output.txt"
done
exit
}
run_py_test_case() {
green "CASE #$3 "
cat "$2/input.txt" | timeout 3s python3 "$1" > "$base_loc/out" || error "runtime error"
if diff "$base_loc/out" "$2/output.txt" -white > /dev/null; then
green "[ passed ]\n"
else
red "[ failed ]\n"
pr -m -t "$base_loc/out" "$2/output.txt"
printf "\n"
fi
rm "$base_loc/out"
}
run_js_test_case() {
green "CASE #$3 "
cat "$2/input.txt" | timeout 2s node "$1" > "$base_loc/out" || error "runtime error"
if diff "$base_loc/out" "$2/output.txt" -white > /dev/null; then
green "[ passed ]\n"
else
red "[ failed ]\n"
pr -m -t "$base_loc/out" "$2/output.txt"
printf "\n"
fi
rm "$base_loc/out"
}
run_cpp_test_case() {
green "CASE #$3 "
if [ ! -f $exec_loc ]
then
g++ $1 -o "$exec_loc" || error "compilation error"
fi
cat "$2/input.txt" | timeout 1s "$exec_loc" > "$base_loc/out" || error "runtime error"
if diff "$base_loc/out" "$2/output.txt" -white > /dev/null; then
green "[ passed ]\n"
else
red "[ failed ]\n"
pr -m -t "$base_loc/out" "$2/output.txt"
printf "\n"
fi
rm "$base_loc/out"
}
run_test_cases() {
counter=$(cat "$base_loc/counter")
if [ "$counter" -eq "1" ]
then
echo "no test cases"
return
fi
for i in $(seq $(expr $counter \- 1))
do
case "$(echo $1 | sed 's/.*\.//')" in
"cpp") run_cpp_test_case "$1" "$base_loc/$i" "$i";;
"py") run_py_test_case "$1" "$base_loc/$i" "$i";;
"js") run_js_test_case "$1" "$base_loc/$i" "$i";;
*) error "extension not matched";;
esac
done
[ -f "$exec_loc" ] && rm $exec_loc
}
init_test_cases
[ -f "$base_loc/out" ] && rm "$base_loc/out"
while getopts "a c v" flag
do
case "${flag}" in
a) add_test_case;;
c) purge_test_cases;;
v) view_test_cases;;
esac
done
run_test_cases $1