Change sprintf() to my_sprintf(), and use the returned length from
[p5sagit/p5-mst-13.2.git] / t / op / regexp.t
CommitLineData
378cc40b 1#!./perl
2
ad4f75a6 3# The tests are in a separate file 't/op/re_tests'.
4# Each line in that file is a separate test.
5# There are five columns, separated by tabs.
6#
7# Column 1 contains the pattern, optionally enclosed in C<''>.
8# Modifiers can be put after the closing C<'>.
9#
10# Column 2 contains the string to be matched.
11#
12# Column 3 contains the expected result:
13# y expect a match
14# n expect no match
15# c expect an error
cf93c79d 16# B test exposes a known bug in Perl, should be skipped
17# b test exposes a known bug in Perl, should be skipped if noamp
ad4f75a6 18#
1b1626e4 19# Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
ad4f75a6 20#
21# Column 4 contains a string, usually C<$&>.
22#
23# Column 5 contains the expected result of double-quote
c277df42 24# interpolating that string after the match, or start of error message.
25#
ee595aa6 26# Column 6, if present, contains a reason why the test is skipped.
27# This is printed with "skipped", for harness to pick up.
28#
9d116dd7 29# \n in the tests are interpolated, as are variables of the form ${\w+}.
83e898de 30#
8d37f932 31# If you want to add a regular expression test that can't be expressed
32# in this format, don't add it here: put it in op/pat.t instead.
b2a156bd 33#
34# Note that columns 2,3 and 5 are all enclosed in double quotes and then
35# evalled; so something like a\"\x{100}$1 has length 3+length($1).
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
95e8664e 44open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') || open(TESTS,':op:re_tests') ||
8d37f932 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;
7fba1cd6 55$OP = $qr ? 'qr' : 'm';
9d116dd7 56
1462b684 57$| = 1;
c277df42 58print "1..$numtests\n# $iters iterations\n";
cfa4f241 59TEST:
378cc40b 60while (<TESTS>) {
b85d18e9 61 chomp;
62 s/\\n/\n/g;
ee595aa6 63 ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
378cc40b 64 $input = join(':',$pat,$subject,$result,$repl,$expect);
83e898de 65 infty_subst(\$pat);
66 infty_subst(\$expect);
1b1626e4 67 $pat = "'$pat'" unless $pat =~ /^[:']/;
9d116dd7 68 $pat =~ s/(\$\{\w+\})/$1/eeg;
b8c5462f 69 $pat =~ s/\\n/\n/g;
b2a156bd 70 $subject = eval qq("$subject");
71 $expect = eval qq("$expect");
c277df42 72 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
cf93c79d 73 $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
906e884f 74 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
cf93c79d 75 $result =~ s/B//i unless $skip;
1de06328 76
77 for $study ('', 'study $subject') {
c277df42 78 $c = $iters;
1de06328 79 if ($repl eq 'pos') {
80 $code= <<EOFCODE;
81 $study;
82 pos(\$subject)=0;
83 \$match = ( \$subject =~ m${pat}g );
84 \$got = pos(\$subject);
85EOFCODE
86 }
87 elsif ($qr_embed) {
88 $code= <<EOFCODE;
89 my \$RE = qr$pat;
90 $study;
91 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
92 \$got = "$repl";
93EOFCODE
94 }
95 else {
96 $code= <<EOFCODE;
97 $study;
98 \$match = (\$subject =~ $OP$pat$addg) while \$c--;
99 \$got = "$repl";
100EOFCODE
101 }
102 eval $code;
c277df42 103 chomp( $err = $@ );
cfa4f241 104 if ($result eq 'c') {
c277df42 105 if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
cfa4f241 106 last; # no need to study a syntax error
107 }
cf93c79d 108 elsif ( $skip ) {
ee595aa6 109 print "ok $. # skipped", length($reason) ? " $reason" : '', "\n";
110 next TEST;
cf93c79d 111 }
c277df42 112 elsif ($@) {
1de06328 113 print "not ok $. $input => error `$err'\n$code\n$@\n"; next TEST;
c277df42 114 }
cfa4f241 115 elsif ($result eq 'n') {
c277df42 116 if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
378cc40b 117 }
118 else {
cfa4f241 119 if (!$match || $got ne $expect) {
1de06328 120 print "not ok $. ($study) $input => `$got', match=$match\n$code\n";
cfa4f241 121 next TEST;
122 }
378cc40b 123 }
124 }
cfa4f241 125 print "ok $.\n";
378cc40b 126}
cfa4f241 127
378cc40b 128close(TESTS);
83e898de 129
130sub infty_subst # Special-case substitution
131{ # of $reg_infty and friends
132 my $tp = shift;
133 $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
134 $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
135 $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
136}