5 my $progname="sg_unit_extractor";
7 die "USAGE: $progname infile [outfile]\n"
8 if (scalar @ARGV == 0 || scalar @ARGV > 2);
9 my ($infile,$outfile) = @ARGV;
11 if (not defined($outfile)) {
13 $outfile =~ s/\.c$/_unit.c/;
14 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
18 my ($unit_source,$suite_name,$suite_title)=("","","");
19 my (%tests); # to detect multiple definition
20 my (@tests); # actual content
22 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
29 if (m/ifdef +SIMGRID_TEST/) {
34 if (m/endif.*SIMGRID_TEST/) {
39 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*,(.*?)\);/) { #"
40 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n"
41 if length($suite_name);
42 ($suite_name,$suite_title)=($1,$2);
46 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"
47 die "$progname: multiply defined unit in file $infile: $1\n"
48 if (defined($tests{$1}));
54 $unit_source .= $_ if $takeit;
56 close IN || die "$progname: cannot close input file '$infile': $!\n";
60 die "$progname: end of file reached in SIMGRID_TEST block.\n".
61 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
63 "#endif /* SIMGRID_TEST */\n"
66 die "$progname: no suite defined in $infile\n"
67 unless (length($suite_name));
71 my ($GENERATED)=("/*******************************/\n".
72 "/* GENERATED FILE, DO NOT EDIT */\n".
73 "/*******************************/\n\n");
75 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
77 print OUT "#include <stdio.h>\n";
78 print OUT "#include \"xbt.h\"\n";
80 print OUT "# $beginline \"$infile\" \n";
81 print OUT "$unit_source";
83 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
85 # write the main skeleton if needed
86 if (! -e "simgrid_units_main.c") {
87 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
89 print OUT "#include <stdio.h>\n\n";
90 print OUT "#include \"xbt.h\"\n\n";
91 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
92 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
93 print OUT "/* SGU: END PROTOTYPES */\n\n";
95 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
97 int main(int argc, char *argv[]) {
98 xbt_test_suite_t suite;
102 /* SGU: BEGIN SUITES DECLARATION */
103 /* SGU: END SUITES DECLARATION */
105 xbt_init(&argc,argv);
107 /* Search for the tests to do */
109 for (i=1;i<argc;i++) {
110 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
111 char *p=strchr(argv[i],'=')+1;
112 if (selection[0] == '\\0') {
113 strcpy(selection, p);
115 strcat(selection, \",\");
116 strcat(selection, p);
118 } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
119 !strncmp(argv[i],\"--dump\", strlen(\"--dump\"))) {
120 xbt_test_dump(selection);
122 } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
124 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
125 "--help: display this help\\n"
126 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
127 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
128 " --tests can be used more than once, and selection may be a comma\\n"
129 " separated list of directives.\\n\\n"
130 "Directives are of the form:\\n"
131 " [-]suitename[:unitname]\\n\\n"
132 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
133 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
134 "it applies on any unit.\\n\\n"
135 "By default, everything is enabled.\\n\\n"
136 "'all' as suite name apply to all suites.\\n\\n"
137 "Example 1: \\"-toto,+toto:tutu\\"\\n"
138 " disables the whole toto testsuite (any unit in it),\\n"
139 " then reenables the tutu unit of the toto test suite.\\n\\n"
140 "Example 2: \\"-all,+toto\\"\\n"
141 " Run nothing but the toto suite.\\n");
144 printf("testall: Unknown option: %s\\n",argv[i]);
148 /* Got all my tests to do */
150 res = xbt_test_run(selection);
155 print OUT $GENERATED;
156 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
159 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
161 my ($name,$func,$title) = @{$_};
162 print " unit $name: func=$func; title=$title\n";
165 #while (my $t = shift @tests) {
167 # add this suite to the main
169 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
173 # print "Look for proto: $_";
174 last if /SGU: BEGIN PROTOTYPES/;
177 # search my prototype
179 # print "Seek protos: $_";
180 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
183 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
185 last if /SGU: END FILE/;
187 $_ = <IN>; # pass extra blank line
189 die "this line should be blank ($_). Did you edit the file?" if /\W/;
193 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
195 my ($name,$func,$title) = @{$_};
196 $newmain .= " void $func(void);\n"
199 $newmain .= " /* SGU: END FILE */\n\n";
200 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
204 # pass remaining prototypes, search declarations
206 $newmain .= $_ unless /SGU: END PROTOTYPES/;
207 last if /SGU: BEGIN SUITES DECLARATION/;
210 ### Done with prototypes. And now, the actual code
212 # search my prototype
214 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
217 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
219 last if /SGU: END FILE/;
221 $_ = <IN>; # pass extra blank line
223 die "this line should be blank ($_). Did you edit the file?" if /\W/;
227 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
228 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
230 my ($name,$func,$title) = @{$_};
231 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
234 $newmain .= " /* SGU: END FILE */\n\n";
235 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
243 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
245 # write it back to main
246 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
248 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";