tests: tests_icode also use alterner.pl
[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
23 usage()
24 {
25 e=`basename "$0"`
26 cat<<END
27 Usage: $e [options] modulenames
28 -o option Pass option to nitc
29 -v Verbose (show tests steps)
30 -h This help
31 END
32 }
33
34 # As argument: the pattern used for the file
35 function process_result()
36 {
37 # Result
38 pattern=$1
39 SAV=""
40 FAIL=""
41 SOSO=""
42 SOSOF=""
43 if [ -r "sav/$pattern.sav" ]; then
44 diff -u "out/$pattern.res" "sav/$pattern.sav" > "out/$pattern.diff.sav.log"
45 if [ "$?" == 0 ]; then
46 SAV=OK
47 else
48 SAV=NOK
49 fi
50 sed '/[Ww]arning/d;/[Ee]rror/d' "out/$pattern.res" > "out/$pattern.res2"
51 sed '/[Ww]arning/d;/[Ee]rror/d' "sav/$pattern.sav" > "out/$pattern.sav2"
52 grep '[Ee]rror' "out/$pattern.res" >/dev/null && echo "Error" >> "out/$pattern.res2"
53 grep '[Ee]rror' "sav/$pattern.sav" >/dev/null && echo "Error" >> "out/$pattern.sav2"
54 diff -u "out/$pattern.res2" "out/$pattern.sav2" > "out/$pattern.diff.sav2.log"
55 if [ "$?" == 0 ]; then
56 SOSO=OK
57 else
58 SOSO=NOK
59 fi
60 fi
61 if [ -r "sav/$pattern.fail" ]; then
62 diff -u "out/$pattern.res" "sav/$pattern.fail" > "out/$pattern.diff.fail.log"
63 if [ "$?" == 0 ]; then
64 FAIL=OK
65 else
66 FAIL=NOK
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/$pattern.fail" > "out/$pattern.fail2"
70 grep '[Ee]rror' "out/$pattern.res" >/dev/null && echo "Error" >> "out/$pattern.res2"
71 grep '[Ee]rror' "sav/$pattern.fail" >/dev/null && echo "Error" >> "out/$pattern.fail2"
72 diff -u "out/$pattern.res2" "out/$pattern.fail2" > "out/$pattern.diff.fail2.log"
73 if [ "$?" == 0 ]; then
74 SOSOF=OK
75 else
76 SOSOF=NOK
77 fi
78 fi
79 if [ "x$SAV" = "xOK" ]; then
80 if [ "x$FAIL" = "x" ]; then
81 echo "[ok] out/$pattern.res"
82 else
83 echo "[ok] out/$pattern.res - but sav/$pattern.fail remains!"
84 fi
85 ok="$ok $pattern"
86 elif [ "x$FAIL" = "xOK" ]; then
87 echo "[fail] out/$pattern.res"
88 ok="$ok $pattern"
89 elif [ "x$SOSO" = "xOK" ]; then
90 echo "[soso] out/$pattern.res sav/$pattern.sav"
91 ok="$ok $pattern"
92 elif [ "x$SOSOF" = "xOK" ]; then
93 echo "[fail soso] out/$pattern.res sav/$pattern.fail"
94 ok="$ok $pattern"
95 elif [ "x$SAV" = "xNOK" ]; then
96 echo "[======= fail out/$pattern.res sav/$pattern.sav =======]"
97 nok="$nok $pattern"
98 echo "$ii" >> "$ERRLIST"
99 elif [ "x$FAIL" = "xNOK" ]; then
100 echo "[======= changed out/$pattern.res sav/$pattern.fail ======]"
101 nok="$nok $pattern"
102 echo "$ii" >> "$ERRLIST"
103 else
104 echo "[=== no sav ===] out/$pattern.res"
105 nos="$nos $pattern"
106 fi
107 }
108
109 find_nitc()
110 {
111 recent=`ls -t ../src/nitc ../src/nitc_[0-9] ../bin/nitc ../c_src/nitc 2>/dev/null | head -1`
112 if [[ "x$recent" == "x" ]]; then
113 echo 'Could not find nitc, aborting'
114 exit 1
115 fi
116 echo 'Using nitc from: '$recent
117 NITC=$recent
118 }
119
120 # The default nitc compiler
121 [ -z "$NITC" ] && find_nitc
122
123 # Set NIT_DIR if needed
124 [ -z "$NIT_DIR" ] && export NIT_DIR=..
125
126 verbose=false
127 stop=false
128 while [ $stop = false ]; do
129 case $1 in
130 -o) OPT="$OPT $2"; shift; shift;;
131 -v) verbose=true; shift;;
132 -h) usage; exit;;
133 *) stop=true
134 esac
135 done
136
137 # Mark to distinguish files among tests
138 # MARK=
139
140 # File where error tests are outputed
141 # Old ERRLIST is backuped
142 ERRLIST=${ERRLIST:-errlist}
143 ERRLIST_TARGET=$ERRLIST
144
145 if [ $# = 0 ]; then
146 usage;
147 exit
148 fi
149
150 # Initiate new ERRLIST
151 if [ "x$ERRLIST" = "x" ]; then
152 ERRLIST=/dev=null
153 else
154 ERRLIST=$ERRLIST.tmp
155 > "$ERRLIST"
156 fi
157
158 ok=""
159 nok=""
160
161 # CLEAN the out directory
162 rm -rf out/ 2>/dev/null
163 mkdir out 2>/dev/null
164
165 for ii in "$@"; do
166 if [ ! -f $ii ]; then
167 echo "File '$ii' does not exist."
168 continue
169 fi
170
171 tmp=${ii/../AA}
172 if [ "x$tmp" = "x$ii" ]; then
173 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
174 else
175 includes="-I alt"
176 fi
177
178 f=`basename "$ii" .nit`
179 for i in "$ii" `./alterner.pl --start '#' --altsep '_' $ii`; do
180 bf=`basename $i .nit`
181 ff="out/$bf"
182 echo -n "=> $bf: "
183
184 if [ -f "$f.inputs" ]; then
185 inputs="$f.inputs"
186 else
187 inputs=/dev/null
188 fi
189
190 # Compile
191 if [ "x$verbose" = "xtrue" ]; then
192 echo ""
193 echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes"
194 fi
195 $NITC --no-color $OPT -o "$ff.bin" "$i" $includes <"$inputs" 2> "$ff.cmp.err" > "$ff.compile.log"
196 ERR=$?
197 if [ "x$verbose" = "xtrue" ]; then
198 cat "$ff.compile.log"
199 cat >&2 "$ff.cmp.err"
200 fi
201 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
202 if [ "$ERR" != 0 ]; then
203 echo -n "! "
204 cat "$ff.cmp.err" "$ff.compile.log" > "$ff.res"
205 process_result $bf
206 elif [ -x "./$ff.bin" ]; then
207 cp "$ff.cmp.err" "$ff.res"
208 echo -n ". "
209 # Execute
210 args=""
211 if [ "x$verbose" = "xtrue" ]; then
212 echo ""
213 echo "NIT_NO_STACK=1 ./$ff.bin" $args
214 fi
215 NIT_NO_STACK=1 "./$ff.bin" $args < "$inputs" >> "$ff.res" 2>"$ff.err"
216 if [ "x$verbose" = "xtrue" ]; then
217 cat "$ff.res"
218 cat >&2 "$ff.err"
219 fi
220 if [ -f "$ff.write" ]; then
221 cat "$ff.write" >> "$ff.res"
222 elif [ -d "$ff.write" ]; then
223 LANG=C /bin/ls -F $ff.write >> "$ff.res"
224 fi
225 if [ -s "$ff.err" ]; then
226 cat "$ff.err" >> "$ff.res"
227 fi
228 process_result $bf
229
230 if [ -f "$f.args" ]; then
231 fargs=$f.args
232 cptr=0
233 while read line; do
234 ((cptr=cptr+1))
235 args="$line"
236 bff=$bf"_args"$cptr
237 fff=$ff"_args"$cptr
238 rm -rf "$fff.res" "$fff.err" "$fff.write" 2> /dev/null
239 if [ "x$verbose" = "xtrue" ]; then
240 echo ""
241 echo "NIT_NO_STACK=1 ./$ff.bin" $args
242 fi
243 echo -n "==> args #"$cptr " "
244 sh -c "NIT_NO_STACK=1 ./$ff.bin ''$args < $inputs > $fff.res 2>$fff.err"
245 if [ "x$verbose" = "xtrue" ]; then
246 cat "$fff.res"
247 cat >&2 "$fff.err"
248 fi
249 if [ -f "$fff.write" ]; then
250 cat "$fff.write" >> "$fff.res"
251 elif [ -d "$fff.write" ]; then
252 LANG=C /bin/ls -F $fff.write >> "$fff.res"
253 fi
254 if [ -s "$fff.err" ]; then
255 cat "$fff.err" >> "$fff.res"
256 fi
257 process_result $bff
258 done < $fargs
259 fi
260 else
261 echo -n "! "
262 cat "$ff.cmp.err" "$ff.compile.log" > "$ff.res"
263 #echo "Compilation error" > "$ff.res"
264 process_result $bf
265 fi
266 done
267 done
268
269 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
270
271 if [ -n "$nok" ]; then
272 echo "fail: $nok"
273 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
274 fi
275 if [ -n "$nos" ]; then
276 echo "no sav: $nos"
277 fi
278
279 # write $ERRLIST
280 if [ "x$ERRLIST" != "x" ]; then
281 if [ -x "$ERRLIST_TARGET" ]; then
282 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
283 fi
284 mv $ERRLIST $ERRLIST_TARGET
285 fi
286
287 if [ -n "$nok" ]; then
288 exit 1
289 else
290 exit 0
291 fi