Merged the FsmCodeGen, TabCodeGen and JavaTabCodeGen classes. Updated the
[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:CDJ" 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) 
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"
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
64 function test_error
65 {
66         exit 1;
67 }
68
69 for test_case; do
70         root=${test_case%.rl};
71
72         if ! [ -f "$test_case" ]; then
73                 echo "runtests: not a file: $test_case"; >&2
74                 exit 1;
75         fi
76
77         # Check if we should ignore the test case
78         ignore=`sed '/@IGNORE:/s/^.*: *//p;d' $test_case`
79     if [ "$ignore" = yes ]; then
80         continue;
81     fi
82
83         # If the generated flag is given make sure that the test case is generated.
84         is_generated=`sed '/@GENERATED:/s/^.*: *//p;d' $test_case`
85         if [ "$is_generated" = yes ] && [ "$allow_generated" != true ]; then
86                 continue;
87         fi
88
89         expected_out=$root.exp;
90         sed '1,/_____OUTPUT_____/d;$d' $test_case > $expected_out
91
92         lang=`sed '/@LANG:/s/^.*: *//p;d' $test_case`
93         if [ -z "$lang" ]; then
94                 echo "$test_case: language unset"; >&2
95                 exit 1;
96         fi
97
98         case $lang in
99                 c++)
100                         codegen=../rlcodegen/rlcodegen;
101                         code_suffix=cpp;
102                         compiler=$cxx_compiler;
103                         lang_opt=-C;
104                         cflags="-pedantic -ansi -Wall -O3"
105                 ;;
106                 d)
107                         codegen=../rlcodegen/rlcodegen;
108                         code_suffix=d;
109                         compiler=$d_compiler;
110                         lang_opt=-D;
111                         cflags="-Wall -O3"
112                 ;;
113                 c)
114                         codegen=../rlcodegen/rlcodegen;
115                         code_suffix=c;
116                         compiler=$c_compiler;
117                         lang_opt=-C;
118                         cflags="-pedantic -ansi -Wall -O3"
119                 ;;
120                 obj-c)
121                         codegen=../rlcodegen/rlcodegen;
122                         code_suffix=m;
123                         compiler=$objc_compiler
124                         lang_opt=-C;
125                         cflags="-Wall -O3 -fno-strict-aliasing -lobjc"
126                 ;;
127                 java)
128                         codegen=../rlgen-java/rlgen-java;
129                         code_suffix=java;
130                         compiler=$java_compiler
131                         lang_opt=-J;
132                         cflags=""
133                 ;;
134                 indep)
135                         # If we have no compiler for the source program then skip it.
136                         [ -z "$txl_engine" ] && continue
137                         for lang in c d java; do
138                                 case $lang in 
139                                         c) lf="-C";;
140                                         d) lf="-D";;
141                                         java) lf="-J";;
142                                 esac
143
144                                 echo "$langflags" | grep -e $lf >/dev/null || continue
145
146                                 targ=${root}_$lang.rl
147                                 echo "./langtrans_$lang.sh $test_case > $targ"
148                                 if ! ./langtrans_$lang.sh $test_case > $targ; then
149                                         test_error
150                                 fi
151                                 echo "./runtests -g $options $targ"
152                                 if !  ./runtests -g $options $targ; then
153                                         test_error
154                                 fi
155                         done
156                         continue;
157                 ;;
158                 *)
159                         echo "$test_case: unknown language type $lang" >&2
160                         exit 1;
161                 ;;
162         esac
163
164         # Make sure that we are interested in the host language.
165         echo "$langflags" | grep -e $lang_opt >/dev/null || continue
166
167         code_src=$root.$code_suffix;
168         binary=$root.bin;
169         output=$root.out;
170
171         # If we have no compiler for the source program then skip it.
172         [ -z "$compiler" ] && continue
173
174         additional_cflags=`sed '/@CFLAGS:/s/^.*: *//p;d' $test_case`
175         [ -n "$additional_cflags" ] && cflags="$cflags $additional_cflags"
176
177         allow_minflags=`sed '/@ALLOW_MINFLAGS:/s/^.*: *//p;d' $test_case`
178         [ -z "$allow_minflags" ] && allow_minflags="-n -m -l -e"
179
180         allow_genflags=`sed '/@ALLOW_GENFLAGS:/s/^.*: *//p;d' $test_case`
181         [ -z "$allow_genflags" ] && allow_genflags="-T0 -T1 -F0 -F1 -G0 -G1 -G2"
182
183         for min_opt in $minflags; do
184                 for gen_opt in $genflags; do
185                         echo "$allow_minflags" | grep -e $min_opt >/dev/null || continue
186
187                         grep_gen_opt=${gen_opt}
188                         split_iters=${gen_opt#-P}
189                         if test $split_iters != $gen_opt; then
190                                 grep_gen_opt="-P";
191                         fi
192                         echo "$allow_genflags" | grep -e $grep_gen_opt >/dev/null || continue
193
194                         echo "$ragel $min_opt $lang_opt $test_case | $codegen  $gen_opt -o $code_src"
195                         if ! $ragel $min_opt $lang_opt $test_case | $codegen $gen_opt -o $code_src; then
196                                 test_error;
197                         fi
198
199                         split_objs=""
200                         if test $split_iters != $gen_opt; then
201                                 n=0;
202                                 while test $n -lt $split_iters; do
203                                         part_root=${root}_`awk 'BEGIN {
204                                                 width = 0;
205                                                 high = '$split_iters' - 1;
206                                                 while ( high > 0 ) {
207                                                         width = width + 1;
208                                                         high = int(high / 10);
209                                                 }
210                                                 suffFormat = "%" width "." width "d\n";
211                                                 printf( suffFormat, '$n' );
212                                                 exit 0;
213                                         }'`
214                                         part_src=${part_root}.c
215                                         part_bin=${part_root}.o
216                                         echo "$compiler -c $cflags -o $part_bin $part_src"
217                                         if ! $compiler -c $cflags -o $part_bin $part_src; then
218                                                 test_error;
219                                         fi
220                                         split_objs="$split_objs $part_bin"
221                                         n=$((n+1))
222                                 done
223                         fi
224
225                         out_args=""
226                         [ $lang != java ] && out_args="-o ${binary}";
227
228                         echo "$compiler ${cflags} ${out_args} ${code_src}"
229                         if ! $compiler ${cflags} ${out_args} ${code_src}; then
230                                 test_error;
231                         fi
232
233                         if [ "$compile_only" != "true" ]; then
234                                 echo -n "running $root ... ";
235                                 
236                                 exec_cmd=./$binary
237                                 [ $lang = java ] && exec_cmd="java $root"
238                                         
239                                 $exec_cmd 2>&1 > $output;
240                                 if diff $expected_out $output > /dev/null; then
241                                         echo "passed";
242                                 else
243                                         echo "FAILED";
244                                         test_error;
245                                 fi;
246                         fi
247                 done
248         done
249 done