-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathtests.sh
executable file
·276 lines (256 loc) · 5.42 KB
/
tests.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
#!/bin/bash
PP=$(dirname $0)
function usage {
echo "tests.sh [-h] [-r n] [-v] MODEL [TESTS]"
if test "x$1" == "xhelp"
then
echo " -h|--help Help (this message)"
echo " -r|--repeat n Repeat test n times"
echo " -v|--verbose Verbose output"
echo " MODEL The name of the model to test"
echo " TESTS The list of test to run (optional)"
fi
}
function comment_wait {
# echo -ne "[ ] $1\r"
printf "[ ] %-70s %6s" "$1" "$2"
}
function comment_ok {
# echo -e "[\e[92m OK \e[0m] $1"
printf "\r[\e[92m OK \e[0m] %-70s %6s\n" "$1" "$2"
}
function comment_fail {
# echo -e "[\e[91m FAIL \e[0m] $1"
printf "\r[\e[91m FAIL \e[0m] %-70s %6s\n" "$1" "$2"
}
function try {
comment="$1"
shift
mkdir -p output/
log=$(echo $comment | sed 's|[ /\t]|.|g').log
comment_wait "$comment"
NEG=false
if test "x$1" == 'x!'
then
NEG=true
shift
fi
if env time --quiet -f "%e" -o $log.time "$@" >$log 2>&1
then
RES=true
else
RES=false
fi
if test $NEG != $RES
then
comment_ok "$comment" "$(cat $log.time)s"
if $VERBOSE
then
(
echo "---------------- LOG ---------------"
cat $log
echo "--------------------------------------"
) | sed 's|^| |'
fi
else
comment_fail "$comment"
(
echo "---------------- CMD ---------------"
echo $@
echo "---------------- TIME ---------------"
echo "Time: $(cat $log.time)s"
echo "---------------- LOG ---------------"
cat $log
echo "--------------------------------------"
) | sed 's|^| |'
exit -1;
fi
return 0;
}
REPEATS=1
VERBOSE=false
while true
do
case "$1" in
-r|--repeat)
REPEATS="$2"
if ! test "$REPEATS" -gt 0
then
echo "Error: number of repeats have to be greater then 0"
exit -1
fi
shift
;;
-v|--verbose)
VERBOSE=true
;;
-h|--help)
usage help
exit 0;
;;
*)
break;
esac
shift
done
MODEL=$1
if test -z "$MODEL"
then
usage
exit -1;
fi
shift
if ! test -f "CLB/$MODEL/main"
then
echo "\"$MODEL\" is not compiled"
echo " run: make $MODEL"
exit -1;
fi
if ! test -f "tests/external/README.md"
then
echo "\"tests/external\" submodule is not checked out"
echo " run: git submodule init"
echo " run: git submodule update"
exit -1
fi
if ! test -d "tests/external/$MODEL"
then
echo "No tests for model $MODEL."
echo "Exiting with no error."
exit 0
fi
TESTS_ARG="$*"
if test -z "$TESTS_ARG"
then
TESTS_ARG="."
fi
TESTS=""
DIR="tests/external/$MODEL"
for t in $TESTS_ARG
do
ADD=""
if test -f "$DIR/$t"
then
ADD="$t"
elif test -f "$DIR/$t.test"
then
ADD="$t.test"
elif test -d "$DIR/$t"
then
ADD="$(cd $DIR; ls $t/*.test 2>/dev/null)"
else
echo "Test not found: $t"
exit -1
fi
TESTS="$TESTS $ADD"
done
if test -z "$TESTS"
then
echo "No tests for model $MODEL (WARNING: there is a directory tests/external/$MODEL)"
echo "Exiting with no error."
exit 0
fi
GLOBAL="OK"
export PYTHONPATH="$PYTHONPATH:$PWD/tools/python"
function runline {
CMD=$1
shift
case $CMD in
run) try "running solver" "$@"; return $?;;
fail) try "running solver (should fail)" '!' "$@"; return $? ;;
csvconcatenate) try "concatenating csv files" $TCLB/tools/csvconcatenate "$@"; return $? ;;
esac
R=$1
shift
case $CMD in
exists) try "checking $R (exists)" test -f "$R"; return $? ;;
sha1) G="$R.sha1" ;;
*) G="$R" ;;
esac
if test -f "$TEST_DIR/$1"
then
G="$1"
shift
fi
G="$TEST_DIR/$G"
if ! test -f "$G"
then
comment_fail "Requested file not found: $G"
return -1
fi
case $CMD in
need) try "copy needed file" cp "$G" "$R"; return $? ;;
csvdiff) try "checking $R (csvdiff)" $TCLB/tools/csvdiff -a "$R" -b "$G" -x "${1:-1e-10}" -d ${2:-$CSV_DISCARD}; return $? ;;
diff) try "checking $R" diff "$R" "$G"; return $? ;;
sha1) try "checking $R (sha1)" sha1sum -c "$G.sha1"; return $? ;;
pvtidiff) try "checking $R (pvtidiff)" $TCLB/CLB/$MODEL/compare "$R" "$G" "${1:-8}" ${2:-} ${3:-} ${4:-}; return $? ;; # ${2:-8} is { if $2 == "" then "8" else $2 }
*) echo "unknown: $CMD"; return -1;;
esac
return 0;
}
function testModel {
for t in $TESTS
do
TEST="${t%.test}"
t="$TEST.test"
TEST=$(echo $TEST | sed 's|^[.]/||g' | sed 's|/|-|g')
TDIR="test-$MODEL-$TEST-$1"
test -d "$TDIR" && rm -r "$TDIR"
RESULT="OK"
TCLB=".."
SOLVER="$TCLB/CLB/$MODEL/main"
MODELBIN="$TCLB/CLB/$MODEL"
TOOLS="$TCLB/tools"
TEST_DIR="../tests/external/$MODEL"
CAN_FAIL=false
CSV_DISCARD=Walltime
EXC_SH="$PP/etc/test.exceptions.sh"
if test -f "$EXC_SH"
then
source "$EXC_SH"
fi
if test -f "tests/external/$MODEL/$t"
then
echo -e "\n\e[1mRunning $TEST test...\e[0m"
mkdir -p $TDIR
while read -r -u 3 line
do
if ! (cd $TDIR && runline $(eval echo $line))
then
RESULT="FAILED"
break
fi
done 3< "tests/external/$MODEL/$t"
else
echo "$t: test not found"
RESULT="NOT FOUND"
fi
# echo -n " Test \"$TEST\" returned:"
if test "x$RESULT" == "xOK"
then
comment_ok "$TEST test finished" "-----"
else
if $CAN_FAIL
then
comment_fail "$TEST test finished (can fail)" "-----"
else
comment_fail "$TEST test finished" "-----"
GLOBAL="FAILED"
fi
fi
done
}
REPEAT=1
while test "$REPEAT" -le "$REPEATS"
do
test "$REPEATS" -gt "1" && echo "############ repeat: $REPEAT ################"
testModel $REPEAT
REPEAT=$(expr $REPEAT + 1)
done
if test "x$GLOBAL" == "xOK"
then
exit 0
else
echo "Some tests failed"
exit -1
fi