summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAndrew Dalke <dalke@dalkescientific.com>2006-05-24 18:55:37 (GMT)
committerAndrew Dalke <dalke@dalkescientific.com>2006-05-24 18:55:37 (GMT)
commite5488ec01e38e7edfb27ebddd410ec55a3120777 (patch)
tree2a0fdf49e3fc3464b8b5c47e0227d16c5eff7fe6 /Lib
parent347ee277aa80a98d61d85fef6d1b8f8467ad7eaf (diff)
downloadcpython-e5488ec01e38e7edfb27ebddd410ec55a3120777.zip
cpython-e5488ec01e38e7edfb27ebddd410ec55a3120777.tar.gz
cpython-e5488ec01e38e7edfb27ebddd410ec55a3120777.tar.bz2
Added a slew of test for string replace, based various corner cases from
the Need For Speed sprint coding. Includes commented out overflow tests which will be uncommented once the code is fixed. This test will break the 8-bit string tests because "".replace("", "A") == "" when it should == "A" We have a fix for it, which should be added tomorrow.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/string_tests.py157
1 files changed, 157 insertions, 0 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index aab98c2..473a2e7 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -376,6 +376,153 @@ class CommonTest(unittest.TestCase):
self.checkraises(TypeError, 'hello', 'swapcase', 42)
def test_replace(self):
+ EQ = self.checkequal
+
+ # Operations on the empty string
+ EQ("", "", "replace", "", "")
+ EQ("A", "", "replace", "", "A")
+ EQ("", "", "replace", "A", "")
+ EQ("", "", "replace", "A", "A")
+ EQ("", "", "replace", "", "", 100)
+ EQ("", "", "replace", "", "", sys.maxint)
+
+ # interleave (from=="", 'to' gets inserted everywhere)
+ EQ("A", "A", "replace", "", "")
+ EQ("*A*", "A", "replace", "", "*")
+ EQ("*1A*1", "A", "replace", "", "*1")
+ EQ("*-#A*-#", "A", "replace", "", "*-#")
+ EQ("*-A*-A*-", "AA", "replace", "", "*-")
+ EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
+ EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
+ EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
+ EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
+ EQ("*-A*-A", "AA", "replace", "", "*-", 2)
+ EQ("*-AA", "AA", "replace", "", "*-", 1)
+ EQ("AA", "AA", "replace", "", "*-", 0)
+
+ # single character deletion (from=="A", to=="")
+ EQ("", "A", "replace", "A", "")
+ EQ("", "AAA", "replace", "A", "")
+ EQ("", "AAA", "replace", "A", "", -1)
+ EQ("", "AAA", "replace", "A", "", sys.maxint)
+ EQ("", "AAA", "replace", "A", "", 4)
+ EQ("", "AAA", "replace", "A", "", 3)
+ EQ("A", "AAA", "replace", "A", "", 2)
+ EQ("AA", "AAA", "replace", "A", "", 1)
+ EQ("AAA", "AAA", "replace", "A", "", 0)
+ EQ("", "AAAAAAAAAA", "replace", "A", "")
+ EQ("BCD", "ABACADA", "replace", "A", "")
+ EQ("BCD", "ABACADA", "replace", "A", "", -1)
+ EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
+ EQ("BCD", "ABACADA", "replace", "A", "", 5)
+ EQ("BCD", "ABACADA", "replace", "A", "", 4)
+ EQ("BCDA", "ABACADA", "replace", "A", "", 3)
+ EQ("BCADA", "ABACADA", "replace", "A", "", 2)
+ EQ("BACADA", "ABACADA", "replace", "A", "", 1)
+ EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
+ EQ("BCD", "ABCAD", "replace", "A", "")
+ EQ("BCD", "ABCADAA", "replace", "A", "")
+ EQ("BCD", "BCD", "replace", "A", "")
+ EQ("*************", "*************", "replace", "A", "")
+ EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
+
+ # substring deletion (from=="the", to=="")
+ EQ("", "the", "replace", "the", "")
+ EQ("ater", "theater", "replace", "the", "")
+ EQ("", "thethe", "replace", "the", "")
+ EQ("", "thethethethe", "replace", "the", "")
+ EQ("aaaa", "theatheatheathea", "replace", "the", "")
+ EQ("that", "that", "replace", "the", "")
+ EQ("thaet", "thaet", "replace", "the", "")
+ EQ("here and re", "here and there", "replace", "the", "")
+ EQ("here and re and re", "here and there and there",
+ "replace", "the", "", sys.maxint)
+ EQ("here and re and re", "here and there and there",
+ "replace", "the", "", -1)
+ EQ("here and re and re", "here and there and there",
+ "replace", "the", "", 3)
+ EQ("here and re and re", "here and there and there",
+ "replace", "the", "", 2)
+ EQ("here and re and there", "here and there and there",
+ "replace", "the", "", 1)
+ EQ("here and there and there", "here and there and there",
+ "replace", "the", "", 0)
+ EQ("here and re and re", "here and there and there", "replace", "the", "")
+
+ EQ("abc", "abc", "replace", "the", "")
+ EQ("abcdefg", "abcdefg", "replace", "the", "")
+
+ # substring deletion (from=="bob", to=="")
+ EQ("bob", "bbobob", "replace", "bob", "")
+ EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
+ EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
+ EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
+
+ # single character replace in place (len(from)==len(to)==1)
+ EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
+ EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
+ EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
+ EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
+ EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
+ EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
+ EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
+ EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
+
+ EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
+ EQ("who goes there?", "Who goes there?", "replace", "W", "w")
+ EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
+ EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
+ EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
+
+ EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
+
+ # substring replace in place (len(from)==len(to) > 1)
+ EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
+ EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
+ EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
+ EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
+ EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
+ EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
+ EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
+ EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
+ EQ("cobob", "bobob", "replace", "bob", "cob")
+ EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
+ EQ("bobob", "bobob", "replace", "bot", "bot")
+
+ # replace single character (len(from)==1, len(to)>1)
+ EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
+ EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
+ EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
+ EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
+ EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
+ EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
+ EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
+
+ EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
+
+ # replace substring (len(from)>1, len(to)!=len(from))
+ EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
+ "replace", "spam", "ham")
+ EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
+ "replace", "spam", "ham", sys.maxint)
+ EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
+ "replace", "spam", "ham", -1)
+ EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
+ "replace", "spam", "ham", 4)
+ EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
+ "replace", "spam", "ham", 3)
+ EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
+ "replace", "spam", "ham", 2)
+ EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
+ "replace", "spam", "ham", 1)
+ EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
+ "replace", "spam", "ham", 0)
+
+ EQ("bobob", "bobobob", "replace", "bobob", "bob")
+ EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
+ EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
+
+ #
self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
@@ -403,6 +550,16 @@ class CommonTest(unittest.TestCase):
self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
+### Commented out until the underlying libraries are fixed
+## def test_replace_overflow(self):
+## # Check for overflow checking on 32 bit machines
+## if sys.maxint != 2147483647:
+## return
+## A2_16 = "A" * (2**16)
+## self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
+## self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
+## self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
+
def test_zfill(self):
self.checkequal('123', '123', 'zfill', 2)
self.checkequal('123', '123', 'zfill', 3)