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.
7 # $RCSfile: pat.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:12 $
13 @INC = "../lib" if -d "../lib";
15 eval 'use Config'; # Defaults assumed if this fails
17 $ENV{PERL_DESTRUCT_LEVEL} = 0; # XXX known to leaks scalars
21 if ($x =~ /^abc/) {print "ok 1\n";} else {print "not ok 1\n";}
22 if ($x !~ /^def/) {print "ok 2\n";} else {print "not ok 2\n";}
25 if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";}
29 if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";}
31 if ($x =~ /^xxx/) {print "not ok 5\n";} else {print "ok 5\n";}
32 if ($x !~ /^abc/) {print "not ok 6\n";} else {print "ok 6\n";}
34 if ($x =~ /def/) {print "ok 7\n";} else {print "not ok 7\n";}
35 if ($x !~ /def/) {print "not ok 8\n";} else {print "ok 8\n";}
37 if ($x !~ /.def/) {print "ok 9\n";} else {print "not ok 9\n";}
38 if ($x =~ /.def/) {print "not ok 10\n";} else {print "ok 10\n";}
40 if ($x =~ /\ndef/) {print "ok 11\n";} else {print "not ok 11\n";}
41 if ($x !~ /\ndef/) {print "not ok 12\n";} else {print "ok 12\n";}
44 if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') {
49 if (/(a+b+c+)/ && $1 eq 'aaabbbccc') {
55 if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";}
58 if (/a+b?c+/) {print "ok 16\n";} else {print "not ok 16\n";}
59 if (/a*b+c*/) {print "ok 17\n";} else {print "not ok 17\n";}
62 if (/a*b?c*/) {print "ok 18\n";} else {print "not ok 18\n";}
63 if (/a*b+c*/) {print "not ok 19\n";} else {print "ok 19\n";}
66 if (/bcd|xyz/) {print "ok 20\n";} else {print "not ok 20\n";}
67 if (/xyz|bcd/) {print "ok 21\n";} else {print "not ok 21\n";}
69 if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";}
71 if (/^$_$/) {print "ok 23\n";} else {print "not ok 23\n";}
73 $* = 1; # test 3 only tested the optimized version--this one is for real
74 if ("ab\ncd\n" =~ /^cd/) {print "ok 24\n";} else {print "not ok 24\n";}
81 @XXX = ('ok 25','not ok 25', 'ok 26','not ok 26','not ok 27');
82 while ($_ = shift(@XXX)) {
83 ?(.*)? && (print $1,"\n");
85 /not ok 26/ && reset 'X';
88 while (($key,$val) = each(%XXX)) {
97 if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";}
102 if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";}
107 if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";}
110 /def/; # optimized up to cmd
111 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";}
113 /cde/ + 0; # optimized only to spat
114 if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";}
116 /[d][e][f]/; # not optimized
117 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 33\n";} else {print "not ok 33\n";}
119 $_ = 'now is the {time for all} good men to come to.';
121 if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";}
123 $_ = 'xxx {3,4} yyy zzz';
124 print /( {3,4})/ ? "ok 35\n" : "not ok 35\n";
125 print $1 eq ' ' ? "ok 36\n" : "not ok 36\n";
126 print /( {4,})/ ? "not ok 37\n" : "ok 37\n";
127 print /( {2,3}.)/ ? "ok 38\n" : "not ok 38\n";
128 print $1 eq ' y' ? "ok 39\n" : "not ok 39\n";
129 print /(y{2,3}.)/ ? "ok 40\n" : "not ok 40\n";
130 print $1 eq 'yyy ' ? "ok 41\n" : "not ok 41\n";
131 print /x {3,4}/ ? "not ok 42\n" : "ok 42\n";
132 print /^xxx {3,4}/ ? "not ok 43\n" : "ok 43\n";
134 $_ = "now is the time for all good men to come to.";
136 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
144 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
153 print join(':',@words) eq "to:to"
155 : "not ok 46 `@words'\n";
159 print join(':',@words) eq "to:to"
161 : "not ok 47 `@words'\n";
175 $t1=$t2=$t3=$t4=$t5=$t6=$t7=$t8=$t9=0;
189 $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
190 print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n";
193 print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n";
195 # perl 4.009 says "unmatched ()"
196 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
197 print $@ eq "" ? "ok 50\n" : "not ok 50\n";
198 print $result eq "abc:bc" ? "ok 51\n" : "not ok 51\n";
203 print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x;
205 print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x;
207 print $x == 0 ? "ok 54\n" : "not ok 54\n";
210 print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x;
212 print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x;
214 print $x == 0 ? "ok 57\n" : "not ok 57\n";
217 print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x;
219 print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x;
222 print scalar @x == 2 ? "ok 60\n" : "not ok 60\n";
227 print "not " if (pos $_) != 2;
230 print "not " if defined pos $_;
234 'abc' =~ m'a(?{ $out = 2 })b';
235 print "not " if $out != 2;
239 'abc' =~ m'a(?{ $out = 3 })c';
240 print "not " if $out != 1;
243 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
244 @out = /(?<!foo)bar./g;
245 print "not " if "@out" ne 'bar2 barf';
248 # Tests which depend on REG_INFTY
249 $reg_infty = defined $Config{reg_infty} ? $Config{reg_infty} : 32767;
250 $reg_infty_m = $reg_infty - 1; $reg_infty_p = $reg_infty + 1;
252 # As well as failing if the pattern matches do unexpected things, the
253 # next three tests will fail if you should have picked up a lower-than-
254 # default value for $reg_infty from Config.pm, but have not.
257 print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@;
261 print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@;
265 print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@;
269 eval "'aaa' =~ /a{1,$reg_infty}/";
270 print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%;
273 eval "'aaa' =~ /a{1,$reg_infty_p}/";
275 if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%;
279 # Poke a couple more parse failures
281 $context = 'x' x 256;
282 eval qq("${context}y" =~ /(?<=$context)y/);
283 print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%;
286 # This one will fail when POSIX character classes do get implemented
290 local $SIG{__WARN__} = sub{$w = shift};
291 eval q('a' =~ /[[:alpha:]]/);
292 print "not " if $w !~ /^\QCharacter class syntax [: :] is reserved/;
298 for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
300 print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
304 print "not " if "b$a=" =~ /a$a=/;
309 # 20000 nodes, each taking 3 words per string, and 1 per branch
310 $long_constant_len = join '|', 12120 .. 32645;
311 $long_var_len = join '|', 8120 .. 28645;
312 %ans = ( 'ax13876y25677lbc' => 1,
313 'ax13876y25677mcb' => 0, # not b.
314 'ax13876y35677nbc' => 0, # Num too big
315 'ax13876y25677y21378obc' => 1,
316 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
317 'ax13876y25677y21378y21378kbc' => 1,
318 'ax13876y25677y21378y21378kcb' => 0, # Not b.
319 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
323 print "# const-len `$_' not => $ans{$_}\nnot "
324 if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
327 print "# var-len `$_' not => $ans{$_}\nnot "
328 if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
333 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
334 $expect = "(bla()) ((l)u((e))) (l(e)e)";
340 (?{ $c = 1 }) # Initialize
342 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
344 ) # Fail: will unwind one iteration back
347 [^()]+ # Match a big chunk
350 ) # Do not try to match subchunks
358 )+ # This may not match with different subblocks
363 ) # 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;
379 @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
380 print "not " if "@ans" ne 'a/ b';
384 $code = '{$blah = 45}';
387 print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
391 for $code ('{$blah = 45}','=xx') {
393 $res = eval { "xx" =~ /(?$code)/o };
394 if ($code eq '=xx') {
395 print "#'$@','$res','$blah'\nnot " unless not $@ and $res;
397 print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
403 $code = '{$blah = 45}';
406 print "not " if $blah != 45;
412 print "not " if $blah != 45;
418 print "not " unless pos($x) == 2;
423 print "not " unless pos($x) == 2;
433 print "not " unless f(pos($x)) == 4;
438 'foot' =~ /foo(?{$x = 12; 75})[t]/;
439 print "not " unless $^R eq '75';
444 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
445 print "not " unless $^R eq '67' and $x eq '12';
450 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
451 print "not " unless $^R eq '79' and $x eq '12';
455 print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
459 print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
463 print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
467 print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
471 print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
475 print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
480 foreach $ans ('', 'c') {
481 /(?<=(?=a)..)((?=c)|.)/g;
482 print "not " unless $1 eq $ans;
488 foreach $ans ('', 'a', '') {
490 print "not " unless $& eq $ans;
496 my($v,$a,$b,$res) = @_;
498 print "not " unless $res eq $v;
502 prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
503 prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
507 print "not " unless $1 and /$1/;
514 print "not " unless $b eq '9';
520 print "not " unless $b eq '11';
527 print "not " unless $b eq '14';
532 $match = eval { /$a$c$a/ };
534 unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
540 my $warn_pat = shift;
541 return sub { print "not " unless $_[0] =~ /$warn_pat/ }
545 my ($warn_pat, $code) = @_;
546 local $^W; local %SIG;
547 eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code;
554 my $warn_pat = shift;
555 return sub { must_warn(must_warn_pat($warn_pat)) }
558 my $for_future = make_must_warn('reserved for future extensions');
560 &$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
561 &$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
562 &$for_future('q(a.[b].) =~ /[x[.foo.]]/');