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. Tests for \N
6 # should be added here because they are treated as single quoted strings
7 # there, which means they avoid the lexer which otherwise would look at them.
20 @INC = ('../lib','.');
24 plan tests => 463; # Update this when adding/deleting tests.
26 run_tests() unless caller;
35 (my $x_pretty = $x) =~ s/\n/\\n/g;
37 ok $x =~ /^abc/, qq ["$x_pretty" =~ /^abc/];
38 ok $x !~ /^def/, qq ["$x_pretty" !~ /^def/];
40 # used to be a test for $*
41 ok $x =~ /^def/m, qq ["$x_pretty" =~ /^def/m];
43 ok(!($x =~ /^xxx/), qq ["$x_pretty" =~ /^xxx/]);
44 ok(!($x !~ /^abc/), qq ["$x_pretty" !~ /^abc/]);
46 ok $x =~ /def/, qq ["$x_pretty" =~ /def/];
47 ok(!($x !~ /def/), qq ["$x_pretty" !~ /def/]);
49 ok $x !~ /.def/, qq ["$x_pretty" !~ /.def/];
50 ok(!($x =~ /.def/), qq ["$x_pretty" =~ /.def/]);
52 ok $x =~ /\ndef/, qq ["$x_pretty" =~ /\\ndef/];
53 ok(!($x !~ /\ndef/), qq ["$x_pretty" !~ /\\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 unlike($_, qr/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 unlike($_, qr/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, q ["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 $e = index ($_, 'not') >= 0 ? '' : 1;
111 ok !keys %XXX, "%XXX is empty";
117 my $message = "Test empty pattern";
123 is($&, $xyz, $message);
128 is($&, $xyz, $message);
132 no warnings 'uninitialized';
134 is($&, $xyz, $message);
138 is($&, $xyz, $message);
142 my $message = q !Check $`, $&, $'!;
144 /def/; # optimized up to cmd
145 is("$`:$&:$'", 'abc:def:ghi', $message);
148 /cde/ + 0; # optimized only to spat
149 is("$`:$&:$'", 'ab:cde:fghi', $message);
151 /[d][e][f]/; # not optimized
152 is("$`:$&:$'", 'abc:def:ghi', $message);
156 $_ = 'now is the {time for all} good men to come to.';
158 is($1, 'time for all', "Match braces");
162 my $message = "{N,M} quantifier";
163 $_ = 'xxx {3,4} yyy zzz';
164 ok(/( {3,4})/, $message);
165 is($1, ' ', $message);
166 unlike($_, qr/( {4,})/, $message);
167 ok(/( {2,3}.)/, $message);
168 is($1, ' y', $message);
169 ok(/(y{2,3}.)/, $message);
170 is($1, 'yyy ', $message);
171 unlike($_, qr/x {3,4}/, $message);
172 unlike($_, qr/^xxx {3,4}/, $message);
176 my $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";
182 is("@words", $exp, $message);
188 is("@words", $exp, $message);
195 is("@words", "to:to", $message);
199 is("@words", "to:to", $message);
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 is($x, '505550555', "Test /o");
235 ok "abc" =~ /^abc$|$xyz/, "| after \$";
237 # perl 4.009 says "unmatched ()"
238 my $message = '$ inside ()';
241 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
242 is($@, "", $message);
243 is($result, "abc:bc", $message);
247 my $message = "Scalar /g";
250 ok( /abc/g && $` eq "", $message);
251 ok( /abc/g && $` eq "abcfoo", $message);
252 ok(!/abc/g, $message);
254 $message = "Scalar /gi";
256 ok( /ABC/gi && $` eq "", $message);
257 ok( /ABC/gi && $` eq "abcfoo", $message);
258 ok(!/ABC/gi, $message);
260 $message = "Scalar /g";
262 ok( /abc/g && $' eq "fooabcbar", $message);
263 ok( /abc/g && $' eq "bar", $message);
267 is(@x, 2, "/g reset after assignment");
271 my $message = '/g, \G and pos';
275 is(pos $_, 2, $message);
277 is(pos $_, undef, $message);
281 my $message = '(?{ })';
283 'abc' =~ m'a(?{ $out = 2 })b';
284 is($out, 2, $message);
287 'abc' =~ m'a(?{ $out = 3 })c';
288 is($out, 1, $message);
292 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
293 my @out = /(?<!foo)bar./g;
294 is("@out", 'bar2 barf', "Negative lookbehind");
298 my $message = "REG_INFTY tests";
299 # Tests which depend on REG_INFTY
301 # Defaults assumed if this fails
302 eval { require Config; };
303 $::reg_infty = $Config::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; # Suppress 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 is(eval q{('aaa' =~ /(a{1,$::reg_infty_m})/)[0]}, 'aaa', $message);
313 is($@, '', $message);
314 is(eval q{('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/}, 1, $message);
315 is($@, '', $message);
316 isnt(q{('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/}, 1, $message);
317 is($@, '', $message);
319 eval "'aaa' =~ /a{1,$::reg_infty}/";
320 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
321 eval "'aaa' =~ /a{1,$::reg_infty_p}/";
322 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
326 # Poke a couple more parse failures
327 my $context = 'x' x 256;
328 eval qq("${context}y" =~ /(?<=$context)y/);
329 ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
334 for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
336 my $message = "Long monster, length = $l";
337 like("ba$a=", qr/a$a=/, $message);
338 unlike("b$a=", qr/a$a=/, $message);
339 like("b$a=", qr/ba+=/, $message);
341 like("ba$a=", qr/b(?:a|b)+=/, $message);
346 # 20000 nodes, each taking 3 words per string, and 1 per branch
347 my $long_constant_len = join '|', 12120 .. 32645;
348 my $long_var_len = join '|', 8120 .. 28645;
349 my %ans = ( 'ax13876y25677lbc' => 1,
350 'ax13876y25677mcb' => 0, # not b.
351 'ax13876y35677nbc' => 0, # Num too big
352 'ax13876y25677y21378obc' => 1,
353 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
354 'ax13876y25677y21378y21378kbc' => 1,
355 'ax13876y25677y21378y21378kcb' => 0, # Not b.
356 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
360 my $message = "20000 nodes, const-len '$_'";
361 ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o), $message;
363 $message = "20000 nodes, var-len '$_'";
364 ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o,), $message;
369 my $message = "Complicated backtracking";
370 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
371 my $expect = "(bla()) ((l)u((e))) (l(e)e)";
378 (?{ $c = 1 }) # Initialize
380 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
382 ) # Fail: will unwind one iteration back
385 [^()]+ # Match a big chunk
388 ) # Do not try to match subchunks
396 )+ # This may not match with different subblocks
401 ) # Otherwise the chunk 1 may succeed with $c>0
407 push @ans, $res while $res = matchit;
408 is("@ans", "1 1 1", $message);
411 is("@ans", $expect, $message);
413 $message = "Recursion with (??{ })";
415 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
417 @ans = my @ans1 = ();
418 push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
420 is("@ans", "1 1 1", $message);
421 is("@ans1", $expect, $message);
424 is("@ans", $expect, $message);
429 ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
433 my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
434 is("@ans", 'a/ b', "Stack may be bad");
438 my $message = "Eval-group not allowed at runtime";
439 my $code = '{$blah = 45}';
442 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
445 my $res = eval { "xx" =~ /(?$code)/o };
447 no warnings 'uninitialized';
448 chomp $@; my $message = "$message '$@', '$res', '$blah'";
449 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
454 $res = eval { "xx" =~ /(?$code)/o };
456 no warnings 'uninitialized';
457 my $message = "$message '$@', '$res', '$blah'";
458 ok(!$@ && $res, $message);
461 $code = '{$blah = 45}';
464 is($blah, 45, $message);
468 is($blah, 45, $message);
472 my $message = "Pos checks";
475 is(pos $x, 2, $message);
478 is(pos $x, 2, $message);
486 is(f (pos $x), 4, $message);
490 my $message = 'Checking $^R';
492 'foot' =~ /foo(?{$x = 12; 75})[t]/;
493 is($^R, 75, $message);
496 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
497 ok($^R eq '67' && $x eq '12', $message);
500 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
501 ok($^R eq '79' && $x eq '12', $message);
505 is(qr/\b\v$/i, '(?^i:\b\v$)', 'qr/\b\v$/i');
506 is(qr/\b\v$/s, '(?^s:\b\v$)', 'qr/\b\v$/s');
507 is(qr/\b\v$/m, '(?^m:\b\v$)', 'qr/\b\v$/m');
508 is(qr/\b\v$/x, '(?^x:\b\v$)', 'qr/\b\v$/x');
509 is(qr/\b\v$/xism, '(?^msix:\b\v$)', 'qr/\b\v$/xism');
510 is(qr/\b\v$/, '(?^:\b\v$)', 'qr/\b\v$/');
513 { # Test that charset modifier work, and are interpolated
514 is(qr/\b\v$/, '(?^:\b\v$)', 'Verify no locale, no unicode_strings gives default modifier');
515 is(qr/(?l:\b\v$)/, '(?^:(?l:\b\v$))', 'Verify infix l modifier compiles');
516 is(qr/(?u:\b\v$)/, '(?^:(?u:\b\v$))', 'Verify infix u modifier compiles');
517 is(qr/(?l)\b\v$/, '(?^:(?l)\b\v$)', 'Verify (?l) compiles');
518 is(qr/(?u)\b\v$/, '(?^:(?u)\b\v$)', 'Verify (?u) compiles');
520 my $dual = qr/\b\v$/;
522 my $locale = qr/\b\v$/;
523 is($locale, '(?^l:\b\v$)', 'Verify has l modifier when compiled under use locale');
526 use feature 'unicode_strings';
527 my $unicode = qr/\b\v$/;
528 is($unicode, '(?^u:\b\v$)', 'Verify has u modifier when compiled under unicode_strings');
529 is(qr/abc$dual/, '(?^u:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
530 is(qr/abc$locale/, '(?^u:abc(?^l:\b\v$))', 'Verify retains l when interpolated under unicode_strings');
532 no feature 'unicode_strings';
533 is(qr/abc$locale/, '(?^:abc(?^l:\b\v$))', 'Verify retains l when interpolated outside locale and unicode strings');
534 is(qr/def$unicode/, '(?^:def(?^u:\b\v$))', 'Verify retains u when interpolated outside locale and unicode strings');
537 is(qr/abc$dual/, '(?^l:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
538 is(qr/abc$unicode/, '(?^l:abc(?^u:\b\v$))', 'Verify retains u when interpolated under locale');
542 my $message = "Look around";
544 foreach my $ans ('', 'c') {
545 ok(/(?<=(?=a)..)((?=c)|.)/g, $message);
546 is($1, $ans, $message);
551 my $message = "Empty clause";
553 foreach my $ans ('', 'a', '') {
554 ok(/^|a|$/g, $message);
555 is($&, $ans, $message);
561 my $message = "Prefixify";
563 my ($v, $a, $b, $res) = @_;
564 ok($v =~ s/\Q$a\E/$b/, $message);
565 is($v, $res, $message);
569 prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
570 prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
576 ok $1 && /$1/, "Capture a quote";
580 no warnings 'closure';
581 my $message = '(?{ $var } refers to package vars';
585 '' =~ /(?{ $c = 4 })/;
586 main::is($c, 4, $message);
587 main::is($::c, 3, $message);
591 is(eval 'q(a:[b]:) =~ /[x[:foo:]]/', undef);
592 like ($@, qr/POSIX class \[:[^:]+:\] unknown in regex/,
593 'POSIX class [: :] must have valid name');
595 for my $d (qw [= .]) {
596 is(eval "/[[${d}foo${d}]]/", undef);
597 like ($@, qr/\QPOSIX syntax [$d $d] is reserved for future extensions/,
598 "POSIX syntax [[$d $d]] is an error");
603 # test if failure of patterns returns empty list
604 my $message = "Failed pattern returns empty list";
607 is("@_", "", $message);
610 is("@_", "", $message);
613 is("@_", "", $message);
616 is("@_", "", $message);
620 my $message = '@- and @+ tests';
623 is($#+, 0, $message);
624 is($#-, 0, $message);
625 is($+ [0], 2, $message);
626 is($- [0], 1, $message);
627 ok(!defined $+ [1] && !defined $- [1] &&
628 !defined $+ [2] && !defined $- [2], $message);
631 is($#+, 2, $message);
632 is($#-, 2, $message);
633 is($+ [0], 3, $message);
634 is($- [0], 0, $message);
635 is($+ [1], 2, $message);
636 is($- [1], 1, $message);
637 is($+ [2], 3, $message);
638 is($- [2], 2, $message);
639 ok(!defined $+ [3] && !defined $- [3] &&
640 !defined $+ [4] && !defined $- [4], $message);
642 # Exists has a special check for @-/@+ - bug 45147
643 ok(exists $-[0], $message);
644 ok(exists $+[0], $message);
645 ok(exists $-[2], $message);
646 ok(exists $+[2], $message);
647 ok(!exists $-[3], $message);
648 ok(!exists $+[3], $message);
649 ok(exists $-[-1], $message);
650 ok(exists $+[-1], $message);
651 ok(exists $-[-3], $message);
652 ok(exists $+[-3], $message);
653 ok(!exists $-[-4], $message);
654 ok(!exists $+[-4], $message);
657 is($#+, 3, $message);
658 is($#-, 3, $message);
659 is($+ [1], 2, $message);
660 is($- [1], 1, $message);
661 is($+ [3], 3, $message);
662 is($- [3], 2, $message);
663 ok(!defined $+ [2] && !defined $- [2] &&
664 !defined $+ [4] && !defined $- [4], $message);
667 is($#+, 1, $message);
668 is($#-, 1, $message);
669 is($+ [0], 2, $message);
670 is($- [0], 0, $message);
671 is($+ [1], 2, $message);
672 is($- [1], 1, $message);
673 ok(!defined $+ [2] && !defined $- [2] &&
674 !defined $+ [3] && !defined $- [3], $message);
677 is($#+, 2, $message);
678 is($#-, 1, $message);
681 foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)', '@- = qw (foo bar)') {
683 like($@, qr/^Modification of a read-only value attempted/,
684 'Elements of @- and @+ are read-only');
688 my $message = '\G testing';
692 is("@a", "a a", $message);
696 unlike($str, qr/^\G/, $message);
697 unlike($str, qr/^.\G/, $message);
698 like($str, qr/^..\G/, $message);
699 unlike($str, qr/^...\G/, $message);
700 ok($str =~ /\G../ && $& eq 'cd', $message);
702 local $::TODO = $::running_as_thread;
703 ok($str =~ /.\G./ && $& eq 'bc', $message);
707 my $message = 'pos inside (?{ })';
710 like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message);
711 is($foo, $str, $message);
712 is($bar, 2, $message);
713 is(pos $str, undef, $message);
718 ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message);
719 is($foo, $str, $message);
720 is($bar, 2, $message);
721 is(pos $str, 3, $message);
726 like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message);
727 is($foo, $str, $message);
728 is($bar, 2, $message);
732 ok(/b(?{$foo = $_; $bar = pos})c/g, $message);
733 is($foo, $str, $message);
734 is($bar, 2, $message);
735 is(pos, 3, $message);
740 1 while /b(?{$foo = $_; $bar = pos})c/g;
741 is($foo, $str, $message);
742 is($bar, 2, $message);
743 is(pos, undef, $message);
748 ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message);
749 is($foo, 'abcde|abcde', $message);
750 is($bar, 8, $message);
751 is($_, 'axde|axde', $message);
756 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
757 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
758 is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message);
761 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
762 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
763 is("@res", "'' 'ab' 'cde|abcde' " .
764 "'' 'abc' 'de|abcde' " .
765 "'abcd' 'e|' 'abcde' " .
766 "'abcde|' 'ab' 'cde' " .
767 "'abcde|' 'abc' 'de'", $message);
771 my $message = '\G anchor checks';
772 my $foo = 'aabbccddeeffgg';
775 local $::TODO = $::running_as_thread;
776 no warnings 'uninitialized';
777 ok($foo =~ /.\G(..)/g, $message);
778 is($1, 'ab', $message);
781 ok($foo =~ /.\G(..)/g, $message);
782 is($1, 'cc', $message);
785 ok($foo =~ /.\G(..)/g, $message);
786 is($1, 'de', $message);
788 ok($foo =~ /\Gef/g, $message);
792 ok($foo =~ /\G(..)/g, $message);
793 is($1, 'aa', $message);
795 ok($foo =~ /\G(..)/g, $message);
796 is($1, 'bb', $message);
799 ok($foo =~ /\G(..)/g, $message);
800 is($1, 'cd', $message);
805 my @res = /(\d*|x)/g;
807 is("@res", "123||x|123|", "0 match in alternation");
811 my $message = "Match against temporaries (created via pp_helem())" .
813 ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message);
814 is($1, "bar", $message);
818 my $message = 'package $i inside (?{ }), ' .
819 'saved substrings and changing $_';
820 our @a = qw [foo bar];
822 s/(\w)(?{push @b, $1})/,$1,/g for @a;
823 is("@b", "f o o b a r", $message);
824 is("@a", ",f,,o,,o, ,b,,a,,r,", $message);
826 $message = 'lexical $i inside (?{ }), ' .
827 'saved substrings and changing $_';
828 no warnings 'closure';
829 my @c = qw [foo bar];
831 s/(\w)(?{push @d, $1})/,$1,/g for @c;
832 is("@d", "f o o b a r", $message);
833 is("@c", ",f,,o,,o, ,b,,a,,r,", $message);
837 my $message = 'Brackets';
840 { (?> [^{}]+ | (??{ $brackets }) )* }
843 ok("{{}" =~ $brackets, $message);
844 is($&, "{}", $message);
845 ok("something { long { and } hairy" =~ $brackets, $message);
846 is($&, "{ and }", $message);
847 ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message);
848 is($&, "{ and }", $message);
854 ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg');
858 my $message = '\G anchor checks';
859 my $text = "aaXbXcc";
861 ok($text !~ /\GXb*X/g, $message);
866 unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"');
868 my $text = "abc dbf";
869 my @res = ($text =~ /.*?(b).*?\b/g);
870 is("@res", "b b", '\b is not special');
874 my $message = '\S, [\S], \s, [\s]';
875 my @a = map chr, 0 .. 255;
876 my @b = grep m/\S/, @a;
877 my @c = grep m/[^\s]/, @a;
878 is("@b", "@c", $message);
881 @c = grep /[\S]/, @a;
882 is("@b", "@c", $message);
885 @c = grep /[^\S]/, @a;
886 is("@b", "@c", $message);
889 @c = grep /[\s]/, @a;
890 is("@b", "@c", $message);
893 my $message = '\D, [\D], \d, [\d]';
894 my @a = map chr, 0 .. 255;
895 my @b = grep /\D/, @a;
896 my @c = grep /[^\d]/, @a;
897 is("@b", "@c", $message);
900 @c = grep /[\D]/, @a;
901 is("@b", "@c", $message);
904 @c = grep /[^\D]/, @a;
905 is("@b", "@c", $message);
908 @c = grep /[\d]/, @a;
909 is("@b", "@c", $message);
912 my $message = '\W, [\W], \w, [\w]';
913 my @a = map chr, 0 .. 255;
914 my @b = grep /\W/, @a;
915 my @c = grep /[^\w]/, @a;
916 is("@b", "@c", $message);
919 @c = grep /[\W]/, @a;
920 is("@b", "@c", $message);
923 @c = grep /[^\W]/, @a;
924 is("@b", "@c", $message);
927 @c = grep /[\w]/, @a;
928 is("@b", "@c", $message);
932 # see if backtracking optimization works correctly
933 my $message = 'Backtrack optimization';
934 like("\n\n", qr/\n $ \n/x, $message);
935 like("\n\n", qr/\n* $ \n/x, $message);
936 like("\n\n", qr/\n+ $ \n/x, $message);
937 like("\n\n", qr/\n? $ \n/x, $message);
938 like("\n\n", qr/\n*? $ \n/x, $message);
939 like("\n\n", qr/\n+? $ \n/x, $message);
940 like("\n\n", qr/\n?? $ \n/x, $message);
941 unlike("\n\n", qr/\n*+ $ \n/x, $message);
942 unlike("\n\n", qr/\n++ $ \n/x, $message);
943 like("\n\n", qr/\n?+ $ \n/x, $message);
948 use overload '""' => sub {'Object S'};
951 my $message = "Ref stringification";
952 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
953 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
954 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
955 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
956 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
960 my $message = "Test result of match used as match";
961 ok('a1b' =~ ('xyz' =~ /y/), $message);
962 is($`, 'a', $message);
963 ok('a1b' =~ ('xyz' =~ /t/), $message);
964 is($`, 'a', $message);
968 my $message = '"1" is not \s';
969 warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)},
970 undef, "$message (did not warn)");
974 my $message = '\s, [[:space:]] and [[:blank:]]';
975 my %space = (spc => " ",
980 # There's no \v but the vertical tabulator seems miraculously
981 # be 11 both in ASCII and EBCDIC.
985 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
986 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
987 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
989 is("@space0", "cr ff lf spc tab", $message);
990 is("@space1", "cr ff lf spc tab vt", $message);
991 is("@space2", "spc tab", $message);
996 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
997 # seem to be predictable. Slight changes to the test make it fail earlier or later.
998 foreach my $i (0 .. $n)
1001 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
1005 # we are actually testing that we dont die when executing these patterns
1008 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1010 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1011 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1012 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1013 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1016 # we are actually testing that we dont die when executing these patterns
1017 my $e = "B\x{f6}ck";
1018 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1020 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1021 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1022 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1023 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1028 # Tests for bug 77414.
1031 my $message = '\p property after empty * match';
1033 like("1", qr/\s*\pN/, $message);
1034 like("-", qr/\s*\p{Dash}/, $message);
1035 like(" ", qr/\w*\p{Blank}/, $message);
1038 like("1", qr/\s*\pN+/, $message);
1039 like("-", qr/\s*\p{Dash}{1}/, $message);
1040 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
1044 SKIP: { # Some constructs with Latin1 characters cause a utf8 string not
1045 # to match itself in non-utf8
1047 skip "Needs to be customized to run on EBCDIC", 6;
1050 my $pattern = my $utf8_pattern = qr/((\xc0)+,?)/;
1051 utf8::upgrade($utf8_pattern);
1052 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
1053 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
1054 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
1055 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
1057 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
1058 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
1059 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
1060 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
1063 SKIP: { # Make sure can override the formatting
1065 skip "Needs to be customized to run on EBCDIC", 2;
1067 use feature 'unicode_strings';
1068 ok "\xc0" =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1069 ok "\xc0" !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
1073 # Test that a regex followed by an operator and/or a statement modifier work
1074 # These tests use string-eval so that it reports a clean error when it fails
1075 # (without the string eval the test script might be unparseable)
1077 # Note: these test check the behaviour that currently is valid syntax
1078 # If a new regex modifier is added and a test fails then there is a backwards-compatibility issue
1079 # Note-2: a new deprecate warning was added for this with commit e6897b1a5db0410e387ccbf677e89fc4a1d8c97a
1080 # which indicate that this syntax will be removed in 5.16.
1081 # When this happens the tests can be removed
1083 foreach (['my $r = "a" =~ m/a/lt 2', 'm', 'lt'],
1084 ['my $r = "a" =~ m/a/le 1', 'm', 'le'],
1085 ['my $r = "a" =~ m/a/eq 1', 'm', 'eq'],
1086 ['my $r = "a" =~ m/a/ne 0', 'm', 'ne'],
1087 ['my $r = "a" =~ m/a/and 1', 'm', 'and'],
1088 ['my $r = "a" =~ m/a/unless 0', 'm', 'unless'],
1089 ['my $c = 1; my $r; $r = "a" =~ m/a/while $c--', 'm', 'while'],
1090 ['my $c = 0; my $r; $r = "a" =~ m/a/until $c++', 'm', 'until'],
1091 ['my $r; $r = "a" =~ m/a/for 1', 'm', 'for'],
1092 ['my $r; $r = "a" =~ m/a/foreach 1', 'm', 'foreach'],
1094 ['my $t = "a"; my $r = $t =~ s/a//lt 2', 's', 'lt'],
1095 ['my $t = "a"; my $r = $t =~ s/a//le 1', 's', 'le'],
1096 ['my $t = "a"; my $r = $t =~ s/a//ne 0', 's', 'ne'],
1097 ['my $t = "a"; my $r = $t =~ s/a//and 1', 's', 'and'],
1098 ['my $t = "a"; my $r = $t =~ s/a//unless 0', 's', 'unless'],
1100 ['my $c = 1; my $r; my $t = "a"; $r = $t =~ s/a//while $c--', 's', 'while'],
1101 ['my $c = 0; my $r; my $t = "a"; $r = $t =~ s/a//until $c++', 's', 'until'],
1102 ['my $r; my $t = "a"; $r = $t =~ s/a//for 1', 's', 'for'],
1103 ['my $r; my $t = "a"; $r = $t =~ s/a//for 1', 's', 'foreach'],
1105 my $message = sprintf 'regex (%s) followed by $_->[2]',
1106 $_->[1] eq 'm' ? 'm//' : 's///';
1107 my $code = "$_->[0]; 'eval_ok ' . \$r";
1109 no warnings 'syntax';
1112 is($@, '', $message);
1113 is($result, 'eval_ok 1', $message);
1121 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
1124 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
1129 local $::TODO = "[perl #38133]";
1131 "A" =~ /(((?:A))?)+/;
1137 is($first, $second);
1141 # RT #3516: \G in a m//g expression causes problems
1143 while ("abc" =~ m/(\G[ac])?/g) {
1144 last if $count++ > 10;
1146 ok($count < 10, 'RT #3516 A');
1149 while ("abc" =~ m/(\G|.)[ac]/g) {
1150 last if $count++ > 10;
1152 ok($count < 10, 'RT #3516 B');
1155 while ("abc" =~ m/(\G?[ac])?/g) {
1156 last if $count++ > 10;
1158 ok($count < 10, 'RT #3516 C');
1161 # RT #84294: Is this a bug in the simple Perl regex?
1162 # : Nested buffers and (?{...}) dont play nicely on partial matches
1164 ok("ab" =~ /((\w+)(?{ push @got, $2 })){2}/,"RT #84294: Pattern should match");
1165 my $want= "'ab', 'a', 'b'";
1166 my $got= join(", ", map { defined($_) ? "'$_'" : "undef" } @got);
1167 is($got,$want,'RT #84294: check that "ab" =~ /((\w+)(?{ push @got, $2 })){2}/ leaves @got in the correct state');
1171 # Suppress warnings, as the non-unicode one comes out even if turn off
1172 # warnings here (because the execution is done in another scope).
1173 local $SIG{__WARN__} = sub {};
1174 my $str = "\x{110000}";
1176 # No non-unicode code points match any Unicode property, even inverse
1178 unlike($str, qr/\p{ASCII_Hex_Digit=True}/, "Non-Unicode doesn't match \\p{}");
1179 unlike($str, qr/\p{ASCII_Hex_Digit=False}/, "Non-Unicode doesn't match \\p{}");
1180 like($str, qr/\P{ASCII_Hex_Digit=True}/, "Non-Unicode matches \\P{}");
1181 like($str, qr/\P{ASCII_Hex_Digit=False}/, "Non-Unicode matches \\P{}");
1185 # Test that IDstart works, but doing because the author (khw) knows
1186 # regexes much better than the rest of the core, it is being done here
1187 # in the context of a regex which relies on buffer names beginng with
1191 like($str, qr/(?<a>abc)/, "'a' is legal IDStart");
1192 like($str, qr/(?<_>abc)/, "'_' is legal IDStart");
1193 like($str, qr/(?<ß>abc)/, "U+00DF is legal IDStart");
1194 like($str, qr/(?<ℕ>abc)/, "U+2115' is legal IDStart");
1196 # This test works on Unicode 6.0 in which U+2118 and U+212E are legal
1197 # IDStarts there, but are not Word characters, and therefore Perl
1198 # doesn't allow them to be IDStarts. But there is no guarantee that
1199 # Unicode won't change things around in the future so that at some
1200 # future Unicode revision these tests would need to be revised.
1201 foreach my $char ("%", "×", chr(0x2118), chr(0x212E)) {
1204 "abc" =~ qr/(?<$char>abc)/;
1206 utf8::encode($prog);
1207 fresh_perl_like($prog, qr!Sequence.* not recognized!, "",
1208 sprintf("'U+%04X not legal IDFirst'", ord($char)));
1211 } # End of sub run_tests