a1d0f9908b522ca4407974fcf71ad5c2cebb7a50
[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 cp "$ff.cmp.err" "$ff.res"
205 echo -n ". "
206 # Execute
207 args=""
208 if [ "x$verbose" = "xtrue" ]; then
209 echo ""
210 echo "NIT_NO_STACK=1 ./$ff.bin" $args
211 fi
212 if [ -f "$f.inputs" ]; then
213 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" >> "$ff.res" 2>"$ff.err"
214 else
215 NIT_NO_STACK=1 "./$ff.bin" $args >> "$ff.res" 2>"$ff.err"
216 fi
217 if [ "x$verbose" = "xtrue" ]; then
218 cat "$ff.res"
219 cat >&2 "$ff.err"
220 fi
221 if [ -f "$ff.write" ]; then
222 cat "$ff.write" >> "$ff.res"
223 elif [ -d "$ff.write" ]; then
224 LANG=C /bin/ls -F $ff.write >> "$ff.res"
225 fi
226 if [ -s "$ff.err" ]; then
227 cat "$ff.err" >> "$ff.res"
228 fi
229 process_result $ff
230
231 if [ -f "$f.args" ]; then
232 fargs=$f.args
233 cptr=0
234 while read line; do
235 ((cptr=cptr+1))
236 args=$line
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 if [ -f "$f.inputs" ]; then
245 NIT_NO_STACK=1 "./$ff.bin" $args < "$f.inputs" > "$fff.res" 2>"$fff.err"
246 else
247 sh -c "NIT_NO_STACK=1 ./$ff.bin ''$args > $fff.res 2>$fff.err"
248 fi
249 if [ "x$verbose" = "xtrue" ]; then
250 cat "$fff.res"
251 cat >&2 "$fff.err"
252 fi
253 if [ -f "$fff.write" ]; then
254 cat "$fff.write" >> "$fff.res"
255 elif [ -d "$fff.write" ]; then
256 LANG=C /bin/ls -F $fff.write >> "$fff.res"
257 fi
258 if [ -s "$fff.err" ]; then
259 cat "$fff.err" >> "$fff.res"
260 fi
261 process_result $fff
262 done < $fargs
263 fi
264 else
265 echo -n "! "
266 echo "Compilation error" > "$ff.res"
267 process_result $ff
268 fi
269 done
270 done
271
272 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
273
274 if [ -n "$nok" ]; then
275 echo "fail: $nok"
276 echo "There were $(echo $nok | wc -w) errors ! (see file $ERRLIST)"
277 fi
278 if [ -n "$nos" ]; then
279 echo "no sav: $nos"
280 fi
281
282 # write $ERRLIST
283 if [ "x$ERRLIST" != "x" ]; then
284 if [ -x "$ERRLIST_TARGET" ]; then
285 mv "$ERRLIST_TARGET" "${ERRLIST_TARGET}.bak"
286 fi
287 mv $ERRLIST $ERRLIST_TARGET
288 fi
289
290 if [ -n "$nok" ]; then
291 exit 1
292 else
293 exit 0
294 fi