Commit | Line | Data |
---|---|---|
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 | |
cf93c79d | 7 | print "1..188\n"; |
8d37f932 | 8 | |
e4d48cc9 GS |
9 | BEGIN { |
10 | chdir 't' if -d 't'; | |
93430cb4 | 11 | unshift @INC, "../lib" if -d "../lib"; |
e4d48cc9 | 12 | } |
8d37f932 | 13 | eval '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 | ||
20 | if ($x =~ /^abc/) {print "ok 1\n";} else {print "not ok 1\n";} | |
21 | if ($x !~ /^def/) {print "ok 2\n";} else {print "not ok 2\n";} | |
22 | ||
23 | $* = 1; | |
24 | if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";} | |
25 | $* = 0; | |
26 | ||
27 | $_ = '123'; | |
28 | if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";} | |
29 | ||
30 | if ($x =~ /^xxx/) {print "not ok 5\n";} else {print "ok 5\n";} | |
31 | if ($x !~ /^abc/) {print "not ok 6\n";} else {print "ok 6\n";} | |
32 | ||
33 | if ($x =~ /def/) {print "ok 7\n";} else {print "not ok 7\n";} | |
34 | if ($x !~ /def/) {print "not ok 8\n";} else {print "ok 8\n";} | |
35 | ||
36 | if ($x !~ /.def/) {print "ok 9\n";} else {print "not ok 9\n";} | |
37 | if ($x =~ /.def/) {print "not ok 10\n";} else {print "ok 10\n";} | |
38 | ||
39 | if ($x =~ /\ndef/) {print "ok 11\n";} else {print "not ok 11\n";} | |
40 | if ($x !~ /\ndef/) {print "not ok 12\n";} else {print "ok 12\n";} | |
41 | ||
42 | $_ = 'aaabbbccc'; | |
43 | if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') { | |
44 | print "ok 13\n"; | |
45 | } else { | |
46 | print "not ok 13\n"; | |
47 | } | |
48 | if (/(a+b+c+)/ && $1 eq 'aaabbbccc') { | |
49 | print "ok 14\n"; | |
50 | } else { | |
51 | print "not ok 14\n"; | |
52 | } | |
53 | ||
54 | if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";} | |
55 | ||
56 | $_ = 'aaabccc'; | |
57 | if (/a+b?c+/) {print "ok 16\n";} else {print "not ok 16\n";} | |
58 | if (/a*b+c*/) {print "ok 17\n";} else {print "not ok 17\n";} | |
59 | ||
60 | $_ = 'aaaccc'; | |
61 | if (/a*b?c*/) {print "ok 18\n";} else {print "not ok 18\n";} | |
62 | if (/a*b+c*/) {print "not ok 19\n";} else {print "ok 19\n";} | |
63 | ||
64 | $_ = 'abcdef'; | |
65 | if (/bcd|xyz/) {print "ok 20\n";} else {print "not ok 20\n";} | |
66 | if (/xyz|bcd/) {print "ok 21\n";} else {print "not ok 21\n";} | |
67 | ||
68 | if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";} | |
378cc40b LW |
69 | |
70 | if (/^$_$/) {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 | |
73 | if ("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 | 81 | while ($_ = shift(@XXX)) { |
378cc40b LW |
82 | ?(.*)? && (print $1,"\n"); |
83 | /not/ && reset; | |
84 | /not ok 26/ && reset 'X'; | |
85 | } | |
86 | ||
a0d0e21e | 87 | while (($key,$val) = each(%XXX)) { |
378cc40b LW |
88 | print "not ok 27\n"; |
89 | exit; | |
90 | } | |
91 | ||
92 | print "ok 27\n"; | |
93 | ||
94 | 'cde' =~ /[^ab]*/; | |
95 | 'xyz' =~ //; | |
96 | if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";} | |
97 | ||
98 | $foo = '[^ab]*'; | |
99 | 'cde' =~ /$foo/; | |
100 | 'xyz' =~ //; | |
101 | if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";} | |
102 | ||
103 | $foo = '[^ab]*'; | |
104 | 'cde' =~ /$foo/; | |
105 | 'xyz' =~ /$null/; | |
106 | if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";} | |
a687059c LW |
107 | |
108 | $_ = 'abcdefghi'; | |
109 | /def/; # optimized up to cmd | |
110 | if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";} | |
111 | ||
112 | /cde/ + 0; # optimized only to spat | |
113 | if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";} | |
114 | ||
115 | /[d][e][f]/; # not optimized | |
116 | if ("$`:$&:$'" 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 | / {([^}]*)}/; | |
120 | if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";} | |
121 | ||
122 | $_ = 'xxx {3,4} yyy zzz'; | |
123 | print /( {3,4})/ ? "ok 35\n" : "not ok 35\n"; | |
124 | print $1 eq ' ' ? "ok 36\n" : "not ok 36\n"; | |
125 | print /( {4,})/ ? "not ok 37\n" : "ok 37\n"; | |
126 | print /( {2,3}.)/ ? "ok 38\n" : "not ok 38\n"; | |
127 | print $1 eq ' y' ? "ok 39\n" : "not ok 39\n"; | |
128 | print /(y{2,3}.)/ ? "ok 40\n" : "not ok 40\n"; | |
129 | print $1 eq 'yyy ' ? "ok 41\n" : "not ok 41\n"; | |
130 | print /x {3,4}/ ? "not ok 42\n" : "ok 42\n"; | |
131 | print /^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; | |
135 | print 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 = (); | |
140 | while (/\w+/g) { | |
141 | push(@words, $&); | |
142 | } | |
143 | print 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 | 148 | pos = 0; |
352d5a3a LW |
149 | while (/to/g) { |
150 | push(@words, $&); | |
151 | } | |
152 | print join(':',@words) eq "to:to" | |
153 | ? "ok 46\n" | |
71be2cbc | 154 | : "not ok 46 `@words'\n"; |
352d5a3a | 155 | |
71be2cbc | 156 | pos $_ = 0; |
352d5a3a LW |
157 | @words = /to/g; |
158 | print 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 | ||
176 | for $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"; | |
189 | print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n"; | |
1462b684 LW |
190 | |
191 | $xyz = 'xyz'; | |
192 | print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n"; | |
193 | ||
194 | # perl 4.009 says "unmatched ()" | |
195 | eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"'; | |
196 | print $@ eq "" ? "ok 50\n" : "not ok 50\n"; | |
197 | print $result eq "abc:bc" ? "ok 51\n" : "not ok 51\n"; | |
a0d0e21e LW |
198 | |
199 | ||
200 | $_="abcfooabcbar"; | |
201 | $x=/abc/g; | |
202 | print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x; | |
203 | $x=/abc/g; | |
204 | print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x; | |
205 | $x=/abc/g; | |
206 | print $x == 0 ? "ok 54\n" : "not ok 54\n"; | |
71be2cbc | 207 | pos = 0; |
a0d0e21e LW |
208 | $x=/ABC/gi; |
209 | print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x; | |
210 | $x=/ABC/gi; | |
211 | print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x; | |
212 | $x=/ABC/gi; | |
213 | print $x == 0 ? "ok 57\n" : "not ok 57\n"; | |
71be2cbc | 214 | pos = 0; |
a0d0e21e LW |
215 | $x=/abc/g; |
216 | print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x; | |
217 | $x=/abc/g; | |
218 | print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x; | |
219 | $_ .= ''; | |
220 | @x=/abc/g; | |
221 | print scalar @x == 2 ? "ok 60\n" : "not ok 60\n"; | |
71be2cbc | 222 | |
223 | $_ = "abdc"; | |
224 | pos $_ = 2; | |
c90c0ff4 | 225 | /\Gc/gc; |
71be2cbc | 226 | print "not " if (pos $_) != 2; |
227 | print "ok 61\n"; | |
c90c0ff4 | 228 | /\Gc/g; |
229 | print "not " if defined pos $_; | |
230 | print "ok 62\n"; | |
c277df42 IZ |
231 | |
232 | $out = 1; | |
233 | 'abc' =~ m'a(?{ $out = 2 })b'; | |
234 | print "not " if $out != 2; | |
235 | print "ok 63\n"; | |
236 | ||
237 | $out = 1; | |
238 | 'abc' =~ m'a(?{ $out = 3 })c'; | |
239 | print "not " if $out != 1; | |
240 | print "ok 64\n"; | |
241 | ||
242 | $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6'; | |
243 | @out = /(?<!foo)bar./g; | |
244 | print "not " if "@out" ne 'bar2 barf'; | |
245 | print "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 | ||
255 | undef $@; | |
256 | print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@; | |
257 | print "ok 66\n"; | |
258 | ||
259 | undef $@; | |
260 | print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@; | |
261 | print "ok 67\n"; | |
262 | ||
263 | undef $@; | |
264 | print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@; | |
265 | print "ok 68\n"; | |
266 | ||
267 | undef $@; | |
268 | eval "'aaa' =~ /a{1,$reg_infty}/"; | |
269 | print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%; | |
270 | print "ok 69\n"; | |
271 | ||
272 | eval "'aaa' =~ /a{1,$reg_infty_p}/"; | |
273 | print "not " | |
274 | if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%; | |
275 | print "ok 70\n"; | |
276 | undef $@; | |
277 | ||
278 | # Poke a couple more parse failures | |
279 | ||
280 | $context = 'x' x 256; | |
281 | eval qq("${context}y" =~ /(?<=$context)y/); | |
282 | print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%; | |
283 | print "ok 71\n"; | |
284 | ||
b8c5462f | 285 | # removed test |
8d37f932 DD |
286 | print "ok 72\n"; |
287 | ||
c277df42 | 288 | # Long Monsters |
8d37f932 | 289 | $test = 73; |
c277df42 IZ |
290 | for $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 | ||
314 | for ( 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 | ||
328 | sub matchit { | |
cc6b7395 | 329 | m/ |
c277df42 IZ |
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 IZ |
357 | } |
358 | ||
0f5d15d6 | 359 | @ans = (); |
c277df42 IZ |
360 | push @ans, $res while $res = matchit; |
361 | ||
362 | print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1"; | |
363 | print "ok $test\n"; | |
364 | $test++; | |
365 | ||
366 | @ans = matchit; | |
367 | ||
368 | print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect; | |
369 | print "ok $test\n"; | |
370 | $test++; | |
371 | ||
0f5d15d6 IZ |
372 | my $matched; |
373 | $matched = qr/\((?:(?>[^()]+)|(?p{$matched}))*\)/; | |
374 | ||
375 | @ans = @ans1 = (); | |
376 | push(@ans, $res), push(@ans1, $&) while $res = m/$matched/g; | |
377 | ||
378 | print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1"; | |
379 | print "ok $test\n"; | |
380 | $test++; | |
381 | ||
382 | print "# ans1='@ans1'\n# expect='$expect'\nnot " if "@ans1" ne $expect; | |
383 | print "ok $test\n"; | |
384 | $test++; | |
385 | ||
386 | @ans = m/$matched/g; | |
387 | ||
388 | print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect; | |
389 | print "ok $test\n"; | |
390 | $test++; | |
391 | ||
c277df42 IZ |
392 | @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad |
393 | print "not " if "@ans" ne 'a/ b'; | |
394 | print "ok $test\n"; | |
395 | $test++; | |
396 | ||
cc6b7395 | 397 | $code = '{$blah = 45}'; |
c277df42 | 398 | $blah = 12; |
2cd61cdb IZ |
399 | eval { /(?$code)/ }; |
400 | print "not " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12; | |
e4d48cc9 GS |
401 | print "ok $test\n"; |
402 | $test++; | |
403 | ||
2cd61cdb IZ |
404 | for $code ('{$blah = 45}','=xx') { |
405 | $blah = 12; | |
406 | $res = eval { "xx" =~ /(?$code)/o }; | |
407 | if ($code eq '=xx') { | |
408 | print "#'$@','$res','$blah'\nnot " unless not $@ and $res; | |
409 | } else { | |
410 | print "#'$@','$res','$blah'\nnot " unless $@ and $@ =~ /not allowed at runtime/ and $blah == 12; | |
411 | } | |
412 | print "ok $test\n"; | |
413 | $test++; | |
414 | } | |
415 | ||
e4d48cc9 GS |
416 | $code = '{$blah = 45}'; |
417 | $blah = 12; | |
418 | eval "/(?$code)/"; | |
cc6b7395 IZ |
419 | print "not " if $blah != 45; |
420 | print "ok $test\n"; | |
421 | $test++; | |
422 | ||
423 | $blah = 12; | |
424 | /(?{$blah = 45})/; | |
c277df42 IZ |
425 | print "not " if $blah != 45; |
426 | print "ok $test\n"; | |
427 | $test++; | |
428 | ||
74d6a13a MB |
429 | $x = 'banana'; |
430 | $x =~ /.a/g; | |
431 | print "not " unless pos($x) == 2; | |
432 | print "ok $test\n"; | |
433 | $test++; | |
434 | ||
435 | $x =~ /.z/gc; | |
436 | print "not " unless pos($x) == 2; | |
437 | print "ok $test\n"; | |
438 | $test++; | |
439 | ||
440 | sub f { | |
441 | my $p = $_[0]; | |
442 | return $p; | |
443 | } | |
444 | ||
445 | $x =~ /.a/g; | |
446 | print "not " unless f(pos($x)) == 4; | |
447 | print "ok $test\n"; | |
448 | $test++; | |
4599a1de | 449 | |
ce862d02 IZ |
450 | $x = $^R = 67; |
451 | 'foot' =~ /foo(?{$x = 12; 75})[t]/; | |
452 | print "not " unless $^R eq '75'; | |
453 | print "ok $test\n"; | |
454 | $test++; | |
455 | ||
456 | $x = $^R = 67; | |
457 | 'foot' =~ /foo(?{$x = 12; 75})[xy]/; | |
458 | print "not " unless $^R eq '67' and $x eq '12'; | |
459 | print "ok $test\n"; | |
460 | $test++; | |
461 | ||
462 | $x = $^R = 67; | |
463 | 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/; | |
464 | print "not " unless $^R eq '79' and $x eq '12'; | |
465 | print "ok $test\n"; | |
466 | $test++; | |
467 | ||
8782bef2 GB |
468 | print "not " unless qr/\b\v$/i eq '(?i-xsm:\bv$)'; |
469 | print "ok $test\n"; | |
470 | $test++; | |
471 | ||
472 | print "not " unless qr/\b\v$/s eq '(?s-xim:\bv$)'; | |
473 | print "ok $test\n"; | |
474 | $test++; | |
475 | ||
476 | print "not " unless qr/\b\v$/m eq '(?m-xis:\bv$)'; | |
477 | print "ok $test\n"; | |
478 | $test++; | |
479 | ||
480 | print "not " unless qr/\b\v$/x eq '(?x-ism:\bv$)'; | |
481 | print "ok $test\n"; | |
482 | $test++; | |
483 | ||
484 | print "not " unless qr/\b\v$/xism eq '(?msix:\bv$)'; | |
485 | print "ok $test\n"; | |
486 | $test++; | |
487 | ||
488 | print "not " unless qr/\b\v$/ eq '(?-xism:\bv$)'; | |
97197631 IZ |
489 | print "ok $test\n"; |
490 | $test++; | |
491 | ||
7e5428c5 IZ |
492 | $_ = 'xabcx'; |
493 | foreach $ans ('', 'c') { | |
494 | /(?<=(?=a)..)((?=c)|.)/g; | |
495 | print "not " unless $1 eq $ans; | |
496 | print "ok $test\n"; | |
497 | $test++; | |
498 | } | |
499 | ||
500 | $_ = 'a'; | |
501 | foreach $ans ('', 'a', '') { | |
502 | /^|a|$/g; | |
503 | print "not " unless $& eq $ans; | |
504 | print "ok $test\n"; | |
505 | $test++; | |
506 | } | |
507 | ||
09f25ae4 IZ |
508 | sub prefixify { |
509 | my($v,$a,$b,$res) = @_; | |
510 | $v =~ s/\Q$a\E/$b/; | |
511 | print "not " unless $res eq $v; | |
512 | print "ok $test\n"; | |
513 | $test++; | |
514 | } | |
515 | prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch'); | |
516 | prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch'); | |
517 | ||
518 | $_ = 'var="foo"'; | |
519 | /(\")/; | |
520 | print "not " unless $1 and /$1/; | |
521 | print "ok $test\n"; | |
522 | $test++; | |
523 | ||
8782bef2 | 524 | $a=qr/(?{++$b})/; |
2cd61cdb IZ |
525 | $b = 7; |
526 | /$a$a/; | |
527 | print "not " unless $b eq '9'; | |
528 | print "ok $test\n"; | |
529 | $test++; | |
530 | ||
531 | $c="$a"; | |
532 | /$a$a/; | |
533 | print "not " unless $b eq '11'; | |
534 | print "ok $test\n"; | |
535 | $test++; | |
536 | ||
537 | { | |
538 | use re "eval"; | |
539 | /$a$c$a/; | |
540 | print "not " unless $b eq '14'; | |
541 | print "ok $test\n"; | |
542 | $test++; | |
543 | ||
544 | no re "eval"; | |
545 | $match = eval { /$a$c$a/ }; | |
546 | print "not " | |
547 | unless $b eq '14' and $@ =~ /Eval-group not allowed/ and not $match; | |
548 | print "ok $test\n"; | |
549 | $test++; | |
550 | } | |
cbce877f IZ |
551 | |
552 | { | |
553 | package aa; | |
554 | $c = 2; | |
555 | $::c = 3; | |
556 | '' =~ /(?{ $c = 4 })/; | |
557 | print "not " unless $c == 4; | |
558 | } | |
559 | print "ok $test\n"; | |
560 | $test++; | |
561 | print "not " unless $c == 3; | |
562 | print "ok $test\n"; | |
563 | $test++; | |
2cd61cdb | 564 | |
4599a1de JH |
565 | sub must_warn_pat { |
566 | my $warn_pat = shift; | |
567 | return sub { print "not " unless $_[0] =~ /$warn_pat/ } | |
568 | } | |
569 | ||
570 | sub must_warn { | |
571 | my ($warn_pat, $code) = @_; | |
572 | local $^W; local %SIG; | |
573 | eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code; | |
574 | print "ok $test\n"; | |
575 | $test++; | |
576 | } | |
577 | ||
578 | ||
579 | sub make_must_warn { | |
580 | my $warn_pat = shift; | |
581 | return sub { must_warn(must_warn_pat($warn_pat)) } | |
582 | } | |
583 | ||
584 | my $for_future = make_must_warn('reserved for future extensions'); | |
585 | ||
586 | &$for_future('q(a:[b]:) =~ /[x[:foo:]]/'); | |
587 | &$for_future('q(a=[b]=) =~ /[x[=foo=]]/'); | |
588 | &$for_future('q(a.[b].) =~ /[x[.foo.]]/'); | |
f7e33566 GS |
589 | |
590 | # test if failure of patterns returns empty list | |
591 | $_ = 'aaa'; | |
592 | @_ = /bbb/; | |
593 | print "not " if @_; | |
594 | print "ok $test\n"; | |
595 | $test++; | |
596 | ||
597 | @_ = /bbb/g; | |
598 | print "not " if @_; | |
599 | print "ok $test\n"; | |
600 | $test++; | |
601 | ||
602 | @_ = /(bbb)/; | |
603 | print "not " if @_; | |
604 | print "ok $test\n"; | |
605 | $test++; | |
606 | ||
607 | @_ = /(bbb)/g; | |
608 | print "not " if @_; | |
609 | print "ok $test\n"; | |
610 | $test++; | |
611 | ||
6cef1e77 IZ |
612 | /a(?=.$)/; |
613 | print "not " if $#+ != 0 or $#- != 0; | |
614 | print "ok $test\n"; | |
615 | $test++; | |
616 | ||
617 | print "not " if $+[0] != 2 or $-[0] != 1; | |
618 | print "ok $test\n"; | |
619 | $test++; | |
620 | ||
621 | print "not " | |
622 | if defined $+[1] or defined $-[1] or defined $+[2] or defined $-[2]; | |
623 | print "ok $test\n"; | |
624 | $test++; | |
625 | ||
626 | /a(a)(a)/; | |
627 | print "not " if $#+ != 2 or $#- != 2; | |
628 | print "ok $test\n"; | |
629 | $test++; | |
630 | ||
631 | print "not " if $+[0] != 3 or $-[0] != 0; | |
632 | print "ok $test\n"; | |
633 | $test++; | |
634 | ||
635 | print "not " if $+[1] != 2 or $-[1] != 1; | |
636 | print "ok $test\n"; | |
637 | $test++; | |
638 | ||
639 | print "not " if $+[2] != 3 or $-[2] != 2; | |
640 | print "ok $test\n"; | |
641 | $test++; | |
642 | ||
643 | print "not " | |
644 | if defined $+[3] or defined $-[3] or defined $+[4] or defined $-[4]; | |
645 | print "ok $test\n"; | |
646 | $test++; | |
647 | ||
648 | /.(a)(b)?(a)/; | |
649 | print "not " if $#+ != 3 or $#- != 3; | |
650 | print "ok $test\n"; | |
651 | $test++; | |
652 | ||
653 | print "not " if $+[0] != 3 or $-[0] != 0; | |
654 | print "ok $test\n"; | |
655 | $test++; | |
656 | ||
657 | print "not " if $+[1] != 2 or $-[1] != 1; | |
658 | print "ok $test\n"; | |
659 | $test++; | |
660 | ||
661 | print "not " if $+[3] != 3 or $-[3] != 2; | |
662 | print "ok $test\n"; | |
663 | $test++; | |
664 | ||
665 | print "not " | |
666 | if defined $+[2] or defined $-[2] or defined $+[4] or defined $-[4]; | |
667 | print "ok $test\n"; | |
668 | $test++; | |
669 | ||
670 | /.(a)/; | |
671 | print "not " if $#+ != 1 or $#- != 1; | |
672 | print "ok $test\n"; | |
673 | $test++; | |
674 | ||
675 | print "not " if $+[0] != 2 or $-[0] != 0; | |
676 | print "ok $test\n"; | |
677 | $test++; | |
678 | ||
679 | print "not " if $+[1] != 2 or $-[1] != 1; | |
680 | print "ok $test\n"; | |
681 | $test++; | |
682 | ||
683 | print "not " | |
684 | if defined $+[2] or defined $-[2] or defined $+[3] or defined $-[3]; | |
685 | print "ok $test\n"; | |
686 | $test++; | |
687 | ||
8f580fb8 IZ |
688 | /.(a)(ba*)?/; |
689 | print "#$#-..$#+\nnot " if $#+ != 2 or $#- != 1; | |
690 | print "ok $test\n"; | |
691 | $test++; | |
692 | ||
ad94a511 IZ |
693 | $_ = 'aaa'; |
694 | pos = 1; | |
695 | @a = /\Ga/g; | |
696 | print "not " unless "@a" eq "a a"; | |
697 | print "ok $test\n"; | |
698 | $test++; | |
699 | ||
22e551b9 IZ |
700 | $str = 'abcde'; |
701 | pos $str = 2; | |
702 | ||
703 | print "not " if $str =~ /^\G/; | |
704 | print "ok $test\n"; | |
705 | $test++; | |
706 | ||
707 | print "not " if $str =~ /^.\G/; | |
708 | print "ok $test\n"; | |
709 | $test++; | |
710 | ||
711 | print "not " unless $str =~ /^..\G/; | |
712 | print "ok $test\n"; | |
713 | $test++; | |
714 | ||
715 | print "not " if $str =~ /^...\G/; | |
716 | print "ok $test\n"; | |
717 | $test++; | |
718 | ||
719 | print "not " unless $str =~ /.\G./ and $& eq 'bc'; | |
720 | print "ok $test\n"; | |
721 | $test++; | |
722 | ||
723 | print "not " unless $str =~ /\G../ and $& eq 'cd'; | |
724 | print "ok $test\n"; | |
725 | $test++; | |
726 | ||
9661b544 IZ |
727 | undef $foo; undef $bar; |
728 | print "#'$str','$foo','$bar'\nnot " | |
729 | unless $str =~ /b(?{$foo = $_; $bar = pos})c/ | |
730 | and $foo eq 'abcde' and $bar eq 2; | |
731 | print "ok $test\n"; | |
732 | $test++; | |
733 | ||
734 | undef $foo; undef $bar; | |
735 | pos $str = undef; | |
736 | print "#'$str','$foo','$bar'\nnot " | |
737 | unless $str =~ /b(?{$foo = $_; $bar = pos})c/g | |
738 | and $foo eq 'abcde' and $bar eq 2 and pos $str eq 3; | |
739 | print "ok $test\n"; | |
740 | $test++; | |
741 | ||
742 | $_ = $str; | |
743 | ||
744 | undef $foo; undef $bar; | |
745 | print "#'$str','$foo','$bar'\nnot " | |
746 | unless /b(?{$foo = $_; $bar = pos})c/ | |
747 | and $foo eq 'abcde' and $bar eq 2; | |
748 | print "ok $test\n"; | |
749 | $test++; | |
750 | ||
751 | undef $foo; undef $bar; | |
752 | print "#'$str','$foo','$bar'\nnot " | |
753 | unless /b(?{$foo = $_; $bar = pos})c/g | |
754 | and $foo eq 'abcde' and $bar eq 2 and pos eq 3; | |
755 | print "ok $test\n"; | |
756 | $test++; | |
757 | ||
758 | undef $foo; undef $bar; | |
759 | pos = undef; | |
760 | 1 while /b(?{$foo = $_; $bar = pos})c/g; | |
761 | print "#'$str','$foo','$bar'\nnot " | |
762 | unless $foo eq 'abcde' and $bar eq 2 and not defined pos; | |
763 | print "ok $test\n"; | |
764 | $test++; | |
765 | ||
766 | undef $foo; undef $bar; | |
767 | $_ = 'abcde|abcde'; | |
768 | print "#'$str','$foo','$bar','$_'\nnot " | |
769 | unless s/b(?{$foo = $_; $bar = pos})c/x/g and $foo eq 'abcde|abcde' | |
770 | and $bar eq 8 and $_ eq 'axde|axde'; | |
771 | print "ok $test\n"; | |
772 | $test++; | |
773 | ||
5c5e4c24 IZ |
774 | @res = (); |
775 | # List context: | |
776 | $_ = 'abcde|abcde'; | |
777 | @dummy = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g; | |
778 | @res = map {defined $_ ? "'$_'" : 'undef'} @res; | |
779 | $res = "@res"; | |
780 | print "#'@res' '$_'\nnot " | |
781 | unless "@res" eq "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'"; | |
782 | print "ok $test\n"; | |
783 | $test++; | |
784 | ||
785 | @res = (); | |
786 | @dummy = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g; | |
787 | @res = map {defined $_ ? "'$_'" : 'undef'} @res; | |
788 | $res = "@res"; | |
789 | print "#'@res' '$_'\nnot " | |
790 | unless "@res" eq | |
791 | "'' 'ab' 'cde|abcde' " . | |
792 | "'' 'abc' 'de|abcde' " . | |
793 | "'abcd' 'e|' 'abcde' " . | |
794 | "'abcde|' 'ab' 'cde' " . | |
795 | "'abcde|' 'abc' 'de'" ; | |
796 | print "ok $test\n"; | |
797 | $test++; | |
798 | ||
b7a35066 IZ |
799 | #Some more \G anchor checks |
800 | $foo='aabbccddeeffgg'; | |
801 | ||
802 | pos($foo)=1; | |
803 | ||
804 | $foo=~/.\G(..)/g; | |
805 | print "not " unless($1 eq 'ab'); | |
806 | print "ok $test\n"; | |
807 | $test++; | |
808 | ||
809 | pos($foo) += 1; | |
810 | $foo=~/.\G(..)/g; | |
811 | print "not " unless($1 eq 'cc'); | |
812 | print "ok $test\n"; | |
813 | $test++; | |
814 | ||
815 | pos($foo) += 1; | |
816 | $foo=~/.\G(..)/g; | |
817 | print "not " unless($1 eq 'de'); | |
818 | print "ok $test\n"; | |
819 | $test++; | |
820 | ||
0ef3e39e HS |
821 | print "not " unless $foo =~ /\Gef/g; |
822 | print "ok $test\n"; | |
823 | $test++; | |
824 | ||
b7a35066 IZ |
825 | undef pos $foo; |
826 | ||
827 | $foo=~/\G(..)/g; | |
828 | print "not " unless($1 eq 'aa'); | |
829 | print "ok $test\n"; | |
830 | $test++; | |
831 | ||
832 | $foo=~/\G(..)/g; | |
833 | print "not " unless($1 eq 'bb'); | |
834 | print "ok $test\n"; | |
835 | $test++; | |
836 | ||
837 | pos($foo)=5; | |
838 | $foo=~/\G(..)/g; | |
839 | print "not " unless($1 eq 'cd'); | |
840 | print "ok $test\n"; | |
841 | $test++; | |
842 | ||
e60df1fa IZ |
843 | $_='123x123'; |
844 | @res = /(\d*|x)/g; | |
845 | print "not " unless('123||x|123|' eq join '|', @res); | |
846 | print "ok $test\n"; | |
847 | $test++; | |
848 | ||
9d080a66 GS |
849 | # see if matching against temporaries (created via pp_helem()) is safe |
850 | { foo => "ok $test\n".$^X }->{foo} =~ /^(.*)\n/g; | |
851 | print "$1\n"; | |
852 | $test++; | |
853 | ||
cf93c79d IZ |
854 | # See if $i work inside (?{}) in the presense of saved substrings and |
855 | # changing $_ | |
856 | @a = qw(foo bar); | |
857 | @b = (); | |
858 | s/(\w)(?{push @b, $1})/,$1,/g for @a; | |
859 | ||
860 | print "# \@b='@b', expect 'f o o b a r'\nnot " unless("@b" eq "f o o b a r"); | |
861 | print "ok $test\n"; | |
862 | $test++; | |
863 | ||
864 | print "not " unless("@a" eq ",f,,o,,o, ,b,,a,,r,"); | |
865 | print "ok $test\n"; | |
866 | $test++; | |
867 |