]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-110160: Fix flaky `test_find_periodic_pattern` in `string_tests` (GH-110170...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Oct 2023 15:48:17 +0000 (08:48 -0700)
committerGitHub <noreply@github.com>
Mon, 2 Oct 2023 15:48:17 +0000 (17:48 +0200)
gh-110160: Fix flaky `test_find_periodic_pattern` in `string_tests` (GH-110170)
(cherry picked from commit 06faa9a39bd93c5e7999d52b52043ecdd0774dac)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/test/string_tests.py

index 709cac7a27a4497f4edc560a963c3227993c5a23..408b19338b55b5ade11356ed018b61a15582d396 100644 (file)
@@ -327,11 +327,12 @@ class BaseTest:
             for i in range(len(s)):
                 if s.startswith(p, i):
                     return i
+            if p == '' and s == '':
+                return 0
             return -1
 
-        rr = random.randrange
-        choices = random.choices
-        for _ in range(1000):
+        def check_pattern(rr):
+            choices = random.choices
             p0 = ''.join(choices('abcde', k=rr(10))) * rr(10, 20)
             p = p0[:len(p0) - rr(10)] # pop off some characters
             left = ''.join(choices('abcdef', k=rr(2000)))
@@ -341,6 +342,13 @@ class BaseTest:
                 self.checkequal(reference_find(p, text),
                                 text, 'find', p)
 
+        rr = random.randrange
+        for _ in range(1000):
+            check_pattern(rr)
+
+        # Test that empty string always work:
+        check_pattern(lambda *args: 0)
+
     def test_find_many_lengths(self):
         haystack_repeats = [a * 10**e for e in range(6) for a in (1,2,5)]
         haystacks = [(n, self.fixtype("abcab"*n + "da")) for n in haystack_repeats]