blob: 9cf334573d68ef54d5ea39ecb5a67b8876a3af94 [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
Kristján Valur Jónsson170eee92007-05-03 20:09:56 +00005import unittest, string, sys, struct
Walter Dörwald0fd583c2003-02-21 12:53:50 +00006from test import test_support
Jeremy Hylton20f41b62000-07-11 03:31:55 +00007from UserList import UserList
8
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):
15 def __init__(self): self.seq = [7, 'hello', 123L]
16
17class BadSeq2(Sequence):
18 def __init__(self): self.seq = ['a', 'b', 'c']
19 def __len__(self): return 8
20
Walter Dörwald0fd583c2003-02-21 12:53:50 +000021class CommonTest(unittest.TestCase):
22 # This testcase contains test that can be used in all
23 # stringlike classes. Currently this is str, unicode
24 # UserString and the string module.
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000025
Walter Dörwald0fd583c2003-02-21 12:53:50 +000026 # The type to be tested
27 # Change in subclasses to change the behaviour of fixtesttype()
28 type2test = None
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000029
Walter Dörwald0fd583c2003-02-21 12:53:50 +000030 # All tests pass their arguments to the testing methods
31 # as str objects. fixtesttype() can be used to propagate
32 # these arguments to the appropriate type
33 def fixtype(self, obj):
34 if isinstance(obj, str):
35 return self.__class__.type2test(obj)
36 elif isinstance(obj, list):
37 return [self.fixtype(x) for x in obj]
38 elif isinstance(obj, tuple):
39 return tuple([self.fixtype(x) for x in obj])
40 elif isinstance(obj, dict):
41 return dict([
42 (self.fixtype(key), self.fixtype(value))
43 for (key, value) in obj.iteritems()
44 ])
45 else:
46 return obj
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000047
Walter Dörwald0fd583c2003-02-21 12:53:50 +000048 # check that object.method(*args) returns result
49 def checkequal(self, result, object, methodname, *args):
50 result = self.fixtype(result)
51 object = self.fixtype(object)
52 args = self.fixtype(args)
53 realresult = getattr(object, methodname)(*args)
54 self.assertEqual(
55 result,
56 realresult
57 )
58 # if the original is returned make sure that
59 # this doesn't happen with subclasses
60 if object == realresult:
61 class subtype(self.__class__.type2test):
62 pass
63 object = subtype(object)
64 realresult = getattr(object, methodname)(*args)
Ezio Melotti2623a372010-11-21 13:34:58 +000065 self.assertTrue(object is not realresult)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000066
Walter Dörwald0fd583c2003-02-21 12:53:50 +000067 # check that object.method(*args) raises exc
68 def checkraises(self, exc, object, methodname, *args):
69 object = self.fixtype(object)
70 args = self.fixtype(args)
71 self.assertRaises(
72 exc,
73 getattr(object, methodname),
74 *args
75 )
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000076
Walter Dörwald0fd583c2003-02-21 12:53:50 +000077 # call object.method(*args) without any checks
78 def checkcall(self, object, methodname, *args):
79 object = self.fixtype(object)
80 args = self.fixtype(args)
81 getattr(object, methodname)(*args)
82
Raymond Hettinger561fbf12004-10-26 01:52:37 +000083 def test_hash(self):
84 # SF bug 1054139: += optimization was not invalidating cached hash value
85 a = self.type2test('DNSSEC')
86 b = self.type2test('')
87 for c in a:
88 b += c
89 hash(b)
90 self.assertEqual(hash(a), hash(b))
91
Walter Dörwald0fd583c2003-02-21 12:53:50 +000092 def test_capitalize(self):
93 self.checkequal(' hello ', ' hello ', 'capitalize')
94 self.checkequal('Hello ', 'Hello ','capitalize')
95 self.checkequal('Hello ', 'hello ','capitalize')
96 self.checkequal('Aaaa', 'aaaa', 'capitalize')
97 self.checkequal('Aaaa', 'AaAa', 'capitalize')
98
Ezio Melotti15d6b652011-08-15 09:22:24 +030099 # check that titlecased chars are lowered correctly
100 # \u1ffc is the titlecased char
101 self.checkequal(u'\u1ffc\u1ff3\u1ff3\u1ff3',
102 u'\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize')
103 # check with cased non-letter chars
104 self.checkequal(u'\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
105 u'\u24c5\u24ce\u24c9\u24bd\u24c4\u24c3', 'capitalize')
106 self.checkequal(u'\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
107 u'\u24df\u24e8\u24e3\u24d7\u24de\u24dd', 'capitalize')
108 self.checkequal(u'\u2160\u2171\u2172',
109 u'\u2160\u2161\u2162', 'capitalize')
110 self.checkequal(u'\u2160\u2171\u2172',
111 u'\u2170\u2171\u2172', 'capitalize')
112 # check with Ll chars with no upper - nothing changes here
113 self.checkequal(u'\u019b\u1d00\u1d86\u0221\u1fb7',
114 u'\u019b\u1d00\u1d86\u0221\u1fb7', 'capitalize')
115
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000116 self.checkraises(TypeError, 'hello', 'capitalize', 42)
117
118 def test_count(self):
119 self.checkequal(3, 'aaa', 'count', 'a')
120 self.checkequal(0, 'aaa', 'count', 'b')
121 self.checkequal(3, 'aaa', 'count', 'a')
122 self.checkequal(0, 'aaa', 'count', 'b')
123 self.checkequal(3, 'aaa', 'count', 'a')
124 self.checkequal(0, 'aaa', 'count', 'b')
125 self.checkequal(0, 'aaa', 'count', 'b')
Fredrik Lundhb51b4702006-05-29 22:42:07 +0000126 self.checkequal(2, 'aaa', 'count', 'a', 1)
127 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000128 self.checkequal(1, 'aaa', 'count', 'a', -1)
129 self.checkequal(3, 'aaa', 'count', 'a', -10)
Fredrik Lundhb51b4702006-05-29 22:42:07 +0000130 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
131 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000132 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
133 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Fredrik Lundhb51b4702006-05-29 22:42:07 +0000134 self.checkequal(3, 'aaa', 'count', '', 1)
Fredrik Lundh9e9ef9f2006-05-30 17:39:58 +0000135 self.checkequal(1, 'aaa', 'count', '', 3)
136 self.checkequal(0, 'aaa', 'count', '', 10)
Fredrik Lundhb51b4702006-05-29 22:42:07 +0000137 self.checkequal(2, 'aaa', 'count', '', -1)
138 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000139
Amaury Forgeot d'Arcfc5ea392008-09-26 22:34:08 +0000140 self.checkequal(1, '', 'count', '')
141 self.checkequal(0, '', 'count', '', 1, 1)
142 self.checkequal(0, '', 'count', '', sys.maxint, 0)
143
144 self.checkequal(0, '', 'count', 'xx')
145 self.checkequal(0, '', 'count', 'xx', 1, 1)
146 self.checkequal(0, '', 'count', 'xx', sys.maxint, 0)
147
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000148 self.checkraises(TypeError, 'hello', 'count')
149 self.checkraises(TypeError, 'hello', 'count', 42)
150
Raymond Hettinger57e74472005-02-20 09:54:53 +0000151 # For a variety of combinations,
152 # verify that str.count() matches an equivalent function
153 # replacing all occurrences and then differencing the string lengths
154 charset = ['', 'a', 'b']
155 digits = 7
156 base = len(charset)
157 teststrings = set()
158 for i in xrange(base ** digits):
159 entry = []
160 for j in xrange(digits):
161 i, m = divmod(i, base)
162 entry.append(charset[m])
163 teststrings.add(''.join(entry))
164 teststrings = list(teststrings)
165 for i in teststrings:
166 i = self.fixtype(i)
167 n = len(i)
168 for j in teststrings:
169 r1 = i.count(j)
170 if j:
171 r2, rem = divmod(n - len(i.replace(j, '')), len(j))
172 else:
173 r2, rem = len(i)+1, 0
174 if rem or r1 != r2:
Neal Norwitzf71ec5a2006-07-30 06:57:04 +0000175 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
176 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000177
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000178 def test_find(self):
179 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
180 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
181 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
182
Fredrik Lundh93eff6f2006-05-30 17:11:48 +0000183 self.checkequal(0, 'abc', 'find', '', 0)
184 self.checkequal(3, 'abc', 'find', '', 3)
185 self.checkequal(-1, 'abc', 'find', '', 4)
186
Facundo Batista57d56692007-11-16 18:04:14 +0000187 # to check the ability to pass None as defaults
188 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
189 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
190 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
191 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
192 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
193
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000194 self.checkraises(TypeError, 'hello', 'find')
195 self.checkraises(TypeError, 'hello', 'find', 42)
196
Amaury Forgeot d'Arcfc5ea392008-09-26 22:34:08 +0000197 self.checkequal(0, '', 'find', '')
198 self.checkequal(-1, '', 'find', '', 1, 1)
199 self.checkequal(-1, '', 'find', '', sys.maxint, 0)
200
201 self.checkequal(-1, '', 'find', 'xx')
202 self.checkequal(-1, '', 'find', 'xx', 1, 1)
203 self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0)
204
Antoine Pitrou83f86e82010-01-02 21:47:10 +0000205 # issue 7458
206 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
207
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000208 # For a variety of combinations,
209 # verify that str.find() matches __contains__
210 # and that the found substring is really at that location
211 charset = ['', 'a', 'b', 'c']
212 digits = 5
213 base = len(charset)
214 teststrings = set()
215 for i in xrange(base ** digits):
216 entry = []
217 for j in xrange(digits):
218 i, m = divmod(i, base)
219 entry.append(charset[m])
220 teststrings.add(''.join(entry))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000221 teststrings = list(teststrings)
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000222 for i in teststrings:
223 i = self.fixtype(i)
224 for j in teststrings:
225 loc = i.find(j)
226 r1 = (loc != -1)
227 r2 = j in i
Antoine Pitroub538d542010-01-02 21:53:44 +0000228 self.assertEqual(r1, r2)
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000229 if loc != -1:
230 self.assertEqual(i[loc:loc+len(j)], j)
231
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000232 def test_rfind(self):
233 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
234 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
235 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
236 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
237
Fredrik Lundh93eff6f2006-05-30 17:11:48 +0000238 self.checkequal(3, 'abc', 'rfind', '', 0)
239 self.checkequal(3, 'abc', 'rfind', '', 3)
240 self.checkequal(-1, 'abc', 'rfind', '', 4)
241
Facundo Batista57d56692007-11-16 18:04:14 +0000242 # to check the ability to pass None as defaults
243 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
244 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
245 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
246 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
247 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
248
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000249 self.checkraises(TypeError, 'hello', 'rfind')
250 self.checkraises(TypeError, 'hello', 'rfind', 42)
251
Antoine Pitrou5b7139a2010-01-02 21:12:58 +0000252 # For a variety of combinations,
253 # verify that str.rfind() matches __contains__
254 # and that the found substring is really at that location
255 charset = ['', 'a', 'b', 'c']
256 digits = 5
257 base = len(charset)
258 teststrings = set()
259 for i in xrange(base ** digits):
260 entry = []
261 for j in xrange(digits):
262 i, m = divmod(i, base)
263 entry.append(charset[m])
264 teststrings.add(''.join(entry))
265 teststrings = list(teststrings)
266 for i in teststrings:
267 i = self.fixtype(i)
268 for j in teststrings:
269 loc = i.rfind(j)
270 r1 = (loc != -1)
271 r2 = j in i
Antoine Pitroub538d542010-01-02 21:53:44 +0000272 self.assertEqual(r1, r2)
Antoine Pitrou5b7139a2010-01-02 21:12:58 +0000273 if loc != -1:
Florent Xiclunac0c0b142010-09-13 08:53:00 +0000274 self.assertEqual(i[loc:loc+len(j)], self.fixtype(j))
Antoine Pitrou5b7139a2010-01-02 21:12:58 +0000275
Antoine Pitrou83f86e82010-01-02 21:47:10 +0000276 # issue 7458
277 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
278
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000279 def test_index(self):
280 self.checkequal(0, 'abcdefghiabc', 'index', '')
281 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
282 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
283 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
284
285 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
286 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
287 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
288 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
289
Facundo Batista57d56692007-11-16 18:04:14 +0000290 # to check the ability to pass None as defaults
291 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
292 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
293 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
294 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
295 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
296
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000297 self.checkraises(TypeError, 'hello', 'index')
298 self.checkraises(TypeError, 'hello', 'index', 42)
299
300 def test_rindex(self):
301 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
302 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
303 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
304 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
305
306 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
307 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
308 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
309 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
310 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
311
Facundo Batista57d56692007-11-16 18:04:14 +0000312 # to check the ability to pass None as defaults
313 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
314 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
315 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
316 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
317 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
318
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000319 self.checkraises(TypeError, 'hello', 'rindex')
320 self.checkraises(TypeError, 'hello', 'rindex', 42)
321
322 def test_lower(self):
323 self.checkequal('hello', 'HeLLo', 'lower')
324 self.checkequal('hello', 'hello', 'lower')
325 self.checkraises(TypeError, 'hello', 'lower', 42)
326
327 def test_upper(self):
328 self.checkequal('HELLO', 'HeLLo', 'upper')
329 self.checkequal('HELLO', 'HELLO', 'upper')
330 self.checkraises(TypeError, 'hello', 'upper', 42)
331
332 def test_expandtabs(self):
333 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
334 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
335 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
336 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
337 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
338 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
339 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
Neal Norwitz5c9a81a2007-06-11 02:16:10 +0000340 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000341
342 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
Neal Norwitz5c9a81a2007-06-11 02:16:10 +0000343 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
344 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
345 self.checkraises(OverflowError,
346 '\ta\n\tb', 'expandtabs', sys.maxint)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000347
348 def test_split(self):
349 self.checkequal(['this', 'is', 'the', 'split', 'function'],
350 'this is the split function', 'split')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000351
352 # by whitespace
353 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000354 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
355 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
356 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
357 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
Andrew Dalke005aee22006-05-26 12:28:15 +0000358 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
359 sys.maxint-1)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000360 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
Andrew Dalke725fe402006-05-26 16:22:52 +0000361 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000362 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000363
Andrew Dalke984b9712006-05-26 11:11:38 +0000364 self.checkequal([], ' ', 'split')
365 self.checkequal(['a'], ' a ', 'split')
366 self.checkequal(['a', 'b'], ' a b ', 'split')
367 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
368 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
369 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
Andrew Dalke03fb4442006-05-26 11:15:22 +0000370 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
Andrew Dalke005aee22006-05-26 12:28:15 +0000371 aaa = ' a '*20
372 self.checkequal(['a']*20, aaa, 'split')
373 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
Andrew Dalke669fa182006-05-26 13:05:55 +0000374 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
Andrew Dalke984b9712006-05-26 11:11:38 +0000375
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000376 # by a char
377 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Andrew Dalke005aee22006-05-26 12:28:15 +0000378 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000379 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
380 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
381 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
382 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Andrew Dalke005aee22006-05-26 12:28:15 +0000383 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
384 sys.maxint-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000385 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
386 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
387 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Andrew Dalke005aee22006-05-26 12:28:15 +0000388 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
389 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000390 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
391
Andrew Dalke005aee22006-05-26 12:28:15 +0000392 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
393 self.checkequal(['a']*15 +['a|a|a|a|a'],
394 ('a|'*20)[:-1], 'split', '|', 15)
395
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000396 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000397 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000398 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
399 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
400 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
401 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Andrew Dalke005aee22006-05-26 12:28:15 +0000402 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
403 sys.maxint-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000404 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
405 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000406 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Andrew Dalke669fa182006-05-26 13:05:55 +0000407 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
408 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
409 'split', 'test')
410 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
Andrew Dalke005aee22006-05-26 12:28:15 +0000411 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
412 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
413 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
414 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
415 self.checkequal([''], '', 'split', 'aaa')
416 self.checkequal(['aa'], 'aa', 'split', 'aaa')
Andrew Dalke5cc60092006-05-26 12:31:00 +0000417 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
418 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
Andrew Dalke005aee22006-05-26 12:28:15 +0000419
420 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
421 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
422 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
423 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000424
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000425 # mixed use of str and unicode
426 self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2)
427
428 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000429 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
430
Andrew Dalke005aee22006-05-26 12:28:15 +0000431 # null case
432 self.checkraises(ValueError, 'hello', 'split', '')
433 self.checkraises(ValueError, 'hello', 'split', '', 0)
434
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000435 def test_rsplit(self):
436 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
437 'this is the rsplit function', 'rsplit')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000438
439 # by whitespace
440 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000441 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
442 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
443 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
444 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
Andrew Dalke669fa182006-05-26 13:05:55 +0000445 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
446 sys.maxint-20)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000447 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
Andrew Dalke725fe402006-05-26 16:22:52 +0000448 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000449 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000450
Andrew Dalke669fa182006-05-26 13:05:55 +0000451 self.checkequal([], ' ', 'rsplit')
452 self.checkequal(['a'], ' a ', 'rsplit')
453 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
454 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
455 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
456 None, 1)
457 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
458 None, 2)
459 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
460 aaa = ' a '*20
461 self.checkequal(['a']*20, aaa, 'rsplit')
462 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
463 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
464
465
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000466 # by a char
467 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
468 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
469 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
470 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
471 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Andrew Dalke669fa182006-05-26 13:05:55 +0000472 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
473 sys.maxint-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000474 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
475 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
476 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Andrew Dalke669fa182006-05-26 13:05:55 +0000477 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
478 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
479
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000480 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
481
Andrew Dalke669fa182006-05-26 13:05:55 +0000482 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
483 self.checkequal(['a|a|a|a|a']+['a']*15,
484 ('a|'*20)[:-1], 'rsplit', '|', 15)
485
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000486 # by string
487 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
488 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
489 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
490 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
491 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Andrew Dalke669fa182006-05-26 13:05:55 +0000492 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
493 sys.maxint-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000494 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
495 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
496 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Andrew Dalke669fa182006-05-26 13:05:55 +0000497 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
498 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
499 'rsplit', 'test')
500 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
501 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
502 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
503 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
504 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
505 self.checkequal([''], '', 'rsplit', 'aaa')
506 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
507 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
508 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
509
510 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
511 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
512 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
513 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000514
515 # mixed use of str and unicode
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000516 self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000517
518 # argument type
519 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000520
Andrew Dalke669fa182006-05-26 13:05:55 +0000521 # null case
522 self.checkraises(ValueError, 'hello', 'rsplit', '')
523 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
524
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000525 def test_strip(self):
526 self.checkequal('hello', ' hello ', 'strip')
527 self.checkequal('hello ', ' hello ', 'lstrip')
528 self.checkequal(' hello', ' hello ', 'rstrip')
529 self.checkequal('hello', 'hello', 'strip')
530
Neal Norwitzffe33b72003-04-10 22:35:32 +0000531 # strip/lstrip/rstrip with None arg
532 self.checkequal('hello', ' hello ', 'strip', None)
533 self.checkequal('hello ', ' hello ', 'lstrip', None)
534 self.checkequal(' hello', ' hello ', 'rstrip', None)
535 self.checkequal('hello', 'hello', 'strip', None)
536
537 # strip/lstrip/rstrip with str arg
538 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
539 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
540 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
541 self.checkequal('hello', 'hello', 'strip', 'xyz')
542
543 # strip/lstrip/rstrip with unicode arg
544 if test_support.have_unicode:
545 self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
546 'strip', unicode('xyz', 'ascii'))
547 self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
548 'lstrip', unicode('xyz', 'ascii'))
549 self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
550 'rstrip', unicode('xyz', 'ascii'))
Christian Heimes1a6387e2008-03-26 12:49:49 +0000551 # XXX
552 #self.checkequal(unicode('hello', 'ascii'), 'hello',
553 # 'strip', unicode('xyz', 'ascii'))
Neal Norwitzffe33b72003-04-10 22:35:32 +0000554
555 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
556 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
557 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
558
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000559 def test_ljust(self):
560 self.checkequal('abc ', 'abc', 'ljust', 10)
561 self.checkequal('abc ', 'abc', 'ljust', 6)
562 self.checkequal('abc', 'abc', 'ljust', 3)
563 self.checkequal('abc', 'abc', 'ljust', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000564 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000565 self.checkraises(TypeError, 'abc', 'ljust')
566
567 def test_rjust(self):
568 self.checkequal(' abc', 'abc', 'rjust', 10)
569 self.checkequal(' abc', 'abc', 'rjust', 6)
570 self.checkequal('abc', 'abc', 'rjust', 3)
571 self.checkequal('abc', 'abc', 'rjust', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000572 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000573 self.checkraises(TypeError, 'abc', 'rjust')
574
575 def test_center(self):
576 self.checkequal(' abc ', 'abc', 'center', 10)
577 self.checkequal(' abc ', 'abc', 'center', 6)
578 self.checkequal('abc', 'abc', 'center', 3)
579 self.checkequal('abc', 'abc', 'center', 2)
Raymond Hettinger4f8f9762003-11-26 08:21:35 +0000580 self.checkequal('***abc****', 'abc', 'center', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000581 self.checkraises(TypeError, 'abc', 'center')
582
583 def test_swapcase(self):
584 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
585
586 self.checkraises(TypeError, 'hello', 'swapcase', 42)
587
588 def test_replace(self):
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000589 EQ = self.checkequal
590
591 # Operations on the empty string
592 EQ("", "", "replace", "", "")
Tim Peters80a18f02006-06-01 13:56:26 +0000593 EQ("A", "", "replace", "", "A")
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000594 EQ("", "", "replace", "A", "")
595 EQ("", "", "replace", "A", "A")
596 EQ("", "", "replace", "", "", 100)
597 EQ("", "", "replace", "", "", sys.maxint)
598
599 # interleave (from=="", 'to' gets inserted everywhere)
600 EQ("A", "A", "replace", "", "")
601 EQ("*A*", "A", "replace", "", "*")
602 EQ("*1A*1", "A", "replace", "", "*1")
603 EQ("*-#A*-#", "A", "replace", "", "*-#")
604 EQ("*-A*-A*-", "AA", "replace", "", "*-")
605 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
606 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
607 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
608 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
609 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
610 EQ("*-AA", "AA", "replace", "", "*-", 1)
611 EQ("AA", "AA", "replace", "", "*-", 0)
612
613 # single character deletion (from=="A", to=="")
614 EQ("", "A", "replace", "A", "")
615 EQ("", "AAA", "replace", "A", "")
616 EQ("", "AAA", "replace", "A", "", -1)
617 EQ("", "AAA", "replace", "A", "", sys.maxint)
618 EQ("", "AAA", "replace", "A", "", 4)
619 EQ("", "AAA", "replace", "A", "", 3)
620 EQ("A", "AAA", "replace", "A", "", 2)
621 EQ("AA", "AAA", "replace", "A", "", 1)
622 EQ("AAA", "AAA", "replace", "A", "", 0)
623 EQ("", "AAAAAAAAAA", "replace", "A", "")
624 EQ("BCD", "ABACADA", "replace", "A", "")
625 EQ("BCD", "ABACADA", "replace", "A", "", -1)
626 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
627 EQ("BCD", "ABACADA", "replace", "A", "", 5)
628 EQ("BCD", "ABACADA", "replace", "A", "", 4)
629 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
630 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
631 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
632 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
633 EQ("BCD", "ABCAD", "replace", "A", "")
634 EQ("BCD", "ABCADAA", "replace", "A", "")
635 EQ("BCD", "BCD", "replace", "A", "")
636 EQ("*************", "*************", "replace", "A", "")
637 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
638
639 # substring deletion (from=="the", to=="")
640 EQ("", "the", "replace", "the", "")
641 EQ("ater", "theater", "replace", "the", "")
642 EQ("", "thethe", "replace", "the", "")
643 EQ("", "thethethethe", "replace", "the", "")
644 EQ("aaaa", "theatheatheathea", "replace", "the", "")
645 EQ("that", "that", "replace", "the", "")
646 EQ("thaet", "thaet", "replace", "the", "")
647 EQ("here and re", "here and there", "replace", "the", "")
648 EQ("here and re and re", "here and there and there",
649 "replace", "the", "", sys.maxint)
650 EQ("here and re and re", "here and there and there",
651 "replace", "the", "", -1)
652 EQ("here and re and re", "here and there and there",
653 "replace", "the", "", 3)
654 EQ("here and re and re", "here and there and there",
655 "replace", "the", "", 2)
656 EQ("here and re and there", "here and there and there",
657 "replace", "the", "", 1)
658 EQ("here and there and there", "here and there and there",
659 "replace", "the", "", 0)
660 EQ("here and re and re", "here and there and there", "replace", "the", "")
661
662 EQ("abc", "abc", "replace", "the", "")
663 EQ("abcdefg", "abcdefg", "replace", "the", "")
664
665 # substring deletion (from=="bob", to=="")
666 EQ("bob", "bbobob", "replace", "bob", "")
667 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
668 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
669 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
Tim Petersbeaec0c2006-05-24 20:27:18 +0000670
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000671 # single character replace in place (len(from)==len(to)==1)
672 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
673 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
674 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
675 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
676 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
677 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
678 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
679 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
680
681 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
682 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
683 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
684 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
685 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
686
687 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
Tim Petersbeaec0c2006-05-24 20:27:18 +0000688
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000689 # substring replace in place (len(from)==len(to) > 1)
690 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
691 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
692 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
693 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
694 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
695 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
696 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
697 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
698 EQ("cobob", "bobob", "replace", "bob", "cob")
699 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
700 EQ("bobob", "bobob", "replace", "bot", "bot")
701
702 # replace single character (len(from)==1, len(to)>1)
703 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
704 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
705 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
706 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
707 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
708 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
709 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
710
711 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
712
713 # replace substring (len(from)>1, len(to)!=len(from))
714 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
715 "replace", "spam", "ham")
716 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
717 "replace", "spam", "ham", sys.maxint)
718 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
719 "replace", "spam", "ham", -1)
720 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
721 "replace", "spam", "ham", 4)
722 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
723 "replace", "spam", "ham", 3)
724 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
725 "replace", "spam", "ham", 2)
726 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
727 "replace", "spam", "ham", 1)
728 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
729 "replace", "spam", "ham", 0)
730
731 EQ("bobob", "bobobob", "replace", "bobob", "bob")
732 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
733 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
Tim Petersbeaec0c2006-05-24 20:27:18 +0000734
Florent Xicluna6de9e932010-03-07 12:18:33 +0000735 with test_support.check_py3k_warnings():
Antoine Pitrou5b7139a2010-01-02 21:12:58 +0000736 ba = buffer('a')
737 bb = buffer('b')
Neal Norwitzf71ec5a2006-07-30 06:57:04 +0000738 EQ("bbc", "abc", "replace", ba, bb)
739 EQ("aac", "abc", "replace", bb, ba)
740
Tim Petersbeaec0c2006-05-24 20:27:18 +0000741 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000742 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
743 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
744 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
745 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
746 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
747 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
748 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
749 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
750 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
751 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
752 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
753 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
754 self.checkequal('', '', 'replace', '', '')
755 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
756 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
757 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
758 # MemoryError due to empty result (platform malloc issue when requesting
759 # 0 bytes).
760 self.checkequal('', '123', 'replace', '123', '')
761 self.checkequal('', '123123', 'replace', '123', '')
762 self.checkequal('x', '123x123', 'replace', '123', '')
763
764 self.checkraises(TypeError, 'hello', 'replace')
765 self.checkraises(TypeError, 'hello', 'replace', 42)
766 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
767 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
768
Fredrik Lundh0c71f882006-05-25 16:46:54 +0000769 def test_replace_overflow(self):
770 # Check for overflow checking on 32 bit machines
Kristján Valur Jónsson170eee92007-05-03 20:09:56 +0000771 if sys.maxint != 2147483647 or struct.calcsize("P") > 4:
Fredrik Lundh0c71f882006-05-25 16:46:54 +0000772 return
773 A2_16 = "A" * (2**16)
774 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
775 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
776 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000777
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
Christian Heimes1a6387e2008-03-26 12:49:49 +0000794# XXX alias for py3k forward compatibility
795BaseTest = CommonTest
796
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000797class MixinStrUnicodeUserStringTest:
798 # additional tests that only work for
799 # stringlike objects, i.e. str, unicode, UserString
800 # (but not the string module)
801
802 def test_islower(self):
803 self.checkequal(False, '', 'islower')
804 self.checkequal(True, 'a', 'islower')
805 self.checkequal(False, 'A', 'islower')
806 self.checkequal(False, '\n', 'islower')
807 self.checkequal(True, 'abc', 'islower')
808 self.checkequal(False, 'aBc', 'islower')
809 self.checkequal(True, 'abc\n', 'islower')
810 self.checkraises(TypeError, 'abc', 'islower', 42)
811
812 def test_isupper(self):
813 self.checkequal(False, '', 'isupper')
814 self.checkequal(False, 'a', 'isupper')
815 self.checkequal(True, 'A', 'isupper')
816 self.checkequal(False, '\n', 'isupper')
817 self.checkequal(True, 'ABC', 'isupper')
818 self.checkequal(False, 'AbC', 'isupper')
819 self.checkequal(True, 'ABC\n', 'isupper')
820 self.checkraises(TypeError, 'abc', 'isupper', 42)
821
822 def test_istitle(self):
823 self.checkequal(False, '', 'istitle')
824 self.checkequal(False, 'a', 'istitle')
825 self.checkequal(True, 'A', 'istitle')
826 self.checkequal(False, '\n', 'istitle')
827 self.checkequal(True, 'A Titlecased Line', 'istitle')
828 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
829 self.checkequal(True, 'A Titlecased, Line', 'istitle')
830 self.checkequal(False, 'Not a capitalized String', 'istitle')
831 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
832 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
833 self.checkequal(False, 'NOT', 'istitle')
834 self.checkraises(TypeError, 'abc', 'istitle', 42)
835
836 def test_isspace(self):
837 self.checkequal(False, '', 'isspace')
838 self.checkequal(False, 'a', 'isspace')
839 self.checkequal(True, ' ', 'isspace')
840 self.checkequal(True, '\t', 'isspace')
841 self.checkequal(True, '\r', 'isspace')
842 self.checkequal(True, '\n', 'isspace')
843 self.checkequal(True, ' \t\r\n', 'isspace')
844 self.checkequal(False, ' \t\r\na', 'isspace')
845 self.checkraises(TypeError, 'abc', 'isspace', 42)
846
847 def test_isalpha(self):
848 self.checkequal(False, '', 'isalpha')
849 self.checkequal(True, 'a', 'isalpha')
850 self.checkequal(True, 'A', 'isalpha')
851 self.checkequal(False, '\n', 'isalpha')
852 self.checkequal(True, 'abc', 'isalpha')
853 self.checkequal(False, 'aBc123', 'isalpha')
854 self.checkequal(False, 'abc\n', 'isalpha')
855 self.checkraises(TypeError, 'abc', 'isalpha', 42)
856
857 def test_isalnum(self):
858 self.checkequal(False, '', 'isalnum')
859 self.checkequal(True, 'a', 'isalnum')
860 self.checkequal(True, 'A', 'isalnum')
861 self.checkequal(False, '\n', 'isalnum')
862 self.checkequal(True, '123abc456', 'isalnum')
863 self.checkequal(True, 'a1b3c', 'isalnum')
864 self.checkequal(False, 'aBc000 ', 'isalnum')
865 self.checkequal(False, 'abc\n', 'isalnum')
866 self.checkraises(TypeError, 'abc', 'isalnum', 42)
867
868 def test_isdigit(self):
869 self.checkequal(False, '', 'isdigit')
870 self.checkequal(False, 'a', 'isdigit')
871 self.checkequal(True, '0', 'isdigit')
872 self.checkequal(True, '0123456789', 'isdigit')
873 self.checkequal(False, '0123456789a', 'isdigit')
874
875 self.checkraises(TypeError, 'abc', 'isdigit', 42)
876
877 def test_title(self):
878 self.checkequal(' Hello ', ' hello ', 'title')
879 self.checkequal('Hello ', 'hello ', 'title')
880 self.checkequal('Hello ', 'Hello ', 'title')
881 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
882 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
883 self.checkequal('Getint', "getInt", 'title')
884 self.checkraises(TypeError, 'hello', 'title', 42)
885
886 def test_splitlines(self):
887 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
888 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
889 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
890 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
891 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
892 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
893 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
894
895 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
896
897 def test_startswith(self):
898 self.checkequal(True, 'hello', 'startswith', 'he')
899 self.checkequal(True, 'hello', 'startswith', 'hello')
900 self.checkequal(False, 'hello', 'startswith', 'hello world')
901 self.checkequal(True, 'hello', 'startswith', '')
902 self.checkequal(False, 'hello', 'startswith', 'ello')
903 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
904 self.checkequal(True, 'hello', 'startswith', 'o', 4)
905 self.checkequal(False, 'hello', 'startswith', 'o', 5)
906 self.checkequal(True, 'hello', 'startswith', '', 5)
907 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
908 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
909 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
910 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
911
912 # test negative indices
913 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
914 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
915 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
916 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
917 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
918 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
919 self.checkequal(False, 'hello', 'startswith', 'o', -2)
920 self.checkequal(True, 'hello', 'startswith', 'o', -1)
921 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
922 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
923
924 self.checkraises(TypeError, 'hello', 'startswith')
925 self.checkraises(TypeError, 'hello', 'startswith', 42)
926
Georg Brandl24250812006-06-09 18:45:48 +0000927 # test tuple arguments
928 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
929 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
930 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
931 self.checkequal(False, 'hello', 'startswith', ())
932 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
933 'rld', 'lowo'), 3)
934 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
935 'rld'), 3)
936 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
937 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
938 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
939
940 self.checkraises(TypeError, 'hello', 'startswith', (42,))
941
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000942 def test_endswith(self):
943 self.checkequal(True, 'hello', 'endswith', 'lo')
944 self.checkequal(False, 'hello', 'endswith', 'he')
945 self.checkequal(True, 'hello', 'endswith', '')
946 self.checkequal(False, 'hello', 'endswith', 'hello world')
947 self.checkequal(False, 'helloworld', 'endswith', 'worl')
948 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
949 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
950 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
951 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
952 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
953 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
954 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
955 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
956 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
957
958 # test negative indices
959 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
960 self.checkequal(False, 'hello', 'endswith', 'he', -2)
961 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
962 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
963 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
964 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
965 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
966 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
967 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
968 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
969 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
970 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
971 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
972
973 self.checkraises(TypeError, 'hello', 'endswith')
974 self.checkraises(TypeError, 'hello', 'endswith', 42)
975
Georg Brandl24250812006-06-09 18:45:48 +0000976 # test tuple arguments
977 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
978 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
979 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
980 self.checkequal(False, 'hello', 'endswith', ())
981 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
982 'rld', 'lowo'), 3)
983 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
984 'rld'), 3, -1)
985 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
986 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
987 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
988
989 self.checkraises(TypeError, 'hello', 'endswith', (42,))
990
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000991 def test___contains__(self):
Ezio Melotti469a05f2010-01-24 20:48:35 +0000992 self.checkequal(True, '', '__contains__', '')
993 self.checkequal(True, 'abc', '__contains__', '')
994 self.checkequal(False, 'abc', '__contains__', '\0')
995 self.checkequal(True, '\0abc', '__contains__', '\0')
996 self.checkequal(True, 'abc\0', '__contains__', '\0')
997 self.checkequal(True, '\0abc', '__contains__', 'a')
998 self.checkequal(True, 'asdf', '__contains__', 'asdf')
999 self.checkequal(False, 'asd', '__contains__', 'asdf')
1000 self.checkequal(False, '', '__contains__', 'asdf')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001001
1002 def test_subscript(self):
1003 self.checkequal(u'a', 'abc', '__getitem__', 0)
1004 self.checkequal(u'c', 'abc', '__getitem__', -1)
1005 self.checkequal(u'a', 'abc', '__getitem__', 0L)
1006 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3))
1007 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
1008 self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
1009 self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001010
1011 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1012
1013 def test_slice(self):
1014 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)
1015 self.checkequal('abc', 'abc', '__getslice__', 0, 3)
1016 self.checkequal('ab', 'abc', '__getslice__', 0, 2)
1017 self.checkequal('bc', 'abc', '__getslice__', 1, 3)
1018 self.checkequal('b', 'abc', '__getslice__', 1, 2)
1019 self.checkequal('', 'abc', '__getslice__', 2, 2)
1020 self.checkequal('', 'abc', '__getslice__', 1000, 1000)
1021 self.checkequal('', 'abc', '__getslice__', 2000, 1000)
1022 self.checkequal('', 'abc', '__getslice__', 2, 1)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001023
1024 self.checkraises(TypeError, 'abc', '__getslice__', 'def')
1025
Thomas Wouters3ccec682007-08-28 15:28:19 +00001026 def test_extended_getslice(self):
1027 # Test extended slicing by comparing with list slicing.
1028 s = string.ascii_letters + string.digits
1029 indices = (0, None, 1, 3, 41, -1, -2, -37)
1030 for start in indices:
1031 for stop in indices:
1032 # Skip step 0 (invalid)
1033 for step in indices[1:]:
1034 L = list(s)[start:stop:step]
1035 self.checkequal(u"".join(L), s, '__getitem__',
1036 slice(start, stop, step))
1037
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001038 def test_mul(self):
1039 self.checkequal('', 'abc', '__mul__', -1)
1040 self.checkequal('', 'abc', '__mul__', 0)
1041 self.checkequal('abc', 'abc', '__mul__', 1)
1042 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
1043 self.checkraises(TypeError, 'abc', '__mul__')
1044 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +00001045 # XXX: on a 64-bit system, this doesn't raise an overflow error,
1046 # but either raises a MemoryError, or succeeds (if you have 54TiB)
1047 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001048
1049 def test_join(self):
1050 # join now works with any sequence type
1051 # moved here, because the argument order is
1052 # different in string.join (see the test in
1053 # test.test_string.StringTest.test_join)
1054 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
1055 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Georg Brandl90e27d32006-06-10 06:40:50 +00001056 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
1057 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001058 self.checkequal('w x y z', ' ', 'join', Sequence())
1059 self.checkequal('abc', 'a', 'join', ('abc',))
1060 self.checkequal('z', 'a', 'join', UserList(['z']))
1061 if test_support.have_unicode:
1062 self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])
1063 self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])
1064 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])
1065 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])
1066 self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])
1067 for i in [5, 25, 125]:
1068 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1069 ['a' * i] * i)
1070 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1071 ('a' * i,) * i)
1072
1073 self.checkraises(TypeError, ' ', 'join', BadSeq1())
1074 self.checkequal('a b c', ' ', 'join', BadSeq2())
1075
1076 self.checkraises(TypeError, ' ', 'join')
1077 self.checkraises(TypeError, ' ', 'join', 7)
1078 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001079 try:
1080 def f():
1081 yield 4 + ""
1082 self.fixtype(' ').join(f())
1083 except TypeError, e:
1084 if '+' not in str(e):
1085 self.fail('join() ate exception message')
1086 else:
1087 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001088
1089 def test_formatting(self):
1090 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1091 self.checkequal('+10+', '+%d+', '__mod__', 10)
1092 self.checkequal('a', "%c", '__mod__', "a")
1093 self.checkequal('a', "%c", '__mod__', "a")
1094 self.checkequal('"', "%c", '__mod__', 34)
1095 self.checkequal('$', "%c", '__mod__', 36)
1096 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001097 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001098
1099 for ordinal in (-100, 0x200000):
1100 # unicode raises ValueError, str raises OverflowError
1101 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1102
Facundo Batistac11cecf2008-02-24 03:17:21 +00001103 longvalue = sys.maxint + 10L
1104 slongvalue = str(longvalue)
1105 if slongvalue[-1] in ("L","l"): slongvalue = slongvalue[:-1]
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001106 self.checkequal(' 42', '%3ld', '__mod__', 42)
Facundo Batistac11cecf2008-02-24 03:17:21 +00001107 self.checkequal('42', '%d', '__mod__', 42L)
1108 self.checkequal('42', '%d', '__mod__', 42.0)
1109 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1110 self.checkcall('%d', '__mod__', float(longvalue))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001111 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001112 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001113
1114 self.checkraises(TypeError, 'abc', '__mod__')
1115 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1116 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1117 self.checkraises(TypeError, '%c', '__mod__', (None,))
1118 self.checkraises(ValueError, '%(foo', '__mod__', {})
1119 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
Facundo Batistac11cecf2008-02-24 03:17:21 +00001120 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
1121 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int/long conversion provided
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001122
1123 # argument names with properly nested brackets are supported
1124 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1125
1126 # 100 is a magic number in PyUnicode_Format, this forces a resize
1127 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1128
1129 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1130 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1131 self.checkraises(ValueError, '%10', '__mod__', (42,))
1132
1133 def test_floatformatting(self):
1134 # float formatting
1135 for prec in xrange(100):
1136 format = '%%.%if' % prec
1137 value = 0.01
1138 for x in xrange(60):
Florent Xicluna9b90cd12010-09-13 07:46:37 +00001139 value = value * 3.14159265359 / 3.0 * 10.0
Mark Dickinson18cfada2009-11-23 18:46:41 +00001140 self.checkcall(format, "__mod__", value)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001141
Andrew Dalke2bddcbf2006-05-25 16:30:52 +00001142 def test_inplace_rewrites(self):
1143 # Check that strings don't copy and modify cached single-character strings
1144 self.checkequal('a', 'A', 'lower')
1145 self.checkequal(True, 'A', 'isupper')
1146 self.checkequal('A', 'a', 'upper')
1147 self.checkequal(True, 'a', 'islower')
Tim Petersd95d5932006-05-25 21:52:19 +00001148
Andrew Dalke2bddcbf2006-05-25 16:30:52 +00001149 self.checkequal('a', 'A', 'replace', 'A', 'a')
1150 self.checkequal(True, 'A', 'isupper')
1151
1152 self.checkequal('A', 'a', 'capitalize')
1153 self.checkequal(True, 'a', 'islower')
Tim Petersd95d5932006-05-25 21:52:19 +00001154
Andrew Dalke2bddcbf2006-05-25 16:30:52 +00001155 self.checkequal('A', 'a', 'swapcase')
1156 self.checkequal(True, 'a', 'islower')
1157
1158 self.checkequal('A', 'a', 'title')
1159 self.checkequal(True, 'a', 'islower')
1160
Fredrik Lundh06a69dd2006-05-26 08:54:28 +00001161 def test_partition(self):
1162
Fredrik Lundh9c0e9c02006-05-26 18:24:15 +00001163 self.checkequal(('this is the par', 'ti', 'tion method'),
1164 'this is the partition method', 'partition', 'ti')
Fredrik Lundh06a69dd2006-05-26 08:54:28 +00001165
1166 # from raymond's original specification
1167 S = 'http://www.python.org'
1168 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1169 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1170 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1171 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1172
1173 self.checkraises(ValueError, S, 'partition', '')
1174 self.checkraises(TypeError, S, 'partition', None)
1175
Amaury Forgeot d'Arc3571fbf2008-09-01 19:52:00 +00001176 # mixed use of str and unicode
1177 self.assertEqual('a/b/c'.partition(u'/'), ('a', '/', 'b/c'))
1178
Fredrik Lundh9c0e9c02006-05-26 18:24:15 +00001179 def test_rpartition(self):
1180
1181 self.checkequal(('this is the rparti', 'ti', 'on method'),
1182 'this is the rpartition method', 'rpartition', 'ti')
1183
1184 # from raymond's original specification
1185 S = 'http://www.python.org'
1186 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Raymond Hettingera0c95fa2006-09-04 15:32:48 +00001187 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Fredrik Lundh9c0e9c02006-05-26 18:24:15 +00001188 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1189 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1190
1191 self.checkraises(ValueError, S, 'rpartition', '')
1192 self.checkraises(TypeError, S, 'rpartition', None)
1193
Amaury Forgeot d'Arc3571fbf2008-09-01 19:52:00 +00001194 # mixed use of str and unicode
1195 self.assertEqual('a/b/c'.rpartition(u'/'), ('a/b', '/', 'c'))
Walter Dörwald57d88e52004-08-26 16:53:04 +00001196
Jesus Cea44e81682011-04-20 16:39:15 +02001197 def test_none_arguments(self):
1198 # issue 11828
1199 s = 'hello'
1200 self.checkequal(2, s, 'find', 'l', None)
1201 self.checkequal(3, s, 'find', 'l', -2, None)
1202 self.checkequal(2, s, 'find', 'l', None, -2)
1203 self.checkequal(0, s, 'find', 'h', None, None)
1204
1205 self.checkequal(3, s, 'rfind', 'l', None)
1206 self.checkequal(3, s, 'rfind', 'l', -2, None)
1207 self.checkequal(2, s, 'rfind', 'l', None, -2)
1208 self.checkequal(0, s, 'rfind', 'h', None, None)
1209
1210 self.checkequal(2, s, 'index', 'l', None)
1211 self.checkequal(3, s, 'index', 'l', -2, None)
1212 self.checkequal(2, s, 'index', 'l', None, -2)
1213 self.checkequal(0, s, 'index', 'h', None, None)
1214
1215 self.checkequal(3, s, 'rindex', 'l', None)
1216 self.checkequal(3, s, 'rindex', 'l', -2, None)
1217 self.checkequal(2, s, 'rindex', 'l', None, -2)
1218 self.checkequal(0, s, 'rindex', 'h', None, None)
1219
1220 self.checkequal(2, s, 'count', 'l', None)
1221 self.checkequal(1, s, 'count', 'l', -2, None)
1222 self.checkequal(1, s, 'count', 'l', None, -2)
1223 self.checkequal(0, s, 'count', 'x', None, None)
1224
1225 self.checkequal(True, s, 'endswith', 'o', None)
1226 self.checkequal(True, s, 'endswith', 'lo', -2, None)
1227 self.checkequal(True, s, 'endswith', 'l', None, -2)
1228 self.checkequal(False, s, 'endswith', 'x', None, None)
1229
1230 self.checkequal(True, s, 'startswith', 'h', None)
1231 self.checkequal(True, s, 'startswith', 'l', -2, None)
1232 self.checkequal(True, s, 'startswith', 'h', None, -2)
1233 self.checkequal(False, s, 'startswith', 'x', None, None)
1234
1235 def test_find_etc_raise_correct_error_messages(self):
1236 # issue 11828
1237 s = 'hello'
1238 x = 'x'
1239 self.assertRaisesRegexp(TypeError, r'\bfind\b', s.find,
1240 x, None, None, None)
1241 self.assertRaisesRegexp(TypeError, r'\brfind\b', s.rfind,
1242 x, None, None, None)
1243 self.assertRaisesRegexp(TypeError, r'\bindex\b', s.index,
1244 x, None, None, None)
1245 self.assertRaisesRegexp(TypeError, r'\brindex\b', s.rindex,
1246 x, None, None, None)
1247 self.assertRaisesRegexp(TypeError, r'^count\(', s.count,
1248 x, None, None, None)
1249 self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith,
1250 x, None, None, None)
1251 self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith,
1252 x, None, None, None)
1253
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001254class MixinStrStringUserStringTest:
1255 # Additional tests for 8bit strings, i.e. str, UserString and
1256 # the string module
1257
1258 def test_maketrans(self):
1259 self.assertEqual(
1260 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),
1261 string.maketrans('abc', 'xyz')
1262 )
1263 self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw')
1264
1265 def test_translate(self):
1266 table = string.maketrans('abc', 'xyz')
1267 self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def')
1268
1269 table = string.maketrans('a', 'A')
1270 self.checkequal('Abc', 'abc', 'translate', table)
1271 self.checkequal('xyz', 'xyz', 'translate', table)
1272 self.checkequal('yz', 'xyz', 'translate', table, 'x')
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001273 self.checkequal('yx', 'zyzzx', 'translate', None, 'z')
Raymond Hettinger4db5fe92007-04-12 04:10:00 +00001274 self.checkequal('zyzzx', 'zyzzx', 'translate', None, '')
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001275 self.checkequal('zyzzx', 'zyzzx', 'translate', None)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001276 self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')
1277 self.checkraises(ValueError, 'xyz', 'translate', 'too short')
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00001278
1279
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001280class MixinStrUserStringTest:
1281 # Additional tests that only work with
1282 # 8bit compatible object, i.e. str and UserString
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00001283
Walter Dörwald6eea7892005-07-28 16:49:15 +00001284 if test_support.have_unicode:
1285 def test_encoding_decoding(self):
1286 codecs = [('rot13', 'uryyb jbeyq'),
1287 ('base64', 'aGVsbG8gd29ybGQ=\n'),
1288 ('hex', '68656c6c6f20776f726c64'),
1289 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
1290 for encoding, data in codecs:
1291 self.checkequal(data, 'hello world', 'encode', encoding)
1292 self.checkequal('hello world', data, 'decode', encoding)
1293 # zlib is optional, so we make the test optional too...
1294 try:
1295 import zlib
1296 except ImportError:
1297 pass
1298 else:
1299 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
1300 self.checkequal(data, 'hello world', 'encode', 'zlib')
1301 self.checkequal('hello world', data, 'decode', 'zlib')
Walter Dörwald97951de2003-03-26 14:31:25 +00001302
Walter Dörwald6eea7892005-07-28 16:49:15 +00001303 self.checkraises(TypeError, 'xyz', 'decode', 42)
1304 self.checkraises(TypeError, 'xyz', 'encode', 42)
Walter Dörwald57d88e52004-08-26 16:53:04 +00001305
1306
1307class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001308 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001309
1310 def test_bug1001011(self):
1311 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001312 # involving a subclass.
1313 # Make sure that it is of the appropriate type.
1314 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001315 t = self.type2test
1316 class subclass(t):
1317 pass
1318 s1 = subclass("abcd")
1319 s2 = t().join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001320 self.assertTrue(s1 is not s2)
1321 self.assertTrue(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001322
1323 s1 = t("abcd")
1324 s2 = t().join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001325 self.assertTrue(s1 is s2)
Tim Peters108f1372004-08-27 05:36:07 +00001326
1327 # Should also test mixed-type join.
1328 if t is unicode:
1329 s1 = subclass("abcd")
1330 s2 = "".join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001331 self.assertTrue(s1 is not s2)
1332 self.assertTrue(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001333
1334 s1 = t("abcd")
1335 s2 = "".join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001336 self.assertTrue(s1 is s2)
Tim Peters108f1372004-08-27 05:36:07 +00001337
1338 elif t is str:
1339 s1 = subclass("abcd")
1340 s2 = u"".join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001341 self.assertTrue(s1 is not s2)
1342 self.assertTrue(type(s2) is unicode) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001343
1344 s1 = t("abcd")
1345 s2 = u"".join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001346 self.assertTrue(s1 is not s2)
1347 self.assertTrue(type(s2) is unicode) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001348
1349 else:
1350 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)