Initialize Tizen 2.3
[external/ragel.git] / test / runtests.in
1 #!/bin/bash
2
3 #
4 #   Copyright 2006-2009 Adrian Thurston <thurston@complang.org>
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:CDJRA" 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|A) 
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 -A"
49
50 shift $((OPTIND - 1));
51
52 [ -z "$*" ] && set -- *.rl
53
54 config=../ragel/config.h
55 ragel=../ragel/ragel
56
57 cxx_compiler="@CXX@"
58 c_compiler="@CC@"
59 objc_compiler="@GOBJC@"
60 d_compiler="@GDC@"
61 java_compiler="@JAVAC@"
62 txl_engine="@TXL@"
63 ruby_engine="@RUBY@"
64 csharp_compiler="@GMCS@"
65
66 function test_error
67 {
68         exit 1;
69 }
70
71 #       split_objs=""
72 #       if test $split_iters != "$gen_opt"; then
73 #               n=0;
74 #               while test $n -lt $split_iters; do
75 #                       part_root=${root}_`awk 'BEGIN {
76 #                               width = 0;
77 #                               high = '$split_iters' - 1;
78 #                               while ( high > 0 ) {
79 #                                       width = width + 1;
80 #                                       high = int(high / 10);
81 #                               }
82 #                               suffFormat = "%" width "." width "d\n";
83 #                               printf( suffFormat, '$n' );
84 #                               exit 0;
85 #                       }'`
86 #                       part_src=${part_root}.c
87 #                       part_bin=${part_root}.o
88 #                       echo "$compiler -c $cflags -o $part_bin $part_src"
89 #                       if ! $compiler -c $cflags -o $part_bin $part_src; then
90 #                               test_error;
91 #                       fi
92 #                       split_objs="$split_objs $part_bin"
93 #                       n=$((n+1))
94 #               done
95 #       fi
96
97 function run_test()
98 {
99         echo "$ragel $lang_opt $min_opt $gen_opt -o $code_src $test_case"
100         if ! $ragel $lang_opt $min_opt $gen_opt -o $code_src $test_case; then
101                 test_error;
102         fi
103
104         out_args=""
105         [ $lang != java ] && out_args="-o ${binary}";
106     [ $lang == csharp ] && out_args="-out:${binary}";
107
108         # Ruby doesn't need to be compiled.
109         if [ $lang != ruby ]; then
110                 echo "$compiler ${cflags} ${out_args} ${code_src}"
111                 if ! $compiler ${cflags} ${out_args} ${code_src}; then
112                         test_error;
113                 fi
114         fi
115
116         if [ "$compile_only" != "true" ]; then
117                 echo -n "running $root ... ";
118                 
119                 exec_cmd=./$binary
120                 [ $lang = java ] && exec_cmd="java ${root}"
121                 [ $lang = ruby ] && exec_cmd="ruby ${code_src}"
122                 [ $lang = csharp ] && [ "$csharp_compiler" = gmcs ] && exec_cmd="mono ${exec_cmd}"
123
124                 $exec_cmd 2>&1 > $output;
125                 if diff --strip-trailing-cr $expected_out $output > /dev/null; then
126                         echo "passed";
127                 else
128                         echo "FAILED";
129                         test_error;
130                 fi;
131         fi
132 }
133
134 for test_case; do
135         root=${test_case%.rl};
136
137         if ! [ -f "$test_case" ]; then
138                 echo "runtests: not a file: $test_case"; >&2
139                 exit 1;
140         fi
141
142         # Check if we should ignore the test case
143         ignore=`sed '/@IGNORE:/s/^.*: *//p;d' $test_case`
144     if [ "$ignore" = yes ]; then
145         continue;
146     fi
147
148         # If the generated flag is given make sure that the test case is generated.
149         is_generated=`sed '/@GENERATED:/s/^.*: *//p;d' $test_case`
150         if [ "$is_generated" = yes ] && [ "$allow_generated" != true ]; then
151                 continue;
152         fi
153
154         expected_out=$root.exp;
155         sed '1,/_____OUTPUT_____/d;$d' $test_case > $expected_out
156
157         lang=`sed '/@LANG:/s/^.*: *//p;d' $test_case`
158         if [ -z "$lang" ]; then
159                 echo "$test_case: language unset"; >&2
160                 exit 1;
161         fi
162
163         case $lang in
164                 c++)
165                         lang_opt=-C;
166                         code_suffix=cpp;
167                         compiler=$cxx_compiler;
168                         cflags="-pedantic -ansi -Wall -O3"
169                 ;;
170                 d)
171                         lang_opt=-D;
172                         code_suffix=d;
173                         compiler=$d_compiler;
174                         cflags="-Wall -O3"
175                 ;;
176                 c)
177                         lang_opt=-C;
178                         code_suffix=c;
179                         compiler=$c_compiler;
180                         cflags="-pedantic -ansi -Wall -O3"
181                 ;;
182                 obj-c)
183                         lang_opt=-C;
184                         code_suffix=m;
185                         compiler=$objc_compiler
186                         cflags="-Wall -O3 -fno-strict-aliasing -lobjc"
187                 ;;
188                 java)
189                         lang_opt=-J;
190                         code_suffix=java;
191                         compiler=$java_compiler
192                         cflags=""
193                 ;;
194                 ruby)
195                         lang_opt=-R;
196                         code_suffix=rb;
197                         compiler=$ruby_engine
198                         cflags=""
199                 ;;
200         csharp)
201             lang_opt="-A";
202             code_suffix=cs;
203             compiler=$csharp_compiler
204             cflags=""
205         ;;
206                 indep)
207                         lang_opt="";
208
209                         # If we have no txl engine then skip this test.
210                         [ -z "$txl_engine" ] && continue
211                         for lang in c d java ruby csharp; do
212                                 case $lang in 
213                                         c) lf="-C";;
214                                         d) lf="-D";;
215                                         java) lf="-J";;
216                                         ruby) lf="-R";;
217                     csharp) lf="-A";;
218                                 esac
219
220                                 echo "$langflags" | grep -e $lf >/dev/null || continue
221
222                                 targ=${root}_$lang.rl
223                                 echo "./langtrans_$lang.sh $test_case > $targ"
224                                 if ! ./langtrans_$lang.sh $test_case > $targ; then
225                                         test_error
226                                 fi
227                                 echo "./runtests -g $options $targ"
228                                 if !  ./runtests -g $options $targ; then
229                                         test_error
230                                 fi
231                         done
232                         continue;
233                 ;;
234                 *)
235                         echo "$test_case: unknown language type $lang" >&2
236                         exit 1;
237                 ;;
238         esac
239
240         # Make sure that we are interested in the host language.
241         echo "$langflags" | grep -e $lang_opt >/dev/null || continue
242
243         code_src=$root.$code_suffix;
244         binary=$root.bin;
245         output=$root.out;
246
247         # If we have no compiler for the source program then skip it.
248         [ -z "$compiler" ] && continue
249
250         additional_cflags=`sed '/@CFLAGS:/s/^.*: *//p;d' $test_case`
251         [ -n "$additional_cflags" ] && cflags="$cflags $additional_cflags"
252
253         allow_minflags=`sed '/@ALLOW_MINFLAGS:/s/^.*: *//p;d' $test_case`
254         [ -z "$allow_minflags" ] && allow_minflags="-n -m -l -e"
255
256         case $lang in
257         c|c++|d)
258                 # Using genflags, get the allowed gen flags from the test case. If the
259                 # test case doesn't specify assume that all gen flags are allowed.
260                 allow_genflags=`sed '/@ALLOW_GENFLAGS:/s/^.*: *//p;d' $test_case`
261                 [ -z "$allow_genflags" ] && allow_genflags="-T0 -T1 -F0 -F1 -G0 -G1 -G2"
262
263                 for min_opt in $minflags; do
264                         echo "$allow_minflags" | grep -e $min_opt >/dev/null || continue
265                         for gen_opt in $genflags; do
266                                 echo "$allow_genflags" | grep -e $gen_opt >/dev/null || continue
267                                 run_test
268                         done
269                 done
270         ;;
271
272         java) 
273                 # Not interested in gen opt.
274                 gen_opt=""
275                 for min_opt in $minflags; do
276                         echo "$allow_minflags" | grep -e $min_opt >/dev/null || continue
277                         run_test
278                 done
279         ;;
280
281         ruby) 
282                 # Using genflags, get the allowed gen flags from the test case. If the
283                 # test case doesn't specify assume that all gen flags are allowed.
284                 allow_genflags=`sed '/@ALLOW_GENFLAGS:/s/^.*: *//p;d' $test_case`
285                 [ -z "$allow_genflags" ] && allow_genflags="-T0 -T1 -F0 -F1"
286
287                 for min_opt in $minflags; do
288                         echo "$allow_minflags" | grep -e $min_opt >/dev/null || continue
289
290                         for gen_opt in $genflags; do
291                                 echo "$allow_genflags" | grep -e $gen_opt >/dev/null || continue
292                                 run_test
293                         done
294                 done
295         ;;
296
297         csharp)
298                 # Using genflags, get the allowed gen flags from the test case. If the
299                 # test case doesn't specify assume that all gen flags are allowed.
300                 allow_genflags=`sed '/@ALLOW_GENFLAGS:/s/^.*: *//p;d' $test_case`
301                 [ -z "$allow_genflags" ] && allow_genflags="-T0 -T1 -F0 -F1 -G0 -G1"
302
303                 for min_opt in $minflags; do
304                         echo "$allow_minflags" | grep -e $min_opt >/dev/null || continue
305                         for gen_opt in $genflags; do
306                                 echo "$allow_genflags" | grep -e $gen_opt >/dev/null || continue
307                                 run_test
308                         done
309                 done
310         ;;
311         esac
312
313 done