Commit | Line | Data |
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 | |
09f25ae4 |
9 | print "1..123\n"; |
8d37f932 |
10 | |
11 | chdir 't' if -d 't'; |
12 | @INC = "../lib"; |
13 | eval 'use Config'; # Defaults assumed if this fails |
8d063cd8 |
14 | |
15 | $x = "abc\ndef\n"; |
16 | |
17 | if ($x =~ /^abc/) {print "ok 1\n";} else {print "not ok 1\n";} |
18 | if ($x !~ /^def/) {print "ok 2\n";} else {print "not ok 2\n";} |
19 | |
20 | $* = 1; |
21 | if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";} |
22 | $* = 0; |
23 | |
24 | $_ = '123'; |
25 | if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";} |
26 | |
27 | if ($x =~ /^xxx/) {print "not ok 5\n";} else {print "ok 5\n";} |
28 | if ($x !~ /^abc/) {print "not ok 6\n";} else {print "ok 6\n";} |
29 | |
30 | if ($x =~ /def/) {print "ok 7\n";} else {print "not ok 7\n";} |
31 | if ($x !~ /def/) {print "not ok 8\n";} else {print "ok 8\n";} |
32 | |
33 | if ($x !~ /.def/) {print "ok 9\n";} else {print "not ok 9\n";} |
34 | if ($x =~ /.def/) {print "not ok 10\n";} else {print "ok 10\n";} |
35 | |
36 | if ($x =~ /\ndef/) {print "ok 11\n";} else {print "not ok 11\n";} |
37 | if ($x !~ /\ndef/) {print "not ok 12\n";} else {print "ok 12\n";} |
38 | |
39 | $_ = 'aaabbbccc'; |
40 | if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') { |
41 | print "ok 13\n"; |
42 | } else { |
43 | print "not ok 13\n"; |
44 | } |
45 | if (/(a+b+c+)/ && $1 eq 'aaabbbccc') { |
46 | print "ok 14\n"; |
47 | } else { |
48 | print "not ok 14\n"; |
49 | } |
50 | |
51 | if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";} |
52 | |
53 | $_ = 'aaabccc'; |
54 | if (/a+b?c+/) {print "ok 16\n";} else {print "not ok 16\n";} |
55 | if (/a*b+c*/) {print "ok 17\n";} else {print "not ok 17\n";} |
56 | |
57 | $_ = 'aaaccc'; |
58 | if (/a*b?c*/) {print "ok 18\n";} else {print "not ok 18\n";} |
59 | if (/a*b+c*/) {print "not ok 19\n";} else {print "ok 19\n";} |
60 | |
61 | $_ = 'abcdef'; |
62 | if (/bcd|xyz/) {print "ok 20\n";} else {print "not ok 20\n";} |
63 | if (/xyz|bcd/) {print "ok 21\n";} else {print "not ok 21\n";} |
64 | |
65 | if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";} |
378cc40b |
66 | |
67 | if (/^$_$/) {print "ok 23\n";} else {print "not ok 23\n";} |
68 | |
69 | $* = 1; # test 3 only tested the optimized version--this one is for real |
70 | if ("ab\ncd\n" =~ /^cd/) {print "ok 24\n";} else {print "not ok 24\n";} |
71 | $* = 0; |
72 | |
73 | $XXX{123} = 123; |
74 | $XXX{234} = 234; |
75 | $XXX{345} = 345; |
76 | |
77 | @XXX = ('ok 25','not ok 25', 'ok 26','not ok 26','not ok 27'); |
c6aa4a32 |
78 | while ($_ = shift(@XXX)) { |
378cc40b |
79 | ?(.*)? && (print $1,"\n"); |
80 | /not/ && reset; |
81 | /not ok 26/ && reset 'X'; |
82 | } |
83 | |
a0d0e21e |
84 | while (($key,$val) = each(%XXX)) { |
378cc40b |
85 | print "not ok 27\n"; |
86 | exit; |
87 | } |
88 | |
89 | print "ok 27\n"; |
90 | |
91 | 'cde' =~ /[^ab]*/; |
92 | 'xyz' =~ //; |
93 | if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";} |
94 | |
95 | $foo = '[^ab]*'; |
96 | 'cde' =~ /$foo/; |
97 | 'xyz' =~ //; |
98 | if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";} |
99 | |
100 | $foo = '[^ab]*'; |
101 | 'cde' =~ /$foo/; |
102 | 'xyz' =~ /$null/; |
103 | if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";} |
a687059c |
104 | |
105 | $_ = 'abcdefghi'; |
106 | /def/; # optimized up to cmd |
107 | if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";} |
108 | |
109 | /cde/ + 0; # optimized only to spat |
110 | if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";} |
111 | |
112 | /[d][e][f]/; # not optimized |
113 | if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 33\n";} else {print "not ok 33\n";} |
114 | |
115 | $_ = 'now is the {time for all} good men to come to.'; |
116 | / {([^}]*)}/; |
117 | if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";} |
118 | |
119 | $_ = 'xxx {3,4} yyy zzz'; |
120 | print /( {3,4})/ ? "ok 35\n" : "not ok 35\n"; |
121 | print $1 eq ' ' ? "ok 36\n" : "not ok 36\n"; |
122 | print /( {4,})/ ? "not ok 37\n" : "ok 37\n"; |
123 | print /( {2,3}.)/ ? "ok 38\n" : "not ok 38\n"; |
124 | print $1 eq ' y' ? "ok 39\n" : "not ok 39\n"; |
125 | print /(y{2,3}.)/ ? "ok 40\n" : "not ok 40\n"; |
126 | print $1 eq 'yyy ' ? "ok 41\n" : "not ok 41\n"; |
127 | print /x {3,4}/ ? "not ok 42\n" : "ok 42\n"; |
128 | print /^xxx {3,4}/ ? "not ok 43\n" : "ok 43\n"; |
352d5a3a |
129 | |
130 | $_ = "now is the time for all good men to come to."; |
131 | @words = /(\w+)/g; |
132 | print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to" |
133 | ? "ok 44\n" |
134 | : "not ok 44\n"; |
135 | |
136 | @words = (); |
137 | while (/\w+/g) { |
138 | push(@words, $&); |
139 | } |
140 | print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to" |
141 | ? "ok 45\n" |
142 | : "not ok 45\n"; |
143 | |
144 | @words = (); |
71be2cbc |
145 | pos = 0; |
352d5a3a |
146 | while (/to/g) { |
147 | push(@words, $&); |
148 | } |
149 | print join(':',@words) eq "to:to" |
150 | ? "ok 46\n" |
71be2cbc |
151 | : "not ok 46 `@words'\n"; |
352d5a3a |
152 | |
71be2cbc |
153 | pos $_ = 0; |
352d5a3a |
154 | @words = /to/g; |
155 | print join(':',@words) eq "to:to" |
156 | ? "ok 47\n" |
71be2cbc |
157 | : "not ok 47 `@words'\n"; |
352d5a3a |
158 | |
159 | $_ = "abcdefghi"; |
160 | |
161 | $pat1 = 'def'; |
162 | $pat2 = '^def'; |
163 | $pat3 = '.def.'; |
164 | $pat4 = 'abc'; |
165 | $pat5 = '^abc'; |
166 | $pat6 = 'abc$'; |
167 | $pat7 = 'ghi'; |
168 | $pat8 = '\w*ghi'; |
169 | $pat9 = 'ghi$'; |
170 | |
171 | $t1=$t2=$t3=$t4=$t5=$t6=$t7=$t8=$t9=0; |
172 | |
173 | for $iter (1..5) { |
174 | $t1++ if /$pat1/o; |
175 | $t2++ if /$pat2/o; |
176 | $t3++ if /$pat3/o; |
177 | $t4++ if /$pat4/o; |
178 | $t5++ if /$pat5/o; |
179 | $t6++ if /$pat6/o; |
180 | $t7++ if /$pat7/o; |
181 | $t8++ if /$pat8/o; |
182 | $t9++ if /$pat9/o; |
183 | } |
184 | |
185 | $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9"; |
186 | print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n"; |
1462b684 |
187 | |
188 | $xyz = 'xyz'; |
189 | print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n"; |
190 | |
191 | # perl 4.009 says "unmatched ()" |
192 | eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"'; |
193 | print $@ eq "" ? "ok 50\n" : "not ok 50\n"; |
194 | print $result eq "abc:bc" ? "ok 51\n" : "not ok 51\n"; |
a0d0e21e |
195 | |
196 | |
197 | $_="abcfooabcbar"; |
198 | $x=/abc/g; |
199 | print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x; |
200 | $x=/abc/g; |
201 | print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x; |
202 | $x=/abc/g; |
203 | print $x == 0 ? "ok 54\n" : "not ok 54\n"; |
71be2cbc |
204 | pos = 0; |
a0d0e21e |
205 | $x=/ABC/gi; |
206 | print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x; |
207 | $x=/ABC/gi; |
208 | print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x; |
209 | $x=/ABC/gi; |
210 | print $x == 0 ? "ok 57\n" : "not ok 57\n"; |
71be2cbc |
211 | pos = 0; |
a0d0e21e |
212 | $x=/abc/g; |
213 | print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x; |
214 | $x=/abc/g; |
215 | print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x; |
216 | $_ .= ''; |
217 | @x=/abc/g; |
218 | print scalar @x == 2 ? "ok 60\n" : "not ok 60\n"; |
71be2cbc |
219 | |
220 | $_ = "abdc"; |
221 | pos $_ = 2; |
c90c0ff4 |
222 | /\Gc/gc; |
71be2cbc |
223 | print "not " if (pos $_) != 2; |
224 | print "ok 61\n"; |
c90c0ff4 |
225 | /\Gc/g; |
226 | print "not " if defined pos $_; |
227 | print "ok 62\n"; |
c277df42 |
228 | |
229 | $out = 1; |
230 | 'abc' =~ m'a(?{ $out = 2 })b'; |
231 | print "not " if $out != 2; |
232 | print "ok 63\n"; |
233 | |
234 | $out = 1; |
235 | 'abc' =~ m'a(?{ $out = 3 })c'; |
236 | print "not " if $out != 1; |
237 | print "ok 64\n"; |
238 | |
239 | $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6'; |
240 | @out = /(?<!foo)bar./g; |
241 | print "not " if "@out" ne 'bar2 barf'; |
242 | print "ok 65\n"; |
243 | |
8d37f932 |
244 | # Tests which depend on REG_INFTY |
245 | $reg_infty = defined $Config{reg_infty} ? $Config{reg_infty} : 32767; |
246 | $reg_infty_m = $reg_infty - 1; $reg_infty_p = $reg_infty + 1; |
247 | |
248 | # As well as failing if the pattern matches do unexpected things, the |
249 | # next three tests will fail if you should have picked up a lower-than- |
250 | # default value for $reg_infty from Config.pm, but have not. |
251 | |
252 | undef $@; |
253 | print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@; |
254 | print "ok 66\n"; |
255 | |
256 | undef $@; |
257 | print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@; |
258 | print "ok 67\n"; |
259 | |
260 | undef $@; |
261 | print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@; |
262 | print "ok 68\n"; |
263 | |
264 | undef $@; |
265 | eval "'aaa' =~ /a{1,$reg_infty}/"; |
266 | print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%; |
267 | print "ok 69\n"; |
268 | |
269 | eval "'aaa' =~ /a{1,$reg_infty_p}/"; |
270 | print "not " |
271 | if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%; |
272 | print "ok 70\n"; |
273 | undef $@; |
274 | |
275 | # Poke a couple more parse failures |
276 | |
277 | $context = 'x' x 256; |
278 | eval qq("${context}y" =~ /(?<=$context)y/); |
279 | print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%; |
280 | print "ok 71\n"; |
281 | |
282 | # This one will fail when POSIX character classes do get implemented |
283 | { |
284 | my $w; |
285 | local $^W = 1; |
286 | local $SIG{__WARN__} = sub{$w = shift}; |
287 | eval q('a' =~ /[[:alpha:]]/); |
288 | print "not " if $w !~ /^\QCharacter class syntax [: :] is reserved/; |
289 | } |
290 | print "ok 72\n"; |
291 | |
c277df42 |
292 | # Long Monsters |
8d37f932 |
293 | $test = 73; |
c277df42 |
294 | for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory |
295 | $a = 'a' x $l; |
296 | print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/; |
297 | print "ok $test\n"; |
298 | $test++; |
299 | |
300 | print "not " if "b$a=" =~ /a$a=/; |
301 | print "ok $test\n"; |
302 | $test++; |
303 | } |
304 | |
305 | # 20000 nodes, each taking 3 words per string, and 1 per branch |
306 | $long_constant_len = join '|', 12120 .. 32645; |
307 | $long_var_len = join '|', 8120 .. 28645; |
308 | %ans = ( 'ax13876y25677lbc' => 1, |
309 | 'ax13876y25677mcb' => 0, # not b. |
310 | 'ax13876y35677nbc' => 0, # Num too big |
311 | 'ax13876y25677y21378obc' => 1, |
312 | 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o] |
313 | 'ax13876y25677y21378y21378kbc' => 1, |
314 | 'ax13876y25677y21378y21378kcb' => 0, # Not b. |
315 | 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs |
316 | ); |
317 | |
318 | for ( keys %ans ) { |
319 | print "# const-len `$_' not => $ans{$_}\nnot " |
320 | if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o; |
321 | print "ok $test\n"; |
322 | $test++; |
323 | print "# var-len `$_' not => $ans{$_}\nnot " |
324 | if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o; |
325 | print "ok $test\n"; |
326 | $test++; |
327 | } |
328 | |
329 | $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e"; |
330 | $expect = "(bla()) ((l)u((e))) (l(e)e)"; |
331 | |
332 | sub matchit { |
cc6b7395 |
333 | m/ |
c277df42 |
334 | ( |
335 | \( |
336 | (?{ $c = 1 }) # Initialize |
337 | (?: |
338 | (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop |
339 | (?! |
340 | ) # Fail: will unwind one iteration back |
341 | ) |
342 | (?: |
343 | [^()]+ # Match a big chunk |
344 | (?= |
345 | [()] |
346 | ) # Do not try to match subchunks |
347 | | |
348 | \( |
349 | (?{ ++$c }) |
350 | | |
351 | \) |
352 | (?{ --$c }) |
353 | ) |
354 | )+ # This may not match with different subblocks |
355 | ) |
356 | (?(?{ $c != 0 }) |
357 | (?! |
358 | ) # Fail |
359 | ) # Otherwise the chunk 1 may succeed with $c>0 |
cc6b7395 |
360 | /xg; |
c277df42 |
361 | } |
362 | |
363 | push @ans, $res while $res = matchit; |
364 | |
365 | print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1"; |
366 | print "ok $test\n"; |
367 | $test++; |
368 | |
369 | @ans = matchit; |
370 | |
371 | print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect; |
372 | print "ok $test\n"; |
373 | $test++; |
374 | |
375 | @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad |
376 | print "not " if "@ans" ne 'a/ b'; |
377 | print "ok $test\n"; |
378 | $test++; |
379 | |
cc6b7395 |
380 | $code = '{$blah = 45}'; |
c277df42 |
381 | $blah = 12; |
cc6b7395 |
382 | /(?$code)/; |
383 | print "not " if $blah != 45; |
384 | print "ok $test\n"; |
385 | $test++; |
386 | |
387 | $blah = 12; |
388 | /(?{$blah = 45})/; |
c277df42 |
389 | print "not " if $blah != 45; |
390 | print "ok $test\n"; |
391 | $test++; |
392 | |
74d6a13a |
393 | $x = 'banana'; |
394 | $x =~ /.a/g; |
395 | print "not " unless pos($x) == 2; |
396 | print "ok $test\n"; |
397 | $test++; |
398 | |
399 | $x =~ /.z/gc; |
400 | print "not " unless pos($x) == 2; |
401 | print "ok $test\n"; |
402 | $test++; |
403 | |
404 | sub f { |
405 | my $p = $_[0]; |
406 | return $p; |
407 | } |
408 | |
409 | $x =~ /.a/g; |
410 | print "not " unless f(pos($x)) == 4; |
411 | print "ok $test\n"; |
412 | $test++; |
4599a1de |
413 | |
ce862d02 |
414 | $x = $^R = 67; |
415 | 'foot' =~ /foo(?{$x = 12; 75})[t]/; |
416 | print "not " unless $^R eq '75'; |
417 | print "ok $test\n"; |
418 | $test++; |
419 | |
420 | $x = $^R = 67; |
421 | 'foot' =~ /foo(?{$x = 12; 75})[xy]/; |
422 | print "not " unless $^R eq '67' and $x eq '12'; |
423 | print "ok $test\n"; |
424 | $test++; |
425 | |
426 | $x = $^R = 67; |
427 | 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/; |
428 | print "not " unless $^R eq '79' and $x eq '12'; |
429 | print "ok $test\n"; |
430 | $test++; |
431 | |
97197631 |
432 | # This should be changed to qr/\b\v$/ ASAP |
433 | print "not " unless study(/\b\v$/) eq '\bv$'; |
434 | print "ok $test\n"; |
435 | $test++; |
436 | |
7e5428c5 |
437 | $_ = 'xabcx'; |
438 | foreach $ans ('', 'c') { |
439 | /(?<=(?=a)..)((?=c)|.)/g; |
440 | print "not " unless $1 eq $ans; |
441 | print "ok $test\n"; |
442 | $test++; |
443 | } |
444 | |
445 | $_ = 'a'; |
446 | foreach $ans ('', 'a', '') { |
447 | /^|a|$/g; |
448 | print "not " unless $& eq $ans; |
449 | print "ok $test\n"; |
450 | $test++; |
451 | } |
452 | |
09f25ae4 |
453 | sub prefixify { |
454 | my($v,$a,$b,$res) = @_; |
455 | $v =~ s/\Q$a\E/$b/; |
456 | print "not " unless $res eq $v; |
457 | print "ok $test\n"; |
458 | $test++; |
459 | } |
460 | prefixify('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch'); |
461 | prefixify('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch'); |
462 | |
463 | $_ = 'var="foo"'; |
464 | /(\")/; |
465 | print "not " unless $1 and /$1/; |
466 | print "ok $test\n"; |
467 | $test++; |
468 | |
4599a1de |
469 | sub must_warn_pat { |
470 | my $warn_pat = shift; |
471 | return sub { print "not " unless $_[0] =~ /$warn_pat/ } |
472 | } |
473 | |
474 | sub must_warn { |
475 | my ($warn_pat, $code) = @_; |
476 | local $^W; local %SIG; |
477 | eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code; |
478 | print "ok $test\n"; |
479 | $test++; |
480 | } |
481 | |
482 | |
483 | sub make_must_warn { |
484 | my $warn_pat = shift; |
485 | return sub { must_warn(must_warn_pat($warn_pat)) } |
486 | } |
487 | |
488 | my $for_future = make_must_warn('reserved for future extensions'); |
489 | |
490 | &$for_future('q(a:[b]:) =~ /[x[:foo:]]/'); |
491 | &$for_future('q(a=[b]=) =~ /[x[=foo=]]/'); |
492 | &$for_future('q(a.[b].) =~ /[x[.foo.]]/'); |