This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
applied patch, tweak for threads awareness
[perl5.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 # \n in the tests are 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     chomp;
44     s/\\n/\n/g;
45     ($pat, $subject, $result, $repl, $expect) = split(/\t/,$_);
46     $input = join(':',$pat,$subject,$result,$repl,$expect);
47     infty_subst(\$pat);
48     infty_subst(\$expect);
49     $pat = "'$pat'" unless $pat =~ /^[:']/;
50     $pat =~ s/\\n/\n/g;
51     $subject =~ s/\\n/\n/g;
52     $expect =~ s/\\n/\n/g;
53     $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
54     for $study ("", "study \$subject") {
55         $c = $iters;
56         eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
57         chomp( $err = $@ );
58         if ($result eq 'c') {
59             if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
60             last;  # no need to study a syntax error
61         }
62         elsif ($@) {
63             print "not ok $. $input => error `$err'\n"; next TEST;
64         }
65         elsif ($result eq 'n') {
66             if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
67         }
68         else {
69             if (!$match || $got ne $expect) {
70                 print "not ok $. ($study) $input => `$got', match=$match\n";
71                 next TEST;
72             }
73         }
74     }
75     print "ok $.\n";
76 }
77
78 close(TESTS);
79
80 sub infty_subst                             # Special-case substitution
81 {                                           #  of $reg_infty and friends
82     my $tp = shift;
83     $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
84     $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
85     $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
86 }