Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Lib/test/string_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,14 @@ def test_rindex(self):

def test_find_periodic_pattern(self):
"""Cover the special path for periodic patterns."""
def reference_find(p, s):
for i in range(len(s)):
if s.startswith(p, i):
def reference_find(text, p):
for i in range(len(text) - len(p) + 1):
if text.startswith(p, i):
return i
return -1

short = ['', 'a', 'b', 'ab', 'ba', 'aab', 'aba', 'baa']
pairs = [(text, p) for text in short for p in short]
rr = random.randrange
choices = random.choices
for _ in range(1000):
Expand All @@ -337,8 +339,10 @@ def reference_find(p, s):
left = ''.join(choices('abcdef', k=rr(2000)))
right = ''.join(choices('abcdef', k=rr(2000)))
text = left + p + right
pairs.append((text, p))
for text, p in pairs:
with self.subTest(p=p, text=text):
self.checkequal(reference_find(p, text),
self.checkequal(reference_find(text, p),
text, 'find', p)

def test_find_many_lengths(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an intermittent test failure in ``test_find_periodic_pattern`` from the ``string_tests`` module.