add a few more PURIFY guards
[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
2cd61cdb 9print "1..130\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
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 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 81 ?(.*)? && (print $1,"\n");
82 /not/ && reset;
83 /not ok 26/ && reset 'X';
84}
85
a0d0e21e 86while (($key,$val) = each(%XXX)) {
378cc40b 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 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 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 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 156@words = /to/g;
157print join(':',@words) eq "to:to"
158 ? "ok 47\n"
71be2cbc 159 : "not ok 47 `@words'\n";
352d5a3a 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 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 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 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 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 221
222$_ = "abdc";
223pos $_ = 2;
c90c0ff4 224/\Gc/gc;
71be2cbc 225print "not " if (pos $_) != 2;
226print "ok 61\n";
c90c0ff4 227/\Gc/g;
228print "not " if defined pos $_;
229print "ok 62\n";
c277df42 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 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 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 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 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 384eval { /(?$code)/ };
385print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
e4d48cc9 386print "ok $test\n";
387$test++;
388
2cd61cdb 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 401$code = '{$blah = 45}';
402$blah = 12;
403eval "/(?$code)/";
cc6b7395 404print "not " if $blah != 45;
405print "ok $test\n";
406$test++;
407
408$blah = 12;
409/(?{$blah = 45})/;
c277df42 410print "not " if $blah != 45;
411print "ok $test\n";
412$test++;
413
74d6a13a 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 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
97197631 453# This should be changed to qr/\b\v$/ ASAP
1bd3ad17 454print "not " unless study(/\b\v$/) eq '(?:\bv$)';
97197631 455print "ok $test\n";
456$test++;
457
7e5428c5 458$_ = 'xabcx';
459foreach $ans ('', 'c') {
460 /(?<=(?=a)..)((?=c)|.)/g;
461 print "not " unless $1 eq $ans;
462 print "ok $test\n";
463 $test++;
464}
465
466$_ = 'a';
467foreach $ans ('', 'a', '') {
468 /^|a|$/g;
469 print "not " unless $& eq $ans;
470 print "ok $test\n";
471 $test++;
472}
473
09f25ae4 474sub prefixify {
475 my($v,$a,$b,$res) = @_;
476 $v =~ s/\Q$a\E/$b/;
477 print "not " unless $res eq $v;
478 print "ok $test\n";
479 $test++;
480}
481prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
482prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
483
484$_ = 'var="foo"';
485/(\")/;
486print "not " unless $1 and /$1/;
487print "ok $test\n";
488$test++;
489
2cd61cdb 490$a=study/(?{++$b})/;
491$b = 7;
492/$a$a/;
493print "not " unless $b eq '9';
494print "ok $test\n";
495$test++;
496
497$c="$a";
498/$a$a/;
499print "not " unless $b eq '11';
500print "ok $test\n";
501$test++;
502
503{
504 use re "eval";
505 /$a$c$a/;
506 print "not " unless $b eq '14';
507 print "ok $test\n";
508 $test++;
509
510 no re "eval";
511 $match = eval { /$a$c$a/ };
512 print "not "
513 unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
514 print "ok $test\n";
515 $test++;
516}
517
4599a1de 518sub must_warn_pat {
519 my $warn_pat = shift;
520 return sub { print "not " unless $_[0] =~ /$warn_pat/ }
521}
522
523sub must_warn {
524 my ($warn_pat, $code) = @_;
525 local $^W; local %SIG;
526 eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code;
527 print "ok $test\n";
528 $test++;
529}
530
531
532sub make_must_warn {
533 my $warn_pat = shift;
534 return sub { must_warn(must_warn_pat($warn_pat)) }
535}
536
537my $for_future = make_must_warn('reserved for future extensions');
538
539&$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
540&$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
541&$for_future('q(a.[b].) =~ /[x[.foo.]]/');