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
CommitLineData
8d063cd8 1#!./perl
8d37f932
DD
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
GS
11BEGIN {
12 chdir 't' if -d 't';
13 @INC = "../lib" if -d "../lib";
14}
8d37f932 15eval 'use Config'; # Defaults assumed if this fails
8d063cd8
LW
16
17$x = "abc\ndef\n";
18
19if ($x =~ /^abc/) {print "ok 1\n";} else {print "not ok 1\n";}
20if ($x !~ /^def/) {print "ok 2\n";} else {print "not ok 2\n";}
21
22$* = 1;
23if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";}
24$* = 0;
25
26$_ = '123';
27if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";}
28
29if ($x =~ /^xxx/) {print "not ok 5\n";} else {print "ok 5\n";}
30if ($x !~ /^abc/) {print "not ok 6\n";} else {print "ok 6\n";}
31
32if ($x =~ /def/) {print "ok 7\n";} else {print "not ok 7\n";}
33if ($x !~ /def/) {print "not ok 8\n";} else {print "ok 8\n";}
34
35if ($x !~ /.def/) {print "ok 9\n";} else {print "not ok 9\n";}
36if ($x =~ /.def/) {print "not ok 10\n";} else {print "ok 10\n";}
37
38if ($x =~ /\ndef/) {print "ok 11\n";} else {print "not ok 11\n";}
39if ($x !~ /\ndef/) {print "not ok 12\n";} else {print "ok 12\n";}
40
41$_ = 'aaabbbccc';
42if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') {
43 print "ok 13\n";
44} else {
45 print "not ok 13\n";
46}
47if (/(a+b+c+)/ && $1 eq 'aaabbbccc') {
48 print "ok 14\n";
49} else {
50 print "not ok 14\n";
51}
52
53if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";}
54
55$_ = 'aaabccc';
56if (/a+b?c+/) {print "ok 16\n";} else {print "not ok 16\n";}
57if (/a*b+c*/) {print "ok 17\n";} else {print "not ok 17\n";}
58
59$_ = 'aaaccc';
60if (/a*b?c*/) {print "ok 18\n";} else {print "not ok 18\n";}
61if (/a*b+c*/) {print "not ok 19\n";} else {print "ok 19\n";}
62
63$_ = 'abcdef';
64if (/bcd|xyz/) {print "ok 20\n";} else {print "not ok 20\n";}
65if (/xyz|bcd/) {print "ok 21\n";} else {print "not ok 21\n";}
66
67if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";}
378cc40b
LW
68
69if (/^$_$/) {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
72if ("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');
c6aa4a32 80while ($_ = shift(@XXX)) {
378cc40b
LW
81 ?(.*)? && (print $1,"\n");
82 /not/ && reset;
83 /not ok 26/ && reset 'X';
84}
85
a0d0e21e 86while (($key,$val) = each(%XXX)) {
378cc40b
LW
87 print "not ok 27\n";
88 exit;
89}
90
91print "ok 27\n";
92
93'cde' =~ /[^ab]*/;
94'xyz' =~ //;
95if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";}
96
97$foo = '[^ab]*';
98'cde' =~ /$foo/;
99'xyz' =~ //;
100if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";}
101
102$foo = '[^ab]*';
103'cde' =~ /$foo/;
104'xyz' =~ /$null/;
105if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";}
a687059c
LW
106
107$_ = 'abcdefghi';
108/def/; # optimized up to cmd
109if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";}
110
111/cde/ + 0; # optimized only to spat
112if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";}
113
114/[d][e][f]/; # not optimized
115if ("$`:$&:$'" 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/ {([^}]*)}/;
119if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";}
120
121$_ = 'xxx {3,4} yyy zzz';
122print /( {3,4})/ ? "ok 35\n" : "not ok 35\n";
123print $1 eq ' ' ? "ok 36\n" : "not ok 36\n";
124print /( {4,})/ ? "not ok 37\n" : "ok 37\n";
125print /( {2,3}.)/ ? "ok 38\n" : "not ok 38\n";
126print $1 eq ' y' ? "ok 39\n" : "not ok 39\n";
127print /(y{2,3}.)/ ? "ok 40\n" : "not ok 40\n";
128print $1 eq 'yyy ' ? "ok 41\n" : "not ok 41\n";
129print /x {3,4}/ ? "not ok 42\n" : "ok 42\n";
130print /^xxx {3,4}/ ? "not ok 43\n" : "ok 43\n";
352d5a3a
LW
131
132$_ = "now is the time for all good men to come to.";
133@words = /(\w+)/g;
134print 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 = ();
139while (/\w+/g) {
140 push(@words, $&);
141}
142print 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 = ();
71be2cbc 147pos = 0;
352d5a3a
LW
148while (/to/g) {
149 push(@words, $&);
150}
151print join(':',@words) eq "to:to"
152 ? "ok 46\n"
71be2cbc 153 : "not ok 46 `@words'\n";
352d5a3a 154
71be2cbc 155pos $_ = 0;
352d5a3a
LW
156@words = /to/g;
157print join(':',@words) eq "to:to"
158 ? "ok 47\n"
71be2cbc 159 : "not ok 47 `@words'\n";
352d5a3a
LW
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
175for $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";
188print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n";
1462b684
LW
189
190$xyz = 'xyz';
191print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n";
192
193# perl 4.009 says "unmatched ()"
194eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
195print $@ eq "" ? "ok 50\n" : "not ok 50\n";
196print $result eq "abc:bc" ? "ok 51\n" : "not ok 51\n";
a0d0e21e
LW
197
198
199$_="abcfooabcbar";
200$x=/abc/g;
201print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x;
202$x=/abc/g;
203print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x;
204$x=/abc/g;
205print $x == 0 ? "ok 54\n" : "not ok 54\n";
71be2cbc 206pos = 0;
a0d0e21e
LW
207$x=/ABC/gi;
208print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x;
209$x=/ABC/gi;
210print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x;
211$x=/ABC/gi;
212print $x == 0 ? "ok 57\n" : "not ok 57\n";
71be2cbc 213pos = 0;
a0d0e21e
LW
214$x=/abc/g;
215print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x;
216$x=/abc/g;
217print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x;
218$_ .= '';
219@x=/abc/g;
220print scalar @x == 2 ? "ok 60\n" : "not ok 60\n";
71be2cbc
PP
221
222$_ = "abdc";
223pos $_ = 2;
c90c0ff4 224/\Gc/gc;
71be2cbc
PP
225print "not " if (pos $_) != 2;
226print "ok 61\n";
c90c0ff4
PP
227/\Gc/g;
228print "not " if defined pos $_;
229print "ok 62\n";
c277df42
IZ
230
231$out = 1;
232'abc' =~ m'a(?{ $out = 2 })b';
233print "not " if $out != 2;
234print "ok 63\n";
235
236$out = 1;
237'abc' =~ m'a(?{ $out = 3 })c';
238print "not " if $out != 1;
239print "ok 64\n";
240
241$_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
242@out = /(?<!foo)bar./g;
243print "not " if "@out" ne 'bar2 barf';
244print "ok 65\n";
245
8d37f932
DD
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
254undef $@;
255print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@;
256print "ok 66\n";
257
258undef $@;
259print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@;
260print "ok 67\n";
261
262undef $@;
263print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@;
264print "ok 68\n";
265
266undef $@;
267eval "'aaa' =~ /a{1,$reg_infty}/";
268print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%;
269print "ok 69\n";
270
271eval "'aaa' =~ /a{1,$reg_infty_p}/";
272print "not "
273 if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%;
274print "ok 70\n";
275undef $@;
276
277# Poke a couple more parse failures
278
279$context = 'x' x 256;
280eval qq("${context}y" =~ /(?<=$context)y/);
281print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%;
282print "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}
292print "ok 72\n";
293
c277df42 294# Long Monsters
8d37f932 295$test = 73;
c277df42
IZ
296for $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
320for ( 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
334sub matchit {
cc6b7395 335 m/
c277df42
IZ
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
cc6b7395 362 /xg;
c277df42
IZ
363}
364
365push @ans, $res while $res = matchit;
366
367print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
368print "ok $test\n";
369$test++;
370
371@ans = matchit;
372
373print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
374print "ok $test\n";
375$test++;
376
377@ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
378print "not " if "@ans" ne 'a/ b';
379print "ok $test\n";
380$test++;
381
cc6b7395 382$code = '{$blah = 45}';
c277df42 383$blah = 12;
2cd61cdb
IZ
384eval { /(?$code)/ };
385print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
e4d48cc9
GS
386print "ok $test\n";
387$test++;
388
2cd61cdb
IZ
389for $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
e4d48cc9
GS
401$code = '{$blah = 45}';
402$blah = 12;
403eval "/(?$code)/";
cc6b7395
IZ
404print "not " if $blah != 45;
405print "ok $test\n";
406$test++;
407
408$blah = 12;
409/(?{$blah = 45})/;
c277df42
IZ
410print "not " if $blah != 45;
411print "ok $test\n";
412$test++;
413
74d6a13a
MB
414$x = 'banana';
415$x =~ /.a/g;
416print "not " unless pos($x) == 2;
417print "ok $test\n";
418$test++;
419
420$x =~ /.z/gc;
421print "not " unless pos($x) == 2;
422print "ok $test\n";
423$test++;
424
425sub f {
426 my $p = $_[0];
427 return $p;
428}
429
430$x =~ /.a/g;
431print "not " unless f(pos($x)) == 4;
432print "ok $test\n";
433$test++;
4599a1de 434
ce862d02
IZ
435$x = $^R = 67;
436'foot' =~ /foo(?{$x = 12; 75})[t]/;
437print "not " unless $^R eq '75';
438print "ok $test\n";
439$test++;
440
441$x = $^R = 67;
442'foot' =~ /foo(?{$x = 12; 75})[xy]/;
443print "not " unless $^R eq '67' and $x eq '12';
444print "ok $test\n";
445$test++;
446
447$x = $^R = 67;
448'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
449print "not " unless $^R eq '79' and $x eq '12';
450print "ok $test\n";
451$test++;
452
8782bef2
GB
453print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
454print "ok $test\n";
455$test++;
456
457print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
458print "ok $test\n";
459$test++;
460
461print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
462print "ok $test\n";
463$test++;
464
465print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
466print "ok $test\n";
467$test++;
468
469print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
470print "ok $test\n";
471$test++;
472
473print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
97197631
IZ
474print "ok $test\n";
475$test++;
476
7e5428c5
IZ
477$_ = 'xabcx';
478foreach $ans ('', 'c') {
479 /(?<=(?=a)..)((?=c)|.)/g;
480 print "not " unless $1 eq $ans;
481 print "ok $test\n";
482 $test++;
483}
484
485$_ = 'a';
486foreach $ans ('', 'a', '') {
487 /^|a|$/g;
488 print "not " unless $& eq $ans;
489 print "ok $test\n";
490 $test++;
491}
492
09f25ae4
IZ
493sub 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}
500prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
501prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
502
503$_ = 'var="foo"';
504/(\")/;
505print "not " unless $1 and /$1/;
506print "ok $test\n";
507$test++;
508
8782bef2 509$a=qr/(?{++$b})/;
2cd61cdb
IZ
510$b = 7;
511/$a$a/;
512print "not " unless $b eq '9';
513print "ok $test\n";
514$test++;
515
516$c="$a";
517/$a$a/;
518print "not " unless $b eq '11';
519print "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
4599a1de
JH
537sub must_warn_pat {
538 my $warn_pat = shift;
539 return sub { print "not " unless $_[0] =~ /$warn_pat/ }
540}
541
542sub 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
551sub make_must_warn {
552 my $warn_pat = shift;
553 return sub { must_warn(must_warn_pat($warn_pat)) }
554}
555
556my $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.]]/');