Skip to content

Commit 06faa9a

Browse files
authored
gh-110160: Fix flaky test_find_periodic_pattern in string_tests (#110170)
1 parent 038c356 commit 06faa9a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Lib/test/string_tests.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,12 @@ def reference_find(p, s):
327327
for i in range(len(s)):
328328
if s.startswith(p, i):
329329
return i
330+
if p == '' and s == '':
331+
return 0
330332
return -1
331333

332-
rr = random.randrange
333-
choices = random.choices
334-
for _ in range(1000):
334+
def check_pattern(rr):
335+
choices = random.choices
335336
p0 = ''.join(choices('abcde', k=rr(10))) * rr(10, 20)
336337
p = p0[:len(p0) - rr(10)] # pop off some characters
337338
left = ''.join(choices('abcdef', k=rr(2000)))
@@ -341,6 +342,13 @@ def reference_find(p, s):
341342
self.checkequal(reference_find(p, text),
342343
text, 'find', p)
343344

345+
rr = random.randrange
346+
for _ in range(1000):
347+
check_pattern(rr)
348+
349+
# Test that empty string always work:
350+
check_pattern(lambda *args: 0)
351+
344352
def test_find_many_lengths(self):
345353
haystack_repeats = [a * 10**e for e in range(6) for a in (1,2,5)]
346354
haystacks = [(n, self.fixtype("abcab"*n + "da")) for n in haystack_repeats]

0 commit comments

Comments
 (0)