8e2c65f8a7288648a272f6326a60f6b9a4113e19
[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-sg)
308 enginebinname=nitg;
309 OPT="--semi-global $OPT"
310 ;;
311 nitg-g)
312 enginebinname=nitg;
313 OPT="--global $OPT"
314 ;;
315 nit)
316 engine=niti
317 ;;
318 niti)
319 enginebinname=nit
320 ;;
321 nitc)
322 echo "disabled engine $engine"
323 exit 0
324 ;;
325 *)
326 echo "unknown engine $engine"
327 exit 1
328 ;;
329 esac
330
331 savdirs="sav/$engine $savdirs sav/"
332
333 # The default nitc compiler
334 [ -z "$NITC" ] && find_nitc
335
336 # Set NIT_DIR if needed
337 [ -z "$NIT_DIR" ] && export NIT_DIR=..
338
339 if sh -c "timelimit echo" 1>/dev/null 2>&1; then
340 TIMEOUT="timelimit -t 600"
341 elif sh -c "timeout 1 echo" 1>/dev/null 2>&1; then
342 TIMEOUT="timeout 600s"
343 else
344 echo "No timelimit or timeout command detected. Tests may hang :("
345 fi
346
347 # Mark to distinguish files among tests
348 # MARK=
349
350 # File where error tests are outputed
351 # Old ERRLIST is backuped
352 ERRLIST=${ERRLIST:-errlist}
353 ERRLIST_TARGET=$ERRLIST
354
355 if [ $# = 0 ]; then
356 usage;
357 exit
358 fi
359
360 # Initiate new ERRLIST
361 if [ "x$ERRLIST" = "x" ]; then
362 ERRLIST=/dev=null
363 else
364 ERRLIST=$ERRLIST.tmp
365 > "$ERRLIST"
366 fi
367
368 ok=""
369 nok=""
370 todos=""
371 xml="tests-$engine.xml"
372 echo >$xml "<testsuites><testsuite>"
373
374 # CLEAN the out directory
375 rm -rf out/ 2>/dev/null
376 mkdir out 2>/dev/null
377
378 for ii in "$@"; do
379 if [ ! -f $ii ]; then
380 echo "File '$ii' does not exist."
381 continue
382 fi
383 f=`basename "$ii" .nit`
384
385 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|'`
386
387 # Sould we skip the file for this engine?
388 need_skip $f $f $pack && continue
389
390 tmp=${ii/../AA}
391 if [ "x$tmp" = "x$ii" ]; then
392 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
393 else
394 includes="-I alt"
395 fi
396
397 for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
398 bf=`basename $i .nit`
399 ff="out/$bf"
400
401 # Sould we skip the alternative for this engine?
402 need_skip $bf $bf $pack && continue
403
404 echo -n "=> $bf: "
405
406 if [ -f "$f.inputs" ]; then
407 inputs="$f.inputs"
408 else
409 inputs=/dev/null
410 fi
411
412 ffout="$ff.bin"
413
414 if [ "$engine" = "niti" ]; then
415 cat > "./$ff.bin" <<END
416 exec $NITC --no-color $OPT "$i" $includes -- "\$@"
417 END
418 chmod +x "./$ff.bin"
419 > "$ff.cmp.err"
420 > "$ff.compile.log"
421 ERR=0
422 else
423 if skip_cc "$bf"; then
424 nocc="--no-cc"
425 else
426 nocc=
427 fi
428 # Compile
429 if [ "x$verbose" = "xtrue" ]; then
430 echo ""
431 echo $NITC --no-color $OPT -o "$ffout" "$i" "$includes" $nocc
432 fi
433 NIT_NO_STACK=1 JNI_LIB_PATH=$JNI_LIB_PATH JAVA_HOME=$JAVA_HOME \
434 $TIMEOUT $NITC --no-color $OPT -o "$ffout" "$i" $includes $nocc 2> "$ff.cmp.err" > "$ff.compile.log"
435 ERR=$?
436 if [ "x$verbose" = "xtrue" ]; then
437 cat "$ff.compile.log"
438 cat >&2 "$ff.cmp.err"
439 fi
440 fi
441 if [ "$ERR" != 0 ]; then
442 echo -n "! "
443 cat "$ff.compile.log" "$ff.cmp.err" > "$ff.res"
444 process_result $bf $bf $pack
445 elif skip_exec "$bf"; then
446 # No exec
447 > "$ff.res"
448 process_result $bf $bf $pack
449 elif [ -n "$nocc" ]; then
450 # not compiled
451 echo -n "nocc "
452 > "$ff.res"
453 process_result $bf $bf $pack
454 elif [ -x "./$ff.bin" ]; then
455 echo -n ". "
456 # Execute
457 args=""
458 if [ "x$verbose" = "xtrue" ]; then
459 echo ""
460 echo "NIT_NO_STACK=1 ./$ff.bin" $args
461 fi
462 NIT_NO_STACK=1 LD_LIBRARY_PATH=$JNI_LIB_PATH \
463 $TIMEOUT "./$ff.bin" $args < "$inputs" > "$ff.res" 2>"$ff.err"
464 if [ "x$verbose" = "xtrue" ]; then
465 cat "$ff.res"
466 cat >&2 "$ff.err"
467 fi
468 if [ -f "$ff.write" ]; then
469 cat "$ff.write" >> "$ff.res"
470 elif [ -d "$ff.write" ]; then
471 LANG=C /bin/ls -F $ff.write >> "$ff.res"
472 fi
473 cp "$ff.res" "$ff.res2"
474 cat "$ff.cmp.err" "$ff.err" "$ff.res2" > "$ff.res"
475 process_result $bf $bf $pack
476
477 if [ -f "$f.args" ]; then
478 fargs=$f.args
479 cptr=0
480 while read line; do
481 ((cptr=cptr+1))
482 args="$line"
483 bff=$bf"_args"$cptr
484 fff=$ff"_args"$cptr
485 name="$bf args $cptr"
486
487 # Sould we skip the input for this engine?
488 need_skip $bff " $name" $pack && continue
489
490 # use a specific inputs file, if required
491 if [ -f "$bff.inputs" ]; then
492 ffinputs="$bff.inputs"
493 else
494 ffinputs=$inputs
495 fi
496
497 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
498 if [ "x$verbose" = "xtrue" ]; then
499 echo ""
500 echo "NIT_NO_STACK=1 ./$ff.bin" $args
501 fi
502 echo -n "==> $name "
503 echo "./$ff.bin $args" > "./$fff.bin"
504 chmod +x "./$fff.bin"
505 WRITE="$fff.write" sh -c "NIT_NO_STACK=1 $TIMEOUT ./$fff.bin < $ffinputs > $fff.res 2>$fff.err"
506 if [ "x$verbose" = "xtrue" ]; then
507 cat "$fff.res"
508 cat >&2 "$fff.err"
509 fi
510 if [ -f "$fff.write" ]; then
511 cat "$fff.write" >> "$fff.res"
512 elif [ -d "$fff.write" ]; then
513 LANG=C /bin/ls -F $fff.write >> "$fff.res"
514 fi
515 if [ -s "$fff.err" ]; then
516 cp "$fff.res" "$fff.res2"
517 cat "$fff.err" "$fff.res2" > "$fff.res"
518 fi
519 process_result $bff " $name" $pack
520 done < $fargs
521 fi
522 elif [ -f "./$ff.bin" ]; then
523 echo "Not executable (platform?)" > "$ff.res"
524 process_result $bf "$bf" $pack
525 else
526 echo -n "! "
527 cat "$ff.cmp.err" > "$ff.res"
528 echo "Compilation error" > "$ff.res"
529 process_result $bf "$bf" $pack
530 fi
531 done
532 done
533
534 echo "engine: $engine ($enginebinname $OPT)"
535 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos $todos | wc -w`
536
537 if [ -n "$nok" ]; then
538 echo "fail: $nok"
539 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
540 fi
541 if [ -n "$nos" ]; then
542 echo "no sav: $nos"
543 fi
544 if [ -n "$todos" ]; then
545 echo "todo/fixme: $todos"
546 fi
547 if [ -n "$remains" ]; then
548 echo "sav that remains: $remains"
549 fi
550
551 # write $ERRLIST
552 if [ "x$ERRLIST" != "x" ]; then
553 if [ -x "$ERRLIST_TARGET" ]; then
554 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
555 fi
556 mv $ERRLIST $ERRLIST_TARGET
557 fi
558
559 echo >>$xml "</testsuite></testsuites>"
560
561 if [ -n "$nok" ]; then
562 exit 1
563 else
564 exit 0
565 fi