blob: ea49e3827363ed7a9b682599735a2f20492faf85 [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
50 def checkequal(self, result, obj, methodname, *args):
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)
Guido van Rossum09549f42007-08-27 20:40:10 +000054 realresult = getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000055 self.assertEqual(
56 result,
57 realresult
58 )
59 # if the original is returned make sure that
60 # this doesn't happen with subclasses
Guido van Rossum09549f42007-08-27 20:40:10 +000061 if obj is realresult:
62 try:
63 class subtype(self.__class__.type2test):
64 pass
65 except TypeError:
66 pass # Skip this if we can't subclass
67 else:
68 obj = subtype(obj)
69 realresult = getattr(obj, methodname)(*args)
70 self.assert_(obj is not realresult)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000071
Guido van Rossum09549f42007-08-27 20:40:10 +000072 # check that obj.method(*args) raises exc
73 def checkraises(self, exc, obj, methodname, *args):
74 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000075 args = self.fixtype(args)
76 self.assertRaises(
77 exc,
Guido van Rossum09549f42007-08-27 20:40:10 +000078 getattr(obj, methodname),
Walter Dörwald0fd583c2003-02-21 12:53:50 +000079 *args
80 )
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000081
Guido van Rossum09549f42007-08-27 20:40:10 +000082 # call obj.method(*args) without any checks
83 def checkcall(self, obj, methodname, *args):
84 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000085 args = self.fixtype(args)
Guido van Rossum09549f42007-08-27 20:40:10 +000086 getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000087
Walter Dörwald0fd583c2003-02-21 12:53:50 +000088 def test_count(self):
89 self.checkequal(3, 'aaa', 'count', 'a')
90 self.checkequal(0, 'aaa', 'count', 'b')
91 self.checkequal(3, 'aaa', 'count', 'a')
92 self.checkequal(0, 'aaa', 'count', 'b')
93 self.checkequal(3, 'aaa', 'count', 'a')
94 self.checkequal(0, 'aaa', 'count', 'b')
95 self.checkequal(0, 'aaa', 'count', 'b')
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000096 self.checkequal(2, 'aaa', 'count', 'a', 1)
97 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000098 self.checkequal(1, 'aaa', 'count', 'a', -1)
99 self.checkequal(3, 'aaa', 'count', 'a', -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000100 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
101 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000102 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
103 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000104 self.checkequal(3, 'aaa', 'count', '', 1)
105 self.checkequal(1, 'aaa', 'count', '', 3)
106 self.checkequal(0, 'aaa', 'count', '', 10)
107 self.checkequal(2, 'aaa', 'count', '', -1)
108 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000109
Amaury Forgeot d'Arcf2e93682008-09-26 22:48:41 +0000110 self.checkequal(1, '', 'count', '')
111 self.checkequal(0, '', 'count', '', 1, 1)
112 self.checkequal(0, '', 'count', '', sys.maxsize, 0)
113
114 self.checkequal(0, '', 'count', 'xx')
115 self.checkequal(0, '', 'count', 'xx', 1, 1)
116 self.checkequal(0, '', 'count', 'xx', sys.maxsize, 0)
117
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000118 self.checkraises(TypeError, 'hello', 'count')
119 self.checkraises(TypeError, 'hello', 'count', 42)
120
Raymond Hettinger57e74472005-02-20 09:54:53 +0000121 # For a variety of combinations,
122 # verify that str.count() matches an equivalent function
123 # replacing all occurrences and then differencing the string lengths
124 charset = ['', 'a', 'b']
125 digits = 7
126 base = len(charset)
127 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000128 for i in range(base ** digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000129 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000130 for j in range(digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000131 i, m = divmod(i, base)
132 entry.append(charset[m])
133 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000134 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger57e74472005-02-20 09:54:53 +0000135 for i in teststrings:
Raymond Hettinger57e74472005-02-20 09:54:53 +0000136 n = len(i)
137 for j in teststrings:
138 r1 = i.count(j)
139 if j:
Guido van Rossum09549f42007-08-27 20:40:10 +0000140 r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
141 len(j))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000142 else:
143 r2, rem = len(i)+1, 0
144 if rem or r1 != r2:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000145 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
146 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000147
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000148 def test_find(self):
149 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
150 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
151 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
152
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000153 self.checkequal(0, 'abc', 'find', '', 0)
154 self.checkequal(3, 'abc', 'find', '', 3)
155 self.checkequal(-1, 'abc', 'find', '', 4)
156
Christian Heimes9cd17752007-11-18 19:35:23 +0000157 # to check the ability to pass None as defaults
158 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
159 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
160 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
161 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
162 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
163
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000164 self.checkraises(TypeError, 'hello', 'find')
165 self.checkraises(TypeError, 'hello', 'find', 42)
166
Amaury Forgeot d'Arcf2e93682008-09-26 22:48:41 +0000167 self.checkequal(0, '', 'find', '')
168 self.checkequal(-1, '', 'find', '', 1, 1)
169 self.checkequal(-1, '', 'find', '', sys.maxsize, 0)
170
171 self.checkequal(-1, '', 'find', 'xx')
172 self.checkequal(-1, '', 'find', 'xx', 1, 1)
173 self.checkequal(-1, '', 'find', 'xx', sys.maxsize, 0)
174
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000175 # For a variety of combinations,
176 # verify that str.find() matches __contains__
177 # and that the found substring is really at that location
178 charset = ['', 'a', 'b', 'c']
179 digits = 5
180 base = len(charset)
181 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000182 for i in range(base ** digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000183 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000184 for j in range(digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000185 i, m = divmod(i, base)
186 entry.append(charset[m])
187 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000188 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000189 for i in teststrings:
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000190 for j in teststrings:
191 loc = i.find(j)
192 r1 = (loc != -1)
193 r2 = j in i
194 if r1 != r2:
195 self.assertEqual(r1, r2)
196 if loc != -1:
197 self.assertEqual(i[loc:loc+len(j)], j)
198
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000199 def test_rfind(self):
200 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
201 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
202 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
203 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
204
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000205 self.checkequal(3, 'abc', 'rfind', '', 0)
206 self.checkequal(3, 'abc', 'rfind', '', 3)
207 self.checkequal(-1, 'abc', 'rfind', '', 4)
208
Christian Heimes9cd17752007-11-18 19:35:23 +0000209 # to check the ability to pass None as defaults
210 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
211 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
212 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
213 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
214 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
215
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000216 self.checkraises(TypeError, 'hello', 'rfind')
217 self.checkraises(TypeError, 'hello', 'rfind', 42)
218
Antoine Pitrouda2ecaf2010-01-02 21:40:36 +0000219 # For a variety of combinations,
220 # verify that str.rfind() matches __contains__
221 # and that the found substring is really at that location
222 charset = ['', 'a', 'b', 'c']
223 digits = 5
224 base = len(charset)
225 teststrings = set()
226 for i in range(base ** digits):
227 entry = []
228 for j in range(digits):
229 i, m = divmod(i, base)
230 entry.append(charset[m])
231 teststrings.add(''.join(entry))
232 teststrings = [self.fixtype(ts) for ts in teststrings]
233 for i in teststrings:
234 for j in teststrings:
235 loc = i.rfind(j)
236 r1 = (loc != -1)
237 r2 = j in i
238 if r1 != r2:
239 self.assertEqual(r1, r2)
240 if loc != -1:
241 self.assertEqual(i[loc:loc+len(j)], j)
242
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000243 def test_index(self):
244 self.checkequal(0, 'abcdefghiabc', 'index', '')
245 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
246 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
247 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
248
249 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
250 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
251 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
252 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
253
Christian Heimes9cd17752007-11-18 19:35:23 +0000254 # to check the ability to pass None as defaults
255 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
256 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
257 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
258 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
259 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
260
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000261 self.checkraises(TypeError, 'hello', 'index')
262 self.checkraises(TypeError, 'hello', 'index', 42)
263
264 def test_rindex(self):
265 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
266 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
267 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
268 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
269
270 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
271 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
272 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
273 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
274 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
275
Christian Heimes9cd17752007-11-18 19:35:23 +0000276 # to check the ability to pass None as defaults
277 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
278 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
279 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
280 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
281 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
282
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000283 self.checkraises(TypeError, 'hello', 'rindex')
284 self.checkraises(TypeError, 'hello', 'rindex', 42)
285
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000286 def test_lower(self):
287 self.checkequal('hello', 'HeLLo', 'lower')
288 self.checkequal('hello', 'hello', 'lower')
289 self.checkraises(TypeError, 'hello', 'lower', 42)
290
291 def test_upper(self):
292 self.checkequal('HELLO', 'HeLLo', 'upper')
293 self.checkequal('HELLO', 'HELLO', 'upper')
294 self.checkraises(TypeError, 'hello', 'upper', 42)
295
296 def test_expandtabs(self):
297 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
298 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
299 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
300 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
301 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
302 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
303 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
304 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
305
306 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
307 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000308 if sys.maxsize < (1 << 32) and struct.calcsize('P') == 4:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000309 self.checkraises(OverflowError,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000310 '\ta\n\tb', 'expandtabs', sys.maxsize)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000311
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000312 def test_split(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000313 # by a char
314 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000315 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000316 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
317 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
318 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
319 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000320 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000321 sys.maxsize-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000322 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
323 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
324 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
326 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000327 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
328
Thomas Wouters477c8d52006-05-27 19:21:47 +0000329 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
330 self.checkequal(['a']*15 +['a|a|a|a|a'],
331 ('a|'*20)[:-1], 'split', '|', 15)
332
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000333 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000334 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000335 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
336 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
337 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
338 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000339 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000340 sys.maxsize-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000341 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
342 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000343 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
345 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
346 'split', 'test')
347 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
348 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
349 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
350 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
351 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
352 self.checkequal([''], '', 'split', 'aaa')
353 self.checkequal(['aa'], 'aa', 'split', 'aaa')
354 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
355 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
356
357 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
358 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
359 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
360 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000361
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000362 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000363 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
364
Thomas Wouters477c8d52006-05-27 19:21:47 +0000365 # null case
366 self.checkraises(ValueError, 'hello', 'split', '')
367 self.checkraises(ValueError, 'hello', 'split', '', 0)
368
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000369 def test_rsplit(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000370 # by a char
371 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
372 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
373 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
374 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
375 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000376 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000377 sys.maxsize-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000378 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
379 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
380 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000381 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
382 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
383
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000384 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
385
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
387 self.checkequal(['a|a|a|a|a']+['a']*15,
388 ('a|'*20)[:-1], 'rsplit', '|', 15)
389
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000390 # by string
391 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
392 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
393 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
394 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
395 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000396 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000397 sys.maxsize-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000398 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
399 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
400 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000401 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
402 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
403 'rsplit', 'test')
404 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
405 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
406 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
407 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
408 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
409 self.checkequal([''], '', 'rsplit', 'aaa')
410 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
411 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
412 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
413
414 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
415 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
416 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
417 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000418
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000419 # argument type
420 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000421
Thomas Wouters477c8d52006-05-27 19:21:47 +0000422 # null case
423 self.checkraises(ValueError, 'hello', 'rsplit', '')
424 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
425
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000426 def test_replace(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000427 EQ = self.checkequal
428
429 # Operations on the empty string
430 EQ("", "", "replace", "", "")
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000431 EQ("A", "", "replace", "", "A")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000432 EQ("", "", "replace", "A", "")
433 EQ("", "", "replace", "A", "A")
434 EQ("", "", "replace", "", "", 100)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000435 EQ("", "", "replace", "", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000436
437 # interleave (from=="", 'to' gets inserted everywhere)
438 EQ("A", "A", "replace", "", "")
439 EQ("*A*", "A", "replace", "", "*")
440 EQ("*1A*1", "A", "replace", "", "*1")
441 EQ("*-#A*-#", "A", "replace", "", "*-#")
442 EQ("*-A*-A*-", "AA", "replace", "", "*-")
443 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000444 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000445 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
446 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
447 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
448 EQ("*-AA", "AA", "replace", "", "*-", 1)
449 EQ("AA", "AA", "replace", "", "*-", 0)
450
451 # single character deletion (from=="A", to=="")
452 EQ("", "A", "replace", "A", "")
453 EQ("", "AAA", "replace", "A", "")
454 EQ("", "AAA", "replace", "A", "", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000455 EQ("", "AAA", "replace", "A", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000456 EQ("", "AAA", "replace", "A", "", 4)
457 EQ("", "AAA", "replace", "A", "", 3)
458 EQ("A", "AAA", "replace", "A", "", 2)
459 EQ("AA", "AAA", "replace", "A", "", 1)
460 EQ("AAA", "AAA", "replace", "A", "", 0)
461 EQ("", "AAAAAAAAAA", "replace", "A", "")
462 EQ("BCD", "ABACADA", "replace", "A", "")
463 EQ("BCD", "ABACADA", "replace", "A", "", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000464 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000465 EQ("BCD", "ABACADA", "replace", "A", "", 5)
466 EQ("BCD", "ABACADA", "replace", "A", "", 4)
467 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
468 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
469 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
470 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
471 EQ("BCD", "ABCAD", "replace", "A", "")
472 EQ("BCD", "ABCADAA", "replace", "A", "")
473 EQ("BCD", "BCD", "replace", "A", "")
474 EQ("*************", "*************", "replace", "A", "")
475 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
476
477 # substring deletion (from=="the", to=="")
478 EQ("", "the", "replace", "the", "")
479 EQ("ater", "theater", "replace", "the", "")
480 EQ("", "thethe", "replace", "the", "")
481 EQ("", "thethethethe", "replace", "the", "")
482 EQ("aaaa", "theatheatheathea", "replace", "the", "")
483 EQ("that", "that", "replace", "the", "")
484 EQ("thaet", "thaet", "replace", "the", "")
485 EQ("here and re", "here and there", "replace", "the", "")
486 EQ("here and re and re", "here and there and there",
Christian Heimesa37d4c62007-12-04 23:02:19 +0000487 "replace", "the", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000488 EQ("here and re and re", "here and there and there",
489 "replace", "the", "", -1)
490 EQ("here and re and re", "here and there and there",
491 "replace", "the", "", 3)
492 EQ("here and re and re", "here and there and there",
493 "replace", "the", "", 2)
494 EQ("here and re and there", "here and there and there",
495 "replace", "the", "", 1)
496 EQ("here and there and there", "here and there and there",
497 "replace", "the", "", 0)
498 EQ("here and re and re", "here and there and there", "replace", "the", "")
499
500 EQ("abc", "abc", "replace", "the", "")
501 EQ("abcdefg", "abcdefg", "replace", "the", "")
502
503 # substring deletion (from=="bob", to=="")
504 EQ("bob", "bbobob", "replace", "bob", "")
505 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
506 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
507 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
508
509 # single character replace in place (len(from)==len(to)==1)
510 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
511 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
Christian Heimesa37d4c62007-12-04 23:02:19 +0000512 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000513 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
514 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
515 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
516 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
517 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
518
519 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
520 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
521 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
522 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
523 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
524
525 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
526
527 # substring replace in place (len(from)==len(to) > 1)
528 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
Christian Heimesa37d4c62007-12-04 23:02:19 +0000529 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000530 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
531 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
532 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
533 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
534 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
535 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
536 EQ("cobob", "bobob", "replace", "bob", "cob")
537 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
538 EQ("bobob", "bobob", "replace", "bot", "bot")
539
540 # replace single character (len(from)==1, len(to)>1)
541 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
542 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000543 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000544 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
545 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
546 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
547 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
548
549 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
550
551 # replace substring (len(from)>1, len(to)!=len(from))
552 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
553 "replace", "spam", "ham")
554 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
Christian Heimesa37d4c62007-12-04 23:02:19 +0000555 "replace", "spam", "ham", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000556 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
557 "replace", "spam", "ham", -1)
558 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
559 "replace", "spam", "ham", 4)
560 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
561 "replace", "spam", "ham", 3)
562 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
563 "replace", "spam", "ham", 2)
564 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
565 "replace", "spam", "ham", 1)
566 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
567 "replace", "spam", "ham", 0)
568
569 EQ("bobob", "bobobob", "replace", "bobob", "bob")
570 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
571 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
572
Guido van Rossum39478e82007-08-27 17:23:59 +0000573 # XXX Commented out. Is there any reason to support buffer objects
574 # as arguments for str.replace()? GvR
Guido van Rossum254348e2007-11-21 19:29:53 +0000575## ba = bytearray('a')
576## bb = bytearray('b')
Guido van Rossum39478e82007-08-27 17:23:59 +0000577## EQ("bbc", "abc", "replace", ba, bb)
578## EQ("aac", "abc", "replace", bb, ba)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000579
Thomas Wouters477c8d52006-05-27 19:21:47 +0000580 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000581 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
582 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
583 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
584 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
585 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
586 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
587 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
588 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
589 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
590 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
591 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
592 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
593 self.checkequal('', '', 'replace', '', '')
594 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
595 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
596 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
597 # MemoryError due to empty result (platform malloc issue when requesting
598 # 0 bytes).
599 self.checkequal('', '123', 'replace', '123', '')
600 self.checkequal('', '123123', 'replace', '123', '')
601 self.checkequal('x', '123x123', 'replace', '123', '')
602
603 self.checkraises(TypeError, 'hello', 'replace')
604 self.checkraises(TypeError, 'hello', 'replace', 42)
605 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
606 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
607
Thomas Wouters477c8d52006-05-27 19:21:47 +0000608 def test_replace_overflow(self):
609 # Check for overflow checking on 32 bit machines
Christian Heimesa37d4c62007-12-04 23:02:19 +0000610 if sys.maxsize != 2147483647 or struct.calcsize("P") > 4:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 return
612 A2_16 = "A" * (2**16)
613 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
614 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
615 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
616
Georg Brandlc7885542007-03-06 19:16:20 +0000617
618
619class CommonTest(BaseTest):
620 # This testcase contains test that can be used in all
621 # stringlike classes. Currently this is str, unicode
622 # UserString and the string module.
623
624 def test_hash(self):
625 # SF bug 1054139: += optimization was not invalidating cached hash value
626 a = self.type2test('DNSSEC')
627 b = self.type2test('')
628 for c in a:
629 b += c
630 hash(b)
631 self.assertEqual(hash(a), hash(b))
632
633 def test_capitalize(self):
634 self.checkequal(' hello ', ' hello ', 'capitalize')
635 self.checkequal('Hello ', 'Hello ','capitalize')
636 self.checkequal('Hello ', 'hello ','capitalize')
637 self.checkequal('Aaaa', 'aaaa', 'capitalize')
638 self.checkequal('Aaaa', 'AaAa', 'capitalize')
639
640 self.checkraises(TypeError, 'hello', 'capitalize', 42)
641
642 def test_lower(self):
643 self.checkequal('hello', 'HeLLo', 'lower')
644 self.checkequal('hello', 'hello', 'lower')
645 self.checkraises(TypeError, 'hello', 'lower', 42)
646
647 def test_upper(self):
648 self.checkequal('HELLO', 'HeLLo', 'upper')
649 self.checkequal('HELLO', 'HELLO', 'upper')
650 self.checkraises(TypeError, 'hello', 'upper', 42)
651
652 def test_expandtabs(self):
653 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
654 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
655 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
656 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
657 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
658 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
659 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
660
661 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
662
663 def test_additional_split(self):
664 self.checkequal(['this', 'is', 'the', 'split', 'function'],
665 'this is the split function', 'split')
666
667 # by whitespace
668 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
669 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
670 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
671 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
672 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
673 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000674 sys.maxsize-1)
Georg Brandlc7885542007-03-06 19:16:20 +0000675 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
676 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
677 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
678
679 self.checkequal([], ' ', 'split')
680 self.checkequal(['a'], ' a ', 'split')
681 self.checkequal(['a', 'b'], ' a b ', 'split')
682 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
683 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
684 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
685 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
686 aaa = ' a '*20
687 self.checkequal(['a']*20, aaa, 'split')
688 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
689 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
690
691 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000692 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000693
694 def test_additional_rsplit(self):
695 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
696 'this is the rsplit function', 'rsplit')
697
698 # by whitespace
699 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
700 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
701 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
702 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
703 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
704 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000705 sys.maxsize-20)
Georg Brandlc7885542007-03-06 19:16:20 +0000706 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
707 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
708 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
709
710 self.checkequal([], ' ', 'rsplit')
711 self.checkequal(['a'], ' a ', 'rsplit')
712 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
713 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
714 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
715 None, 1)
716 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
717 None, 2)
718 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
719 aaa = ' a '*20
720 self.checkequal(['a']*20, aaa, 'rsplit')
721 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
722 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
723
724 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000725 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000726
727 def test_strip(self):
728 self.checkequal('hello', ' hello ', 'strip')
729 self.checkequal('hello ', ' hello ', 'lstrip')
730 self.checkequal(' hello', ' hello ', 'rstrip')
731 self.checkequal('hello', 'hello', 'strip')
732
733 # strip/lstrip/rstrip with None arg
734 self.checkequal('hello', ' hello ', 'strip', None)
735 self.checkequal('hello ', ' hello ', 'lstrip', None)
736 self.checkequal(' hello', ' hello ', 'rstrip', None)
737 self.checkequal('hello', 'hello', 'strip', None)
738
739 # strip/lstrip/rstrip with str arg
740 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
741 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
742 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
743 self.checkequal('hello', 'hello', 'strip', 'xyz')
744
Georg Brandlc7885542007-03-06 19:16:20 +0000745 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
746 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
747 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
748
749 def test_ljust(self):
750 self.checkequal('abc ', 'abc', 'ljust', 10)
751 self.checkequal('abc ', 'abc', 'ljust', 6)
752 self.checkequal('abc', 'abc', 'ljust', 3)
753 self.checkequal('abc', 'abc', 'ljust', 2)
754 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
755 self.checkraises(TypeError, 'abc', 'ljust')
756
757 def test_rjust(self):
758 self.checkequal(' abc', 'abc', 'rjust', 10)
759 self.checkequal(' abc', 'abc', 'rjust', 6)
760 self.checkequal('abc', 'abc', 'rjust', 3)
761 self.checkequal('abc', 'abc', 'rjust', 2)
762 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
763 self.checkraises(TypeError, 'abc', 'rjust')
764
765 def test_center(self):
766 self.checkequal(' abc ', 'abc', 'center', 10)
767 self.checkequal(' abc ', 'abc', 'center', 6)
768 self.checkequal('abc', 'abc', 'center', 3)
769 self.checkequal('abc', 'abc', 'center', 2)
770 self.checkequal('***abc****', 'abc', 'center', 10, '*')
771 self.checkraises(TypeError, 'abc', 'center')
772
773 def test_swapcase(self):
774 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
775
776 self.checkraises(TypeError, 'hello', 'swapcase', 42)
777
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000778 def test_zfill(self):
779 self.checkequal('123', '123', 'zfill', 2)
780 self.checkequal('123', '123', 'zfill', 3)
781 self.checkequal('0123', '123', 'zfill', 4)
782 self.checkequal('+123', '+123', 'zfill', 3)
783 self.checkequal('+123', '+123', 'zfill', 4)
784 self.checkequal('+0123', '+123', 'zfill', 5)
785 self.checkequal('-123', '-123', 'zfill', 3)
786 self.checkequal('-123', '-123', 'zfill', 4)
787 self.checkequal('-0123', '-123', 'zfill', 5)
788 self.checkequal('000', '', 'zfill', 3)
789 self.checkequal('34', '34', 'zfill', 1)
790 self.checkequal('0034', '34', 'zfill', 4)
791
792 self.checkraises(TypeError, '123', 'zfill')
793
794class MixinStrUnicodeUserStringTest:
795 # additional tests that only work for
796 # stringlike objects, i.e. str, unicode, UserString
797 # (but not the string module)
798
799 def test_islower(self):
800 self.checkequal(False, '', 'islower')
801 self.checkequal(True, 'a', 'islower')
802 self.checkequal(False, 'A', 'islower')
803 self.checkequal(False, '\n', 'islower')
804 self.checkequal(True, 'abc', 'islower')
805 self.checkequal(False, 'aBc', 'islower')
806 self.checkequal(True, 'abc\n', 'islower')
807 self.checkraises(TypeError, 'abc', 'islower', 42)
808
809 def test_isupper(self):
810 self.checkequal(False, '', 'isupper')
811 self.checkequal(False, 'a', 'isupper')
812 self.checkequal(True, 'A', 'isupper')
813 self.checkequal(False, '\n', 'isupper')
814 self.checkequal(True, 'ABC', 'isupper')
815 self.checkequal(False, 'AbC', 'isupper')
816 self.checkequal(True, 'ABC\n', 'isupper')
817 self.checkraises(TypeError, 'abc', 'isupper', 42)
818
819 def test_istitle(self):
820 self.checkequal(False, '', 'istitle')
821 self.checkequal(False, 'a', 'istitle')
822 self.checkequal(True, 'A', 'istitle')
823 self.checkequal(False, '\n', 'istitle')
824 self.checkequal(True, 'A Titlecased Line', 'istitle')
825 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
826 self.checkequal(True, 'A Titlecased, Line', 'istitle')
827 self.checkequal(False, 'Not a capitalized String', 'istitle')
828 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
829 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
830 self.checkequal(False, 'NOT', 'istitle')
831 self.checkraises(TypeError, 'abc', 'istitle', 42)
832
833 def test_isspace(self):
834 self.checkequal(False, '', 'isspace')
835 self.checkequal(False, 'a', 'isspace')
836 self.checkequal(True, ' ', 'isspace')
837 self.checkequal(True, '\t', 'isspace')
838 self.checkequal(True, '\r', 'isspace')
839 self.checkequal(True, '\n', 'isspace')
840 self.checkequal(True, ' \t\r\n', 'isspace')
841 self.checkequal(False, ' \t\r\na', 'isspace')
842 self.checkraises(TypeError, 'abc', 'isspace', 42)
843
844 def test_isalpha(self):
845 self.checkequal(False, '', 'isalpha')
846 self.checkequal(True, 'a', 'isalpha')
847 self.checkequal(True, 'A', 'isalpha')
848 self.checkequal(False, '\n', 'isalpha')
849 self.checkequal(True, 'abc', 'isalpha')
850 self.checkequal(False, 'aBc123', 'isalpha')
851 self.checkequal(False, 'abc\n', 'isalpha')
852 self.checkraises(TypeError, 'abc', 'isalpha', 42)
853
854 def test_isalnum(self):
855 self.checkequal(False, '', 'isalnum')
856 self.checkequal(True, 'a', 'isalnum')
857 self.checkequal(True, 'A', 'isalnum')
858 self.checkequal(False, '\n', 'isalnum')
859 self.checkequal(True, '123abc456', 'isalnum')
860 self.checkequal(True, 'a1b3c', 'isalnum')
861 self.checkequal(False, 'aBc000 ', 'isalnum')
862 self.checkequal(False, 'abc\n', 'isalnum')
863 self.checkraises(TypeError, 'abc', 'isalnum', 42)
864
865 def test_isdigit(self):
866 self.checkequal(False, '', 'isdigit')
867 self.checkequal(False, 'a', 'isdigit')
868 self.checkequal(True, '0', 'isdigit')
869 self.checkequal(True, '0123456789', 'isdigit')
870 self.checkequal(False, '0123456789a', 'isdigit')
871
872 self.checkraises(TypeError, 'abc', 'isdigit', 42)
873
874 def test_title(self):
875 self.checkequal(' Hello ', ' hello ', 'title')
876 self.checkequal('Hello ', 'hello ', 'title')
877 self.checkequal('Hello ', 'Hello ', 'title')
878 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
879 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
880 self.checkequal('Getint', "getInt", 'title')
881 self.checkraises(TypeError, 'hello', 'title', 42)
882
883 def test_splitlines(self):
884 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
885 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
886 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
887 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
888 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
889 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
890 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
891
892 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
893
894 def test_startswith(self):
895 self.checkequal(True, 'hello', 'startswith', 'he')
896 self.checkequal(True, 'hello', 'startswith', 'hello')
897 self.checkequal(False, 'hello', 'startswith', 'hello world')
898 self.checkequal(True, 'hello', 'startswith', '')
899 self.checkequal(False, 'hello', 'startswith', 'ello')
900 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
901 self.checkequal(True, 'hello', 'startswith', 'o', 4)
902 self.checkequal(False, 'hello', 'startswith', 'o', 5)
903 self.checkequal(True, 'hello', 'startswith', '', 5)
904 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
905 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
906 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
907 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
908
909 # test negative indices
910 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
911 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
912 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
913 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
914 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
915 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
916 self.checkequal(False, 'hello', 'startswith', 'o', -2)
917 self.checkequal(True, 'hello', 'startswith', 'o', -1)
918 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
919 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
920
921 self.checkraises(TypeError, 'hello', 'startswith')
922 self.checkraises(TypeError, 'hello', 'startswith', 42)
923
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000924 # test tuple arguments
925 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
926 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
927 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
928 self.checkequal(False, 'hello', 'startswith', ())
929 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
930 'rld', 'lowo'), 3)
931 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
932 'rld'), 3)
933 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
934 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
935 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
936
937 self.checkraises(TypeError, 'hello', 'startswith', (42,))
938
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000939 def test_endswith(self):
940 self.checkequal(True, 'hello', 'endswith', 'lo')
941 self.checkequal(False, 'hello', 'endswith', 'he')
942 self.checkequal(True, 'hello', 'endswith', '')
943 self.checkequal(False, 'hello', 'endswith', 'hello world')
944 self.checkequal(False, 'helloworld', 'endswith', 'worl')
945 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
946 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
947 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
948 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
949 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
950 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
951 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
952 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
953 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
954
955 # test negative indices
956 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
957 self.checkequal(False, 'hello', 'endswith', 'he', -2)
958 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
959 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
960 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
961 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
962 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
963 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
964 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
965 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
966 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
967 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
968 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
969
970 self.checkraises(TypeError, 'hello', 'endswith')
971 self.checkraises(TypeError, 'hello', 'endswith', 42)
972
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000973 # test tuple arguments
974 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
975 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
976 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
977 self.checkequal(False, 'hello', 'endswith', ())
978 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
979 'rld', 'lowo'), 3)
980 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
981 'rld'), 3, -1)
982 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
983 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
984 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
985
986 self.checkraises(TypeError, 'hello', 'endswith', (42,))
987
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000988 def test___contains__(self):
989 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
990 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
991 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
992 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
993 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
994 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
995 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
996 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
997 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
998
999 def test_subscript(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001000 self.checkequal('a', 'abc', '__getitem__', 0)
1001 self.checkequal('c', 'abc', '__getitem__', -1)
1002 self.checkequal('a', 'abc', '__getitem__', 0)
1003 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1004 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1005 self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
1006 self.checkequal('', 'abc', '__getitem__', slice(0, 0))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001007
1008 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1009
1010 def test_slice(self):
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001011 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1012 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1013 self.checkequal('ab', 'abc', '__getitem__', slice(0, 2))
1014 self.checkequal('bc', 'abc', '__getitem__', slice(1, 3))
1015 self.checkequal('b', 'abc', '__getitem__', slice(1, 2))
1016 self.checkequal('', 'abc', '__getitem__', slice(2, 2))
1017 self.checkequal('', 'abc', '__getitem__', slice(1000, 1000))
1018 self.checkequal('', 'abc', '__getitem__', slice(2000, 1000))
1019 self.checkequal('', 'abc', '__getitem__', slice(2, 1))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001020
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001021 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001022
Thomas Woutersed03b412007-08-28 21:37:11 +00001023 def test_extended_getslice(self):
1024 # Test extended slicing by comparing with list slicing.
1025 s = string.ascii_letters + string.digits
1026 indices = (0, None, 1, 3, 41, -1, -2, -37)
1027 for start in indices:
1028 for stop in indices:
1029 # Skip step 0 (invalid)
1030 for step in indices[1:]:
1031 L = list(s)[start:stop:step]
1032 self.checkequal("".join(L), s, '__getitem__',
1033 slice(start, stop, step))
1034
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001035 def test_mul(self):
1036 self.checkequal('', 'abc', '__mul__', -1)
1037 self.checkequal('', 'abc', '__mul__', 0)
1038 self.checkequal('abc', 'abc', '__mul__', 1)
1039 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
1040 self.checkraises(TypeError, 'abc', '__mul__')
1041 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +00001042 # XXX: on a 64-bit system, this doesn't raise an overflow error,
1043 # but either raises a MemoryError, or succeeds (if you have 54TiB)
1044 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001045
1046 def test_join(self):
1047 # join now works with any sequence type
1048 # moved here, because the argument order is
1049 # different in string.join (see the test in
1050 # test.test_string.StringTest.test_join)
1051 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
1052 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001053 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
1054 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001055 self.checkequal('w x y z', ' ', 'join', Sequence())
1056 self.checkequal('abc', 'a', 'join', ('abc',))
1057 self.checkequal('z', 'a', 'join', UserList(['z']))
Walter Dörwald67e83882007-05-05 12:26:27 +00001058 self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
Guido van Rossum98297ee2007-11-06 21:34:58 +00001059 self.assertRaises(TypeError, '.'.join, ['a', 'b', 3])
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001060 for i in [5, 25, 125]:
1061 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1062 ['a' * i] * i)
1063 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1064 ('a' * i,) * i)
1065
Guido van Rossum98297ee2007-11-06 21:34:58 +00001066 #self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001067 self.checkequal('a b c', ' ', 'join', BadSeq2())
1068
1069 self.checkraises(TypeError, ' ', 'join')
1070 self.checkraises(TypeError, ' ', 'join', 7)
Guido van Rossumf1044292007-09-27 18:01:22 +00001071 self.checkraises(TypeError, ' ', 'join', [1, 2, bytes()])
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001072 try:
1073 def f():
1074 yield 4 + ""
1075 self.fixtype(' ').join(f())
Guido van Rossumb940e112007-01-10 16:19:56 +00001076 except TypeError as e:
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001077 if '+' not in str(e):
1078 self.fail('join() ate exception message')
1079 else:
1080 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001081
1082 def test_formatting(self):
1083 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1084 self.checkequal('+10+', '+%d+', '__mod__', 10)
1085 self.checkequal('a', "%c", '__mod__', "a")
1086 self.checkequal('a', "%c", '__mod__', "a")
1087 self.checkequal('"', "%c", '__mod__', 34)
1088 self.checkequal('$', "%c", '__mod__', 36)
1089 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001090 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001091
1092 for ordinal in (-100, 0x200000):
1093 # unicode raises ValueError, str raises OverflowError
1094 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1095
Christian Heimesa612dc02008-02-24 13:08:18 +00001096 longvalue = sys.maxsize + 10
1097 slongvalue = str(longvalue)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001098 self.checkequal(' 42', '%3ld', '__mod__', 42)
Christian Heimesa612dc02008-02-24 13:08:18 +00001099 self.checkequal('42', '%d', '__mod__', 42.0)
1100 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1101 self.checkcall('%d', '__mod__', float(longvalue))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001102 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001103 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001104
1105 self.checkraises(TypeError, 'abc', '__mod__')
1106 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1107 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1108 self.checkraises(TypeError, '%c', '__mod__', (None,))
1109 self.checkraises(ValueError, '%(foo', '__mod__', {})
1110 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
Christian Heimesa612dc02008-02-24 13:08:18 +00001111 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
Mark Dickinson5c2db372009-12-05 20:28:34 +00001112 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int conversion provided
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001113
1114 # argument names with properly nested brackets are supported
1115 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1116
1117 # 100 is a magic number in PyUnicode_Format, this forces a resize
1118 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1119
1120 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1121 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1122 self.checkraises(ValueError, '%10', '__mod__', (42,))
1123
1124 def test_floatformatting(self):
1125 # float formatting
Guido van Rossum805365e2007-05-07 22:24:25 +00001126 for prec in range(100):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001127 format = '%%.%if' % prec
1128 value = 0.01
Guido van Rossum805365e2007-05-07 22:24:25 +00001129 for x in range(60):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001130 value = value * 3.141592655 / 3.0 * 10.0
Mark Dickinsonf489caf2009-05-01 11:42:00 +00001131 self.checkcall(format, "__mod__", value)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001132
Thomas Wouters477c8d52006-05-27 19:21:47 +00001133 def test_inplace_rewrites(self):
1134 # Check that strings don't copy and modify cached single-character strings
1135 self.checkequal('a', 'A', 'lower')
1136 self.checkequal(True, 'A', 'isupper')
1137 self.checkequal('A', 'a', 'upper')
1138 self.checkequal(True, 'a', 'islower')
1139
1140 self.checkequal('a', 'A', 'replace', 'A', 'a')
1141 self.checkequal(True, 'A', 'isupper')
1142
1143 self.checkequal('A', 'a', 'capitalize')
1144 self.checkequal(True, 'a', 'islower')
1145
1146 self.checkequal('A', 'a', 'swapcase')
1147 self.checkequal(True, 'a', 'islower')
1148
1149 self.checkequal('A', 'a', 'title')
1150 self.checkequal(True, 'a', 'islower')
1151
1152 def test_partition(self):
1153
1154 self.checkequal(('this is the par', 'ti', 'tion method'),
1155 'this is the partition method', 'partition', 'ti')
1156
1157 # from raymond's original specification
1158 S = 'http://www.python.org'
1159 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1160 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1161 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1162 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1163
1164 self.checkraises(ValueError, S, 'partition', '')
1165 self.checkraises(TypeError, S, 'partition', None)
1166
1167 def test_rpartition(self):
1168
1169 self.checkequal(('this is the rparti', 'ti', 'on method'),
1170 'this is the rpartition method', 'rpartition', 'ti')
1171
1172 # from raymond's original specification
1173 S = 'http://www.python.org'
1174 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001175 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Thomas Wouters477c8d52006-05-27 19:21:47 +00001176 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1177 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1178
1179 self.checkraises(ValueError, S, 'rpartition', '')
1180 self.checkraises(TypeError, S, 'rpartition', None)
1181
Walter Dörwald57d88e52004-08-26 16:53:04 +00001182
Walter Dörwald57d88e52004-08-26 16:53:04 +00001183class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001184 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001185
1186 def test_bug1001011(self):
1187 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001188 # involving a subclass.
1189 # Make sure that it is of the appropriate type.
1190 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001191 t = self.type2test
1192 class subclass(t):
1193 pass
1194 s1 = subclass("abcd")
1195 s2 = t().join([s1])
1196 self.assert_(s1 is not s2)
1197 self.assert_(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001198
1199 s1 = t("abcd")
1200 s2 = t().join([s1])
1201 self.assert_(s1 is s2)
1202
1203 # Should also test mixed-type join.
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001204 if t is str:
Tim Peters108f1372004-08-27 05:36:07 +00001205 s1 = subclass("abcd")
1206 s2 = "".join([s1])
1207 self.assert_(s1 is not s2)
1208 self.assert_(type(s2) is t)
1209
1210 s1 = t("abcd")
1211 s2 = "".join([s1])
1212 self.assert_(s1 is s2)
1213
Guido van Rossum98297ee2007-11-06 21:34:58 +00001214## elif t is str8:
1215## s1 = subclass("abcd")
1216## s2 = "".join([s1])
1217## self.assert_(s1 is not s2)
1218## self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001219
Guido van Rossum98297ee2007-11-06 21:34:58 +00001220## s1 = t("abcd")
1221## s2 = "".join([s1])
1222## self.assert_(s1 is not s2)
1223## self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001224
1225 else:
1226 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)