diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 8d210b198d248d..5c859400ed943b 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -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): @@ -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): diff --git a/Misc/NEWS.d/next/Tests/2023-06-10-22-24-34.gh-issue-105639.Zg6a5c.rst b/Misc/NEWS.d/next/Tests/2023-06-10-22-24-34.gh-issue-105639.Zg6a5c.rst new file mode 100644 index 00000000000000..a5419019d6d84d --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-06-10-22-24-34.gh-issue-105639.Zg6a5c.rst @@ -0,0 +1 @@ +Fixed an intermittent test failure in ``test_find_periodic_pattern`` from the ``string_tests`` module.