| 1 |
import sys |
| 2 |
import py |
| 3 |
from rpython.jit.metainterp.test import support |
| 4 |
from rpython.rlib.rsre.test.test_match import get_code |
| 5 |
from rpython.rlib.rsre import rsre_core |
| 6 |
from rpython.rtyper.lltypesystem import lltype |
| 7 |
from rpython.rtyper.annlowlevel import llstr, hlstr |
| 8 |
|
| 9 |
def entrypoint1(r, string, repeat): |
| 10 |
r = rsre_core.CompiledPattern(array2list(r), 0) |
| 11 |
string = hlstr(string) |
| 12 |
match = None |
| 13 |
for i in range(repeat): |
| 14 |
match = rsre_core.match(r, string) |
| 15 |
if match is None: |
| 16 |
return -1 |
| 17 |
if match is None: |
| 18 |
return -1 |
| 19 |
else: |
| 20 |
return match.match_end |
| 21 |
|
| 22 |
def entrypoint2(r, string, repeat): |
| 23 |
r = rsre_core.CompiledPattern(array2list(r), 0) |
| 24 |
string = hlstr(string) |
| 25 |
match = None |
| 26 |
for i in range(repeat): |
| 27 |
match = rsre_core.search(r, string) |
| 28 |
if match is None: |
| 29 |
return -1 |
| 30 |
else: |
| 31 |
return match.match_start |
| 32 |
|
| 33 |
def list2array(lst): |
| 34 |
a = lltype.malloc(lltype.GcArray(lltype.Signed), len(lst)) |
| 35 |
for i, x in enumerate(lst): |
| 36 |
a[i] = int(x) |
| 37 |
return a |
| 38 |
|
| 39 |
def array2list(a): |
| 40 |
return [a[i] for i in range(len(a))] |
| 41 |
|
| 42 |
|
| 43 |
def test_jit_unroll_safe(): |
| 44 |
|
| 45 |
assert not hasattr(rsre_core.sre_match, '_jit_unroll_safe_') |
| 46 |
for m in rsre_core.sre_match._specialized_methods_: |
| 47 |
assert m._jit_unroll_safe_ |
| 48 |
|
| 49 |
|
| 50 |
class TestJitRSre(support.LLJitMixin): |
| 51 |
|
| 52 |
def meta_interp_match(self, pattern, string, repeat=1): |
| 53 |
r = get_code(pattern) |
| 54 |
return self.meta_interp_match_raw(r.pattern, string, repeat) |
| 55 |
|
| 56 |
def meta_interp_match_raw(self, compiled_pattern, string, repeat=1): |
| 57 |
return self.meta_interp(entrypoint1, [list2array(compiled_pattern), llstr(string), |
| 58 |
repeat], |
| 59 |
listcomp=True, backendopt=True) |
| 60 |
|
| 61 |
def meta_interp_search(self, pattern, string, repeat=1): |
| 62 |
r = get_code(pattern) |
| 63 |
return self.meta_interp(entrypoint2, [list2array(r.pattern), llstr(string), |
| 64 |
repeat], |
| 65 |
listcomp=True, backendopt=True) |
| 66 |
|
| 67 |
def test_simple_match_1(self): |
| 68 |
res = self.meta_interp_match(r"ab*bbbbbbbc", "abbbbbbbbbcdef") |
| 69 |
assert res == 11 |
| 70 |
|
| 71 |
def test_simple_match_2(self): |
| 72 |
res = self.meta_interp_match(r".*abc", "xxabcyyyyyyyyyyyyy") |
| 73 |
assert res == 5 |
| 74 |
|
| 75 |
def test_simple_match_repeated(self): |
| 76 |
res = self.meta_interp_match(r"abcdef", "abcdef", repeat=10) |
| 77 |
assert res == 6 |
| 78 |
self.check_trace_count(1) |
| 79 |
self.check_jitcell_token_count(1) |
| 80 |
|
| 81 |
def test_match_minrepeat_1(self): |
| 82 |
res = self.meta_interp_match(r".*?abc", "xxxxxxxxxxxxxxabc") |
| 83 |
assert res == 17 |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
def test_branch_1(self): |
| 90 |
res = self.meta_interp_match(r".*?(ab|x)c", "xxxxxxxxxxxxxxabc") |
| 91 |
assert res == 17 |
| 92 |
|
| 93 |
def test_match_minrepeat_2(self): |
| 94 |
s = ("xxxxxxxxxxabbbbbbbbbb" + |
| 95 |
"xxxxxxxxxxabbbbbbbbbb" + |
| 96 |
"xxxxxxxxxxabbbbbbbbbb" + |
| 97 |
"xxxxxxxxxxabbbbbbbbbbc") |
| 98 |
res = self.meta_interp_match(r".*?ab+?c", s) |
| 99 |
assert res == len(s) |
| 100 |
|
| 101 |
|
| 102 |
def test_fast_search(self): |
| 103 |
res = self.meta_interp_search(r"<foo\w+>", "e<f<f<foxd<f<fh<foobar>ua") |
| 104 |
assert res == 15 |
| 105 |
self.check_resops(guard_value=0) |
| 106 |
|
| 107 |
def test_regular_search(self): |
| 108 |
res = self.meta_interp_search(r"<\w+>", "eiofweoxdiwhdoh<foobar>ua") |
| 109 |
assert res == 15 |
| 110 |
|
| 111 |
def test_regular_search_upcase(self): |
| 112 |
res = self.meta_interp_search(r"<\w+>", "EIOFWEOXDIWHDOH<FOOBAR>UA") |
| 113 |
assert res == 15 |
| 114 |
|
| 115 |
def test_max_until_1(self): |
| 116 |
res = self.meta_interp_match(r"(ab)*abababababc", |
| 117 |
"ababababababababababc") |
| 118 |
assert res == 21 |
| 119 |
|
| 120 |
def test_min_until_1(self): |
| 121 |
res = self.meta_interp_match(r"^(?:ab)*?c", |
| 122 |
"ababababababababababc") |
| 123 |
assert res == 21 |
| 124 |
self.check_trace_count(1) |
| 125 |
|
| 126 |
def test_max_until_possessive(self): |
| 127 |
from rpython.rlib.rsre.test.test_ext_opcode import ab_plus_plus_bb |
| 128 |
res = self.meta_interp_match_raw(ab_plus_plus_bb, |
| 129 |
"ababababababababababbb") |
| 130 |
self.check_trace_count(1) |
| 131 |
|
| 132 |
def test_example_1(self): |
| 133 |
res = self.meta_interp_search( |
| 134 |
r"Active\s+20\d\d-\d\d-\d\d\s+[[]\d+[]]([^[]+)", |
| 135 |
"Active"*20 + "Active 2010-04-07 [42] Foobar baz boz blah[43]") |
| 136 |
assert res == 6*20 |
| 137 |
|
| 138 |
def test_aorbstar(self): |
| 139 |
res = self.meta_interp_match("(a|b)*a", "a" * 100) |
| 140 |
assert res == 100 |
| 141 |
self.check_resops(guard_value=0) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
def test_group_range(self): |
| 147 |
res = self.meta_interp_match(r"<[^b-c]+>", "<aeaeaea>") |
| 148 |
assert res == 9 |
| 149 |
self.check_enter_count(1) |
| 150 |
|
| 151 |
def test_group_single_chars(self): |
| 152 |
res = self.meta_interp_match(r"<[ae]+>", "<aeaeaea>") |
| 153 |
assert res == 9 |
| 154 |
self.check_enter_count(1) |
| 155 |
|
| 156 |
def test_group_digit(self): |
| 157 |
res = self.meta_interp_match(r"<[^\d]+>", "<..a..aa>") |
| 158 |
assert res == 9 |
| 159 |
self.check_enter_count(1) |
| 160 |
|
| 161 |
def test_group_space(self): |
| 162 |
res = self.meta_interp_match(r"<\S+>", "<..a..aa>") |
| 163 |
assert res == 9 |
| 164 |
self.check_enter_count(1) |
| 165 |
|
| 166 |
def test_group_word(self): |
| 167 |
res = self.meta_interp_match(r"<\w+>", "<ab09_a1>") |
| 168 |
assert res == 9 |
| 169 |
self.check_enter_count(1) |
| 170 |
|
| 171 |
def test_group_complex(self): |
| 172 |
res = self.meta_interp_match(r"<[a@h\d\s]+>", "<a93919a @ a23>") |
| 173 |
assert res == 15 |
| 174 |
self.check_enter_count(1) |
| 175 |
|
| 176 |
@py.test.mark.xfail |
| 177 |
def test_group_space_but_not_space(self): |
| 178 |
res = self.meta_interp_match(r"<[\S ]+>", "<..a .. aa>") |
| 179 |
assert res == 13 |
| 180 |
self.check_enter_count(1) |
| 181 |
|
| 182 |
|
| 183 |
def test_find_repetition_end_fastpath(self): |
| 184 |
res = self.meta_interp_search(r"b+", "a"*30 + "b") |
| 185 |
assert res == 30 |
| 186 |
self.check_resops(call=0) |
| 187 |
|
| 188 |
@py.test.mark.skipif('sys.maxint <= 2**31 - 1') |
| 189 |
def test_match_jit_bug(self): |
| 190 |
pattern = ".a" * 2500 |
| 191 |
text = "a" * 6000 |
| 192 |
res = self.meta_interp_match(pattern, text, repeat=10) |
| 193 |
assert res != -1 |
| 194 |
|
| 195 |
def test_getlower_branch_free(self): |
| 196 |
pattern = "(?i)a[bx]*c" |
| 197 |
text = "a" + "bBbbB" * 1000 + "c" |
| 198 |
res = self.meta_interp_match(pattern, text) |
| 199 |
self.check_enter_count(1) |
| 200 |
|
| 201 |
def test_set_one_fewer_instruction(self): |
| 202 |
pattern = "a[bBx12358]*c" |
| 203 |
text = "a" + "bB2b2bB1" * 2000 + "c" |
| 204 |
res = self.meta_interp_match(pattern, text) |
| 205 |
self.check_enter_count(1) |
| 206 |
self.check_resops(int_is_true=0) |