File: test_zjit.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (176 lines) | stat: -rw-r--r-- 5,644 bytes parent folder | download | duplicates (2)
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
import py
from rpython.jit.metainterp.test import support
from rpython.rlib.rsre.test.test_match import get_code
from rpython.rlib.rsre import rsre_core
from rpython.rtyper.lltypesystem import lltype
from rpython.rtyper.annlowlevel import llstr, hlstr

def entrypoint1(r, string, repeat):
    r = rsre_core.CompiledPattern(array2list(r))
    string = hlstr(string)
    match = None
    for i in range(repeat):
        match = rsre_core.match(r, string)
        if match is None:
            return -1
    if match is None:
        return -1
    else:
        return match.match_end

def entrypoint2(r, string, repeat):
    r = rsre_core.CompiledPattern(array2list(r))
    string = hlstr(string)
    match = None
    for i in range(repeat):
        match = rsre_core.search(r, string)
    if match is None:
        return -1
    else:
        return match.match_start

def list2array(lst):
    a = lltype.malloc(lltype.GcArray(lltype.Signed), len(lst))
    for i, x in enumerate(lst):
        a[i] = int(x)
    return a

def array2list(a):
    return [a[i] for i in range(len(a))]


def test_jit_unroll_safe():
    # test that the decorators are applied in the right order
    assert not hasattr(rsre_core.sre_match, '_jit_unroll_safe_')
    for m in rsre_core.sre_match._specialized_methods_:
        assert m._jit_unroll_safe_


class TestJitRSre(support.LLJitMixin):

    def meta_interp_match(self, pattern, string, repeat=1):
        r = get_code(pattern)
        return self.meta_interp(entrypoint1, [list2array(r.pattern), llstr(string),
                                              repeat],
                                listcomp=True, backendopt=True)

    def meta_interp_search(self, pattern, string, repeat=1):
        r = get_code(pattern)
        return self.meta_interp(entrypoint2, [list2array(r.pattern), llstr(string),
                                              repeat],
                                listcomp=True, backendopt=True)

    def test_simple_match_1(self):
        res = self.meta_interp_match(r"ab*bbbbbbbc", "abbbbbbbbbcdef")
        assert res == 11

    def test_simple_match_2(self):
        res = self.meta_interp_match(r".*abc", "xxabcyyyyyyyyyyyyy")
        assert res == 5

    def test_simple_match_repeated(self):
        res = self.meta_interp_match(r"abcdef", "abcdef", repeat=10)
        assert res == 6
        self.check_trace_count(1)
        self.check_jitcell_token_count(1)

    def test_match_minrepeat_1(self):
        res = self.meta_interp_match(r".*?abc", "xxxxxxxxxxxxxxabc")
        assert res == 17

    #def test_match_maxuntil_1(self):
    #    res = self.meta_interp_match(r"(ab)*c", "ababababababababc")
    #    assert res == 17

    def test_branch_1(self):
        res = self.meta_interp_match(r".*?(ab|x)c", "xxxxxxxxxxxxxxabc")
        assert res == 17

    def test_match_minrepeat_2(self):
        s = ("xxxxxxxxxxabbbbbbbbbb" +
             "xxxxxxxxxxabbbbbbbbbb" +
             "xxxxxxxxxxabbbbbbbbbb" +
             "xxxxxxxxxxabbbbbbbbbbc")
        res = self.meta_interp_match(r".*?ab+?c", s)
        assert res == len(s)


    def test_fast_search(self):
        res = self.meta_interp_search(r"<foo\w+>", "e<f<f<foxd<f<fh<foobar>ua")
        assert res == 15
        self.check_resops(guard_value=0)

    def test_regular_search(self):
        res = self.meta_interp_search(r"<\w+>", "eiofweoxdiwhdoh<foobar>ua")
        assert res == 15

    def test_regular_search_upcase(self):
        res = self.meta_interp_search(r"<\w+>", "EIOFWEOXDIWHDOH<FOOBAR>UA")
        assert res == 15

    def test_max_until_1(self):
        res = self.meta_interp_match(r"(ab)*abababababc",
                                     "ababababababababababc")
        assert res == 21

    def test_example_1(self):
        res = self.meta_interp_search(
            r"Active\s+20\d\d-\d\d-\d\d\s+[[]\d+[]]([^[]+)",
            "Active"*20 + "Active 2010-04-07 [42] Foobar baz boz blah[43]")
        assert res == 6*20

    def test_aorbstar(self):
        res = self.meta_interp_match("(a|b)*a", "a" * 100)
        assert res == 100
        self.check_resops(guard_value=0)


    # group guards tests

    def test_group_range(self):
        res = self.meta_interp_match(r"<[^b-c]+>", "<aeaeaea>")
        assert res == 9
        self.check_enter_count(1)

    def test_group_single_chars(self):
        res = self.meta_interp_match(r"<[ae]+>", "<aeaeaea>")
        assert res == 9
        self.check_enter_count(1)

    def test_group_digit(self):
        res = self.meta_interp_match(r"<[^\d]+>", "<..a..aa>")
        assert res == 9
        self.check_enter_count(1)

    def test_group_space(self):
        res = self.meta_interp_match(r"<\S+>", "<..a..aa>")
        assert res == 9
        self.check_enter_count(1)

    def test_group_word(self):
        res = self.meta_interp_match(r"<\w+>", "<ab09_a1>")
        assert res == 9
        self.check_enter_count(1)

    def test_group_complex(self):
        res = self.meta_interp_match(r"<[a@h\d\s]+>", "<a93919a @ a23>")
        assert res == 15
        self.check_enter_count(1)

    @py.test.mark.xfail
    def test_group_space_but_not_space(self):
        res = self.meta_interp_match(r"<[\S ]+>", "<..a   .. aa>")
        assert res == 13
        self.check_enter_count(1)


    def test_find_repetition_end_fastpath(self):
        res = self.meta_interp_search(r"b+", "a"*30 + "b")
        assert res == 30
        self.check_resops(call=0)

    def test_match_jit_bug(self):
        pattern = ".a" * 2500
        text = "a" * 6000
        res = self.meta_interp_match(pattern, text, repeat=10)
        assert res != -1