This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
aec5f31d738e4cbf73bbb5b37e42c537cb0769a8
[perl5.git] / t / op / pat.t
1 #!./perl
2 #
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.
6
7 print "1..158\n";
8
9 BEGIN {
10     chdir 't' if -d 't';
11     @INC = "../lib" if -d "../lib";
12 }
13 eval 'use Config';          #  Defaults assumed if this fails
14
15 # XXX known to leak scalars
16 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
17
18 $x = "abc\ndef\n";
19
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";}
22
23 $* = 1;
24 if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";}
25 $* = 0;
26
27 $_ = '123';
28 if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";}
29
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";}
32
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";}
35
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";}
38
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";}
41
42 $_ = 'aaabbbccc';
43 if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') {
44         print "ok 13\n";
45 } else {
46         print "not ok 13\n";
47 }
48 if (/(a+b+c+)/ && $1 eq 'aaabbbccc') {
49         print "ok 14\n";
50 } else {
51         print "not ok 14\n";
52 }
53
54 if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";}
55
56 $_ = 'aaabccc';
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";}
59
60 $_ = 'aaaccc';
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";}
63
64 $_ = 'abcdef';
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";}
67
68 if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";}
69
70 if (/^$_$/) {print "ok 23\n";} else {print "not ok 23\n";}
71
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";}
74 $* = 0;
75
76 $XXX{123} = 123;
77 $XXX{234} = 234;
78 $XXX{345} = 345;
79
80 @XXX = ('ok 25','not ok 25', 'ok 26','not ok 26','not ok 27');
81 while ($_ = shift(@XXX)) {
82     ?(.*)? && (print $1,"\n");
83     /not/ && reset;
84     /not ok 26/ && reset 'X';
85 }
86
87 while (($key,$val) = each(%XXX)) {
88     print "not ok 27\n";
89     exit;
90 }
91
92 print "ok 27\n";
93
94 'cde' =~ /[^ab]*/;
95 'xyz' =~ //;
96 if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";}
97
98 $foo = '[^ab]*';
99 'cde' =~ /$foo/;
100 'xyz' =~ //;
101 if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";}
102
103 $foo = '[^ab]*';
104 'cde' =~ /$foo/;
105 'xyz' =~ /$null/;
106 if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";}
107
108 $_ = 'abcdefghi';
109 /def/;          # optimized up to cmd
110 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";}
111
112 /cde/ + 0;      # optimized only to spat
113 if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";}
114
115 /[d][e][f]/;    # not optimized
116 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 33\n";} else {print "not ok 33\n";}
117
118 $_ = 'now is the {time for all} good men to come to.';
119 / {([^}]*)}/;
120 if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";}
121
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";
132
133 $_ = "now is the time for all good men to come to.";
134 @words = /(\w+)/g;
135 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
136     ? "ok 44\n"
137     : "not ok 44\n";
138
139 @words = ();
140 while (/\w+/g) {
141     push(@words, $&);
142 }
143 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
144     ? "ok 45\n"
145     : "not ok 45\n";
146
147 @words = ();
148 pos = 0;
149 while (/to/g) {
150     push(@words, $&);
151 }
152 print join(':',@words) eq "to:to"
153     ? "ok 46\n"
154     : "not ok 46 `@words'\n";
155
156 pos $_ = 0;
157 @words = /to/g;
158 print join(':',@words) eq "to:to"
159     ? "ok 47\n"
160     : "not ok 47 `@words'\n";
161
162 $_ = "abcdefghi";
163
164 $pat1 = 'def';
165 $pat2 = '^def';
166 $pat3 = '.def.';
167 $pat4 = 'abc';
168 $pat5 = '^abc';
169 $pat6 = 'abc$';
170 $pat7 = 'ghi';
171 $pat8 = '\w*ghi';
172 $pat9 = 'ghi$';
173
174 $t1=$t2=$t3=$t4=$t5=$t6=$t7=$t8=$t9=0;
175
176 for $iter (1..5) {
177     $t1++ if /$pat1/o;
178     $t2++ if /$pat2/o;
179     $t3++ if /$pat3/o;
180     $t4++ if /$pat4/o;
181     $t5++ if /$pat5/o;
182     $t6++ if /$pat6/o;
183     $t7++ if /$pat7/o;
184     $t8++ if /$pat8/o;
185     $t9++ if /$pat9/o;
186 }
187
188 $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
189 print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n";
190
191 $xyz = 'xyz';
192 print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n";
193
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";
198
199
200 $_="abcfooabcbar";
201 $x=/abc/g;
202 print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x;
203 $x=/abc/g;
204 print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x;
205 $x=/abc/g;
206 print $x == 0 ? "ok 54\n" : "not ok 54\n";
207 pos = 0;
208 $x=/ABC/gi;
209 print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x;
210 $x=/ABC/gi;
211 print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x;
212 $x=/ABC/gi;
213 print $x == 0 ? "ok 57\n" : "not ok 57\n";
214 pos = 0;
215 $x=/abc/g;
216 print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x;
217 $x=/abc/g;
218 print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x;
219 $_ .= '';
220 @x=/abc/g;
221 print scalar @x == 2 ? "ok 60\n" : "not ok 60\n";
222
223 $_ = "abdc";
224 pos $_ = 2;
225 /\Gc/gc;
226 print "not " if (pos $_) != 2;
227 print "ok 61\n";
228 /\Gc/g;
229 print "not " if defined pos $_;
230 print "ok 62\n";
231
232 $out = 1;
233 'abc' =~ m'a(?{ $out = 2 })b';
234 print "not " if $out != 2;
235 print "ok 63\n";
236
237 $out = 1;
238 'abc' =~ m'a(?{ $out = 3 })c';
239 print "not " if $out != 1;
240 print "ok 64\n";
241
242 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
243 @out = /(?<!foo)bar./g;
244 print "not " if "@out" ne 'bar2 barf';
245 print "ok 65\n";
246
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;
250
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.
254
255 undef $@;
256 print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@;
257 print "ok 66\n";
258
259 undef $@;
260 print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@;
261 print "ok 67\n";
262
263 undef $@;
264 print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@;
265 print "ok 68\n";
266
267 undef $@;
268 eval "'aaa' =~ /a{1,$reg_infty}/";
269 print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%;
270 print "ok 69\n";
271
272 eval "'aaa' =~ /a{1,$reg_infty_p}/";
273 print "not "
274         if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%;
275 print "ok 70\n";
276 undef $@;
277
278 # Poke a couple more parse failures
279
280 $context = 'x' x 256;
281 eval qq("${context}y" =~ /(?<=$context)y/);
282 print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%;
283 print "ok 71\n";
284
285 # This one will fail when POSIX character classes do get implemented
286 {
287         my $w;
288         local $^W = 1;
289         local $SIG{__WARN__} = sub{$w = shift};
290         eval q('a' =~ /[[:alpha:]]/);
291         print "not " if $w !~ /^\QCharacter class syntax [: :] is reserved/;
292 }
293 print "ok 72\n";
294
295 # Long Monsters
296 $test = 73;
297 for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
298   $a = 'a' x $l;
299   print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
300   print "ok $test\n";
301   $test++;
302   
303   print "not " if "b$a=" =~ /a$a=/;
304   print "ok $test\n";
305   $test++;
306 }
307
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
319        );
320
321 for ( keys %ans ) {
322   print "# const-len `$_' not =>  $ans{$_}\nnot " 
323     if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
324   print "ok $test\n";
325   $test++;
326   print "# var-len   `$_' not =>  $ans{$_}\nnot " 
327     if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
328   print "ok $test\n";
329   $test++;
330 }
331
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)";
334
335 sub matchit { 
336   m/
337      (
338        \( 
339        (?{ $c = 1 })            # Initialize
340        (?:
341          (?(?{ $c == 0 })       # PREVIOUS iteration was OK, stop the loop
342            (?!
343            )                    # Fail: will unwind one iteration back
344          )          
345          (?:
346            [^()]+               # Match a big chunk
347            (?=
348              [()]
349            )                    # Do not try to match subchunks
350          |
351            \( 
352            (?{ ++$c })
353          |
354            \) 
355            (?{ --$c })
356          )
357        )+                       # This may not match with different subblocks
358      )
359      (?(?{ $c != 0 })
360        (?!
361        )                        # Fail
362      )                          # Otherwise the chunk 1 may succeed with $c>0
363    /xg;
364 }
365
366 push @ans, $res while $res = matchit;
367
368 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
369 print "ok $test\n";
370 $test++;
371
372 @ans = matchit;
373
374 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
375 print "ok $test\n";
376 $test++;
377
378 @ans = ('a/b' =~ m%(.*/)?(.*)%);        # Stack may be bad
379 print "not " if "@ans" ne 'a/ b';
380 print "ok $test\n";
381 $test++;
382
383 $code = '{$blah = 45}';
384 $blah = 12;
385 eval { /(?$code)/ };
386 print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
387 print "ok $test\n";
388 $test++;
389
390 for $code ('{$blah = 45}','=xx') {
391   $blah = 12;
392   $res = eval { "xx" =~ /(?$code)/o };
393   if ($code eq '=xx') {
394     print "#'$@','$res','$blah'\nnot " unless not $@ and $res;
395   } else {
396     print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;    
397   }
398   print "ok $test\n";
399   $test++;
400 }
401
402 $code = '{$blah = 45}';
403 $blah = 12;
404 eval "/(?$code)/";                      
405 print "not " if $blah != 45;
406 print "ok $test\n";
407 $test++;
408
409 $blah = 12;
410 /(?{$blah = 45})/;                      
411 print "not " if $blah != 45;
412 print "ok $test\n";
413 $test++;
414
415 $x = 'banana';
416 $x =~ /.a/g;
417 print "not " unless pos($x) == 2;
418 print "ok $test\n";
419 $test++;
420
421 $x =~ /.z/gc;
422 print "not " unless pos($x) == 2;
423 print "ok $test\n";
424 $test++;
425
426 sub f {
427     my $p = $_[0];
428     return $p;
429 }
430
431 $x =~ /.a/g;
432 print "not " unless f(pos($x)) == 4;
433 print "ok $test\n";
434 $test++;
435
436 $x = $^R = 67;
437 'foot' =~ /foo(?{$x = 12; 75})[t]/;
438 print "not " unless $^R eq '75';
439 print "ok $test\n";
440 $test++;
441
442 $x = $^R = 67;
443 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
444 print "not " unless $^R eq '67' and $x eq '12';
445 print "ok $test\n";
446 $test++;
447
448 $x = $^R = 67;
449 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
450 print "not " unless $^R eq '79' and $x eq '12';
451 print "ok $test\n";
452 $test++;
453
454 print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
455 print "ok $test\n";
456 $test++;
457
458 print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
459 print "ok $test\n";
460 $test++;
461
462 print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
463 print "ok $test\n";
464 $test++;
465
466 print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
467 print "ok $test\n";
468 $test++;
469
470 print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
471 print "ok $test\n";
472 $test++;
473
474 print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
475 print "ok $test\n";
476 $test++;
477
478 $_ = 'xabcx';
479 foreach $ans ('', 'c') {
480   /(?<=(?=a)..)((?=c)|.)/g;
481   print "not " unless $1 eq $ans;
482   print "ok $test\n";
483   $test++;
484 }
485
486 $_ = 'a';
487 foreach $ans ('', 'a', '') {
488   /^|a|$/g;
489   print "not " unless $& eq $ans;
490   print "ok $test\n";
491   $test++;
492 }
493
494 sub prefixify {
495   my($v,$a,$b,$res) = @_; 
496   $v =~ s/\Q$a\E/$b/; 
497   print "not " unless $res eq $v; 
498   print "ok $test\n";
499   $test++;
500 }
501 prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
502 prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
503
504 $_ = 'var="foo"';
505 /(\")/;
506 print "not " unless $1 and /$1/;
507 print "ok $test\n";
508 $test++;
509
510 $a=qr/(?{++$b})/; 
511 $b = 7;
512 /$a$a/; 
513 print "not " unless $b eq '9'; 
514 print "ok $test\n";
515 $test++;
516
517 $c="$a"; 
518 /$a$a/; 
519 print "not " unless $b eq '11'; 
520 print "ok $test\n";
521 $test++;
522
523 {
524   use re "eval"; 
525   /$a$c$a/; 
526   print "not " unless $b eq '14'; 
527   print "ok $test\n";
528   $test++;
529
530   no re "eval"; 
531   $match = eval { /$a$c$a/ };
532   print "not " 
533     unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
534   print "ok $test\n";
535   $test++;
536 }
537
538 {
539   package aa;
540   $c = 2;
541   $::c = 3;
542   '' =~ /(?{ $c = 4 })/;
543   print "not " unless $c == 4;
544 }
545 print "ok $test\n";
546 $test++;
547 print "not " unless $c == 3;
548 print "ok $test\n";
549 $test++;  
550   
551 sub must_warn_pat {
552     my $warn_pat = shift;
553     return sub { print "not " unless $_[0] =~ /$warn_pat/ }
554 }
555
556 sub must_warn {
557     my ($warn_pat, $code) = @_;
558     local $^W; local %SIG;
559     eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code;
560     print "ok $test\n";
561     $test++;
562 }
563
564
565 sub make_must_warn {
566     my $warn_pat = shift;
567     return sub { must_warn(must_warn_pat($warn_pat)) }
568 }
569
570 my $for_future = make_must_warn('reserved for future extensions');
571
572 &$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
573 &$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
574 &$for_future('q(a.[b].) =~ /[x[.foo.]]/');
575
576 # test if failure of patterns returns empty list
577 $_ = 'aaa';
578 @_ = /bbb/;
579 print "not " if @_;
580 print "ok $test\n";
581 $test++;
582
583 @_ = /bbb/g;
584 print "not " if @_;
585 print "ok $test\n";
586 $test++;
587
588 @_ = /(bbb)/;
589 print "not " if @_;
590 print "ok $test\n";
591 $test++;
592
593 @_ = /(bbb)/g;
594 print "not " if @_;
595 print "ok $test\n";
596 $test++;
597
598 /a(?=.$)/;
599 print "not " if $#+ != 0 or $#- != 0;
600 print "ok $test\n";
601 $test++;
602
603 print "not " if $+[0] != 2 or $-[0] != 1;
604 print "ok $test\n";
605 $test++;
606
607 print "not " 
608    if defined $+[1] or defined $-[1] or defined $+[2] or defined $-[2];
609 print "ok $test\n";
610 $test++;
611
612 /a(a)(a)/;
613 print "not " if $#+ != 2 or $#- != 2;
614 print "ok $test\n";
615 $test++;
616
617 print "not " if $+[0] != 3 or $-[0] != 0;
618 print "ok $test\n";
619 $test++;
620
621 print "not " if $+[1] != 2 or $-[1] != 1;
622 print "ok $test\n";
623 $test++;
624
625 print "not " if $+[2] != 3 or $-[2] != 2;
626 print "ok $test\n";
627 $test++;
628
629 print "not " 
630    if defined $+[3] or defined $-[3] or defined $+[4] or defined $-[4];
631 print "ok $test\n";
632 $test++;
633
634 /.(a)(b)?(a)/;
635 print "not " if $#+ != 3 or $#- != 3;
636 print "ok $test\n";
637 $test++;
638
639 print "not " if $+[0] != 3 or $-[0] != 0;
640 print "ok $test\n";
641 $test++;
642
643 print "not " if $+[1] != 2 or $-[1] != 1;
644 print "ok $test\n";
645 $test++;
646
647 print "not " if $+[3] != 3 or $-[3] != 2;
648 print "ok $test\n";
649 $test++;
650
651 print "not " 
652    if defined $+[2] or defined $-[2] or defined $+[4] or defined $-[4];
653 print "ok $test\n";
654 $test++;
655
656 /.(a)/;
657 print "not " if $#+ != 1 or $#- != 1;
658 print "ok $test\n";
659 $test++;
660
661 print "not " if $+[0] != 2 or $-[0] != 0;
662 print "ok $test\n";
663 $test++;
664
665 print "not " if $+[1] != 2 or $-[1] != 1;
666 print "ok $test\n";
667 $test++;
668
669 print "not " 
670    if defined $+[2] or defined $-[2] or defined $+[3] or defined $-[3];
671 print "ok $test\n";
672 $test++;
673