source: tags/arb-6.0/UNIT_TESTER/sym2testcode.pl

Last change on this file was 11488, checked in by westram, 10 years ago
  • reintegrates 'tree' into 'trunk'
    • implements #417 (multifurcate tree)
    • tree display
      • adds MULTIFURC MODE
      • reordered modes (synchronizes NTREE and PARSIMONY)
    • branch analysis
      • display number of multifurcations in 'mark long branches'
      • display "in-tree-distance" and "per-species-distance"
    • added function to toggle '100%' bootstraps
    • document bug in GBT_remove_leafs (#452)
  • adds:
  • Property svn:executable set to *
File size: 10.2 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6# --------------------------------------------------------------------------------
7
8my %location      = (); # key=symbol, value=location
9my %exported      = (); # key=exported symbols
10my %simple_test   = (); # key=names of existing simple test functions; value=1
11my %postcond      = (); # key=names of existing post-condition tests; value=1
12my %skipped_test  = (); # like simple_test, but not performed (atm)
13my %test_priority = (); # key=test value=priority
14
15# --------------------------------------------------------------------------------
16
17my $warn_level = undef;
18
19# when file exists, skip slow tests (see reporter.pl@SkipSlow)
20my $skip_slow  = (-e 'skipslow.stamp');
21
22# --------------------------------------------------------------------------------
23
24sub symbol_message($$$) {
25  my ($symbol,$message,$type) = @_;
26  my $loc = $location{$symbol};
27
28  if (defined $loc) {
29    print STDERR "$loc: $type: $message ($symbol)\n";
30  }
31  else {
32    print STDERR "sym2testcode.pl: Error: Location of '$symbol' is unknown\n";
33    print STDERR "sym2testcode.pl: $type: $message ($symbol)\n";
34  }
35}
36sub symbol_warning($$) { my($symbol, $message)= @_; $warn_level==0 || symbol_message($symbol, $message, "Warning"); }
37sub symbol_error($$)   { my($symbol, $message)= @_; symbol_message($symbol, $message, "Error");   }
38
39
40sub fail_if_no_tests_defined($) {
41  my ($libname) = @_;
42  my $skipped = scalar(keys %skipped_test);
43  my $active  = scalar(keys %simple_test);
44
45  if (($skipped+$active)==0) {
46    my $makefileDefiningTests = $ENV{ARBHOME}.'/Makefile';
47    # my $makefileDefiningTests = '../Makefile';
48    my $thisTest = $libname;
49
50    $thisTest =~ s/\.(a|o|so)/.test/o;
51    $thisTest =~ s/^lib//o;
52
53    my $cmd = "grep -Hn '$thisTest' $makefileDefiningTests";
54
55    open(GREP,$cmd.'|') || die "can't execute '$cmd' (Reason: $!)";
56    my $lineCount = 0;
57    foreach (<GREP>) {
58      if (/^([^:]+:[0-9]+:)/o) {
59        my ($loc,$line) = ($1,$');
60        chomp($line);
61        print $1.' Error: No tests defined by '.$libname." (do not call this test!)\n";
62        $lineCount++;
63      }
64      else { print "unhandled grep out='$_'\n"; }
65    }
66    close(GREP);
67    if ($lineCount!=1) {
68      die "expected exactly one line from grep (got $lineCount)\n".
69        "grep-cmd was '$cmd'";
70    }
71    die "sym2testcode.pl: won't generate useless test code\n";
72  }
73
74  return $active; # return number of active tests
75}
76
77sub skip_slow_tests() {
78  foreach (keys %simple_test) {
79    if (/^TEST_SLOW_/) { $skipped_test{$_} = 1; }
80  }
81  foreach (keys %skipped_test) { delete $simple_test{$_}; }
82}
83
84sub calculate_priorities() {
85  foreach (keys %simple_test) {
86    if    (/^TEST_BASIC_/)      { $test_priority{$_} = 20; }
87    elsif (/^TEST_EARLY_/)      { $test_priority{$_} = 50; }
88    elsif (/^TEST_LATE_/)       { $test_priority{$_} = 200; }
89    elsif (/^TEST_SLOW_/)       { $test_priority{$_} = 900; }
90    elsif (/^TEST_AFTER_SLOW_/) { $test_priority{$_} = 910; }
91    elsif (/^TEST_([0-9]+)_/)   { $test_priority{$_} = $1; }
92    else                        { $test_priority{$_} = 100; }
93  }
94}
95
96sub parse($) {
97  my ($nm_output) = @_;
98  open(IN,'<'.$nm_output) || die "can't read '$nm_output' (Reason: $!)";
99
100  my $line;
101  my $lineNr=0;
102  eval {
103  LINE: while (defined ($line = <IN>)) {
104      $lineNr++;
105      if ($line =~ /^(([0-9a-f]|\s)+) (.+?) (.*)\n/o) {
106        my ($type,$rest) = ($3,$4);
107        my $symbol;
108        my $location = undef;
109        if ($rest =~ /\t/o) {
110          ($symbol,$location) = ($`,$');
111        }
112        else { # symbol w/o location
113          $symbol = $rest;
114        }
115
116        next LINE if ($symbol =~ /::/); # skip static variables and other scoped symbols
117        if ($symbol =~ /\(/o) { $symbol = $`; } # skip prototype
118        if (defined $location) { $location{$symbol} = $location; }
119
120        my $is_unit_test     = undef;
121        my $is_postcond      = undef;
122        my $is_disabled_test = undef;
123
124        if ($symbol =~ /^TEST_/o) {
125          $is_unit_test = 1;
126          if ($' =~ /^POSTCOND_/o) { $is_postcond = 1; }
127        }
128        elsif ($symbol =~ /TEST_/o) { $is_disabled_test = 1; }
129
130        my $is_global_symbol = ($type eq 'T');
131
132        if ($is_global_symbol) { $exported{$symbol} = 1; }
133
134        if ($is_unit_test) {
135          if ($is_global_symbol) {
136            if ($is_postcond) {
137              $postcond{$symbol} = 1;
138            }
139            else {
140              $simple_test{$symbol} = 1;
141            }
142          }
143          else {
144            symbol_warning($symbol, "unit-tests need global scope (type='$type' symbol='$symbol')");
145          }
146        }
147        elsif ($is_disabled_test) {
148          if ($is_global_symbol) {
149            symbol_warning($symbol, "Test looks disabled");
150            $skipped_test{$symbol} = 1; # just note down for summary
151          }
152        }
153      }
154      else {
155        die "can't parse line '$line'\n";
156      }
157    }
158  };
159  if ($@) {
160    print "$nm_output:$lineNr: $@\n";
161    die;
162  }
163
164  close(IN);
165}
166
167# --------------------------------------------------------------------------------
168
169sub filter($$) {
170  my ($expr_mod,$expr_fun) = @_;
171
172  my $reg_mod = qr/$expr_mod/;
173  my $reg_fun = qr/$expr_fun/;
174
175  my %del = ();
176  foreach my $symbol (keys %simple_test) {
177    my $loc = $location{$symbol};
178    if (defined $loc and not $loc =~ $reg_mod) { $del{$symbol} = 1; }
179    elsif (not $symbol =~ $reg_fun) { $del{$symbol} = 2; }
180    else { $del{$symbol} = 0; }
181  }
182
183  {
184    my $warned = 0;
185    foreach (sort keys %simple_test) {
186      my $del = $del{$_};
187      if (defined $del and $del==1) {
188        if ($warned==0) {
189          print "Skipped tests (restricting to modules matching '$expr_mod'):\n";
190          $warned = 1;
191        }
192        print '* '.$_."\n";
193      }
194    }
195    $warned = 0;
196    foreach (sort keys %simple_test) {
197      my $del = $del{$_};
198      if (defined $del and $del==2) {
199        if ($warned==0) {
200          print "Skipped tests (restricting to functions matching '$expr_fun'):\n";
201          $warned = 1;
202        }
203        print '* '.$_."\n";
204      }
205    }
206  }
207
208  %simple_test = map {
209    my $del = $del{$_};
210    if (not defined $del or $del==0) { $_ => $simple_test{$_}; }
211    else { ; }
212  } keys %simple_test;
213}
214
215# --------------------------------------------------------------------------------
216
217sub UT_type($) { my ($name) = @_; return 'UnitTest_'.$name; }
218sub UT_name($) { my ($name) = @_; return 'unitTest_'.$name; }
219
220sub prototype_simple($) {
221  my ($fun) = @_;
222  return 'void '.$fun.'();'."\n";
223}
224
225sub generate_table($$\%\&) {
226  my ($type,$name,$id_r,$prototyper_r) = @_;
227
228  if ($skip_slow) { skip_slow_tests(); }
229  calculate_priorities();
230
231  my @tests = sort {
232    my $prioa = $test_priority{$a};
233    my $priob = $test_priority{$b};
234
235    my $cmp = $prioa - $priob;
236
237    if ($cmp == 0) {
238      my $loca = $location{$a};
239      my $locb = $location{$b};
240      if (defined $loca) {
241        if (defined $locb) {
242          my ($fa,$la,$fb,$lb);
243          if ($loca =~ /^(.*):([0-9]+)$/) { ($fa,$la) = ($1,$2); } else { die "Invalid location '$loca'"; }
244          if ($locb =~ /^(.*):([0-9]+)$/) { ($fb,$lb) = ($1,$2); } else { die "Invalid location '$locb'"; }
245          $cmp = $fa cmp $fb;
246          if ($cmp==0) { $cmp = $la <=> $lb; }
247        }
248        else { $cmp = 1; }
249      }
250      else {
251        if (defined $locb) { $cmp = -1; }
252        else { $cmp = $a cmp $b; }
253      }
254    }
255    $cmp;
256  } keys %$id_r;
257
258  my $code = '';
259
260  # "prototypes"
261  foreach (@tests) {
262    $code .= &$prototyper_r($_);
263  }
264  $code .= "\n";
265
266  # table
267  $code .= 'static '.$type.' '.$name.'[] = {'."\n";
268  foreach (@tests) {
269    my $loc = $location{$_};
270    if (defined $loc)  { $loc = '"'.$loc.'"'; }
271    else { $loc = 'NULL'; }
272
273    $code .= '    { '.$_.', "'.$_.'", '.$loc.' },'."\n";
274  }
275  $code .= '    { NULL, NULL, NULL },'."\n";
276  $code .= '};'."\n";
277  $code .= "\n";
278
279  return $code;
280}
281
282sub create($$) {
283  my ($libname,$gen_cxx) = @_;
284  open(OUT, '>'.$gen_cxx) || die "can't write '$gen_cxx' (Reason: $!)";
285
286  my $HEAD = <<HEAD;
287#include <UnitTester.hxx>
288#include <cstdlib>
289HEAD
290
291  my $TABLES = generate_table(UT_type('simple'), UT_name('simple'), %simple_test, &prototype_simple);
292  $TABLES   .= generate_table(UT_type('simple'), UT_name('postcond'), %postcond, &prototype_simple);
293
294  my $skipped_count = scalar(keys %skipped_test);
295
296  # UnitTester is declared in UnitTester.cxx@InvokeUnitTester
297  my $UNIT_TESTER = 'UnitTester unitTester("'.$libname.'", ';
298  $UNIT_TESTER .= UT_name('simple');
299  $UNIT_TESTER .= ', '.$warn_level;
300  $UNIT_TESTER .= ', '.$skipped_count;
301  $UNIT_TESTER .= ', '.UT_name('postcond');
302  $UNIT_TESTER .= ');';
303
304  my $MAIN = '';
305  my $have_main = defined $exported{'main'};
306  if ($have_main==1) {
307    $MAIN .= "#error tested code uses main() - not possible. use ARB_main instead and link normal executable with arb_main.o\n";
308  }
309  $MAIN .= 'int main(void) {'."\n";
310  $MAIN .= '    '.$UNIT_TESTER."\n";
311  $MAIN .= '    return EXIT_SUCCESS;'."\n";
312  $MAIN .= '}'."\n";
313
314  print OUT $HEAD."\n";
315  print OUT $TABLES."\n";
316  print OUT $MAIN."\n";
317  close(OUT);
318}
319
320# --------------------------------------------------------------------------------
321
322sub main() {
323  my $args = scalar(@ARGV);
324  if ($args != 6) {
325    die("Usage: sym2testcode.pl libname restrict-mod restrict-fun nm-output gen-cxx warn-level\n".
326        "    libname        name of library to run tests for\n".
327        "    restrict-mod   regexpr to restrict to specific module in library\n".
328        "    restrict-fun   regexpr to restrict to matching test functions\n".
329        "    nm-output      output of nm\n".
330        "    gen_cxx        name of C++ file to generate\n".
331        "    warn-level     (0=quiet|1=noisy)\n".
332        "Error: Expected 5 arguments\n");
333  }
334
335  my $libname     = shift @ARGV;
336  my $restrictMod = shift @ARGV;
337  my $restrictFun = shift @ARGV;
338  my $nm_output   = shift @ARGV;
339  my $gen_cxx     = shift @ARGV;
340  $warn_level     = shift @ARGV;
341
342  parse($nm_output);
343  fail_if_no_tests_defined($libname); # comment out to disableErrorOnUnitsWithoutTests
344
345  filter($restrictMod,$restrictFun);
346  eval {
347    create($libname,$gen_cxx);
348  };
349  if ($@) {
350    my $err = "Error: Failed to generate '$gen_cxx' (Reason: $@)";
351    if (-f $gen_cxx) {
352      if (not unlink($gen_cxx)) {
353        $err .= "\nError: Failed to unlink '$gen_cxx' (Reason: $!)";
354      }
355    }
356    die $err;
357  }
358}
359main();
Note: See TracBrowser for help on using the repository browser.