test.pl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. #
  2. # t/test.pl - from CORE
  3. use Test::More;
  4. use File::Spec;
  5. sub _where {
  6. my @caller = caller($Level);
  7. return "at $caller[1] line $caller[2]";
  8. }
  9. # runperl - Runs a separate perl interpreter.
  10. # Arguments :
  11. # switches => [ command-line switches ]
  12. # nolib => 1 # don't use -I../lib (included by default)
  13. # prog => one-liner (avoid quotes)
  14. # progs => [ multi-liner (avoid quotes) ]
  15. # progfile => perl script
  16. # stdin => string to feed the stdin
  17. # stderr => redirect stderr to stdout
  18. # args => [ command-line arguments to the perl program ]
  19. # verbose => print the command line
  20. my $is_mswin = $^O eq 'MSWin32';
  21. my $is_netware = $^O eq 'NetWare';
  22. my $is_macos = $^O eq 'MacOS';
  23. my $is_vms = $^O eq 'VMS';
  24. my $is_cygwin = $^O eq 'cygwin';
  25. sub _quote_args {
  26. my ($runperl, $args) = @_;
  27. foreach (@$args) {
  28. # In VMS protect with doublequotes because otherwise
  29. # DCL will lowercase -- unless already doublequoted.
  30. $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
  31. $$runperl .= ' ' . $_;
  32. }
  33. }
  34. sub _create_runperl { # Create the string to qx in runperl().
  35. my %args = @_;
  36. my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  37. #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind
  38. if ($ENV{PERL_RUNPERL_DEBUG}) {
  39. $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl";
  40. }
  41. unless ($args{nolib}) {
  42. if ($is_macos) {
  43. $runperl .= ' -I::lib';
  44. # Use UNIX style error messages instead of MPW style.
  45. $runperl .= ' -MMac::err=unix' if $args{stderr};
  46. }
  47. else {
  48. $runperl .= ' "-I../lib"'; # doublequotes because of VMS
  49. }
  50. }
  51. if ($args{switches}) {
  52. local $Level = 2;
  53. die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
  54. unless ref $args{switches} eq "ARRAY";
  55. _quote_args(\$runperl, $args{switches});
  56. }
  57. if (defined $args{prog}) {
  58. die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
  59. if defined $args{progs};
  60. $args{progs} = [$args{prog}]
  61. }
  62. if (defined $args{progs}) {
  63. die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
  64. unless ref $args{progs} eq "ARRAY";
  65. foreach my $prog (@{$args{progs}}) {
  66. if ($is_mswin || $is_netware || $is_vms) {
  67. $runperl .= qq ( -e "$prog" );
  68. }
  69. else {
  70. $runperl .= qq ( -e '$prog' );
  71. }
  72. }
  73. } elsif (defined $args{progfile}) {
  74. $runperl .= qq( "$args{progfile}");
  75. } else {
  76. # You probaby didn't want to be sucking in from the upstream stdin
  77. die "test.pl:runperl(): none of prog, progs, progfile, args, "
  78. . " switches or stdin specified"
  79. unless defined $args{args} or defined $args{switches}
  80. or defined $args{stdin};
  81. }
  82. if (defined $args{stdin}) {
  83. # so we don't try to put literal newlines and crs onto the
  84. # command line.
  85. $args{stdin} =~ s/\n/\\n/g;
  86. $args{stdin} =~ s/\r/\\r/g;
  87. if ($is_mswin || $is_netware || $is_vms) {
  88. $runperl = qq{$^X -e "print qq(} .
  89. $args{stdin} . q{)" | } . $runperl;
  90. }
  91. elsif ($is_macos) {
  92. # MacOS can only do two processes under MPW at once;
  93. # the test itself is one; we can't do two more, so
  94. # write to temp file
  95. my $stdin = qq{$^X -e 'print qq(} . $args{stdin} . qq{)' > teststdin; };
  96. if ($args{verbose}) {
  97. my $stdindisplay = $stdin;
  98. $stdindisplay =~ s/\n/\n\#/g;
  99. print STDERR "# $stdindisplay\n";
  100. }
  101. `$stdin`;
  102. $runperl .= q{ < teststdin };
  103. }
  104. else {
  105. $runperl = qq{$^X -e 'print qq(} .
  106. $args{stdin} . q{)' | } . $runperl;
  107. }
  108. }
  109. if (defined $args{args}) {
  110. _quote_args(\$runperl, $args{args});
  111. }
  112. $runperl .= ' 2>&1' if $args{stderr} && !$is_mswin && !$is_macos;
  113. $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
  114. if ($args{verbose}) {
  115. my $runperldisplay = $runperl;
  116. $runperldisplay =~ s/\n/\n\#/g;
  117. print STDERR "# $runperldisplay\n";
  118. }
  119. return $runperl;
  120. }
  121. sub runperl {
  122. die "test.pl:runperl() does not take a hashref"
  123. if ref $_[0] and ref $_[0] eq 'HASH';
  124. my $runperl = &_create_runperl;
  125. # ${^TAINT} is invalid in perl5.00505
  126. my $tainted;
  127. eval '$tainted = ${^TAINT};' if $] >= 5.006;
  128. my %args = @_;
  129. exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1;
  130. if ($tainted) {
  131. # We will assume that if you're running under -T, you really mean to
  132. # run a fresh perl, so we'll brute force launder everything for you
  133. my $sep;
  134. eval "require Config; Config->import";
  135. if ($@) {
  136. warn "test.pl had problems loading Config: $@";
  137. $sep = ':';
  138. } else {
  139. $sep = $Config{path_sep};
  140. }
  141. my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV);
  142. local @ENV{@keys} = ();
  143. # Untaint, plus take out . and empty string:
  144. local $ENV{'DCL$PATH'} = $1 if $is_vms && ($ENV{'DCL$PATH'} =~ /(.*)/s);
  145. $ENV{PATH} =~ /(.*)/s;
  146. local $ENV{PATH} =
  147. join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and
  148. ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) }
  149. split quotemeta ($sep), $1;
  150. $ENV{PATH} .= "$sep/bin" if $is_cygwin; # Must have /bin under Cygwin
  151. $runperl =~ /(.*)/s;
  152. $runperl = $1;
  153. my ($err,$result,$stderr) = run_cmd($runperl, $args{timeout});
  154. $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
  155. return $result;
  156. } else {
  157. my ($err,$result,$stderr) = run_cmd($runperl, $args{timeout});
  158. $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
  159. return $result;
  160. }
  161. }
  162. *run_perl = \&runperl; # Nice alias.
  163. sub DIE {
  164. print STDERR "# @_\n";
  165. exit 1;
  166. }
  167. # A somewhat safer version of the sometimes wrong $^X.
  168. my $Perl;
  169. sub which_perl {
  170. unless (defined $Perl) {
  171. $Perl = $^X;
  172. # VMS should have 'perl' aliased properly
  173. return $Perl if $^O eq 'VMS';
  174. my $exe;
  175. eval "require Config; Config->import";
  176. if ($@) {
  177. warn "test.pl had problems loading Config: $@";
  178. $exe = '';
  179. } else {
  180. $exe = $Config{exe_ext};
  181. }
  182. $exe = '' unless defined $exe;
  183. # This doesn't absolutize the path: beware of future chdirs().
  184. # We could do File::Spec->abs2rel() but that does getcwd()s,
  185. # which is a bit heavyweight to do here.
  186. if ($Perl =~ /^perl\Q$exe\E$/i) {
  187. my $perl = "perl$exe";
  188. eval "require File::Spec";
  189. if ($@) {
  190. warn "test.pl had problems loading File::Spec: $@";
  191. $Perl = "./$perl";
  192. } else {
  193. $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
  194. }
  195. }
  196. # Build up the name of the executable file from the name of
  197. # the command.
  198. if ($Perl !~ /\Q$exe\E$/i) {
  199. $Perl .= $exe;
  200. }
  201. warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
  202. # For subcommands to use.
  203. $ENV{PERLEXE} = $Perl;
  204. }
  205. return $Perl;
  206. }
  207. sub unlink_all {
  208. foreach my $file (@_) {
  209. 1 while unlink $file;
  210. print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;
  211. }
  212. }
  213. my $tmpfile = "misctmp000";
  214. 1 while -f ++$tmpfile;
  215. END { unlink_all $tmpfile }
  216. #
  217. # _fresh_perl
  218. #
  219. # The $resolve must be a subref that tests the first argument
  220. # for success, or returns the definition of success (e.g. the
  221. # expected scalar) if given no arguments.
  222. #
  223. sub _fresh_perl {
  224. my($prog, $resolve, $runperl_args, $name) = @_;
  225. $runperl_args ||= {};
  226. $runperl_args->{progfile} = $tmpfile;
  227. $runperl_args->{stderr} = 1;
  228. open TEST, ">", $tmpfile or die "Cannot open $tmpfile: $!";
  229. # VMS adjustments
  230. if( $^O eq 'VMS' ) {
  231. $prog =~ s#/dev/null#NL:#;
  232. # VMS file locking
  233. $prog =~ s{if \(-e _ and -f _ and -r _\)}
  234. {if (-e _ and -f _)}
  235. }
  236. print TEST $prog;
  237. close TEST or die "Cannot close $tmpfile: $!";
  238. my $results = runperl(%$runperl_args);
  239. my $status = $?;
  240. # Clean up the results into something a bit more predictable.
  241. $results =~ s/\n+$//;
  242. $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
  243. $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
  244. # bison says 'parse error' instead of 'syntax error',
  245. # various yaccs may or may not capitalize 'syntax'.
  246. $results =~ s/^(syntax|parse) error/syntax error/mig;
  247. if ($^O eq 'VMS') {
  248. # some tests will trigger VMS messages that won't be expected
  249. $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
  250. # pipes double these sometimes
  251. $results =~ s/\n\n/\n/g;
  252. }
  253. my $pass = $resolve->($results);
  254. unless ($pass) {
  255. diag "# PROG: \n$prog\n";
  256. diag "# EXPECTED:\n", $resolve->(), "\n";
  257. diag "# GOT:\n$results\n";
  258. diag "# STATUS: $status\n";
  259. }
  260. # Use the first line of the program as a name if none was given
  261. unless( $name ) {
  262. ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
  263. $name .= '...' if length $first_line > length $name;
  264. }
  265. ok($pass, "fresh_perl - $name");
  266. }
  267. #
  268. # fresh_perl_is
  269. #
  270. # Combination of run_perl() and is().
  271. #
  272. sub fresh_perl_is {
  273. my($prog, $expected, $runperl_args, $name) = @_;
  274. local $Level = 2;
  275. _fresh_perl($prog,
  276. sub { @_ ? $_[0] eq $expected : $expected },
  277. $runperl_args, $name);
  278. }
  279. #
  280. # fresh_perl_like
  281. #
  282. # Combination of run_perl() and like().
  283. #
  284. sub fresh_perl_like {
  285. my($prog, $expected, $runperl_args, $name) = @_;
  286. local $Level = 2;
  287. _fresh_perl($prog,
  288. sub { @_ ?
  289. $_[0] =~ (ref $expected ? $expected : /$expected/) :
  290. $expected },
  291. $runperl_args, $name);
  292. }
  293. # now my new B::C functions
  294. sub run_cmd {
  295. my ($cmd, $timeout) = @_;
  296. my ($result, $out, $err) = (0, '', '');
  297. if ( ! defined $IPC::Run::VERSION ) {
  298. local $@;
  299. if (ref($cmd) eq 'ARRAY') {
  300. $cmd = join " ", @$cmd;
  301. }
  302. # No real way to trap STDERR?
  303. $cmd .= " 2>&1" if ($^O !~ /^MSWin32|VMS/);
  304. $out = `$cmd`;
  305. $result = $?;
  306. }
  307. else {
  308. my $in;
  309. # XXX TODO this fails with spaces in path. pass and check ARRAYREF then
  310. my @cmd = ref($cmd) eq 'ARRAY' ? @$cmd : split /\s+/, $cmd;
  311. eval {
  312. # XXX TODO hanging or stacktrace'd children are not killed on cygwin
  313. my $h = IPC::Run::start(\@cmd, \$in, \$out, \$err);
  314. if ($timeout) {
  315. my $secs10 = $timeout/10;
  316. for (1..$secs10) {
  317. if(!$h->pumpable) {
  318. last;
  319. }
  320. else {
  321. $h->pump_nb;
  322. diag sprintf("waiting %d[s]",$_*10) if $_ > 30;
  323. sleep 10;
  324. }
  325. }
  326. if($h->pumpable) {
  327. $h->kill_kill;
  328. $err .= "Timed out waiting for process exit";
  329. }
  330. }
  331. $h->finish or die "cmd returned $?";
  332. $result = $h->result(0);
  333. };
  334. $err .= "\$\@ = $@" if($@);
  335. }
  336. return ($result, $out, $err);
  337. }
  338. sub Mblib {
  339. $^O eq 'MSWin32' ? '-Iblib\arch -Iblib\lib' : "-Iblib/arch -Iblib/lib";
  340. }
  341. sub tests {
  342. my $in = shift || "t/TESTS";
  343. $in = "TESTS" unless -f $in;
  344. undef $/;
  345. open TEST, "< $in" or die "Cannot open $in";
  346. my @tests = split /\n####+.*##\n/, <TEST>;
  347. close TEST;
  348. delete $tests[$#tests] unless $tests[$#tests];
  349. @tests;
  350. }
  351. sub run_cc_test {
  352. my ($cnt, $backend, $script, $expect, $keep_c, $keep_c_fail, $todo) = @_;
  353. my ($opt, $got);
  354. local($\, $,); # guard against -l and other things that screw with
  355. # print
  356. $expect =~ s/\n$//;
  357. my ($out,$result,$stderr) = ('');
  358. my $fnbackend = lc($backend); #C,-O2
  359. ($fnbackend,$opt) = $fnbackend =~ /^(cc?)(,-o.)?/;
  360. $opt =~ s/,-/_/ if $opt;
  361. $opt = '' unless $opt;
  362. use Config;
  363. require B::C::Flags;
  364. my $test = $fnbackend."code".$cnt.".pl";
  365. my $cfile = $fnbackend."code".$cnt.$opt.".c";
  366. my @obj;
  367. @obj = ($fnbackend."code".$cnt.$opt.".obj",
  368. $fnbackend."code".$cnt.$opt.".ilk",
  369. $fnbackend."code".$cnt.$opt.".pdb")
  370. if $Config{cc} =~ /^cl/i; # MSVC uses a lot of intermediate files
  371. my $exe = $fnbackend."code".$cnt.$opt.$Config{exe_ext};
  372. unlink ($test, $cfile, $exe, @obj);
  373. open T, ">", $test; print T $script; close T;
  374. # Being able to test also the CORE B in older perls
  375. my $Mblib = $] >= 5.009005 ? Mblib() : "";
  376. my $useshrplib = $Config{useshrplib} eq 'true';
  377. unless ($Mblib) { # check for -Mblib from the testsuite
  378. if (grep { m{blib(/|\\)arch$} } @INC) {
  379. $Mblib = Mblib(); # forced -Mblib via cmdline without
  380. # printing to stderr
  381. $backend = "-qq,$backend,-q" if !$ENV{TEST_VERBOSE} and $] > 5.007;
  382. }
  383. } else {
  384. $backend = "-qq,$backend,-q" if !$ENV{TEST_VERBOSE} and $] > 5.007;
  385. }
  386. $backend .= ",-fno-warnings" if $] >= 5.013005;
  387. $backend .= ",-fno-fold" if $] >= 5.013009;
  388. $got = run_perl(switches => [ "$Mblib -MO=$backend,-o${cfile}" ],
  389. verbose => $ENV{TEST_VERBOSE}, # for debugging
  390. nolib => $ENV{PERL_CORE} ? 0 : 1, # include ../lib only in CORE
  391. stderr => 1, # to capture the "ccode.pl syntax ok"
  392. timeout => 120,
  393. progfile => $test);
  394. if (! $? and -s $cfile) {
  395. use ExtUtils::Embed ();
  396. my $command = ExtUtils::Embed::ccopts;
  397. $command .= " -DHAVE_INDEPENDENT_COMALLOC "
  398. if $B::C::Flags::have_independent_comalloc;
  399. $command .= " -o $exe $cfile ".$B::C::Flags::extra_cflags . " ";
  400. if ($Config{cc} eq 'cl') {
  401. if ($^O eq 'MSWin32' and $Config{ccversion} eq '12.0.8804' and $Config{cc} eq 'cl') {
  402. $command =~ s/ -opt:ref,icf//;
  403. }
  404. my $obj = $obj[0];
  405. $command =~ s/ \Q-o $exe\E / -c -Fo$obj /;
  406. my $cmdline = "$Config{cc} $command";
  407. diag ($cmdline) if $ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE} == 2;
  408. run_cmd($cmdline, 20);
  409. $command = '';
  410. }
  411. my $coredir = $ENV{PERL_SRC} || File::Spec->catdir($Config{installarchlib}, "CORE");
  412. my $libdir = File::Spec->catdir($Config{prefix}, "lib");
  413. my $so = $Config{so};
  414. if ( -e "$coredir/$Config{libperl}" and $Config{libperl} !~ /\.$so$/) {
  415. $linkargs = ExtUtils::Embed::ldopts('-std');
  416. $command .= $linkargs;
  417. } elsif ( $useshrplib and -e "$libdir/$Config{libperl}" ) {
  418. # debian: /usr/lib/libperl.so.5.10.1 and broken ExtUtils::Embed::ldopts
  419. my $linkargs = ExtUtils::Embed::ldopts('-std');
  420. if ($Config{libperl} =~ /\.$so$/) {
  421. my $libperl = File::Spec->catfile($coredir, $Config{libperl});
  422. $linkargs =~ s|-lperl |$libperl |; # link directly
  423. }
  424. $linkargs =~ s/-fstack-protector\b//
  425. if $^O eq 'cygwin' and $command =~ /-fstack-protector\b/ and $linkargs =~ /-fstack-protector\b/;
  426. $command .= $linkargs;
  427. } else {
  428. my $linkargs = ExtUtils::Embed::ldopts('-std');
  429. # cygwin gcc-4.3 crashes with -fstack-protector 2x
  430. $linkargs =~ s/-fstack-protector\b//
  431. if $^O eq 'cygwin' and $command =~ /-fstack-protector\b/ and $linkargs =~ /-fstack-protector\b/;
  432. $command .= $linkargs;
  433. $command .= " -lperl" if $command !~ /(-lperl|CORE\/libperl5)/ and $^O ne 'MSWin32';
  434. }
  435. $command .= $B::C::Flags::extra_libs;
  436. my $NULL = $^O eq 'MSWin32' ? '' : '2>/dev/null';
  437. my $cmdline = "$Config{cc} $command $NULL";
  438. if ($Config{cc} eq 'cl') {
  439. $cmdline = "$Config{ld} $linkargs -out:$exe $obj[0] $command";
  440. }
  441. diag ($cmdline) if $ENV{TEST_VERBOSE} and $ENV{TEST_VERBOSE} == 2;
  442. run_cmd($cmdline, 20);
  443. unless (-e $exe) {
  444. print "not ok $cnt $todo failed $cmdline\n";
  445. print STDERR "# ",system("$Config{cc} $command"), "\n";
  446. #unlink ($test, $cfile, $exe, @obj) unless $keep_c_fail;
  447. return 0;
  448. }
  449. $exe = "./".$exe unless $^O eq 'MSWin32';
  450. # system("/bin/bash -c ulimit -d 1000000") if -e "/bin/bash";
  451. ($result,$out,$stderr) = run_cmd($exe, 5);
  452. if (defined($out) and !$result) {
  453. if ($out =~ /^$expect$/) {
  454. print "ok $cnt", $todo eq '#' ? "\n" : " $todo\n";
  455. unlink ($test, $cfile, $exe, @obj) unless $keep_c;
  456. return 1;
  457. } else {
  458. # cc test failed, double check uncompiled
  459. $got = run_perl(verbose => $ENV{TEST_VERBOSE}, # for debugging
  460. nolib => $ENV{PERL_CORE} ? 0 : 1, # include ../lib only in CORE
  461. stderr => 1, # to capture the "ccode.pl syntax ok"
  462. timeout => 10,
  463. progfile => $test);
  464. if (! $? and $got =~ /^$expect$/) {
  465. print "not ok $cnt $todo wanted: \"$expect\", got: \"$out\"\n";
  466. } else {
  467. print "ok $cnt # skip also fails uncompiled\n";
  468. return 1;
  469. }
  470. unlink ($test, $cfile, $exe, @obj) unless $keep_c_fail;
  471. return 0;
  472. }
  473. } else {
  474. $out = '';
  475. }
  476. }
  477. print "not ok $cnt $todo wanted: \"$expect\", \$\? = $?, got: \"$out\"\n";
  478. if ($stderr) {
  479. $stderr =~ s/\n./\n# /xmsg;
  480. print "# $stderr\n";
  481. }
  482. unlink ($test, $cfile, $exe, @obj) unless $keep_c_fail;
  483. return 0;
  484. }
  485. sub prepare_c_tests {
  486. BEGIN {
  487. use Config;
  488. if ($^O eq 'VMS') {
  489. print "1..0 # skip - B::C doesn't work on VMS\n";
  490. exit 0;
  491. }
  492. if (($Config{'extensions'} !~ /\bB\b/) ) {
  493. print "1..0 # Skip -- Perl configured without B module\n";
  494. exit 0;
  495. }
  496. # with 5.10 and 5.8.9 PERL_COPY_ON_WRITE was renamed to PERL_OLD_COPY_ON_WRITE
  497. if ($Config{ccflags} =~ /-DPERL_OLD_COPY_ON_WRITE/) {
  498. print "1..0 # skip - no OLD COW for now\n";
  499. exit 0;
  500. }
  501. }
  502. }
  503. sub run_c_tests {
  504. my $backend = $_[0];
  505. my @todo = @{$_[1]};
  506. my @skip = @{$_[2]};
  507. use Config;
  508. my $AUTHOR = (-d ".git" and !$ENV{NO_AUTHOR}) ? 1 : 0;
  509. my $keep_c = 0; # set it to keep the pl, c and exe files
  510. my $keep_c_fail = 1; # keep on failures
  511. my %todo = map { $_ => 1 } @todo;
  512. my %skip = map { $_ => 1 } @skip;
  513. my @tests = tests();
  514. # add some CC specific tests after 100
  515. # perl -lne "/^\s*sub pp_(\w+)/ && print \$1" lib/B/CC.pm > ccpp
  516. # for p in `cat ccpp`; do echo -n "$p "; grep -m1 " $p[(\[ ]" *.concise; done
  517. #
  518. # grep -A1 "coverage: ny" lib/B/CC.pm|grep sub
  519. # pp_stub pp_cond_expr pp_dbstate pp_reset pp_stringify pp_ncmp pp_preinc
  520. # pp_formline pp_enterwrite pp_leavewrite pp_entergiven pp_leavegiven
  521. # pp_dofile pp_grepstart pp_mapstart pp_grepwhile pp_mapwhile
  522. if ($backend =~ /^CC/) {
  523. local $/;
  524. my $cctests = <<'CCTESTS';
  525. my ($r_i,$i_i,$d_d)=(0,2,3.0); $r_i=$i_i*$i_i; $r_i*=$d_d; print $r_i;
  526. >>>>
  527. 12
  528. ######### 101 - CC types and arith ###############
  529. if ($x eq "2"){}else{print "ok"}
  530. >>>>
  531. ok
  532. ######### 102 - CC cond_expr,stub,scope ############
  533. require B; my $x=1e1; my $s="$x"; print ref B::svref_2object(\$s)
  534. >>>>
  535. B::PV
  536. ######### 103 - CC stringify srefgen ############
  537. @a=(1..4);while($a=shift@a){print $a;}continue{$a=~/2/ and reset q(a);}
  538. >>>>
  539. 12
  540. ######### 104 CC reset ###############################
  541. use blib;use B::CC;my int $r;my $i:int=2;our double $d=3.0; $r=$i*$i; $r*=$d; print $r;
  542. >>>>
  543. 12
  544. ######### 105 CC attrs ###############################
  545. CCTESTS
  546. my $i = 100;
  547. for (split /\n####+.*##\n/, $cctests) {
  548. next unless $_;
  549. $tests[$i] = $_;
  550. $i++;
  551. }
  552. }
  553. print "1..".(scalar @tests)."\n";
  554. my $cnt = 1;
  555. for (@tests) {
  556. my $todo = $todo{$cnt} ? "#TODO" : "#";
  557. # skip empty CC holes to have the same test indices in STATUS and t/testcc.sh
  558. unless ($_) {
  559. print sprintf("ok %d # skip hole for CC\n", $cnt);
  560. $cnt++;
  561. next;
  562. }
  563. # only once. skip subsequent tests 29 on MSVC. 7:30min!
  564. if ($cnt == 29 and !$AUTHOR) {
  565. $todo{$cnt} = $skip{$cnt} = 1;
  566. }
  567. if ($todo{$cnt} and $skip{$cnt} and
  568. # those are currently blocking the system
  569. # do not even run them at home if TODO+SKIP
  570. (!$AUTHOR
  571. or ($cnt==15 and $backend eq 'C,-O1') # hanging
  572. or ($cnt==103 and $backend eq 'CC,-O2') # hanging
  573. ))
  574. {
  575. print sprintf("ok %d # skip\n", $cnt);
  576. } else {
  577. my ($script, $expect) = split />>>+\n/;
  578. die "Invalid empty t/TESTS" if !$script or $expect eq '';
  579. if ($cnt == 4 and $] >= 5.017005) {
  580. $expect = 'zzz2y2y2';
  581. }
  582. run_cc_test($cnt, $backend.($cnt == 46 ? ',-fstash' : ''),
  583. $script, $expect, $keep_c, $keep_c_fail, $todo);
  584. }
  585. $cnt++;
  586. }
  587. }
  588. sub plctestok {
  589. my ($num, $base, $script, $todo) = @_;
  590. plctest($num,'^ok', $base, $script, $todo);
  591. }
  592. sub plctest {
  593. my ($num, $expected, $base, $script, $todo) = @_;
  594. my $name = $base."_$num";
  595. unlink($name, "$name.plc", "$name.pl", "$name.exe");
  596. open F, ">", "$base.pl";
  597. print F $script;
  598. close F;
  599. my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  600. # we don't want to change STDOUT/STDERR on STDOUT/STDERR tests, so no -qq
  601. my $nostdoutclobber = $base !~ /^ccode93i/;
  602. my $b = ($] > 5.008 and $nostdoutclobber) ? "-qq,Bytecode" : "Bytecode";
  603. my $Mblib = Mblib;
  604. system "$runperl $Mblib -MO=$b,-o$name.plc $base.pl";
  605. # $out =~ s/^$base.pl syntax OK\n//m;
  606. unless (-e "$name.plc") {
  607. print "not ok $num #B::Bytecode failed\n";
  608. exit;
  609. }
  610. my $out = qx($runperl $Mblib -MByteLoader $name.plc);
  611. chomp $out;
  612. my $ok = $out =~ /$expected/;
  613. if ($todo and $todo =~ /TODO/) {
  614. $todo =~ s/TODO //;
  615. TODO: {
  616. local $TODO = $todo;
  617. ok($ok);
  618. }
  619. } else {
  620. ok($ok, $todo ? "$todo " : '');
  621. }
  622. if ($ok) {
  623. unlink("$name.plc", "$base.pl");
  624. }
  625. }
  626. sub ctestok {
  627. my ($num, $backend, $base, $script, $todo) = @_;
  628. my $qr = '^ok'; # how lame
  629. ctest($num, $qr, $backend, $base, $script, $todo);
  630. }
  631. sub ctest {
  632. my ($num, $expected, $backend, $base, $script, $todo) = @_;
  633. my $name = $base."_$num";
  634. unlink($name, "$name.c", "$name.pl", "$name.exe");
  635. open F, ">", "$name.pl";
  636. print F $script;
  637. close F;
  638. my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  639. # we don't want to change STDOUT/STDERR on STDOUT/STDERR tests, so no -qq
  640. my $nostdoutclobber = $base !~ /^ccode93i/;
  641. my $post = '';
  642. my $b = ($] > 5.008 and $nostdoutclobber) ? "-qq,$backend" : "$backend";
  643. ($b, $post) = split(" ", $b);
  644. $b .= q(,-fno-fold,-fno-warnings) if $] >= 5.013005 and $b !~ /-O3/;
  645. system "$runperl ".Mblib." -MO=$b,-o$name.c $post $name.pl";
  646. unless (-e "$name.c") {
  647. print "not ok $num #B::$backend failed\n";
  648. exit;
  649. }
  650. system "$runperl ".Mblib." blib/script/cc_harness -q -o $name $name.c";
  651. my $exe = $name.$Config{exe_ext};
  652. unless (-e $exe) {
  653. if ($todo and $todo =~ /TODO/) {
  654. $todo =~ s/TODO //;
  655. TODO: {
  656. local $TODO = $todo;
  657. ok(undef, "failed to compile");
  658. }
  659. } else {
  660. ok(undef, "failed to compile $todo");
  661. }
  662. return;
  663. }
  664. $exe = "./".$exe unless $^O eq 'MSWin32';
  665. ($result,$out,$stderr) = run_cmd($exe, 5);
  666. my $ok;
  667. if (defined($out) and !$result) {
  668. chomp $out;
  669. $ok = $out =~ /$expected/;
  670. diag($out) if $ENV{TEST_VERBOSE};
  671. unless ($ok) { #crosscheck uncompiled
  672. my $out1 = `$runperl $name.pl`;
  673. unless ($out1 =~ /$expected/) {
  674. ok(1, "skip also fails uncompiled $todo");
  675. return 1;
  676. }
  677. }
  678. if ($todo and $todo =~ /TODO/) {
  679. $todo =~ s/TODO //;
  680. TODO: {
  681. local $TODO = $todo;
  682. ok ($out =~ /$expected/);
  683. diag($out) if $ENV{TEST_VERBOSE};
  684. }
  685. } else {
  686. ok ($out =~ /$expected/, $todo);
  687. }
  688. } else {
  689. if ($todo and $todo =~ /TODO/) {
  690. $todo =~ s/TODO //;
  691. TODO: {
  692. local $TODO = $todo;
  693. ok (undef);
  694. }
  695. } else {
  696. #crosscheck uncompiled
  697. my $out1 = `$runperl $name.pl`;
  698. unless ($out1 =~ /$expected/) {
  699. ok(1, "skip also fails uncompiled");
  700. return $ok;
  701. }
  702. ok (undef, $todo);
  703. }
  704. }
  705. unlink("$name.pl");
  706. if ($ok) {
  707. unlink($name, "$name.c", "$name.exe");
  708. }
  709. $ok
  710. }
  711. sub ccompileok {
  712. my ($num, $backend, $base, $script, $todo) = @_;
  713. my $name = $base."_$num";
  714. unlink($name, "$name.c", "$name.pl", "$name.exe");
  715. open F, ">", "$name.pl";
  716. print F $script;
  717. close F;
  718. my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
  719. my $b = $] > 5.008 ? "-qq,$backend" : "$backend";
  720. my $Mblib = Mblib;
  721. system "$runperl $Mblib -MO=$b,-o$name.c $name.pl";
  722. unless (-e "$name.c") {
  723. print "not ok 1 #B::$backend failed\n";
  724. exit;
  725. }
  726. system "$runperl $Mblib blib/script/cc_harness -q -o $name $name.c";
  727. my $ok = -e $name or -e "$name.exe";
  728. if ($todo and $todo =~ /TODO/) {
  729. TODO: {
  730. $todo =~ s/TODO //;
  731. local $TODO = $todo;
  732. ok($ok);
  733. }
  734. } else {
  735. ok($ok, $todo);
  736. }
  737. unlink("$name.pl");
  738. if ($ok) {
  739. unlink($name, "$name.c", "$name.exe");
  740. }
  741. }
  742. sub todo_tests_default {
  743. my $what = shift;
  744. my $DEBUGGING = ($Config{ccflags} =~ m/-DDEBUGGING/);
  745. my $ITHREADS = ($Config{useithreads});
  746. my @todo = ();
  747. push @todo, (15) if $] < 5.007;
  748. push @todo, (10) if $ITHREADS;
  749. push @todo, (42,43) if $] >= 5.012 and $] < 5.014;
  750. if ($what =~ /^c(|_o[1-4])$/) {
  751. push @todo, (7) if $] == 5.008005;
  752. push @todo, (21) if $] >= 5.012 and $] < 5.014;
  753. push @todo, (15) if $] > 5.010 and $] < 5.016 and $ITHREADS;
  754. push @todo, (27) if $] >= 5.012 and $] < 5.014 and $ITHREADS and $DEBUGGING;
  755. # @ISA issue 64
  756. push @todo, (10,12,19,25,42,43,50) if $what eq 'c_o4';
  757. push @todo, (48) if $] >= 5.008009 and $] < 5.010 and $what eq 'c_o4';
  758. # DynaLoader::dl_load_file()
  759. #push @todo, (42..43) if $] > 5.015 and $what eq 'c_o4';
  760. #push @todo, (15,42..45) if $] >= 5.016; #1.42_66
  761. } elsif ($what =~ /^cc/) {
  762. # 8,11,14..16,18..19 fail on 5.00505 + 5.6, old core failures (max 20)
  763. # on cygwin 29 passes
  764. #15,21,27,30,41-45,50,103,105
  765. #15,46,50,103 fixed with 1.42_61
  766. push @todo, (21,30,105);
  767. push @todo, (104,105) if $] < 5.007; # leaveloop, no cxstack
  768. push @todo, (3,7,15,41,44,45) if $] > 5.008 and $] <= 5.008005;
  769. push @todo, (42,43) if $] > 5.008 and $] <= 5.008005 and !$ITHREADS;
  770. push @todo, (14) if $] >= 5.012;
  771. push @todo, (10,16,50) if $what eq 'cc_o2';
  772. #push @todo, (29) if $] >= 5.013 and $what eq 'cc_o2';
  773. push @todo, (43) if $what eq 'cc_o2'; # -faelem
  774. #push @todo, (103) if $] > 5.007 and $] < 5.009 and $what eq 'cc_o1';
  775. # only tested 5.8.4 and .5
  776. push @todo, (27) if $] <= 5.008008;
  777. push @todo, (49) if $] >= 5.007 and $] < 5.008008;
  778. push @todo, (29) if $] < 5.009; # or ($] > 5.013 and $] < 5.015);
  779. push @todo, (14) if $] >= 5.010 and $^O !~ /MSWin32|cygwin/i;
  780. # solaris also. I suspected nvx<=>cop_seq_*
  781. push @todo, (12) if $^O eq 'MSWin32' and $Config{cc} =~ /^cl/i;
  782. push @todo, (26) if $what =~ /^cc_o[12]/;
  783. push @todo, (27) if $] > 5.008008 and $] < 5.009 and $what eq 'cc_o2';
  784. push @todo, (25) if $] >= 5.011004 and $DEBUGGING and $ITHREADS;
  785. push @todo, (3,4) if $] >= 5.011004 and $] < 5.016 and $ITHREADS;
  786. push @todo, (49) if $] >= 5.013009 and !$ITHREADS;
  787. #push @todo, (15,42..45,103) if $] >= 5.016;
  788. push @todo, (103) if ($] >= 5.012 and $] < 5.014 and !$ITHREADS);
  789. }
  790. push @todo, (48) if $] > 5.007 and $] < 5.009 and $^O =~ /MSWin32|cygwin/i;
  791. return @todo;
  792. }
  793. 1;
  794. # Local Variables:
  795. # mode: cperl
  796. # cperl-indent-level: 4
  797. # fill-column: 78
  798. # End:
  799. # vim: expandtab shiftwidth=4: