-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.bash
134 lines (116 loc) · 2.81 KB
/
funcs.bash
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
test_file_name="$(basename "${0}")"
test_num="${test_file_name%%-*}"
test_pts="${test_file_name##*-}"
test_pts="${test_pts/.sh/}"
test_name="${test_file_name%-*}"
test_name="${test_name##*-}"
in_test=false
program_output=""
filtered_output=""
reference_output=""
run_timeout=0
exec &> "${TEST_DIR}/out.${test_num}.md"
test_start() {
if [[ ${in_test} == true ]]; then
echo "FATAL: Entering test block failed: missing 'test_end'?"
exit 1
fi
in_test=true
((test_count++))
echo "## Test ${test_num}: ${1} [${test_pts} pts]"
if [[ -n ${2} ]]; then
echo
echo "${2}"
fi
echo
echo '```'
trace_on
}
test_end() {
return=${?}
if [[ -n ${1} ]]; then
return=${1}
fi
if [[ "${return}" -eq 139 ]]; then
echo '--------------------------------------------'
echo ' --> ERROR: program terminated with SIGSEGV '
echo '--------------------------------------------'
echo
fi
if [[ "${return}" -eq 124 ]]; then
echo '--------------------------------------------'
echo " --> ERROR: program timed out (${run_timeout}s) "
echo '--------------------------------------------'
echo
fi
if [[ ${return} -ne 0 ]]; then
echo " --> Test failed (${return})"
fi
{ trace_off; } 2> /dev/null
in_test=false
echo -e '```'"\n"
exit "${return}"
}
trace_on() {
set -v
}
trace_off() {
{ set +v; } 2> /dev/null
}
run() {
program_output=$(timeout ${run_timeout} ${@})
program_return=$?
if [[ "${program_return}" -ne 0 ]]; then
test_end "${program_return}"
else
return 0
fi
}
reference_run() {
reference_output=$(${@})
return $?
}
filter() {
filtered_output=$(grep -iE ${@} <<< "${program_output}")
matches=0
if [[ -n "${filtered_output}" ]]; then
matches=$(wc -l <<< "${filtered_output}")
fi
echo " --> Filter matched ${matches} line(s)"
}
draw_sep() {
local term_sz="$(tput cols)"
local half=$(((term_sz - 1) / 2))
local midpoint="${1}"
if [[ -z "${midpoint}" ]]; then
midpoint='-'
fi
for (( i = 0 ; i < half ; ++i )); do
echo -n "-"
done
echo -n "${midpoint}"
for (( i = 0 ; i < (( half - (term_sz % 2))); ++i )); do
echo -n "-"
done
echo
}
compare_outputs() {
compare ${@} <(echo "${reference_output}") <(echo "${program_output}")
}
compare() {
echo
local term_sz="$(tput cols)"
local half=$(((term_sz - 1) / 2))
printf "%-${half}s| %s\n" "Expected Program Output" "Actual Program Output"
draw_sep 'V'
sdiff --expand-tabs --width="${term_sz}" ${@}
local result=${?}
draw_sep '^'
echo -n " --> "
if [[ ${result} -eq 0 ]]; then
echo "OK"
else
echo "FAIL"
fi
return ${result}
}