[pypy-commit] pypy default: Add extra-tests for string methods, matching the interp-level tests added in 88bed3bb8ad4
rlamy
pypy.commits at gmail.com
Sat Nov 4 16:28:39 EDT 2017
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch:
Changeset: r92930:b97f900404e5
Date: 2017-11-04 20:28 +0000
http://bitbucket.org/pypy/pypy/changeset/b97f900404e5/
Log: Add extra-tests for string methods, matching the interp-level tests
added in 88bed3bb8ad4
diff --git a/extra_tests/test_bytes.py b/extra_tests/test_bytes.py
new file mode 100644
--- /dev/null
+++ b/extra_tests/test_bytes.py
@@ -0,0 +1,82 @@
+from hypothesis import strategies as st
+from hypothesis import given, example
+
+ at given(st.binary(), st.binary(), st.binary())
+def test_find(u, prefix, suffix):
+ s = prefix + u + suffix
+ assert 0 <= s.find(u) <= len(prefix)
+ assert s.find(u, len(prefix), len(s) - len(suffix)) == len(prefix)
+
+ at given(st.binary(), st.binary(), st.binary())
+def test_index(u, prefix, suffix):
+ s = prefix + u + suffix
+ assert 0 <= s.index(u) <= len(prefix)
+ assert s.index(u, len(prefix), len(s) - len(suffix)) == len(prefix)
+
+ at given(st.binary(), st.binary(), st.binary())
+def test_rfind(u, prefix, suffix):
+ s = prefix + u + suffix
+ assert s.rfind(u) >= len(prefix)
+ assert s.rfind(u, len(prefix), len(s) - len(suffix)) == len(prefix)
+
+ at given(st.binary(), st.binary(), st.binary())
+def test_rindex(u, prefix, suffix):
+ s = prefix + u + suffix
+ assert s.rindex(u) >= len(prefix)
+ assert s.rindex(u, len(prefix), len(s) - len(suffix)) == len(prefix)
+
+def adjust_indices(u, start, end):
+ if end < 0:
+ end = max(end + len(u), 0)
+ else:
+ end = min(end, len(u))
+ if start < 0:
+ start = max(start + len(u), 0)
+ return start, end
+
+ at given(st.binary(), st.binary())
+def test_startswith_basic(u, v):
+ assert u.startswith(v) is (u[:len(v)] == v)
+
+ at example(b'x', b'', 1)
+ at example(b'x', b'', 2)
+ at given(st.binary(), st.binary(), st.integers())
+def test_startswith_start(u, v, start):
+ expected = u[start:].startswith(v) if v else (start <= len(u))
+ assert u.startswith(v, start) is expected
+
+ at example(b'x', b'', 1, 0)
+ at example(b'xx', b'', -1, 0)
+ at given(st.binary(), st.binary(), st.integers(), st.integers())
+def test_startswith_3(u, v, start, end):
+ if v:
+ expected = u[start:end].startswith(v)
+ else: # CPython leaks implementation details in this case
+ start0, end0 = adjust_indices(u, start, end)
+ expected = start0 <= len(u) and start0 <= end0
+ assert u.startswith(v, start, end) is expected
+
+ at given(st.binary(), st.binary())
+def test_endswith_basic(u, v):
+ if len(v) > len(u):
+ assert u.endswith(v) is False
+ else:
+ assert u.endswith(v) is (u[len(u) - len(v):] == v)
+
+ at example(b'x', b'', 1)
+ at example(b'x', b'', 2)
+ at given(st.binary(), st.binary(), st.integers())
+def test_endswith_2(u, v, start):
+ expected = u[start:].endswith(v) if v else (start <= len(u))
+ assert u.endswith(v, start) is expected
+
+ at example(b'x', b'', 1, 0)
+ at example(b'xx', b'', -1, 0)
+ at given(st.binary(), st.binary(), st.integers(), st.integers())
+def test_endswith_3(u, v, start, end):
+ if v:
+ expected = u[start:end].endswith(v)
+ else: # CPython leaks implementation details in this case
+ start0, end0 = adjust_indices(u, start, end)
+ expected = start0 <= len(u) and start0 <= end0
+ assert u.endswith(v, start, end) is expected
diff --git a/extra_tests/test_unicode.py b/extra_tests/test_unicode.py
--- a/extra_tests/test_unicode.py
+++ b/extra_tests/test_unicode.py
@@ -1,3 +1,4 @@
+import sys
import pytest
from hypothesis import strategies as st
from hypothesis import given, settings, example
@@ -32,3 +33,89 @@
@given(s=st.text())
def test_composition(s, norm1, norm2, norm3):
assert normalize(norm2, normalize(norm1, s)) == normalize(norm3, s)
+
+ at given(st.text(), st.text(), st.text())
+def test_find(u, prefix, suffix):
+ s = prefix + u + suffix
+ assert 0 <= s.find(u) <= len(prefix)
+ assert s.find(u, len(prefix), len(s) - len(suffix)) == len(prefix)
+
+ at given(st.text(), st.text(), st.text())
+def test_index(u, prefix, suffix):
+ s = prefix + u + suffix
+ assert 0 <= s.index(u) <= len(prefix)
+ assert s.index(u, len(prefix), len(s) - len(suffix)) == len(prefix)
+
+ at given(st.text(), st.text(), st.text())
+def test_rfind(u, prefix, suffix):
+ s = prefix + u + suffix
+ assert s.rfind(u) >= len(prefix)
+ assert s.rfind(u, len(prefix), len(s) - len(suffix)) == len(prefix)
+
+ at given(st.text(), st.text(), st.text())
+def test_rindex(u, prefix, suffix):
+ s = prefix + u + suffix
+ assert s.rindex(u) >= len(prefix)
+ assert s.rindex(u, len(prefix), len(s) - len(suffix)) == len(prefix)
+
+def adjust_indices(u, start, end):
+ if end < 0:
+ end = max(end + len(u), 0)
+ else:
+ end = min(end, len(u))
+ if start < 0:
+ start = max(start + len(u), 0)
+ return start, end
+
+ at given(st.text(), st.text())
+def test_startswith_basic(u, v):
+ assert u.startswith(v) is (u[:len(v)] == v)
+
+ at example(u'x', u'', 1)
+ at example(u'x', u'', 2)
+ at given(st.text(), st.text(), st.integers())
+def test_startswith_2(u, v, start):
+ if v or sys.version_info[0] == 2:
+ expected = u[start:].startswith(v)
+ else: # CPython leaks implementation details in this case
+ expected = start <= len(u)
+ assert u.startswith(v, start) is expected
+
+ at example(u'x', u'', 1, 0)
+ at example(u'xx', u'', -1, 0)
+ at given(st.text(), st.text(), st.integers(), st.integers())
+def test_startswith_3(u, v, start, end):
+ if v or sys.version_info[0] == 2:
+ expected = u[start:end].startswith(v)
+ else: # CPython leaks implementation details in this case
+ start0, end0 = adjust_indices(u, start, end)
+ expected = start0 <= len(u) and start0 <= end0
+ assert u.startswith(v, start, end) is expected
+
+ at given(st.text(), st.text())
+def test_endswith_basic(u, v):
+ if len(v) > len(u):
+ assert u.endswith(v) is False
+ else:
+ assert u.endswith(v) is (u[len(u) - len(v):] == v)
+
+ at example(u'x', u'', 1)
+ at example(u'x', u'', 2)
+ at given(st.text(), st.text(), st.integers())
+def test_endswith_2(u, v, start):
+ if v or sys.version_info[0] == 2:
+ expected = u[start:].endswith(v)
+ else: # CPython leaks implementation details in this case
+ expected = start <= len(u)
+ assert u.endswith(v, start) is expected
+
+ at example(u'x', u'', 1, 0)
+ at example(u'xx', u'', -1, 0)
+ at given(st.text(), st.text(), st.integers(), st.integers())
+def test_endswith_3(u, v, start, end):
+ if v or sys.version_info[0] == 2:
+ expected = u[start:end].endswith(v)
+ else: # CPython leaks implementation details in this case
+ start0, end0 = adjust_indices(u, start, end)
+ expected = start0 <= len(u) and start0 <= end0
+ assert u.endswith(v, start, end) is expected
More information about the pypy-commit
mailing list