The runtests script was improved so that the Java and Ruby test cases don't
[external/ragel.git] / test / runtests
1 #!/bin/bash
2
3 #
4 #   Copyright 2006 Adrian Thurston <thurston@cs.queensu.ca>
5 #
6
7 #   This file is part of Ragel.
8 #
9 #   Ragel is free software; you can redistribute it and/or modify
10 #   it under the terms of the GNU General Public License as published by
11 #   the Free Software Foundation; either version 2 of the License, or
12 #   (at your option) any later version.
13 #
14 #   Ragel is distributed in the hope that it will be useful,
15 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #   GNU General Public License for more details.
18 #
19 #   You should have received a copy of the GNU General Public License
20 #   along with Ragel; if not, write to the Free Software
21 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
22
23 while getopts "gcnmleT:F:G:P:CDJR" opt; do
24         case $opt in
25                 T|F|G|P) 
26                         genflags="$genflags -$opt$OPTARG"
27                         options="$options -$opt$OPTARG"
28                         ;;
29                 n|m|l|e) 
30                         minflags="$minflags -$opt"
31                         options="$options -$opt"
32                         ;;
33                 c) 
34                         compile_only="true"
35                         options="$options -$opt"
36                         ;;
37                 g) 
38                         allow_generated="true"
39                         ;;
40                 C|D|J|R) 
41                         langflags="$langflags -$opt"
42                         ;;
43         esac
44 done
45
46 [ -z "$minflags" ] && minflags="-n -m -l -e"
47 [ -z "$genflags" ] && genflags="-T0 -T1 -F0 -F1 -G0 -G1 -G2"
48 [ -z "$langflags" ] && langflags="-C -D -J -R"
49
50 shift $((OPTIND - 1));
51
52 [ -z "$*" ] && set -- *.rl
53
54 config=../common/config.h
55 ragel=../ragel/ragel
56
57 cxx_compiler=`sed '/^#define CXX/s/#define CXX *//p;d' $config`
58 c_compiler=`sed '/^#define CC/s/#define CC *//p;d' $config`
59 objc_compiler=`sed '/^#define GOBJC/s/#define GOBJC *//p;d' $config`
60 d_compiler=`sed '/^#define GDC/s/#define GDC *//p;d' $config`
61 java_compiler=`sed '/#define JAVAC/s/#define JAVAC *//p;d' $config`
62 txl_engine=`sed '/^#define TXL/s/#define TXL *//p;d' $config`
63 ruby_engine=`sed '/^#define RUBY/s/#define RUBY *//p;d' $config`
64
65 function test_error
66 {
67         exit 1;
68 }
69
70 for test_case; do
71         root=${test_case%.rl};
72
73         if ! [ -f "$test_case" ]; then
74                 echo "runtests: not a file: $test_case"; >&2
75                 exit 1;
76         fi
77
78         # Check if we should ignore the test case
79         ignore=`sed '/@IGNORE:/s/^.*: *//p;d' $test_case`
80     if [ "$ignore" = yes ]; then
81         continue;
82     fi
83
84         # If the generated flag is given make sure that the test case is generated.
85         is_generated=`sed '/@GENERATED:/s/^.*: *//p;d' $test_case`
86         if [ "$is_generated" = yes ] && [ "$allow_generated" != true ]; then
87                 continue;
88         fi
89
90         expected_out=$root.exp;
91         sed '1,/_____OUTPUT_____/d;$d' $test_case > $expected_out
92
93         lang=`sed '/@LANG:/s/^.*: *//p;d' $test_case`
94         if [ -z "$lang" ]; then
95                 echo "$test_case: language unset"; >&2
96                 exit 1;
97         fi
98
99         case $lang in
100                 c++)
101                         lang_opt=-C;
102                         codegen=../rlgen-cd/rlgen-cd;
103                         code_suffix=cpp;
104                         compiler=$cxx_compiler;
105                         cflags="-pedantic -ansi -Wall -O3"
106                 ;;
107                 d)
108                         lang_opt=-D;
109                         codegen=../rlgen-cd/rlgen-cd;
110                         code_suffix=d;
111                         compiler=$d_compiler;
112                         cflags="-Wall -O3"
113                 ;;
114                 c)
115                         lang_opt=-C;
116                         codegen=../rlgen-cd/rlgen-cd;
117                         code_suffix=c;
118                         compiler=$c_compiler;
119                         cflags="-pedantic -ansi -Wall -O3"
120                 ;;
121                 obj-c)
122                         lang_opt=-C;
123                         codegen=../rlgen-cd/rlgen-cd;
124                         code_suffix=m;
125                         compiler=$objc_compiler
126                         cflags="-Wall -O3 -fno-strict-aliasing -lobjc"
127                 ;;
128                 java)
129                         lang_opt=-J;
130                         codegen=../rlgen-java/rlgen-java;
131                         code_suffix=java;
132                         compiler=$java_compiler
133                         cflags=""
134                 ;;
135                 ruby)
136                         lang_opt=-R;
137                         codegen=../rlgen-ruby/rlgen-ruby;
138                         code_suffix=rb;
139                         compiler=$ruby_engine
140                         cflags=""
141                 ;;
142                 indep)
143                         lang_opt="";
144
145                         # If we have no txl engine then skip this test.
146                         [ -z "$txl_engine" ] && continue
147                         for lang in c d java ruby; do
148                                 case $lang in 
149                                         c) lf="-C";;
150                                         d) lf="-D";;
151                                         java) lf="-J";;
152                                         ruby) lf="-R";;
153                                 esac
154
155                                 echo "$langflags" | grep -e $lf >/dev/null || continue
156
157                                 targ=${root}_$lang.rl
158                                 echo "./langtrans_$lang.sh $test_case > $targ"
159                                 if ! ./langtrans_$lang.sh $test_case > $targ; then
160                                         test_error
161                                 fi
162                                 echo "./runtests -g $options $targ"
163                                 if !  ./runtests -g $options $targ; then
164                                         test_error
165                                 fi
166                         done
167                         continue;
168                 ;;
169                 *)
170                         echo "$test_case: unknown language type $lang" >&2
171                         exit 1;
172                 ;;
173         esac
174
175         # Make sure that we are interested in the host language.
176         echo "$langflags" | grep -e $lang_opt >/dev/null || continue
177
178         code_src=$root.$code_suffix;
179         binary=$root.bin;
180         output=$root.out;
181
182         # If we have no compiler for the source program then skip it.
183         [ -z "$compiler" ] && continue
184
185         additional_cflags=`sed '/@CFLAGS:/s/^.*: *//p;d' $test_case`
186         [ -n "$additional_cflags" ] && cflags="$cflags $additional_cflags"
187
188         allow_minflags=`sed '/@ALLOW_MINFLAGS:/s/^.*: *//p;d' $test_case`
189         [ -z "$allow_minflags" ] && allow_minflags="-n -m -l -e"
190
191
192         # If we are in java or ruby then we are not interested in genflags
193         case $lang in
194         c|c++|objc|d)
195                 # Using genflags, get the allowed gen flags from the test case. If the
196                 # test case doesn't specify assume that all gen flags are allowed.
197                 allow_genflags=`sed '/@ALLOW_GENFLAGS:/s/^.*: *//p;d' $test_case`
198                 [ -z "$allow_genflags" ] && allow_genflags="-T0 -T1 -F0 -F1 -G0 -G1 -G2"
199         ;;
200         *)
201                 # Not interested in gen flags.
202                 allow_genflags=noflags
203                 genflags=noflags
204         ;;
205         esac
206
207         for min_opt in $minflags; do
208                 for gen_opt in $genflags; do
209                         echo "$allow_minflags" | grep -e $min_opt >/dev/null || continue
210
211                         grep_gen_opt=${gen_opt}
212                         split_iters=${gen_opt#-P}
213                         if test $split_iters != $gen_opt; then
214                                 grep_gen_opt="-P";
215                         fi
216                         echo "$allow_genflags" | grep -e $grep_gen_opt >/dev/null || continue
217
218                         if [ $genflags == noflags ]; then
219                                 be_gen_opt=""
220                         else
221                                 be_gen_opt=$gen_opt
222                         fi
223
224                         echo "$ragel $min_opt $lang_opt $test_case | $codegen $be_gen_opt -o $code_src"
225                         if ! $ragel $min_opt $lang_opt $test_case | $codegen $be_gen_opt -o $code_src; then
226                                 test_error;
227                         fi
228
229                         split_objs=""
230                         if test $split_iters != "$gen_opt"; then
231                                 n=0;
232                                 while test $n -lt $split_iters; do
233                                         part_root=${root}_`awk 'BEGIN {
234                                                 width = 0;
235                                                 high = '$split_iters' - 1;
236                                                 while ( high > 0 ) {
237                                                         width = width + 1;
238                                                         high = int(high / 10);
239                                                 }
240                                                 suffFormat = "%" width "." width "d\n";
241                                                 printf( suffFormat, '$n' );
242                                                 exit 0;
243                                         }'`
244                                         part_src=${part_root}.c
245                                         part_bin=${part_root}.o
246                                         echo "$compiler -c $cflags -o $part_bin $part_src"
247                                         if ! $compiler -c $cflags -o $part_bin $part_src; then
248                                                 test_error;
249                                         fi
250                                         split_objs="$split_objs $part_bin"
251                                         n=$((n+1))
252                                 done
253                         fi
254
255                         out_args=""
256                         [ $lang != java ] && out_args="-o ${binary}";
257
258                         # Ruby doesn't need to be compiled.
259                         if [ $lang != ruby ]; then
260                                 echo "$compiler ${cflags} ${out_args} ${code_src}"
261                                 if ! $compiler ${cflags} ${out_args} ${code_src}; then
262                                         test_error;
263                                 fi
264                         fi
265
266                         if [ "$compile_only" != "true" ]; then
267                                 echo -n "running $root ... ";
268                                 
269                                 exec_cmd=./$binary
270                                 [ $lang = java ] && exec_cmd="java $root"
271                                 [ $lang = ruby ] && exec_cmd="ruby ${code_src}"
272
273                                 $exec_cmd 2>&1 > $output;
274                                 if diff $expected_out $output > /dev/null; then
275                                         echo "passed";
276                                 else
277                                         echo "FAILED";
278                                         test_error;
279                                 fi;
280                         fi
281                 done
282         done
283 done