: Move REG_INFTY-dependent tests from op/regexp.t
[p5sagit/p5-mst-13.2.git] / t / op / regexp.t
1 #!./perl
2
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
16 #
17 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
18 #
19 # Column 4 contains a string, usually C<$&>.
20 #
21 # Column 5 contains the expected result of double-quote
22 # interpolating that string after the match, or start of error message.
23 #
24 # Columns 1, 2 and 5 are \n-interpolated.
25 #
26 # If you want to add a regular expression test that can't be expressed
27 # in this format, don't add it here: put it in op/pat.t instead.
28
29 $iters = shift || 1;            # Poor man performance suite, 10000 is OK.
30
31 open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') ||
32         die "Can't open re_tests";
33
34 while (<TESTS>) { }
35 $numtests = $.;
36 seek(TESTS,0,0);
37 $. = 0;
38
39 $| = 1;
40 print "1..$numtests\n# $iters iterations\n";
41 TEST:
42 while (<TESTS>) {
43     ($pat, $subject, $result, $repl, $expect) = split(/[\t\n]/,$_);
44     $input = join(':',$pat,$subject,$result,$repl,$expect);
45     infty_subst(\$pat);
46     infty_subst(\$expect);
47     $pat = "'$pat'" unless $pat =~ /^[:']/;
48     $pat =~ s/\\n/\n/g;
49     $subject =~ s/\\n/\n/g;
50     $expect =~ s/\\n/\n/g;
51     $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
52     for $study ("", "study \$subject") {
53         $c = $iters;
54         eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
55         chomp( $err = $@ );
56         if ($result eq 'c') {
57             if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
58             last;  # no need to study a syntax error
59         }
60         elsif ($@) {
61             print "not ok $. $input => error `$err'\n"; next TEST;
62         }
63         elsif ($result eq 'n') {
64             if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
65         }
66         else {
67             if (!$match || $got ne $expect) {
68                 print "not ok $. ($study) $input => `$got', match=$match\n";
69                 next TEST;
70             }
71         }
72     }
73     print "ok $.\n";
74 }
75
76 close(TESTS);
77
78 sub infty_subst                             # Special-case substitution
79 {                                           #  of $reg_infty and friends
80     my $tp = shift;
81     $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
82     $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
83     $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
84 }