blob: 4964248eade3dc4d80dda87a0c4bf5e08dddb367 [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
Antoine Pitrou74edda02010-01-02 21:51:33 +0000175 # issue 7458
176 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
177
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000178 # For a variety of combinations,
179 # verify that str.find() matches __contains__
180 # and that the found substring is really at that location
181 charset = ['', 'a', 'b', 'c']
182 digits = 5
183 base = len(charset)
184 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000185 for i in range(base ** digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000186 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000187 for j in range(digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000188 i, m = divmod(i, base)
189 entry.append(charset[m])
190 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000191 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000192 for i in teststrings:
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000193 for j in teststrings:
194 loc = i.find(j)
195 r1 = (loc != -1)
196 r2 = j in i
197 if r1 != r2:
198 self.assertEqual(r1, r2)
199 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
241 if r1 != r2:
242 self.assertEqual(r1, r2)
243 if loc != -1:
244 self.assertEqual(i[loc:loc+len(j)], j)
245
Antoine Pitrou74edda02010-01-02 21:51:33 +0000246 # issue 7458
247 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
248
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000249 def test_index(self):
250 self.checkequal(0, 'abcdefghiabc', 'index', '')
251 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
252 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
253 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
254
255 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
256 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
257 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
258 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
259
Christian Heimes9cd17752007-11-18 19:35:23 +0000260 # to check the ability to pass None as defaults
261 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
262 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
263 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
264 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
265 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
266
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000267 self.checkraises(TypeError, 'hello', 'index')
268 self.checkraises(TypeError, 'hello', 'index', 42)
269
270 def test_rindex(self):
271 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
272 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
273 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
274 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
275
276 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
277 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
278 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
279 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
280 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
281
Christian Heimes9cd17752007-11-18 19:35:23 +0000282 # to check the ability to pass None as defaults
283 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
284 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
285 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
286 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
287 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
288
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000289 self.checkraises(TypeError, 'hello', 'rindex')
290 self.checkraises(TypeError, 'hello', 'rindex', 42)
291
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000292 def test_lower(self):
293 self.checkequal('hello', 'HeLLo', 'lower')
294 self.checkequal('hello', 'hello', 'lower')
295 self.checkraises(TypeError, 'hello', 'lower', 42)
296
297 def test_upper(self):
298 self.checkequal('HELLO', 'HeLLo', 'upper')
299 self.checkequal('HELLO', 'HELLO', 'upper')
300 self.checkraises(TypeError, 'hello', 'upper', 42)
301
302 def test_expandtabs(self):
303 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
304 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
305 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
306 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
307 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
308 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
309 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
310 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
311
312 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
313 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000314 if sys.maxsize < (1 << 32) and struct.calcsize('P') == 4:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000315 self.checkraises(OverflowError,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000316 '\ta\n\tb', 'expandtabs', sys.maxsize)
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000317
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000318 def test_split(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000319 # by a char
320 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000321 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000322 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
323 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
324 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
325 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000326 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000327 sys.maxsize-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000328 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
329 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
330 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000331 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
332 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000333 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
334
Thomas Wouters477c8d52006-05-27 19:21:47 +0000335 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
336 self.checkequal(['a']*15 +['a|a|a|a|a'],
337 ('a|'*20)[:-1], 'split', '|', 15)
338
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000339 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000340 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000341 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
342 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
343 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
344 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000345 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000346 sys.maxsize-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000347 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
348 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000349 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000350 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
351 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
352 'split', 'test')
353 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
354 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
355 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
356 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
357 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
358 self.checkequal([''], '', 'split', 'aaa')
359 self.checkequal(['aa'], 'aa', 'split', 'aaa')
360 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
361 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
362
363 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
364 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
365 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
366 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000367
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000368 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000369 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
370
Thomas Wouters477c8d52006-05-27 19:21:47 +0000371 # null case
372 self.checkraises(ValueError, 'hello', 'split', '')
373 self.checkraises(ValueError, 'hello', 'split', '', 0)
374
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000375 def test_rsplit(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000376 # by a char
377 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
378 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
379 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
380 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
381 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000382 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000383 sys.maxsize-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000384 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
385 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
386 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000387 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
388 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
389
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000390 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
391
Thomas Wouters477c8d52006-05-27 19:21:47 +0000392 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
393 self.checkequal(['a|a|a|a|a']+['a']*15,
394 ('a|'*20)[:-1], 'rsplit', '|', 15)
395
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000396 # by string
397 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
398 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
399 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
400 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
401 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000402 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
Christian Heimesa37d4c62007-12-04 23:02:19 +0000403 sys.maxsize-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000404 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
405 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
406 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000407 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
408 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
409 'rsplit', 'test')
410 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
411 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
412 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
413 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
414 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
415 self.checkequal([''], '', 'rsplit', 'aaa')
416 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
417 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
418 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
419
420 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
421 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
422 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
423 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000424
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000425 # argument type
426 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000427
Thomas Wouters477c8d52006-05-27 19:21:47 +0000428 # null case
429 self.checkraises(ValueError, 'hello', 'rsplit', '')
430 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
431
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000432 def test_replace(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000433 EQ = self.checkequal
434
435 # Operations on the empty string
436 EQ("", "", "replace", "", "")
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000437 EQ("A", "", "replace", "", "A")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000438 EQ("", "", "replace", "A", "")
439 EQ("", "", "replace", "A", "A")
440 EQ("", "", "replace", "", "", 100)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000441 EQ("", "", "replace", "", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000442
443 # interleave (from=="", 'to' gets inserted everywhere)
444 EQ("A", "A", "replace", "", "")
445 EQ("*A*", "A", "replace", "", "*")
446 EQ("*1A*1", "A", "replace", "", "*1")
447 EQ("*-#A*-#", "A", "replace", "", "*-#")
448 EQ("*-A*-A*-", "AA", "replace", "", "*-")
449 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000450 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000451 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
452 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
453 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
454 EQ("*-AA", "AA", "replace", "", "*-", 1)
455 EQ("AA", "AA", "replace", "", "*-", 0)
456
457 # single character deletion (from=="A", to=="")
458 EQ("", "A", "replace", "A", "")
459 EQ("", "AAA", "replace", "A", "")
460 EQ("", "AAA", "replace", "A", "", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000461 EQ("", "AAA", "replace", "A", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000462 EQ("", "AAA", "replace", "A", "", 4)
463 EQ("", "AAA", "replace", "A", "", 3)
464 EQ("A", "AAA", "replace", "A", "", 2)
465 EQ("AA", "AAA", "replace", "A", "", 1)
466 EQ("AAA", "AAA", "replace", "A", "", 0)
467 EQ("", "AAAAAAAAAA", "replace", "A", "")
468 EQ("BCD", "ABACADA", "replace", "A", "")
469 EQ("BCD", "ABACADA", "replace", "A", "", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000470 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000471 EQ("BCD", "ABACADA", "replace", "A", "", 5)
472 EQ("BCD", "ABACADA", "replace", "A", "", 4)
473 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
474 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
475 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
476 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
477 EQ("BCD", "ABCAD", "replace", "A", "")
478 EQ("BCD", "ABCADAA", "replace", "A", "")
479 EQ("BCD", "BCD", "replace", "A", "")
480 EQ("*************", "*************", "replace", "A", "")
481 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
482
483 # substring deletion (from=="the", to=="")
484 EQ("", "the", "replace", "the", "")
485 EQ("ater", "theater", "replace", "the", "")
486 EQ("", "thethe", "replace", "the", "")
487 EQ("", "thethethethe", "replace", "the", "")
488 EQ("aaaa", "theatheatheathea", "replace", "the", "")
489 EQ("that", "that", "replace", "the", "")
490 EQ("thaet", "thaet", "replace", "the", "")
491 EQ("here and re", "here and there", "replace", "the", "")
492 EQ("here and re and re", "here and there and there",
Christian Heimesa37d4c62007-12-04 23:02:19 +0000493 "replace", "the", "", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000494 EQ("here and re and re", "here and there and there",
495 "replace", "the", "", -1)
496 EQ("here and re and re", "here and there and there",
497 "replace", "the", "", 3)
498 EQ("here and re and re", "here and there and there",
499 "replace", "the", "", 2)
500 EQ("here and re and there", "here and there and there",
501 "replace", "the", "", 1)
502 EQ("here and there and there", "here and there and there",
503 "replace", "the", "", 0)
504 EQ("here and re and re", "here and there and there", "replace", "the", "")
505
506 EQ("abc", "abc", "replace", "the", "")
507 EQ("abcdefg", "abcdefg", "replace", "the", "")
508
509 # substring deletion (from=="bob", to=="")
510 EQ("bob", "bbobob", "replace", "bob", "")
511 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
512 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
513 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
514
515 # single character replace in place (len(from)==len(to)==1)
516 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
517 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
Christian Heimesa37d4c62007-12-04 23:02:19 +0000518 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000519 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
520 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
521 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
522 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
523 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
524
525 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
526 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
527 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
528 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
529 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
530
531 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
532
533 # substring replace in place (len(from)==len(to) > 1)
534 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
Christian Heimesa37d4c62007-12-04 23:02:19 +0000535 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000536 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
537 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
538 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
539 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
540 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
541 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
542 EQ("cobob", "bobob", "replace", "bob", "cob")
543 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
544 EQ("bobob", "bobob", "replace", "bot", "bot")
545
546 # replace single character (len(from)==1, len(to)>1)
547 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
548 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
Christian Heimesa37d4c62007-12-04 23:02:19 +0000549 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000550 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
551 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
552 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
553 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
554
555 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
556
557 # replace substring (len(from)>1, len(to)!=len(from))
558 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
559 "replace", "spam", "ham")
560 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
Christian Heimesa37d4c62007-12-04 23:02:19 +0000561 "replace", "spam", "ham", sys.maxsize)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000562 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
563 "replace", "spam", "ham", -1)
564 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
565 "replace", "spam", "ham", 4)
566 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
567 "replace", "spam", "ham", 3)
568 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
569 "replace", "spam", "ham", 2)
570 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
571 "replace", "spam", "ham", 1)
572 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
573 "replace", "spam", "ham", 0)
574
575 EQ("bobob", "bobobob", "replace", "bobob", "bob")
576 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
577 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
578
Guido van Rossum39478e82007-08-27 17:23:59 +0000579 # XXX Commented out. Is there any reason to support buffer objects
580 # as arguments for str.replace()? GvR
Guido van Rossum254348e2007-11-21 19:29:53 +0000581## ba = bytearray('a')
582## bb = bytearray('b')
Guido van Rossum39478e82007-08-27 17:23:59 +0000583## EQ("bbc", "abc", "replace", ba, bb)
584## EQ("aac", "abc", "replace", bb, ba)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000585
Thomas Wouters477c8d52006-05-27 19:21:47 +0000586 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000587 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
588 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
589 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
590 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
591 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
592 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
593 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
594 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
595 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
596 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
597 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
598 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
599 self.checkequal('', '', 'replace', '', '')
600 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
601 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
602 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
603 # MemoryError due to empty result (platform malloc issue when requesting
604 # 0 bytes).
605 self.checkequal('', '123', 'replace', '123', '')
606 self.checkequal('', '123123', 'replace', '123', '')
607 self.checkequal('x', '123x123', 'replace', '123', '')
608
609 self.checkraises(TypeError, 'hello', 'replace')
610 self.checkraises(TypeError, 'hello', 'replace', 42)
611 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
612 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
613
Thomas Wouters477c8d52006-05-27 19:21:47 +0000614 def test_replace_overflow(self):
615 # Check for overflow checking on 32 bit machines
Christian Heimesa37d4c62007-12-04 23:02:19 +0000616 if sys.maxsize != 2147483647 or struct.calcsize("P") > 4:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000617 return
618 A2_16 = "A" * (2**16)
619 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
620 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
621 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
622
Georg Brandlc7885542007-03-06 19:16:20 +0000623
624
625class CommonTest(BaseTest):
626 # This testcase contains test that can be used in all
627 # stringlike classes. Currently this is str, unicode
628 # UserString and the string module.
629
630 def test_hash(self):
631 # SF bug 1054139: += optimization was not invalidating cached hash value
632 a = self.type2test('DNSSEC')
633 b = self.type2test('')
634 for c in a:
635 b += c
636 hash(b)
637 self.assertEqual(hash(a), hash(b))
638
639 def test_capitalize(self):
640 self.checkequal(' hello ', ' hello ', 'capitalize')
641 self.checkequal('Hello ', 'Hello ','capitalize')
642 self.checkequal('Hello ', 'hello ','capitalize')
643 self.checkequal('Aaaa', 'aaaa', 'capitalize')
644 self.checkequal('Aaaa', 'AaAa', 'capitalize')
645
646 self.checkraises(TypeError, 'hello', 'capitalize', 42)
647
648 def test_lower(self):
649 self.checkequal('hello', 'HeLLo', 'lower')
650 self.checkequal('hello', 'hello', 'lower')
651 self.checkraises(TypeError, 'hello', 'lower', 42)
652
653 def test_upper(self):
654 self.checkequal('HELLO', 'HeLLo', 'upper')
655 self.checkequal('HELLO', 'HELLO', 'upper')
656 self.checkraises(TypeError, 'hello', 'upper', 42)
657
658 def test_expandtabs(self):
659 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
660 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
661 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
662 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
663 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
664 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
665 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
666
667 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
668
669 def test_additional_split(self):
670 self.checkequal(['this', 'is', 'the', 'split', 'function'],
671 'this is the split function', 'split')
672
673 # by whitespace
674 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
675 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
676 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
677 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
678 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
679 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000680 sys.maxsize-1)
Georg Brandlc7885542007-03-06 19:16:20 +0000681 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
682 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
683 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
684
685 self.checkequal([], ' ', 'split')
686 self.checkequal(['a'], ' a ', 'split')
687 self.checkequal(['a', 'b'], ' a b ', 'split')
688 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
689 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
690 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
691 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
692 aaa = ' a '*20
693 self.checkequal(['a']*20, aaa, 'split')
694 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
695 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
696
697 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000698 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000699
700 def test_additional_rsplit(self):
701 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
702 'this is the rsplit function', 'rsplit')
703
704 # by whitespace
705 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
706 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
707 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
708 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
709 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
710 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
Christian Heimesa37d4c62007-12-04 23:02:19 +0000711 sys.maxsize-20)
Georg Brandlc7885542007-03-06 19:16:20 +0000712 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
713 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
714 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
715
716 self.checkequal([], ' ', 'rsplit')
717 self.checkequal(['a'], ' a ', 'rsplit')
718 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
719 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
720 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
721 None, 1)
722 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
723 None, 2)
724 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
725 aaa = ' a '*20
726 self.checkequal(['a']*20, aaa, 'rsplit')
727 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
728 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
729
730 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000731 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000732
733 def test_strip(self):
734 self.checkequal('hello', ' hello ', 'strip')
735 self.checkequal('hello ', ' hello ', 'lstrip')
736 self.checkequal(' hello', ' hello ', 'rstrip')
737 self.checkequal('hello', 'hello', 'strip')
738
739 # strip/lstrip/rstrip with None arg
740 self.checkequal('hello', ' hello ', 'strip', None)
741 self.checkequal('hello ', ' hello ', 'lstrip', None)
742 self.checkequal(' hello', ' hello ', 'rstrip', None)
743 self.checkequal('hello', 'hello', 'strip', None)
744
745 # strip/lstrip/rstrip with str arg
746 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
747 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
748 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
749 self.checkequal('hello', 'hello', 'strip', 'xyz')
750
Georg Brandlc7885542007-03-06 19:16:20 +0000751 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
752 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
753 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
754
755 def test_ljust(self):
756 self.checkequal('abc ', 'abc', 'ljust', 10)
757 self.checkequal('abc ', 'abc', 'ljust', 6)
758 self.checkequal('abc', 'abc', 'ljust', 3)
759 self.checkequal('abc', 'abc', 'ljust', 2)
760 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
761 self.checkraises(TypeError, 'abc', 'ljust')
762
763 def test_rjust(self):
764 self.checkequal(' abc', 'abc', 'rjust', 10)
765 self.checkequal(' abc', 'abc', 'rjust', 6)
766 self.checkequal('abc', 'abc', 'rjust', 3)
767 self.checkequal('abc', 'abc', 'rjust', 2)
768 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
769 self.checkraises(TypeError, 'abc', 'rjust')
770
771 def test_center(self):
772 self.checkequal(' abc ', 'abc', 'center', 10)
773 self.checkequal(' abc ', 'abc', 'center', 6)
774 self.checkequal('abc', 'abc', 'center', 3)
775 self.checkequal('abc', 'abc', 'center', 2)
776 self.checkequal('***abc****', 'abc', 'center', 10, '*')
777 self.checkraises(TypeError, 'abc', 'center')
778
779 def test_swapcase(self):
780 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
781
782 self.checkraises(TypeError, 'hello', 'swapcase', 42)
783
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000784 def test_zfill(self):
785 self.checkequal('123', '123', 'zfill', 2)
786 self.checkequal('123', '123', 'zfill', 3)
787 self.checkequal('0123', '123', 'zfill', 4)
788 self.checkequal('+123', '+123', 'zfill', 3)
789 self.checkequal('+123', '+123', 'zfill', 4)
790 self.checkequal('+0123', '+123', 'zfill', 5)
791 self.checkequal('-123', '-123', 'zfill', 3)
792 self.checkequal('-123', '-123', 'zfill', 4)
793 self.checkequal('-0123', '-123', 'zfill', 5)
794 self.checkequal('000', '', 'zfill', 3)
795 self.checkequal('34', '34', 'zfill', 1)
796 self.checkequal('0034', '34', 'zfill', 4)
797
798 self.checkraises(TypeError, '123', 'zfill')
799
800class MixinStrUnicodeUserStringTest:
801 # additional tests that only work for
802 # stringlike objects, i.e. str, unicode, UserString
803 # (but not the string module)
804
805 def test_islower(self):
806 self.checkequal(False, '', 'islower')
807 self.checkequal(True, 'a', 'islower')
808 self.checkequal(False, 'A', 'islower')
809 self.checkequal(False, '\n', 'islower')
810 self.checkequal(True, 'abc', 'islower')
811 self.checkequal(False, 'aBc', 'islower')
812 self.checkequal(True, 'abc\n', 'islower')
813 self.checkraises(TypeError, 'abc', 'islower', 42)
814
815 def test_isupper(self):
816 self.checkequal(False, '', 'isupper')
817 self.checkequal(False, 'a', 'isupper')
818 self.checkequal(True, 'A', 'isupper')
819 self.checkequal(False, '\n', 'isupper')
820 self.checkequal(True, 'ABC', 'isupper')
821 self.checkequal(False, 'AbC', 'isupper')
822 self.checkequal(True, 'ABC\n', 'isupper')
823 self.checkraises(TypeError, 'abc', 'isupper', 42)
824
825 def test_istitle(self):
826 self.checkequal(False, '', 'istitle')
827 self.checkequal(False, 'a', 'istitle')
828 self.checkequal(True, 'A', 'istitle')
829 self.checkequal(False, '\n', 'istitle')
830 self.checkequal(True, 'A Titlecased Line', 'istitle')
831 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
832 self.checkequal(True, 'A Titlecased, Line', 'istitle')
833 self.checkequal(False, 'Not a capitalized String', 'istitle')
834 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
835 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
836 self.checkequal(False, 'NOT', 'istitle')
837 self.checkraises(TypeError, 'abc', 'istitle', 42)
838
839 def test_isspace(self):
840 self.checkequal(False, '', 'isspace')
841 self.checkequal(False, 'a', 'isspace')
842 self.checkequal(True, ' ', 'isspace')
843 self.checkequal(True, '\t', 'isspace')
844 self.checkequal(True, '\r', 'isspace')
845 self.checkequal(True, '\n', 'isspace')
846 self.checkequal(True, ' \t\r\n', 'isspace')
847 self.checkequal(False, ' \t\r\na', 'isspace')
848 self.checkraises(TypeError, 'abc', 'isspace', 42)
849
850 def test_isalpha(self):
851 self.checkequal(False, '', 'isalpha')
852 self.checkequal(True, 'a', 'isalpha')
853 self.checkequal(True, 'A', 'isalpha')
854 self.checkequal(False, '\n', 'isalpha')
855 self.checkequal(True, 'abc', 'isalpha')
856 self.checkequal(False, 'aBc123', 'isalpha')
857 self.checkequal(False, 'abc\n', 'isalpha')
858 self.checkraises(TypeError, 'abc', 'isalpha', 42)
859
860 def test_isalnum(self):
861 self.checkequal(False, '', 'isalnum')
862 self.checkequal(True, 'a', 'isalnum')
863 self.checkequal(True, 'A', 'isalnum')
864 self.checkequal(False, '\n', 'isalnum')
865 self.checkequal(True, '123abc456', 'isalnum')
866 self.checkequal(True, 'a1b3c', 'isalnum')
867 self.checkequal(False, 'aBc000 ', 'isalnum')
868 self.checkequal(False, 'abc\n', 'isalnum')
869 self.checkraises(TypeError, 'abc', 'isalnum', 42)
870
871 def test_isdigit(self):
872 self.checkequal(False, '', 'isdigit')
873 self.checkequal(False, 'a', 'isdigit')
874 self.checkequal(True, '0', 'isdigit')
875 self.checkequal(True, '0123456789', 'isdigit')
876 self.checkequal(False, '0123456789a', 'isdigit')
877
878 self.checkraises(TypeError, 'abc', 'isdigit', 42)
879
880 def test_title(self):
881 self.checkequal(' Hello ', ' hello ', 'title')
882 self.checkequal('Hello ', 'hello ', 'title')
883 self.checkequal('Hello ', 'Hello ', 'title')
884 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
885 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
886 self.checkequal('Getint', "getInt", 'title')
887 self.checkraises(TypeError, 'hello', 'title', 42)
888
889 def test_splitlines(self):
890 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
891 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
892 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
893 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
894 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
895 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
896 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
897
898 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
899
900 def test_startswith(self):
901 self.checkequal(True, 'hello', 'startswith', 'he')
902 self.checkequal(True, 'hello', 'startswith', 'hello')
903 self.checkequal(False, 'hello', 'startswith', 'hello world')
904 self.checkequal(True, 'hello', 'startswith', '')
905 self.checkequal(False, 'hello', 'startswith', 'ello')
906 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
907 self.checkequal(True, 'hello', 'startswith', 'o', 4)
908 self.checkequal(False, 'hello', 'startswith', 'o', 5)
909 self.checkequal(True, 'hello', 'startswith', '', 5)
910 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
911 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
912 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
913 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
914
915 # test negative indices
916 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
917 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
918 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
919 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
920 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
921 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
922 self.checkequal(False, 'hello', 'startswith', 'o', -2)
923 self.checkequal(True, 'hello', 'startswith', 'o', -1)
924 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
925 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
926
927 self.checkraises(TypeError, 'hello', 'startswith')
928 self.checkraises(TypeError, 'hello', 'startswith', 42)
929
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000930 # test tuple arguments
931 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
932 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
933 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
934 self.checkequal(False, 'hello', 'startswith', ())
935 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
936 'rld', 'lowo'), 3)
937 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
938 'rld'), 3)
939 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
940 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
941 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
942
943 self.checkraises(TypeError, 'hello', 'startswith', (42,))
944
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000945 def test_endswith(self):
946 self.checkequal(True, 'hello', 'endswith', 'lo')
947 self.checkequal(False, 'hello', 'endswith', 'he')
948 self.checkequal(True, 'hello', 'endswith', '')
949 self.checkequal(False, 'hello', 'endswith', 'hello world')
950 self.checkequal(False, 'helloworld', 'endswith', 'worl')
951 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
952 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
953 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
954 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
955 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
956 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
957 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
958 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
959 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
960
961 # test negative indices
962 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
963 self.checkequal(False, 'hello', 'endswith', 'he', -2)
964 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
965 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
966 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
967 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
968 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
969 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
970 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
971 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
972 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
973 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
974 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
975
976 self.checkraises(TypeError, 'hello', 'endswith')
977 self.checkraises(TypeError, 'hello', 'endswith', 42)
978
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000979 # test tuple arguments
980 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
981 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
982 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
983 self.checkequal(False, 'hello', 'endswith', ())
984 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
985 'rld', 'lowo'), 3)
986 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
987 'rld'), 3, -1)
988 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
989 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
990 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
991
992 self.checkraises(TypeError, 'hello', 'endswith', (42,))
993
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000994 def test___contains__(self):
995 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
996 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
997 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
998 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
999 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
1000 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
1001 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
1002 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
1003 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
1004
1005 def test_subscript(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001006 self.checkequal('a', 'abc', '__getitem__', 0)
1007 self.checkequal('c', 'abc', '__getitem__', -1)
1008 self.checkequal('a', 'abc', '__getitem__', 0)
1009 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1010 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1011 self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
1012 self.checkequal('', 'abc', '__getitem__', slice(0, 0))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001013
1014 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1015
1016 def test_slice(self):
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001017 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
1018 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
1019 self.checkequal('ab', 'abc', '__getitem__', slice(0, 2))
1020 self.checkequal('bc', 'abc', '__getitem__', slice(1, 3))
1021 self.checkequal('b', 'abc', '__getitem__', slice(1, 2))
1022 self.checkequal('', 'abc', '__getitem__', slice(2, 2))
1023 self.checkequal('', 'abc', '__getitem__', slice(1000, 1000))
1024 self.checkequal('', 'abc', '__getitem__', slice(2000, 1000))
1025 self.checkequal('', 'abc', '__getitem__', slice(2, 1))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001026
Thomas Woutersd2cf20e2007-08-30 22:57:53 +00001027 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001028
Thomas Woutersed03b412007-08-28 21:37:11 +00001029 def test_extended_getslice(self):
1030 # Test extended slicing by comparing with list slicing.
1031 s = string.ascii_letters + string.digits
1032 indices = (0, None, 1, 3, 41, -1, -2, -37)
1033 for start in indices:
1034 for stop in indices:
1035 # Skip step 0 (invalid)
1036 for step in indices[1:]:
1037 L = list(s)[start:stop:step]
1038 self.checkequal("".join(L), s, '__getitem__',
1039 slice(start, stop, step))
1040
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001041 def test_mul(self):
1042 self.checkequal('', 'abc', '__mul__', -1)
1043 self.checkequal('', 'abc', '__mul__', 0)
1044 self.checkequal('abc', 'abc', '__mul__', 1)
1045 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
1046 self.checkraises(TypeError, 'abc', '__mul__')
1047 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +00001048 # XXX: on a 64-bit system, this doesn't raise an overflow error,
1049 # but either raises a MemoryError, or succeeds (if you have 54TiB)
1050 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001051
1052 def test_join(self):
1053 # join now works with any sequence type
1054 # moved here, because the argument order is
1055 # different in string.join (see the test in
1056 # test.test_string.StringTest.test_join)
1057 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
1058 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001059 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
1060 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001061 self.checkequal('w x y z', ' ', 'join', Sequence())
1062 self.checkequal('abc', 'a', 'join', ('abc',))
1063 self.checkequal('z', 'a', 'join', UserList(['z']))
Walter Dörwald67e83882007-05-05 12:26:27 +00001064 self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
Guido van Rossum98297ee2007-11-06 21:34:58 +00001065 self.assertRaises(TypeError, '.'.join, ['a', 'b', 3])
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001066 for i in [5, 25, 125]:
1067 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1068 ['a' * i] * i)
1069 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1070 ('a' * i,) * i)
1071
Guido van Rossum98297ee2007-11-06 21:34:58 +00001072 #self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001073 self.checkequal('a b c', ' ', 'join', BadSeq2())
1074
1075 self.checkraises(TypeError, ' ', 'join')
1076 self.checkraises(TypeError, ' ', 'join', 7)
Guido van Rossumf1044292007-09-27 18:01:22 +00001077 self.checkraises(TypeError, ' ', 'join', [1, 2, bytes()])
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001078 try:
1079 def f():
1080 yield 4 + ""
1081 self.fixtype(' ').join(f())
Guido van Rossumb940e112007-01-10 16:19:56 +00001082 except TypeError as e:
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001083 if '+' not in str(e):
1084 self.fail('join() ate exception message')
1085 else:
1086 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001087
1088 def test_formatting(self):
1089 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1090 self.checkequal('+10+', '+%d+', '__mod__', 10)
1091 self.checkequal('a', "%c", '__mod__', "a")
1092 self.checkequal('a', "%c", '__mod__', "a")
1093 self.checkequal('"', "%c", '__mod__', 34)
1094 self.checkequal('$', "%c", '__mod__', 36)
1095 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001096 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001097
1098 for ordinal in (-100, 0x200000):
1099 # unicode raises ValueError, str raises OverflowError
1100 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1101
Christian Heimesa612dc02008-02-24 13:08:18 +00001102 longvalue = sys.maxsize + 10
1103 slongvalue = str(longvalue)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001104 self.checkequal(' 42', '%3ld', '__mod__', 42)
Christian Heimesa612dc02008-02-24 13:08:18 +00001105 self.checkequal('42', '%d', '__mod__', 42.0)
1106 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1107 self.checkcall('%d', '__mod__', float(longvalue))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001108 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001109 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001110
1111 self.checkraises(TypeError, 'abc', '__mod__')
1112 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1113 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1114 self.checkraises(TypeError, '%c', '__mod__', (None,))
1115 self.checkraises(ValueError, '%(foo', '__mod__', {})
1116 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
Christian Heimesa612dc02008-02-24 13:08:18 +00001117 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
Mark Dickinson5c2db372009-12-05 20:28:34 +00001118 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int conversion provided
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001119
1120 # argument names with properly nested brackets are supported
1121 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1122
1123 # 100 is a magic number in PyUnicode_Format, this forces a resize
1124 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1125
1126 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1127 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1128 self.checkraises(ValueError, '%10', '__mod__', (42,))
1129
1130 def test_floatformatting(self):
1131 # float formatting
Guido van Rossum805365e2007-05-07 22:24:25 +00001132 for prec in range(100):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001133 format = '%%.%if' % prec
1134 value = 0.01
Guido van Rossum805365e2007-05-07 22:24:25 +00001135 for x in range(60):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001136 value = value * 3.141592655 / 3.0 * 10.0
Mark Dickinsonf489caf2009-05-01 11:42:00 +00001137 self.checkcall(format, "__mod__", value)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001138
Thomas Wouters477c8d52006-05-27 19:21:47 +00001139 def test_inplace_rewrites(self):
1140 # Check that strings don't copy and modify cached single-character strings
1141 self.checkequal('a', 'A', 'lower')
1142 self.checkequal(True, 'A', 'isupper')
1143 self.checkequal('A', 'a', 'upper')
1144 self.checkequal(True, 'a', 'islower')
1145
1146 self.checkequal('a', 'A', 'replace', 'A', 'a')
1147 self.checkequal(True, 'A', 'isupper')
1148
1149 self.checkequal('A', 'a', 'capitalize')
1150 self.checkequal(True, 'a', 'islower')
1151
1152 self.checkequal('A', 'a', 'swapcase')
1153 self.checkequal(True, 'a', 'islower')
1154
1155 self.checkequal('A', 'a', 'title')
1156 self.checkequal(True, 'a', 'islower')
1157
1158 def test_partition(self):
1159
1160 self.checkequal(('this is the par', 'ti', 'tion method'),
1161 'this is the partition method', 'partition', 'ti')
1162
1163 # from raymond's original specification
1164 S = 'http://www.python.org'
1165 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1166 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1167 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1168 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1169
1170 self.checkraises(ValueError, S, 'partition', '')
1171 self.checkraises(TypeError, S, 'partition', None)
1172
1173 def test_rpartition(self):
1174
1175 self.checkequal(('this is the rparti', 'ti', 'on method'),
1176 'this is the rpartition method', 'rpartition', 'ti')
1177
1178 # from raymond's original specification
1179 S = 'http://www.python.org'
1180 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001181 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Thomas Wouters477c8d52006-05-27 19:21:47 +00001182 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1183 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1184
1185 self.checkraises(ValueError, S, 'rpartition', '')
1186 self.checkraises(TypeError, S, 'rpartition', None)
1187
Walter Dörwald57d88e52004-08-26 16:53:04 +00001188
Walter Dörwald57d88e52004-08-26 16:53:04 +00001189class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001190 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001191
1192 def test_bug1001011(self):
1193 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001194 # involving a subclass.
1195 # Make sure that it is of the appropriate type.
1196 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001197 t = self.type2test
1198 class subclass(t):
1199 pass
1200 s1 = subclass("abcd")
1201 s2 = t().join([s1])
1202 self.assert_(s1 is not s2)
1203 self.assert_(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001204
1205 s1 = t("abcd")
1206 s2 = t().join([s1])
1207 self.assert_(s1 is s2)
1208
1209 # Should also test mixed-type join.
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001210 if t is str:
Tim Peters108f1372004-08-27 05:36:07 +00001211 s1 = subclass("abcd")
1212 s2 = "".join([s1])
1213 self.assert_(s1 is not s2)
1214 self.assert_(type(s2) is t)
1215
1216 s1 = t("abcd")
1217 s2 = "".join([s1])
1218 self.assert_(s1 is s2)
1219
Guido van Rossum98297ee2007-11-06 21:34:58 +00001220## elif t is str8:
1221## s1 = subclass("abcd")
1222## s2 = "".join([s1])
1223## self.assert_(s1 is not s2)
1224## self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001225
Guido van Rossum98297ee2007-11-06 21:34:58 +00001226## s1 = t("abcd")
1227## s2 = "".join([s1])
1228## self.assert_(s1 is not s2)
1229## self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001230
1231 else:
1232 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)