summaryrefslogtreecommitdiffstats
path: root/Lib/test/string_tests.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2005-02-20 09:54:53 (GMT)
committerRaymond Hettinger <python@rcn.com>2005-02-20 09:54:53 (GMT)
commit57e7447c447249a33a13da853f8c1bfec546842a (patch)
treeb1c4d48fef4139e44d09df84134bd8596c9f22bf /Lib/test/string_tests.py
parent7cbf1bcb3e55c61617352ec1b20176603dacbafe (diff)
downloadcpython-57e7447c447249a33a13da853f8c1bfec546842a.zip
cpython-57e7447c447249a33a13da853f8c1bfec546842a.tar.gz
cpython-57e7447c447249a33a13da853f8c1bfec546842a.tar.bz2
* Beef-up tests for str.count().
* Speed-up str.count() by using memchr() to fly between first char matches.
Diffstat (limited to 'Lib/test/string_tests.py')
-rw-r--r--Lib/test/string_tests.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 0ce9618..f2173a7 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -114,6 +114,33 @@ class CommonTest(unittest.TestCase):
self.checkraises(TypeError, 'hello', 'count')
self.checkraises(TypeError, 'hello', 'count', 42)
+ # For a variety of combinations,
+ # verify that str.count() matches an equivalent function
+ # replacing all occurrences and then differencing the string lengths
+ charset = ['', 'a', 'b']
+ digits = 7
+ base = len(charset)
+ teststrings = set()
+ for i in xrange(base ** digits):
+ entry = []
+ for j in xrange(digits):
+ i, m = divmod(i, base)
+ entry.append(charset[m])
+ teststrings.add(''.join(entry))
+ teststrings = list(teststrings)
+ for i in teststrings:
+ i = self.fixtype(i)
+ n = len(i)
+ for j in teststrings:
+ r1 = i.count(j)
+ if j:
+ r2, rem = divmod(n - len(i.replace(j, '')), len(j))
+ else:
+ r2, rem = len(i)+1, 0
+ if rem or r1 != r2:
+ self.assertEqual(rem, 0)
+ self.assertEqual(r1, r2)
+
def test_find(self):
self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
@@ -135,6 +162,7 @@ class CommonTest(unittest.TestCase):
i, m = divmod(i, base)
entry.append(charset[m])
teststrings.add(''.join(entry))
+ teststrings = list(teststrings)
for i in teststrings:
i = self.fixtype(i)
for j in teststrings: