summaryrefslogtreecommitdiff
path: root/t/re/opt.t
blob: cee0601fcf2bd61f6b33a09b690b447354092dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!./perl
#
# ex: set ts=8 sts=4 sw=4 et:
#
# Here we test for optimizations in the regexp engine.
# We try to distinguish between "nice to have" optimizations and those
# we consider essential: failure of the latter should be considered bugs,
# while failure of the former should at worst be TODO.
#
# Format of data lines is tab-separated: pattern, minlen, anchored, floating,
# other-options, comment.
# - pattern will be subject to string eval as "qr{$pattern}".
# - minlen is a non-negative integer.
# - anchored/floating are of the form "u23:45+string". If initial "u" is
#   present we expect a utf8 substring, else a byte substring; subsequent
#   digits are the min offset; optional /:\d+/ is the max offset (not
#   supported for anchored; assumed undef if not present for floating);
#   subsequent '-' or '+' indicates if this is the substring being checked;
#   "string" is the substring to expect. Use "-" for the whole entry to
#   indicate no substring of this type.
# - other-options is a comma-separated list of bare flags or option=value
#   strings. Those with an initial "T" mark the corresponding test TODO.
#   Booleans (noscan, isall, skip, implicit, anchor SBOL, anchor MBOL,
#   anchor GPOS) are expected false if not mentioned, expected true if
#   supplied as bare flags. stclass may be supplied as a pattern match
#   as eg "stclass=~^ANYOF".
# - as a special-case, minlenret is expected to be the same as minlen
#   unless specified in other-options.
#

use strict;
use warnings;
use 5.010;

$| = 1;

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
    skip_all_if_miniperl("no dynamic loading on miniperl, no re::optimization");
}

no warnings qw{ experimental };
use feature qw{ refaliasing declared_refs };
our \$TODO = \$::TODO;

use re ();

while (<DATA>) {
    chomp;
    if (m{^\s*(?:#|\z)}) {
        # skip blank/comment lines
        next;
    }
    my($pat, $minlen, $anchored, $floating, $other, $comment) = split /\t/;
    my %todo;
    my %opt = map {
        my($k, $v) = split /=/, $_, 2;
        ($k =~ s/^T//) ? do { $todo{$k} = $v; () } : ($k => $v);
    } split /,/, $other // '';
    $comment = (defined $comment && length $comment)
        ? "$pat ($comment):"
        : "$pat:";

    my $o = re::optimization(eval "qr{$pat}");
    ok($o, "$comment compiled ok");

    my $skip = $o ? undef : "could not get info for qr{$pat}";
    my $test = 0;

    my($got, $expect) = ($o->{minlen}, $minlen);
    if (exists $todo{minlen}) {
        ++$test;
        $skip || ok($got >= $expect, "$comment minlen $got >= $expect");
        my $todo = $todo{minlen};
        local $TODO = 1;
        $skip || is($got, $todo, "$comment minlen $got = $todo");
    } else {
        ++$test;
        $skip || is($got, $expect, "$comment minlen $got = $expect");
    }

    ($got, $expect) = ($o->{minlenret}, $opt{minlenret} // $minlen);
    if (exists $todo{minlenret}) {
        ++$test;
        $skip || ok($got >= $expect, "$comment minlenret $got >= $expect");
        my $todo = $todo{minlenret};
        local $TODO = 1;
        $skip || is($got, $todo, "$comment minlenret $got = $todo");
    } else {
        ++$test;
        $skip || is($got, $expect, "$comment minlenret $got = $expect");
    }

    my($autf, $aoff, $acheck, $astr) = ($anchored =~ m{
        ^ (u?) (\d*) ([-+]) (.*) \z
    }sx) or die "Can't parse anchored test '$anchored'";
    if ($autf eq 'u') {
        ++$test;
        $skip || is($o->{anchored}, undef, "$comment no anchored");
        ++$test;
        local $TODO = 1 if exists $todo{'anchored utf8'};
        $skip || is($o->{'anchored utf8'}, $astr, "$comment got anchored utf8");
    } elsif (length $astr) {
        ++$test;
        $skip || is($o->{anchored_utf8}, undef, "$comment no anchored utf8");
        ++$test;
        local $TODO = 1 if exists $todo{anchored};
        $skip || is($o->{anchored}, $astr, "$comment got anchored");
    } else {
        ++$test;
        $skip || is($o->{anchored}, undef, "$comment no anchored");
        ++$test;
        $skip || is($o->{anchored_utf8}, undef, "$comment no anchored utf8");
    }
    # skip offset checks if we failed to find a string
    my $local_skip = (
        !$skip && !defined($o->{anchored} // $o->{anchored_utf8})
    ) ? 'no anchored string' : undef;
    if (length $aoff) {
        ++$test;
        SKIP: {
            skip($local_skip) if $local_skip;
            local $TODO = 1 if exists $todo{'anchored min offset'};
            $skip || is($o->{'anchored min offset'}, $aoff,
                    "$comment anchored min offset");
        }
        # we don't care about anchored max: it may be set same as min or 0
    }

    my($futf, $fmin, $fmax, $fcheck, $fstr) = ($floating =~ m{
        ^ (u?) (\d*) (?: : (\d*) )? ([-+]) (.*) \z
    }sx) or die "Can't parse floating test '$floating'";
    if ($futf eq 'u') {
        ++$test;
        $skip || is($o->{floating}, undef, "$comment no floating");
        ++$test;
        local $TODO = 1 if exists $todo{'floating utf8'};
        $skip || is($o->{'floating utf8'}, $fstr, "$comment got floating utf8");
    } elsif (length $fstr) {
        ++$test;
        $skip || is($o->{floating_utf8}, undef, "$comment no floating utf8");
        ++$test;
        local $TODO = 1 if exists $todo{floating};
        $skip || is($o->{floating}, $fstr, "$comment got floating");
    } else {
        ++$test;
        $skip || is($o->{floating}, undef, "$comment no floating");
        ++$test;
        $skip || is($o->{floating_utf8}, undef, "$comment no floating utf8");
    }
    # skip offset checks if we failed to find a string
    $local_skip = (
        !$skip && !defined($o->{floating} // $o->{floating_utf8})
    ) ? 'no floating string' : undef;
    if (length $fmin) {
        ++$test;
        SKIP: {
            skip($local_skip) if $local_skip;
            local $TODO = 1 if exists $todo{'floating min offset'};
            $skip || is($o->{'floating min offset'}, $fmin,
                    "$comment floating min offset");
        }
    }
    if (defined $fmax) {
        ++$test;
        SKIP: {
            skip($local_skip) if $local_skip;
            local $TODO = 1 if exists $todo{'floating max offset'};
            $skip || is($o->{'floating max offset'}, $fmax,
                    "$comment floating max offset");
        }
    }

    my $check = ($acheck eq '+') ? 'anchored'
            : ($fcheck eq '+') ? 'floating'
            : ($acheck eq '-') ? undef
            : 'none';
    $local_skip = (
        !$skip && $check && (
            ($check eq 'anchored'
                    && !defined($o->{anchored} // $o->{anchored_utf8}))
            || ($check eq 'floating'
                    && !defined($o->{floating} // $o->{floating_utf8}))
        )
    ) ? "$check not found" : undef;
    if (defined $check) {
        ++$test;
        SKIP: {
            skip($local_skip) if $local_skip;
            local $TODO = 1 if exists $todo{checking};
            $skip || is($o->{checking}, $check, "$comment checking $check");
        }
    }

    # booleans
    for (qw{ noscan isall skip implicit },
        'anchor SBOL', 'anchor MBOL', 'anchor GPOS'
    ) {
        my $got = $o->{$_};
        my $expect = exists($opt{$_}) ? ($opt{$_} // 1) : 0;
        ++$test;
        local $TODO = 1 if exists $todo{"T$_"};
        $skip || is($got, $expect ? 1 : 0, "$comment $_");
    }

    # integer
    for (qw{ gofs }) {
        my $got = $o->{$_};
        my $expect = $opt{$_} // 0;
        ++$test;
        local $TODO = 1 if exists $todo{"T$_"};
        $skip || is($got, $expect || 0, "$comment $_");
    }

    # string
    for (qw{ stclass }) {
        my $got = $o->{$_};
        my $expect = $opt{$_};
        my $qr = (defined($expect) && ($expect =~ s{^~}{})) ? 1 : 0;
        ++$test;
        local $TODO = 1 if exists $todo{"T$_"};
        $skip || ($qr
            ? like($got, qr{$expect}, "$comment $_")
            : is($got, $expect, "$comment $_")
        );
    }

    skip($skip, $test) if $skip;
}
done_testing();
__END__
(?:)	0	-	-	Tisall

# various forms of anchored substring
abc	3	0+abc	-	isall
.{10}abc	13	10+abc	-	-
(?i:)abc	3	0+abc	-	isall
a(?:)bc	3	0+abc	-	isall
a()bc	3	0+abc	-	-
a(?i:)bc	3	0+abc	-	isall
a(b)c	3	0+abc	-	-
a((?i:b))c	3	0+abc	-	Tanchored
a[bB]c	3	0+abc	-	Tanchored
(?=abc)	0	0+abc	-	Tanchored,Tminlen=3,minlenret=0
abc|abc	3	0+abc	-	isall
abcd|abce	4	0+abc	-	-
acde|bcde	4	1+cde	-	Tanchored,stclass=~[ab]
acdef|bcdeg	5	1+cde	-	Tanchored,stclass=~[ab]

# same as above, floating
.?abc	3	-	0:1+abc	-
.?.{10}abc	13	-	10:11+abc	-
.?(?i:)abc	3	-	0:1+abc	-
.?a(?:)bc	3	-	0:1+abc	-
.?a()bc	3	-	0:1+abc	-
.?a(?i:)bc	3	-	0:1+abc	-
.?a(b)c	3	-	0+abc	-
.?a((?i:b))c	3	-	0+abc	Tfloating
.?a[bB]c	3	-	0:1+abc	Tfloating
.?(?=abc)	0	-	0:1+abc	Tfloating,Tminlen=3,minlenret=0
.?(?:abc|abc)	3	-	0:1+abc	-
.?(?:abcd|abce)	4	-	0:1+abc	-
.?(?:acde|bcde)	4	-	1:2+cde	Tfloating
.?(?:acdef|bcdeg)	5	-	1:2+cde	Tfloating

a(b){2,3}c	4	-abb	1+bbc
a(b|bb)c	3	-ab	1-bc	Tfloating,Tfloating min offset
a(b|bb){2}c	4	-abb	1-bbc	Tanchored,Tfloating,Tfloating min offset

abc(*COMMIT)xyz	6	0+abc	-	-
abc(*ACCEPT)xyz	3	0+abc	-	-
# Must not have stclass=[x]
(*ACCEPT)xyz	0	-	-	-
(a(*ACCEPT)){2}	1	0+a	-	-