Skip to content

Commit fe1c75e

Browse files
authored
gh-160501: Test adaptive_find in fastsearch.h for all string kinds (#160502)
1 parent d2db18f commit fe1c75e

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎Lib/test/test_str.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,30 @@ class MyStr(str):
267267
pass
268268
self.checkequal(3, MyStr('aaa'), 'count', 'a')
269269

270+
def test_find_adaptive_widths(self):
271+
# The adaptive path in Objects/stringlib/fastsearch.h needs a haystack
272+
# of at least 2500 characters and a needle of at least a third of it,
273+
# and it is compiled once per character width. Exercise all four.
274+
cases = [
275+
('ascii', 'a', 'b', 'c'),
276+
('latin1', '\xe9', '\xea', '\xeb'),
277+
('ucs2', '\u0100', '\u0101', '\u0102'),
278+
('ucs4', '\U0001d11e', '\U0001d11f', '\U0001d120'),
279+
]
280+
for kind, c, d, x in cases:
281+
# A needle of 2000 characters, none of which is x.
282+
needle = c * 1000 + d * 1000
283+
with self.subTest(kind=kind):
284+
# 4000 characters, mostly x: the skip branches jump by m.
285+
haystack = c * 1000 + x * 3000
286+
self.assertEqual(haystack.find(needle), -1)
287+
self.assertEqual(haystack.count(needle), 0)
288+
# A match at 3000 in a haystack of 5000, so count() reaches
289+
# the match branch inside adaptive_find rather than two-way.
290+
haystack = c * 1000 + x * 2000 + needle
291+
self.assertEqual(haystack.find(needle), 3000)
292+
self.assertEqual(haystack.count(needle), 1)
293+
270294
def test_find(self):
271295
string_tests.StringLikeTest.test_find(self)
272296
# test implementation details of the memchr fast path

0 commit comments

Comments
 (0)