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