https://github.com/python/cpython/commit/fe1c75e2efa7220141b454f356e3b1879bddafa8
commit: fe1c75e2efa7220141b454f356e3b1879bddafa8
branch: main
author: scrieve <[email protected]>
committer: StanFromIreland <[email protected]>
date: 2026-11-10T21:32:35Z
summary:

gh-160501: Test `adaptive_find` in `fastsearch.h` for all string kinds (#160502)

files:
M Lib/test/test_str.py

diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py
index b4b4572256c8f2..e0632db94b9a84 100644
--- a/Lib/test/test_str.py
+++ b/Lib/test/test_str.py
@@ -267,6 +267,30 @@ class MyStr(str):
             pass
         self.checkequal(3, MyStr('aaa'), 'count', 'a')
 
+    def test_find_adaptive_widths(self):
+        # The adaptive path in Objects/stringlib/fastsearch.h needs a haystack
+        # of at least 2500 characters and a needle of at least a third of it,
+        # and it is compiled once per character width. Exercise all four.
+        cases = [
+            ('ascii', 'a', 'b', 'c'),
+            ('latin1', '\xe9', '\xea', '\xeb'),
+            ('ucs2', '\u0100', '\u0101', '\u0102'),
+            ('ucs4', '\U0001d11e', '\U0001d11f', '\U0001d120'),
+        ]
+        for kind, c, d, x in cases:
+            # A needle of 2000 characters, none of which is x.
+            needle = c * 1000 + d * 1000
+            with self.subTest(kind=kind):
+                # 4000 characters, mostly x: the skip branches jump by m.
+                haystack = c * 1000 + x * 3000
+                self.assertEqual(haystack.find(needle), -1)
+                self.assertEqual(haystack.count(needle), 0)
+                # A match at 3000 in a haystack of 5000, so count() reaches
+                # the match branch inside adaptive_find rather than two-way.
+                haystack = c * 1000 + x * 2000 + needle
+                self.assertEqual(haystack.find(needle), 3000)
+                self.assertEqual(haystack.count(needle), 1)
+
     def test_find(self):
         string_tests.StringLikeTest.test_find(self)
         # test implementation details of the memchr fast path

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to