-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck
More file actions
executable file
·124 lines (107 loc) · 2.94 KB
/
check
File metadata and controls
executable file
·124 lines (107 loc) · 2.94 KB
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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
function usage {
echo "Usage:"
echo " ${0} help Show this screen"
echo " ${0} list List problems in recommended order"
echo " ${0} problems Check problems"
echo " ${0} solutions Check solutions"
echo " ${0} <file.hx> Check single file"
}
# Recommended order.
declare -r problems="
lists/IsEmpty.hx
lists/HeadTail.hx
lists/Length.hx
lists/Product.hx
lists/Reverse.hx
parsing/CalcParse.hx
parsing/CalcEval.hx
"
# Default to "help".
declare -r option="${1:-help}"
if [[ "${option}" == "help" ]]; then
usage
exit 0
fi
if [[ "${option}" == "list" ]]; then
# shellcheck disable=SC2086
echo ${problems} | tr " " "\n"
exit 0
fi
if [[ "${option}" == "problems" || "${option}" == "solutions" ]]; then
# Call itself on every file, exit on the first failure.
for problem in ${problems}; do
if [[ "${option}" == "solutions" ]]; then
problem="${problem/.hx/Solution.hx}"
fi
echo "Checking ${problem}"
declare cmd="${0} ${problem}"
if ! ${cmd} >/dev/null 2>&1; then
echo " Failing tests"
echo " ${cmd}"
exit 1
fi
done
echo "All tests passed"
exit 0
fi
# .hx file is the only valid option remaining.
declare -r hx="${option}"
if [[ ! -r "${hx}" ]]; then
echo "Missing or not readable: ${hx}"
exit 1
fi
declare -r base="${hx%.hx}"
declare -r csv="${base/Solution/}Test.csv"
if [[ ! -r "${csv}" ]]; then
echo "Missing or not readable: ${csv}"
exit 1
fi
if ! type hacs >/dev/null 2>&1; then
echo "Missing command: hacs (check your \$PATH)"
exit 1
fi
echo "Compiling ${hx}"
declare -r dir="$(dirname "${hx}")"
declare -r src="$(basename "${hx}")"
# HACS prefers running in the same directory as the sources.
if ! (cd "${dir}" && hacs "${src}" >/dev/null 2>&1); then
# Chain with "true" to appease "errexit".
(cd "${dir}"; hacs --errors "${src}" || true)
echo "${hx} failed to compile"
exit 1
fi
echo "Running tests from ${csv}"
declare -r run="${base}.run"
if [[ ! -x "${run}" ]]; then
echo "Missing or not executable: ${run}"
exit 1
fi
declare -i total=0
declare -i passed=0
while IFS="," read scheme term expect; do
# Chain with "true" for "errexit", collapse spaces.
declare actual=$( ("${run}" --scheme="${scheme}" --term="${term}" \
2>/dev/null || true) | sed -e 's/^ *//' -e 's/ *$//' -e 's/ \+/ /g')
total=$((total + 1))
declare outcome="Test ${total}"
if [[ "${expect}" == "${actual}" ]]; then
outcome="${outcome} passed"
passed=$((passed + 1))
else
outcome="${outcome} failed"
fi
echo "${outcome}"
echo " ./${run} --scheme=${scheme} --term='${term}'"
echo " Expect: ${expect}"
echo " Actual: ${actual}"
done < "${csv}"
echo "Passed ${passed}/${total} tests"
if [[ ${passed} -eq ${total} ]]; then
exit 0
else
exit 1
fi