update NOTICE and LICENSE
[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 if [ -r "sav/$pattern.sav" ]; then
42 diff -u "$pattern.res" "sav/$pattern.sav" > "$pattern.diff.sav.log"
43 if [ "$?" == 0 ]; then
44 SAV=OK
45 else
46 SAV=NOK
47 fi
48 fi
49 if [ -r "sav/$pattern.fail" ]; then
50 diff -u "$pattern.res" "sav/$pattern.fail" > "$pattern.diff.fail.log"
51 if [ "$?" == 0 ]; then
52 FAIL=OK
53 else
54 FAIL=NOK
55 fi
56 fi
57 if [ "x$SAV" = "xOK" ]; then
58 if [ "x$FAIL" = "x" ]; then
59 echo "[ok] $pattern.res"
60 else
61 echo "[ok] $pattern.res - but sav/$pattern.fail remains!"
62 fi
63 ok="$ok $pattern"
64 elif [ "x$FAIL" = "xOK" ]; then
65 echo "[fail] $pattern.res"
66 ok="$ok $pattern"
67 elif [ "x$SAV" = "xNOK" ]; then
68 echo "[======= fail $pattern.res sav/$pattern.sav =======]"
69 nok="$nok $ff"
70 echo "$ii" >> "$ERRLIST"
71 elif [ "x$FAIL" = "xNOK" ]; then
72 echo "[======= changed $pattern.res sav/$pattern.fail ======]"
73 nok="$nok $ff"
74 echo "$ii" >> "$ERRLIST"
75 else
76 echo "[=== no sav ===] $pattern.res"
77 nos="$nos $pattern"
78 fi
79 }
80
81 find_nitc()
82 {
83 recent=`ls -t ../src/nitc ../src/nitc_[0-9] ../bin/nitc ../c_src/nitc 2>/dev/null | head -1`
84 if [[ "x$recent" == "x" ]]; then
85 echo 'Could not find nitc, aborting'
86 exit 1
87 fi
88 echo 'Using nitc from: '$recent
89 NITC=$recent
90 }
91
92 make_alts0()
93 {
94 ii="$1"
95 xalt="$2"
96 fs=""
97 for alt in `sed -n "s/.*#!*\($xalt[0-9]*\)#.*/\1/p" "$ii" | sort -u`; do
98 f=`basename "$ii" .nit`
99 d=`dirname "$ii"`
100 ff="$f"
101 i="$ii"
102
103 if [ "x$alt" != "x" ]; then
104 test -d alt || mkdir -p alt
105 i="alt/${f}_$alt.nit"
106 ff="${ff}_$alt"
107 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
108 fi
109 ff="$ff$MARK"
110 fs="$fs $i"
111 done
112 echo "$fs"
113 }
114 make_alts()
115 {
116 ii="$1"
117 fs="$1"
118 for xalt in `sed -n 's/.*#!*\([0-9]*alt\)[0-9]*#.*/\1/p' "$ii" | sort -u`; do
119 fs2=""
120 for f in $fs; do
121 fs2="$fs2 `make_alts0 $f $xalt`"
122 done
123 fs="$fs $fs2"
124 done
125 echo "$fs"
126 }
127
128 # The default nitc compiler
129 [ -z "$NITC" ] && find_nitc
130
131 verbose=false
132 stop=false
133 while [ $stop = false ]; do
134 case $1 in
135 -o) OPT="$OPT $2"; shift; shift;;
136 -v) verbose=true; shift;;
137 -h) usage; exit;;
138 *) stop=true
139 esac
140 done
141
142 # Mark to distinguish files among tests
143 # MARK=
144
145 # File where error tests are outputed
146 # Old ERRLIST is backuped
147 ERRLIST=${ERRLIST:-errlist}
148 ERRLIST_TARGET=$ERRLIST
149
150 if [ $# = 0 ]; then
151 usage;
152 exit
153 fi
154
155 # Initiate new ERRLIST
156 if [ "x$ERRLIST" = "x" ]; then
157 ERRLIST=/dev=null
158 else
159 ERRLIST=$ERRLIST.tmp
160 > "$ERRLIST"
161 fi
162
163 ok=""
164 nok=""
165
166
167 for ii in "$@"; do
168 if [ ! -f $ii ]; then
169 echo "File '$ii' does not exist."
170 continue
171 fi
172
173 tmp=${ii/../AA}
174 if [ "x$tmp" = "x$ii" ]; then
175 includes="-I . -I ../lib/standard -I ../lib/standard/collection -I alt"
176 else
177 includes="-I alt"
178 fi
179
180 f=`basename "$ii" .nit`
181 for i in `make_alts $ii`; do
182 ff=`basename $i .nit`
183 echo -n "=> $ff: "
184
185 rm -rf "$ff.res" "$ff.err" "$ff.write" "$ff.bin" 2> /dev/null
186
187 # Compile
188 if [ "x$verbose" = "xtrue" ]; then
189 echo ""
190 echo $NITC --no-color $OPT -o "$ff.bin" "$i" "$includes"
191 fi
192 $NITC --no-color $OPT -o "$ff.bin" "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
193 ERR=$?
194 if [ "x$verbose" = "xtrue" ]; then
195 cat "$ff.compile.log"
196 cat >&2 "$ff.cmp.err"
197 fi
198 egrep '^[A-Z0-9_]*$' "$ff.compile.log" > "$ff.res"
199 if [ "$ERR" != 0 ]; then
200 echo -n "! "
201 cp "$ff.cmp.err" "$ff.res"
202 process_result $ff
203 elif [ -x "./$ff.bin" ]; then
204 echo -n ". "
205 # Execute
206 args=""
207 if [ "x$verbose" = "xtrue" ]; then
208 echo ""
209 echo "NIT_NO_STACK=1 ./$ff.bin" $args
210 fi
211 if [ -f "$f.inputs" ]; then
212 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" > "$ff.res" 2>"$ff.err"
213 else
214 NIT_NO_STACK=1 "./$ff.bin" $args > "$ff.res" 2>"$ff.err"
215 fi
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 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 $ff
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 fff=$ff"_args"$cptr
237 if [ "x$verbose" = "xtrue" ]; then
238 echo ""
239 echo "NIT_NO_STACK=1 ./$ff.bin" $args
240 fi
241 echo -n "==> args #"$cptr " "
242 if [ -f "$f.inputs" ]; then
243 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" > "$fff.res" 2>"$fff.err"
244 else
245 sh -c "NIT_NO_STACK=1 ./$ff.bin ''$args > $fff.res 2>$fff.err"
246 fi
247 if [ "x$verbose" = "xtrue" ]; then
248 cat "$fff.res"
249 cat >&2 "$fff.err"
250 fi
251 if [ -f "$fff.write" ]; then
252 cat "$fff.write" >> "$fff.res"
253 elif [ -d "$fff.write" ]; then
254 ls -F $fff.write >> "$fff.res"
255 fi
256 if [ -s "$fff.err" ]; then
257 cat "$fff.err" >> "$fff.res"
258 fi
259 process_result $fff
260 done < $fargs
261 fi
262 else
263 echo -n "! "
264 echo "Compilation error" > "$ff.res"
265 process_result $ff
266 fi
267 done
268 done
269
270 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
271
272 if [ -n "$nok" ]; then
273 echo "fail: $nok"
274 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
275 fi
276 if [ -n "$nos" ]; then
277 echo "no sav: $nos"
278 fi
279
280 # write $ERRLIST
281 if [ "x$ERRLIST" != "x" ]; then
282 if [ -x "$ERRLIST_TARGET" ]; then
283 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
284 fi
285 mv $ERRLIST $ERRLIST_TARGET
286 fi
287
288 if [ -n "$nok" ]; then
289 exit 1
290 else
291 exit 0
292 fi