3 # This is a home for regular expression tests that don't fit into
4 # the format supported by re/regexp.t. If you want to add a test
5 # that does fit that format, add it to re/re_tests, not here.
19 @INC = ('../lib','.');
20 do "re/ReTest.pl" or die $@;
24 plan tests => 293; # Update this when adding/deleting tests.
26 run_tests() unless caller;
37 ok $x =~ /^abc/, qq ["$x" =~ /^abc/];
38 ok $x !~ /^def/, qq ["$x" !~ /^def/];
40 # used to be a test for $*
41 ok $x =~ /^def/m, qq ["$x" =~ /^def/m];
43 nok $x =~ /^xxx/, qq ["$x" =~ /^xxx/];
44 nok $x !~ /^abc/, qq ["$x" !~ /^abc/];
46 ok $x =~ /def/, qq ["$x" =~ /def/];
47 nok $x !~ /def/, qq ["$x" !~ /def/];
49 ok $x !~ /.def/, qq ["$x" !~ /.def/];
50 nok $x =~ /.def/, qq ["$x" =~ /.def/];
52 ok $x =~ /\ndef/, qq ["$x" =~ /\ndef/];
53 nok $x !~ /\ndef/, qq ["$x" !~ /\ndef/];
58 ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/];
63 ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc',
64 qq [\$_ = '$_'; /(a*b*)(c*)/];
65 ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/];
66 nok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
69 ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
70 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
73 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
74 nok /a*b+c*/, qq [\$_ = '$_'; /a*b+c*/];
77 ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/];
78 ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/];
79 ok m|bc/*d|, qq [\$_ = '$_'; m|bc/*d|];
80 ok /^$_$/, qq [\$_ = '$_'; /^\$_\$/];
84 # used to be a test for $*
85 ok "ab\ncd\n" =~ /^cd/m, qq ["ab\ncd\n" =~ /^cd/m];
89 our %XXX = map {($_ => $_)} 123, 234, 345;
91 our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3');
92 while ($_ = shift(@XXX)) {
93 my $f = index ($_, 'not') >= 0 ? \&nok : \&ok;
111 ok !keys %XXX, "%XXX is empty";
117 local $Message = "Test empty pattern";
132 no warnings 'uninitialized';
142 local $Message = q !Check $`, $&, $'!;
144 /def/; # optimized up to cmd
145 iseq "$`:$&:$'", 'abc:def:ghi';
148 /cde/ + 0; # optimized only to spat
149 iseq "$`:$&:$'", 'ab:cde:fghi';
151 /[d][e][f]/; # not optimized
152 iseq "$`:$&:$'", 'abc:def:ghi';
156 $_ = 'now is the {time for all} good men to come to.';
158 iseq $1, 'time for all', "Match braces";
162 local $Message = "{N,M} quantifier";
163 $_ = 'xxx {3,4} yyy zzz';
176 local $Message = "Test /g";
178 $_ = "now is the time for all good men to come to.";
179 my @words = /(\w+)/g;
180 my $exp = "now:is:the:time:for:all:good:men:to:come:to";
195 iseq "@words", "to:to";
199 iseq "@words", "to:to";
215 my $t1 = my $t2 = my $t3 = my $t4 = my $t5 =
216 my $t6 = my $t7 = my $t8 = my $t9 = 0;
218 for my $iter (1 .. 5) {
229 my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
230 iseq $x, '505550555', "Test /o";
236 ok "abc" =~ /^abc$|$xyz/, "| after \$";
238 # perl 4.009 says "unmatched ()"
239 local $Message = '$ inside ()';
242 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
243 iseq $@, "" or skip "eval failed", 1;
244 iseq $result, "abc:bc";
249 local $Message = "Scalar /g";
252 ok /abc/g && $` eq "";
253 ok /abc/g && $` eq "abcfoo";
256 local $Message = "Scalar /gi";
258 ok /ABC/gi && $` eq "";
259 ok /ABC/gi && $` eq "abcfoo";
262 local $Message = "Scalar /g";
264 ok /abc/g && $' eq "fooabcbar";
265 ok /abc/g && $' eq "bar";
269 iseq @x, 2, "/g reset after assignment";
273 local $Message = '/g, \G and pos';
283 local $Message = '(?{ })';
285 'abc' =~ m'a(?{ $out = 2 })b';
289 'abc' =~ m'a(?{ $out = 3 })c';
295 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
296 my @out = /(?<!foo)bar./g;
297 iseq "@out", 'bar2 barf', "Negative lookbehind";
301 local $Message = "REG_INFTY tests";
302 # Tests which depend on REG_INFTY
303 $::reg_infty = $Config {reg_infty} // 32767;
304 $::reg_infty_m = $::reg_infty - 1;
305 $::reg_infty_p = $::reg_infty + 1;
306 $::reg_infty_m = $::reg_infty_m; # Surpress warning.
308 # As well as failing if the pattern matches do unexpected things, the
309 # next three tests will fail if you should have picked up a lower-than-
310 # default value for $reg_infty from Config.pm, but have not.
312 eval_ok q (('aaa' =~ /(a{1,$::reg_infty_m})/)[0] eq 'aaa');
313 eval_ok q (('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/);
314 eval_ok q (('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/);
315 eval "'aaa' =~ /a{1,$::reg_infty}/";
316 ok $@ =~ /^\QQuantifier in {,} bigger than/;
317 eval "'aaa' =~ /a{1,$::reg_infty_p}/";
318 ok $@ =~ /^\QQuantifier in {,} bigger than/;
322 # Poke a couple more parse failures
323 my $context = 'x' x 256;
324 eval qq("${context}y" =~ /(?<=$context)y/);
325 ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
330 local $Message = "Long monster";
331 for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
333 local $Error = "length = $l";
334 ok "ba$a=" =~ /a$a=/;
335 nok "b$a=" =~ /a$a=/;
338 ok "ba$a=" =~ /b(?:a|b)+=/;
344 # 20000 nodes, each taking 3 words per string, and 1 per branch
345 my $long_constant_len = join '|', 12120 .. 32645;
346 my $long_var_len = join '|', 8120 .. 28645;
347 my %ans = ( 'ax13876y25677lbc' => 1,
348 'ax13876y25677mcb' => 0, # not b.
349 'ax13876y35677nbc' => 0, # Num too big
350 'ax13876y25677y21378obc' => 1,
351 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
352 'ax13876y25677y21378y21378kbc' => 1,
353 'ax13876y25677y21378y21378kcb' => 0, # Not b.
354 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
357 local $Message = "20000 nodes";
359 local $Error = "const-len '$_'";
360 ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o);
362 local $Error = "var-len '$_'";
363 ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o);
368 local $Message = "Complicated backtracking";
369 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
370 my $expect = "(bla()) ((l)u((e))) (l(e)e)";
377 (?{ $c = 1 }) # Initialize
379 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
381 ) # Fail: will unwind one iteration back
384 [^()]+ # Match a big chunk
387 ) # Do not try to match subchunks
395 )+ # This may not match with different subblocks
400 ) # Otherwise the chunk 1 may succeed with $c>0
406 push @ans, $res while $res = matchit;
407 iseq "@ans", "1 1 1";
410 iseq "@ans", $expect;
412 local $Message = "Recursion with (??{ })";
414 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
416 @ans = my @ans1 = ();
417 push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
419 iseq "@ans", "1 1 1";
420 iseq "@ans1", $expect;
423 iseq "@ans", $expect;
428 ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
432 my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
433 iseq "@ans", 'a/ b', "Stack may be bad";
437 local $Message = "Eval-group not allowed at runtime";
438 my $code = '{$blah = 45}';
441 ok $@ && $@ =~ /not allowed at runtime/ && $blah == 12;
443 for $code ('{$blah = 45}','=xx') {
445 my $res = eval { "xx" =~ /(?$code)/o };
446 no warnings 'uninitialized';
447 local $Error = "'$@', '$res', '$blah'";
448 if ($code eq '=xx') {
452 ok $@ && $@ =~ /not allowed at runtime/ && $blah == 12;
456 $code = '{$blah = 45}';
467 local $Message = "Pos checks";
481 iseq f (pos ($x)), 4;
485 local $Message = 'Checking $^R';
487 'foot' =~ /foo(?{$x = 12; 75})[t]/;
491 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
492 ok $^R eq '67' && $x eq '12';
495 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
496 ok $^R eq '79' && $x eq '12';
500 iseq qr/\b\v$/i, '(?i-xsm:\b\v$)', 'qr/\b\v$/i';
501 iseq qr/\b\v$/s, '(?s-xim:\b\v$)', 'qr/\b\v$/s';
502 iseq qr/\b\v$/m, '(?m-xis:\b\v$)', 'qr/\b\v$/m';
503 iseq qr/\b\v$/x, '(?x-ism:\b\v$)', 'qr/\b\v$/x';
504 iseq qr/\b\v$/xism, '(?msix:\b\v$)', 'qr/\b\v$/xism';
505 iseq qr/\b\v$/, '(?-xism:\b\v$)', 'qr/\b\v$/';
510 local $Message = "Look around";
513 foreach my $ans ('', 'c') {
514 ok /(?<=(?=a)..)((?=c)|.)/g or skip "Match failed", 1;
520 local $Message = "Empty clause";
522 foreach my $ans ('', 'a', '') {
523 ok /^|a|$/g or skip "Match failed", 1;
529 local $Message = "Prefixify";
532 my ($v, $a, $b, $res) = @_;
533 ok $v =~ s/\Q$a\E/$b/ or skip "Match failed", 1;
538 prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
539 prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
545 ok $1 && /$1/, "Capture a quote";
549 no warnings 'closure';
550 local $Message = '(?{ $var } refers to package vars';
554 '' =~ /(?{ $c = 4 })/;
560 must_die 'q(a:[b]:) =~ /[x[:foo:]]/',
561 'POSIX class \[:[^:]+:\] unknown in regex',
562 'POSIX class [: :] must have valid name';
564 for my $d (qw [= .]) {
565 must_die "/[[${d}foo${d}]]/",
566 "\QPOSIX syntax [$d $d] is reserved for future extensions",
567 "POSIX syntax [[$d $d]] is an error";
573 # test if failure of patterns returns empty list
574 local $Message = "Failed pattern returns empty list";
591 local $Message = '@- and @+ tests';
598 ok !defined $+ [1] && !defined $- [1] &&
599 !defined $+ [2] && !defined $- [2];
610 ok !defined $+ [3] && !defined $- [3] &&
611 !defined $+ [4] && !defined $- [4];
621 ok !defined $+ [2] && !defined $- [2] &&
622 !defined $+ [4] && !defined $- [4];
632 ok !defined $+ [2] && !defined $- [2] &&
633 !defined $+ [3] && !defined $- [3];
642 local $DiePattern = '^Modification of a read-only value attempted';
643 local $Message = 'Elements of @- and @+ are read-only';
644 must_die '$+[0] = 13';
645 must_die '$-[0] = 13';
646 must_die '@+ = (7, 6, 5)';
647 must_die '@- = qw (foo bar)';
652 local $Message = '\G testing';
664 ok $str =~ /\G../ && $& eq 'cd';
666 local $TODO = $running_as_thread;
667 ok $str =~ /.\G./ && $& eq 'bc';
672 local $Message = 'pos inside (?{ })';
675 ok $str =~ /b(?{$foo = $_; $bar = pos})c/;
678 ok !defined pos ($str);
683 ok $str =~ /b(?{$foo = $_; $bar = pos})c/g;
691 ok /b(?{$foo = $_; $bar = pos})c/;
697 ok /b(?{$foo = $_; $bar = pos})c/g;
705 1 while /b(?{$foo = $_; $bar = pos})c/g;
713 ok s/b(?{$foo = $_; $bar = pos})c/x/g;
714 iseq $foo, 'abcde|abcde';
716 iseq $_, 'axde|axde';
721 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
722 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
723 iseq "@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'";
726 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
727 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
728 iseq "@res", "'' 'ab' 'cde|abcde' " .
729 "'' 'abc' 'de|abcde' " .
730 "'abcd' 'e|' 'abcde' " .
731 "'abcde|' 'ab' 'cde' " .
732 "'abcde|' 'abc' 'de'" ;
737 local $Message = '\G anchor checks';
738 my $foo = 'aabbccddeeffgg';
741 local $TODO = $running_as_thread;
742 no warnings 'uninitialized';
743 ok $foo =~ /.\G(..)/g;
747 ok $foo =~ /.\G(..)/g;
751 ok $foo =~ /.\G(..)/g;
758 ok $foo =~ /\G(..)/g;
761 ok $foo =~ /\G(..)/g;
765 ok $foo =~ /\G(..)/g;
772 my @res = /(\d*|x)/g;
774 iseq "@res", "123||x|123|", "0 match in alternation";
779 local $Message = "Match against temporaries (created via pp_helem())" .
781 ok {foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g;
787 local $Message = 'package $i inside (?{ }), ' .
788 'saved substrings and changing $_';
789 our @a = qw [foo bar];
791 s/(\w)(?{push @b, $1})/,$1,/g for @a;
792 iseq "@b", "f o o b a r";
793 iseq "@a", ",f,,o,,o, ,b,,a,,r,";
795 local $Message = 'lexical $i inside (?{ }), ' .
796 'saved substrings and changing $_';
797 no warnings 'closure';
798 my @c = qw [foo bar];
800 s/(\w)(?{push @d, $1})/,$1,/g for @c;
801 iseq "@d", "f o o b a r";
802 iseq "@c", ",f,,o,,o, ,b,,a,,r,";
807 local $Message = 'Brackets';
810 { (?> [^{}]+ | (??{ $brackets }) )* }
813 ok "{{}" =~ $brackets;
815 ok "something { long { and } hairy" =~ $brackets;
817 ok "something { long { and } hairy" =~ m/((??{ $brackets }))/;
825 nok m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg';
830 local $Message = '\G anchor checks';
831 my $text = "aaXbXcc";
833 ok $text !~ /\GXb*X/g;
839 nok /^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"';
841 my $text = "abc dbf";
842 my @res = ($text =~ /.*?(b).*?\b/g);
843 iseq "@res", "b b", '\b is not special';
848 local $Message = '\S, [\S], \s, [\s]';
849 my @a = map chr, 0 .. 255;
850 my @b = grep m/\S/, @a;
851 my @c = grep m/[^\s]/, @a;
855 @c = grep /[\S]/, @a;
859 @c = grep /[^\S]/, @a;
863 @c = grep /[\s]/, @a;
867 local $Message = '\D, [\D], \d, [\d]';
868 my @a = map chr, 0 .. 255;
869 my @b = grep /\D/, @a;
870 my @c = grep /[^\d]/, @a;
874 @c = grep /[\D]/, @a;
878 @c = grep /[^\D]/, @a;
882 @c = grep /[\d]/, @a;
886 local $Message = '\W, [\W], \w, [\w]';
887 my @a = map chr, 0 .. 255;
888 my @b = grep /\W/, @a;
889 my @c = grep /[^\w]/, @a;
893 @c = grep /[\W]/, @a;
897 @c = grep /[^\W]/, @a;
901 @c = grep /[\w]/, @a;
907 # see if backtracking optimization works correctly
908 local $Message = 'Backtrack optimization';
909 ok "\n\n" =~ /\n $ \n/x;
910 ok "\n\n" =~ /\n* $ \n/x;
911 ok "\n\n" =~ /\n+ $ \n/x;
912 ok "\n\n" =~ /\n? $ \n/x;
913 ok "\n\n" =~ /\n*? $ \n/x;
914 ok "\n\n" =~ /\n+? $ \n/x;
915 ok "\n\n" =~ /\n?? $ \n/x;
916 ok "\n\n" !~ /\n*+ $ \n/x;
917 ok "\n\n" !~ /\n++ $ \n/x;
918 ok "\n\n" =~ /\n?+ $ \n/x;
924 use overload '""' => sub {'Object S'};
927 local $::Message = "Ref stringification";
928 ::ok do { \my $v} =~ /^SCALAR/, "Scalar ref stringification";
929 ::ok do {\\my $v} =~ /^REF/, "Ref ref stringification";
930 ::ok [] =~ /^ARRAY/, "Array ref stringification";
931 ::ok {} =~ /^HASH/, "Hash ref stringification";
932 ::ok 'S' -> new =~ /^Object S/, "Object stringification";
937 local $Message = "Test result of match used as match";
938 ok 'a1b' =~ ('xyz' =~ /y/);
940 ok 'a1b' =~ ('xyz' =~ /t/);
946 local $Message = '"1" is not \s';
947 may_not_warn sub {ok ("1\n" x 102) !~ /^\s*\n/m};
952 local $Message = '\s, [[:space:]] and [[:blank:]]';
953 my %space = (spc => " ",
958 # There's no \v but the vertical tabulator seems miraculously
959 # be 11 both in ASCII and EBCDIC.
963 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
964 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
965 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
967 iseq "@space0", "cr ff lf spc tab";
968 iseq "@space1", "cr ff lf spc tab vt";
969 iseq "@space2", "spc tab";
972 } # End of sub run_tests