SYN SYN
[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
22d4bb9c 7print "1..223\n";
8d37f932 8
e4d48cc9 9BEGIN {
10 chdir 't' if -d 't';
22d4bb9c 11 @INC = '../lib';
e4d48cc9 12}
8d37f932 13eval 'use Config'; # Defaults assumed if this fails
8d063cd8 14
9c63abab 15# XXX known to leak scalars
16$ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
eec2d3df 17
8d063cd8 18$x = "abc\ndef\n";
19
20if ($x =~ /^abc/) {print "ok 1\n";} else {print "not ok 1\n";}
21if ($x !~ /^def/) {print "ok 2\n";} else {print "not ok 2\n";}
22
23$* = 1;
24if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";}
25$* = 0;
26
27$_ = '123';
28if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";}
29
30if ($x =~ /^xxx/) {print "not ok 5\n";} else {print "ok 5\n";}
31if ($x !~ /^abc/) {print "not ok 6\n";} else {print "ok 6\n";}
32
33if ($x =~ /def/) {print "ok 7\n";} else {print "not ok 7\n";}
34if ($x !~ /def/) {print "not ok 8\n";} else {print "ok 8\n";}
35
36if ($x !~ /.def/) {print "ok 9\n";} else {print "not ok 9\n";}
37if ($x =~ /.def/) {print "not ok 10\n";} else {print "ok 10\n";}
38
39if ($x =~ /\ndef/) {print "ok 11\n";} else {print "not ok 11\n";}
40if ($x !~ /\ndef/) {print "not ok 12\n";} else {print "ok 12\n";}
41
42$_ = 'aaabbbccc';
43if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') {
44 print "ok 13\n";
45} else {
46 print "not ok 13\n";
47}
48if (/(a+b+c+)/ && $1 eq 'aaabbbccc') {
49 print "ok 14\n";
50} else {
51 print "not ok 14\n";
52}
53
54if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";}
55
56$_ = 'aaabccc';
57if (/a+b?c+/) {print "ok 16\n";} else {print "not ok 16\n";}
58if (/a*b+c*/) {print "ok 17\n";} else {print "not ok 17\n";}
59
60$_ = 'aaaccc';
61if (/a*b?c*/) {print "ok 18\n";} else {print "not ok 18\n";}
62if (/a*b+c*/) {print "not ok 19\n";} else {print "ok 19\n";}
63
64$_ = 'abcdef';
65if (/bcd|xyz/) {print "ok 20\n";} else {print "not ok 20\n";}
66if (/xyz|bcd/) {print "ok 21\n";} else {print "not ok 21\n";}
67
68if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";}
378cc40b 69
70if (/^$_$/) {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
73if ("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');
c6aa4a32 81while ($_ = shift(@XXX)) {
378cc40b 82 ?(.*)? && (print $1,"\n");
83 /not/ && reset;
84 /not ok 26/ && reset 'X';
85}
86
a0d0e21e 87while (($key,$val) = each(%XXX)) {
378cc40b 88 print "not ok 27\n";
89 exit;
90}
91
92print "ok 27\n";
93
94'cde' =~ /[^ab]*/;
95'xyz' =~ //;
96if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";}
97
98$foo = '[^ab]*';
99'cde' =~ /$foo/;
100'xyz' =~ //;
101if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";}
102
103$foo = '[^ab]*';
104'cde' =~ /$foo/;
105'xyz' =~ /$null/;
106if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";}
a687059c 107
108$_ = 'abcdefghi';
109/def/; # optimized up to cmd
110if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";}
111
112/cde/ + 0; # optimized only to spat
113if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";}
114
115/[d][e][f]/; # not optimized
116if ("$`:$&:$'" 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/ {([^}]*)}/;
120if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";}
121
122$_ = 'xxx {3,4} yyy zzz';
123print /( {3,4})/ ? "ok 35\n" : "not ok 35\n";
124print $1 eq ' ' ? "ok 36\n" : "not ok 36\n";
125print /( {4,})/ ? "not ok 37\n" : "ok 37\n";
126print /( {2,3}.)/ ? "ok 38\n" : "not ok 38\n";
127print $1 eq ' y' ? "ok 39\n" : "not ok 39\n";
128print /(y{2,3}.)/ ? "ok 40\n" : "not ok 40\n";
129print $1 eq 'yyy ' ? "ok 41\n" : "not ok 41\n";
130print /x {3,4}/ ? "not ok 42\n" : "ok 42\n";
131print /^xxx {3,4}/ ? "not ok 43\n" : "ok 43\n";
352d5a3a 132
133$_ = "now is the time for all good men to come to.";
134@words = /(\w+)/g;
135print 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 = ();
140while (/\w+/g) {
141 push(@words, $&);
142}
143print 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 = ();
71be2cbc 148pos = 0;
352d5a3a 149while (/to/g) {
150 push(@words, $&);
151}
152print join(':',@words) eq "to:to"
153 ? "ok 46\n"
71be2cbc 154 : "not ok 46 `@words'\n";
352d5a3a 155
71be2cbc 156pos $_ = 0;
352d5a3a 157@words = /to/g;
158print join(':',@words) eq "to:to"
159 ? "ok 47\n"
71be2cbc 160 : "not ok 47 `@words'\n";
352d5a3a 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
176for $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";
189print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n";
1462b684 190
191$xyz = 'xyz';
192print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n";
193
194# perl 4.009 says "unmatched ()"
195eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
196print $@ eq "" ? "ok 50\n" : "not ok 50\n";
197print $result eq "abc:bc" ? "ok 51\n" : "not ok 51\n";
a0d0e21e 198
199
200$_="abcfooabcbar";
201$x=/abc/g;
202print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x;
203$x=/abc/g;
204print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x;
205$x=/abc/g;
206print $x == 0 ? "ok 54\n" : "not ok 54\n";
71be2cbc 207pos = 0;
a0d0e21e 208$x=/ABC/gi;
209print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x;
210$x=/ABC/gi;
211print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x;
212$x=/ABC/gi;
213print $x == 0 ? "ok 57\n" : "not ok 57\n";
71be2cbc 214pos = 0;
a0d0e21e 215$x=/abc/g;
216print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x;
217$x=/abc/g;
218print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x;
219$_ .= '';
220@x=/abc/g;
221print scalar @x == 2 ? "ok 60\n" : "not ok 60\n";
71be2cbc 222
223$_ = "abdc";
224pos $_ = 2;
c90c0ff4 225/\Gc/gc;
71be2cbc 226print "not " if (pos $_) != 2;
227print "ok 61\n";
c90c0ff4 228/\Gc/g;
229print "not " if defined pos $_;
230print "ok 62\n";
c277df42 231
232$out = 1;
233'abc' =~ m'a(?{ $out = 2 })b';
234print "not " if $out != 2;
235print "ok 63\n";
236
237$out = 1;
238'abc' =~ m'a(?{ $out = 3 })c';
239print "not " if $out != 1;
240print "ok 64\n";
241
242$_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
243@out = /(?<!foo)bar./g;
244print "not " if "@out" ne 'bar2 barf';
245print "ok 65\n";
246
8d37f932 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
255undef $@;
256print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@;
257print "ok 66\n";
258
259undef $@;
260print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@;
261print "ok 67\n";
262
263undef $@;
264print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@;
265print "ok 68\n";
266
267undef $@;
268eval "'aaa' =~ /a{1,$reg_infty}/";
22d4bb9c 269print "not " if $@ !~ m%^\QQuantifier in {,} bigger than%;
8d37f932 270print "ok 69\n";
271
272eval "'aaa' =~ /a{1,$reg_infty_p}/";
273print "not "
22d4bb9c 274 if $@ !~ m%^\QQuantifier in {,} bigger than%;
8d37f932 275print "ok 70\n";
276undef $@;
277
278# Poke a couple more parse failures
279
280$context = 'x' x 256;
281eval qq("${context}y" =~ /(?<=$context)y/);
22d4bb9c 282print "not " if $@ !~ m%^\QLookbehind longer than 255 not%;
8d37f932 283print "ok 71\n";
284
b8c5462f 285# removed test
8d37f932 286print "ok 72\n";
287
c277df42 288# Long Monsters
8d37f932 289$test = 73;
c277df42 290for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
291 $a = 'a' x $l;
292 print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
293 print "ok $test\n";
294 $test++;
295
296 print "not " if "b$a=" =~ /a$a=/;
297 print "ok $test\n";
298 $test++;
299}
300
301# 20000 nodes, each taking 3 words per string, and 1 per branch
302$long_constant_len = join '|', 12120 .. 32645;
303$long_var_len = join '|', 8120 .. 28645;
304%ans = ( 'ax13876y25677lbc' => 1,
305 'ax13876y25677mcb' => 0, # not b.
306 'ax13876y35677nbc' => 0, # Num too big
307 'ax13876y25677y21378obc' => 1,
308 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
309 'ax13876y25677y21378y21378kbc' => 1,
310 'ax13876y25677y21378y21378kcb' => 0, # Not b.
311 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
312 );
313
314for ( keys %ans ) {
315 print "# const-len `$_' not => $ans{$_}\nnot "
316 if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
317 print "ok $test\n";
318 $test++;
319 print "# var-len `$_' not => $ans{$_}\nnot "
320 if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
321 print "ok $test\n";
322 $test++;
323}
324
325$_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
326$expect = "(bla()) ((l)u((e))) (l(e)e)";
327
328sub matchit {
cc6b7395 329 m/
c277df42 330 (
331 \(
332 (?{ $c = 1 }) # Initialize
333 (?:
334 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
335 (?!
336 ) # Fail: will unwind one iteration back
337 )
338 (?:
339 [^()]+ # Match a big chunk
340 (?=
341 [()]
342 ) # Do not try to match subchunks
343 |
344 \(
345 (?{ ++$c })
346 |
347 \)
348 (?{ --$c })
349 )
350 )+ # This may not match with different subblocks
351 )
352 (?(?{ $c != 0 })
353 (?!
354 ) # Fail
355 ) # Otherwise the chunk 1 may succeed with $c>0
cc6b7395 356 /xg;
c277df42 357}
358
0f5d15d6 359@ans = ();
c277df42 360push @ans, $res while $res = matchit;
361
362print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
363print "ok $test\n";
364$test++;
365
366@ans = matchit;
367
368print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
369print "ok $test\n";
370$test++;
371
96776eda 372print "not " unless "abc" =~ /^(??{"a"})b/;
373print "ok $test\n";
374$test++;
375
0f5d15d6 376my $matched;
14455d6c 377$matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
0f5d15d6 378
379@ans = @ans1 = ();
380push(@ans, $res), push(@ans1, $&) while $res = m/$matched/g;
381
382print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
383print "ok $test\n";
384$test++;
385
386print "# ans1='@ans1'\n# expect='$expect'\nnot " if "@ans1" ne $expect;
387print "ok $test\n";
388$test++;
389
390@ans = m/$matched/g;
391
392print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
393print "ok $test\n";
394$test++;
395
c277df42 396@ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
397print "not " if "@ans" ne 'a/ b';
398print "ok $test\n";
399$test++;
400
cc6b7395 401$code = '{$blah = 45}';
c277df42 402$blah = 12;
2cd61cdb 403eval { /(?$code)/ };
404print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
e4d48cc9 405print "ok $test\n";
406$test++;
407
2cd61cdb 408for $code ('{$blah = 45}','=xx') {
409 $blah = 12;
410 $res = eval { "xx" =~ /(?$code)/o };
411 if ($code eq '=xx') {
412 print "#'$@','$res','$blah'\nnot " unless not $@ and $res;
413 } else {
414 print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
415 }
416 print "ok $test\n";
417 $test++;
418}
419
e4d48cc9 420$code = '{$blah = 45}';
421$blah = 12;
422eval "/(?$code)/";
cc6b7395 423print "not " if $blah != 45;
424print "ok $test\n";
425$test++;
426
427$blah = 12;
428/(?{$blah = 45})/;
c277df42 429print "not " if $blah != 45;
430print "ok $test\n";
431$test++;
432
74d6a13a 433$x = 'banana';
434$x =~ /.a/g;
435print "not " unless pos($x) == 2;
436print "ok $test\n";
437$test++;
438
439$x =~ /.z/gc;
440print "not " unless pos($x) == 2;
441print "ok $test\n";
442$test++;
443
444sub f {
445 my $p = $_[0];
446 return $p;
447}
448
449$x =~ /.a/g;
450print "not " unless f(pos($x)) == 4;
451print "ok $test\n";
452$test++;
4599a1de 453
ce862d02 454$x = $^R = 67;
455'foot' =~ /foo(?{$x = 12; 75})[t]/;
456print "not " unless $^R eq '75';
457print "ok $test\n";
458$test++;
459
460$x = $^R = 67;
461'foot' =~ /foo(?{$x = 12; 75})[xy]/;
462print "not " unless $^R eq '67' and $x eq '12';
463print "ok $test\n";
464$test++;
465
466$x = $^R = 67;
467'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
468print "not " unless $^R eq '79' and $x eq '12';
469print "ok $test\n";
470$test++;
471
8782bef2 472print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
473print "ok $test\n";
474$test++;
475
476print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
477print "ok $test\n";
478$test++;
479
480print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
481print "ok $test\n";
482$test++;
483
484print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
485print "ok $test\n";
486$test++;
487
488print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
489print "ok $test\n";
490$test++;
491
492print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
97197631 493print "ok $test\n";
494$test++;
495
7e5428c5 496$_ = 'xabcx';
497foreach $ans ('', 'c') {
498 /(?<=(?=a)..)((?=c)|.)/g;
499 print "not " unless $1 eq $ans;
500 print "ok $test\n";
501 $test++;
502}
503
504$_ = 'a';
505foreach $ans ('', 'a', '') {
506 /^|a|$/g;
507 print "not " unless $& eq $ans;
508 print "ok $test\n";
509 $test++;
510}
511
09f25ae4 512sub prefixify {
513 my($v,$a,$b,$res) = @_;
514 $v =~ s/\Q$a\E/$b/;
515 print "not " unless $res eq $v;
516 print "ok $test\n";
517 $test++;
518}
519prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
520prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
521
522$_ = 'var="foo"';
523/(\")/;
524print "not " unless $1 and /$1/;
525print "ok $test\n";
526$test++;
527
8782bef2 528$a=qr/(?{++$b})/;
2cd61cdb 529$b = 7;
530/$a$a/;
531print "not " unless $b eq '9';
532print "ok $test\n";
533$test++;
534
535$c="$a";
536/$a$a/;
537print "not " unless $b eq '11';
538print "ok $test\n";
539$test++;
540
541{
542 use re "eval";
543 /$a$c$a/;
544 print "not " unless $b eq '14';
545 print "ok $test\n";
546 $test++;
547
548 no re "eval";
549 $match = eval { /$a$c$a/ };
550 print "not "
551 unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
552 print "ok $test\n";
553 $test++;
554}
cbce877f 555
556{
557 package aa;
558 $c = 2;
559 $::c = 3;
560 '' =~ /(?{ $c = 4 })/;
561 print "not " unless $c == 4;
562}
563print "ok $test\n";
564$test++;
565print "not " unless $c == 3;
566print "ok $test\n";
567$test++;
2cd61cdb 568
4599a1de 569sub must_warn_pat {
570 my $warn_pat = shift;
571 return sub { print "not " unless $_[0] =~ /$warn_pat/ }
572}
573
574sub must_warn {
575 my ($warn_pat, $code) = @_;
ee8c7f54 576 local %SIG;
577 eval 'BEGIN { use warnings; $SIG{__WARN__} = $warn_pat };' . $code;
4599a1de 578 print "ok $test\n";
579 $test++;
580}
581
582
583sub make_must_warn {
584 my $warn_pat = shift;
585 return sub { must_warn(must_warn_pat($warn_pat)) }
586}
587
588my $for_future = make_must_warn('reserved for future extensions');
589
590&$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
22d4bb9c 591
592#&$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
593print "ok $test\n"; $test++; # now a fatal croak
594
595#&$for_future('q(a.[b].) =~ /[x[.foo.]]/');
596print "ok $test\n"; $test++; # now a fatal croak
f7e33566 597
598# test if failure of patterns returns empty list
599$_ = 'aaa';
600@_ = /bbb/;
601print "not " if @_;
602print "ok $test\n";
603$test++;
604
605@_ = /bbb/g;
606print "not " if @_;
607print "ok $test\n";
608$test++;
609
610@_ = /(bbb)/;
611print "not " if @_;
612print "ok $test\n";
613$test++;
614
615@_ = /(bbb)/g;
616print "not " if @_;
617print "ok $test\n";
618$test++;
619
6cef1e77 620/a(?=.$)/;
621print "not " if $#+ != 0 or $#- != 0;
622print "ok $test\n";
623$test++;
624
625print "not " if $+[0] != 2 or $-[0] != 1;
626print "ok $test\n";
627$test++;
628
629print "not "
630 if defined $+[1] or defined $-[1] or defined $+[2] or defined $-[2];
631print "ok $test\n";
632$test++;
633
634/a(a)(a)/;
635print "not " if $#+ != 2 or $#- != 2;
636print "ok $test\n";
637$test++;
638
639print "not " if $+[0] != 3 or $-[0] != 0;
640print "ok $test\n";
641$test++;
642
643print "not " if $+[1] != 2 or $-[1] != 1;
644print "ok $test\n";
645$test++;
646
647print "not " if $+[2] != 3 or $-[2] != 2;
648print "ok $test\n";
649$test++;
650
651print "not "
652 if defined $+[3] or defined $-[3] or defined $+[4] or defined $-[4];
653print "ok $test\n";
654$test++;
655
656/.(a)(b)?(a)/;
657print "not " if $#+ != 3 or $#- != 3;
658print "ok $test\n";
659$test++;
660
661print "not " if $+[0] != 3 or $-[0] != 0;
662print "ok $test\n";
663$test++;
664
665print "not " if $+[1] != 2 or $-[1] != 1;
666print "ok $test\n";
667$test++;
668
669print "not " if $+[3] != 3 or $-[3] != 2;
670print "ok $test\n";
671$test++;
672
673print "not "
674 if defined $+[2] or defined $-[2] or defined $+[4] or defined $-[4];
675print "ok $test\n";
676$test++;
677
678/.(a)/;
679print "not " if $#+ != 1 or $#- != 1;
680print "ok $test\n";
681$test++;
682
683print "not " if $+[0] != 2 or $-[0] != 0;
684print "ok $test\n";
685$test++;
686
687print "not " if $+[1] != 2 or $-[1] != 1;
688print "ok $test\n";
689$test++;
690
691print "not "
692 if defined $+[2] or defined $-[2] or defined $+[3] or defined $-[3];
693print "ok $test\n";
694$test++;
695
22d4bb9c 696eval { $+[0] = 13; };
697print "not "
698 if $@ !~ /^Modification of a read-only value attempted/;
699print "ok $test\n";
700$test++;
701
702eval { $-[0] = 13; };
703print "not "
704 if $@ !~ /^Modification of a read-only value attempted/;
705print "ok $test\n";
706$test++;
707
708eval { @+ = (7, 6, 5); };
709print "not "
710 if $@ !~ /^Modification of a read-only value attempted/;
711print "ok $test\n";
712$test++;
713
714eval { @- = qw(foo bar); };
715print "not "
716 if $@ !~ /^Modification of a read-only value attempted/;
717print "ok $test\n";
718$test++;
719
8f580fb8 720/.(a)(ba*)?/;
721print "#$#-..$#+\nnot " if $#+ != 2 or $#- != 1;
722print "ok $test\n";
723$test++;
724
ad94a511 725$_ = 'aaa';
726pos = 1;
727@a = /\Ga/g;
728print "not " unless "@a" eq "a a";
729print "ok $test\n";
730$test++;
731
22e551b9 732$str = 'abcde';
733pos $str = 2;
734
735print "not " if $str =~ /^\G/;
736print "ok $test\n";
737$test++;
738
739print "not " if $str =~ /^.\G/;
740print "ok $test\n";
741$test++;
742
743print "not " unless $str =~ /^..\G/;
744print "ok $test\n";
745$test++;
746
747print "not " if $str =~ /^...\G/;
748print "ok $test\n";
749$test++;
750
751print "not " unless $str =~ /.\G./ and $& eq 'bc';
752print "ok $test\n";
753$test++;
754
755print "not " unless $str =~ /\G../ and $& eq 'cd';
756print "ok $test\n";
757$test++;
758
9661b544 759undef $foo; undef $bar;
760print "#'$str','$foo','$bar'\nnot "
761 unless $str =~ /b(?{$foo = $_; $bar = pos})c/
762 and $foo eq 'abcde' and $bar eq 2;
763print "ok $test\n";
764$test++;
765
766undef $foo; undef $bar;
767pos $str = undef;
768print "#'$str','$foo','$bar'\nnot "
769 unless $str =~ /b(?{$foo = $_; $bar = pos})c/g
770 and $foo eq 'abcde' and $bar eq 2 and pos $str eq 3;
771print "ok $test\n";
772$test++;
773
774$_ = $str;
775
776undef $foo; undef $bar;
777print "#'$str','$foo','$bar'\nnot "
778 unless /b(?{$foo = $_; $bar = pos})c/
779 and $foo eq 'abcde' and $bar eq 2;
780print "ok $test\n";
781$test++;
782
783undef $foo; undef $bar;
784print "#'$str','$foo','$bar'\nnot "
785 unless /b(?{$foo = $_; $bar = pos})c/g
786 and $foo eq 'abcde' and $bar eq 2 and pos eq 3;
787print "ok $test\n";
788$test++;
789
790undef $foo; undef $bar;
791pos = undef;
7921 while /b(?{$foo = $_; $bar = pos})c/g;
793print "#'$str','$foo','$bar'\nnot "
794 unless $foo eq 'abcde' and $bar eq 2 and not defined pos;
795print "ok $test\n";
796$test++;
797
798undef $foo; undef $bar;
799$_ = 'abcde|abcde';
800print "#'$str','$foo','$bar','$_'\nnot "
801 unless s/b(?{$foo = $_; $bar = pos})c/x/g and $foo eq 'abcde|abcde'
802 and $bar eq 8 and $_ eq 'axde|axde';
803print "ok $test\n";
804$test++;
805
5c5e4c24 806@res = ();
807# List context:
808$_ = 'abcde|abcde';
809@dummy = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
810@res = map {defined $_ ? "'$_'" : 'undef'} @res;
811$res = "@res";
812print "#'@res' '$_'\nnot "
813 unless "@res" eq "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'";
814print "ok $test\n";
815$test++;
816
817@res = ();
818@dummy = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
819@res = map {defined $_ ? "'$_'" : 'undef'} @res;
820$res = "@res";
821print "#'@res' '$_'\nnot "
822 unless "@res" eq
823 "'' 'ab' 'cde|abcde' " .
824 "'' 'abc' 'de|abcde' " .
825 "'abcd' 'e|' 'abcde' " .
826 "'abcde|' 'ab' 'cde' " .
827 "'abcde|' 'abc' 'de'" ;
828print "ok $test\n";
829$test++;
830
b7a35066 831#Some more \G anchor checks
832$foo='aabbccddeeffgg';
833
834pos($foo)=1;
835
836$foo=~/.\G(..)/g;
837print "not " unless($1 eq 'ab');
838print "ok $test\n";
839$test++;
840
841pos($foo) += 1;
842$foo=~/.\G(..)/g;
843print "not " unless($1 eq 'cc');
844print "ok $test\n";
845$test++;
846
847pos($foo) += 1;
848$foo=~/.\G(..)/g;
849print "not " unless($1 eq 'de');
850print "ok $test\n";
851$test++;
852
0ef3e39e 853print "not " unless $foo =~ /\Gef/g;
854print "ok $test\n";
855$test++;
856
b7a35066 857undef pos $foo;
858
859$foo=~/\G(..)/g;
860print "not " unless($1 eq 'aa');
861print "ok $test\n";
862$test++;
863
864$foo=~/\G(..)/g;
865print "not " unless($1 eq 'bb');
866print "ok $test\n";
867$test++;
868
869pos($foo)=5;
870$foo=~/\G(..)/g;
871print "not " unless($1 eq 'cd');
872print "ok $test\n";
873$test++;
874
e60df1fa 875$_='123x123';
876@res = /(\d*|x)/g;
877print "not " unless('123||x|123|' eq join '|', @res);
878print "ok $test\n";
879$test++;
880
9d080a66 881# see if matching against temporaries (created via pp_helem()) is safe
882{ foo => "ok $test\n".$^X }->{foo} =~ /^(.*)\n/g;
883print "$1\n";
884$test++;
885
cf93c79d 886# See if $i work inside (?{}) in the presense of saved substrings and
887# changing $_
888@a = qw(foo bar);
889@b = ();
890s/(\w)(?{push @b, $1})/,$1,/g for @a;
891
892print "# \@b='@b', expect 'f o o b a r'\nnot " unless("@b" eq "f o o b a r");
893print "ok $test\n";
894$test++;
895
896print "not " unless("@a" eq ",f,,o,,o, ,b,,a,,r,");
897print "ok $test\n";
898$test++;
899
2c914db6 900$brackets = qr{
14455d6c 901 { (?> [^{}]+ | (??{ $brackets }) )* }
2c914db6 902 }x;
903
904"{{}" =~ $brackets;
905print "ok $test\n"; # Did we survive?
906$test++;
907
908"something { long { and } hairy" =~ $brackets;
909print "ok $test\n"; # Did we survive?
910$test++;
911
14455d6c 912"something { long { and } hairy" =~ m/((??{ $brackets }))/;
2c914db6 913print "not " unless $1 eq "{ and }";
914print "ok $test\n";
915$test++;
916
30944b6d 917$_ = "a-a\nxbb";
918pos=1;
919m/^-.*bb/mg and print "not ";
920print "ok $test\n";
921$test++;
30382c73 922
923$text = "aaXbXcc";
924pos($text)=0;
925$text =~ /\GXb*X/g and print 'not ';
926print "ok $test\n";
927$test++;
3cf5c195 928
929$text = "xA\n" x 500;
930$text =~ /^\s*A/m and print 'not ';
931print "ok $test\n";
932$test++;
d506a20d 933
934$text = "abc dbf";
935@res = ($text =~ /.*?(b).*?\b/g);
936"@res" eq 'b b' or print 'not ';
937print "ok $test\n";
938$test++;
939
9442cb0e 940@a = map chr,0..255;
aeaf5620 941
942@b = grep(/\S/,@a);
943@c = grep(/[^\s]/,@a);
944print "not " if "@b" ne "@c";
9442cb0e 945print "ok $test\n";
946$test++;
947
aeaf5620 948@b = grep(/\S/,@a);
949@c = grep(/[\S]/,@a);
950print "not " if "@b" ne "@c";
9442cb0e 951print "ok $test\n";
952$test++;
953
aeaf5620 954@b = grep(/\s/,@a);
955@c = grep(/[^\S]/,@a);
956print "not " if "@b" ne "@c";
9442cb0e 957print "ok $test\n";
958$test++;
959
aeaf5620 960@b = grep(/\s/,@a);
961@c = grep(/[\s]/,@a);
962print "not " if "@b" ne "@c";
9442cb0e 963print "ok $test\n";
964$test++;
965
aeaf5620 966@b = grep(/\D/,@a);
967@c = grep(/[^\d]/,@a);
968print "not " if "@b" ne "@c";
9442cb0e 969print "ok $test\n";
970$test++;
971
aeaf5620 972@b = grep(/\D/,@a);
973@c = grep(/[\D]/,@a);
974print "not " if "@b" ne "@c";
9442cb0e 975print "ok $test\n";
976$test++;
977
aeaf5620 978@b = grep(/\d/,@a);
979@c = grep(/[^\D]/,@a);
980print "not " if "@b" ne "@c";
9442cb0e 981print "ok $test\n";
982$test++;
983
aeaf5620 984@b = grep(/\d/,@a);
985@c = grep(/[\d]/,@a);
986print "not " if "@b" ne "@c";
9442cb0e 987print "ok $test\n";
988$test++;
989
aeaf5620 990@b = grep(/\W/,@a);
991@c = grep(/[^\w]/,@a);
992print "not " if "@b" ne "@c";
9442cb0e 993print "ok $test\n";
994$test++;
995
aeaf5620 996@b = grep(/\W/,@a);
997@c = grep(/[\W]/,@a);
998print "not " if "@b" ne "@c";
9442cb0e 999print "ok $test\n";
1000$test++;
1001
aeaf5620 1002@b = grep(/\w/,@a);
1003@c = grep(/[^\W]/,@a);
1004print "not " if "@b" ne "@c";
9442cb0e 1005print "ok $test\n";
1006$test++;
1007
aeaf5620 1008@b = grep(/\w/,@a);
1009@c = grep(/[\w]/,@a);
1010print "not " if "@b" ne "@c";
9442cb0e 1011print "ok $test\n";
1012$test++;
1aeab75a 1013
1014# see if backtracking optimization works correctly
1015"\n\n" =~ /\n $ \n/x or print "not ";
1016print "ok $test\n";
1017$test++;
1018
1019"\n\n" =~ /\n* $ \n/x or print "not ";
1020print "ok $test\n";
1021$test++;
1022
1023"\n\n" =~ /\n+ $ \n/x or print "not ";
1024print "ok $test\n";
1025$test++;
ee8c7f54 1026
1027[] =~ /^ARRAY/ or print "# [] \nnot ";
1028print "ok $test\n";
1029$test++;
1030
1031eval << 'EOE';
1032{
1033 package S;
1034 use overload '""' => sub { 'Object S' };
1035 sub new { bless [] }
1036}
1037$a = 'S'->new;
1038EOE
1039
1040$a and $a =~ /^Object\sS/ or print "# '$a' \nnot ";
1041print "ok $test\n";
1042$test++;
4b19af01 1043
1044# test result of match used as match (!)
1045'a1b' =~ ('xyz' =~ /y/) and $` eq 'a' or print "not ";
1046print "ok $test\n";
1047$test++;
1048
1049'a1b' =~ ('xyz' =~ /t/) and $` eq 'a' or print "not ";
1050print "ok $test\n";
1051$test++;
22d4bb9c 1052
1053$w = 0;
1054{
1055 local $SIG{__WARN__} = sub { $w = 1 };
1056 local $^W = 1;
1057 $w = 1 if ("1\n" x 102) =~ /^\s*\n/m;
1058}
1059print $w ? "not " : "", "ok $test\n";
1060$test++;
1061
1062my %space = ( spc => " ",
1063 tab => "\t",
1064 cr => "\r",
1065 lf => "\n",
1066 ff => "\f",
1067# The vertical tabulator seems miraculously be 12 both in ASCII and EBCDIC.
1068 vt => chr(11),
1069 false => "space" );
1070
1071my @space0 = sort grep { $space{$_} =~ /\s/ } keys %space;
1072my @space1 = sort grep { $space{$_} =~ /[[:space:]]/ } keys %space;
1073my @space2 = sort grep { $space{$_} =~ /[[:blank:]]/ } keys %space;
1074
1075print "not " unless "@space0" eq "cr ff lf spc tab";
1076print "ok $test\n";
1077$test++;
1078
1079print "not " unless "@space1" eq "cr ff lf spc tab vt";
1080print "ok $test\n";
1081$test++;
1082
1083print "not " unless "@space2" eq "spc tab";
1084print "ok $test\n";
1085$test++;
1086