source: trunk/UNIT_TESTER/sym2testcode.pl

Last change on this file was 19617, checked in by westram, 4 weeks ago
  • remove now unneeded hack (added with [19613])
  • Property svn:executable set to *
File size: 11.5 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). values: 1=slow, 2=modfilt, 3=funfilt
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
39sub fail_if_no_tests_defined($) {
40  my ($libname) = @_;
41  my $skipped = scalar(keys %skipped_test);
42  my $active  = scalar(keys %simple_test);
43
44  if (($skipped+$active)==0) {
45    my $ACCEPT_ZERO_TESTS = $ENV{ACCEPT_ZERO_TESTS}; # if this environment variable is "1" -> do not fail when no tests are defined (used to check linkage)
46
47    if ($ACCEPT_ZERO_TESTS) {
48      print "ACCEPT_ZERO_TESTS is $ACCEPT_ZERO_TESTS -> continue generating test code (to test link dependencies)\n";
49    }
50    else {
51      my $makefileDefiningTests = $ENV{ARBHOME}.'/Makefile';
52      # my $makefileDefiningTests = '../Makefile';
53      my $thisTest = $libname;
54
55      $thisTest =~ s/\.(a|o|so)/.test/o;
56      $thisTest =~ s/^lib//o;
57
58      # the following grep command tries to avoid finding e.g. 'AW_HELIX.test' when searching 'HELIX.test'.
59      my $cmd = "grep -PHn '([^a-zA-Z_]|lib)$thisTest' $makefileDefiningTests";
60
61      open(GREP,$cmd.'|') || die "failed to fork '$cmd' (Reason: $!)";
62      my $lineCount = 0;
63      foreach (<GREP>) {
64        if (/^([^:]+:[0-9]+:)/o) {
65          my ($loc,$line) = ($1,$');
66          chomp($line);
67          print $1.' Error: No tests defined by '.$libname." (do not call this test!)\n";
68          $lineCount++;
69        }
70        else { print "unhandled grep out='$_'\n"; }
71      }
72      close(GREP) || die "failed to execute '$cmd' (Reason: $! exitcode=$?)";
73      if ($lineCount!=1) {
74        die "expected exactly one line from grep (got $lineCount)\n".
75          "grep-cmd was '$cmd'";
76      }
77      die "sym2testcode.pl: won't generate useless test code\n";
78    }
79  }
80
81  return $active; # return number of active tests
82}
83
84sub skip_slow_tests() {
85  foreach (keys %simple_test) {
86    if (/^TEST_SLOW_/) { $skipped_test{$_} = 1; }
87  }
88  foreach (keys %skipped_test) { delete $simple_test{$_}; }
89}
90
91sub calculate_priorities() {
92  foreach (keys %simple_test) {
93    if    (/^TEST_BASIC_/)      { $test_priority{$_} = 20; }
94    elsif (/^TEST_EARLY_/)      { $test_priority{$_} = 50; }
95    elsif (/^TEST_LATE_/)       { $test_priority{$_} = 200; }
96    elsif (/^TEST_SLOW_/)       { $test_priority{$_} = 900; }
97    elsif (/^TEST_AFTER_SLOW_/) { $test_priority{$_} = 910; }
98    elsif (/^TEST_([0-9]+)_/)   { $test_priority{$_} = $1; }
99    else                        { $test_priority{$_} = 100; }
100  }
101}
102
103sub parse($) {
104  my ($nm_output) = @_;
105  open(IN,'<'.$nm_output) || die "can't read '$nm_output' (Reason: $!)";
106
107  my $line;
108  my $lineNr=0;
109  eval {
110  LINE: while (defined ($line = <IN>)) {
111      $lineNr++;
112      chomp($line);
113      if ($line =~ /^(([0-9a-f]|\s)+) (.+?) (.*)$/o) {
114        my ($type,$rest) = ($3,$4);
115        my $symbol;
116        my $location = undef;
117        if ($rest =~ /\t/o) {
118          ($symbol,$location) = ($`,$');
119        }
120        else { # symbol w/o location
121          $symbol = $rest;
122        }
123
124        my $symbol_with_prototype = $symbol;
125
126        next LINE if ($symbol =~ /::/); # skip static variables and other scoped symbols
127        if ($symbol =~ /\(/o) { $symbol = $`; } # skip prototype
128        if (defined $location) { $location{$symbol} = $location; }
129
130        my $is_unit_test     = undef;
131        my $is_postcond      = undef;
132        my $is_disabled_test = undef;
133
134        if ($symbol =~ /^TEST_/o) {
135          $is_unit_test = 1;
136          if ($' =~ /^POSTCOND_/o) { $is_postcond = 1; }
137        }
138        elsif ($symbol =~ /TEST_/o) {
139          if (not $` =~  /publish/) { # skip publishers
140            $is_disabled_test = 1;
141          }
142        }
143
144        my $is_global_symbol = ($type eq 'T');
145
146        if ($is_global_symbol) { $exported{$symbol} = 1; }
147
148        if ($is_unit_test) {
149          if ($is_global_symbol) {
150            if ($is_postcond) {
151              $postcond{$symbol} = 1;
152            }
153            else {
154              $simple_test{$symbol} = 1;
155            }
156          }
157          else {
158            symbol_warning($symbol, "unit-tests need global scope (type='$type' symbol='$symbol')");
159          }
160        }
161        elsif ($is_disabled_test) {
162          if ($is_global_symbol) {
163            symbol_warning($symbol, "Test looks disabled");
164            $skipped_test{$symbol} = 1; # just note down for summary
165          }
166        }
167      }
168      elsif (($line ne "\n") and ($line ne '') and
169             not ($line =~ /^[A-Za-z0-9_-]+\.o:$/) and
170             not ($line =~ /\([A-Za-z0-9_-]+\.o\):$/)) {
171        die "can't parse line '$line'\n";
172      }
173    }
174  };
175  if ($@) {
176    print "$nm_output:$lineNr: $@\n";
177    die;
178  }
179
180  close(IN);
181}
182
183# --------------------------------------------------------------------------------
184
185sub inv_expr_match($$) {
186  my ($inv,$match_result) = @_;
187  return $inv ? (not $match_result) : $match_result;
188}
189
190sub filter($$) {
191  my ($expr_mod,$expr_fun) = @_;
192
193  my $inv_mod = 0;
194  my $inv_fun = 0;
195
196  if ($expr_mod =~ /^!/) { $inv_mod = 1; $expr_mod = $'; }
197  if ($expr_fun =~ /^!/) { $inv_fun = 1; $expr_fun = $'; }
198
199  my $reg_mod = qr/$expr_mod/i;
200  my $reg_fun = qr/$expr_fun/i;
201
202  my %del = ();
203  foreach my $symbol (keys %simple_test) {
204    my $loc = $location{$symbol};
205    if (defined $loc and not inv_expr_match($inv_mod, $loc =~ $reg_mod)) { $del{$symbol} = 1; }
206    elsif (not inv_expr_match($inv_fun, $symbol =~ $reg_fun)) { $del{$symbol} = 2; }
207    else { $del{$symbol} = 0; }
208  }
209
210  {
211    my $warned = 0;
212    foreach (sort keys %simple_test) {
213      my $del = $del{$_};
214      if (defined $del and $del==1) {
215        if ($warned==0) {
216          print 'Skipped tests (restricting to modules '.($inv_mod ? 'NOT ': '')."matching '$expr_mod'):\n";
217          $warned = 1;
218        }
219        print '* '.$_."\n";
220        $skipped_test{$_} = 2;
221      }
222    }
223    $warned = 0;
224    foreach (sort keys %simple_test) {
225      my $del = $del{$_};
226      if (defined $del and $del==2) {
227        if ($warned==0) {
228          print 'Skipped tests (restricting to functions '.($inv_fun ? 'NOT ': '')."matching '$expr_fun'):\n";
229          $warned = 1;
230        }
231        print '* '.$_."\n";
232        $skipped_test{$_} = 3;
233      }
234    }
235  }
236
237  %simple_test = map {
238    my $del = $del{$_};
239    if (not defined $del or $del==0) { $_ => $simple_test{$_}; }
240    else { ; }
241  } keys %simple_test;
242}
243
244# --------------------------------------------------------------------------------
245
246sub UT_type($) { my ($name) = @_; return 'UnitTest_'.$name; }
247sub UT_name($) { my ($name) = @_; return 'unitTest_'.$name; }
248
249sub prototype_simple($) {
250  my ($fun) = @_;
251  return 'void '.$fun.'();'."\n";
252}
253
254sub generate_table($$\%\&) {
255  my ($type,$name,$id_r,$prototyper_r) = @_;
256
257  if ($skip_slow) { skip_slow_tests(); }
258  calculate_priorities();
259
260  my @tests = sort {
261    my $prioa = $test_priority{$a};
262    my $priob = $test_priority{$b};
263
264    my $cmp = $prioa - $priob;
265
266    if ($cmp == 0) {
267      my $loca = $location{$a};
268      my $locb = $location{$b};
269      if (defined $loca) {
270        if (defined $locb) {
271          my ($fa,$la,$fb,$lb);
272          if ($loca =~ /^(.*):([0-9]+)$/) { ($fa,$la) = ($1,$2); } else { die "Invalid location '$loca'"; }
273          if ($locb =~ /^(.*):([0-9]+)$/) { ($fb,$lb) = ($1,$2); } else { die "Invalid location '$locb'"; }
274          $cmp = $fa cmp $fb;
275          if ($cmp==0) { $cmp = $la <=> $lb; }
276        }
277        else { $cmp = 1; }
278      }
279      else {
280        if (defined $locb) { $cmp = -1; }
281        else { $cmp = $a cmp $b; }
282      }
283    }
284    $cmp;
285  } keys %$id_r;
286
287  my $code = '';
288
289  # "prototypes"
290  foreach (@tests) {
291    $code .= &$prototyper_r($_);
292  }
293  $code .= "\n";
294
295  # table
296  $code .= 'static '.$type.' '.$name.'[] = {'."\n";
297  foreach (@tests) {
298    my $loc = $location{$_};
299    if (defined $loc)  { $loc = '"'.$loc.'"'; }
300    else { $loc = 'NULp'; }
301
302    $code .= '    { '.$_.', "'.$_.'", '.$loc.' },'."\n";
303  }
304  $code .= '    { NULp, NULp, NULp },'."\n";
305  $code .= '};'."\n";
306  $code .= "\n";
307
308  return $code;
309}
310
311sub create($$) {
312  my ($libname,$gen_cxx) = @_;
313  open(OUT, '>'.$gen_cxx) || die "can't write '$gen_cxx' (Reason: $!)";
314
315  my $HEAD = <<HEAD;
316#include <UnitTester.hxx>
317#include <cxxforward.h>
318#include <cstdlib>
319HEAD
320
321  my $TABLES = generate_table(UT_type('simple'), UT_name('simple'), %simple_test, &prototype_simple);
322  $TABLES   .= generate_table(UT_type('simple'), UT_name('postcond'), %postcond, &prototype_simple);
323
324  my $skipped_count = scalar(keys %skipped_test);
325
326  # UnitTester is declared in UnitTester.cxx@InvokeUnitTester
327  my $UNIT_TESTER = 'UnitTester unitTester("'.$libname.'", ';
328  $UNIT_TESTER .= UT_name('simple');
329  $UNIT_TESTER .= ', '.$warn_level;
330  $UNIT_TESTER .= ', '.$skipped_count;
331  $UNIT_TESTER .= ', '.UT_name('postcond');
332  $UNIT_TESTER .= ');';
333
334  my $MAIN = '';
335  my $have_main = defined $exported{'main'};
336  if ($have_main==1) {
337    $MAIN .= "#error tested code uses main() - not possible. use ARB_main instead and link normal executable with arb_main.o\n";
338  }
339  $MAIN .= "\n";
340  $MAIN .= 'int main(void) {'."\n";
341  $MAIN .= '    '.$UNIT_TESTER."\n";
342  $MAIN .= '    return EXIT_SUCCESS;'."\n";
343  $MAIN .= '}'."\n";
344
345  print OUT $HEAD."\n";
346  print OUT $TABLES."\n";
347  print OUT $MAIN."\n";
348  close(OUT);
349}
350
351# --------------------------------------------------------------------------------
352
353sub main() {
354  my $args = scalar(@ARGV);
355  if ($args != 6) {
356    die("Usage: sym2testcode.pl libname restrict-mod restrict-fun nm-output gen-cxx warn-level\n".
357        "    libname        name of library to run tests for\n".
358        "    restrict-mod   regexpr to restrict to specific modules in library (prefix with ! to invert)\n".
359        "    restrict-fun   regexpr to restrict to matching test functions (prefix with ! to invert)\n".
360        "    nm-output      output of nm\n".
361        "    gen_cxx        name of C++ file to generate\n".
362        "    warn-level     (0=quiet|1=noisy)\n".
363        "Error: Expected 5 arguments\n");
364  }
365
366  my $libname     = shift @ARGV;
367  my $restrictMod = shift @ARGV;
368  my $restrictFun = shift @ARGV;
369  my $nm_output   = shift @ARGV;
370  my $gen_cxx     = shift @ARGV;
371  $warn_level     = shift @ARGV;
372
373  parse($nm_output);
374  fail_if_no_tests_defined($libname);
375
376  filter($restrictMod,$restrictFun);
377  eval {
378    create($libname,$gen_cxx);
379  };
380  if ($@) {
381    my $err = "Error: Failed to generate '$gen_cxx' (Reason: $@)";
382    if (-f $gen_cxx) {
383      if (not unlink($gen_cxx)) {
384        $err .= "\nError: Failed to unlink '$gen_cxx' (Reason: $!)";
385      }
386    }
387    die $err;
388  }
389}
390main();
Note: See TracBrowser for help on using the repository browser.