From aadfb54bb6fe4718fbd6c4495585c1526dd542d1 Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Sat, 10 Jun 2023 18:14:30 -0400 Subject: [PATCH 1/4] Fix reference_find for empty strings --- Lib/test/string_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index a6ea2f378b37ac..170fa78ab2c382 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -318,7 +318,7 @@ 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)): + for i in range(len(s)+1): if s.startswith(p, i): return i return -1 From b94940d7c611f49837d064c577c1ded2d818cc27 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 10 Jun 2023 22:24:39 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Tests/2023-06-10-22-24-34.gh-issue-105639.Zg6a5c.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2023-06-10-22-24-34.gh-issue-105639.Zg6a5c.rst 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. From 8fc6622d51de0b25c2d65d6aee9fcf0e84bb569b Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Sun, 11 Jun 2023 01:50:17 -0400 Subject: [PATCH 3/4] rename and re-order arguments, check only i <= n-m --- Lib/test/string_tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 170fa78ab2c382..f1294c45f3c63f 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -317,9 +317,9 @@ 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)+1): - 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 @@ -332,7 +332,7 @@ def reference_find(p, s): right = ''.join(choices('abcdef', k=rr(2000))) text = left + p + right 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): From 897bfa59a9ffa5e4dfe11ed489ac714884c97d9d Mon Sep 17 00:00:00 2001 From: sweeneyde Date: Sun, 11 Jun 2023 03:10:12 -0400 Subject: [PATCH 4/4] Add some deterministic cases --- Lib/test/string_tests.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index f1294c45f3c63f..013efc5199b448 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -323,6 +323,8 @@ def reference_find(text, p): 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): @@ -331,6 +333,8 @@ def reference_find(text, p): 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(text, p), text, 'find', p)