Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert config module to unit testing
[simgrid.git] / tools / sg_unit_extractor.pl
1 #! /usr/bin/perl
2
3 use strict;
4
5 my $progname="sg_unit_extractor";
6 # Get the args 
7 die "USAGE: $progname infile [outfile]\n"
8   if (scalar @ARGV == 0 || scalar @ARGV > 2);
9 my ($infile,$outfile) = @ARGV;
10
11 if (not defined($outfile)) {
12   $outfile =  $infile;
13   $outfile =~ s/\.c$/_unit.c/;
14   $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
15 }
16
17 # Get the unit data
18 my ($unit_source,$suite_name,$suite_title)=("","","");
19 my (%tests); # to detect multiple definition
20 my (@tests); # actual content
21
22 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
23
24 my $takeit=0;
25 while (<IN>) {
26   if (m/ifdef +SIMGRID_TEST/) {
27     $takeit = 1;
28     next;
29   }
30   if (m/endif.*SIMGRID_TEST/) {
31     $takeit = 0;
32     next
33   }
34
35   if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*,(.*?)\);/) { #"
36     die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" 
37       if length($suite_name);
38     ($suite_name,$suite_title)=($1,$2);
39     next;
40   } 
41   
42   if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"
43     die "$progname: multiply defined test in file $infile: $1\n"
44       if (defined($tests{$1}));
45       
46     my @t=($1,$2,$3);
47     push @tests,\@t;
48     $tests{$1} = 1;
49   }
50   $unit_source .= $_ if $takeit;
51 }
52 close IN || die "$progname: cannot close input file '$infile': $!\n";
53
54
55 if ($takeit) {
56   die "$progname: end of file reached in SIMGRID_TEST block.\n".
57      "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
58      "Example:\n".
59      "#endif /* SIMGRID_TEST */\n"
60 }
61
62 die "$progname: no suite defined in $infile\n"
63   unless (length($suite_name));
64   
65 # Write the test
66
67 my ($GENERATED)=("/*******************************/\n".
68                  "/* GENERATED FILE, DO NOT EDIT */\n".
69                  "/*******************************/\n\n");
70
71 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
72 print OUT $GENERATED;
73 print OUT "#include \"xbt.h\"\n";
74 print OUT $GENERATED;
75 print OUT "$unit_source";
76 print OUT $GENERATED;
77 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
78
79 # write the main skeleton if needed
80 if (! -e "simgrid_units_main.c") {
81   open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
82   print OUT $GENERATED;
83   print OUT "#include \"xbt.h\"\n\n";
84   print OUT "/* SGU: BEGIN PROTOTYPES */\n";
85   print OUT "/* SGU: END PROTOTYPES */\n\n";
86   print OUT $GENERATED;
87   print OUT "int main(int argc, char *argv[]) {\n";
88   print OUT "  xbt_test_suite_t suite;\n\n";
89   print OUT "  /* SGU: BEGIN SUITES DECLARATION */\n";
90   print OUT "  /* SGU: END SUITES DECLARATION */\n\n";  
91   print OUT "  return xbt_test_run();\n";
92   print OUT "}\n";
93   print OUT $GENERATED;
94   close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
95 }
96
97 print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
98 map {
99   my ($name,$func,$title) = @{$_};
100   print "    test $name: func=$func; title=$title\n";
101 } @tests;
102
103 #while (my $t = shift @tests) {
104
105 # add this suite to the main
106 my $newmain="";
107 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
108   # search prototypes
109   while (<IN>) {
110     $newmain .= $_;
111 #    print "Look for proto: $_";
112     last if /SGU: BEGIN PROTOTYPES/;
113   }
114
115   # search my prototype
116   while (<IN>) {
117 #    print "Seek protos: $_";
118     last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
119     $newmain .= $_;
120   }
121   if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
122     while (<IN>) {
123       last if /SGU: END FILE/;
124     }
125     $_ = <IN>; # pass extra blank line
126     chomp;
127     die "this line should be blank ($_). Did you edit the file?" if /\W/;
128   }
129   my ($old_)=($_);
130   # add my section
131   $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
132   map {
133     my ($name,$func,$title) = @{$_};
134     $newmain .=  "    void $func(xbt_test_unit_t _unit);\n"
135   } @tests;
136
137   $newmain .= "  /* SGU: END FILE */\n\n";
138   if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
139     $newmain .= $old_;
140   }
141
142   # pass remaining prototypes, search declarations
143   while (<IN>) {
144     $newmain .= $_ unless /SGU: END PROTOTYPES/;
145     last if /SGU: BEGIN SUITES DECLARATION/;
146   }
147
148   ### Done with prototypes. And now, the actual code
149   
150   # search my prototype
151   while (<IN>) {
152     last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
153     $newmain .= $_;
154   }
155   if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
156     while (<IN>) {
157       last if /SGU: END FILE/;
158     }
159     $_ = <IN>; # pass extra blank line
160     chomp;
161     die "this line should be blank ($_). Did you edit the file?" if /\W/;
162   }
163   my ($old_)=($_);
164   # add my section
165   $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
166   $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
167   map {
168     my ($name,$func,$title) = @{$_};
169     $newmain .=  "      xbt_test_suite_push(suite, $func, $title);\n";
170   } @tests;
171
172   $newmain .= "    /* SGU: END FILE */\n\n";
173   if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
174     $newmain .= $old_;
175   }
176
177   # pass the remaining 
178   while (<IN>) {
179     $newmain .= $_;
180   }
181 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
182
183 # write it back to main
184 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
185 print OUT $newmain;
186 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
187
188 0;