tests: teach a res file to contains `UNDEFINED` and always match
[nit.git] / tests / tests.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # This shell script compile, run and verify Nit program files
19
20 # Set lang do default to avoid failed tests because of locale
21 export LANG=C
22 export NIT_TESTING=true
23
24 unset NIT_DIR
25
26 # Get the first Java lib available
27 shopt -s nullglob
28 paths=`echo /usr/lib/jvm/*/`
29 paths=($paths)
30 JAVA_HOME=${paths[0]}
31
32 paths=`echo $JAVA_HOME/jre/lib/*/{client,server}/`
33 paths=($paths)
34 JNI_LIB_PATH=${paths[0]}
35 shopt -u nullglob
36
37 usage()
38 {
39 e=`basename "$0"`
40 cat<<END
41 Usage: $e [options] modulenames
42 -o option Pass option to the engine
43 -v Verbose (show tests steps)
44 -h This help
45 --engine Use a specific engine (default=nitg)
46 --noskip Do not skip a test even if the .skip file matches
47 END
48 }
49
50 # $1 is the pattern of the test
51 # $2 is the file to compare to
52 # the result is:
53 # 0: if the file to compare to do not exists
54 # 1: if the file match
55 # 2: if the file match with soso
56 # 3: if the file do not match
57 function compare_to_result()
58 {
59 local pattern="$1"
60 local sav="$2"
61 if [ ! -r "$sav" ]; then return 0; fi
62 test "`cat "$sav"`" = "UNDEFINED" && return 1
63 diff -u "$sav" "out/$pattern.res" > "out/$pattern.diff.sav.log"
64 if [ "$?" == 0 ]; then
65 return 1
66 fi
67 sed '/[Ww]arning/d;/[Ee]rror/d' "out/$pattern.res" > "out/$pattern.res2"
68 sed '/[Ww]arning/d;/[Ee]rror/d' "$sav" > "out/$pattern.sav2"
69 grep '[Ee]rror' "out/$pattern.res" >/dev/null && echo "Error" >> "out/$pattern.res2"
70 grep '[Ee]rror' "$sav" >/dev/null && echo "Error" >> "out/$pattern.sav2"
71 diff -u "out/$pattern.sav2" "out/$pattern.res2" > "out/$pattern.diff.sav.log2"
72 if [ "$?" == 0 ]; then
73 return 2
74 else
75 return 3
76 fi
77 }
78
79 # As argument: the pattern used for the file
80 function process_result()
81 {
82 # Result
83 pattern=$1
84 description=$2
85 pack=$3
86 SAV=""
87 NSAV=""
88 FIXME=""
89 NFIXME=""
90 SOSO=""
91 NSOSO=""
92 SOSOF=""
93 NSOSOF=""
94 OLD=""
95 LIST=""
96 FIRST=""
97 echo >>$xml "<testcase classname='$pack' name='$description'>"
98 #for sav in "sav/$engine/fixme/$pattern.res" "sav/$engine/$pattern.res" "sav/fixme/$pattern.res" "sav/$pattern.res" "sav/$pattern.sav"; do
99 for savdir in $savdirs; do
100 sav=$savdir/fixme/$pattern.res
101 compare_to_result "$pattern" "$sav"
102 case "$?" in
103 0)
104 ;; # no file
105 1)
106 OLD="$LIST"
107 FIXME="$sav"
108 LIST="$LIST $sav"
109 ;;
110 2)
111 if [ -z "$FIRST" ]; then
112 SOSOF="$sav"
113 FIRST="$sav"
114 fi
115 LIST="$LIST $sav"
116 ;;
117 3)
118 if [ -z "$FIRST" ]; then
119 NFIXME="$sav"
120 FIRST="$sav"
121 fi
122 LIST="$LIST $sav"
123 ;;
124 esac
125
126 sav=$savdir/$pattern.res
127 compare_to_result "$pattern" "$sav"
128 case "$?" in
129 0)
130 ;; # no file
131 1)
132 OLD="$LIST"
133 SAV="$sav"
134 LIST="$LIST $sav"
135 ;;
136 2)
137 if [ -z "$FIRST" ]; then
138 SOSO="$sav"
139 FIRST="$sav"
140 fi
141 LIST="$LIST $sav"
142 ;;
143 3)
144 if [ -z "$FIRST" ]; then
145 NSAV="$sav"
146 FIRST="$sav"
147 fi
148 LIST="$LIST $sav"
149 ;;
150 esac
151 done
152 OLD=`echo "$OLD" | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//'`
153 grep 'NOT YET IMPLEMENTED' "out/$pattern.res" >/dev/null
154 NYI="$?"
155 if [ -n "$SAV" ]; then
156 if [ -n "$OLD" ]; then
157 echo "[*ok*] out/$pattern.res $SAV - but $OLD remains!"
158 echo >>$xml "<error message='ok out/$pattern.res - but $OLD remains'/>"
159 remains="$remains $OLD"
160 else
161 echo "[ok] out/$pattern.res $SAV"
162 fi
163 ok="$ok $pattern"
164 elif [ -n "$FIXME" ]; then
165 if [ -n "$OLD" ]; then
166 echo "[*fixme*] out/$pattern.res $FIXME - but $OLD remains!"
167 echo >>$xml "<error message='ok out/$pattern.res - but $OLD remains'/>"
168 remains="$remains $OLD"
169 else
170 echo "[fixme] out/$pattern.res $FIXME"
171 echo >>$xml "<skipped/>"
172 fi
173 todos="$todos $pattern"
174 elif [ "x$NYI" = "x0" ]; then
175 echo "[todo] out/$pattern.res -> not yet implemented"
176 echo >>$xml "<skipped/>"
177 todos="$todos $pattern"
178 elif [ -n "$SOSO" ]; then
179 echo "[======= soso out/$pattern.res $SOSO =======]"
180 echo >>$xml "<error message='soso out/$pattern.res $SOSO'/>"
181 echo >>$xml "<system-out><![CDATA["
182 cat -v out/$pattern.diff.sav.log | head >>$xml -n 50
183 echo >>$xml "]]></system-out>"
184 nok="$nok $pattern"
185 echo "$ii" >> "$ERRLIST"
186 elif [ -n "$SOSOF" ]; then
187 echo "[======= fixme soso out/$pattern.res $SOSOF =======]"
188 echo >>$xml "<error message='soso out/$pattern.res $SOSO'/>"
189 echo >>$xml "<system-out><![CDATA["
190 cat -v out/$pattern.diff.sav.log | head >>$xml -n 50
191 echo >>$xml "]]></system-out>"
192 nok="$nok $pattern"
193 echo "$ii" >> "$ERRLIST"
194 elif [ -n "$NSAV" ]; then
195 echo "[======= fail out/$pattern.res $NSAV =======]"
196 echo >>$xml "<error message='fail out/$pattern.res $NSAV'/>"
197 echo >>$xml "<system-out><![CDATA["
198 cat -v out/$pattern.diff.sav.log | head >>$xml -n 50
199 echo >>$xml "]]></system-out>"
200 nok="$nok $pattern"
201 echo "$ii" >> "$ERRLIST"
202 elif [ -n "$NFIXME" ]; then
203 echo "[======= changed out/$pattern.res $NFIXME ======]"
204 echo >>$xml "<error message='changed out/$pattern.res $NFIXME'/>"
205 echo >>$xml "<system-out><![CDATA["
206 cat -v out/$pattern.diff.sav.log | head >>$xml -n 50
207 echo >>$xml "]]></system-out>"
208 nok="$nok $pattern"
209 echo "$ii" >> "$ERRLIST"
210 elif [ -s out/$pattern.res ]; then
211 echo "[=== no sav ===] out/$pattern.res is not empty"
212 echo >>$xml "<error message='no sav and not empty'/>"
213 echo >>$xml "<system-out><![CDATA["
214 cat -v >>$xml out/$pattern.res
215 echo >>$xml "]]></system-out>"
216 nos="$nos $pattern"
217 else
218 # no sav but empty res
219 echo "[0k] out/$pattern.res is empty"
220 ok="$ok $pattern"
221 fi
222 if test -s out/$pattern.cmp.err; then
223 echo >>$xml "<system-err><![CDATA["
224 cat -v >>$xml out/$pattern.cmp.err
225 echo >>$xml "]]></system-err>"
226 fi
227 echo >>$xml "</testcase>"
228 }
229
230 need_skip()
231 {
232 test "$noskip" = true && return 1
233 if echo "$1" | grep -f "$engine.skip" >/dev/null 2>&1; then
234 echo "=> $2: [skip]"
235 echo >>$xml "<testcase classname='$3' name='$2'><skipped/></testcase>"
236 return 0
237 fi
238 if test $engine = niti && echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
239 echo "=> $2: [skip exec]"
240 echo >>$xml "<testcase classname='$3' name='$2'><skipped/></testcase>"
241 return 0
242 fi
243 return 1
244 }
245
246 skip_exec()
247 {
248 test "$noskip" = true && return 1
249 if echo "$1" | grep -f "exec.skip" >/dev/null 2>&1; then
250 echo -n "_ "
251 return 0
252 fi
253 return 1
254 }
255
256 skip_cc()
257 {
258 test "$noskip" = true && return 1
259 if echo "$1" | grep -f "cc.skip" >/dev/null 2>&1; then
260 return 0
261 fi
262 return 1
263 }
264
265 find_nitc()
266 {
267 name="$enginebinname"
268 recent=`ls -t ../src/$name ../src/$name_[0-9] ../bin/$name ../c_src/$name 2>/dev/null | head -1`
269 if [[ "x$recent" == "x" ]]; then
270 echo "Could not find binary for engine $engine, aborting"
271 exit 1
272 fi
273 echo "Find binary for engine $engine: $recent $OPT"
274 NITC=$recent
275 }
276
277 verbose=false
278 stop=false
279 engine=nitg
280 noskip=
281 savdirs=
282 while [ $stop = false ]; do
283 case $1 in
284 -o) OPT="$OPT $2"; shift; shift;;
285 -v) verbose=true; shift;;
286 -h) usage; exit;;
287 --engine) engine="$2"; shift; shift;;
288 --noskip) noskip=true; shift;;
289 *) stop=true
290 esac
291 done
292 enginebinname=$engine
293 case $engine in
294 nitg)
295 engine=nitg-s;
296 enginebinname=nitg;
297 OPT="--separate $OPT"
298 ;;
299 nitg-s)
300 enginebinname=nitg;
301 OPT="--separate $OPT"
302 ;;
303 nitg-e)
304 enginebinname=nitg;
305 OPT="--erasure $OPT"
306 ;;
307 nitg-g)
308 enginebinname=nitg;
309 OPT="--global $OPT"
310 ;;
311 nit)
312 engine=niti
313 ;;
314 niti)
315 enginebinname=nit
316 ;;
317 nitc)
318 echo "disabled engine $engine"
319 exit 0
320 ;;
321 *)
322 echo "unknown engine $engine"
323 exit 1
324 ;;
325 esac
326
327 savdirs="sav/$engine $savdirs sav/"
328
329 # The default nitc compiler
330 [ -z "$NITC" ] && find_nitc
331
332 # Set NIT_DIR if needed
333 [ -z "$NIT_DIR" ] && export NIT_DIR=..
334
335 if sh -c "timelimit echo" 1>/dev/null 2>&1; then
336 TIMEOUT="timelimit -t 600"
337 elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
338 TIMEOUT="timeout 600s"
339 else
340 echo "No timelimit or timeout command detected. Tests may hang :("
341 fi
342
343 # Mark to distinguish files among tests
344 # MARK=
345
346 # File where error tests are outputed
347 # Old ERRLIST is backuped
348 ERRLIST=${ERRLIST:-errlist}
349 ERRLIST_TARGET=$ERRLIST
350
351 if [ $# = 0 ]; then
352 usage;
353 exit
354 fi
355
356 # Initiate new ERRLIST
357 if [ "x$ERRLIST" = "x" ]; then
358 ERRLIST=/dev=null
359 else
360 ERRLIST=$ERRLIST.tmp
361 > "$ERRLIST"
362 fi
363
364 ok=""
365 nok=""
366 todos=""
367 xml="tests-$engine.xml"
368 echo >$xml "<testsuites><testsuite>"
369
370 # CLEAN the out directory
371 rm -rf out/ 2>/dev/null
372 mkdir out 2>/dev/null
373
374 for ii in "$@"; do
375 if [ ! -f $ii ]; then
376 echo "File '$ii' does not exist."
377 continue
378 fi
379 f=`basename "$ii" .nit`
380
381 pack=`echo $ii | perl -p -e 's|^../([^/]*)/([a-zA-Z_]*).*|\1.\2| || s|^([a-zA-Z]*)[^_]*_([a-zA-Z]*).*|\1.\2| || s|\W*([a-zA-Z_]*).*|\1|'`
382
383 # Sould we skip the file for this engine?
384 need_skip $f $f $pack && continue
385
386 tmp=${ii/../AA}
387 if [ "x$tmp" = "x$ii" ]; then
388 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
389 else
390 includes="-I alt"
391 fi
392
393 for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
394 bf=`basename $i .nit`
395 ff="out/$bf"
396
397 # Sould we skip the alternative for this engine?
398 need_skip $bf $bf $pack && continue
399
400 echo -n "=> $bf: "
401
402 if [ -f "$f.inputs" ]; then
403 inputs="$f.inputs"
404 else
405 inputs=/dev/null
406 fi
407
408 if [ "$engine" = "niti" ]; then
409 cat > "./$ff.bin" <<END
410 exec $NITC --no-color $OPT "$i" $includes -- "\$@"
411 END
412 chmod +x "./$ff.bin"
413 > "$ff.cmp.err"
414 > "$ff.compile.log"
415 ERR=0
416 else
417 if skip_cc "$bf"; then
418 nocc="--no-cc"
419 else
420 nocc=
421 fi
422 # Compile
423 if [ "x$verbose" = "xtrue" ]; then
424 echo ""
425 echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes" $nocc
426 fi
427 NIT_NO_STACK=1 JNI_LIB_PATH=$JNI_LIB_PATH JAVA_HOME=$JAVA_HOME \
428 $TIMEOUT $NITC --no-color $OPT -o "$ff.bin" "$i" $includes $nocc 2> "$ff.cmp.err" > "$ff.compile.log"
429 ERR=$?
430 if [ "x$verbose" = "xtrue" ]; then
431 cat "$ff.compile.log"
432 cat >&2 "$ff.cmp.err"
433 fi
434 fi
435 if [ "$ERR" != 0 ]; then
436 echo -n "! "
437 cat "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
438 process_result $bf $bf $pack
439 elif skip_exec "$bf"; then
440 # No exec
441 > "$ff.res"
442 process_result $bf $bf $pack
443 elif [ -n "$nocc" ]; then
444 # not compiled
445 echo -n "nocc "
446 > "$ff.res"
447 process_result $bf $bf $pack
448 elif [ -x "./$ff.bin" ]; then
449 echo -n ". "
450 # Execute
451 args=""
452 if [ "x$verbose" = "xtrue" ]; then
453 echo ""
454 echo "NIT_NO_STACK=1 ./$ff.bin" $args
455 fi
456 NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
457 $TIMEOUT "./$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
458 if [ "x$verbose" = "xtrue" ]; then
459 cat "$ff.res"
460 cat >&2 "$ff.err"
461 fi
462 if [ -f "$ff.write" ]; then
463 cat "$ff.write" >> "$ff.res"
464 elif [ -d "$ff.write" ]; then
465 LANG=C /bin/ls -F $ff.write >> "$ff.res"
466 fi
467 cp "$ff.res" "$ff.res2"
468 cat "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
469 process_result $bf $bf $pack
470
471 if [ -f "$f.args" ]; then
472 fargs=$f.args
473 cptr=0
474 while read line; do
475 ((cptr=cptr+1))
476 args="$line"
477 bff=$bf"_args"$cptr
478 fff=$ff"_args"$cptr
479 name="$bf args $cptr"
480
481 # Sould we skip the input for this engine?
482 need_skip $bff " $name" $pack && continue
483
484 # use a specific inputs file, if required
485 if [ -f "$bff.inputs" ]; then
486 ffinputs="$bff.inputs"
487 else
488 ffinputs=$inputs
489 fi
490
491 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
492 if [ "x$verbose" = "xtrue" ]; then
493 echo ""
494 echo "NIT_NO_STACK=1 ./$ff.bin" $args
495 fi
496 echo -n "==> $name "
497 echo "./$ff.bin $args" > "./$fff.bin"
498 chmod +x "./$fff.bin"
499 WRITE="$fff.write" sh -c "NIT_NO_STACK=1 $TIMEOUT ./$fff.bin < $ffinputs > $fff.res 2>$fff.err"
500 if [ "x$verbose" = "xtrue" ]; then
501 cat "$fff.res"
502 cat >&2 "$fff.err"
503 fi
504 if [ -f "$fff.write" ]; then
505 cat "$fff.write" >> "$fff.res"
506 elif [ -d "$fff.write" ]; then
507 LANG=C /bin/ls -F $fff.write >> "$fff.res"
508 fi
509 if [ -s "$fff.err" ]; then
510 cp "$fff.res" "$fff.res2"
511 cat "$fff.err" "$fff.res2" > "$fff.res"
512 fi
513 process_result $bff " $name" $pack
514 done < $fargs
515 fi
516 elif [ -f "./$ff.bin" ]; then
517 echo "Not executable (platform?)" > "$ff.res"
518 process_result $bf "$bf" $pack
519 else
520 echo -n "! "
521 cat "$ff.cmp.err" > "$ff.res"
522 echo "Compilation error" > "$ff.res"
523 process_result $bf "$bf" $pack
524 fi
525 done
526 done
527
528 echo "engine: $engine ($enginebinname $OPT)"
529 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
530
531 if [ -n "$nok" ]; then
532 echo "fail: $nok"
533 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
534 fi
535 if [ -n "$nos" ]; then
536 echo "no sav: $nos"
537 fi
538 if [ -n "$todos" ]; then
539 echo "todo/fixme: $todos"
540 fi
541 if [ -n "$remains" ]; then
542 echo "sav that remains: $remains"
543 fi
544
545 # write $ERRLIST
546 if [ "x$ERRLIST" != "x" ]; then
547 if [ -x "$ERRLIST_TARGET" ]; then
548 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
549 fi
550 mv $ERRLIST $ERRLIST_TARGET
551 fi
552
553 echo >>$xml "</testsuite></testsuites>"
554
555 if [ -n "$nok" ]; then
556 exit 1
557 else
558 exit 0
559 fi