First NIT release and new clean mercurial repository
[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 # Ruby binary (none if you want to let the program to decide)
21 #RUBY=
22 [ -z "$NITC" ] && NITC=../bin/nitc
23
24 # Options to use with the ruby compiler
25 # OPT=
26
27 # Mark to distinguish files among tests
28 # MARK=
29
30 if [ $# = 0 ]; then
31 echo "usage: $0 file.nit ..."
32 fi
33
34 if [ -z "$HTM" ]; then
35 HTM=/dev/null
36 elif [ ! -f "$HTM" ]; then
37 echo "<html><body><table border=\"1\">" > $HTM
38 echo "<tr><td>Mode</td>" >> $HTM
39 for i in "$@"; do
40 f=`echo $i | cut -f1 -d.`
41 echo -n "<td><a href=\"$f.nit\">$f</a>" >> $HTM
42 if [ -r "$f.sav" ]; then
43 echo -n " - <a href=\"$f.sav\">sav</a>" >> $HTM
44 fi
45 if [ -f "$f.inputs" ]; then
46 echo -n " - <a href=\"$f.inputs\">inputs</a>" >> $HTM
47 fi
48 echo "</td>" >> $HTM
49 done
50 echo "<td>ok</td><td>fails</td></tr>" >> $HTM
51 fi
52
53 ok=""
54 nok=""
55 echo "<tr><td>$MARK</td>" >> $HTM
56
57 for ii in "$@"; do
58 for alt in "" `sed -n 's/.*#\(alt[0-9]*\)#.*/\1/p' "$ii" | sort -u`; do
59 f=`basename "$ii" .nit`
60 d=`dirname "$ii"`
61 ff="$f"
62 i="$ii"
63 if [ "x$alt" != "x" ]; then
64 test -d alt || mkdir -p alt
65 i="alt/${f}_$alt.nit"
66 ff="${ff}_$alt"
67 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
68 fi
69 ff="$ff$MARK"
70
71 echo -n "=> $i: "
72
73 rm "$ff.res" "$ff.err" "$ff.write" 2> /dev/null
74
75 # Compile
76 $NITC $OPT -o "$f.bin" "$i" -I . -I alt -I ../lib/standard 2> "$ff.cmp.err" > "$ff.compile.log"
77 ERR=$?
78 mv "$f.bin" "$ff.bin" 2> /dev/null
79 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
80 echo -n "<td><a href=\"$ff.compile.log\">compil " >> $HTM
81 if [ "$ERR" != 0 ]; then
82 echo -n "! "
83 echo -n "fail</a> - " >> $HTM
84 cp "$ff.cmp.err" "$ff.res"
85 else
86 echo -n ". "
87 echo -n "ok</a> - " >> $HTM
88 # Execute
89 if [ -f "$f.args" ]; then
90 args=`cat "$f.args"`
91 else
92 args=""
93 fi
94 if [ -f "$f.inputs" ]; then
95 "./$ff.bin" $args < "$f.inputs" > "$ff.res" 2>"$ff.err"
96 else
97 "./$ff.bin" $args > "$ff.res" 2>"$ff.err"
98 fi
99 if [ -f "$ff.write" ]; then
100 cat "$ff.write" >> "$ff.res"
101 echo -n "<a href=\"$ff.err\">write</a> - " >> $HTM
102 fi
103 if [ -s "$ff.err" ]; then
104 cat "$ff.err" >> "$ff.res"
105 echo -n "<a href=\"$ff.err\">err</a> - " >> $HTM
106 fi
107 fi
108
109 # Result
110 if [ -r "$ff.sav" ]; then
111 diff "$ff.res" "$ff.sav" > "$ff.diff.log"
112 if [ "$?" == 0 ]; then
113 echo "[ok] $ff.res"
114 ok="$ok $ff"
115 echo -n "<a href=\"$ff.res\">res ok</a>" >> $HTM
116 else
117 echo "[======= fail $ff.res $ff.sav =======]"
118 nok="$nok $ff"
119 echo -n "<strong style=\"background-color:red\"><a href=\"$ff.res\">res fail</a> - <a href=\"$ff.diff.log\">diff</a></strong>" >> $HTM
120 fi
121 else
122 echo "[=== no sav ===] $ff.res"
123 echo -n "<a href=\"$ff.res\">res</a>" >> $HTM
124 fi
125 echo "</td>" >> $HTM
126 done
127 done
128
129 echo "ok: " `echo $ok | wc -w` "/ $#"
130 echo "<td>" `echo $ok | wc -w` "</td><td>" `echo $nok | wc -w` "</td></tr>" >> $HTM
131
132 if [ -n "$nok" ]; then
133 echo "fail: $nok"
134 echo "There were errors !"
135 fi