blob: 34651cbee60df4b96a8bcf24d61b6c04aa99f406 [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
Martin Panter86e0d572016-04-06 06:37:17 +000048 def test_fixtype(self):
49 self.assertIs(type(self.fixtype("123")), self.type2test)
50
Walter Dörwald0fd583c2003-02-21 12:53:50 +000051 # check that object.method(*args) returns result
52 def checkequal(self, result, object, methodname, *args):
53 result = self.fixtype(result)
54 object = self.fixtype(object)
55 args = self.fixtype(args)
56 realresult = getattr(object, methodname)(*args)
57 self.assertEqual(
58 result,
59 realresult
60 )
61 # if the original is returned make sure that
62 # this doesn't happen with subclasses
63 if object == realresult:
64 class subtype(self.__class__.type2test):
65 pass
66 object = subtype(object)
67 realresult = getattr(object, methodname)(*args)
Ezio Melotti2623a372010-11-21 13:34:58 +000068 self.assertTrue(object is not realresult)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000069
Walter Dörwald0fd583c2003-02-21 12:53:50 +000070 # check that object.method(*args) raises exc
Benjamin Peterson1643d5c2014-09-28 12:48:46 -040071 def checkraises(self, exc, obj, methodname, *args):
72 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000073 args = self.fixtype(args)
Benjamin Peterson1643d5c2014-09-28 12:48:46 -040074 with self.assertRaises(exc) as cm:
75 getattr(obj, methodname)(*args)
Terry Jan Reedyc0dc65e2014-10-12 22:00:10 -040076 self.assertNotEqual(cm.exception.args[0], '')
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000077
Walter Dörwald0fd583c2003-02-21 12:53:50 +000078 # call object.method(*args) without any checks
79 def checkcall(self, object, methodname, *args):
80 object = self.fixtype(object)
81 args = self.fixtype(args)
82 getattr(object, methodname)(*args)
83
Raymond Hettinger561fbf12004-10-26 01:52:37 +000084 def test_hash(self):
85 # SF bug 1054139: += optimization was not invalidating cached hash value
86 a = self.type2test('DNSSEC')
87 b = self.type2test('')
88 for c in a:
89 b += c
90 hash(b)
91 self.assertEqual(hash(a), hash(b))
92
Walter Dörwald0fd583c2003-02-21 12:53:50 +000093 def test_capitalize(self):
94 self.checkequal(' hello ', ' hello ', 'capitalize')
95 self.checkequal('Hello ', 'Hello ','capitalize')
96 self.checkequal('Hello ', 'hello ','capitalize')
97 self.checkequal('Aaaa', 'aaaa', 'capitalize')
98 self.checkequal('Aaaa', 'AaAa', 'capitalize')
99
100 self.checkraises(TypeError, 'hello', 'capitalize', 42)
101
102 def test_count(self):
103 self.checkequal(3, 'aaa', 'count', 'a')
104 self.checkequal(0, 'aaa', 'count', 'b')
105 self.checkequal(3, 'aaa', 'count', 'a')
106 self.checkequal(0, 'aaa', 'count', 'b')
107 self.checkequal(3, 'aaa', 'count', 'a')
108 self.checkequal(0, 'aaa', 'count', 'b')
109 self.checkequal(0, 'aaa', 'count', 'b')
Fredrik Lundhb51b4702006-05-29 22:42:07 +0000110 self.checkequal(2, 'aaa', 'count', 'a', 1)
111 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000112 self.checkequal(1, 'aaa', 'count', 'a', -1)
113 self.checkequal(3, 'aaa', 'count', 'a', -10)
Fredrik Lundhb51b4702006-05-29 22:42:07 +0000114 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
115 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000116 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
117 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Fredrik Lundhb51b4702006-05-29 22:42:07 +0000118 self.checkequal(3, 'aaa', 'count', '', 1)
Fredrik Lundh9e9ef9f2006-05-30 17:39:58 +0000119 self.checkequal(1, 'aaa', 'count', '', 3)
120 self.checkequal(0, 'aaa', 'count', '', 10)
Fredrik Lundhb51b4702006-05-29 22:42:07 +0000121 self.checkequal(2, 'aaa', 'count', '', -1)
122 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000123
Amaury Forgeot d'Arcfc5ea392008-09-26 22:34:08 +0000124 self.checkequal(1, '', 'count', '')
125 self.checkequal(0, '', 'count', '', 1, 1)
126 self.checkequal(0, '', 'count', '', sys.maxint, 0)
127
128 self.checkequal(0, '', 'count', 'xx')
129 self.checkequal(0, '', 'count', 'xx', 1, 1)
130 self.checkequal(0, '', 'count', 'xx', sys.maxint, 0)
131
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000132 self.checkraises(TypeError, 'hello', 'count')
133 self.checkraises(TypeError, 'hello', 'count', 42)
134
Raymond Hettinger57e74472005-02-20 09:54:53 +0000135 # For a variety of combinations,
136 # verify that str.count() matches an equivalent function
137 # replacing all occurrences and then differencing the string lengths
138 charset = ['', 'a', 'b']
139 digits = 7
140 base = len(charset)
141 teststrings = set()
142 for i in xrange(base ** digits):
143 entry = []
144 for j in xrange(digits):
145 i, m = divmod(i, base)
146 entry.append(charset[m])
147 teststrings.add(''.join(entry))
148 teststrings = list(teststrings)
149 for i in teststrings:
150 i = self.fixtype(i)
151 n = len(i)
152 for j in teststrings:
153 r1 = i.count(j)
154 if j:
155 r2, rem = divmod(n - len(i.replace(j, '')), len(j))
156 else:
157 r2, rem = len(i)+1, 0
158 if rem or r1 != r2:
Neal Norwitzf71ec5a2006-07-30 06:57:04 +0000159 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
160 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000161
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000162 def test_find(self):
163 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
164 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
165 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
166
Fredrik Lundh93eff6f2006-05-30 17:11:48 +0000167 self.checkequal(0, 'abc', 'find', '', 0)
168 self.checkequal(3, 'abc', 'find', '', 3)
169 self.checkequal(-1, 'abc', 'find', '', 4)
170
Facundo Batista57d56692007-11-16 18:04:14 +0000171 # to check the ability to pass None as defaults
172 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
173 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
174 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
175 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
176 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
177
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000178 self.checkraises(TypeError, 'hello', 'find')
179 self.checkraises(TypeError, 'hello', 'find', 42)
180
Amaury Forgeot d'Arcfc5ea392008-09-26 22:34:08 +0000181 self.checkequal(0, '', 'find', '')
182 self.checkequal(-1, '', 'find', '', 1, 1)
183 self.checkequal(-1, '', 'find', '', sys.maxint, 0)
184
185 self.checkequal(-1, '', 'find', 'xx')
186 self.checkequal(-1, '', 'find', 'xx', 1, 1)
187 self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0)
188
Antoine Pitrou83f86e82010-01-02 21:47:10 +0000189 # issue 7458
190 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)
191
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000192 # For a variety of combinations,
193 # verify that str.find() matches __contains__
194 # and that the found substring is really at that location
195 charset = ['', 'a', 'b', 'c']
196 digits = 5
197 base = len(charset)
198 teststrings = set()
199 for i in xrange(base ** digits):
200 entry = []
201 for j in xrange(digits):
202 i, m = divmod(i, base)
203 entry.append(charset[m])
204 teststrings.add(''.join(entry))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000205 teststrings = list(teststrings)
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000206 for i in teststrings:
207 i = self.fixtype(i)
208 for j in teststrings:
209 loc = i.find(j)
210 r1 = (loc != -1)
211 r2 = j in i
Antoine Pitroub538d542010-01-02 21:53:44 +0000212 self.assertEqual(r1, r2)
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000213 if loc != -1:
214 self.assertEqual(i[loc:loc+len(j)], j)
215
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000216 def test_rfind(self):
217 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
218 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
219 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
220 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
221
Fredrik Lundh93eff6f2006-05-30 17:11:48 +0000222 self.checkequal(3, 'abc', 'rfind', '', 0)
223 self.checkequal(3, 'abc', 'rfind', '', 3)
224 self.checkequal(-1, 'abc', 'rfind', '', 4)
225
Facundo Batista57d56692007-11-16 18:04:14 +0000226 # to check the ability to pass None as defaults
227 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
228 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
229 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
230 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
231 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
232
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000233 self.checkraises(TypeError, 'hello', 'rfind')
234 self.checkraises(TypeError, 'hello', 'rfind', 42)
235
Antoine Pitrou5b7139a2010-01-02 21:12:58 +0000236 # For a variety of combinations,
237 # verify that str.rfind() matches __contains__
238 # and that the found substring is really at that location
239 charset = ['', 'a', 'b', 'c']
240 digits = 5
241 base = len(charset)
242 teststrings = set()
243 for i in xrange(base ** digits):
244 entry = []
245 for j in xrange(digits):
246 i, m = divmod(i, base)
247 entry.append(charset[m])
248 teststrings.add(''.join(entry))
249 teststrings = list(teststrings)
250 for i in teststrings:
251 i = self.fixtype(i)
252 for j in teststrings:
253 loc = i.rfind(j)
254 r1 = (loc != -1)
255 r2 = j in i
Antoine Pitroub538d542010-01-02 21:53:44 +0000256 self.assertEqual(r1, r2)
Antoine Pitrou5b7139a2010-01-02 21:12:58 +0000257 if loc != -1:
Florent Xiclunac0c0b142010-09-13 08:53:00 +0000258 self.assertEqual(i[loc:loc+len(j)], self.fixtype(j))
Antoine Pitrou5b7139a2010-01-02 21:12:58 +0000259
Antoine Pitrou83f86e82010-01-02 21:47:10 +0000260 # issue 7458
261 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)
262
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000263 def test_index(self):
264 self.checkequal(0, 'abcdefghiabc', 'index', '')
265 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
266 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
267 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
268
269 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
270 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
271 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
272 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
273
Facundo Batista57d56692007-11-16 18:04:14 +0000274 # to check the ability to pass None as defaults
275 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
276 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
277 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
278 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
279 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
280
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000281 self.checkraises(TypeError, 'hello', 'index')
282 self.checkraises(TypeError, 'hello', 'index', 42)
283
284 def test_rindex(self):
285 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
286 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
287 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
288 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
289
290 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
291 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
292 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
293 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
294 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
295
Facundo Batista57d56692007-11-16 18:04:14 +0000296 # to check the ability to pass None as defaults
297 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
298 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
299 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
300 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
301 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
302
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000303 self.checkraises(TypeError, 'hello', 'rindex')
304 self.checkraises(TypeError, 'hello', 'rindex', 42)
305
306 def test_lower(self):
307 self.checkequal('hello', 'HeLLo', 'lower')
308 self.checkequal('hello', 'hello', 'lower')
309 self.checkraises(TypeError, 'hello', 'lower', 42)
310
311 def test_upper(self):
312 self.checkequal('HELLO', 'HeLLo', 'upper')
313 self.checkequal('HELLO', 'HELLO', 'upper')
314 self.checkraises(TypeError, 'hello', 'upper', 42)
315
316 def test_expandtabs(self):
317 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
318 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
319 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
320 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
321 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
322 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
323 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 +0000324 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000325
326 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
Neal Norwitz5c9a81a2007-06-11 02:16:10 +0000327 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
328 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
329 self.checkraises(OverflowError,
330 '\ta\n\tb', 'expandtabs', sys.maxint)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000331
332 def test_split(self):
333 self.checkequal(['this', 'is', 'the', 'split', 'function'],
334 'this is the split function', 'split')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000335
336 # by whitespace
337 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000338 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
339 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
340 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
341 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
Andrew Dalke005aee22006-05-26 12:28:15 +0000342 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
343 sys.maxint-1)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000344 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
Andrew Dalke725fe402006-05-26 16:22:52 +0000345 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000346 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000347
Andrew Dalke984b9712006-05-26 11:11:38 +0000348 self.checkequal([], ' ', 'split')
349 self.checkequal(['a'], ' a ', 'split')
350 self.checkequal(['a', 'b'], ' a b ', 'split')
351 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
352 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
353 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
Andrew Dalke03fb4442006-05-26 11:15:22 +0000354 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
Andrew Dalke005aee22006-05-26 12:28:15 +0000355 aaa = ' a '*20
356 self.checkequal(['a']*20, aaa, 'split')
357 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
Andrew Dalke669fa182006-05-26 13:05:55 +0000358 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
Andrew Dalke984b9712006-05-26 11:11:38 +0000359
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000360 # by a char
361 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Andrew Dalke005aee22006-05-26 12:28:15 +0000362 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000363 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
364 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
365 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
366 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Andrew Dalke005aee22006-05-26 12:28:15 +0000367 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
368 sys.maxint-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000369 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
370 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
371 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Andrew Dalke005aee22006-05-26 12:28:15 +0000372 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
373 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000374 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
375
Andrew Dalke005aee22006-05-26 12:28:15 +0000376 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
377 self.checkequal(['a']*15 +['a|a|a|a|a'],
378 ('a|'*20)[:-1], 'split', '|', 15)
379
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000380 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000381 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000382 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
383 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
384 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
385 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Andrew Dalke005aee22006-05-26 12:28:15 +0000386 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
387 sys.maxint-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000388 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
389 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000390 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Andrew Dalke669fa182006-05-26 13:05:55 +0000391 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
392 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
393 'split', 'test')
394 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
Andrew Dalke005aee22006-05-26 12:28:15 +0000395 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
396 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
397 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
398 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
399 self.checkequal([''], '', 'split', 'aaa')
400 self.checkequal(['aa'], 'aa', 'split', 'aaa')
Andrew Dalke5cc60092006-05-26 12:31:00 +0000401 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
402 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
Andrew Dalke005aee22006-05-26 12:28:15 +0000403
404 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
405 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
406 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
407 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000408
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000409 # mixed use of str and unicode
Martin Panter86e0d572016-04-06 06:37:17 +0000410 if self.type2test is not bytearray:
411 result = [u'a', u'b', u'c d']
412 self.checkequal(result, 'a b c d', 'split', u' ', 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000413
414 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000415 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
416
Andrew Dalke005aee22006-05-26 12:28:15 +0000417 # null case
418 self.checkraises(ValueError, 'hello', 'split', '')
419 self.checkraises(ValueError, 'hello', 'split', '', 0)
420
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000421 def test_rsplit(self):
422 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
423 'this is the rsplit function', 'rsplit')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000424
425 # by whitespace
426 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000427 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
428 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
429 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
430 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
Andrew Dalke669fa182006-05-26 13:05:55 +0000431 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
432 sys.maxint-20)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000433 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
Andrew Dalke725fe402006-05-26 16:22:52 +0000434 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000435 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000436
Andrew Dalke669fa182006-05-26 13:05:55 +0000437 self.checkequal([], ' ', 'rsplit')
438 self.checkequal(['a'], ' a ', 'rsplit')
439 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
440 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
441 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
442 None, 1)
443 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
444 None, 2)
445 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
446 aaa = ' a '*20
447 self.checkequal(['a']*20, aaa, 'rsplit')
448 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
449 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
450
451
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000452 # by a char
453 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
454 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
455 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
456 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
457 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Andrew Dalke669fa182006-05-26 13:05:55 +0000458 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
459 sys.maxint-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000460 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
461 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
462 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Andrew Dalke669fa182006-05-26 13:05:55 +0000463 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
464 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
465
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000466 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
467
Andrew Dalke669fa182006-05-26 13:05:55 +0000468 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
469 self.checkequal(['a|a|a|a|a']+['a']*15,
470 ('a|'*20)[:-1], 'rsplit', '|', 15)
471
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000472 # by string
473 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
474 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
475 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
476 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
477 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Andrew Dalke669fa182006-05-26 13:05:55 +0000478 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
479 sys.maxint-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000480 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
481 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
482 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Andrew Dalke669fa182006-05-26 13:05:55 +0000483 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
484 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
485 'rsplit', 'test')
486 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
487 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
488 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
489 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
490 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
491 self.checkequal([''], '', 'rsplit', 'aaa')
492 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
493 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
494 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
495
496 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
497 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
498 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
499 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000500
501 # mixed use of str and unicode
Martin Panter86e0d572016-04-06 06:37:17 +0000502 if self.type2test is not bytearray:
503 result = [u'a b', u'c', u'd']
504 self.checkequal(result, 'a b c d', 'rsplit', u' ', 2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000505
506 # argument type
507 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000508
Andrew Dalke669fa182006-05-26 13:05:55 +0000509 # null case
510 self.checkraises(ValueError, 'hello', 'rsplit', '')
511 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
512
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000513 def test_strip(self):
514 self.checkequal('hello', ' hello ', 'strip')
515 self.checkequal('hello ', ' hello ', 'lstrip')
516 self.checkequal(' hello', ' hello ', 'rstrip')
517 self.checkequal('hello', 'hello', 'strip')
518
Neal Norwitzffe33b72003-04-10 22:35:32 +0000519 # strip/lstrip/rstrip with None arg
520 self.checkequal('hello', ' hello ', 'strip', None)
521 self.checkequal('hello ', ' hello ', 'lstrip', None)
522 self.checkequal(' hello', ' hello ', 'rstrip', None)
523 self.checkequal('hello', 'hello', 'strip', None)
524
525 # strip/lstrip/rstrip with str arg
526 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
527 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
528 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
529 self.checkequal('hello', 'hello', 'strip', 'xyz')
530
531 # strip/lstrip/rstrip with unicode arg
Martin Panter86e0d572016-04-06 06:37:17 +0000532 if self.type2test is not bytearray and test_support.have_unicode:
Neal Norwitzffe33b72003-04-10 22:35:32 +0000533 self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
534 'strip', unicode('xyz', 'ascii'))
535 self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
536 'lstrip', unicode('xyz', 'ascii'))
537 self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
538 'rstrip', unicode('xyz', 'ascii'))
Christian Heimes1a6387e2008-03-26 12:49:49 +0000539 # XXX
540 #self.checkequal(unicode('hello', 'ascii'), 'hello',
541 # 'strip', unicode('xyz', 'ascii'))
Neal Norwitzffe33b72003-04-10 22:35:32 +0000542
543 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
544 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
545 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
546
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000547 def test_ljust(self):
548 self.checkequal('abc ', 'abc', 'ljust', 10)
549 self.checkequal('abc ', 'abc', 'ljust', 6)
550 self.checkequal('abc', 'abc', 'ljust', 3)
551 self.checkequal('abc', 'abc', 'ljust', 2)
Martin Panter86e0d572016-04-06 06:37:17 +0000552 if self.type2test is bytearray:
553 # Special case because bytearray argument is not accepted
554 self.assertEqual(b'abc*******', bytearray(b'abc').ljust(10, '*'))
555 else:
556 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000557 self.checkraises(TypeError, 'abc', 'ljust')
558
559 def test_rjust(self):
560 self.checkequal(' abc', 'abc', 'rjust', 10)
561 self.checkequal(' abc', 'abc', 'rjust', 6)
562 self.checkequal('abc', 'abc', 'rjust', 3)
563 self.checkequal('abc', 'abc', 'rjust', 2)
Martin Panter86e0d572016-04-06 06:37:17 +0000564 if self.type2test is bytearray:
565 # Special case because bytearray argument is not accepted
566 self.assertEqual(b'*******abc', bytearray(b'abc').rjust(10, '*'))
567 else:
568 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000569 self.checkraises(TypeError, 'abc', 'rjust')
570
571 def test_center(self):
572 self.checkequal(' abc ', 'abc', 'center', 10)
573 self.checkequal(' abc ', 'abc', 'center', 6)
574 self.checkequal('abc', 'abc', 'center', 3)
575 self.checkequal('abc', 'abc', 'center', 2)
Martin Panter86e0d572016-04-06 06:37:17 +0000576 if self.type2test is bytearray:
577 # Special case because bytearray argument is not accepted
578 result = bytearray(b'abc').center(10, '*')
579 self.assertEqual(b'***abc****', result)
580 else:
581 self.checkequal('***abc****', 'abc', 'center', 10, '*')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000582 self.checkraises(TypeError, 'abc', 'center')
583
584 def test_swapcase(self):
585 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
586
587 self.checkraises(TypeError, 'hello', 'swapcase', 42)
588
589 def test_replace(self):
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000590 EQ = self.checkequal
591
592 # Operations on the empty string
593 EQ("", "", "replace", "", "")
Tim Peters80a18f02006-06-01 13:56:26 +0000594 EQ("A", "", "replace", "", "A")
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000595 EQ("", "", "replace", "A", "")
596 EQ("", "", "replace", "A", "A")
597 EQ("", "", "replace", "", "", 100)
598 EQ("", "", "replace", "", "", sys.maxint)
599
600 # interleave (from=="", 'to' gets inserted everywhere)
601 EQ("A", "A", "replace", "", "")
602 EQ("*A*", "A", "replace", "", "*")
603 EQ("*1A*1", "A", "replace", "", "*1")
604 EQ("*-#A*-#", "A", "replace", "", "*-#")
605 EQ("*-A*-A*-", "AA", "replace", "", "*-")
606 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
607 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
608 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
609 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
610 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
611 EQ("*-AA", "AA", "replace", "", "*-", 1)
612 EQ("AA", "AA", "replace", "", "*-", 0)
613
614 # single character deletion (from=="A", to=="")
615 EQ("", "A", "replace", "A", "")
616 EQ("", "AAA", "replace", "A", "")
617 EQ("", "AAA", "replace", "A", "", -1)
618 EQ("", "AAA", "replace", "A", "", sys.maxint)
619 EQ("", "AAA", "replace", "A", "", 4)
620 EQ("", "AAA", "replace", "A", "", 3)
621 EQ("A", "AAA", "replace", "A", "", 2)
622 EQ("AA", "AAA", "replace", "A", "", 1)
623 EQ("AAA", "AAA", "replace", "A", "", 0)
624 EQ("", "AAAAAAAAAA", "replace", "A", "")
625 EQ("BCD", "ABACADA", "replace", "A", "")
626 EQ("BCD", "ABACADA", "replace", "A", "", -1)
627 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
628 EQ("BCD", "ABACADA", "replace", "A", "", 5)
629 EQ("BCD", "ABACADA", "replace", "A", "", 4)
630 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
631 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
632 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
633 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
634 EQ("BCD", "ABCAD", "replace", "A", "")
635 EQ("BCD", "ABCADAA", "replace", "A", "")
636 EQ("BCD", "BCD", "replace", "A", "")
637 EQ("*************", "*************", "replace", "A", "")
638 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
639
640 # substring deletion (from=="the", to=="")
641 EQ("", "the", "replace", "the", "")
642 EQ("ater", "theater", "replace", "the", "")
643 EQ("", "thethe", "replace", "the", "")
644 EQ("", "thethethethe", "replace", "the", "")
645 EQ("aaaa", "theatheatheathea", "replace", "the", "")
646 EQ("that", "that", "replace", "the", "")
647 EQ("thaet", "thaet", "replace", "the", "")
648 EQ("here and re", "here and there", "replace", "the", "")
649 EQ("here and re and re", "here and there and there",
650 "replace", "the", "", sys.maxint)
651 EQ("here and re and re", "here and there and there",
652 "replace", "the", "", -1)
653 EQ("here and re and re", "here and there and there",
654 "replace", "the", "", 3)
655 EQ("here and re and re", "here and there and there",
656 "replace", "the", "", 2)
657 EQ("here and re and there", "here and there and there",
658 "replace", "the", "", 1)
659 EQ("here and there and there", "here and there and there",
660 "replace", "the", "", 0)
661 EQ("here and re and re", "here and there and there", "replace", "the", "")
662
663 EQ("abc", "abc", "replace", "the", "")
664 EQ("abcdefg", "abcdefg", "replace", "the", "")
665
666 # substring deletion (from=="bob", to=="")
667 EQ("bob", "bbobob", "replace", "bob", "")
668 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
669 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
670 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
Tim Petersbeaec0c2006-05-24 20:27:18 +0000671
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000672 # single character replace in place (len(from)==len(to)==1)
673 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
674 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
675 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
676 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
677 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
678 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
679 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
680 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
681
682 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
683 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
684 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
685 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
686 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
687
688 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
Tim Petersbeaec0c2006-05-24 20:27:18 +0000689
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000690 # substring replace in place (len(from)==len(to) > 1)
691 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
692 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
693 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
694 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
695 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
696 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
697 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
698 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
699 EQ("cobob", "bobob", "replace", "bob", "cob")
700 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
701 EQ("bobob", "bobob", "replace", "bot", "bot")
702
703 # replace single character (len(from)==1, len(to)>1)
704 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
705 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
706 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
707 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
708 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
709 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
710 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
711
712 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
713
714 # replace substring (len(from)>1, len(to)!=len(from))
715 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
716 "replace", "spam", "ham")
717 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
718 "replace", "spam", "ham", sys.maxint)
719 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
720 "replace", "spam", "ham", -1)
721 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
722 "replace", "spam", "ham", 4)
723 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
724 "replace", "spam", "ham", 3)
725 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
726 "replace", "spam", "ham", 2)
727 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
728 "replace", "spam", "ham", 1)
729 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
730 "replace", "spam", "ham", 0)
731
732 EQ("bobob", "bobobob", "replace", "bobob", "bob")
733 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
734 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
Tim Petersbeaec0c2006-05-24 20:27:18 +0000735
Florent Xicluna6de9e932010-03-07 12:18:33 +0000736 with test_support.check_py3k_warnings():
Antoine Pitrou5b7139a2010-01-02 21:12:58 +0000737 ba = buffer('a')
738 bb = buffer('b')
Neal Norwitzf71ec5a2006-07-30 06:57:04 +0000739 EQ("bbc", "abc", "replace", ba, bb)
740 EQ("aac", "abc", "replace", bb, ba)
741
Tim Petersbeaec0c2006-05-24 20:27:18 +0000742 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000743 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
744 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
745 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
746 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
747 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
748 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
749 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
750 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
751 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
752 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
753 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
754 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
755 self.checkequal('', '', 'replace', '', '')
756 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
757 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
758 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
759 # MemoryError due to empty result (platform malloc issue when requesting
760 # 0 bytes).
761 self.checkequal('', '123', 'replace', '123', '')
762 self.checkequal('', '123123', 'replace', '123', '')
763 self.checkequal('x', '123x123', 'replace', '123', '')
764
765 self.checkraises(TypeError, 'hello', 'replace')
766 self.checkraises(TypeError, 'hello', 'replace', 42)
767 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
768 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
769
Zachary Ware1f702212013-12-10 14:09:20 -0600770 @unittest.skipIf(sys.maxint > (1 << 32) or struct.calcsize('P') != 4,
771 'only applies to 32-bit platforms')
Fredrik Lundh0c71f882006-05-25 16:46:54 +0000772 def test_replace_overflow(self):
773 # Check for overflow checking on 32 bit machines
Fredrik Lundh0c71f882006-05-25 16:46:54 +0000774 A2_16 = "A" * (2**16)
775 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
776 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
777 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
Andrew Dalkee5488ec2006-05-24 18:55:37 +0000778
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000779 def test_zfill(self):
780 self.checkequal('123', '123', 'zfill', 2)
781 self.checkequal('123', '123', 'zfill', 3)
782 self.checkequal('0123', '123', 'zfill', 4)
783 self.checkequal('+123', '+123', 'zfill', 3)
784 self.checkequal('+123', '+123', 'zfill', 4)
785 self.checkequal('+0123', '+123', 'zfill', 5)
786 self.checkequal('-123', '-123', 'zfill', 3)
787 self.checkequal('-123', '-123', 'zfill', 4)
788 self.checkequal('-0123', '-123', 'zfill', 5)
789 self.checkequal('000', '', 'zfill', 3)
790 self.checkequal('34', '34', 'zfill', 1)
791 self.checkequal('0034', '34', 'zfill', 4)
792
793 self.checkraises(TypeError, '123', 'zfill')
794
Christian Heimes1a6387e2008-03-26 12:49:49 +0000795
Martin Panter86e0d572016-04-06 06:37:17 +0000796class NonStringModuleTest:
797 # additional test cases for all string classes from bytearray to
798 # UserString, but not valid for the "string" module
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000799
800 def test_islower(self):
801 self.checkequal(False, '', 'islower')
802 self.checkequal(True, 'a', 'islower')
803 self.checkequal(False, 'A', 'islower')
804 self.checkequal(False, '\n', 'islower')
805 self.checkequal(True, 'abc', 'islower')
806 self.checkequal(False, 'aBc', 'islower')
807 self.checkequal(True, 'abc\n', 'islower')
808 self.checkraises(TypeError, 'abc', 'islower', 42)
809
810 def test_isupper(self):
811 self.checkequal(False, '', 'isupper')
812 self.checkequal(False, 'a', 'isupper')
813 self.checkequal(True, 'A', 'isupper')
814 self.checkequal(False, '\n', 'isupper')
815 self.checkequal(True, 'ABC', 'isupper')
816 self.checkequal(False, 'AbC', 'isupper')
817 self.checkequal(True, 'ABC\n', 'isupper')
818 self.checkraises(TypeError, 'abc', 'isupper', 42)
819
820 def test_istitle(self):
821 self.checkequal(False, '', 'istitle')
822 self.checkequal(False, 'a', 'istitle')
823 self.checkequal(True, 'A', 'istitle')
824 self.checkequal(False, '\n', 'istitle')
825 self.checkequal(True, 'A Titlecased Line', 'istitle')
826 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
827 self.checkequal(True, 'A Titlecased, Line', 'istitle')
828 self.checkequal(False, 'Not a capitalized String', 'istitle')
829 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
830 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
831 self.checkequal(False, 'NOT', 'istitle')
832 self.checkraises(TypeError, 'abc', 'istitle', 42)
833
834 def test_isspace(self):
835 self.checkequal(False, '', 'isspace')
836 self.checkequal(False, 'a', 'isspace')
837 self.checkequal(True, ' ', 'isspace')
838 self.checkequal(True, '\t', 'isspace')
839 self.checkequal(True, '\r', 'isspace')
840 self.checkequal(True, '\n', 'isspace')
841 self.checkequal(True, ' \t\r\n', 'isspace')
842 self.checkequal(False, ' \t\r\na', 'isspace')
843 self.checkraises(TypeError, 'abc', 'isspace', 42)
844
845 def test_isalpha(self):
846 self.checkequal(False, '', 'isalpha')
847 self.checkequal(True, 'a', 'isalpha')
848 self.checkequal(True, 'A', 'isalpha')
849 self.checkequal(False, '\n', 'isalpha')
850 self.checkequal(True, 'abc', 'isalpha')
851 self.checkequal(False, 'aBc123', 'isalpha')
852 self.checkequal(False, 'abc\n', 'isalpha')
853 self.checkraises(TypeError, 'abc', 'isalpha', 42)
854
855 def test_isalnum(self):
856 self.checkequal(False, '', 'isalnum')
857 self.checkequal(True, 'a', 'isalnum')
858 self.checkequal(True, 'A', 'isalnum')
859 self.checkequal(False, '\n', 'isalnum')
860 self.checkequal(True, '123abc456', 'isalnum')
861 self.checkequal(True, 'a1b3c', 'isalnum')
862 self.checkequal(False, 'aBc000 ', 'isalnum')
863 self.checkequal(False, 'abc\n', 'isalnum')
864 self.checkraises(TypeError, 'abc', 'isalnum', 42)
865
866 def test_isdigit(self):
867 self.checkequal(False, '', 'isdigit')
868 self.checkequal(False, 'a', 'isdigit')
869 self.checkequal(True, '0', 'isdigit')
870 self.checkequal(True, '0123456789', 'isdigit')
871 self.checkequal(False, '0123456789a', 'isdigit')
872
873 self.checkraises(TypeError, 'abc', 'isdigit', 42)
874
875 def test_title(self):
876 self.checkequal(' Hello ', ' hello ', 'title')
877 self.checkequal('Hello ', 'hello ', 'title')
878 self.checkequal('Hello ', 'Hello ', 'title')
879 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
880 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
881 self.checkequal('Getint', "getInt", 'title')
882 self.checkraises(TypeError, 'hello', 'title', 42)
883
884 def test_splitlines(self):
885 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
886 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
887 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
888 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
889 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
890 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
891 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
892
893 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
894
Martin Panter86e0d572016-04-06 06:37:17 +0000895
896class MixinStrUnicodeUserStringTest(NonStringModuleTest):
897 # additional tests that only work for
898 # stringlike objects, i.e. str, unicode, UserString
899 # (but not the string module)
900
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000901 def test_startswith(self):
902 self.checkequal(True, 'hello', 'startswith', 'he')
903 self.checkequal(True, 'hello', 'startswith', 'hello')
904 self.checkequal(False, 'hello', 'startswith', 'hello world')
905 self.checkequal(True, 'hello', 'startswith', '')
906 self.checkequal(False, 'hello', 'startswith', 'ello')
907 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
908 self.checkequal(True, 'hello', 'startswith', 'o', 4)
909 self.checkequal(False, 'hello', 'startswith', 'o', 5)
910 self.checkequal(True, 'hello', 'startswith', '', 5)
911 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
912 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
913 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
914 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
915
916 # test negative indices
917 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
918 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
919 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
920 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
921 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
922 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
923 self.checkequal(False, 'hello', 'startswith', 'o', -2)
924 self.checkequal(True, 'hello', 'startswith', 'o', -1)
925 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
926 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
927
928 self.checkraises(TypeError, 'hello', 'startswith')
929 self.checkraises(TypeError, 'hello', 'startswith', 42)
930
Georg Brandl24250812006-06-09 18:45:48 +0000931 # test tuple arguments
932 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
933 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
934 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
935 self.checkequal(False, 'hello', 'startswith', ())
936 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
937 'rld', 'lowo'), 3)
938 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
939 'rld'), 3)
940 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
941 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
942 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
943
944 self.checkraises(TypeError, 'hello', 'startswith', (42,))
945
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000946 def test_endswith(self):
947 self.checkequal(True, 'hello', 'endswith', 'lo')
948 self.checkequal(False, 'hello', 'endswith', 'he')
949 self.checkequal(True, 'hello', 'endswith', '')
950 self.checkequal(False, 'hello', 'endswith', 'hello world')
951 self.checkequal(False, 'helloworld', 'endswith', 'worl')
952 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
953 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
954 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
955 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
956 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
957 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
958 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
959 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
960 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
961
962 # test negative indices
963 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
964 self.checkequal(False, 'hello', 'endswith', 'he', -2)
965 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
966 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
967 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
968 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
969 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
970 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
971 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
972 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
973 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
974 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
975 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
976
977 self.checkraises(TypeError, 'hello', 'endswith')
978 self.checkraises(TypeError, 'hello', 'endswith', 42)
979
Georg Brandl24250812006-06-09 18:45:48 +0000980 # test tuple arguments
981 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
982 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
983 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
984 self.checkequal(False, 'hello', 'endswith', ())
985 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
986 'rld', 'lowo'), 3)
987 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
988 'rld'), 3, -1)
989 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
990 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
991 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
992
993 self.checkraises(TypeError, 'hello', 'endswith', (42,))
994
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000995 def test___contains__(self):
Ezio Melotti469a05f2010-01-24 20:48:35 +0000996 self.checkequal(True, '', '__contains__', '')
997 self.checkequal(True, 'abc', '__contains__', '')
998 self.checkequal(False, 'abc', '__contains__', '\0')
999 self.checkequal(True, '\0abc', '__contains__', '\0')
1000 self.checkequal(True, 'abc\0', '__contains__', '\0')
1001 self.checkequal(True, '\0abc', '__contains__', 'a')
1002 self.checkequal(True, 'asdf', '__contains__', 'asdf')
1003 self.checkequal(False, 'asd', '__contains__', 'asdf')
1004 self.checkequal(False, '', '__contains__', 'asdf')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001005
1006 def test_subscript(self):
1007 self.checkequal(u'a', 'abc', '__getitem__', 0)
1008 self.checkequal(u'c', 'abc', '__getitem__', -1)
1009 self.checkequal(u'a', 'abc', '__getitem__', 0L)
1010 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3))
1011 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
1012 self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
1013 self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001014
1015 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
1016
1017 def test_slice(self):
1018 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)
1019 self.checkequal('abc', 'abc', '__getslice__', 0, 3)
1020 self.checkequal('ab', 'abc', '__getslice__', 0, 2)
1021 self.checkequal('bc', 'abc', '__getslice__', 1, 3)
1022 self.checkequal('b', 'abc', '__getslice__', 1, 2)
1023 self.checkequal('', 'abc', '__getslice__', 2, 2)
1024 self.checkequal('', 'abc', '__getslice__', 1000, 1000)
1025 self.checkequal('', 'abc', '__getslice__', 2000, 1000)
1026 self.checkequal('', 'abc', '__getslice__', 2, 1)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001027
1028 self.checkraises(TypeError, 'abc', '__getslice__', 'def')
1029
Thomas Wouters3ccec682007-08-28 15:28:19 +00001030 def test_extended_getslice(self):
1031 # Test extended slicing by comparing with list slicing.
1032 s = string.ascii_letters + string.digits
1033 indices = (0, None, 1, 3, 41, -1, -2, -37)
1034 for start in indices:
1035 for stop in indices:
1036 # Skip step 0 (invalid)
1037 for step in indices[1:]:
1038 L = list(s)[start:stop:step]
1039 self.checkequal(u"".join(L), s, '__getitem__',
1040 slice(start, stop, step))
1041
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001042 def test_mul(self):
1043 self.checkequal('', 'abc', '__mul__', -1)
1044 self.checkequal('', 'abc', '__mul__', 0)
1045 self.checkequal('abc', 'abc', '__mul__', 1)
1046 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
1047 self.checkraises(TypeError, 'abc', '__mul__')
1048 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +00001049 # XXX: on a 64-bit system, this doesn't raise an overflow error,
1050 # but either raises a MemoryError, or succeeds (if you have 54TiB)
1051 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001052
1053 def test_join(self):
1054 # join now works with any sequence type
1055 # moved here, because the argument order is
1056 # different in string.join (see the test in
1057 # test.test_string.StringTest.test_join)
1058 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
1059 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Georg Brandl90e27d32006-06-10 06:40:50 +00001060 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
1061 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001062 self.checkequal('w x y z', ' ', 'join', Sequence())
1063 self.checkequal('abc', 'a', 'join', ('abc',))
1064 self.checkequal('z', 'a', 'join', UserList(['z']))
1065 if test_support.have_unicode:
1066 self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])
1067 self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])
1068 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])
1069 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])
1070 self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])
1071 for i in [5, 25, 125]:
1072 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1073 ['a' * i] * i)
1074 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1075 ('a' * i,) * i)
1076
1077 self.checkraises(TypeError, ' ', 'join', BadSeq1())
1078 self.checkequal('a b c', ' ', 'join', BadSeq2())
1079
1080 self.checkraises(TypeError, ' ', 'join')
Benjamin Peterson1643d5c2014-09-28 12:48:46 -04001081 self.checkraises(TypeError, ' ', 'join', None)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001082 self.checkraises(TypeError, ' ', 'join', 7)
1083 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001084 try:
1085 def f():
1086 yield 4 + ""
1087 self.fixtype(' ').join(f())
1088 except TypeError, e:
1089 if '+' not in str(e):
1090 self.fail('join() ate exception message')
1091 else:
1092 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001093
1094 def test_formatting(self):
1095 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1096 self.checkequal('+10+', '+%d+', '__mod__', 10)
1097 self.checkequal('a', "%c", '__mod__', "a")
1098 self.checkequal('a', "%c", '__mod__', "a")
1099 self.checkequal('"', "%c", '__mod__', 34)
1100 self.checkequal('$', "%c", '__mod__', 36)
1101 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001102 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001103
1104 for ordinal in (-100, 0x200000):
1105 # unicode raises ValueError, str raises OverflowError
1106 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1107
Facundo Batistac11cecf2008-02-24 03:17:21 +00001108 longvalue = sys.maxint + 10L
1109 slongvalue = str(longvalue)
1110 if slongvalue[-1] in ("L","l"): slongvalue = slongvalue[:-1]
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001111 self.checkequal(' 42', '%3ld', '__mod__', 42)
Facundo Batistac11cecf2008-02-24 03:17:21 +00001112 self.checkequal('42', '%d', '__mod__', 42L)
1113 self.checkequal('42', '%d', '__mod__', 42.0)
1114 self.checkequal(slongvalue, '%d', '__mod__', longvalue)
1115 self.checkcall('%d', '__mod__', float(longvalue))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001116 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001117 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001118
1119 self.checkraises(TypeError, 'abc', '__mod__')
1120 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1121 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1122 self.checkraises(TypeError, '%c', '__mod__', (None,))
1123 self.checkraises(ValueError, '%(foo', '__mod__', {})
1124 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
Facundo Batistac11cecf2008-02-24 03:17:21 +00001125 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric
1126 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int/long conversion provided
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001127
1128 # argument names with properly nested brackets are supported
1129 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1130
1131 # 100 is a magic number in PyUnicode_Format, this forces a resize
1132 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1133
1134 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1135 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1136 self.checkraises(ValueError, '%10', '__mod__', (42,))
1137
Benjamin Peterson23d49d32012-08-28 17:55:35 -04001138 class X(object): pass
1139 self.checkraises(TypeError, 'abc', '__mod__', X())
Benjamin Petersonda2c7eb2013-03-23 22:32:00 -05001140 class X(Exception):
1141 def __getitem__(self, k):
1142 return k
1143 self.checkequal('melon apple', '%(melon)s %(apple)s', '__mod__', X())
Benjamin Peterson23d49d32012-08-28 17:55:35 -04001144
Serhiy Storchaka76249ea2014-02-07 10:06:05 +02001145 @test_support.cpython_only
1146 def test_formatting_c_limits(self):
1147 from _testcapi import PY_SSIZE_T_MAX, INT_MAX, UINT_MAX
1148 SIZE_MAX = (1 << (PY_SSIZE_T_MAX.bit_length() + 1)) - 1
1149 width = int(PY_SSIZE_T_MAX + 1)
1150 if width <= sys.maxint:
1151 self.checkraises(OverflowError, '%*s', '__mod__', (width, ''))
1152 prec = int(INT_MAX + 1)
1153 if prec <= sys.maxint:
1154 self.checkraises(OverflowError, '%.*f', '__mod__', (prec, 1. / 7))
1155 # Issue 15989
1156 width = int(SIZE_MAX + 1)
1157 if width <= sys.maxint:
1158 self.checkraises(OverflowError, '%*s', '__mod__', (width, ''))
1159 prec = int(UINT_MAX + 1)
1160 if prec <= sys.maxint:
1161 self.checkraises(OverflowError, '%.*f', '__mod__', (prec, 1. / 7))
1162
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001163 def test_floatformatting(self):
1164 # float formatting
1165 for prec in xrange(100):
1166 format = '%%.%if' % prec
1167 value = 0.01
1168 for x in xrange(60):
Florent Xicluna9b90cd12010-09-13 07:46:37 +00001169 value = value * 3.14159265359 / 3.0 * 10.0
Mark Dickinson18cfada2009-11-23 18:46:41 +00001170 self.checkcall(format, "__mod__", value)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001171
Andrew Dalke2bddcbf2006-05-25 16:30:52 +00001172 def test_inplace_rewrites(self):
1173 # Check that strings don't copy and modify cached single-character strings
1174 self.checkequal('a', 'A', 'lower')
1175 self.checkequal(True, 'A', 'isupper')
1176 self.checkequal('A', 'a', 'upper')
1177 self.checkequal(True, 'a', 'islower')
Tim Petersd95d5932006-05-25 21:52:19 +00001178
Andrew Dalke2bddcbf2006-05-25 16:30:52 +00001179 self.checkequal('a', 'A', 'replace', 'A', 'a')
1180 self.checkequal(True, 'A', 'isupper')
1181
1182 self.checkequal('A', 'a', 'capitalize')
1183 self.checkequal(True, 'a', 'islower')
Tim Petersd95d5932006-05-25 21:52:19 +00001184
Andrew Dalke2bddcbf2006-05-25 16:30:52 +00001185 self.checkequal('A', 'a', 'swapcase')
1186 self.checkequal(True, 'a', 'islower')
1187
1188 self.checkequal('A', 'a', 'title')
1189 self.checkequal(True, 'a', 'islower')
1190
Fredrik Lundh06a69dd2006-05-26 08:54:28 +00001191 def test_partition(self):
1192
Fredrik Lundh9c0e9c02006-05-26 18:24:15 +00001193 self.checkequal(('this is the par', 'ti', 'tion method'),
1194 'this is the partition method', 'partition', 'ti')
Fredrik Lundh06a69dd2006-05-26 08:54:28 +00001195
1196 # from raymond's original specification
1197 S = 'http://www.python.org'
1198 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1199 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1200 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1201 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1202
1203 self.checkraises(ValueError, S, 'partition', '')
1204 self.checkraises(TypeError, S, 'partition', None)
1205
Amaury Forgeot d'Arc3571fbf2008-09-01 19:52:00 +00001206 # mixed use of str and unicode
1207 self.assertEqual('a/b/c'.partition(u'/'), ('a', '/', 'b/c'))
1208
Fredrik Lundh9c0e9c02006-05-26 18:24:15 +00001209 def test_rpartition(self):
1210
1211 self.checkequal(('this is the rparti', 'ti', 'on method'),
1212 'this is the rpartition method', 'rpartition', 'ti')
1213
1214 # from raymond's original specification
1215 S = 'http://www.python.org'
1216 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Raymond Hettingera0c95fa2006-09-04 15:32:48 +00001217 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Fredrik Lundh9c0e9c02006-05-26 18:24:15 +00001218 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1219 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1220
1221 self.checkraises(ValueError, S, 'rpartition', '')
1222 self.checkraises(TypeError, S, 'rpartition', None)
1223
Amaury Forgeot d'Arc3571fbf2008-09-01 19:52:00 +00001224 # mixed use of str and unicode
1225 self.assertEqual('a/b/c'.rpartition(u'/'), ('a/b', '/', 'c'))
Walter Dörwald57d88e52004-08-26 16:53:04 +00001226
Jesus Cea44e81682011-04-20 16:39:15 +02001227 def test_none_arguments(self):
1228 # issue 11828
1229 s = 'hello'
1230 self.checkequal(2, s, 'find', 'l', None)
1231 self.checkequal(3, s, 'find', 'l', -2, None)
1232 self.checkequal(2, s, 'find', 'l', None, -2)
1233 self.checkequal(0, s, 'find', 'h', None, None)
1234
1235 self.checkequal(3, s, 'rfind', 'l', None)
1236 self.checkequal(3, s, 'rfind', 'l', -2, None)
1237 self.checkequal(2, s, 'rfind', 'l', None, -2)
1238 self.checkequal(0, s, 'rfind', 'h', None, None)
1239
1240 self.checkequal(2, s, 'index', 'l', None)
1241 self.checkequal(3, s, 'index', 'l', -2, None)
1242 self.checkequal(2, s, 'index', 'l', None, -2)
1243 self.checkequal(0, s, 'index', 'h', None, None)
1244
1245 self.checkequal(3, s, 'rindex', 'l', None)
1246 self.checkequal(3, s, 'rindex', 'l', -2, None)
1247 self.checkequal(2, s, 'rindex', 'l', None, -2)
1248 self.checkequal(0, s, 'rindex', 'h', None, None)
1249
1250 self.checkequal(2, s, 'count', 'l', None)
1251 self.checkequal(1, s, 'count', 'l', -2, None)
1252 self.checkequal(1, s, 'count', 'l', None, -2)
1253 self.checkequal(0, s, 'count', 'x', None, None)
1254
1255 self.checkequal(True, s, 'endswith', 'o', None)
1256 self.checkequal(True, s, 'endswith', 'lo', -2, None)
1257 self.checkequal(True, s, 'endswith', 'l', None, -2)
1258 self.checkequal(False, s, 'endswith', 'x', None, None)
1259
1260 self.checkequal(True, s, 'startswith', 'h', None)
1261 self.checkequal(True, s, 'startswith', 'l', -2, None)
1262 self.checkequal(True, s, 'startswith', 'h', None, -2)
1263 self.checkequal(False, s, 'startswith', 'x', None, None)
1264
1265 def test_find_etc_raise_correct_error_messages(self):
1266 # issue 11828
1267 s = 'hello'
1268 x = 'x'
1269 self.assertRaisesRegexp(TypeError, r'\bfind\b', s.find,
1270 x, None, None, None)
1271 self.assertRaisesRegexp(TypeError, r'\brfind\b', s.rfind,
1272 x, None, None, None)
1273 self.assertRaisesRegexp(TypeError, r'\bindex\b', s.index,
1274 x, None, None, None)
1275 self.assertRaisesRegexp(TypeError, r'\brindex\b', s.rindex,
1276 x, None, None, None)
1277 self.assertRaisesRegexp(TypeError, r'^count\(', s.count,
1278 x, None, None, None)
1279 self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith,
1280 x, None, None, None)
1281 self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith,
1282 x, None, None, None)
1283
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001284class MixinStrStringUserStringTest:
1285 # Additional tests for 8bit strings, i.e. str, UserString and
1286 # the string module
1287
1288 def test_maketrans(self):
1289 self.assertEqual(
1290 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),
1291 string.maketrans('abc', 'xyz')
1292 )
1293 self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw')
1294
1295 def test_translate(self):
1296 table = string.maketrans('abc', 'xyz')
1297 self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def')
1298
1299 table = string.maketrans('a', 'A')
1300 self.checkequal('Abc', 'abc', 'translate', table)
1301 self.checkequal('xyz', 'xyz', 'translate', table)
1302 self.checkequal('yz', 'xyz', 'translate', table, 'x')
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001303 self.checkequal('yx', 'zyzzx', 'translate', None, 'z')
Raymond Hettinger4db5fe92007-04-12 04:10:00 +00001304 self.checkequal('zyzzx', 'zyzzx', 'translate', None, '')
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001305 self.checkequal('zyzzx', 'zyzzx', 'translate', None)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001306 self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')
1307 self.checkraises(ValueError, 'xyz', 'translate', 'too short')
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00001308
1309
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001310class MixinStrUserStringTest:
1311 # Additional tests that only work with
1312 # 8bit compatible object, i.e. str and UserString
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00001313
Zachary Ware1f702212013-12-10 14:09:20 -06001314 @unittest.skipUnless(test_support.have_unicode, 'no unicode support')
1315 def test_encoding_decoding(self):
1316 codecs = [('rot13', 'uryyb jbeyq'),
1317 ('base64', 'aGVsbG8gd29ybGQ=\n'),
1318 ('hex', '68656c6c6f20776f726c64'),
1319 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]
1320 for encoding, data in codecs:
Serhiy Storchakac7797dc2015-05-31 20:21:00 +03001321 with test_support.check_py3k_warnings():
1322 self.checkequal(data, 'hello world', 'encode', encoding)
1323 with test_support.check_py3k_warnings():
1324 self.checkequal('hello world', data, 'decode', encoding)
Zachary Ware1f702212013-12-10 14:09:20 -06001325 # zlib is optional, so we make the test optional too...
1326 try:
1327 import zlib
1328 except ImportError:
1329 pass
1330 else:
1331 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'
Serhiy Storchakac7797dc2015-05-31 20:21:00 +03001332 with test_support.check_py3k_warnings():
1333 self.checkequal(data, 'hello world', 'encode', 'zlib')
1334 with test_support.check_py3k_warnings():
1335 self.checkequal('hello world', data, 'decode', 'zlib')
Walter Dörwald97951de2003-03-26 14:31:25 +00001336
Zachary Ware1f702212013-12-10 14:09:20 -06001337 self.checkraises(TypeError, 'xyz', 'decode', 42)
1338 self.checkraises(TypeError, 'xyz', 'encode', 42)
Walter Dörwald57d88e52004-08-26 16:53:04 +00001339
1340
1341class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001342 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001343
1344 def test_bug1001011(self):
1345 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001346 # involving a subclass.
1347 # Make sure that it is of the appropriate type.
1348 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001349 t = self.type2test
1350 class subclass(t):
1351 pass
1352 s1 = subclass("abcd")
1353 s2 = t().join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001354 self.assertTrue(s1 is not s2)
1355 self.assertTrue(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001356
1357 s1 = t("abcd")
1358 s2 = t().join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001359 self.assertTrue(s1 is s2)
Tim Peters108f1372004-08-27 05:36:07 +00001360
1361 # Should also test mixed-type join.
1362 if t is unicode:
1363 s1 = subclass("abcd")
1364 s2 = "".join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001365 self.assertTrue(s1 is not s2)
1366 self.assertTrue(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001367
1368 s1 = t("abcd")
1369 s2 = "".join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001370 self.assertTrue(s1 is s2)
Tim Peters108f1372004-08-27 05:36:07 +00001371
1372 elif t is str:
1373 s1 = subclass("abcd")
1374 s2 = u"".join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001375 self.assertTrue(s1 is not s2)
1376 self.assertTrue(type(s2) is unicode) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001377
1378 s1 = t("abcd")
1379 s2 = u"".join([s1])
Ezio Melotti2623a372010-11-21 13:34:58 +00001380 self.assertTrue(s1 is not s2)
1381 self.assertTrue(type(s2) is unicode) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001382
1383 else:
1384 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)