This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
resend [PATCH 5.005_55] Various win32/win32.c cleanup
[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
e60df1fa 7print "1..185\n";
8d37f932 8
e4d48cc9
GS
9BEGIN {
10 chdir 't' if -d 't';
93430cb4 11 unshift @INC, "../lib" if -d "../lib";
e4d48cc9 12}
8d37f932 13eval 'use Config'; # Defaults assumed if this fails
8d063cd8 14
9c63abab
GS
15# XXX known to leak scalars
16$ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
eec2d3df 17
8d063cd8
LW
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
LW
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
LW
82 ?(.*)? && (print $1,"\n");
83 /not/ && reset;
84 /not ok 26/ && reset 'X';
85}
86
a0d0e21e 87while (($key,$val) = each(%XXX)) {
378cc40b
LW
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
LW
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
LW
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
LW
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
LW
157@words = /to/g;
158print join(':',@words) eq "to:to"
159 ? "ok 47\n"
71be2cbc 160 : "not ok 47 `@words'\n";
352d5a3a
LW
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
LW
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
LW
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
LW
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
LW
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
PP
222
223$_ = "abdc";
224pos $_ = 2;
c90c0ff4 225/\Gc/gc;
71be2cbc
PP
226print "not " if (pos $_) != 2;
227print "ok 61\n";
c90c0ff4
PP
228/\Gc/g;
229print "not " if defined pos $_;
230print "ok 62\n";
c277df42
IZ
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
DD
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}/";
269print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%;
270print "ok 69\n";
271
272eval "'aaa' =~ /a{1,$reg_infty_p}/";
273print "not "
274 if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%;
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/);
282print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%;
283print "ok 71\n";
284
285# This one will fail when POSIX character classes do get implemented
286{
287 my $w;
288 local $^W = 1;
289 local $SIG{__WARN__} = sub{$w = shift};
290 eval q('a' =~ /[[:alpha:]]/);
291 print "not " if $w !~ /^\QCharacter class syntax [: :] is reserved/;
292}
293print "ok 72\n";
294
c277df42 295# Long Monsters
8d37f932 296$test = 73;
c277df42
IZ
297for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
298 $a = 'a' x $l;
299 print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
300 print "ok $test\n";
301 $test++;
302
303 print "not " if "b$a=" =~ /a$a=/;
304 print "ok $test\n";
305 $test++;
306}
307
308# 20000 nodes, each taking 3 words per string, and 1 per branch
309$long_constant_len = join '|', 12120 .. 32645;
310$long_var_len = join '|', 8120 .. 28645;
311%ans = ( 'ax13876y25677lbc' => 1,
312 'ax13876y25677mcb' => 0, # not b.
313 'ax13876y35677nbc' => 0, # Num too big
314 'ax13876y25677y21378obc' => 1,
315 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
316 'ax13876y25677y21378y21378kbc' => 1,
317 'ax13876y25677y21378y21378kcb' => 0, # Not b.
318 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
319 );
320
321for ( keys %ans ) {
322 print "# const-len `$_' not => $ans{$_}\nnot "
323 if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
324 print "ok $test\n";
325 $test++;
326 print "# var-len `$_' not => $ans{$_}\nnot "
327 if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
328 print "ok $test\n";
329 $test++;
330}
331
332$_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
333$expect = "(bla()) ((l)u((e))) (l(e)e)";
334
335sub matchit {
cc6b7395 336 m/
c277df42
IZ
337 (
338 \(
339 (?{ $c = 1 }) # Initialize
340 (?:
341 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
342 (?!
343 ) # Fail: will unwind one iteration back
344 )
345 (?:
346 [^()]+ # Match a big chunk
347 (?=
348 [()]
349 ) # Do not try to match subchunks
350 |
351 \(
352 (?{ ++$c })
353 |
354 \)
355 (?{ --$c })
356 )
357 )+ # This may not match with different subblocks
358 )
359 (?(?{ $c != 0 })
360 (?!
361 ) # Fail
362 ) # Otherwise the chunk 1 may succeed with $c>0
cc6b7395 363 /xg;
c277df42
IZ
364}
365
0f5d15d6 366@ans = ();
c277df42
IZ
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
0f5d15d6
IZ
379my $matched;
380$matched = qr/\((?:(?>[^()]+)|(?p{$matched}))*\)/;
381
382@ans = @ans1 = ();
383push(@ans, $res), push(@ans1, $&) while $res = m/$matched/g;
384
385print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
386print "ok $test\n";
387$test++;
388
389print "# ans1='@ans1'\n# expect='$expect'\nnot " if "@ans1" ne $expect;
390print "ok $test\n";
391$test++;
392
393@ans = m/$matched/g;
394
395print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
396print "ok $test\n";
397$test++;
398
c277df42
IZ
399@ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
400print "not " if "@ans" ne 'a/ b';
401print "ok $test\n";
402$test++;
403
cc6b7395 404$code = '{$blah = 45}';
c277df42 405$blah = 12;
2cd61cdb
IZ
406eval { /(?$code)/ };
407print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
e4d48cc9
GS
408print "ok $test\n";
409$test++;
410
2cd61cdb
IZ
411for $code ('{$blah = 45}','=xx') {
412 $blah = 12;
413 $res = eval { "xx" =~ /(?$code)/o };
414 if ($code eq '=xx') {
415 print "#'$@','$res','$blah'\nnot " unless not $@ and $res;
416 } else {
417 print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12;
418 }
419 print "ok $test\n";
420 $test++;
421}
422
e4d48cc9
GS
423$code = '{$blah = 45}';
424$blah = 12;
425eval "/(?$code)/";
cc6b7395
IZ
426print "not " if $blah != 45;
427print "ok $test\n";
428$test++;
429
430$blah = 12;
431/(?{$blah = 45})/;
c277df42
IZ
432print "not " if $blah != 45;
433print "ok $test\n";
434$test++;
435
74d6a13a
MB
436$x = 'banana';
437$x =~ /.a/g;
438print "not " unless pos($x) == 2;
439print "ok $test\n";
440$test++;
441
442$x =~ /.z/gc;
443print "not " unless pos($x) == 2;
444print "ok $test\n";
445$test++;
446
447sub f {
448 my $p = $_[0];
449 return $p;
450}
451
452$x =~ /.a/g;
453print "not " unless f(pos($x)) == 4;
454print "ok $test\n";
455$test++;
4599a1de 456
ce862d02
IZ
457$x = $^R = 67;
458'foot' =~ /foo(?{$x = 12; 75})[t]/;
459print "not " unless $^R eq '75';
460print "ok $test\n";
461$test++;
462
463$x = $^R = 67;
464'foot' =~ /foo(?{$x = 12; 75})[xy]/;
465print "not " unless $^R eq '67' and $x eq '12';
466print "ok $test\n";
467$test++;
468
469$x = $^R = 67;
470'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
471print "not " unless $^R eq '79' and $x eq '12';
472print "ok $test\n";
473$test++;
474
8782bef2
GB
475print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)';
476print "ok $test\n";
477$test++;
478
479print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)';
480print "ok $test\n";
481$test++;
482
483print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)';
484print "ok $test\n";
485$test++;
486
487print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)';
488print "ok $test\n";
489$test++;
490
491print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)';
492print "ok $test\n";
493$test++;
494
495print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)';
97197631
IZ
496print "ok $test\n";
497$test++;
498
7e5428c5
IZ
499$_ = 'xabcx';
500foreach $ans ('', 'c') {
501 /(?<=(?=a)..)((?=c)|.)/g;
502 print "not " unless $1 eq $ans;
503 print "ok $test\n";
504 $test++;
505}
506
507$_ = 'a';
508foreach $ans ('', 'a', '') {
509 /^|a|$/g;
510 print "not " unless $& eq $ans;
511 print "ok $test\n";
512 $test++;
513}
514
09f25ae4
IZ
515sub prefixify {
516 my($v,$a,$b,$res) = @_;
517 $v =~ s/\Q$a\E/$b/;
518 print "not " unless $res eq $v;
519 print "ok $test\n";
520 $test++;
521}
522prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
523prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
524
525$_ = 'var="foo"';
526/(\")/;
527print "not " unless $1 and /$1/;
528print "ok $test\n";
529$test++;
530
8782bef2 531$a=qr/(?{++$b})/;
2cd61cdb
IZ
532$b = 7;
533/$a$a/;
534print "not " unless $b eq '9';
535print "ok $test\n";
536$test++;
537
538$c="$a";
539/$a$a/;
540print "not " unless $b eq '11';
541print "ok $test\n";
542$test++;
543
544{
545 use re "eval";
546 /$a$c$a/;
547 print "not " unless $b eq '14';
548 print "ok $test\n";
549 $test++;
550
551 no re "eval";
552 $match = eval { /$a$c$a/ };
553 print "not "
554 unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match;
555 print "ok $test\n";
556 $test++;
557}
cbce877f
IZ
558
559{
560 package aa;
561 $c = 2;
562 $::c = 3;
563 '' =~ /(?{ $c = 4 })/;
564 print "not " unless $c == 4;
565}
566print "ok $test\n";
567$test++;
568print "not " unless $c == 3;
569print "ok $test\n";
570$test++;
2cd61cdb 571
4599a1de
JH
572sub must_warn_pat {
573 my $warn_pat = shift;
574 return sub { print "not " unless $_[0] =~ /$warn_pat/ }
575}
576
577sub must_warn {
578 my ($warn_pat, $code) = @_;
579 local $^W; local %SIG;
580 eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code;
581 print "ok $test\n";
582 $test++;
583}
584
585
586sub make_must_warn {
587 my $warn_pat = shift;
588 return sub { must_warn(must_warn_pat($warn_pat)) }
589}
590
591my $for_future = make_must_warn('reserved for future extensions');
592
593&$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
594&$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
595&$for_future('q(a.[b].) =~ /[x[.foo.]]/');
f7e33566
GS
596
597# test if failure of patterns returns empty list
598$_ = 'aaa';
599@_ = /bbb/;
600print "not " if @_;
601print "ok $test\n";
602$test++;
603
604@_ = /bbb/g;
605print "not " if @_;
606print "ok $test\n";
607$test++;
608
609@_ = /(bbb)/;
610print "not " if @_;
611print "ok $test\n";
612$test++;
613
614@_ = /(bbb)/g;
615print "not " if @_;
616print "ok $test\n";
617$test++;
618
6cef1e77
IZ
619/a(?=.$)/;
620print "not " if $#+ != 0 or $#- != 0;
621print "ok $test\n";
622$test++;
623
624print "not " if $+[0] != 2 or $-[0] != 1;
625print "ok $test\n";
626$test++;
627
628print "not "
629 if defined $+[1] or defined $-[1] or defined $+[2] or defined $-[2];
630print "ok $test\n";
631$test++;
632
633/a(a)(a)/;
634print "not " if $#+ != 2 or $#- != 2;
635print "ok $test\n";
636$test++;
637
638print "not " if $+[0] != 3 or $-[0] != 0;
639print "ok $test\n";
640$test++;
641
642print "not " if $+[1] != 2 or $-[1] != 1;
643print "ok $test\n";
644$test++;
645
646print "not " if $+[2] != 3 or $-[2] != 2;
647print "ok $test\n";
648$test++;
649
650print "not "
651 if defined $+[3] or defined $-[3] or defined $+[4] or defined $-[4];
652print "ok $test\n";
653$test++;
654
655/.(a)(b)?(a)/;
656print "not " if $#+ != 3 or $#- != 3;
657print "ok $test\n";
658$test++;
659
660print "not " if $+[0] != 3 or $-[0] != 0;
661print "ok $test\n";
662$test++;
663
664print "not " if $+[1] != 2 or $-[1] != 1;
665print "ok $test\n";
666$test++;
667
668print "not " if $+[3] != 3 or $-[3] != 2;
669print "ok $test\n";
670$test++;
671
672print "not "
673 if defined $+[2] or defined $-[2] or defined $+[4] or defined $-[4];
674print "ok $test\n";
675$test++;
676
677/.(a)/;
678print "not " if $#+ != 1 or $#- != 1;
679print "ok $test\n";
680$test++;
681
682print "not " if $+[0] != 2 or $-[0] != 0;
683print "ok $test\n";
684$test++;
685
686print "not " if $+[1] != 2 or $-[1] != 1;
687print "ok $test\n";
688$test++;
689
690print "not "
691 if defined $+[2] or defined $-[2] or defined $+[3] or defined $-[3];
692print "ok $test\n";
693$test++;
694
8f580fb8
IZ
695/.(a)(ba*)?/;
696print "#$#-..$#+\nnot " if $#+ != 2 or $#- != 1;
697print "ok $test\n";
698$test++;
699
ad94a511
IZ
700$_ = 'aaa';
701pos = 1;
702@a = /\Ga/g;
703print "not " unless "@a" eq "a a";
704print "ok $test\n";
705$test++;
706
22e551b9
IZ
707$str = 'abcde';
708pos $str = 2;
709
710print "not " if $str =~ /^\G/;
711print "ok $test\n";
712$test++;
713
714print "not " if $str =~ /^.\G/;
715print "ok $test\n";
716$test++;
717
718print "not " unless $str =~ /^..\G/;
719print "ok $test\n";
720$test++;
721
722print "not " if $str =~ /^...\G/;
723print "ok $test\n";
724$test++;
725
726print "not " unless $str =~ /.\G./ and $& eq 'bc';
727print "ok $test\n";
728$test++;
729
730print "not " unless $str =~ /\G../ and $& eq 'cd';
731print "ok $test\n";
732$test++;
733
9661b544
IZ
734undef $foo; undef $bar;
735print "#'$str','$foo','$bar'\nnot "
736 unless $str =~ /b(?{$foo = $_; $bar = pos})c/
737 and $foo eq 'abcde' and $bar eq 2;
738print "ok $test\n";
739$test++;
740
741undef $foo; undef $bar;
742pos $str = undef;
743print "#'$str','$foo','$bar'\nnot "
744 unless $str =~ /b(?{$foo = $_; $bar = pos})c/g
745 and $foo eq 'abcde' and $bar eq 2 and pos $str eq 3;
746print "ok $test\n";
747$test++;
748
749$_ = $str;
750
751undef $foo; undef $bar;
752print "#'$str','$foo','$bar'\nnot "
753 unless /b(?{$foo = $_; $bar = pos})c/
754 and $foo eq 'abcde' and $bar eq 2;
755print "ok $test\n";
756$test++;
757
758undef $foo; undef $bar;
759print "#'$str','$foo','$bar'\nnot "
760 unless /b(?{$foo = $_; $bar = pos})c/g
761 and $foo eq 'abcde' and $bar eq 2 and pos eq 3;
762print "ok $test\n";
763$test++;
764
765undef $foo; undef $bar;
766pos = undef;
7671 while /b(?{$foo = $_; $bar = pos})c/g;
768print "#'$str','$foo','$bar'\nnot "
769 unless $foo eq 'abcde' and $bar eq 2 and not defined pos;
770print "ok $test\n";
771$test++;
772
773undef $foo; undef $bar;
774$_ = 'abcde|abcde';
775print "#'$str','$foo','$bar','$_'\nnot "
776 unless s/b(?{$foo = $_; $bar = pos})c/x/g and $foo eq 'abcde|abcde'
777 and $bar eq 8 and $_ eq 'axde|axde';
778print "ok $test\n";
779$test++;
780
5c5e4c24
IZ
781@res = ();
782# List context:
783$_ = 'abcde|abcde';
784@dummy = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
785@res = map {defined $_ ? "'$_'" : 'undef'} @res;
786$res = "@res";
787print "#'@res' '$_'\nnot "
788 unless "@res" eq "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'";
789print "ok $test\n";
790$test++;
791
792@res = ();
793@dummy = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
794@res = map {defined $_ ? "'$_'" : 'undef'} @res;
795$res = "@res";
796print "#'@res' '$_'\nnot "
797 unless "@res" eq
798 "'' 'ab' 'cde|abcde' " .
799 "'' 'abc' 'de|abcde' " .
800 "'abcd' 'e|' 'abcde' " .
801 "'abcde|' 'ab' 'cde' " .
802 "'abcde|' 'abc' 'de'" ;
803print "ok $test\n";
804$test++;
805
b7a35066
IZ
806#Some more \G anchor checks
807$foo='aabbccddeeffgg';
808
809pos($foo)=1;
810
811$foo=~/.\G(..)/g;
812print "not " unless($1 eq 'ab');
813print "ok $test\n";
814$test++;
815
816pos($foo) += 1;
817$foo=~/.\G(..)/g;
818print "not " unless($1 eq 'cc');
819print "ok $test\n";
820$test++;
821
822pos($foo) += 1;
823$foo=~/.\G(..)/g;
824print "not " unless($1 eq 'de');
825print "ok $test\n";
826$test++;
827
828undef pos $foo;
829
830$foo=~/\G(..)/g;
831print "not " unless($1 eq 'aa');
832print "ok $test\n";
833$test++;
834
835$foo=~/\G(..)/g;
836print "not " unless($1 eq 'bb');
837print "ok $test\n";
838$test++;
839
840pos($foo)=5;
841$foo=~/\G(..)/g;
842print "not " unless($1 eq 'cd');
843print "ok $test\n";
844$test++;
845
e60df1fa
IZ
846$_='123x123';
847@res = /(\d*|x)/g;
848print "not " unless('123||x|123|' eq join '|', @res);
849print "ok $test\n";
850$test++;
851
9d080a66
GS
852# see if matching against temporaries (created via pp_helem()) is safe
853{ foo => "ok $test\n".$^X }->{foo} =~ /^(.*)\n/g;
854print "$1\n";
855$test++;
856