[Python-checkins] python/dist/src/Lib/test string_tests.py, 1.43, 1.44

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sun Feb 20 10:54:55 CET 2005


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22296/Lib/test

Modified Files:
	string_tests.py 
Log Message:
* Beef-up tests for str.count().
* Speed-up str.count() by using memchr() to fly between first char matches.



Index: string_tests.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/string_tests.py,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -d -r1.43 -r1.44
--- string_tests.py	20 Feb 2005 04:07:08 -0000	1.43
+++ string_tests.py	20 Feb 2005 09:54:53 -0000	1.44
@@ -114,6 +114,33 @@
         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 @@
                 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:



More information about the Python-checkins mailing list