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