tests: add a script to test ICode generation
[nit.git] / tests / tests_icode.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
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 usage()
19 {
20 e=`basename "$0"`
21 cat<<END
22 Usage: $e modulenames
23 END
24 }
25
26 find_nitc()
27 {
28 recent=`ls -t ../src/nitc ../src/nitc_[0-9] ../bin/nitc ../c_src/nitc 2>/dev/null | head -1`
29 if [[ "x$recent" == "x" ]]; then
30 echo 'Could not find nitc, aborting'
31 exit 1
32 fi
33 echo 'Using nitc from: '$recent
34 NITC=$recent
35 }
36
37 # The default nitc compiler
38 [ -z "$NITC" ] && find_nitc
39
40 if [ $# = 0 ]; then
41 usage;
42 exit
43 fi
44
45 ok=""
46 nok=""
47
48 for ii in "$@"; do
49 if [ ! -f $ii ]; then
50 echo "File '$ii' does not exist."
51 continue
52 fi
53
54 tmp=${ii/../AA}
55 if [ "x$tmp" = "x$ii" ]; then
56 oincludes="-I . -I ../lib/standard -I ../lib/standard/collection"
57 else
58 oincludes=""
59 fi
60
61 for alt in "" `sed -n 's/.*#!*\(alt[0-9]*\)#.*/\1/p' "$ii" | sort -u`; do
62 f=`basename "$ii" .nit`
63 d=`dirname "$ii"`
64 ff="$f"
65 i="$ii"
66 includes="$oincludes"
67
68 if [ "x$alt" != "x" ]; then
69 test -d alt || mkdir -p alt
70 i="alt/${f}_$alt.nit"
71 ff="${ff}_$alt"
72 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
73 includes="$includes -I alt"
74 fi
75 ff="$ff$MARK"
76
77 echo -n "=> $i: "
78
79 # Clean-up before compile and tests
80 rm -rf .nit_compile 2> /dev/null
81
82 # Compile
83 # The point of ICode testing is to validate analysis/optimizations
84 # Force '--global' option !
85 $NITC $OPT --global --output-format icode "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
86 ERR=$?
87 if [ "$ERR" != 0 ]; then
88 echo "! [======= fail: Compilation error =======]"
89 nok="$nok $ff"
90 else
91 TEST_FILE=$d/$ff.tests
92 if [ ! -f $TEST_FILE ]; then
93 echo ". [======= fail: Cannot open test file =======]"
94 nok="$nok $ff"
95 continue
96 fi
97 echo "."
98
99 # Execute tests
100 cptr=0
101 while IFS=, read CLASS METHOD TYPE ATTR
102 do
103 ICODE_FILE=$d/.nit_compile/$CLASS.icode
104 let cptr+=1
105 echo -n '==> Test #'$cptr' ...... '
106
107 if [ -f $ICODE_FILE ]; then
108 if [ "x$METHOD" = "xno-file" ]; then
109 # this is normal
110 echo "Failed"
111 nok="$nok $ff#$cptr"
112 continue
113 fi
114 awk '
115 BEGIN{
116 process=0
117 }
118 /'"$METHOD"'/{
119 process=1
120 }
121 /^$/ {
122 process=0
123 }
124 /'"$ATTR"'/{
125 if(process){
126 exit -3
127 }
128 }
129 ' $ICODE_FILE
130 awkstatus=$?
131
132 if [ $awkstatus -ne 0 ] ; then
133 # Found the value !
134 if [ "x$TYPE" = "xno" ]; then
135 # Should not have beed found
136 echo "Failed"
137 nok="$nok $ff#$cptr"
138 elif [ "x$TYPE" = "xhas" ]; then
139 # Should have been found
140 echo "Ok"
141 ok="$ok $ff#$cptr"
142 else
143 echo "Test file format error !!!"
144 exit -1
145 fi
146 else
147 # Value not found
148 if [ "x$TYPE" = "xno" ]; then
149 # Should not have beed found
150 echo "Ok"
151 ok="$ok $ff#$cptr"
152 elif [ "x$TYPE" = "xhas" ]; then
153 # Should have been found
154 echo "Failed"
155 nok="$nok $ff#$cptr"
156 else
157 echo "Test file format error !!!"
158 exit -1
159 fi
160 fi
161 else
162 # The class file was not found
163 if [ "x$METHOD" = "xno-file" ]; then
164 # this is normal
165 echo "Ok"
166 ok="$ok $ff#$cptr"
167 else
168 # Should have been found
169 echo "Failed"
170 nok="$nok $ff#$cptr"
171 fi
172 fi
173 done < $TEST_FILE
174 fi
175 done
176 done
177
178 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
179
180 if [ -n "$nok" ]; then
181 echo "fail: $nok"
182 echo "There were $(echo $nok | wc -w) errors !"
183 fi
184 if [ -n "$nos" ]; then
185 echo "no sav: $nos"
186 fi
187
188 if [ -n "$nok" ]; then
189 exit 1
190 else
191 exit 0
192 fi