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