[patch: perl@8211]VMS: add -Duseperlio capacity to configure.com
[p5sagit/p5-mst-13.2.git] / t / op / regexp.t
CommitLineData
378cc40b 1#!./perl
2
9c63abab 3# XXX known to leak scalars
4$ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
eec2d3df 5
ad4f75a6 6# The tests are in a separate file 't/op/re_tests'.
7# Each line in that file is a separate test.
8# There are five columns, separated by tabs.
9#
10# Column 1 contains the pattern, optionally enclosed in C<''>.
11# Modifiers can be put after the closing C<'>.
12#
13# Column 2 contains the string to be matched.
14#
15# Column 3 contains the expected result:
16# y expect a match
17# n expect no match
18# c expect an error
cf93c79d 19# B test exposes a known bug in Perl, should be skipped
20# b test exposes a known bug in Perl, should be skipped if noamp
ad4f75a6 21#
1b1626e4 22# Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
ad4f75a6 23#
24# Column 4 contains a string, usually C<$&>.
25#
26# Column 5 contains the expected result of double-quote
c277df42 27# interpolating that string after the match, or start of error message.
28#
ee595aa6 29# Column 6, if present, contains a reason why the test is skipped.
30# This is printed with "skipped", for harness to pick up.
31#
9d116dd7 32# \n in the tests are interpolated, as are variables of the form ${\w+}.
83e898de 33#
8d37f932 34# If you want to add a regular expression test that can't be expressed
35# in this format, don't add it here: put it in op/pat.t instead.
c277df42 36
e4d48cc9 37BEGIN {
38 chdir 't' if -d 't';
20822f61 39 @INC = '../lib';
e4d48cc9 40}
41
c277df42 42$iters = shift || 1; # Poor man performance suite, 10000 is OK.
ad4f75a6 43
8d37f932 44open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') ||
45 die "Can't open re_tests";
cfa4f241 46
378cc40b 47while (<TESTS>) { }
48$numtests = $.;
cfa4f241 49seek(TESTS,0,0);
50$. = 0;
378cc40b 51
9d116dd7 52$bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
b8c5462f 53$ffff = chr(0xff) x 2;
54$nulnul = "\0" x 2;
9d116dd7 55
1462b684 56$| = 1;
c277df42 57print "1..$numtests\n# $iters iterations\n";
cfa4f241 58TEST:
378cc40b 59while (<TESTS>) {
b85d18e9 60 chomp;
61 s/\\n/\n/g;
ee595aa6 62 ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
378cc40b 63 $input = join(':',$pat,$subject,$result,$repl,$expect);
83e898de 64 infty_subst(\$pat);
65 infty_subst(\$expect);
1b1626e4 66 $pat = "'$pat'" unless $pat =~ /^[:']/;
9d116dd7 67 $pat =~ s/(\$\{\w+\})/$1/eeg;
b8c5462f 68 $pat =~ s/\\n/\n/g;
69 $subject =~ s/(\$\{\w+\})/$1/eeg;
c277df42 70 $subject =~ s/\\n/\n/g;
b8c5462f 71 $expect =~ s/(\$\{\w+\})/$1/eeg;
c277df42 72 $expect =~ s/\\n/\n/g;
73 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
cf93c79d 74 $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
b8c5462f 75 # Certain tests don't work with utf8 (the re_test should be in UTF8)
ee595aa6 76 $skip = 1, $reason = 'utf8'
78109b9e 77 if ($^H &= ~0x00000008) && $pat =~ /\[:\^(alnum|print|word|ascii|xdigit):\]/;
cf93c79d 78 $result =~ s/B//i unless $skip;
79 for $study ('', 'study \$subject') {
c277df42 80 $c = $iters;
81 eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
82 chomp( $err = $@ );
cfa4f241 83 if ($result eq 'c') {
c277df42 84 if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
cfa4f241 85 last; # no need to study a syntax error
86 }
cf93c79d 87 elsif ( $skip ) {
ee595aa6 88 print "ok $. # skipped", length($reason) ? " $reason" : '', "\n";
89 next TEST;
cf93c79d 90 }
c277df42 91 elsif ($@) {
92 print "not ok $. $input => error `$err'\n"; next TEST;
93 }
cfa4f241 94 elsif ($result eq 'n') {
c277df42 95 if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
378cc40b 96 }
97 else {
cfa4f241 98 if (!$match || $got ne $expect) {
c277df42 99 print "not ok $. ($study) $input => `$got', match=$match\n";
cfa4f241 100 next TEST;
101 }
378cc40b 102 }
103 }
cfa4f241 104 print "ok $.\n";
378cc40b 105}
cfa4f241 106
378cc40b 107close(TESTS);
83e898de 108
109sub infty_subst # Special-case substitution
110{ # of $reg_infty and friends
111 my $tp = shift;
112 $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
113 $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
114 $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
115}