This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Compile with perlcc..
[perl5.git] / t / TEST
1 #!./perl
2
3 # This is written in a peculiar style, since we're trying to avoid
4 # most of the constructs we'll be testing for.
5
6 $| = 1;
7
8 # Cheesy version of Getopt::Std.  Maybe we should replace it with that.
9 if ($#ARGV >= 0) {
10     foreach my $idx (0..$#ARGV) {
11         next unless $ARGV[$idx] =~ /^-(\w+)$/;
12         $verbose = 1 if $1 eq 'v';
13         $with_utf= 1 if $1 eq 'utf8';
14         splice(@ARGV, $idx, 1);
15     }
16 }
17
18 chdir 't' if -f 't/TEST';
19
20 die "You need to run \"make test\" first to set things up.\n"
21   unless -e 'perl' or -e 'perl.exe';
22
23 # check leakage for embedders
24 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
25
26 $ENV{EMXSHELL} = 'sh';        # For OS/2
27
28 if ($#ARGV == -1) {
29     @ARGV = split(/[ \n]/,
30       `echo base/*.t comp/*.t cmd/*.t run/*.t io/*.t; echo op/*.t pragma/*.t lib/*.t pod/*.t`);
31 }
32
33 # %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 ); 
34
35 _testprogs('perl', @ARGV);
36 _testprogs('compile', @ARGV) if (-e "../testcompile"); 
37
38 sub _testprogs {
39     $type = shift @_;
40     @tests = @_;
41
42
43     print <<'EOT' if ($type eq 'compile');
44 --------------------------------------------------------------------------------
45 TESTING COMPILER
46 --------------------------------------------------------------------------------
47 EOT
48
49     $ENV{PERLCC_TIMEOUT} = 120 
50           if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
51
52     $bad = 0;
53     $good = 0;
54     $total = @tests;
55     $files  = 0;
56     $totmax = 0;
57     $maxlen = 0;
58     foreach (@tests) {
59         $len = length;
60         $maxlen = $len if $len > $maxlen;
61     }
62     # +3 : we want three dots between the test name and the "ok"
63     # -2 : the .t suffix
64     $dotdotdot = $maxlen + 3 - 2;
65     while ($test = shift @tests) {
66
67         if ( $infinite{$test} && $type eq 'compile' ) {
68             print STDERR "$test creates infinite loop! Skipping.\n"; 
69             next;
70         }
71         if ($test =~ /^$/) {
72             next;
73         }
74         $te = $test;
75         chop($te);
76         print "$te" . '.' x ($dotdotdot - length($te));
77
78         open(SCRIPT,"<$test") or die "Can't run $test.\n";
79         $_ = <SCRIPT>;
80         close(SCRIPT);
81         if (/#!.*perl(.*)$/) {
82             $switch = $1;
83             if ($^O eq 'VMS') {
84                 # Must protect uppercase switches with "" on command line
85                 $switch =~ s/-([A-Z]\S*)/"-$1"/g;
86             }
87         }
88         else {
89             $switch = '';
90         }
91
92         my $utf = $with_utf ? '-I../lib -Mutf8'
93                             : '';
94         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
95         if ($type eq 'perl') {
96             my $run = "./perl $testswitch $switch $utf $test |"; 
97             open(RESULTS,$run) or print "can't run '$run': $!.\n";
98         }
99         else {
100             my $compile =
101                 "./perl $testswitch -I../lib ../utils/perlcc -o ".
102                 "./$test.plc $utf ./$test ".
103                 " && ./$test.plc |";
104             open(RESULTS, $compile)
105                 or print "can't compile '$compile': $!.\n";
106             unlink "./$test.plc";
107         }
108
109         $ok = 0;
110         $next = 0;
111         while (<RESULTS>) {
112             if ($verbose) {
113                 print $_;
114             }
115             unless (/^#/) {
116                 if (/^1\.\.([0-9]+)/) {
117                     $max = $1;
118                     $totmax += $max;
119                     $files += 1;
120                     $next = 1;
121                     $ok = 1;
122                 }
123                 else {
124                     if (/^(not )?ok (\d+)(\s*#.*)?/ &&
125                         $2 == $next) 
126                     {
127                         my($not, $num, $extra) = ($1, $2, $3);
128                         my($istodo) = $extra =~ /^\s*#\s*TODO/ if $extra;
129
130                         if( $not && !$istodo ) {
131                             $ok = 0;
132                             $next = $num;
133                             last;
134                         }
135                         else {
136                             $next = $next + 1;
137                         }
138                     }
139                     elsif (/^Bail out!\s*(.*)/i) { # magic words
140                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
141                     }
142                     else {
143                         $ok = 0;
144                     }
145                 }
146             }
147         }
148         close RESULTS;
149         $next = $next - 1;
150         if ($ok && $next == $max) {
151             if ($max) {
152                 print "ok\n";
153                 $good = $good + 1;
154             }
155             else {
156                 print "skipping test on this platform\n";
157                 $files -= 1;
158             }
159         }
160         else {
161             $next += 1;
162             print "FAILED at test $next\n";
163             $bad = $bad + 1;
164             $_ = $test;
165             if (/^base/) {
166                 die "Failed a basic test--cannot continue.\n";
167             }
168         }
169     }
170
171     if ($bad == 0) {
172         if ($ok) {
173             print "All tests successful.\n";
174             # XXX add mention of 'perlbug -ok' ?
175         }
176         else {
177             die "FAILED--no tests were run for some reason.\n";
178         }
179     }
180     else {
181         $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
182         if ($bad == 1) {
183             warn "Failed 1 test script out of $files, $pct% okay.\n";
184         }
185         else {
186             warn "Failed $bad test scripts out of $files, $pct% okay.\n";
187         }
188         warn <<'SHRDLU';
189    ### Since not all tests were successful, you may want to run some
190    ### of them individually and examine any diagnostic messages they
191    ### produce.  See the INSTALL document's section on "make test".
192    ### If you are testing the compiler, then ignore this message 
193    ### and run 
194    ###      ./perl harness
195    ### in the directory ./t.
196 SHRDLU
197         warn <<'SHRDLU' if $good / $total > 0.8;
198    ###
199    ### Since most tests were successful, you have a good chance to
200    ### get information with better granularity by running
201    ###     ./perl harness 
202    ### in directory ./t.
203 SHRDLU
204     }
205     ($user,$sys,$cuser,$csys) = times;
206     print sprintf("u=%g  s=%g  cu=%g  cs=%g  scripts=%d  tests=%d\n",
207         $user,$sys,$cuser,$csys,$files,$totmax);
208 }
209 exit ($bad != 0);