aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Friedrich Bolz-Tereick <cfbolz@gmx.de>2021-02-25 13:15:00 +0100
committerCarl Friedrich Bolz-Tereick <cfbolz@gmx.de>2021-02-25 13:15:00 +0100
commit08e2fc54685ccfbf5ae58456bd7be5b0f9305ecf (patch)
tree60fadb4638fbfa72d0a1f5ea2bf8c29b4da693ac /rpython
parentfix a tiny performance bug in our string search that we ported from cpython. (diff)
downloadpypy-08e2fc54685ccfbf5ae58456bd7be5b0f9305ecf.tar.gz
pypy-08e2fc54685ccfbf5ae58456bd7be5b0f9305ecf.tar.bz2
pypy-08e2fc54685ccfbf5ae58456bd7be5b0f9305ecf.zip
add a random test for finding
Diffstat (limited to 'rpython')
-rw-r--r--rpython/rlib/test/test_rstring.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/rpython/rlib/test/test_rstring.py b/rpython/rlib/test/test_rstring.py
index 10724b07ee..66c23dbd52 100644
--- a/rpython/rlib/test/test_rstring.py
+++ b/rpython/rlib/test/test_rstring.py
@@ -2,10 +2,12 @@ import sys, py
from rpython.rlib.rstring import StringBuilder, UnicodeBuilder, split, rsplit
from rpython.rlib.rstring import replace, startswith, endswith, replace_count
-from rpython.rlib.rstring import find, rfind, count
+from rpython.rlib.rstring import find, rfind, count, _search, SEARCH_COUNT, SEARCH_FIND
from rpython.rlib.buffer import StringBuffer
from rpython.rtyper.test.tool import BaseRtypingTest
+from hypothesis import given, strategies as st
+
def test_split():
def check_split(value, sub, *args, **kwargs):
result = kwargs['res']
@@ -306,3 +308,21 @@ class TestTranslates(BaseRtypingTest):
return res
res = self.interpret(fn, [])
assert res
+
+@given(u=st.text(), prefix=st.text(), suffix=st.text())
+def test_hypothesis_search(u, prefix, suffix):
+ prefix = prefix.encode("utf-8")
+ u = u.encode("utf-8")
+ suffix = suffix.encode("utf-8")
+ s = prefix + u + suffix
+
+ index = _search(s, u, 0, len(s), SEARCH_FIND)
+ assert index == s.find(u)
+ assert 0 <= index <= len(prefix)
+
+ index = _search(s, u, len(prefix), len(s) - len(suffix), SEARCH_FIND)
+ assert index == len(prefix)
+
+ count = _search(s, u, 0, len(s), SEARCH_COUNT)
+ assert count == s.count(u)
+ assert 1 <= count