This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
added patch, tweaked PERL_OBJECT things
[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 # $RCSfile: pat.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:12 $
8
9 print "1..135\n";
10
11 BEGIN {
12     chdir 't' if -d 't';
13     @INC = "../lib" if -d "../lib";
14 }
15 eval 'use Config';          #  Defaults assumed if this fails
16
17 $x = "abc\ndef\n";
18
19 if ($x =~ /^abc/) {print "ok 1\n";} else {print "not ok 1\n";}
20 if ($x !~ /^def/) {print "ok 2\n";} else {print "not ok 2\n";}
21
22 $* = 1;
23 if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";}
24 $* = 0;
25
26 $_ = '123';
27 if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";}
28
29 if ($x =~ /^xxx/) {print "not ok 5\n";} else {print "ok 5\n";}
30 if ($x !~ /^abc/) {print "not ok 6\n";} else {print "ok 6\n";}
31
32 if ($x =~ /def/) {print "ok 7\n";} else {print "not ok 7\n";}
33 if ($x !~ /def/) {print "not ok 8\n";} else {print "ok 8\n";}
34
35 if ($x !~ /.def/) {print "ok 9\n";} else {print "not ok 9\n";}
36 if ($x =~ /.def/) {print "not ok 10\n";} else {print "ok 10\n";}
37
38 if ($x =~ /\ndef/) {print "ok 11\n";} else {print "not ok 11\n";}
39 if ($x !~ /\ndef/) {print "not ok 12\n";} else {print "ok 12\n";}
40
41 $_ = 'aaabbbccc';
42 if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') {
43         print "ok 13\n";
44 } else {
45         print "not ok 13\n";
46 }
47 if (/(a+b+c+)/ && $1 eq 'aaabbbccc') {
48         print "ok 14\n";
49 } else {
50         print "not ok 14\n";
51 }
52
53 if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";}
54
55 $_ = 'aaabccc';
56 if (/a+b?c+/) {print "ok 16\n";} else {print "not ok 16\n";}
57 if (/a*b+c*/) {print "ok 17\n";} else {print "not ok 17\n";}
58
59 $_ = 'aaaccc';
60 if (/a*b?c*/) {print "ok 18\n";} else {print "not ok 18\n";}
61 if (/a*b+c*/) {print "not ok 19\n";} else {print "ok 19\n";}
62
63 $_ = 'abcdef';
64 if (/bcd|xyz/) {print "ok 20\n";} else {print "not ok 20\n";}
65 if (/xyz|bcd/) {print "ok 21\n";} else {print "not ok 21\n";}
66
67 if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";}
68
69 if (/^$_$/) {print "ok 23\n";} else {print "not ok 23\n";}
70
71 $* = 1;         # test 3 only tested the optimized version--this one is for real
72 if ("ab\ncd\n" =~ /^cd/) {print "ok 24\n";} else {print "not ok 24\n";}
73 $* = 0;
74
75 $XXX{123} = 123;
76 $XXX{234} = 234;
77 $XXX{345} = 345;
78
79 @XXX = ('ok 25','not ok 25', 'ok 26','not ok 26','not ok 27');
80 while ($_ = shift(@XXX)) {
81     ?(.*)? && (print $1,"\n");
82     /not/ && reset;
83     /not ok 26/ && reset 'X';
84 }
85
86 while (($key,$val) = each(%XXX)) {
87     print "not ok 27\n";
88     exit;
89 }
90
91 print "ok 27\n";
92
93 'cde' =~ /[^ab]*/;
94 'xyz' =~ //;
95 if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";}
96
97 $foo = '[^ab]*';
98 'cde' =~ /$foo/;
99 'xyz' =~ //;
100 if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";}
101
102 $foo = '[^ab]*';
103 'cde' =~ /$foo/;
104 'xyz' =~ /$null/;
105 if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";}
106
107 $_ = 'abcdefghi';
108 /def/;          # optimized up to cmd
109 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";}
110
111 /cde/ + 0;      # optimized only to spat
112 if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";}
113
114 /[d][e][f]/;    # not optimized
115 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 33\n";} else {print "not ok 33\n";}
116
117 $_ = 'now is the {time for all} good men to come to.';
118 / {([^}]*)}/;
119 if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";}
120
121 $_ = 'xxx {3,4}  yyy   zzz';
122 print /( {3,4})/ ? "ok 35\n" : "not ok 35\n";
123 print $1 eq '   ' ? "ok 36\n" : "not ok 36\n";
124 print /( {4,})/ ? "not ok 37\n" : "ok 37\n";
125 print /( {2,3}.)/ ? "ok 38\n" : "not ok 38\n";
126 print $1 eq '  y' ? "ok 39\n" : "not ok 39\n";
127 print /(y{2,3}.)/ ? "ok 40\n" : "not ok 40\n";
128 print $1 eq 'yyy ' ? "ok 41\n" : "not ok 41\n";
129 print /x {3,4}/ ? "not ok 42\n" : "ok 42\n";
130 print /^xxx {3,4}/ ? "not ok 43\n" : "ok 43\n";
131
132 $_ = "now is the time for all good men to come to.";
133 @words = /(\w+)/g;
134 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
135     ? "ok 44\n"
136     : "not ok 44\n";
137
138 @words = ();
139 while (/\w+/g) {
140     push(@words, $&);
141 }
142 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
143     ? "ok 45\n"
144     : "not ok 45\n";
145
146 @words = ();
147 pos = 0;
148 while (/to/g) {
149     push(@words, $&);
150 }
151 print join(':',@words) eq "to:to"
152     ? "ok 46\n"
153     : "not ok 46 `@words'\n";
154
155 pos $_ = 0;
156 @words = /to/g;
157 print join(':',@words) eq "to:to"
158     ? "ok 47\n"
159     : "not ok 47 `@words'\n";
160
161 $_ = "abcdefghi";
162
163 $pat1 = 'def';
164 $pat2 = '^def';
165 $pat3 = '.def.';
166 $pat4 = 'abc';
167 $pat5 = '^abc';
168 $pat6 = 'abc$';
169 $pat7 = 'ghi';
170 $pat8 = '\w*ghi';
171 $pat9 = 'ghi$';
172
173 $t1=$t2=$t3=$t4=$t5=$t6=$t7=$t8=$t9=0;
174
175 for $iter (1..5) {
176     $t1++ if /$pat1/o;
177     $t2++ if /$pat2/o;
178     $t3++ if /$pat3/o;
179     $t4++ if /$pat4/o;
180     $t5++ if /$pat5/o;
181     $t6++ if /$pat6/o;
182     $t7++ if /$pat7/o;
183     $t8++ if /$pat8/o;
184     $t9++ if /$pat9/o;
185 }
186
187 $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
188 print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n";
189
190 $xyz = 'xyz';
191 print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n";
192
193 # perl 4.009 says "unmatched ()"
194 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
195 print $@ eq "" ? "ok 50\n" : "not ok 50\n";
196 print $result eq "abc:bc" ? "ok 51\n" : "not ok 51\n";
197
198
199 $_="abcfooabcbar";
200 $x=/abc/g;
201 print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x;
202 $x=/abc/g;
203 print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x;
204 $x=/abc/g;
205 print $x == 0 ? "ok 54\n" : "not ok 54\n";
206 pos = 0;
207 $x=/ABC/gi;
208 print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x;
209 $x=/ABC/gi;
210 print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x;
211 $x=/ABC/gi;
212 print $x == 0 ? "ok 57\n" : "not ok 57\n";
213 pos = 0;
214 $x=/abc/g;
215 print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x;
216 $x=/abc/g;
217 print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x;
218 $_ .= '';
219 @x=/abc/g;
220 print scalar @x == 2 ? "ok 60\n" : "not ok 60\n";
221
222 $_ = "abdc";
223 pos $_ = 2;
224 /\Gc/gc;
225 print "not " if (pos $_) != 2;
226 print "ok 61\n";
227 /\Gc/g;
228 print "not " if defined pos $_;
229 print "ok 62\n";
230
231 $out = 1;
232 'abc' =~ m'a(?{ $out = 2 })b';
233 print "not " if $out != 2;
234 print "ok 63\n";
235
236 $out = 1;
237 'abc' =~ m'a(?{ $out = 3 })c';
238 print "not " if $out != 1;
239 print "ok 64\n";
240
241 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
242 @out = /(?<!foo)bar./g;
243 print "not " if "@out" ne 'bar2 barf';
244 print "ok 65\n";
245
246 # Tests which depend on REG_INFTY
247 $reg_infty = defined $Config{reg_infty} ? $Config{reg_infty} : 32767;
248 $reg_infty_m = $reg_infty - 1; $reg_infty_p = $reg_infty + 1;
249
250 # As well as failing if the pattern matches do unexpected things, the
251 # next three tests will fail if you should have picked up a lower-than-
252 # default value for $reg_infty from Config.pm, but have not.
253
254 undef $@;
255 print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@;
256 print "ok 66\n";
257
258 undef $@;
259 print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@;
260 print "ok 67\n";
261
262 undef $@;
263 print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@;
264 print "ok 68\n";
265
266 undef $@;
267 eval "'aaa' =~ /a{1,$reg_infty}/";
268 print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%;
269 print "ok 69\n";
270
271 eval "'aaa' =~ /a{1,$reg_infty_p}/";
272 print "not "
273         if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%;
274 print "ok 70\n";
275 undef $@;
276
277 # Poke a couple more parse failures
278
279 $context = 'x' x 256;
280 eval qq("${context}y" =~ /(?<=$context)y/);
281 print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%;
282 print "ok 71\n";
283
284 # This one will fail when POSIX character classes do get implemented
285 {
286         my $w;
287         local $^W = 1;
288         local $SIG{__WARN__} = sub{$w = shift};
289         eval q('a' =~ /[[:alpha:]]/);
290         print "not " if $w !~ /^\QCharacter class syntax [: :] is reserved/;
291 }
292 print "ok 72\n";
293
294 # Long Monsters
295 $test = 73;
296 for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
297   $a = 'a' x $l;
298   print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
299   print "ok $test\n";
300   $test++;
301   
302   print "not " if "b$a=" =~ /a$a=/;
303   print "ok $test\n";
304   $test++;
305 }
306
307 # 20000 nodes, each taking 3 words per string, and 1 per branch
308 $long_constant_len = join '|', 12120 .. 32645;
309 $long_var_len = join '|', 8120 .. 28645;
310 %ans = ( 'ax13876y25677lbc' => 1,
311          'ax13876y25677mcb' => 0, # not b.
312          'ax13876y35677nbc' => 0, # Num too big
313          'ax13876y25677y21378obc' => 1,
314          'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
315          'ax13876y25677y21378y21378kbc' => 1,
316          'ax13876y25677y21378y21378kcb' => 0, # Not b.
317          'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
318        );
319
320 for ( keys %ans ) {
321   print "# const-len `$_' not =>  $ans{$_}\nnot " 
322     if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
323   print "ok $test\n";
324   $test++;
325   print "# var-len   `$_' not =>  $ans{$_}\nnot " 
326     if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
327   print "ok $test\n";
328   $test++;
329 }
330
331 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
332 $expect = "(bla()) ((l)u((e))) (l(e)e)";
333
334 sub matchit { 
335   m/
336      (
337        \( 
338        (?{ $c = 1 })            # Initialize
339        (?:
340          (?(?{ $c == 0 })       # PREVIOUS iteration was OK, stop the loop
341            (?!
342            )                    # Fail: will unwind one iteration back
343          )          
344          (?:
345            [^()]+               # Match a big chunk
346            (?=
347              [()]
348            )                    # Do not try to match subchunks
349          |
350            \( 
351            (?{ ++$c })
352          |
353            \) 
354            (?{ --$c })
355          )
356        )+                       # This may not match with different subblocks
357      )
358      (?(?{ $c != 0 })
359        (?!
360        )                        # Fail
361      )                          # Otherwise the chunk 1 may succeed with $c>0
362    /xg;
363 }
364
365 push @ans, $res while $res = matchit;
366
367 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
368 print "ok $test\n";
369 $test++;
370
371 @ans = matchit;
372
373 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
374 print "ok $test\n";
375 $test++;
376
377 @ans = ('a/b' =~ m%(.*/)?(.*)%);        # Stack may be bad
378 print "not " if "@ans" ne 'a/ b';
379 print "ok $test\n";
380 $test++;
381
382 $code = '{$blah = 45}';
383 $blah = 12;
384 eval { /(?$code)/ };
385 print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
386 print "ok $test\n";
387 $test++;
388
389 for $code ('{$blah = 45}','=xx') {
390   $blah = 12;
391   $res = eval { "xx" =~ /(?$code)/o };
392   if ($code eq '=xx') {
393     print "#'$@','$res','$blah'\nnot " unless not $@ and $res;
394   } else {
395     print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;    
396   }
397   print "ok $test\n";
398   $test++;
399 }
400
401 $code = '{$blah = 45}';
402 $blah = 12;
403 eval "/(?$code)/";                      
404 print "not " if $blah != 45;
405 print "ok $test\n";
406 $test++;
407
408 $blah = 12;
409 /(?{$blah = 45})/;                      
410 print "not " if $blah != 45;
411 print "ok $test\n";
412 $test++;
413
414 $x = 'banana';
415 $x =~ /.a/g;
416 print "not " unless pos($x) == 2;
417 print "ok $test\n";
418 $test++;
419
420 $x =~ /.z/gc;
421 print "not " unless pos($x) == 2;
422 print "ok $test\n";
423 $test++;
424
425 sub f {
426     my $p = $_[0];
427     return $p;
428 }
429
430 $x =~ /.a/g;
431 print "not " unless f(pos($x)) == 4;
432 print "ok $test\n";
433 $test++;
434
435 $x = $^R = 67;
436 'foot' =~ /foo(?{$x = 12; 75})[t]/;
437 print "not " unless $^R eq '75';
438 print "ok $test\n";
439 $test++;
440
441 $x = $^R = 67;
442 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
443 print "not " unless $^R eq '67' and $x eq '12';
444 print "ok $test\n";
445 $test++;
446
447 $x = $^R = 67;
448 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
449 print "not " unless $^R eq '79' and $x eq '12';
450 print "ok $test\n";
451 $test++;
452
453 print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
454 print "ok $test\n";
455 $test++;
456
457 print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
458 print "ok $test\n";
459 $test++;
460
461 print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
462 print "ok $test\n";
463 $test++;
464
465 print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
466 print "ok $test\n";
467 $test++;
468
469 print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
470 print "ok $test\n";
471 $test++;
472
473 print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
474 print "ok $test\n";
475 $test++;
476
477 $_ = 'xabcx';
478 foreach $ans ('', 'c') {
479   /(?<=(?=a)..)((?=c)|.)/g;
480   print "not " unless $1 eq $ans;
481   print "ok $test\n";
482   $test++;
483 }
484
485 $_ = 'a';
486 foreach $ans ('', 'a', '') {
487   /^|a|$/g;
488   print "not " unless $& eq $ans;
489   print "ok $test\n";
490   $test++;
491 }
492
493 sub prefixify {
494   my($v,$a,$b,$res) = @_; 
495   $v =~ s/\Q$a\E/$b/; 
496   print "not " unless $res eq $v; 
497   print "ok $test\n";
498   $test++;
499 }
500 prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
501 prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
502
503 $_ = 'var="foo"';
504 /(\")/;
505 print "not " unless $1 and /$1/;
506 print "ok $test\n";
507 $test++;
508
509 $a=qr/(?{++$b})/; 
510 $b = 7;
511 /$a$a/; 
512 print "not " unless $b eq '9'; 
513 print "ok $test\n";
514 $test++;
515
516 $c="$a"; 
517 /$a$a/; 
518 print "not " unless $b eq '11'; 
519 print "ok $test\n";
520 $test++;
521
522 {
523   use re "eval"; 
524   /$a$c$a/; 
525   print "not " unless $b eq '14'; 
526   print "ok $test\n";
527   $test++;
528
529   no re "eval"; 
530   $match = eval { /$a$c$a/ };
531   print "not " 
532     unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
533   print "ok $test\n";
534   $test++;
535 }
536   
537 sub must_warn_pat {
538     my $warn_pat = shift;
539     return sub { print "not " unless $_[0] =~ /$warn_pat/ }
540 }
541
542 sub must_warn {
543     my ($warn_pat, $code) = @_;
544     local $^W; local %SIG;
545     eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code;
546     print "ok $test\n";
547     $test++;
548 }
549
550
551 sub make_must_warn {
552     my $warn_pat = shift;
553     return sub { must_warn(must_warn_pat($warn_pat)) }
554 }
555
556 my $for_future = make_must_warn('reserved for future extensions');
557
558 &$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
559 &$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
560 &$for_future('q(a.[b].) =~ /[x[.foo.]]/');