annotate Lib/test/string_tests.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents dab34b60a72e
children e23cba03cc4b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1 """
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
2 Common tests shared by test_str, test_unicode, test_userstring and test_string.
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
3 """
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
4
41589
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41532
diff changeset
5 import unittest, string, sys, struct
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
6 from test import test_support
13527
07c31182f237 add more tests of string.join variants to run_method_tests
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13517
diff changeset
7 from UserList import UserList
07c31182f237 add more tests of string.join variants to run_method_tests
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13517
diff changeset
8
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
9 class Sequence:
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
10 def __init__(self, seq='wxyz'): self.seq = seq
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
11 def __len__(self): return len(self.seq)
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
12 def __getitem__(self, i): return self.seq[i]
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
14 class BadSeq1(Sequence):
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15 def __init__(self): self.seq = [7, 'hello', 123L]
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17 class BadSeq2(Sequence):
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 def __init__(self): self.seq = ['a', 'b', 'c']
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 def __len__(self): return 8
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
21 class CommonTest(unittest.TestCase):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
22 # This testcase contains test that can be used in all
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
23 # stringlike classes. Currently this is str, unicode
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
24 # UserString and the string module.
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
25
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
26 # The type to be tested
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
27 # Change in subclasses to change the behaviour of fixtesttype()
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
28 type2test = None
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
29
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
30 # All tests pass their arguments to the testing methods
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
31 # as str objects. fixtesttype() can be used to propagate
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
32 # these arguments to the appropriate type
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
33 def fixtype(self, obj):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
34 if isinstance(obj, str):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
35 return self.__class__.type2test(obj)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
36 elif isinstance(obj, list):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
37 return [self.fixtype(x) for x in obj]
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
38 elif isinstance(obj, tuple):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
39 return tuple([self.fixtype(x) for x in obj])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
40 elif isinstance(obj, dict):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
41 return dict([
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
42 (self.fixtype(key), self.fixtype(value))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
43 for (key, value) in obj.iteritems()
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
44 ])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
45 else:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
46 return obj
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
47
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
48 # check that object.method(*args) returns result
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
49 def checkequal(self, result, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
50 result = self.fixtype(result)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
51 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
52 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
53 realresult = getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
54 self.assertEqual(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
55 result,
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
56 realresult
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
57 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
58 # if the original is returned make sure that
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
59 # this doesn't happen with subclasses
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
60 if object == realresult:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
61 class subtype(self.__class__.type2test):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
62 pass
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
63 object = subtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
64 realresult = getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
65 self.assert_(object is not realresult)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
66
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
67 # check that object.method(*args) raises exc
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
68 def checkraises(self, exc, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
69 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
70 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
71 self.assertRaises(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
72 exc,
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
73 getattr(object, methodname),
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
74 *args
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
75 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
76
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
77 # call object.method(*args) without any checks
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
78 def checkcall(self, object, methodname, *args):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
79 object = self.fixtype(object)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
80 args = self.fixtype(args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
81 getattr(object, methodname)(*args)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
82
33833
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
83 def test_hash(self):
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
84 # SF bug 1054139: += optimization was not invalidating cached hash value
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
85 a = self.type2test('DNSSEC')
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
86 b = self.type2test('')
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
87 for c in a:
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
88 b += c
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
89 hash(b)
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
90 self.assertEqual(hash(a), hash(b))
10512ceba30c SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents: 33305
diff changeset
91
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
92 def test_capitalize(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
93 self.checkequal(' hello ', ' hello ', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
94 self.checkequal('Hello ', 'Hello ','capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
95 self.checkequal('Hello ', 'hello ','capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
96 self.checkequal('Aaaa', 'aaaa', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
97 self.checkequal('Aaaa', 'AaAa', 'capitalize')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
98
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
99 self.checkraises(TypeError, 'hello', 'capitalize', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
100
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
101 def test_count(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
102 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
103 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
104 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
105 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
106 self.checkequal(3, 'aaa', 'count', 'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
107 self.checkequal(0, 'aaa', 'count', 'b')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
108 self.checkequal(0, 'aaa', 'count', 'b')
38492
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
109 self.checkequal(2, 'aaa', 'count', 'a', 1)
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
110 self.checkequal(0, 'aaa', 'count', 'a', 10)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
111 self.checkequal(1, 'aaa', 'count', 'a', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
112 self.checkequal(3, 'aaa', 'count', 'a', -10)
38492
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
113 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
114 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
115 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
116 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
38492
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
117 self.checkequal(3, 'aaa', 'count', '', 1)
38516
2fe3233bd426 changed count to return 0 for slices outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38514
diff changeset
118 self.checkequal(1, 'aaa', 'count', '', 3)
2fe3233bd426 changed count to return 0 for slices outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38514
diff changeset
119 self.checkequal(0, 'aaa', 'count', '', 10)
38492
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
120 self.checkequal(2, 'aaa', 'count', '', -1)
d5bdfd120232 fixed "abc".count("", 100) == -96 error (hopefully, nobody's relying on
Fredrik Lundh <fredrik@pythonware.com>
parents: 38391
diff changeset
121 self.checkequal(4, 'aaa', 'count', '', -10)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
122
49308
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
123 self.checkequal(1, '', 'count', '')
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
124 self.checkequal(0, '', 'count', '', 1, 1)
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
125 self.checkequal(0, '', 'count', '', sys.maxint, 0)
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
126
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
127 self.checkequal(0, '', 'count', 'xx')
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
128 self.checkequal(0, '', 'count', 'xx', 1, 1)
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
129 self.checkequal(0, '', 'count', 'xx', sys.maxint, 0)
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
130
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
131 self.checkraises(TypeError, 'hello', 'count')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
132 self.checkraises(TypeError, 'hello', 'count', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
133
34621
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
134 # For a variety of combinations,
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
135 # verify that str.count() matches an equivalent function
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
136 # replacing all occurrences and then differencing the string lengths
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
137 charset = ['', 'a', 'b']
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
138 digits = 7
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
139 base = len(charset)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
140 teststrings = set()
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
141 for i in xrange(base ** digits):
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
142 entry = []
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
143 for j in xrange(digits):
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
144 i, m = divmod(i, base)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
145 entry.append(charset[m])
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
146 teststrings.add(''.join(entry))
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
147 teststrings = list(teststrings)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
148 for i in teststrings:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
149 i = self.fixtype(i)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
150 n = len(i)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
151 for j in teststrings:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
152 r1 = i.count(j)
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
153 if j:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
154 r2, rem = divmod(n - len(i.replace(j, '')), len(j))
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
155 else:
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
156 r2, rem = len(i)+1, 0
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
157 if rem or r1 != r2:
39302
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
158 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
159 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
34621
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
160
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
161 def test_find(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
162 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
163 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
164 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
165
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
166 self.checkequal(0, 'abc', 'find', '', 0)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
167 self.checkequal(3, 'abc', 'find', '', 3)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
168 self.checkequal(-1, 'abc', 'find', '', 4)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
169
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
170 # to check the ability to pass None as defaults
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
171 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
172 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
173 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
174 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
175 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
176
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
177 self.checkraises(TypeError, 'hello', 'find')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
178 self.checkraises(TypeError, 'hello', 'find', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
179
49308
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
180 self.checkequal(0, '', 'find', '')
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
181 self.checkequal(-1, '', 'find', '', 1, 1)
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
182 self.checkequal(-1, '', 'find', '', sys.maxint, 0)
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
183
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
184 self.checkequal(-1, '', 'find', 'xx')
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
185 self.checkequal(-1, '', 'find', 'xx', 1, 1)
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
186 self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0)
304bea8731d2 #3967: Correct a crash in count() and find() methods of string-like objects.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48935
diff changeset
187
58096
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
188 # issue 7458
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
189 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
190
34620
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
191 # For a variety of combinations,
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
192 # verify that str.find() matches __contains__
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
193 # and that the found substring is really at that location
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
194 charset = ['', 'a', 'b', 'c']
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
195 digits = 5
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
196 base = len(charset)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
197 teststrings = set()
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
198 for i in xrange(base ** digits):
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
199 entry = []
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
200 for j in xrange(digits):
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
201 i, m = divmod(i, base)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
202 entry.append(charset[m])
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
203 teststrings.add(''.join(entry))
34621
1edea4c2b011 * Beef-up tests for str.count().
Raymond Hettinger <python@rcn.com>
parents: 34620
diff changeset
204 teststrings = list(teststrings)
34620
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
205 for i in teststrings:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
206 i = self.fixtype(i)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
207 for j in teststrings:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
208 loc = i.find(j)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
209 r1 = (loc != -1)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
210 r2 = j in i
58098
4acd8faea2d9 Remove silly conditional.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58096
diff changeset
211 self.assertEqual(r1, r2)
34620
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
212 if loc != -1:
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
213 self.assertEqual(i[loc:loc+len(j)], j)
556f6f1056a8 * Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents: 33833
diff changeset
214
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
215 def test_rfind(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
216 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
217 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
218 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
219 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
220
38514
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
221 self.checkequal(3, 'abc', 'rfind', '', 0)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
222 self.checkequal(3, 'abc', 'rfind', '', 3)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
223 self.checkequal(-1, 'abc', 'rfind', '', 4)
ecf7173e79ff changed find/rfind to return -1 for matches outside the source string
Fredrik Lundh <fredrik@pythonware.com>
parents: 38492
diff changeset
224
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
225 # to check the ability to pass None as defaults
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
226 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
227 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
228 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
229 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
230 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
231
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
232 self.checkraises(TypeError, 'hello', 'rfind')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
233 self.checkraises(TypeError, 'hello', 'rfind', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
234
58090
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
235 # For a variety of combinations,
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
236 # verify that str.rfind() matches __contains__
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
237 # and that the found substring is really at that location
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
238 charset = ['', 'a', 'b', 'c']
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
239 digits = 5
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
240 base = len(charset)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
241 teststrings = set()
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
242 for i in xrange(base ** digits):
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
243 entry = []
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
244 for j in xrange(digits):
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
245 i, m = divmod(i, base)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
246 entry.append(charset[m])
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
247 teststrings.add(''.join(entry))
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
248 teststrings = list(teststrings)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
249 for i in teststrings:
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
250 i = self.fixtype(i)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
251 for j in teststrings:
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
252 loc = i.rfind(j)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
253 r1 = (loc != -1)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
254 r2 = j in i
58098
4acd8faea2d9 Remove silly conditional.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58096
diff changeset
255 self.assertEqual(r1, r2)
58090
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
256 if loc != -1:
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
257 self.assertEqual(i[loc:loc+len(j)], j)
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
258
58096
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
259 # issue 7458
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
260 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
f5e496a8417f Add tests for issue #7458: str.rfind() would crash when called with an invalid
Antoine Pitrou <solipsis@pitrou.net>
parents: 58090
diff changeset
261
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
262 def test_index(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
263 self.checkequal(0, 'abcdefghiabc', 'index', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
264 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
265 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
266 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
267
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
268 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
269 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
270 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
271 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
272
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
273 # to check the ability to pass None as defaults
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
274 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
275 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
276 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
277 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
278 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
279
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
280 self.checkraises(TypeError, 'hello', 'index')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
281 self.checkraises(TypeError, 'hello', 'index', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
282
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
283 def test_rindex(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
284 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
285 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
286 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
287 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
288
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
289 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
290 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
291 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
292 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
293 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
294
43886
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
295 # to check the ability to pass None as defaults
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
296 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
297 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
298 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
299 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
300 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
25a5f6a30292 Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents: 42951
diff changeset
301
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
302 self.checkraises(TypeError, 'hello', 'rindex')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
303 self.checkraises(TypeError, 'hello', 'rindex', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
304
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
305 def test_lower(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
306 self.checkequal('hello', 'HeLLo', 'lower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
307 self.checkequal('hello', 'hello', 'lower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
308 self.checkraises(TypeError, 'hello', 'lower', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
309
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
310 def test_upper(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
311 self.checkequal('HELLO', 'HeLLo', 'upper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
312 self.checkequal('HELLO', 'HELLO', 'upper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
313 self.checkraises(TypeError, 'hello', 'upper', 42)
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
314
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
315 def test_expandtabs(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
316 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
317 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
318 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
319 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
320 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
321 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
322 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
42024
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
323 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
324
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
325 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
42024
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
326 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
327 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
328 self.checkraises(OverflowError,
b94f284805be Fix a bug when there was a newline in the string expandtabs was called on.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41589
diff changeset
329 '\ta\n\tb', 'expandtabs', sys.maxint)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
330
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
331 def test_split(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
332 self.checkequal(['this', 'is', 'the', 'split', 'function'],
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
333 'this is the split function', 'split')
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
334
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
335 # by whitespace
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
336 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
337 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
338 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
339 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
340 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
341 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
342 sys.maxint-1)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
343 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
38373
4bfeab230e38 Test for more edge strip cases; leading and trailing separator gets removed
Andrew Dalke <dalke@dalkescientific.com>
parents: 38361
diff changeset
344 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
345 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
346
38340
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
347 self.checkequal([], ' ', 'split')
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
348 self.checkequal(['a'], ' a ', 'split')
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
349 self.checkequal(['a', 'b'], ' a b ', 'split')
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
350 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
351 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
352 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
38342
5382aaf3a0da Added split whitespace checks for characters other than space.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38340
diff changeset
353 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
354 aaa = ' a '*20
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
355 self.checkequal(['a']*20, aaa, 'split')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
356 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
357 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
38340
8a99cdef0c94 Added a few more test cases for whitespace split. These strings have leading whitespace.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38335
diff changeset
358
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
359 # by a char
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
360 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
361 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
362 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
363 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
364 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
365 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
366 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
367 sys.maxint-2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
368 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
369 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
370 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
371 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
372 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
373 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
374
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
375 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
376 self.checkequal(['a']*15 +['a|a|a|a|a'],
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
377 ('a|'*20)[:-1], 'split', '|', 15)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
378
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
379 # by string
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
380 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
381 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
382 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
383 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
384 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
385 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
386 sys.maxint-10)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
387 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
388 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
389 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
390 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
391 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
392 'split', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
393 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
394 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
395 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
396 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
397 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
398 self.checkequal([''], '', 'split', 'aaa')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
399 self.checkequal(['aa'], 'aa', 'split', 'aaa')
38357
be7415b0a318 Test cases for off-by-one errors in string split with multicharacter pattern.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38356
diff changeset
400 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
be7415b0a318 Test cases for off-by-one errors in string split with multicharacter pattern.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38356
diff changeset
401 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
402
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
403 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
404 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
405 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
406 'split', 'BLAH', 18)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
407
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
408 # mixed use of str and unicode
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
409 self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
410
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
411 # argument type
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
412 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
413
38356
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
414 # null case
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
415 self.checkraises(ValueError, 'hello', 'split', '')
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
416 self.checkraises(ValueError, 'hello', 'split', '', 0)
05bbbddfce3f I like tests.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38342
diff changeset
417
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
418 def test_rsplit(self):
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
419 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
420 'this is the rsplit function', 'rsplit')
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
421
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
422 # by whitespace
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
423 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
424 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
425 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
426 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
427 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
428 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
429 sys.maxint-20)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
430 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
38373
4bfeab230e38 Test for more edge strip cases; leading and trailing separator gets removed
Andrew Dalke <dalke@dalkescientific.com>
parents: 38361
diff changeset
431 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
432 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
433
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
434 self.checkequal([], ' ', 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
435 self.checkequal(['a'], ' a ', 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
436 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
437 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
438 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
439 None, 1)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
440 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
441 None, 2)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
442 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
443 aaa = ' a '*20
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
444 self.checkequal(['a']*20, aaa, 'rsplit')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
445 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
446 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
447
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
448
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
449 # by a char
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
450 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
451 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
452 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
453 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
454 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
455 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
456 sys.maxint-100)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
457 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
458 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
459 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
460 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
461 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
462
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
463 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
464
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
465 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
466 self.checkequal(['a|a|a|a|a']+['a']*15,
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
467 ('a|'*20)[:-1], 'rsplit', '|', 15)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
468
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
469 # by string
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
470 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
471 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
472 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
473 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
474 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
475 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
476 sys.maxint-5)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
477 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
478 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
479 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
480 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
481 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
482 'rsplit', 'test')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
483 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
484 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
485 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
486 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
487 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
488 self.checkequal([''], '', 'rsplit', 'aaa')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
489 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
490 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
491 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
492
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
493 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
494 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
495 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
496 'rsplit', 'BLAH', 18)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
497
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
498 # mixed use of str and unicode
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
499 self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2)
31350
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
500
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
501 # argument type
eaa1a4b74328 [SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31252
diff changeset
502 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
31199
504943c89193 Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31021
diff changeset
503
38361
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
504 # null case
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
505 self.checkraises(ValueError, 'hello', 'rsplit', '')
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
506 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
6533d8ae1fd9 Added more rstrip tests, including for prealloc'ed arrays
Andrew Dalke <dalke@dalkescientific.com>
parents: 38357
diff changeset
507
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
508 def test_strip(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
509 self.checkequal('hello', ' hello ', 'strip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
510 self.checkequal('hello ', ' hello ', 'lstrip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
511 self.checkequal(' hello', ' hello ', 'rstrip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
512 self.checkequal('hello', 'hello', 'strip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
513
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
514 # strip/lstrip/rstrip with None arg
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
515 self.checkequal('hello', ' hello ', 'strip', None)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
516 self.checkequal('hello ', ' hello ', 'lstrip', None)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
517 self.checkequal(' hello', ' hello ', 'rstrip', None)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
518 self.checkequal('hello', 'hello', 'strip', None)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
519
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
520 # strip/lstrip/rstrip with str arg
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
521 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
522 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
523 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
524 self.checkequal('hello', 'hello', 'strip', 'xyz')
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
525
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
526 # strip/lstrip/rstrip with unicode arg
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
527 if test_support.have_unicode:
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
528 self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
529 'strip', unicode('xyz', 'ascii'))
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
530 self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
531 'lstrip', unicode('xyz', 'ascii'))
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
532 self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
533 'rstrip', unicode('xyz', 'ascii'))
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
534 # XXX
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
535 #self.checkequal(unicode('hello', 'ascii'), 'hello',
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
536 # 'strip', unicode('xyz', 'ascii'))
28575
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
537
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
538 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
539 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
540 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
215f1f4851a4 Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents: 28478
diff changeset
541
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
542 def test_ljust(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
543 self.checkequal('abc ', 'abc', 'ljust', 10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
544 self.checkequal('abc ', 'abc', 'ljust', 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
545 self.checkequal('abc', 'abc', 'ljust', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
546 self.checkequal('abc', 'abc', 'ljust', 2)
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30216
diff changeset
547 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
548 self.checkraises(TypeError, 'abc', 'ljust')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
549
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
550 def test_rjust(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
551 self.checkequal(' abc', 'abc', 'rjust', 10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
552 self.checkequal(' abc', 'abc', 'rjust', 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
553 self.checkequal('abc', 'abc', 'rjust', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
554 self.checkequal('abc', 'abc', 'rjust', 2)
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30216
diff changeset
555 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
556 self.checkraises(TypeError, 'abc', 'rjust')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
557
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
558 def test_center(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
559 self.checkequal(' abc ', 'abc', 'center', 10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
560 self.checkequal(' abc ', 'abc', 'center', 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
561 self.checkequal('abc', 'abc', 'center', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
562 self.checkequal('abc', 'abc', 'center', 2)
31021
21a10c3600b5 Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents: 30216
diff changeset
563 self.checkequal('***abc****', 'abc', 'center', 10, '*')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
564 self.checkraises(TypeError, 'abc', 'center')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
565
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
566 def test_swapcase(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
567 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
568
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
569 self.checkraises(TypeError, 'hello', 'swapcase', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
570
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
571 def test_replace(self):
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
572 EQ = self.checkequal
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
573
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
574 # Operations on the empty string
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
575 EQ("", "", "replace", "", "")
38539
b9b036f02207 Re-enable a new empty-string test added during the NFS sprint,
Tim Peters <tim.peters@gmail.com>
parents: 38516
diff changeset
576 EQ("A", "", "replace", "", "A")
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
577 EQ("", "", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
578 EQ("", "", "replace", "A", "A")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
579 EQ("", "", "replace", "", "", 100)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
580 EQ("", "", "replace", "", "", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
581
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
582 # interleave (from=="", 'to' gets inserted everywhere)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
583 EQ("A", "A", "replace", "", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
584 EQ("*A*", "A", "replace", "", "*")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
585 EQ("*1A*1", "A", "replace", "", "*1")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
586 EQ("*-#A*-#", "A", "replace", "", "*-#")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
587 EQ("*-A*-A*-", "AA", "replace", "", "*-")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
588 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
589 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
590 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
591 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
592 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
593 EQ("*-AA", "AA", "replace", "", "*-", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
594 EQ("AA", "AA", "replace", "", "*-", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
595
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
596 # single character deletion (from=="A", to=="")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
597 EQ("", "A", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
598 EQ("", "AAA", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
599 EQ("", "AAA", "replace", "A", "", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
600 EQ("", "AAA", "replace", "A", "", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
601 EQ("", "AAA", "replace", "A", "", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
602 EQ("", "AAA", "replace", "A", "", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
603 EQ("A", "AAA", "replace", "A", "", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
604 EQ("AA", "AAA", "replace", "A", "", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
605 EQ("AAA", "AAA", "replace", "A", "", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
606 EQ("", "AAAAAAAAAA", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
607 EQ("BCD", "ABACADA", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
608 EQ("BCD", "ABACADA", "replace", "A", "", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
609 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
610 EQ("BCD", "ABACADA", "replace", "A", "", 5)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
611 EQ("BCD", "ABACADA", "replace", "A", "", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
612 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
613 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
614 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
615 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
616 EQ("BCD", "ABCAD", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
617 EQ("BCD", "ABCADAA", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
618 EQ("BCD", "BCD", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
619 EQ("*************", "*************", "replace", "A", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
620 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
621
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
622 # substring deletion (from=="the", to=="")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
623 EQ("", "the", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
624 EQ("ater", "theater", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
625 EQ("", "thethe", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
626 EQ("", "thethethethe", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
627 EQ("aaaa", "theatheatheathea", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
628 EQ("that", "that", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
629 EQ("thaet", "thaet", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
630 EQ("here and re", "here and there", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
631 EQ("here and re and re", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
632 "replace", "the", "", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
633 EQ("here and re and re", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
634 "replace", "the", "", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
635 EQ("here and re and re", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
636 "replace", "the", "", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
637 EQ("here and re and re", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
638 "replace", "the", "", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
639 EQ("here and re and there", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
640 "replace", "the", "", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
641 EQ("here and there and there", "here and there and there",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
642 "replace", "the", "", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
643 EQ("here and re and re", "here and there and there", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
644
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
645 EQ("abc", "abc", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
646 EQ("abcdefg", "abcdefg", "replace", "the", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
647
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
648 # substring deletion (from=="bob", to=="")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
649 EQ("bob", "bbobob", "replace", "bob", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
650 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
651 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
652 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
653
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
654 # single character replace in place (len(from)==len(to)==1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
655 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
656 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
657 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
658 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
659 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
660 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
661 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
662 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
663
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
664 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
665 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
666 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
667 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
668 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
669
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
670 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
671
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
672 # substring replace in place (len(from)==len(to) > 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
673 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
674 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
675 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
676 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
677 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
678 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
679 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
680 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
681 EQ("cobob", "bobob", "replace", "bob", "cob")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
682 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
683 EQ("bobob", "bobob", "replace", "bot", "bot")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
684
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
685 # replace single character (len(from)==1, len(to)>1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
686 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
687 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
688 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
689 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
690 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
691 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
692 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
693
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
694 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
695
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
696 # replace substring (len(from)>1, len(to)!=len(from))
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
697 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
698 "replace", "spam", "ham")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
699 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
700 "replace", "spam", "ham", sys.maxint)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
701 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
702 "replace", "spam", "ham", -1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
703 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
704 "replace", "spam", "ham", 4)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
705 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
706 "replace", "spam", "ham", 3)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
707 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
708 "replace", "spam", "ham", 2)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
709 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
710 "replace", "spam", "ham", 1)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
711 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
712 "replace", "spam", "ham", 0)
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
713
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
714 EQ("bobob", "bobobob", "replace", "bobob", "bob")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
715 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
716 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
717
58090
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
718 # Silence Py3k warning
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
719 with test_support.check_warnings():
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
720 ba = buffer('a')
ddf61cd775e0 Issue #7462: Implement the stringlib fast search algorithm for the `rfind`,
Antoine Pitrou <solipsis@pitrou.net>
parents: 57412
diff changeset
721 bb = buffer('b')
39302
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
722 EQ("bbc", "abc", "replace", ba, bb)
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
723 EQ("aac", "abc", "replace", bb, ba)
041eb6231cdc Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38709
diff changeset
724
38280
32ba3a0c6040 We can't leave the checked-in tests broken.
Tim Peters <tim.peters@gmail.com>
parents: 38279
diff changeset
725 #
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
726 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
727 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
728 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
729 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
730 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
731 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
732 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
733 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
734 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
735 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
736 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
737 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
738 self.checkequal('', '', 'replace', '', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
739 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
740 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
741 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
742 # MemoryError due to empty result (platform malloc issue when requesting
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
743 # 0 bytes).
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
744 self.checkequal('', '123', 'replace', '123', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
745 self.checkequal('', '123123', 'replace', '123', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
746 self.checkequal('x', '123x123', 'replace', '123', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
747
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
748 self.checkraises(TypeError, 'hello', 'replace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
749 self.checkraises(TypeError, 'hello', 'replace', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
750 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
751 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
752
38299
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
753 def test_replace_overflow(self):
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
754 # Check for overflow checking on 32 bit machines
41589
37d82bee7a16 Fix those parts in the testsuite that assumed that sys.maxint would cause overflow on x64. Now the testsuite is well behaved on that platform.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41532
diff changeset
755 if sys.maxint != 2147483647 or struct.calcsize("P") > 4:
38299
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
756 return
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
757 A2_16 = "A" * (2**16)
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
758 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
759 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
ae6bbf2f2a41 needforspeed: check for overflow in replace (from Andrew Dalke)
Fredrik Lundh <fredrik@pythonware.com>
parents: 38296
diff changeset
760 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
38279
6860c36f7db7 Added a slew of test for string replace, based various corner cases from
Andrew Dalke <dalke@dalkescientific.com>
parents: 36501
diff changeset
761
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
762 def test_zfill(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
763 self.checkequal('123', '123', 'zfill', 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
764 self.checkequal('123', '123', 'zfill', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
765 self.checkequal('0123', '123', 'zfill', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
766 self.checkequal('+123', '+123', 'zfill', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
767 self.checkequal('+123', '+123', 'zfill', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
768 self.checkequal('+0123', '+123', 'zfill', 5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
769 self.checkequal('-123', '-123', 'zfill', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
770 self.checkequal('-123', '-123', 'zfill', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
771 self.checkequal('-0123', '-123', 'zfill', 5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
772 self.checkequal('000', '', 'zfill', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
773 self.checkequal('34', '34', 'zfill', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
774 self.checkequal('0034', '34', 'zfill', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
775
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
776 self.checkraises(TypeError, '123', 'zfill')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
777
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
778 # XXX alias for py3k forward compatibility
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
779 BaseTest = CommonTest
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45500
diff changeset
780
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
781 class MixinStrUnicodeUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
782 # additional tests that only work for
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
783 # stringlike objects, i.e. str, unicode, UserString
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
784 # (but not the string module)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
785
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
786 def test_islower(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
787 self.checkequal(False, '', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
788 self.checkequal(True, 'a', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
789 self.checkequal(False, 'A', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
790 self.checkequal(False, '\n', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
791 self.checkequal(True, 'abc', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
792 self.checkequal(False, 'aBc', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
793 self.checkequal(True, 'abc\n', 'islower')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
794 self.checkraises(TypeError, 'abc', 'islower', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
795
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
796 def test_isupper(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
797 self.checkequal(False, '', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
798 self.checkequal(False, 'a', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
799 self.checkequal(True, 'A', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
800 self.checkequal(False, '\n', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
801 self.checkequal(True, 'ABC', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
802 self.checkequal(False, 'AbC', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
803 self.checkequal(True, 'ABC\n', 'isupper')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
804 self.checkraises(TypeError, 'abc', 'isupper', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
805
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
806 def test_istitle(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
807 self.checkequal(False, '', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
808 self.checkequal(False, 'a', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
809 self.checkequal(True, 'A', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
810 self.checkequal(False, '\n', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
811 self.checkequal(True, 'A Titlecased Line', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
812 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
813 self.checkequal(True, 'A Titlecased, Line', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
814 self.checkequal(False, 'Not a capitalized String', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
815 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
816 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
817 self.checkequal(False, 'NOT', 'istitle')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
818 self.checkraises(TypeError, 'abc', 'istitle', 42)
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
819
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
820 def test_isspace(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
821 self.checkequal(False, '', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
822 self.checkequal(False, 'a', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
823 self.checkequal(True, ' ', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
824 self.checkequal(True, '\t', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
825 self.checkequal(True, '\r', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
826 self.checkequal(True, '\n', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
827 self.checkequal(True, ' \t\r\n', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
828 self.checkequal(False, ' \t\r\na', 'isspace')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
829 self.checkraises(TypeError, 'abc', 'isspace', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
830
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
831 def test_isalpha(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
832 self.checkequal(False, '', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
833 self.checkequal(True, 'a', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
834 self.checkequal(True, 'A', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
835 self.checkequal(False, '\n', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
836 self.checkequal(True, 'abc', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
837 self.checkequal(False, 'aBc123', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
838 self.checkequal(False, 'abc\n', 'isalpha')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
839 self.checkraises(TypeError, 'abc', 'isalpha', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
840
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
841 def test_isalnum(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
842 self.checkequal(False, '', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
843 self.checkequal(True, 'a', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
844 self.checkequal(True, 'A', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
845 self.checkequal(False, '\n', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
846 self.checkequal(True, '123abc456', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
847 self.checkequal(True, 'a1b3c', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
848 self.checkequal(False, 'aBc000 ', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
849 self.checkequal(False, 'abc\n', 'isalnum')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
850 self.checkraises(TypeError, 'abc', 'isalnum', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
851
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
852 def test_isdigit(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
853 self.checkequal(False, '', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
854 self.checkequal(False, 'a', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
855 self.checkequal(True, '0', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
856 self.checkequal(True, '0123456789', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
857 self.checkequal(False, '0123456789a', 'isdigit')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
858
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
859 self.checkraises(TypeError, 'abc', 'isdigit', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
860
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
861 def test_title(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
862 self.checkequal(' Hello ', ' hello ', 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
863 self.checkequal('Hello ', 'hello ', 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
864 self.checkequal('Hello ', 'Hello ', 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
865 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
866 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
867 self.checkequal('Getint', "getInt", 'title')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
868 self.checkraises(TypeError, 'hello', 'title', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
869
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
870 def test_splitlines(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
871 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
872 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
873 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
874 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
875 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
876 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
877 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
878
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
879 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
880
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
881 def test_startswith(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
882 self.checkequal(True, 'hello', 'startswith', 'he')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
883 self.checkequal(True, 'hello', 'startswith', 'hello')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
884 self.checkequal(False, 'hello', 'startswith', 'hello world')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
885 self.checkequal(True, 'hello', 'startswith', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
886 self.checkequal(False, 'hello', 'startswith', 'ello')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
887 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
888 self.checkequal(True, 'hello', 'startswith', 'o', 4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
889 self.checkequal(False, 'hello', 'startswith', 'o', 5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
890 self.checkequal(True, 'hello', 'startswith', '', 5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
891 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
892 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
893 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
894 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
895
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
896 # test negative indices
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
897 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
898 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
899 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
900 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
901 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
902 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
903 self.checkequal(False, 'hello', 'startswith', 'o', -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
904 self.checkequal(True, 'hello', 'startswith', 'o', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
905 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
906 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
907
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
908 self.checkraises(TypeError, 'hello', 'startswith')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
909 self.checkraises(TypeError, 'hello', 'startswith', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
910
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
911 # test tuple arguments
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
912 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
913 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
914 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
915 self.checkequal(False, 'hello', 'startswith', ())
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
916 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
917 'rld', 'lowo'), 3)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
918 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
919 'rld'), 3)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
920 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
921 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
922 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
923
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
924 self.checkraises(TypeError, 'hello', 'startswith', (42,))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
925
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
926 def test_endswith(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
927 self.checkequal(True, 'hello', 'endswith', 'lo')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
928 self.checkequal(False, 'hello', 'endswith', 'he')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
929 self.checkequal(True, 'hello', 'endswith', '')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
930 self.checkequal(False, 'hello', 'endswith', 'hello world')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
931 self.checkequal(False, 'helloworld', 'endswith', 'worl')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
932 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
933 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
934 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
935 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
936 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
937 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
938 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
939 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
940 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
941
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
942 # test negative indices
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
943 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
944 self.checkequal(False, 'hello', 'endswith', 'he', -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
945 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
946 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
947 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
948 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
949 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
950 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
951 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
952 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
953 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
954 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
955 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
956
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
957 self.checkraises(TypeError, 'hello', 'endswith')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
958 self.checkraises(TypeError, 'hello', 'endswith', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
959
38697
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
960 # test tuple arguments
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
961 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
962 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
963 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
964 self.checkequal(False, 'hello', 'endswith', ())
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
965 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
966 'rld', 'lowo'), 3)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
967 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
968 'rld'), 3, -1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
969 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
970 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
971 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
972
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
973 self.checkraises(TypeError, 'hello', 'endswith', (42,))
1cafbcd7f55c RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents: 38539
diff changeset
974
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
975 def test___contains__(self):
58474
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
976 self.checkequal(True, '', '__contains__', '')
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
977 self.checkequal(True, 'abc', '__contains__', '')
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
978 self.checkequal(False, 'abc', '__contains__', '\0')
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
979 self.checkequal(True, '\0abc', '__contains__', '\0')
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
980 self.checkequal(True, 'abc\0', '__contains__', '\0')
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
981 self.checkequal(True, '\0abc', '__contains__', 'a')
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
982 self.checkequal(True, 'asdf', '__contains__', 'asdf')
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
983 self.checkequal(False, 'asd', '__contains__', 'asdf')
dab34b60a72e remove archaic functions from test_support
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58098
diff changeset
984 self.checkequal(False, '', '__contains__', 'asdf')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
985
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
986 def test_subscript(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
987 self.checkequal(u'a', 'abc', '__getitem__', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
988 self.checkequal(u'c', 'abc', '__getitem__', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
989 self.checkequal(u'a', 'abc', '__getitem__', 0L)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
990 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
991 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
992 self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
993 self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
994
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
995 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
996
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
997 def test_slice(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
998 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
999 self.checkequal('abc', 'abc', '__getslice__', 0, 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1000 self.checkequal('ab', 'abc', '__getslice__', 0, 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1001 self.checkequal('bc', 'abc', '__getslice__', 1, 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1002 self.checkequal('b', 'abc', '__getslice__', 1, 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1003 self.checkequal('', 'abc', '__getslice__', 2, 2)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1004 self.checkequal('', 'abc', '__getslice__', 1000, 1000)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1005 self.checkequal('', 'abc', '__getslice__', 2000, 1000)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1006 self.checkequal('', 'abc', '__getslice__', 2, 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1007
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1008 self.checkraises(TypeError, 'abc', '__getslice__', 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1009
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1010 def test_extended_getslice(self):
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1011 # Test extended slicing by comparing with list slicing.
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1012 s = string.ascii_letters + string.digits
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1013 indices = (0, None, 1, 3, 41, -1, -2, -37)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1014 for start in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1015 for stop in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1016 # Skip step 0 (invalid)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1017 for step in indices[1:]:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1018 L = list(s)[start:stop:step]
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1019 self.checkequal(u"".join(L), s, '__getitem__',
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1020 slice(start, stop, step))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42024
diff changeset
1021
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1022 def test_mul(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1023 self.checkequal('', 'abc', '__mul__', -1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1024 self.checkequal('', 'abc', '__mul__', 0)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1025 self.checkequal('abc', 'abc', '__mul__', 1)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1026 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1027 self.checkraises(TypeError, 'abc', '__mul__')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1028 self.checkraises(TypeError, 'abc', '__mul__', '')
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35758
diff changeset
1029 # XXX: on a 64-bit system, this doesn't raise an overflow error,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35758
diff changeset
1030 # but either raises a MemoryError, or succeeds (if you have 54TiB)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35758
diff changeset
1031 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1032
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1033 def test_join(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1034 # join now works with any sequence type
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1035 # moved here, because the argument order is
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1036 # different in string.join (see the test in
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1037 # test.test_string.StringTest.test_join)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1038 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1039 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
38709
67450db26f81 Apply perky's fix for #1503157: "/".join([u"", u""]) raising OverflowError.
Georg Brandl <georg@python.org>
parents: 38697
diff changeset
1040 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
67450db26f81 Apply perky's fix for #1503157: "/".join([u"", u""]) raising OverflowError.
Georg Brandl <georg@python.org>
parents: 38697
diff changeset
1041 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1042 self.checkequal('w x y z', ' ', 'join', Sequence())
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1043 self.checkequal('abc', 'a', 'join', ('abc',))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1044 self.checkequal('z', 'a', 'join', UserList(['z']))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1045 if test_support.have_unicode:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1046 self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1047 self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1048 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1049 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1050 self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1051 for i in [5, 25, 125]:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1052 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1053 ['a' * i] * i)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1054 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1055 ('a' * i,) * i)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1056
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1057 self.checkraises(TypeError, ' ', 'join', BadSeq1())
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1058 self.checkequal('a b c', ' ', 'join', BadSeq2())
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1059
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1060 self.checkraises(TypeError, ' ', 'join')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1061 self.checkraises(TypeError, ' ', 'join', 7)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1062 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
35758
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1063 try:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1064 def f():
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1065 yield 4 + ""
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1066 self.fixtype(' ').join(f())
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1067 except TypeError, e:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1068 if '+' not in str(e):
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1069 self.fail('join() ate exception message')
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1070 else:
e0e320f36c93 Fix bug:
Michael W. Hudson <mwh@python.net>
parents: 35287
diff changeset
1071 self.fail('exception not raised')
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1072
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1073 def test_formatting(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1074 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1075 self.checkequal('+10+', '+%d+', '__mod__', 10)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1076 self.checkequal('a', "%c", '__mod__', "a")
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1077 self.checkequal('a', "%c", '__mod__', "a")
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1078 self.checkequal('"', "%c", '__mod__', 34)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1079 self.checkequal('$', "%c", '__mod__', 36)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1080 self.checkequal('10', "%d", '__mod__', 10)
28478
8802de0da533 Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents: 28407
diff changeset
1081 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1082
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1083 for ordinal in (-100, 0x200000):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1084 # unicode raises ValueError, str raises OverflowError
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1085 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1086
45500
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1087 longvalue = sys.maxint + 10L
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1088 slongvalue = str(longvalue)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1089 if slongvalue[-1] in ("L","l"): slongvalue = slongvalue[:-1]
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1090 self.checkequal(' 42', '%3ld', '__mod__', 42)
45500
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1091 self.checkequal('42', '%d', '__mod__', 42L)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1092 self.checkequal('42', '%d', '__mod__', 42.0)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1093 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1094 self.checkcall('%d', '__mod__', float(longvalue))
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1095 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
30216
7ab1a41f16da SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents: 28774
diff changeset
1096 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1097
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1098 self.checkraises(TypeError, 'abc', '__mod__')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1099 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1100 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1101 self.checkraises(TypeError, '%c', '__mod__', (None,))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1102 self.checkraises(ValueError, '%(foo', '__mod__', {})
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1103 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
45500
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1104 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
c03aff6d07f5 Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents: 43886
diff changeset
1105 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int/long conversion provided
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1106
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1107 # argument names with properly nested brackets are supported
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1108 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1109
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1110 # 100 is a magic number in PyUnicode_Format, this forces a resize
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1111 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1112
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1113 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1114 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1115 self.checkraises(ValueError, '%10', '__mod__', (42,))
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1116
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1117 def test_floatformatting(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1118 # float formatting
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1119 for prec in xrange(100):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1120 format = '%%.%if' % prec
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1121 value = 0.01
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1122 for x in xrange(60):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1123 value = value * 3.141592655 / 3.0 * 10.0
57412
808f9c0d4ac4 Remove restriction on precision when formatting floats. This is the
Mark Dickinson <dickinsm@gmail.com>
parents: 49308
diff changeset
1124 self.checkcall(format, "__mod__", value)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1125
38296
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1126 def test_inplace_rewrites(self):
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1127 # Check that strings don't copy and modify cached single-character strings
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1128 self.checkequal('a', 'A', 'lower')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1129 self.checkequal(True, 'A', 'isupper')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1130 self.checkequal('A', 'a', 'upper')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1131 self.checkequal(True, 'a', 'islower')
38327
e9eb526b7111 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38299
diff changeset
1132
38296
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1133 self.checkequal('a', 'A', 'replace', 'A', 'a')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1134 self.checkequal(True, 'A', 'isupper')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1135
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1136 self.checkequal('A', 'a', 'capitalize')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1137 self.checkequal(True, 'a', 'islower')
38327
e9eb526b7111 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38299
diff changeset
1138
38296
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1139 self.checkequal('A', 'a', 'swapcase')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1140 self.checkequal(True, 'a', 'islower')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1141
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1142 self.checkequal('A', 'a', 'title')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1143 self.checkequal(True, 'a', 'islower')
ba713d31d00c Added tests for implementation error we came up with in the need for speed sprint.
Andrew Dalke <dalke@dalkescientific.com>
parents: 38282
diff changeset
1144
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1145 def test_partition(self):
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1146
38391
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1147 self.checkequal(('this is the par', 'ti', 'tion method'),
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1148 'this is the partition method', 'partition', 'ti')
38335
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1149
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1150 # from raymond's original specification
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1151 S = 'http://www.python.org'
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1152 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1153 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1154 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1155 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1156
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1157 self.checkraises(ValueError, S, 'partition', '')
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1158 self.checkraises(TypeError, S, 'partition', None)
f9332ad4287a needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38327
diff changeset
1159
48935
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1160 # mixed use of str and unicode
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1161 self.assertEqual('a/b/c'.partition(u'/'), ('a', '/', 'b/c'))
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1162
38391
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1163 def test_rpartition(self):
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1164
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1165 self.checkequal(('this is the rparti', 'ti', 'on method'),
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1166 'this is the rpartition method', 'rpartition', 'ti')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1167
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1168 # from raymond's original specification
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1169 S = 'http://www.python.org'
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1170 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
39734
cfb77fff0240 Fix endcase for str.rpartition()
Raymond Hettinger <python@rcn.com>
parents: 39302
diff changeset
1171 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
38391
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1172 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1173 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1174
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1175 self.checkraises(ValueError, S, 'rpartition', '')
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1176 self.checkraises(TypeError, S, 'rpartition', None)
78771d673b4b needspeed: rpartition documentation, tests, and a bug fixes.
Fredrik Lundh <fredrik@pythonware.com>
parents: 38373
diff changeset
1177
48935
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1178 # mixed use of str and unicode
b5365f34640f Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46107
diff changeset
1179 self.assertEqual('a/b/c'.rpartition(u'/'), ('a/b', '/', 'c'))
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1180
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1181 class MixinStrStringUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1182 # Additional tests for 8bit strings, i.e. str, UserString and
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1183 # the string module
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1184
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1185 def test_maketrans(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1186 self.assertEqual(
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1187 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1188 string.maketrans('abc', 'xyz')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1189 )
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1190 self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1191
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1192 def test_translate(self):
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1193 table = string.maketrans('abc', 'xyz')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1194 self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1195
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1196 table = string.maketrans('a', 'A')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1197 self.checkequal('Abc', 'abc', 'translate', table)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1198 self.checkequal('xyz', 'xyz', 'translate', table)
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1199 self.checkequal('yz', 'xyz', 'translate', table, 'x')
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41440
diff changeset
1200 self.checkequal('yx', 'zyzzx', 'translate', None, 'z')
41440
cedc8d7d1c7f SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents: 39734
diff changeset
1201 self.checkequal('zyzzx', 'zyzzx', 'translate', None, '')
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41440
diff changeset
1202 self.checkequal('zyzzx', 'zyzzx', 'translate', None)
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1203 self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1204 self.checkraises(ValueError, 'xyz', 'translate', 'too short')
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1205
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1206
28009
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1207 class MixinStrUserStringTest:
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1208 # Additional tests that only work with
fe10ece8ba68 Port all string tests to PyUnit and share as much tests
Walter Dörwald <walter@livinglogic.de>
parents: 26625
diff changeset
1209 # 8bit compatible object, i.e. str and UserString
13517
4f110441db39 factor out test definitions to string_tests module
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1210
35287
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1211 if test_support.have_unicode:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1212 def test_encoding_decoding(self):
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1213 codecs = [('rot13', 'uryyb jbeyq'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1214 ('base64', 'aGVsbG8gd29ybGQ=\n'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1215 ('hex', '68656c6c6f20776f726c64'),
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1216 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1217 for encoding, data in codecs:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1218 self.checkequal(data, 'hello world', 'encode', encoding)
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1219 self.checkequal('hello world', data, 'decode', encoding)
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1220 # zlib is optional, so we make the test optional too...
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1221 try:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1222 import zlib
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1223 except ImportError:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1224 pass
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1225 else:
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1226 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1227 self.checkequal(data, 'hello world', 'encode', 'zlib')
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1228 self.checkequal('hello world', data, 'decode', 'zlib')
28407
632a6cae0be6 Add two tests for simple error cases.
Walter Dörwald <walter@livinglogic.de>
parents: 28033
diff changeset
1229
35287
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1230 self.checkraises(TypeError, 'xyz', 'decode', 42)
2feedf850789 Disable encoding/decoding test, if unicode is disabled.
Walter Dörwald <walter@livinglogic.de>
parents: 34621
diff changeset
1231 self.checkraises(TypeError, 'xyz', 'encode', 42)
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1232
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1233
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1234 class MixinStrUnicodeTest:
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1235 # Additional tests that only work with str and unicode.
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1236
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1237 def test_bug1001011(self):
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1238 # Make sure join returns a NEW object for single item sequences
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1239 # involving a subclass.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1240 # Make sure that it is of the appropriate type.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1241 # Check the optimisation still occurs for standard objects.
33298
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1242 t = self.type2test
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1243 class subclass(t):
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1244 pass
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1245 s1 = subclass("abcd")
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1246 s2 = t().join([s1])
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1247 self.assert_(s1 is not s2)
a37ae7dba814 Move test_bug1001011() to string_tests.MixinStrUnicodeTest so that
Walter Dörwald <walter@livinglogic.de>
parents: 32951
diff changeset
1248 self.assert_(type(s2) is t)
33305
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1249
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1250 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1251 s2 = t().join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1252 self.assert_(s1 is s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1253
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1254 # Should also test mixed-type join.
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1255 if t is unicode:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1256 s1 = subclass("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1257 s2 = "".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1258 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1259 self.assert_(type(s2) is t)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1260
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1261 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1262 s2 = "".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1263 self.assert_(s1 is s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1264
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1265 elif t is str:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1266 s1 = subclass("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1267 s2 = u"".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1268 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1269 self.assert_(type(s2) is unicode) # promotes!
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1270
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1271 s1 = t("abcd")
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1272 s2 = u"".join([s1])
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1273 self.assert_(s1 is not s2)
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1274 self.assert_(type(s2) is unicode) # promotes!
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1275
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1276 else:
78b578c46770 test_bug1001011(): Verify that
Tim Peters <tim.peters@gmail.com>
parents: 33298
diff changeset
1277 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)