74ca639a8c98531324167564b4561bb9b08a1cae
[p5sagit/p5-mst-13.2.git] / t / op / regexp.t
1 #!./perl
2
3 # XXX known to leak scalars
4 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
5
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
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
21 #
22 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
23 #
24 # Column 4 contains a string, usually C<$&>.
25 #
26 # Column 5 contains the expected result of double-quote
27 # interpolating that string after the match, or start of error message.
28 #
29 # \n in the tests are interpolated, as are variables of the form ${\w+}.
30 #
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.
33
34 BEGIN {
35     chdir 't' if -d 't';
36     unshift @INC, '../lib' if -d '../lib';
37 }
38
39 $iters = shift || 1;            # Poor man performance suite, 10000 is OK.
40
41 open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') ||
42         die "Can't open re_tests";
43
44 while (<TESTS>) { }
45 $numtests = $.;
46 seek(TESTS,0,0);
47 $. = 0;
48
49 $bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
50 $ffff  = chr(0xff) x 2;
51 $nulnul = "\0" x 2;
52
53 $| = 1;
54 print "1..$numtests\n# $iters iterations\n";
55 TEST:
56 while (<TESTS>) {
57     chomp;
58     s/\\n/\n/g;
59     ($pat, $subject, $result, $repl, $expect) = split(/\t/,$_);
60     $input = join(':',$pat,$subject,$result,$repl,$expect);
61     infty_subst(\$pat);
62     infty_subst(\$expect);
63     $pat = "'$pat'" unless $pat =~ /^[:']/;
64     $pat =~ s/(\$\{\w+\})/$1/eeg;
65     $pat =~ s/\\n/\n/g;
66     $subject =~ s/(\$\{\w+\})/$1/eeg;
67     $subject =~ s/\\n/\n/g;
68     $expect =~ s/(\$\{\w+\})/$1/eeg;
69     $expect =~ s/\\n/\n/g;
70     $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
71     $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
72     # Certain tests don't work with utf8 (the re_test should be in UTF8)
73     $skip = 1 if ($^H &= ~0x00000008) && $pat =~ /\[:\^(alnum|print|word):\]/;
74     # ebcdic platforms do not do [:ascii:]
75     $skip = 1 if ("\t" ne "\011") && $pat =~ /\[:\^?ascii:\]/;
76     $result =~ s/B//i unless $skip;
77     for $study ('', 'study \$subject') {
78         $c = $iters;
79         eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
80         chomp( $err = $@ );
81         if ($result eq 'c') {
82             if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
83             last;  # no need to study a syntax error
84         }
85         elsif ( $skip ) {
86             print "ok $. # skipped\n"; next TEST;
87         }
88         elsif ($@) {
89             print "not ok $. $input => error `$err'\n"; next TEST;
90         }
91         elsif ($result eq 'n') {
92             if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
93         }
94         else {
95             if (!$match || $got ne $expect) {
96                 print "not ok $. ($study) $input => `$got', match=$match\n";
97                 next TEST;
98             }
99         }
100     }
101     print "ok $.\n";
102 }
103
104 close(TESTS);
105
106 sub infty_subst                             # Special-case substitution
107 {                                           #  of $reg_infty and friends
108     my $tp = shift;
109     $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
110     $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
111     $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
112 }