Debian Codegraph @

rpython/rlib/test/test_rstring.py

raw symbols SCIP
1 import sys, py
2
3 from rpython.rlib.rstring import StringBuilder, UnicodeBuilder, split, rsplit
4 from rpython.rlib.rstring import replace, startswith, endswith, replace_count
5 from rpython.rlib.rstring import find, rfind, count, _search, SEARCH_COUNT, SEARCH_FIND
6 from rpython.rlib.buffer import StringBuffer
7 from rpython.rtyper.test.tool import BaseRtypingTest
8
9 from hypothesis import given, strategies as st, assume
10
11 def test_split():
12 def check_split(value, sub, *args, **kwargs):
13 result = kwargs['res']
14 assert split(value, sub, *args) == result
15
16 list_result = [list(i) for i in result]
17 assert split(list(value), sub, *args) == list_result
18
19 assert split(buffer(value), sub, *args) == result
20
21 check_split("", 'x', res=[''])
22 check_split("a", "a", 1, res=['', ''])
23 check_split(" ", " ", 1, res=['', ''])
24 check_split("aa", "a", 2, res=['', '', ''])
25 check_split('a|b|c|d', '|', res=['a', 'b', 'c', 'd'])
26 check_split('a|b|c|d', '|', 2, res=['a', 'b', 'c|d'])
27 check_split('a//b//c//d', '//', res=['a', 'b', 'c', 'd'])
28 check_split('a//b//c//d', '//', 2, res=['a', 'b', 'c//d'])
29 check_split('endcase test', 'test', res=['endcase ', ''])
30 py.test.raises(ValueError, split, 'abc', '')
31
32 def test_split_no_occurrence():
33 x = "abc"
34 assert x.split("d")[0] is x
35 assert x.rsplit("d")[0] is x
36
37 def test_split_None():
38 assert split("") == []
39 assert split(' a\ta\na b') == ['a', 'a', 'a', 'b']
40 assert split(" a a ", maxsplit=1) == ['a', 'a ']
41
42 def test_split_unicode():
43 assert split(u"", u'x') == [u'']
44 assert split(u"a", u"a", 1) == [u'', u'']
45 assert split(u" ", u" ", 1) == [u'', u'']
46 assert split(u"aa", u"a", 2) == [u'', u'', u'']
47 assert split(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
48 assert split(u'a|b|c|d', u'|', 2) == [u'a', u'b', u'c|d']
49 assert split(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
50 assert split(u'endcase test', u'test') == [u'endcase ', u'']
51 py.test.raises(ValueError, split, u'abc', u'')
52
53 def test_split_utf8():
54 assert split('', 'a', isutf8=1) == ['']
55 assert split('baba', 'a', isutf8=1) == ['b', 'b', '']
56 assert split('b b', isutf8=1) == ['b', 'b']
57 assert split('b\xe1\x9a\x80b', isutf8=1) == ['b', 'b']
58
59 def test_rsplit():
60 def check_rsplit(value, sub, *args, **kwargs):
61 result = kwargs['res']
62 assert rsplit(value, sub, *args) == result
63
64 list_result = [list(i) for i in result]
65 assert rsplit(list(value), sub, *args) == list_result
66
67 assert rsplit(buffer(value), sub, *args) == result
68
69 check_rsplit("a", "a", 1, res=['', ''])
70 check_rsplit(" ", " ", 1, res=['', ''])
71 check_rsplit("aa", "a", 2, res=['', '', ''])
72 check_rsplit('a|b|c|d', '|', res=['a', 'b', 'c', 'd'])
73 check_rsplit('a|b|c|d', '|', 2, res=['a|b', 'c', 'd'])
74 check_rsplit('a//b//c//d', '//', res=['a', 'b', 'c', 'd'])
75 check_rsplit('endcase test', 'test', res=['endcase ', ''])
76 py.test.raises(ValueError, rsplit, "abc", '')
77
78 def test_rsplit_None():
79 assert rsplit("") == []
80 assert rsplit(' a\ta\na b') == ['a', 'a', 'a', 'b']
81 assert rsplit(" a a ", maxsplit=1) == [' a', 'a']
82
83 def test_rsplit_unicode():
84 assert rsplit(u"a", u"a", 1) == [u'', u'']
85 assert rsplit(u" ", u" ", 1) == [u'', u'']
86 assert rsplit(u"aa", u"a", 2) == [u'', u'', u'']
87 assert rsplit(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
88 assert rsplit(u'a|b|c|d', u'|', 2) == [u'a|b', u'c', u'd']
89 assert rsplit(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
90 assert rsplit(u'endcase test', u'test') == [u'endcase ', u'']
91 py.test.raises(ValueError, rsplit, u"abc", u'')
92
93 def test_rsplit_utf8():
94 assert rsplit('', 'a', isutf8=1) == ['']
95 assert rsplit('baba', 'a', isutf8=1) == ['b', 'b', '']
96 assert rsplit('b b', isutf8=1) == ['b', 'b']
97 assert rsplit('b\xe1\x9a\x80b', isutf8=1) == ['b', 'b']
98 assert rsplit('b\xe1\x9a\x80', isutf8=1) == ['b']
99
100 def test_string_replace():
101 def check_replace(value, sub, *args, **kwargs):
102 result = kwargs['res']
103 assert replace(value, sub, *args) == result
104 assert replace(list(value), sub, *args) == list(result)
105 count = value.count(sub)
106 if len(args) >= 2:
107 count = min(count, args[1])
108 assert replace_count(value, sub, *args) == (result, count)
109 assert replace_count(value, sub, *args, isutf8=True) == (result, count)
110
111 check_replace('one!two!three!', '!', '@', 1, res='one@two!three!')
112 check_replace('one!two!three!', '!', '', res='onetwothree')
113 check_replace('one!two!three!', '!', '@', 2, res='one@two@three!')
114 check_replace('one!two!three!', '!', '@', 3, res='one@two@three@')
115 check_replace('one!two!three!', '!', '@', 4, res='one@two@three@')
116 check_replace('one!two!three!', '!', '@', 0, res='one!two!three!')
117 check_replace('one!two!three!', '!', '@', res='one@two@three@')
118 check_replace('one!two!three!', 'x', '@', res='one!two!three!')
119 check_replace('one!two!three!', 'x', '@', 2, res='one!two!three!')
120 check_replace('abc', '', '-', res='-a-b-c-')
121 check_replace('abc', '', '-', 3, res='-a-b-c')
122 check_replace('abc', '', '-', 0, res='abc')
123 check_replace('', '', '', res='')
124 check_replace('', '', 'a', res='a')
125 check_replace('abc', 'ab', '--', 0, res='abc')
126 check_replace('abc', 'xy', '--', res='abc')
127 check_replace('123', '123', '', res='')
128 check_replace('123123', '123', '', res='')
129 check_replace('123x123', '123', '', res='x')
130
131 def test_string_replace_overflow():
132 if sys.maxint > 2**31-1:
133 py.test.skip("Wrong platform")
134 s = "a" * (2**16)
135 with py.test.raises(OverflowError):
136 replace(s, "", s)
137 with py.test.raises(OverflowError):
138 replace(s, "a", s)
139 with py.test.raises(OverflowError):
140 replace(s, "a", s, len(s) - 10)
141
142 def test_unicode_replace():
143 assert replace(u'one!two!three!', u'!', u'@', 1) == u'one@two!three!'
144 assert replace(u'one!two!three!', u'!', u'') == u'onetwothree'
145 assert replace(u'one!two!three!', u'!', u'@', 2) == u'one@two@three!'
146 assert replace(u'one!two!three!', u'!', u'@', 3) == u'one@two@three@'
147 assert replace(u'one!two!three!', u'!', u'@', 4) == u'one@two@three@'
148 assert replace(u'one!two!three!', u'!', u'@', 0) == u'one!two!three!'
149 assert replace(u'one!two!three!', u'!', u'@') == u'one@two@three@'
150 assert replace(u'one!two!three!', u'x', u'@') == u'one!two!three!'
151 assert replace(u'one!two!three!', u'x', u'@', 2) == u'one!two!three!'
152 assert replace(u'abc', u'', u'-') == u'-a-b-c-'
153 assert replace(u'abc', u'', u'-', 3) == u'-a-b-c'
154 assert replace(u'abc', u'', u'-', 0) == u'abc'
155 assert replace(u'', u'', u'') == u''
156 assert replace(u'', u'', u'a') == u'a'
157 assert replace(u'abc', u'ab', u'--', 0) == u'abc'
158 assert replace(u'abc', u'xy', u'--') == u'abc'
159 assert replace(u'123', u'123', u'') == u''
160 assert replace(u'123123', u'123', u'') == u''
161 assert replace(u'123x123', u'123', u'') == u'x'
162
163 def test_unicode_replace_overflow():
164 if sys.maxint > 2**31-1:
165 py.test.skip("Wrong platform")
166 s = u"a" * (2**16)
167 with py.test.raises(OverflowError):
168 replace(s, u"", s)
169 with py.test.raises(OverflowError):
170 replace(s, u"a", s)
171 with py.test.raises(OverflowError):
172 replace(s, u"a", s, len(s) - 10)
173
174 def test_replace_no_occurrence():
175 s = "xyz"
176 assert replace(s, "a", "b") is s
177 s = "xyz"
178 assert replace(s, "abc", "b") is s
179
180 def test_startswith():
181 def check_startswith(value, sub, *args, **kwargs):
182 result = kwargs['res']
183 assert startswith(value, sub, *args) is result
184 assert startswith(list(value), sub, *args) is result
185
186 check_startswith('ab', 'ab', res=True)
187 check_startswith('ab', 'a', res=True)
188 check_startswith('ab', '', res=True)
189 check_startswith('x', 'a', res=False)
190 check_startswith('x', 'x', res=True)
191 check_startswith('', '', res=True)
192 check_startswith('', 'a', res=False)
193 check_startswith('x', 'xx', res=False)
194 check_startswith('y', 'xx', res=False)
195 check_startswith('ab', 'a', 0, res=True)
196 check_startswith('ab', 'a', 1, res=False)
197 check_startswith('ab', 'b', 1, res=True)
198 check_startswith('abc', 'bc', 1, 2, res=False)
199 check_startswith('abc', 'c', -1, 4, res=True)
200
201 def test_endswith():
202 def check_endswith(value, sub, *args, **kwargs):
203 result = kwargs['res']
204 assert endswith(value, sub, *args) is result
205 assert endswith(list(value), sub, *args) is result
206
207 check_endswith('ab', 'ab', res=True)
208 check_endswith('ab', 'b', res=True)
209 check_endswith('ab', '', res=True)
210 check_endswith('x', 'a', res=False)
211 check_endswith('x', 'x', res=True)
212 check_endswith('', '', res=True)
213 check_endswith('', 'a', res=False)
214 check_endswith('x', 'xx', res=False)
215 check_endswith('y', 'xx', res=False)
216 check_endswith('abc', 'ab', 0, 2, res=True)
217 check_endswith('abc', 'bc', 1, res=True)
218 check_endswith('abc', 'bc', 2, res=False)
219 check_endswith('abc', 'b', -3, -1, res=True)
220
221 def test_string_builder():
222 s = StringBuilder()
223 s.append("a")
224 s.append("abc")
225 assert s.getlength() == len('aabc')
226 s.append("a")
227 s.append_slice("abc", 1, 2)
228 s.append_multiple_char('d', 4)
229 result = s.build()
230 assert result == "aabcabdddd"
231 assert result == s.build()
232 s.append("x")
233 assert s.build() == result + "x"
234
235 def test_unicode_builder():
236 s = UnicodeBuilder()
237 s.append(u'a')
238 s.append(u'abc')
239 s.append_slice(u'abcdef', 1, 2)
240 assert s.getlength() == len('aabcb')
241 s.append_multiple_char(u'd', 4)
242 result = s.build()
243 assert result == 'aabcbdddd'
244 assert isinstance(result, unicode)
245
246 def test_search():
247 def check_search(func, value, sub, *args, **kwargs):
248 result = kwargs['res']
249 assert func(value, sub, *args) == result
250 assert func(list(value), sub, *args) == result
251
252 check_search(find, 'one two three', 'ne', 0, 13, res=1)
253 check_search(find, 'one two three', 'ne', 5, 13, res=-1)
254 check_search(find, 'one two three', '', 0, 13, res=0)
255
256 check_search(find, '000000p00000000', 'ap', 0, 15, res=-1)
257
258 check_search(rfind, 'one two three', 'e', 0, 13, res=12)
259 check_search(rfind, 'one two three', 'e', 0, 1, res=-1)
260 check_search(rfind, 'one two three', '', 0, 13, res=13)
261
262 check_search(count, 'one two three', 'e', 0, 13, res=3)
263 check_search(count, 'one two three', 'e', 0, 1, res=0)
264 check_search(count, 'one two three', '', 0, 13, res=14)
265
266 check_search(count, '', 'ab', 0, 0, res=0)
267 check_search(count, 'a', 'ab', 0, 1, res=0)
268 check_search(count, 'ac', 'ab', 0, 2, res=0)
269
270
271 class TestTranslates(BaseRtypingTest):
272 def test_split_rsplit(self):
273 def fn():
274 res = True
275 res = res and split('a//b//c//d', '//') == ['a', 'b', 'c', 'd']
276 res = res and split(' a\ta\na b') == ['a', 'a', 'a', 'b']
277 res = res and split('a//b//c//d', '//', 2) == ['a', 'b', 'c//d']
278 res = res and split('abcd,efghi', ',') == ['abcd', 'efghi']
279 res = res and split(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
280 res = res and split(u'endcase test', u'test') == [u'endcase ', u'']
281 res = res and rsplit('a|b|c|d', '|', 2) == ['a|b', 'c', 'd']
282 res = res and rsplit('a//b//c//d', '//') == ['a', 'b', 'c', 'd']
283 res = res and rsplit(u'a|b|c|d', u'|') == [u'a', u'b', u'c', u'd']
284 res = res and rsplit(u'a|b|c|d', u'|', 2) == [u'a|b', u'c', u'd']
285 res = res and rsplit(u'a//b//c//d', u'//') == [u'a', u'b', u'c', u'd']
286 return res
287 res = self.interpret(fn, [])
288 assert res
289
290 def test_buffer_parameter(self):
291 def fn():
292 res = True
293 res = res and find('a//b//c//d', StringBuffer('//'), 0, 10) != -1
294 res = res and rfind('a//b//c//d', StringBuffer('//'), 0, 10) != -1
295 res = res and count('a//b//c//d', StringBuffer('//'), 0, 10) != 0
296 return res
297 res = self.interpret(fn, [])
298 assert res
299
300
301 def test_replace(self):
302 def fn():
303 res = True
304 res = res and replace('abc', 'ab', '--', 0) == 'abc'
305 res = res and replace('abc', 'xy', '--') == 'abc'
306 res = res and replace('abc', 'ab', '--', 0) == 'abc'
307 res = res and replace('abc', 'xy', '--') == 'abc'
308 return res
309 res = self.interpret(fn, [])
310 assert res
311
312 @given(u=st.text(), prefix=st.text(), suffix=st.text())
313 def test_hypothesis_search(u, prefix, suffix):
314 prefix = prefix.encode("utf-8")
315 u = u.encode("utf-8")
316 suffix = suffix.encode("utf-8")
317 s = prefix + u + suffix
318
319 index = _search(s, u, 0, len(s), SEARCH_FIND)
320 assert index == s.find(u)
321 assert 0 <= index <= len(prefix)
322
323 index = _search(s, u, len(prefix), len(s) - len(suffix), SEARCH_FIND)
324 assert index == len(prefix)
325
326 count = _search(s, u, 0, len(s), SEARCH_COUNT)
327 assert count == s.count(u)
328 assert 1 <= count
329
330
331 @given(st.text(), st.lists(st.text(), min_size=2), st.text(), st.integers(min_value=0, max_value=1000000))
332 def test_hypothesis_search(needle, pieces, by, maxcount):
333 needle = needle.encode("utf-8")
334 pieces = [piece.encode("utf-8") for piece in pieces]
335 by = by.encode("utf-8")
336 input = needle.join(pieces)
337 assume(len(input) > 0)
338
339 res = replace(input, needle, by, maxcount)
340 assert res == input.replace(needle, by, maxcount)