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 unshift @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%;
290 for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
292 print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
296 print "not " if "b$a=" =~ /a$a=/;
301 # 20000 nodes, each taking 3 words per string, and 1 per branch
302 $long_constant_len = join '|', 12120 .. 32645;
303 $long_var_len = join '|', 8120 .. 28645;
304 %ans = ( 'ax13876y25677lbc' => 1,
305 'ax13876y25677mcb' => 0, # not b.
306 'ax13876y35677nbc' => 0, # Num too big
307 'ax13876y25677y21378obc' => 1,
308 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
309 'ax13876y25677y21378y21378kbc' => 1,
310 'ax13876y25677y21378y21378kcb' => 0, # Not b.
311 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
315 print "# const-len `$_' not => $ans{$_}\nnot "
316 if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
319 print "# var-len `$_' not => $ans{$_}\nnot "
320 if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
325 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
326 $expect = "(bla()) ((l)u((e))) (l(e)e)";
332 (?{ $c = 1 }) # Initialize
334 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
336 ) # Fail: will unwind one iteration back
339 [^()]+ # Match a big chunk
342 ) # Do not try to match subchunks
350 )+ # This may not match with different subblocks
355 ) # Otherwise the chunk 1 may succeed with $c>0
360 push @ans, $res while $res = matchit;
362 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
368 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
373 $matched = qr/\((?:(?>[^()]+)|(?p{$matched}))*\)/;
376 push(@ans, $res), push(@ans1, $&) while $res = m/$matched/g;
378 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
382 print "# ans1='@ans1'\n# expect='$expect'\nnot " if "@ans1" ne $expect;
388 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
392 @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
393 print "not " if "@ans" ne 'a/ b';
397 $code = '{$blah = 45}';
400 print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
404 for $code ('{$blah = 45}','=xx') {
406 $res = eval { "xx" =~ /(?$code)/o };
407 if ($code eq '=xx') {
408 print "#'$@','$res','$blah'\nnot " unless not $@ and $res;
410 print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
416 $code = '{$blah = 45}';
419 print "not " if $blah != 45;
425 print "not " if $blah != 45;
431 print "not " unless pos($x) == 2;
436 print "not " unless pos($x) == 2;
446 print "not " unless f(pos($x)) == 4;
451 'foot' =~ /foo(?{$x = 12; 75})[t]/;
452 print "not " unless $^R eq '75';
457 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
458 print "not " unless $^R eq '67' and $x eq '12';
463 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
464 print "not " unless $^R eq '79' and $x eq '12';
468 print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
472 print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
476 print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
480 print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
484 print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
488 print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
493 foreach $ans ('', 'c') {
494 /(?<=(?=a)..)((?=c)|.)/g;
495 print "not " unless $1 eq $ans;
501 foreach $ans ('', 'a', '') {
503 print "not " unless $& eq $ans;
509 my($v,$a,$b,$res) = @_;
511 print "not " unless $res eq $v;
515 prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
516 prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
520 print "not " unless $1 and /$1/;
527 print "not " unless $b eq '9';
533 print "not " unless $b eq '11';
540 print "not " unless $b eq '14';
545 $match = eval { /$a$c$a/ };
547 unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
556 '' =~ /(?{ $c = 4 })/;
557 print "not " unless $c == 4;
561 print "not " unless $c == 3;
566 my $warn_pat = shift;
567 return sub { print "not " unless $_[0] =~ /$warn_pat/ }
571 my ($warn_pat, $code) = @_;
572 local $^W; local %SIG;
573 eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code;
580 my $warn_pat = shift;
581 return sub { must_warn(must_warn_pat($warn_pat)) }
584 my $for_future = make_must_warn('reserved for future extensions');
586 &$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
587 &$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
588 &$for_future('q(a.[b].) =~ /[x[.foo.]]/');
590 # test if failure of patterns returns empty list
613 print "not " if $#+ != 0 or $#- != 0;
617 print "not " if $+[0] != 2 or $-[0] != 1;
622 if defined $+[1] or defined $-[1] or defined $+[2] or defined $-[2];
627 print "not " if $#+ != 2 or $#- != 2;
631 print "not " if $+[0] != 3 or $-[0] != 0;
635 print "not " if $+[1] != 2 or $-[1] != 1;
639 print "not " if $+[2] != 3 or $-[2] != 2;
644 if defined $+[3] or defined $-[3] or defined $+[4] or defined $-[4];
649 print "not " if $#+ != 3 or $#- != 3;
653 print "not " if $+[0] != 3 or $-[0] != 0;
657 print "not " if $+[1] != 2 or $-[1] != 1;
661 print "not " if $+[3] != 3 or $-[3] != 2;
666 if defined $+[2] or defined $-[2] or defined $+[4] or defined $-[4];
671 print "not " if $#+ != 1 or $#- != 1;
675 print "not " if $+[0] != 2 or $-[0] != 0;
679 print "not " if $+[1] != 2 or $-[1] != 1;
684 if defined $+[2] or defined $-[2] or defined $+[3] or defined $-[3];
689 print "#$#-..$#+\nnot " if $#+ != 2 or $#- != 1;
696 print "not " unless "@a" eq "a a";
703 print "not " if $str =~ /^\G/;
707 print "not " if $str =~ /^.\G/;
711 print "not " unless $str =~ /^..\G/;
715 print "not " if $str =~ /^...\G/;
719 print "not " unless $str =~ /.\G./ and $& eq 'bc';
723 print "not " unless $str =~ /\G../ and $& eq 'cd';
727 undef $foo; undef $bar;
728 print "#'$str','$foo','$bar'\nnot "
729 unless $str =~ /b(?{$foo = $_; $bar = pos})c/
730 and $foo eq 'abcde' and $bar eq 2;
734 undef $foo; undef $bar;
736 print "#'$str','$foo','$bar'\nnot "
737 unless $str =~ /b(?{$foo = $_; $bar = pos})c/g
738 and $foo eq 'abcde' and $bar eq 2 and pos $str eq 3;
744 undef $foo; undef $bar;
745 print "#'$str','$foo','$bar'\nnot "
746 unless /b(?{$foo = $_; $bar = pos})c/
747 and $foo eq 'abcde' and $bar eq 2;
751 undef $foo; undef $bar;
752 print "#'$str','$foo','$bar'\nnot "
753 unless /b(?{$foo = $_; $bar = pos})c/g
754 and $foo eq 'abcde' and $bar eq 2 and pos eq 3;
758 undef $foo; undef $bar;
760 1 while /b(?{$foo = $_; $bar = pos})c/g;
761 print "#'$str','$foo','$bar'\nnot "
762 unless $foo eq 'abcde' and $bar eq 2 and not defined pos;
766 undef $foo; undef $bar;
768 print "#'$str','$foo','$bar','$_'\nnot "
769 unless s/b(?{$foo = $_; $bar = pos})c/x/g and $foo eq 'abcde|abcde'
770 and $bar eq 8 and $_ eq 'axde|axde';
777 @dummy = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
778 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
780 print "#'@res' '$_'\nnot "
781 unless "@res" eq "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'";
786 @dummy = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
787 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
789 print "#'@res' '$_'\nnot "
791 "'' 'ab' 'cde|abcde' " .
792 "'' 'abc' 'de|abcde' " .
793 "'abcd' 'e|' 'abcde' " .
794 "'abcde|' 'ab' 'cde' " .
795 "'abcde|' 'abc' 'de'" ;
799 #Some more \G anchor checks
800 $foo='aabbccddeeffgg';
805 print "not " unless($1 eq 'ab');
811 print "not " unless($1 eq 'cc');
817 print "not " unless($1 eq 'de');
821 print "not " unless $foo =~ /\Gef/g;
828 print "not " unless($1 eq 'aa');
833 print "not " unless($1 eq 'bb');
839 print "not " unless($1 eq 'cd');
845 print "not " unless('123||x|123|' eq join '|', @res);
849 # see if matching against temporaries (created via pp_helem()) is safe
850 { foo => "ok $test\n".$^X }->{foo} =~ /^(.*)\n/g;
854 # See if $i work inside (?{}) in the presense of saved substrings and
858 s/(\w)(?{push @b, $1})/,$1,/g for @a;
860 print "# \@b='@b', expect 'f o o b a r'\nnot " unless("@b" eq "f o o b a r");
864 print "not " unless("@a" eq ",f,,o,,o, ,b,,a,,r,");
869 { (?> [^{}]+ | (?p{ $brackets }) )* }
873 print "ok $test\n"; # Did we survive?
876 "something { long { and } hairy" =~ $brackets;
877 print "ok $test\n"; # Did we survive?
880 "something { long { and } hairy" =~ m/((?p{ $brackets }))/;
881 print "not " unless $1 eq "{ and }";