This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Variants of several regression tests that run the actul tests inside
[perl5.git] / t / op / regexp.t
CommitLineData
378cc40b
LW
1#!./perl
2
ad4f75a6
HM
3# The tests are in a separate file 't/op/re_tests'.
4# Each line in that file is a separate test.
5# There are five columns, separated by tabs.
6#
7# Column 1 contains the pattern, optionally enclosed in C<''>.
8# Modifiers can be put after the closing C<'>.
9#
10# Column 2 contains the string to be matched.
11#
12# Column 3 contains the expected result:
13# y expect a match
14# n expect no match
15# c expect an error
cf93c79d
IZ
16# B test exposes a known bug in Perl, should be skipped
17# b test exposes a known bug in Perl, should be skipped if noamp
e3faa678 18# t test exposes a bug with threading, TODO if qr_embed_thr
ad4f75a6 19#
1b1626e4 20# Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
ad4f75a6
HM
21#
22# Column 4 contains a string, usually C<$&>.
23#
24# Column 5 contains the expected result of double-quote
c277df42
IZ
25# interpolating that string after the match, or start of error message.
26#
ee595aa6
LC
27# Column 6, if present, contains a reason why the test is skipped.
28# This is printed with "skipped", for harness to pick up.
29#
9d116dd7 30# \n in the tests are interpolated, as are variables of the form ${\w+}.
83e898de 31#
b9b4dddf
YO
32# Blanks lines are treated as PASSING tests to keep the line numbers
33# linked to the test number.
34#
8d37f932
DD
35# If you want to add a regular expression test that can't be expressed
36# in this format, don't add it here: put it in op/pat.t instead.
b2a156bd
DM
37#
38# Note that columns 2,3 and 5 are all enclosed in double quotes and then
39# evalled; so something like a\"\x{100}$1 has length 3+length($1).
c277df42 40
1a610890 41my $file;
e4d48cc9 42BEGIN {
1a610890
NC
43 $iters = shift || 1; # Poor man performance suite, 10000 is OK.
44
45 # Do this open before any chdir
46 $file = shift;
47 if (defined $file) {
48 open TESTS, $file or die "Can't open $file";
49 }
50
e4d48cc9 51 chdir 't' if -d 't';
20822f61 52 @INC = '../lib';
e3faa678
NC
53
54 if ($qr_embed_thr) {
55 require Config;
56 if (!$Config::Config{useithreads}) {
57 print "1..0 # Skip: no ithreads\n";
58 exit 0;
59 }
60 if ($ENV{PERL_CORE_MINITEST}) {
61 print "1..0 # Skip: no dynamic loading on miniperl, no threads\n";
62 exit 0;
63 }
64 require threads;
65 }
e4d48cc9 66}
1a610890 67
1286eaeb 68use strict;
66fb63c1 69use warnings FATAL=>"all";
1286eaeb 70use vars qw($iters $numtests $bang $ffff $nulnul $OP);
e3faa678 71use vars qw($qr $skip_amp $qr_embed $qr_embed_thr); # set by our callers
e4d48cc9 72
ad4f75a6 73
1a610890
NC
74if (!defined $file) {
75 open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests')
76 || open(TESTS,':op:re_tests') || die "Can't open re_tests";
77}
78
79my @tests = <TESTS>;
cfa4f241 80
1a610890 81close TESTS;
378cc40b 82
9d116dd7 83$bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
b8c5462f
JH
84$ffff = chr(0xff) x 2;
85$nulnul = "\0" x 2;
7fba1cd6 86$OP = $qr ? 'qr' : 'm';
9d116dd7 87
1462b684 88$| = 1;
1a610890 89printf "1..%d\n# $iters iterations\n", scalar @tests;
e3faa678 90
1a610890 91my $test;
cfa4f241 92TEST:
1a610890
NC
93foreach (@tests) {
94 $test++;
5a51db05 95 if (!/\S/ || /^\s*#/ || /^__END__$/) {
1a610890 96 print "ok $test # (Blank line or comment)\n";
5a51db05 97 if (/#/) { print $_ };
b9b4dddf
YO
98 next;
99 }
b85d18e9
IZ
100 chomp;
101 s/\\n/\n/g;
1286eaeb 102 my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
66fb63c1 103 $reason = '' unless defined $reason;
1286eaeb 104 my $input = join(':',$pat,$subject,$result,$repl,$expect);
81714fb9 105 $pat = "'$pat'" unless $pat =~ /^[:'\/]/;
9d116dd7 106 $pat =~ s/(\$\{\w+\})/$1/eeg;
b8c5462f 107 $pat =~ s/\\n/\n/g;
1a610890
NC
108 $subject = eval qq("$subject"); die $@ if $@;
109 $expect = eval qq("$expect"); die $@ if $@;
c277df42 110 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
e3faa678 111 my $todo = $qr_embed_thr && ($result =~ s/t//);
1286eaeb 112 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
906e884f 113 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
cf93c79d 114 $result =~ s/B//i unless $skip;
1de06328 115
52e33015
NC
116 for my $study ('', 'study $subject', 'utf8::upgrade($subject)',
117 'utf8::upgrade($subject); study $subject') {
118 # Need to make a copy, else the utf8::upgrade of an alreay studied
119 # scalar confuses things.
120 my $subject = $subject;
1286eaeb
NC
121 my $c = $iters;
122 my ($code, $match, $got);
1de06328
YO
123 if ($repl eq 'pos') {
124 $code= <<EOFCODE;
125 $study;
126 pos(\$subject)=0;
127 \$match = ( \$subject =~ m${pat}g );
128 \$got = pos(\$subject);
129EOFCODE
130 }
131 elsif ($qr_embed) {
132 $code= <<EOFCODE;
133 my \$RE = qr$pat;
134 $study;
135 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
136 \$got = "$repl";
137EOFCODE
138 }
e3faa678
NC
139 elsif ($qr_embed_thr) {
140 $code= <<EOFCODE;
141 # Can't run the match in a subthread, but can do this and
142 # clone the pattern the other way.
143 my \$RE = threads->new(sub {qr$pat})->join();
144 $study;
145 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
146 \$got = "$repl";
147EOFCODE
148 }
1de06328
YO
149 else {
150 $code= <<EOFCODE;
151 $study;
1286eaeb 152 \$match = (\$subject =~ $OP$pat) while \$c--;
1de06328
YO
153 \$got = "$repl";
154EOFCODE
155 }
e1d1eefb
YO
156 #$code.=qq[\n\$expect="$expect";\n];
157 #use Devel::Peek;
158 #die Dump($code) if $pat=~/\\h/ and $subject=~/\x{A0}/;
66fb63c1
NC
159 {
160 # Probably we should annotate specific tests with which warnings
161 # categories they're known to trigger, and hence should be
162 # disabled just for that test
163 no warnings qw(uninitialized regexp);
164 eval $code;
165 }
1286eaeb 166 chomp( my $err = $@ );
cfa4f241 167 if ($result eq 'c') {
1a610890 168 if ($err !~ m!^\Q$expect!) { print "not ok $test (compile) $input => `$err'\n"; next TEST }
cfa4f241
CS
169 last; # no need to study a syntax error
170 }
cf93c79d 171 elsif ( $skip ) {
1a610890 172 print "ok $test # skipped", length($reason) ? " $reason" : '', "\n";
ee595aa6 173 next TEST;
cf93c79d 174 }
e3faa678
NC
175 elsif ( $todo ) {
176 print "not ok $test # todo", length($reason) ? " - $reason" : '', "\n";
177 next TEST;
178 }
c277df42 179 elsif ($@) {
1a610890 180 print "not ok $test $input => error `$err'\n$code\n$@\n"; next TEST;
c277df42 181 }
e3faa678 182 elsif ($result =~ /^n/) {
1a610890 183 if ($match) { print "not ok $test ($study) $input => false positive\n"; next TEST }
378cc40b
LW
184 }
185 else {
cfa4f241 186 if (!$match || $got ne $expect) {
cde0cee5
YO
187 eval { require Data::Dumper };
188 if ($@) {
1a610890 189 print "not ok $test ($study) $input => `$got', match=$match\n$code\n";
cde0cee5
YO
190 }
191 else { # better diagnostics
192 my $s = Data::Dumper->new([$subject],['subject'])->Useqq(1)->Dump;
193 my $g = Data::Dumper->new([$got],['got'])->Useqq(1)->Dump;
1a610890 194 print "not ok $test ($study) $input => `$got', match=$match\n$s\n$g\n$code\n";
cde0cee5 195 }
cfa4f241
CS
196 next TEST;
197 }
378cc40b
LW
198 }
199 }
1a610890 200 print "ok $test\n";
378cc40b 201}
cfa4f241 202
1a610890 2031;