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