3 # This is a home for regular expression tests that don't fit into
4 # the format supported by op/regexp.t. If you want to add a test
5 # that does fit that format, add it to op/re_tests, not here.
11 @INC = "../lib" if -d "../lib";
13 eval 'use Config'; # Defaults assumed if this fails
15 # XXX known to leak scalars
16 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
20 if ($x =~ /^abc/) {print "ok 1\n";} else {print "not ok 1\n";}
21 if ($x !~ /^def/) {print "ok 2\n";} else {print "not ok 2\n";}
24 if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";}
28 if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";}
30 if ($x =~ /^xxx/) {print "not ok 5\n";} else {print "ok 5\n";}
31 if ($x !~ /^abc/) {print "not ok 6\n";} else {print "ok 6\n";}
33 if ($x =~ /def/) {print "ok 7\n";} else {print "not ok 7\n";}
34 if ($x !~ /def/) {print "not ok 8\n";} else {print "ok 8\n";}
36 if ($x !~ /.def/) {print "ok 9\n";} else {print "not ok 9\n";}
37 if ($x =~ /.def/) {print "not ok 10\n";} else {print "ok 10\n";}
39 if ($x =~ /\ndef/) {print "ok 11\n";} else {print "not ok 11\n";}
40 if ($x !~ /\ndef/) {print "not ok 12\n";} else {print "ok 12\n";}
43 if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') {
48 if (/(a+b+c+)/ && $1 eq 'aaabbbccc') {
54 if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";}
57 if (/a+b?c+/) {print "ok 16\n";} else {print "not ok 16\n";}
58 if (/a*b+c*/) {print "ok 17\n";} else {print "not ok 17\n";}
61 if (/a*b?c*/) {print "ok 18\n";} else {print "not ok 18\n";}
62 if (/a*b+c*/) {print "not ok 19\n";} else {print "ok 19\n";}
65 if (/bcd|xyz/) {print "ok 20\n";} else {print "not ok 20\n";}
66 if (/xyz|bcd/) {print "ok 21\n";} else {print "not ok 21\n";}
68 if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";}
70 if (/^$_$/) {print "ok 23\n";} else {print "not ok 23\n";}
72 $* = 1; # test 3 only tested the optimized version--this one is for real
73 if ("ab\ncd\n" =~ /^cd/) {print "ok 24\n";} else {print "not ok 24\n";}
80 @XXX = ('ok 25','not ok 25', 'ok 26','not ok 26','not ok 27');
81 while ($_ = shift(@XXX)) {
82 ?(.*)? && (print $1,"\n");
84 /not ok 26/ && reset 'X';
87 while (($key,$val) = each(%XXX)) {
96 if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";}
101 if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";}
106 if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";}
109 /def/; # optimized up to cmd
110 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";}
112 /cde/ + 0; # optimized only to spat
113 if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";}
115 /[d][e][f]/; # not optimized
116 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 33\n";} else {print "not ok 33\n";}
118 $_ = 'now is the {time for all} good men to come to.';
120 if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";}
122 $_ = 'xxx {3,4} yyy zzz';
123 print /( {3,4})/ ? "ok 35\n" : "not ok 35\n";
124 print $1 eq ' ' ? "ok 36\n" : "not ok 36\n";
125 print /( {4,})/ ? "not ok 37\n" : "ok 37\n";
126 print /( {2,3}.)/ ? "ok 38\n" : "not ok 38\n";
127 print $1 eq ' y' ? "ok 39\n" : "not ok 39\n";
128 print /(y{2,3}.)/ ? "ok 40\n" : "not ok 40\n";
129 print $1 eq 'yyy ' ? "ok 41\n" : "not ok 41\n";
130 print /x {3,4}/ ? "not ok 42\n" : "ok 42\n";
131 print /^xxx {3,4}/ ? "not ok 43\n" : "ok 43\n";
133 $_ = "now is the time for all good men to come to.";
135 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
143 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
152 print join(':',@words) eq "to:to"
154 : "not ok 46 `@words'\n";
158 print join(':',@words) eq "to:to"
160 : "not ok 47 `@words'\n";
174 $t1=$t2=$t3=$t4=$t5=$t6=$t7=$t8=$t9=0;
188 $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
189 print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n";
192 print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n";
194 # perl 4.009 says "unmatched ()"
195 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
196 print $@ eq "" ? "ok 50\n" : "not ok 50\n";
197 print $result eq "abc:bc" ? "ok 51\n" : "not ok 51\n";
202 print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x;
204 print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x;
206 print $x == 0 ? "ok 54\n" : "not ok 54\n";
209 print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x;
211 print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x;
213 print $x == 0 ? "ok 57\n" : "not ok 57\n";
216 print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x;
218 print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x;
221 print scalar @x == 2 ? "ok 60\n" : "not ok 60\n";
226 print "not " if (pos $_) != 2;
229 print "not " if defined pos $_;
233 'abc' =~ m'a(?{ $out = 2 })b';
234 print "not " if $out != 2;
238 'abc' =~ m'a(?{ $out = 3 })c';
239 print "not " if $out != 1;
242 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
243 @out = /(?<!foo)bar./g;
244 print "not " if "@out" ne 'bar2 barf';
247 # Tests which depend on REG_INFTY
248 $reg_infty = defined $Config{reg_infty} ? $Config{reg_infty} : 32767;
249 $reg_infty_m = $reg_infty - 1; $reg_infty_p = $reg_infty + 1;
251 # As well as failing if the pattern matches do unexpected things, the
252 # next three tests will fail if you should have picked up a lower-than-
253 # default value for $reg_infty from Config.pm, but have not.
256 print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@;
260 print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@;
264 print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@;
268 eval "'aaa' =~ /a{1,$reg_infty}/";
269 print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%;
272 eval "'aaa' =~ /a{1,$reg_infty_p}/";
274 if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%;
278 # Poke a couple more parse failures
280 $context = 'x' x 256;
281 eval qq("${context}y" =~ /(?<=$context)y/);
282 print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%;
285 # This one will fail when POSIX character classes do get implemented
289 local $SIG{__WARN__} = sub{$w = shift};
290 eval q('a' =~ /[[:alpha:]]/);
291 print "not " if $w !~ /^\QCharacter class syntax [: :] is reserved/;
297 for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
299 print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
303 print "not " if "b$a=" =~ /a$a=/;
308 # 20000 nodes, each taking 3 words per string, and 1 per branch
309 $long_constant_len = join '|', 12120 .. 32645;
310 $long_var_len = join '|', 8120 .. 28645;
311 %ans = ( 'ax13876y25677lbc' => 1,
312 'ax13876y25677mcb' => 0, # not b.
313 'ax13876y35677nbc' => 0, # Num too big
314 'ax13876y25677y21378obc' => 1,
315 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
316 'ax13876y25677y21378y21378kbc' => 1,
317 'ax13876y25677y21378y21378kcb' => 0, # Not b.
318 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
322 print "# const-len `$_' not => $ans{$_}\nnot "
323 if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
326 print "# var-len `$_' not => $ans{$_}\nnot "
327 if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
332 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
333 $expect = "(bla()) ((l)u((e))) (l(e)e)";
339 (?{ $c = 1 }) # Initialize
341 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
343 ) # Fail: will unwind one iteration back
346 [^()]+ # Match a big chunk
349 ) # Do not try to match subchunks
357 )+ # This may not match with different subblocks
362 ) # Otherwise the chunk 1 may succeed with $c>0
367 push @ans, $res while $res = matchit;
369 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
375 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
380 $matched = qr/\((?:(?>[^()]+)|(?p{$matched}))*\)/;
383 push(@ans, $res), push(@ans1, $&) while $res = m/$matched/g;
385 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
389 print "# ans1='@ans1'\n# expect='$expect'\nnot " if "@ans1" ne $expect;
395 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
399 @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
400 print "not " if "@ans" ne 'a/ b';
404 $code = '{$blah = 45}';
407 print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
411 for $code ('{$blah = 45}','=xx') {
413 $res = eval { "xx" =~ /(?$code)/o };
414 if ($code eq '=xx') {
415 print "#'$@','$res','$blah'\nnot " unless not $@ and $res;
417 print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
423 $code = '{$blah = 45}';
426 print "not " if $blah != 45;
432 print "not " if $blah != 45;
438 print "not " unless pos($x) == 2;
443 print "not " unless pos($x) == 2;
453 print "not " unless f(pos($x)) == 4;
458 'foot' =~ /foo(?{$x = 12; 75})[t]/;
459 print "not " unless $^R eq '75';
464 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
465 print "not " unless $^R eq '67' and $x eq '12';
470 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
471 print "not " unless $^R eq '79' and $x eq '12';
475 print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
479 print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
483 print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
487 print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
491 print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
495 print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
500 foreach $ans ('', 'c') {
501 /(?<=(?=a)..)((?=c)|.)/g;
502 print "not " unless $1 eq $ans;
508 foreach $ans ('', 'a', '') {
510 print "not " unless $& eq $ans;
516 my($v,$a,$b,$res) = @_;
518 print "not " unless $res eq $v;
522 prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
523 prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
527 print "not " unless $1 and /$1/;
534 print "not " unless $b eq '9';
540 print "not " unless $b eq '11';
547 print "not " unless $b eq '14';
552 $match = eval { /$a$c$a/ };
554 unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
563 '' =~ /(?{ $c = 4 })/;
564 print "not " unless $c == 4;
568 print "not " unless $c == 3;
573 my $warn_pat = shift;
574 return sub { print "not " unless $_[0] =~ /$warn_pat/ }
578 my ($warn_pat, $code) = @_;
579 local $^W; local %SIG;
580 eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code;
587 my $warn_pat = shift;
588 return sub { must_warn(must_warn_pat($warn_pat)) }
591 my $for_future = make_must_warn('reserved for future extensions');
593 &$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
594 &$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
595 &$for_future('q(a.[b].) =~ /[x[.foo.]]/');
597 # test if failure of patterns returns empty list
620 print "not " if $#+ != 0 or $#- != 0;
624 print "not " if $+[0] != 2 or $-[0] != 1;
629 if defined $+[1] or defined $-[1] or defined $+[2] or defined $-[2];
634 print "not " if $#+ != 2 or $#- != 2;
638 print "not " if $+[0] != 3 or $-[0] != 0;
642 print "not " if $+[1] != 2 or $-[1] != 1;
646 print "not " if $+[2] != 3 or $-[2] != 2;
651 if defined $+[3] or defined $-[3] or defined $+[4] or defined $-[4];
656 print "not " if $#+ != 3 or $#- != 3;
660 print "not " if $+[0] != 3 or $-[0] != 0;
664 print "not " if $+[1] != 2 or $-[1] != 1;
668 print "not " if $+[3] != 3 or $-[3] != 2;
673 if defined $+[2] or defined $-[2] or defined $+[4] or defined $-[4];
678 print "not " if $#+ != 1 or $#- != 1;
682 print "not " if $+[0] != 2 or $-[0] != 0;
686 print "not " if $+[1] != 2 or $-[1] != 1;
691 if defined $+[2] or defined $-[2] or defined $+[3] or defined $-[3];
695 # see if matching against temporaries (created via pp_helem()) is safe
696 { foo => "ok $test\n".$^X }->{foo} =~ /^(.*)\n/g;