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.
7 # $RCSfile: pat.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:12 $
13 eval 'use Config'; # Defaults assumed if this fails
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";}
21 if ($x =~ /^def/) {print "ok 3\n";} else {print "not ok 3\n";}
25 if (/^([0-9][0-9]*)/) {print "ok 4\n";} else {print "not ok 4\n";}
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";}
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";}
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";}
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";}
40 if (/(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc') {
45 if (/(a+b+c+)/ && $1 eq 'aaabbbccc') {
51 if (/a+b?c+/) {print "not ok 15\n";} else {print "ok 15\n";}
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";}
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";}
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";}
65 if (m|bc/*d|) {print "ok 22\n";} else {print "not ok 22\n";}
67 if (/^$_$/) {print "ok 23\n";} else {print "not ok 23\n";}
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";}
77 @XXX = ('ok 25','not ok 25', 'ok 26','not ok 26','not ok 27');
78 while ($_ = shift(@XXX)) {
79 ?(.*)? && (print $1,"\n");
81 /not ok 26/ && reset 'X';
84 while (($key,$val) = each(%XXX)) {
93 if ($& eq 'xyz') {print "ok 28\n";} else {print "not ok 28\n";}
98 if ($& eq 'xyz') {print "ok 29\n";} else {print "not ok 29\n";}
103 if ($& eq 'xyz') {print "ok 30\n";} else {print "not ok 30\n";}
106 /def/; # optimized up to cmd
107 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 31\n";} else {print "not ok 31\n";}
109 /cde/ + 0; # optimized only to spat
110 if ("$`:$&:$'" eq 'ab:cde:fghi') {print "ok 32\n";} else {print "not ok 32\n";}
112 /[d][e][f]/; # not optimized
113 if ("$`:$&:$'" eq 'abc:def:ghi') {print "ok 33\n";} else {print "not ok 33\n";}
115 $_ = 'now is the {time for all} good men to come to.';
117 if ($1 eq 'time for all') {print "ok 34\n";} else {print "not ok 34 $1\n";}
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";
130 $_ = "now is the time for all good men to come to.";
132 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
140 print join(':',@words) eq "now:is:the:time:for:all:good:men:to:come:to"
149 print join(':',@words) eq "to:to"
151 : "not ok 46 `@words'\n";
155 print join(':',@words) eq "to:to"
157 : "not ok 47 `@words'\n";
171 $t1=$t2=$t3=$t4=$t5=$t6=$t7=$t8=$t9=0;
185 $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
186 print $x eq '505550555' ? "ok 48\n" : "not ok 48 $x\n";
189 print "abc" =~ /^abc$|$xyz/ ? "ok 49\n" : "not ok 49\n";
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";
199 print $` eq "" ? "ok 52\n" : "not ok 52\n" if $x;
201 print $` eq "abcfoo" ? "ok 53\n" : "not ok 53\n" if $x;
203 print $x == 0 ? "ok 54\n" : "not ok 54\n";
206 print $` eq "" ? "ok 55\n" : "not ok 55\n" if $x;
208 print $` eq "abcfoo" ? "ok 56\n" : "not ok 56\n" if $x;
210 print $x == 0 ? "ok 57\n" : "not ok 57\n";
213 print $' eq "fooabcbar" ? "ok 58\n" : "not ok 58\n" if $x;
215 print $' eq "bar" ? "ok 59\n" : "not ok 59\n" if $x;
218 print scalar @x == 2 ? "ok 60\n" : "not ok 60\n";
223 print "not " if (pos $_) != 2;
226 print "not " if defined pos $_;
230 'abc' =~ m'a(?{ $out = 2 })b';
231 print "not " if $out != 2;
235 'abc' =~ m'a(?{ $out = 3 })c';
236 print "not " if $out != 1;
239 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
240 @out = /(?<!foo)bar./g;
241 print "not " if "@out" ne 'bar2 barf';
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;
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.
253 print "not " if eval q(('aaa' =~ /(a{1,$reg_infty_m})/)[0] ne 'aaa') || $@;
257 print "not " if eval q(('a' x $reg_infty_m) !~ /a{$reg_infty_m}/) || $@;
261 print "not " if eval q(('a' x ($reg_infty_m - 1)) =~ /a{$reg_infty_m}/) || $@;
265 eval "'aaa' =~ /a{1,$reg_infty}/";
266 print "not " if $@ !~ m%^\Q/a{1,$reg_infty}/: Quantifier in {,} bigger than%;
269 eval "'aaa' =~ /a{1,$reg_infty_p}/";
271 if $@ !~ m%^\Q/a{1,$reg_infty_p}/: Quantifier in {,} bigger than%;
275 # Poke a couple more parse failures
277 $context = 'x' x 256;
278 eval qq("${context}y" =~ /(?<=$context)y/);
279 print "not " if $@ !~ m%^\Q/(?<=\Ex+/: lookbehind longer than 255 not%;
282 # This one will fail when POSIX character classes do get implemented
286 local $SIG{__WARN__} = sub{$w = shift};
287 eval q('a' =~ /[[:alpha:]]/);
288 print "not " if $w !~ /^\QCharacter class syntax [: :] is reserved/;
294 for $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
296 print "# length=$l\nnot " unless "ba$a=" =~ /a$a=/;
300 print "not " if "b$a=" =~ /a$a=/;
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
319 print "# const-len `$_' not => $ans{$_}\nnot "
320 if $ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o;
323 print "# var-len `$_' not => $ans{$_}\nnot "
324 if $ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o;
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)";
336 (?{ $c = 1 }) # Initialize
338 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
340 ) # Fail: will unwind one iteration back
343 [^()]+ # Match a big chunk
346 ) # Do not try to match subchunks
354 )+ # This may not match with different subblocks
359 ) # Otherwise the chunk 1 may succeed with $c>0
363 push @ans, $res while $res = matchit;
365 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne "1 1 1";
371 print "# ans='@ans'\n# expect='$expect'\nnot " if "@ans" ne $expect;
375 @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
376 print "not " if "@ans" ne 'a/ b';
380 $code = '{$blah = 45}';
383 print "not " if $blah != 45;
389 print "not " if $blah != 45;
395 print "not " unless pos($x) == 2;
400 print "not " unless pos($x) == 2;
410 print "not " unless f(pos($x)) == 4;
415 'foot' =~ /foo(?{$x = 12; 75})[t]/;
416 print "not " unless $^R eq '75';
421 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
422 print "not " unless $^R eq '67' and $x eq '12';
427 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
428 print "not " unless $^R eq '79' and $x eq '12';
432 # This should be changed to qr/\b\v$/ ASAP
433 print "not " unless study(/\b\v$/) eq '\bv$';
438 foreach $ans ('', 'c') {
439 /(?<=(?=a)..)((?=c)|.)/g;
440 print "not " unless $1 eq $ans;
446 foreach $ans ('', 'a', '') {
448 print "not " unless $& eq $ans;
454 my $warn_pat = shift;
455 return sub { print "not " unless $_[0] =~ /$warn_pat/ }
459 my ($warn_pat, $code) = @_;
460 local $^W; local %SIG;
461 eval 'BEGIN { $^W = 1; $SIG{__WARN__} = $warn_pat };' . $code;
468 my $warn_pat = shift;
469 return sub { must_warn(must_warn_pat($warn_pat)) }
472 my $for_future = make_must_warn('reserved for future extensions');
474 &$for_future('q(a:[b]:) =~ /[x[:foo:]]/');
475 &$for_future('q(a=[b]=) =~ /[x[=foo=]]/');
476 &$for_future('q(a.[b].) =~ /[x[.foo.]]/');