update NOTICE and LICENSE
[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 # Process each files given as arguments
49 for ii in "$@"; do
50 if [ ! -f $ii ]; then
51 echo "File '$ii' does not exist."
52 continue
53 fi
54
55 # Prepare the includes
56 tmp=${ii/../AA}
57 if [ "x$tmp" = "x$ii" ]; then
58 oincludes="-I . -I ../lib/standard -I ../lib/standard/collection"
59 else
60 oincludes=""
61 fi
62
63 # Process each alternatives in the current file
64 for alt in "" `sed -n 's/.*#!*\(alt[0-9]*\)#.*/\1/p' "$ii" | sort -u`; do
65 f=`basename "$ii" .nit`
66 d=`dirname "$ii"`
67 ff="$f"
68 i="$ii"
69 includes="$oincludes"
70
71 if [ "x$alt" != "x" ]; then
72 test -d alt || mkdir -p alt
73 i="alt/${f}_$alt.nit"
74 ff="${ff}_$alt"
75 sed "s/#$alt#//g;/#!$alt#/d" "$ii" > "$i"
76 includes="$includes -I alt"
77 fi
78 ff="$ff$MARK"
79
80 echo -n "=> $i: "
81
82 # Clean-up before compile and tests
83 rm -rf .nit_compile 2> /dev/null
84
85 # Compile
86 # The point of ICode testing is to validate analysis/optimizations
87 # Force '--global' option !
88 $NITC $OPT --global --output-format icode "$i" $includes 2> "$ff.cmp.err" > "$ff.compile.log"
89 ERR=$?
90 if [ "$ERR" != 0 ]; then
91 # Could not compile
92 echo "! [======= fail: Compilation error =======]"
93 nok="$nok $ff"
94 else
95 TEST_FILE=$d/$ff.tests
96 if [ ! -f $TEST_FILE ]; then
97 # Could not find the .tests file associated with this test
98 echo ". [======= fail: Cannot open test file =======]"
99 nok="$nok $ff"
100 continue
101 fi
102 echo "."
103
104 # Execute tests
105 # Each lines in the .tests file describe a test to execute on the generated
106 # ICode. The file has 4 parameters:
107 # - The class
108 # - The method
109 # - The type of test
110 # - Arguments for the test
111 # We use the class to open the right icode file.
112 # We use a special method named "no-file" to ensure that the class file does not exist.
113 # If the method is different from "no-file" then we search in said method for the expression given
114 # with the _arguments_ field. This expression will be evaluated as a awk regexp.
115 # We have two types of tests :
116 # - has : the given argument must be found for the test to be successful
117 # - no : the given argument must NOT be found for the test to be successful
118 cptr=0
119 while IFS=, read CLASS METHOD TYPE ATTR
120 do
121 ICODE_FILE=$d/.nit_compile/$CLASS.icode
122 let cptr+=1
123 echo -n '==> Test #'$cptr' ...... '
124
125 if [ -f $ICODE_FILE ]; then
126 # Check if the file should exist
127 if [ "x$METHOD" = "xno-file" ]; then
128 # this is not normal, the file should not exist
129 echo "Failed"
130 nok="$nok $ff#$cptr"
131 continue
132 fi
133
134 # Use awk to get the method in the file and search in it for the argument
135 awk '
136 BEGIN{
137 process=0
138 }
139 /'"$METHOD"'/{
140 process=1
141 }
142 /^$/ {
143 process=0
144 }
145 /'"$ATTR"'/{
146 if(process){
147 exit -3
148 }
149 }
150 ' $ICODE_FILE
151 awkstatus=$?
152
153 if [ $awkstatus -ne 0 ] ; then
154 # Found the value !
155 if [ "x$TYPE" = "xno" ]; then
156 # Should not have beed found
157 echo "Failed"
158 nok="$nok $ff#$cptr"
159 elif [ "x$TYPE" = "xhas" ]; then
160 # Should have been found
161 echo "Ok"
162 ok="$ok $ff#$cptr"
163 else
164 echo "Test file format error !!!"
165 exit -1
166 fi
167 else
168 # Value not found
169 if [ "x$TYPE" = "xno" ]; then
170 # Should not have beed found
171 echo "Ok"
172 ok="$ok $ff#$cptr"
173 elif [ "x$TYPE" = "xhas" ]; then
174 # Should have been found
175 echo "Failed"
176 nok="$nok $ff#$cptr"
177 else
178 echo "Test file format error !!!"
179 exit -1
180 fi
181 fi
182 else
183 # The class file was not found
184 if [ "x$METHOD" = "xno-file" ]; then
185 # this is normal
186 echo "Ok"
187 ok="$ok $ff#$cptr"
188 else
189 # Should have been found
190 echo "Failed"
191 nok="$nok $ff#$cptr"
192 fi
193 fi
194 done < $TEST_FILE
195 fi
196 done
197 done
198
199 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
200
201 if [ -n "$nok" ]; then
202 echo "fail: $nok"
203 echo "There were $(echo $nok | wc -w) errors !"
204 fi
205 if [ -n "$nos" ]; then
206 echo "no sav: $nos"
207 fi
208
209 if [ -n "$nok" ]; then
210 exit 1
211 else
212 exit 0
213 fi