blob: 049929c9a2a6ab44155f268e8256f93810c8ba27 [file] [log] [blame]
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001"""
2Common tests shared by test_str, test_unicode, test_userstring and test_string.
3"""
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00004
Guido van Rossum360e4b82007-05-14 22:51:27 +00005import unittest, string, sys, struct
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Raymond Hettinger53dbe392008-02-12 20:03:09 +00007from collections import UserList
Jeremy Hylton20f41b62000-07-11 03:31:55 +00008
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00009class Sequence:
Walter Dörwald0fd583c2003-02-21 12:53:50 +000010 def __init__(self, seq='wxyz'): self.seq = seq
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000011 def __len__(self): return len(self.seq)
12 def __getitem__(self, i): return self.seq[i]
13
14class BadSeq1(Sequence):
Guido van Rossume2a383d2007-01-15 16:59:06 +000015 def __init__(self): self.seq = [7, 'hello', 123]
Guido van Rossumf1044292007-09-27 18:01:22 +000016 def __str__(self): return '{0} {1} {2}'.format(*self.seq)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000017
18class BadSeq2(Sequence):
19 def __init__(self): self.seq = ['a', 'b', 'c']
20 def __len__(self): return 8
21
Georg Brandlc7885542007-03-06 19:16:20 +000022class BaseTest(unittest.TestCase):
23 # These tests are for buffers of values (bytes) and not
24 # specific to character interpretation, used for bytes objects
25 # and various string implementations
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000026
Walter Dörwald0fd583c2003-02-21 12:53:50 +000027 # The type to be tested
28 # Change in subclasses to change the behaviour of fixtesttype()
29 type2test = None
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000030
Walter Dörwald0fd583c2003-02-21 12:53:50 +000031 # All tests pass their arguments to the testing methods
32 # as str objects. fixtesttype() can be used to propagate
33 # these arguments to the appropriate type
34 def fixtype(self, obj):
35 if isinstance(obj, str):
36 return self.__class__.type2test(obj)
37 elif isinstance(obj, list):
38 return [self.fixtype(x) for x in obj]
39 elif isinstance(obj, tuple):
40 return tuple([self.fixtype(x) for x in obj])
41 elif isinstance(obj, dict):
42 return dict([
43 (self.fixtype(key), self.fixtype(value))
Guido van Rossumcc2b0162007-02-11 06:12:03 +000044 for (key, value) in obj.items()
Walter Dörwald0fd583c2003-02-21 12:53:50 +000045 ])
46 else:
47 return obj
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000048
Guido van Rossum09549f42007-08-27 20:40:10 +000049 # check that obj.method(*args) returns result
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +010050 def checkequal(self, result, obj, methodname, *args, **kwargs):
Walter Dörwald0fd583c2003-02-21 12:53:50 +000051 result = self.fixtype(result)
Guido van Rossum09549f42007-08-27 20:40:10 +000052 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000053 args = self.fixtype(args)
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +010054 kwargs = self.fixtype(kwargs)
55 realresult = getattr(obj, methodname)(*args, **kwargs)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000056 self.assertEqual(
57 result,
58 realresult
59 )
60 # if the original is returned make sure that
61 # this doesn't happen with subclasses
Guido van Rossum09549f42007-08-27 20:40:10 +000062 if obj is realresult:
63 try:
64 class subtype(self.__class__.type2test):
65 pass
66 except TypeError:
67 pass # Skip this if we can't subclass
68 else:
69 obj = subtype(obj)
70 realresult = getattr(obj, methodname)(*args)
Ezio Melottib3aedd42010-11-20 19:04:17 +000071 self.assertIsNot(obj, realresult)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000072
Guido van Rossum09549f42007-08-27 20:40:10 +000073 # check that obj.method(*args) raises exc
74 def checkraises(self, exc, obj, methodname, *args):
75 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000076 args = self.fixtype(args)
77 self.assertRaises(
78 exc,
Guido van Rossum09549f42007-08-27 20:40:10 +000079 getattr(obj, methodname),
Walter Dörwald0fd583c2003-02-21 12:53:50 +000080 *args
81 )
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000082
Guido van Rossum09549f42007-08-27 20:40:10 +000083 # call obj.method(*args) without any checks
84 def checkcall(self, obj, methodname, *args):
85 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000086 args = self.fixtype(args)
Guido van Rossum09549f42007-08-27 20:40:10 +000087 getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000088
Walter Dörwald0fd583c2003-02-21 12:53:50 +000089 def test_count(self):
90 self.checkequal(3, 'aaa', 'count', 'a')
91 self.checkequal(0, 'aaa', 'count', 'b')
92 self.checkequal(3, 'aaa', 'count', 'a')
93 self.checkequal(0, 'aaa', 'count', 'b')
94 self.checkequal(3, 'aaa', 'count', 'a')
95 self.checkequal(0, 'aaa', 'count', 'b')
96 self.checkequal(0, 'aaa', 'count', 'b')
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000097 self.checkequal(2, 'aaa', 'count', 'a', 1)
98 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000099 self.checkequal(1, 'aaa', 'count', 'a', -1)
100 self.checkequal(3, 'aaa', 'count', 'a', -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000101 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
102 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000103 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
104 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000105 self.checkequal(3, 'aaa', 'count', '', 1)
106 self.checkequal(1, 'aaa', 'count', '', 3)
107 self.checkequal(0, 'aaa', 'count', '', 10)
108 self.checkequal(2, 'aaa', 'count', '', -1)
109 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000110
Amaury Forgeot d'Arcf2e93682008-09-26 22:48:41 +0000111 self.checkequal(1, '', 'count', '')
112 self.checkequal(0, '', 'count', '', 1, 1)
113 self.checkequal(0, '', 'count', '', sys.maxsize, 0)
114
115 self.checkequal(0, '', 'count', 'xx')
116 self.checkequal(0, '', 'count', 'xx', 1, 1)
117 self.checkequal(0, '', 'count', 'xx', sys.maxsize, 0)
118
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000119 self.checkraises(TypeError, 'hello', 'count')
120 self.checkraises(TypeError, 'hello', 'count', 42)
121
Raymond Hettinger57e74472005-02-20 09:54:53 +0000122 # For a variety of combinations,
123 # verify that str.count() matches an equivalent function
124 # replacing all occurrences and then differencing the string lengths
125 charset = ['', 'a', 'b']
126 digits = 7
127 base = len(charset)
128 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000129 for i in range(base ** digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000130 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000131 for j in range(digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000132 i, m = divmod(i, base)
133 entry.append(charset[m])
134 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000135 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger57e74472005-02-20 09:54:53 +0000136 for i in teststrings:
Raymond Hettinger57e74472005-02-20 09:54:53 +0000137 n = len(i)
138 for j in teststrings:
139 r1 = i.count(j)
140 if j:
Guido van Rossum09549f42007-08-27 20:40:10 +0000141 r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
142 len(j))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000143 else:
144 r2, rem = len(i)+1, 0
145 if rem or r1 != r2:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000146 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
147 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000148
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000149 def test_find(self):
150 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
151 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
152 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
153
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000154 self.checkequal(0, 'abc', 'find', '', 0)
155 self.checkequal(3, 'abc', 'find', '', 3)
156 self.checkequal(-1, 'abc', 'find', '', 4)
157
Christian Heimes9cd17752007-11-18 19:35:23 +0000158 # to check the ability to pass None as defaults
159 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
160 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
161 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
162 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
163 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
164
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000165 self.checkraises(TypeError, 'hello', 'find')
166 self.checkraises(TypeError, 'hello', 'find', 42)
167
Amaury Forgeot d'Arcf2e93682008-09-26 22:48:41 +0000168 self.checkequal(0, '', 'find', '')
169 self.checkequal(-1, '', 'find', '', 1, 1)
170 self.checkequal(-1, '', 'find', '', sys.maxsize, 0)
171
172 self.checkequal(-1, '', 'find', 'xx')
173 self.checkequal(-1, '', 'find', 'xx', 1, 1)
174 self.checkequal(-1, '', 'find', 'xx', sys.maxsize, 0)
175
Antoine Pitrou74edda02010-01-02 21:51:33 +0000176 # issue 7458
177 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
178
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000179 # For a variety of combinations,
180 # verify that str.find() matches __contains__
181 # and that the found substring is really at that location
182 charset = ['', 'a', 'b', 'c']
183 digits = 5
184 base = len(charset)
185 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000186 for i in range(base ** digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000187 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000188 for j in range(digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000189 i, m = divmod(i, base)
190 entry.append(charset[m])
191 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000192 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000193 for i in teststrings:
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000194 for j in teststrings:
195 loc = i.find(j)
196 r1 = (loc != -1)
197 r2 = j in i
Antoine Pitrou2e544fb2010-01-02 21:55:17 +0000198 self.assertEqual(r1, r2)
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000199 if loc != -1:
200 self.assertEqual(i[loc:loc+len(j)], j)
201
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000202 def test_rfind(self):
203 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
204 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
205 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
206 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
207
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000208 self.checkequal(3, 'abc', 'rfind', '', 0)
209 self.checkequal(3, 'abc', 'rfind', '', 3)
210 self.checkequal(-1, 'abc', 'rfind', '', 4)
211
Christian Heimes9cd17752007-11-18 19:35:23 +0000212 # to check the ability to pass None as defaults
213 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
214 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
215 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
216 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
217 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
218
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000219 self.checkraises(TypeError, 'hello', 'rfind')
220 self.checkraises(TypeError, 'hello', 'rfind', 42)
221
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000222 # For a variety of combinations,
223 # verify that str.rfind() matches __contains__
224 # and that the found substring is really at that location
225 charset = ['', 'a', 'b', 'c']
226 digits = 5
227 base = len(charset)
228 teststrings = set()
229 for i in range(base ** digits):
230 entry = []
231 for j in range(digits):
232 i, m = divmod(i, base)
233 entry.append(charset[m])
234 teststrings.add(''.join(entry))
235 teststrings = [self.fixtype(ts) for ts in teststrings]
236 for i in teststrings:
237 for j in teststrings:
238 loc = i.rfind(j)
239 r1 = (loc != -1)
240 r2 = j in i
Antoine Pitrou2e544fb2010-01-02 21:55:17 +0000241 self.assertEqual(r1, r2)
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000242 if loc != -1:
243 self.assertEqual(i[loc:loc+len(j)], j)
244
Antoine Pitrou74edda02010-01-02 21:51:33 +0000245 # issue 7458
246 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
247
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000248 def test_index(self):
249 self.checkequal(0, 'abcdefghiabc', 'index', '')
250 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
251 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
252 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
253
254 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
255 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
256 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
257 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
258
Christian Heimes9cd17752007-11-18 19:35:23 +0000259 # to check the ability to pass None as defaults
260 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
261 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
262 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
263 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
264 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
265
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000266 self.checkraises(TypeError, 'hello', 'index')
267 self.checkraises(TypeError, 'hello', 'index', 42)
268
269 def test_rindex(self):
270 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
271 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
272 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
273 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
274
275 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
276 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
277 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
278 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
279 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
280
Christian Heimes9cd17752007-11-18 19:35:23 +0000281 # to check the ability to pass None as defaults
282 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
283 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
284 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
285 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
286 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
287
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000288 self.checkraises(TypeError, 'hello', 'rindex')
289 self.checkraises(TypeError, 'hello', 'rindex', 42)
290
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000291 def test_lower(self):
292 self.checkequal('hello', 'HeLLo', 'lower')
293 self.checkequal('hello', 'hello', 'lower')
294 self.checkraises(TypeError, 'hello', 'lower', 42)
295
296 def test_upper(self):
297 self.checkequal('HELLO', 'HeLLo', 'upper')
298 self.checkequal('HELLO', 'HELLO', 'upper')
299 self.checkraises(TypeError, 'hello', 'upper', 42)
300
301 def test_expandtabs(self):
302 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
303 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
304 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
305 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
306 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
307 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
308 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
309 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
310
311 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
312 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000313 if sys.maxsize < (1 << 32) and struct.calcsize('P') == 4:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000314 self.checkraises(OverflowError,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000315 '\ta\n\tb', 'expandtabs', sys.maxsize)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000316
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000317 def test_split(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000318 # by a char
319 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000320 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000321 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
322 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
323 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
324 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000326 sys.maxsize-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000327 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
328 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
329 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000330 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
331 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000332 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
333
Thomas Wouters477c8d52006-05-27 19:21:47 +0000334 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
335 self.checkequal(['a']*15 +['a|a|a|a|a'],
336 ('a|'*20)[:-1], 'split', '|', 15)
337
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000338 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000339 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000340 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
341 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
342 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
343 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000345 sys.maxsize-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000346 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
347 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000348 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000349 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
350 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
351 'split', 'test')
352 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
353 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
354 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
355 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
356 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
357 self.checkequal([''], '', 'split', 'aaa')
358 self.checkequal(['aa'], 'aa', 'split', 'aaa')
359 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
360 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
361
362 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
363 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
364 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
365 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000366
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000367 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000368 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
369
Thomas Wouters477c8d52006-05-27 19:21:47 +0000370 # null case
371 self.checkraises(ValueError, 'hello', 'split', '')
372 self.checkraises(ValueError, 'hello', 'split', '', 0)
373
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000374 def test_rsplit(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000375 # by a char
376 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
377 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
378 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
379 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
380 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000381 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000382 sys.maxsize-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000383 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
384 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
385 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
387 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
388
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000389 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
390
Thomas Wouters477c8d52006-05-27 19:21:47 +0000391 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
392 self.checkequal(['a|a|a|a|a']+['a']*15,
393 ('a|'*20)[:-1], 'rsplit', '|', 15)
394
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000395 # by string
396 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
397 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
398 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
399 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
400 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000401 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000402 sys.maxsize-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000403 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
404 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
405 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000406 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
407 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
408 'rsplit', 'test')
409 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
410 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
411 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
412 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
413 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
414 self.checkequal([''], '', 'rsplit', 'aaa')
415 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
416 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
417 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
418
419 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
420 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
421 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
422 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000423
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000424 # argument type
425 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000426
Thomas Wouters477c8d52006-05-27 19:21:47 +0000427 # null case
428 self.checkraises(ValueError, 'hello', 'rsplit', '')
429 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
430
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000431 def test_replace(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000432 EQ = self.checkequal
433
434 # Operations on the empty string
435 EQ("", "", "replace", "", "")
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000436 EQ("A", "", "replace", "", "A")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000437 EQ("", "", "replace", "A", "")
438 EQ("", "", "replace", "A", "A")
439 EQ("", "", "replace", "", "", 100)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000440 EQ("", "", "replace", "", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000441
442 # interleave (from=="", 'to' gets inserted everywhere)
443 EQ("A", "A", "replace", "", "")
444 EQ("*A*", "A", "replace", "", "*")
445 EQ("*1A*1", "A", "replace", "", "*1")
446 EQ("*-#A*-#", "A", "replace", "", "*-#")
447 EQ("*-A*-A*-", "AA", "replace", "", "*-")
448 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000449 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000450 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
451 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
452 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
453 EQ("*-AA", "AA", "replace", "", "*-", 1)
454 EQ("AA", "AA", "replace", "", "*-", 0)
455
456 # single character deletion (from=="A", to=="")
457 EQ("", "A", "replace", "A", "")
458 EQ("", "AAA", "replace", "A", "")
459 EQ("", "AAA", "replace", "A", "", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000460 EQ("", "AAA", "replace", "A", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000461 EQ("", "AAA", "replace", "A", "", 4)
462 EQ("", "AAA", "replace", "A", "", 3)
463 EQ("A", "AAA", "replace", "A", "", 2)
464 EQ("AA", "AAA", "replace", "A", "", 1)
465 EQ("AAA", "AAA", "replace", "A", "", 0)
466 EQ("", "AAAAAAAAAA", "replace", "A", "")
467 EQ("BCD", "ABACADA", "replace", "A", "")
468 EQ("BCD", "ABACADA", "replace", "A", "", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000469 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000470 EQ("BCD", "ABACADA", "replace", "A", "", 5)
471 EQ("BCD", "ABACADA", "replace", "A", "", 4)
472 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
473 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
474 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
475 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
476 EQ("BCD", "ABCAD", "replace", "A", "")
477 EQ("BCD", "ABCADAA", "replace", "A", "")
478 EQ("BCD", "BCD", "replace", "A", "")
479 EQ("*************", "*************", "replace", "A", "")
480 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
481
482 # substring deletion (from=="the", to=="")
483 EQ("", "the", "replace", "the", "")
484 EQ("ater", "theater", "replace", "the", "")
485 EQ("", "thethe", "replace", "the", "")
486 EQ("", "thethethethe", "replace", "the", "")
487 EQ("aaaa", "theatheatheathea", "replace", "the", "")
488 EQ("that", "that", "replace", "the", "")
489 EQ("thaet", "thaet", "replace", "the", "")
490 EQ("here and re", "here and there", "replace", "the", "")
491 EQ("here and re and re", "here and there and there",
Christian Heimesa37d4c62007-12-04 23:02:19 +0000492 "replace", "the", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000493 EQ("here and re and re", "here and there and there",
494 "replace", "the", "", -1)
495 EQ("here and re and re", "here and there and there",
496 "replace", "the", "", 3)
497 EQ("here and re and re", "here and there and there",
498 "replace", "the", "", 2)
499 EQ("here and re and there", "here and there and there",
500 "replace", "the", "", 1)
501 EQ("here and there and there", "here and there and there",
502 "replace", "the", "", 0)
503 EQ("here and re and re", "here and there and there", "replace", "the", "")
504
505 EQ("abc", "abc", "replace", "the", "")
506 EQ("abcdefg", "abcdefg", "replace", "the", "")
507
508 # substring deletion (from=="bob", to=="")
509 EQ("bob", "bbobob", "replace", "bob", "")
510 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
511 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
512 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
513
514 # single character replace in place (len(from)==len(to)==1)
515 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
516 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
Christian Heimesa37d4c62007-12-04 23:02:19 +0000517 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000518 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
519 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
520 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
521 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
522 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
523
524 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
525 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
526 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
527 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
528 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
529
530 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
531
532 # substring replace in place (len(from)==len(to) > 1)
533 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
Christian Heimesa37d4c62007-12-04 23:02:19 +0000534 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000535 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
536 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
537 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
538 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
539 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
540 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
541 EQ("cobob", "bobob", "replace", "bob", "cob")
542 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
543 EQ("bobob", "bobob", "replace", "bot", "bot")
544
545 # replace single character (len(from)==1, len(to)>1)
546 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
547 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000548 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000549 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
550 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
551 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
552 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
553
554 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
555
556 # replace substring (len(from)>1, len(to)!=len(from))
557 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
558 "replace", "spam", "ham")
559 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
Christian Heimesa37d4c62007-12-04 23:02:19 +0000560 "replace", "spam", "ham", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000561 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
562 "replace", "spam", "ham", -1)
563 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
564 "replace", "spam", "ham", 4)
565 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
566 "replace", "spam", "ham", 3)
567 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
568 "replace", "spam", "ham", 2)
569 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
570 "replace", "spam", "ham", 1)
571 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
572 "replace", "spam", "ham", 0)
573
574 EQ("bobob", "bobobob", "replace", "bobob", "bob")
575 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
576 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
577
Guido van Rossum39478e82007-08-27 17:23:59 +0000578 # XXX Commented out. Is there any reason to support buffer objects
579 # as arguments for str.replace()? GvR
Guido van Rossum254348e2007-11-21 19:29:53 +0000580## ba = bytearray('a')
581## bb = bytearray('b')
Guido van Rossum39478e82007-08-27 17:23:59 +0000582## EQ("bbc", "abc", "replace", ba, bb)
583## EQ("aac", "abc", "replace", bb, ba)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000584
Thomas Wouters477c8d52006-05-27 19:21:47 +0000585 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000586 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
587 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
588 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
589 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
590 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
591 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
592 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
593 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
594 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
595 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
596 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
597 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
598 self.checkequal('', '', 'replace', '', '')
599 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
600 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
601 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
602 # MemoryError due to empty result (platform malloc issue when requesting
603 # 0 bytes).
604 self.checkequal('', '123', 'replace', '123', '')
605 self.checkequal('', '123123', 'replace', '123', '')
606 self.checkequal('x', '123x123', 'replace', '123', '')
607
608 self.checkraises(TypeError, 'hello', 'replace')
609 self.checkraises(TypeError, 'hello', 'replace', 42)
610 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
611 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
612
Thomas Wouters477c8d52006-05-27 19:21:47 +0000613 def test_replace_overflow(self):
614 # Check for overflow checking on 32 bit machines
Christian Heimesa37d4c62007-12-04 23:02:19 +0000615 if sys.maxsize != 2147483647 or struct.calcsize("P") > 4:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000616 return
617 A2_16 = "A" * (2**16)
618 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
619 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
620 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
621
Georg Brandlc7885542007-03-06 19:16:20 +0000622
623
624class CommonTest(BaseTest):
625 # This testcase contains test that can be used in all
626 # stringlike classes. Currently this is str, unicode
627 # UserString and the string module.
628
629 def test_hash(self):
630 # SF bug 1054139: += optimization was not invalidating cached hash value
631 a = self.type2test('DNSSEC')
632 b = self.type2test('')
633 for c in a:
634 b += c
635 hash(b)
636 self.assertEqual(hash(a), hash(b))
637
638 def test_capitalize(self):
639 self.checkequal(' hello ', ' hello ', 'capitalize')
640 self.checkequal('Hello ', 'Hello ','capitalize')
641 self.checkequal('Hello ', 'hello ','capitalize')
642 self.checkequal('Aaaa', 'aaaa', 'capitalize')
643 self.checkequal('Aaaa', 'AaAa', 'capitalize')
644
Ezio Melottiee8d9982011-08-15 09:09:57 +0300645 # check that titlecased chars are lowered correctly
646 # \u1ffc is the titlecased char
647 self.checkequal('\u1ffc\u1ff3\u1ff3\u1ff3',
648 '\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize')
649 # check with cased non-letter chars
650 self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
651 '\u24c5\u24ce\u24c9\u24bd\u24c4\u24c3', 'capitalize')
652 self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
653 '\u24df\u24e8\u24e3\u24d7\u24de\u24dd', 'capitalize')
654 self.checkequal('\u2160\u2171\u2172',
655 '\u2160\u2161\u2162', 'capitalize')
656 self.checkequal('\u2160\u2171\u2172',
657 '\u2170\u2171\u2172', 'capitalize')
658 # check with Ll chars with no upper - nothing changes here
659 self.checkequal('\u019b\u1d00\u1d86\u0221\u1fb7',
660 '\u019b\u1d00\u1d86\u0221\u1fb7', 'capitalize')
661
Georg Brandlc7885542007-03-06 19:16:20 +0000662 self.checkraises(TypeError, 'hello', 'capitalize', 42)
663
664 def test_lower(self):
665 self.checkequal('hello', 'HeLLo', 'lower')
666 self.checkequal('hello', 'hello', 'lower')
667 self.checkraises(TypeError, 'hello', 'lower', 42)
668
669 def test_upper(self):
670 self.checkequal('HELLO', 'HeLLo', 'upper')
671 self.checkequal('HELLO', 'HELLO', 'upper')
672 self.checkraises(TypeError, 'hello', 'upper', 42)
673
674 def test_expandtabs(self):
675 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
676 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
677 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
678 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
679 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
680 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
681 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
682
683 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
684
685 def test_additional_split(self):
686 self.checkequal(['this', 'is', 'the', 'split', 'function'],
687 'this is the split function', 'split')
688
689 # by whitespace
690 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
691 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
692 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
693 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
694 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
695 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000696 sys.maxsize-1)
Georg Brandlc7885542007-03-06 19:16:20 +0000697 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
698 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
699 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
700
701 self.checkequal([], ' ', 'split')
702 self.checkequal(['a'], ' a ', 'split')
703 self.checkequal(['a', 'b'], ' a b ', 'split')
704 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
705 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
706 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
707 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
708 aaa = ' a '*20
709 self.checkequal(['a']*20, aaa, 'split')
710 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
711 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
712
713 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000714 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000715
716 def test_additional_rsplit(self):
717 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
718 'this is the rsplit function', 'rsplit')
719
720 # by whitespace
721 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
722 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
723 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
724 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
725 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
726 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000727 sys.maxsize-20)
Georg Brandlc7885542007-03-06 19:16:20 +0000728 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
729 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
730 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
731
732 self.checkequal([], ' ', 'rsplit')
733 self.checkequal(['a'], ' a ', 'rsplit')
734 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
735 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
736 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
737 None, 1)
738 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
739 None, 2)
740 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
741 aaa = ' a '*20
742 self.checkequal(['a']*20, aaa, 'rsplit')
743 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
744 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
745
746 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000747 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000748
749 def test_strip(self):
750 self.checkequal('hello', ' hello ', 'strip')
751 self.checkequal('hello ', ' hello ', 'lstrip')
752 self.checkequal(' hello', ' hello ', 'rstrip')
753 self.checkequal('hello', 'hello', 'strip')
754
755 # strip/lstrip/rstrip with None arg
756 self.checkequal('hello', ' hello ', 'strip', None)
757 self.checkequal('hello ', ' hello ', 'lstrip', None)
758 self.checkequal(' hello', ' hello ', 'rstrip', None)
759 self.checkequal('hello', 'hello', 'strip', None)
760
761 # strip/lstrip/rstrip with str arg
762 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
763 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
764 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
765 self.checkequal('hello', 'hello', 'strip', 'xyz')
766
Georg Brandlc7885542007-03-06 19:16:20 +0000767 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
768 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
769 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
770
771 def test_ljust(self):
772 self.checkequal('abc ', 'abc', 'ljust', 10)
773 self.checkequal('abc ', 'abc', 'ljust', 6)
774 self.checkequal('abc', 'abc', 'ljust', 3)
775 self.checkequal('abc', 'abc', 'ljust', 2)
776 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
777 self.checkraises(TypeError, 'abc', 'ljust')
778
779 def test_rjust(self):
780 self.checkequal(' abc', 'abc', 'rjust', 10)
781 self.checkequal(' abc', 'abc', 'rjust', 6)
782 self.checkequal('abc', 'abc', 'rjust', 3)
783 self.checkequal('abc', 'abc', 'rjust', 2)
784 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
785 self.checkraises(TypeError, 'abc', 'rjust')
786
787 def test_center(self):
788 self.checkequal(' abc ', 'abc', 'center', 10)
789 self.checkequal(' abc ', 'abc', 'center', 6)
790 self.checkequal('abc', 'abc', 'center', 3)
791 self.checkequal('abc', 'abc', 'center', 2)
792 self.checkequal('***abc****', 'abc', 'center', 10, '*')
793 self.checkraises(TypeError, 'abc', 'center')
794
795 def test_swapcase(self):
796 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
797
798 self.checkraises(TypeError, 'hello', 'swapcase', 42)
799
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000800 def test_zfill(self):
801 self.checkequal('123', '123', 'zfill', 2)
802 self.checkequal('123', '123', 'zfill', 3)
803 self.checkequal('0123', '123', 'zfill', 4)
804 self.checkequal('+123', '+123', 'zfill', 3)
805 self.checkequal('+123', '+123', 'zfill', 4)
806 self.checkequal('+0123', '+123', 'zfill', 5)
807 self.checkequal('-123', '-123', 'zfill', 3)
808 self.checkequal('-123', '-123', 'zfill', 4)
809 self.checkequal('-0123', '-123', 'zfill', 5)
810 self.checkequal('000', '', 'zfill', 3)
811 self.checkequal('34', '34', 'zfill', 1)
812 self.checkequal('0034', '34', 'zfill', 4)
813
814 self.checkraises(TypeError, '123', 'zfill')
815
816class MixinStrUnicodeUserStringTest:
817 # additional tests that only work for
818 # stringlike objects, i.e. str, unicode, UserString
819 # (but not the string module)
820
821 def test_islower(self):
822 self.checkequal(False, '', 'islower')
823 self.checkequal(True, 'a', 'islower')
824 self.checkequal(False, 'A', 'islower')
825 self.checkequal(False, '\n', 'islower')
826 self.checkequal(True, 'abc', 'islower')
827 self.checkequal(False, 'aBc', 'islower')
828 self.checkequal(True, 'abc\n', 'islower')
829 self.checkraises(TypeError, 'abc', 'islower', 42)
830
831 def test_isupper(self):
832 self.checkequal(False, '', 'isupper')
833 self.checkequal(False, 'a', 'isupper')
834 self.checkequal(True, 'A', 'isupper')
835 self.checkequal(False, '\n', 'isupper')
836 self.checkequal(True, 'ABC', 'isupper')
837 self.checkequal(False, 'AbC', 'isupper')
838 self.checkequal(True, 'ABC\n', 'isupper')
839 self.checkraises(TypeError, 'abc', 'isupper', 42)
840
841 def test_istitle(self):
842 self.checkequal(False, '', 'istitle')
843 self.checkequal(False, 'a', 'istitle')
844 self.checkequal(True, 'A', 'istitle')
845 self.checkequal(False, '\n', 'istitle')
846 self.checkequal(True, 'A Titlecased Line', 'istitle')
847 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
848 self.checkequal(True, 'A Titlecased, Line', 'istitle')
849 self.checkequal(False, 'Not a capitalized String', 'istitle')
850 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
851 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
852 self.checkequal(False, 'NOT', 'istitle')
853 self.checkraises(TypeError, 'abc', 'istitle', 42)
854
855 def test_isspace(self):
856 self.checkequal(False, '', 'isspace')
857 self.checkequal(False, 'a', 'isspace')
858 self.checkequal(True, ' ', 'isspace')
859 self.checkequal(True, '\t', 'isspace')
860 self.checkequal(True, '\r', 'isspace')
861 self.checkequal(True, '\n', 'isspace')
862 self.checkequal(True, ' \t\r\n', 'isspace')
863 self.checkequal(False, ' \t\r\na', 'isspace')
864 self.checkraises(TypeError, 'abc', 'isspace', 42)
865
866 def test_isalpha(self):
867 self.checkequal(False, '', 'isalpha')
868 self.checkequal(True, 'a', 'isalpha')
869 self.checkequal(True, 'A', 'isalpha')
870 self.checkequal(False, '\n', 'isalpha')
871 self.checkequal(True, 'abc', 'isalpha')
872 self.checkequal(False, 'aBc123', 'isalpha')
873 self.checkequal(False, 'abc\n', 'isalpha')
874 self.checkraises(TypeError, 'abc', 'isalpha', 42)
875
876 def test_isalnum(self):
877 self.checkequal(False, '', 'isalnum')
878 self.checkequal(True, 'a', 'isalnum')
879 self.checkequal(True, 'A', 'isalnum')
880 self.checkequal(False, '\n', 'isalnum')
881 self.checkequal(True, '123abc456', 'isalnum')
882 self.checkequal(True, 'a1b3c', 'isalnum')
883 self.checkequal(False, 'aBc000 ', 'isalnum')
884 self.checkequal(False, 'abc\n', 'isalnum')
885 self.checkraises(TypeError, 'abc', 'isalnum', 42)
886
887 def test_isdigit(self):
888 self.checkequal(False, '', 'isdigit')
889 self.checkequal(False, 'a', 'isdigit')
890 self.checkequal(True, '0', 'isdigit')
891 self.checkequal(True, '0123456789', 'isdigit')
892 self.checkequal(False, '0123456789a', 'isdigit')
893
894 self.checkraises(TypeError, 'abc', 'isdigit', 42)
895
896 def test_title(self):
897 self.checkequal(' Hello ', ' hello ', 'title')
898 self.checkequal('Hello ', 'hello ', 'title')
899 self.checkequal('Hello ', 'Hello ', 'title')
900 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
901 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
902 self.checkequal('Getint', "getInt", 'title')
903 self.checkraises(TypeError, 'hello', 'title', 42)
904
905 def test_splitlines(self):
906 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
907 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
908 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
909 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
910 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
911 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
Mark Dickinson0d5f6ad2011-09-24 09:14:39 +0100912 self.checkequal(['', 'abc', 'def', 'ghi', ''],
913 "\nabc\ndef\r\nghi\n\r", 'splitlines', False)
914 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
915 "\nabc\ndef\r\nghi\n\r", 'splitlines', True)
916 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r",
917 'splitlines', keepends=False)
918 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'],
919 "\nabc\ndef\r\nghi\n\r", 'splitlines', keepends=True)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000920
921 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
922
923 def test_startswith(self):
924 self.checkequal(True, 'hello', 'startswith', 'he')
925 self.checkequal(True, 'hello', 'startswith', 'hello')
926 self.checkequal(False, 'hello', 'startswith', 'hello world')
927 self.checkequal(True, 'hello', 'startswith', '')
928 self.checkequal(False, 'hello', 'startswith', 'ello')
929 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
930 self.checkequal(True, 'hello', 'startswith', 'o', 4)
931 self.checkequal(False, 'hello', 'startswith', 'o', 5)
932 self.checkequal(True, 'hello', 'startswith', '', 5)
933 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
934 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
935 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
936 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
937
938 # test negative indices
939 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
940 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
941 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
942 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
943 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
944 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
945 self.checkequal(False, 'hello', 'startswith', 'o', -2)
946 self.checkequal(True, 'hello', 'startswith', 'o', -1)
947 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
948 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
949
950 self.checkraises(TypeError, 'hello', 'startswith')
951 self.checkraises(TypeError, 'hello', 'startswith', 42)
952
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000953 # test tuple arguments
954 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
955 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
956 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
957 self.checkequal(False, 'hello', 'startswith', ())
958 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
959 'rld', 'lowo'), 3)
960 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
961 'rld'), 3)
962 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
963 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
964 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
965
966 self.checkraises(TypeError, 'hello', 'startswith', (42,))
967
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000968 def test_endswith(self):
969 self.checkequal(True, 'hello', 'endswith', 'lo')
970 self.checkequal(False, 'hello', 'endswith', 'he')
971 self.checkequal(True, 'hello', 'endswith', '')
972 self.checkequal(False, 'hello', 'endswith', 'hello world')
973 self.checkequal(False, 'helloworld', 'endswith', 'worl')
974 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
975 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
976 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
977 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
978 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
979 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
980 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
981 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
982 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
983
984 # test negative indices
985 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
986 self.checkequal(False, 'hello', 'endswith', 'he', -2)
987 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
988 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
989 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
990 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
991 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
992 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
993 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
994 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
995 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
996 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
997 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
998
999 self.checkraises(TypeError, 'hello', 'endswith')
1000 self.checkraises(TypeError, 'hello', 'endswith', 42)
1001
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001002 # test tuple arguments
1003 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
1004 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
1005 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
1006 self.checkequal(False, 'hello', 'endswith', ())
1007 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
1008 'rld', 'lowo'), 3)
1009 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
1010 'rld'), 3, -1)
1011 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
1012 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
1013 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
1014
1015 self.checkraises(TypeError, 'hello', 'endswith', (42,))
1016
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001017 def test___contains__(self):
Ezio Melottib19f43d2010-01-24 20:59:24 +00001018 self.checkequal(True, '', '__contains__', '')
1019 self.checkequal(True, 'abc', '__contains__', '')
1020 self.checkequal(False, 'abc', '__contains__', '\0')
1021 self.checkequal(True, '\0abc', '__contains__', '\0')
1022 self.checkequal(True, 'abc\0', '__contains__', '\0')
1023 self.checkequal(True, '\0abc', '__contains__', 'a')
1024 self.checkequal(True, 'asdf', '__contains__', 'asdf')
1025 self.checkequal(False, 'asd', '__contains__', 'asdf')
1026 self.checkequal(False, '', '__contains__', 'asdf')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001027
1028 def test_subscript(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001029 self.checkequal('a', 'abc', '__getitem__', 0)
1030 self.checkequal('c', 'abc', '__getitem__', -1)
1031 self.checkequal('a', 'abc', '__getitem__', 0)
1032 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1033 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1034 self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
1035 self.checkequal('', 'abc', '__getitem__', slice(0, 0))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001036
1037 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1038
1039 def test_slice(self):
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001040 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1041 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1042 self.checkequal('ab', 'abc', '__getitem__', slice(0, 2))
1043 self.checkequal('bc', 'abc', '__getitem__', slice(1, 3))
1044 self.checkequal('b', 'abc', '__getitem__', slice(1, 2))
1045 self.checkequal('', 'abc', '__getitem__', slice(2, 2))
1046 self.checkequal('', 'abc', '__getitem__', slice(1000, 1000))
1047 self.checkequal('', 'abc', '__getitem__', slice(2000, 1000))
1048 self.checkequal('', 'abc', '__getitem__', slice(2, 1))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001049
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001050 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001051
Thomas Woutersed03b412007-08-28 21:37:11 +00001052 def test_extended_getslice(self):
1053 # Test extended slicing by comparing with list slicing.
1054 s = string.ascii_letters + string.digits
1055 indices = (0, None, 1, 3, 41, -1, -2, -37)
1056 for start in indices:
1057 for stop in indices:
1058 # Skip step 0 (invalid)
1059 for step in indices[1:]:
1060 L = list(s)[start:stop:step]
1061 self.checkequal("".join(L), s, '__getitem__',
1062 slice(start, stop, step))
1063
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001064 def test_mul(self):
1065 self.checkequal('', 'abc', '__mul__', -1)
1066 self.checkequal('', 'abc', '__mul__', 0)
1067 self.checkequal('abc', 'abc', '__mul__', 1)
1068 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
1069 self.checkraises(TypeError, 'abc', '__mul__')
1070 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +00001071 # XXX: on a 64-bit system, this doesn't raise an overflow error,
1072 # but either raises a MemoryError, or succeeds (if you have 54TiB)
1073 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001074
1075 def test_join(self):
1076 # join now works with any sequence type
1077 # moved here, because the argument order is
1078 # different in string.join (see the test in
1079 # test.test_string.StringTest.test_join)
1080 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
1081 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001082 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
1083 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001084 self.checkequal('w x y z', ' ', 'join', Sequence())
1085 self.checkequal('abc', 'a', 'join', ('abc',))
1086 self.checkequal('z', 'a', 'join', UserList(['z']))
Walter Dörwald67e83882007-05-05 12:26:27 +00001087 self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
Guido van Rossum98297ee2007-11-06 21:34:58 +00001088 self.assertRaises(TypeError, '.'.join, ['a', 'b', 3])
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001089 for i in [5, 25, 125]:
1090 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1091 ['a' * i] * i)
1092 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1093 ('a' * i,) * i)
1094
Guido van Rossum98297ee2007-11-06 21:34:58 +00001095 #self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001096 self.checkequal('a b c', ' ', 'join', BadSeq2())
1097
1098 self.checkraises(TypeError, ' ', 'join')
1099 self.checkraises(TypeError, ' ', 'join', 7)
Guido van Rossumf1044292007-09-27 18:01:22 +00001100 self.checkraises(TypeError, ' ', 'join', [1, 2, bytes()])
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001101 try:
1102 def f():
1103 yield 4 + ""
1104 self.fixtype(' ').join(f())
Guido van Rossumb940e112007-01-10 16:19:56 +00001105 except TypeError as e:
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001106 if '+' not in str(e):
1107 self.fail('join() ate exception message')
1108 else:
1109 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001110
1111 def test_formatting(self):
1112 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1113 self.checkequal('+10+', '+%d+', '__mod__', 10)
1114 self.checkequal('a', "%c", '__mod__', "a")
1115 self.checkequal('a', "%c", '__mod__', "a")
1116 self.checkequal('"', "%c", '__mod__', 34)
1117 self.checkequal('$', "%c", '__mod__', 36)
1118 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001119 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001120
1121 for ordinal in (-100, 0x200000):
1122 # unicode raises ValueError, str raises OverflowError
1123 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1124
Christian Heimesa612dc02008-02-24 13:08:18 +00001125 longvalue = sys.maxsize + 10
1126 slongvalue = str(longvalue)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001127 self.checkequal(' 42', '%3ld', '__mod__', 42)
Christian Heimesa612dc02008-02-24 13:08:18 +00001128 self.checkequal('42', '%d', '__mod__', 42.0)
1129 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1130 self.checkcall('%d', '__mod__', float(longvalue))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001131 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001132 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001133
1134 self.checkraises(TypeError, 'abc', '__mod__')
1135 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1136 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1137 self.checkraises(TypeError, '%c', '__mod__', (None,))
1138 self.checkraises(ValueError, '%(foo', '__mod__', {})
1139 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
Christian Heimesa612dc02008-02-24 13:08:18 +00001140 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
Mark Dickinson5c2db372009-12-05 20:28:34 +00001141 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int conversion provided
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001142
1143 # argument names with properly nested brackets are supported
1144 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1145
1146 # 100 is a magic number in PyUnicode_Format, this forces a resize
1147 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1148
1149 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1150 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1151 self.checkraises(ValueError, '%10', '__mod__', (42,))
1152
1153 def test_floatformatting(self):
1154 # float formatting
Guido van Rossum805365e2007-05-07 22:24:25 +00001155 for prec in range(100):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001156 format = '%%.%if' % prec
1157 value = 0.01
Guido van Rossum805365e2007-05-07 22:24:25 +00001158 for x in range(60):
Florent Xiclunaa87b3832010-09-13 02:28:18 +00001159 value = value * 3.14159265359 / 3.0 * 10.0
Mark Dickinsonf489caf2009-05-01 11:42:00 +00001160 self.checkcall(format, "__mod__", value)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001161
Thomas Wouters477c8d52006-05-27 19:21:47 +00001162 def test_inplace_rewrites(self):
1163 # Check that strings don't copy and modify cached single-character strings
1164 self.checkequal('a', 'A', 'lower')
1165 self.checkequal(True, 'A', 'isupper')
1166 self.checkequal('A', 'a', 'upper')
1167 self.checkequal(True, 'a', 'islower')
1168
1169 self.checkequal('a', 'A', 'replace', 'A', 'a')
1170 self.checkequal(True, 'A', 'isupper')
1171
1172 self.checkequal('A', 'a', 'capitalize')
1173 self.checkequal(True, 'a', 'islower')
1174
1175 self.checkequal('A', 'a', 'swapcase')
1176 self.checkequal(True, 'a', 'islower')
1177
1178 self.checkequal('A', 'a', 'title')
1179 self.checkequal(True, 'a', 'islower')
1180
1181 def test_partition(self):
1182
1183 self.checkequal(('this is the par', 'ti', 'tion method'),
1184 'this is the partition method', 'partition', 'ti')
1185
1186 # from raymond's original specification
1187 S = 'http://www.python.org'
1188 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1189 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1190 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1191 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1192
1193 self.checkraises(ValueError, S, 'partition', '')
1194 self.checkraises(TypeError, S, 'partition', None)
1195
1196 def test_rpartition(self):
1197
1198 self.checkequal(('this is the rparti', 'ti', 'on method'),
1199 'this is the rpartition method', 'rpartition', 'ti')
1200
1201 # from raymond's original specification
1202 S = 'http://www.python.org'
1203 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001204 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Thomas Wouters477c8d52006-05-27 19:21:47 +00001205 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1206 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1207
1208 self.checkraises(ValueError, S, 'rpartition', '')
1209 self.checkraises(TypeError, S, 'rpartition', None)
1210
Jesus Ceaac451502011-04-20 17:09:23 +02001211 def test_none_arguments(self):
1212 # issue 11828
1213 s = 'hello'
1214 self.checkequal(2, s, 'find', 'l', None)
1215 self.checkequal(3, s, 'find', 'l', -2, None)
1216 self.checkequal(2, s, 'find', 'l', None, -2)
1217 self.checkequal(0, s, 'find', 'h', None, None)
1218
1219 self.checkequal(3, s, 'rfind', 'l', None)
1220 self.checkequal(3, s, 'rfind', 'l', -2, None)
1221 self.checkequal(2, s, 'rfind', 'l', None, -2)
1222 self.checkequal(0, s, 'rfind', 'h', None, None)
1223
1224 self.checkequal(2, s, 'index', 'l', None)
1225 self.checkequal(3, s, 'index', 'l', -2, None)
1226 self.checkequal(2, s, 'index', 'l', None, -2)
1227 self.checkequal(0, s, 'index', 'h', None, None)
1228
1229 self.checkequal(3, s, 'rindex', 'l', None)
1230 self.checkequal(3, s, 'rindex', 'l', -2, None)
1231 self.checkequal(2, s, 'rindex', 'l', None, -2)
1232 self.checkequal(0, s, 'rindex', 'h', None, None)
1233
1234 self.checkequal(2, s, 'count', 'l', None)
1235 self.checkequal(1, s, 'count', 'l', -2, None)
1236 self.checkequal(1, s, 'count', 'l', None, -2)
1237 self.checkequal(0, s, 'count', 'x', None, None)
1238
1239 self.checkequal(True, s, 'endswith', 'o', None)
1240 self.checkequal(True, s, 'endswith', 'lo', -2, None)
1241 self.checkequal(True, s, 'endswith', 'l', None, -2)
1242 self.checkequal(False, s, 'endswith', 'x', None, None)
1243
1244 self.checkequal(True, s, 'startswith', 'h', None)
1245 self.checkequal(True, s, 'startswith', 'l', -2, None)
1246 self.checkequal(True, s, 'startswith', 'h', None, -2)
1247 self.checkequal(False, s, 'startswith', 'x', None, None)
1248
1249 def test_find_etc_raise_correct_error_messages(self):
1250 # issue 11828
1251 s = 'hello'
1252 x = 'x'
Ezio Melottiaf928422011-04-20 21:56:21 +03001253 self.assertRaisesRegex(TypeError, r'^find\(', s.find,
Jesus Ceaac451502011-04-20 17:09:23 +02001254 x, None, None, None)
Ezio Melottiaf928422011-04-20 21:56:21 +03001255 self.assertRaisesRegex(TypeError, r'^rfind\(', s.rfind,
Jesus Ceaac451502011-04-20 17:09:23 +02001256 x, None, None, None)
Ezio Melottiaf928422011-04-20 21:56:21 +03001257 self.assertRaisesRegex(TypeError, r'^index\(', s.index,
Jesus Ceaac451502011-04-20 17:09:23 +02001258 x, None, None, None)
Ezio Melottiaf928422011-04-20 21:56:21 +03001259 self.assertRaisesRegex(TypeError, r'^rindex\(', s.rindex,
Jesus Ceaac451502011-04-20 17:09:23 +02001260 x, None, None, None)
Ezio Melottiaf928422011-04-20 21:56:21 +03001261 self.assertRaisesRegex(TypeError, r'^count\(', s.count,
Jesus Ceaac451502011-04-20 17:09:23 +02001262 x, None, None, None)
Ezio Melottiaf928422011-04-20 21:56:21 +03001263 self.assertRaisesRegex(TypeError, r'^startswith\(', s.startswith,
Jesus Ceaac451502011-04-20 17:09:23 +02001264 x, None, None, None)
Ezio Melottiaf928422011-04-20 21:56:21 +03001265 self.assertRaisesRegex(TypeError, r'^endswith\(', s.endswith,
Jesus Ceaac451502011-04-20 17:09:23 +02001266 x, None, None, None)
1267
Walter Dörwald57d88e52004-08-26 16:53:04 +00001268
Walter Dörwald57d88e52004-08-26 16:53:04 +00001269class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001270 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001271
1272 def test_bug1001011(self):
1273 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001274 # involving a subclass.
1275 # Make sure that it is of the appropriate type.
1276 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001277 t = self.type2test
1278 class subclass(t):
1279 pass
1280 s1 = subclass("abcd")
1281 s2 = t().join([s1])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001282 self.assertIsNot(s1, s2)
1283 self.assertIs(type(s2), t)
Tim Peters108f1372004-08-27 05:36:07 +00001284
1285 s1 = t("abcd")
1286 s2 = t().join([s1])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001287 self.assertIs(s1, s2)
Tim Peters108f1372004-08-27 05:36:07 +00001288
1289 # Should also test mixed-type join.
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001290 if t is str:
Tim Peters108f1372004-08-27 05:36:07 +00001291 s1 = subclass("abcd")
1292 s2 = "".join([s1])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001293 self.assertIsNot(s1, s2)
1294 self.assertIs(type(s2), t)
Tim Peters108f1372004-08-27 05:36:07 +00001295
1296 s1 = t("abcd")
1297 s2 = "".join([s1])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001298 self.assertIs(s1, s2)
Tim Peters108f1372004-08-27 05:36:07 +00001299
Guido van Rossum98297ee2007-11-06 21:34:58 +00001300## elif t is str8:
1301## s1 = subclass("abcd")
1302## s2 = "".join([s1])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001303## self.assertIsNot(s1, s2)
1304## self.assertIs(type(s2), str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001305
Guido van Rossum98297ee2007-11-06 21:34:58 +00001306## s1 = t("abcd")
1307## s2 = "".join([s1])
Ezio Melottib3aedd42010-11-20 19:04:17 +00001308## self.assertIsNot(s1, s2)
1309## self.assertIs(type(s2), str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001310
1311 else:
1312 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)