blob: e9285a62f6ff76906cf430650cc7c2ee67f47947 [file] [log] [blame]
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001"""
2Common tests shared by test_str, test_unicode, test_userstring and test_string.
3"""
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +00004
Guido van Rossum360e4b82007-05-14 22:51:27 +00005import unittest, string, sys, struct
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):
Guido van Rossume2a383d2007-01-15 16:59:06 +000015 def __init__(self): self.seq = [7, 'hello', 123]
Guido van Rossumf1044292007-09-27 18:01:22 +000016 def __str__(self): return '{0} {1} {2}'.format(*self.seq)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000017
18class BadSeq2(Sequence):
19 def __init__(self): self.seq = ['a', 'b', 'c']
20 def __len__(self): return 8
21
Georg Brandlc7885542007-03-06 19:16:20 +000022class BaseTest(unittest.TestCase):
23 # These tests are for buffers of values (bytes) and not
24 # specific to character interpretation, used for bytes objects
25 # and various string implementations
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000026
Walter Dörwald0fd583c2003-02-21 12:53:50 +000027 # The type to be tested
28 # Change in subclasses to change the behaviour of fixtesttype()
29 type2test = None
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000030
Walter Dörwald0fd583c2003-02-21 12:53:50 +000031 # All tests pass their arguments to the testing methods
32 # as str objects. fixtesttype() can be used to propagate
33 # these arguments to the appropriate type
34 def fixtype(self, obj):
35 if isinstance(obj, str):
36 return self.__class__.type2test(obj)
37 elif isinstance(obj, list):
38 return [self.fixtype(x) for x in obj]
39 elif isinstance(obj, tuple):
40 return tuple([self.fixtype(x) for x in obj])
41 elif isinstance(obj, dict):
42 return dict([
43 (self.fixtype(key), self.fixtype(value))
Guido van Rossumcc2b0162007-02-11 06:12:03 +000044 for (key, value) in obj.items()
Walter Dörwald0fd583c2003-02-21 12:53:50 +000045 ])
46 else:
47 return obj
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000048
Guido van Rossum09549f42007-08-27 20:40:10 +000049 # check that obj.method(*args) returns result
50 def checkequal(self, result, obj, methodname, *args):
Walter Dörwald0fd583c2003-02-21 12:53:50 +000051 result = self.fixtype(result)
Guido van Rossum09549f42007-08-27 20:40:10 +000052 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000053 args = self.fixtype(args)
Guido van Rossum09549f42007-08-27 20:40:10 +000054 realresult = getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000055 self.assertEqual(
56 result,
57 realresult
58 )
59 # if the original is returned make sure that
60 # this doesn't happen with subclasses
Guido van Rossum09549f42007-08-27 20:40:10 +000061 if obj is realresult:
62 try:
63 class subtype(self.__class__.type2test):
64 pass
65 except TypeError:
66 pass # Skip this if we can't subclass
67 else:
68 obj = subtype(obj)
69 realresult = getattr(obj, methodname)(*args)
70 self.assert_(obj is not realresult)
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000071
Guido van Rossum09549f42007-08-27 20:40:10 +000072 # check that obj.method(*args) raises exc
73 def checkraises(self, exc, obj, methodname, *args):
74 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000075 args = self.fixtype(args)
76 self.assertRaises(
77 exc,
Guido van Rossum09549f42007-08-27 20:40:10 +000078 getattr(obj, methodname),
Walter Dörwald0fd583c2003-02-21 12:53:50 +000079 *args
80 )
Jeremy Hyltonf82b04e2000-07-10 17:08:42 +000081
Guido van Rossum09549f42007-08-27 20:40:10 +000082 # call obj.method(*args) without any checks
83 def checkcall(self, obj, methodname, *args):
84 obj = self.fixtype(obj)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000085 args = self.fixtype(args)
Guido van Rossum09549f42007-08-27 20:40:10 +000086 getattr(obj, methodname)(*args)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000087
Walter Dörwald0fd583c2003-02-21 12:53:50 +000088 def test_count(self):
89 self.checkequal(3, 'aaa', 'count', 'a')
90 self.checkequal(0, 'aaa', 'count', 'b')
91 self.checkequal(3, 'aaa', 'count', 'a')
92 self.checkequal(0, 'aaa', 'count', 'b')
93 self.checkequal(3, 'aaa', 'count', 'a')
94 self.checkequal(0, 'aaa', 'count', 'b')
95 self.checkequal(0, 'aaa', 'count', 'b')
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000096 self.checkequal(2, 'aaa', 'count', 'a', 1)
97 self.checkequal(0, 'aaa', 'count', 'a', 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +000098 self.checkequal(1, 'aaa', 'count', 'a', -1)
99 self.checkequal(3, 'aaa', 'count', 'a', -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000100 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)
101 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000102 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)
103 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000104 self.checkequal(3, 'aaa', 'count', '', 1)
105 self.checkequal(1, 'aaa', 'count', '', 3)
106 self.checkequal(0, 'aaa', 'count', '', 10)
107 self.checkequal(2, 'aaa', 'count', '', -1)
108 self.checkequal(4, 'aaa', 'count', '', -10)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000109
110 self.checkraises(TypeError, 'hello', 'count')
111 self.checkraises(TypeError, 'hello', 'count', 42)
112
Raymond Hettinger57e74472005-02-20 09:54:53 +0000113 # For a variety of combinations,
114 # verify that str.count() matches an equivalent function
115 # replacing all occurrences and then differencing the string lengths
116 charset = ['', 'a', 'b']
117 digits = 7
118 base = len(charset)
119 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000120 for i in range(base ** digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000121 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000122 for j in range(digits):
Raymond Hettinger57e74472005-02-20 09:54:53 +0000123 i, m = divmod(i, base)
124 entry.append(charset[m])
125 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000126 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger57e74472005-02-20 09:54:53 +0000127 for i in teststrings:
Raymond Hettinger57e74472005-02-20 09:54:53 +0000128 n = len(i)
129 for j in teststrings:
130 r1 = i.count(j)
131 if j:
Guido van Rossum09549f42007-08-27 20:40:10 +0000132 r2, rem = divmod(n - len(i.replace(j, self.fixtype(''))),
133 len(j))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000134 else:
135 r2, rem = len(i)+1, 0
136 if rem or r1 != r2:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000137 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
138 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
Raymond Hettinger57e74472005-02-20 09:54:53 +0000139
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000140 def test_find(self):
141 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
142 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
143 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)
144
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000145 self.checkequal(0, 'abc', 'find', '', 0)
146 self.checkequal(3, 'abc', 'find', '', 3)
147 self.checkequal(-1, 'abc', 'find', '', 4)
148
Christian Heimes9cd17752007-11-18 19:35:23 +0000149 # to check the ability to pass None as defaults
150 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')
151 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)
152 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)
153 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)
154 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)
155
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000156 self.checkraises(TypeError, 'hello', 'find')
157 self.checkraises(TypeError, 'hello', 'find', 42)
158
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000159 # For a variety of combinations,
160 # verify that str.find() matches __contains__
161 # and that the found substring is really at that location
162 charset = ['', 'a', 'b', 'c']
163 digits = 5
164 base = len(charset)
165 teststrings = set()
Guido van Rossum805365e2007-05-07 22:24:25 +0000166 for i in range(base ** digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000167 entry = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000168 for j in range(digits):
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000169 i, m = divmod(i, base)
170 entry.append(charset[m])
171 teststrings.add(''.join(entry))
Guido van Rossum09549f42007-08-27 20:40:10 +0000172 teststrings = [self.fixtype(ts) for ts in teststrings]
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000173 for i in teststrings:
Raymond Hettinger7cbf1bc2005-02-20 04:07:08 +0000174 for j in teststrings:
175 loc = i.find(j)
176 r1 = (loc != -1)
177 r2 = j in i
178 if r1 != r2:
179 self.assertEqual(r1, r2)
180 if loc != -1:
181 self.assertEqual(i[loc:loc+len(j)], j)
182
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000183 def test_rfind(self):
184 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')
185 self.checkequal(12, 'abcdefghiabc', 'rfind', '')
186 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')
187 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')
188
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000189 self.checkequal(3, 'abc', 'rfind', '', 0)
190 self.checkequal(3, 'abc', 'rfind', '', 3)
191 self.checkequal(-1, 'abc', 'rfind', '', 4)
192
Christian Heimes9cd17752007-11-18 19:35:23 +0000193 # to check the ability to pass None as defaults
194 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')
195 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)
196 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)
197 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)
198 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)
199
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000200 self.checkraises(TypeError, 'hello', 'rfind')
201 self.checkraises(TypeError, 'hello', 'rfind', 42)
202
203 def test_index(self):
204 self.checkequal(0, 'abcdefghiabc', 'index', '')
205 self.checkequal(3, 'abcdefghiabc', 'index', 'def')
206 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')
207 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)
208
209 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')
210 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)
211 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)
212 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)
213
Christian Heimes9cd17752007-11-18 19:35:23 +0000214 # to check the ability to pass None as defaults
215 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')
216 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)
217 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)
218 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)
219 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)
220
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000221 self.checkraises(TypeError, 'hello', 'index')
222 self.checkraises(TypeError, 'hello', 'index', 42)
223
224 def test_rindex(self):
225 self.checkequal(12, 'abcdefghiabc', 'rindex', '')
226 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')
227 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')
228 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
229
230 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')
231 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)
232 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)
233 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)
234 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)
235
Christian Heimes9cd17752007-11-18 19:35:23 +0000236 # to check the ability to pass None as defaults
237 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')
238 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)
239 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)
240 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)
241 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)
242
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000243 self.checkraises(TypeError, 'hello', 'rindex')
244 self.checkraises(TypeError, 'hello', 'rindex', 42)
245
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000246 def test_lower(self):
247 self.checkequal('hello', 'HeLLo', 'lower')
248 self.checkequal('hello', 'hello', 'lower')
249 self.checkraises(TypeError, 'hello', 'lower', 42)
250
251 def test_upper(self):
252 self.checkequal('HELLO', 'HeLLo', 'upper')
253 self.checkequal('HELLO', 'HELLO', 'upper')
254 self.checkraises(TypeError, 'hello', 'upper', 42)
255
256 def test_expandtabs(self):
257 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
258 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
259 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
260 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
261 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
262 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
263 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
264 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)
265
266 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
267 # This test is only valid when sizeof(int) == sizeof(void*) == 4.
268 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
269 self.checkraises(OverflowError,
270 '\ta\n\tb', 'expandtabs', sys.maxint)
271
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000272 def test_split(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000273 # by a char
274 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000276 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)
277 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)
278 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)
279 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',
281 sys.maxint-2)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000282 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)
283 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)
284 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000285 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')
286 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000287 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)
288
Thomas Wouters477c8d52006-05-27 19:21:47 +0000289 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')
290 self.checkequal(['a']*15 +['a|a|a|a|a'],
291 ('a|'*20)[:-1], 'split', '|', 15)
292
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000293 # by string
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000294 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000295 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)
296 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)
297 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)
298 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000299 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',
300 sys.maxint-10)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000301 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)
302 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000303 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000304 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')
305 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
306 'split', 'test')
307 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')
308 self.checkequal(['', ''], 'aaa', 'split', 'aaa')
309 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)
310 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')
311 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')
312 self.checkequal([''], '', 'split', 'aaa')
313 self.checkequal(['aa'], 'aa', 'split', 'aaa')
314 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')
315 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')
316
317 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')
318 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)
319 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],
320 'split', 'BLAH', 18)
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000321
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000322 # argument type
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000323 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)
324
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325 # null case
326 self.checkraises(ValueError, 'hello', 'split', '')
327 self.checkraises(ValueError, 'hello', 'split', '', 0)
328
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000329 def test_rsplit(self):
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000330 # by a char
331 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')
332 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)
333 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)
334 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)
335 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000336 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',
337 sys.maxint-100)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000338 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)
339 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)
340 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000341 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')
342 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')
343
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000344 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)
345
Thomas Wouters477c8d52006-05-27 19:21:47 +0000346 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')
347 self.checkequal(['a|a|a|a|a']+['a']*15,
348 ('a|'*20)[:-1], 'rsplit', '|', 15)
349
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000350 # by string
351 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')
352 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)
353 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)
354 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)
355 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000356 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',
357 sys.maxint-5)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000358 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)
359 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)
360 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000361 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')
362 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',
363 'rsplit', 'test')
364 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')
365 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')
366 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)
367 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')
368 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')
369 self.checkequal([''], '', 'rsplit', 'aaa')
370 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')
371 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')
372 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')
373
374 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')
375 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)
376 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],
377 'rsplit', 'BLAH', 18)
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000378
Hye-Shik Chang75c00ef2004-01-05 00:29:51 +0000379 # argument type
380 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +0000381
Thomas Wouters477c8d52006-05-27 19:21:47 +0000382 # null case
383 self.checkraises(ValueError, 'hello', 'rsplit', '')
384 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)
385
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000386 def test_replace(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000387 EQ = self.checkequal
388
389 # Operations on the empty string
390 EQ("", "", "replace", "", "")
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000391 EQ("A", "", "replace", "", "A")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000392 EQ("", "", "replace", "A", "")
393 EQ("", "", "replace", "A", "A")
394 EQ("", "", "replace", "", "", 100)
395 EQ("", "", "replace", "", "", sys.maxint)
396
397 # interleave (from=="", 'to' gets inserted everywhere)
398 EQ("A", "A", "replace", "", "")
399 EQ("*A*", "A", "replace", "", "*")
400 EQ("*1A*1", "A", "replace", "", "*1")
401 EQ("*-#A*-#", "A", "replace", "", "*-#")
402 EQ("*-A*-A*-", "AA", "replace", "", "*-")
403 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)
404 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)
405 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)
406 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)
407 EQ("*-A*-A", "AA", "replace", "", "*-", 2)
408 EQ("*-AA", "AA", "replace", "", "*-", 1)
409 EQ("AA", "AA", "replace", "", "*-", 0)
410
411 # single character deletion (from=="A", to=="")
412 EQ("", "A", "replace", "A", "")
413 EQ("", "AAA", "replace", "A", "")
414 EQ("", "AAA", "replace", "A", "", -1)
415 EQ("", "AAA", "replace", "A", "", sys.maxint)
416 EQ("", "AAA", "replace", "A", "", 4)
417 EQ("", "AAA", "replace", "A", "", 3)
418 EQ("A", "AAA", "replace", "A", "", 2)
419 EQ("AA", "AAA", "replace", "A", "", 1)
420 EQ("AAA", "AAA", "replace", "A", "", 0)
421 EQ("", "AAAAAAAAAA", "replace", "A", "")
422 EQ("BCD", "ABACADA", "replace", "A", "")
423 EQ("BCD", "ABACADA", "replace", "A", "", -1)
424 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)
425 EQ("BCD", "ABACADA", "replace", "A", "", 5)
426 EQ("BCD", "ABACADA", "replace", "A", "", 4)
427 EQ("BCDA", "ABACADA", "replace", "A", "", 3)
428 EQ("BCADA", "ABACADA", "replace", "A", "", 2)
429 EQ("BACADA", "ABACADA", "replace", "A", "", 1)
430 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)
431 EQ("BCD", "ABCAD", "replace", "A", "")
432 EQ("BCD", "ABCADAA", "replace", "A", "")
433 EQ("BCD", "BCD", "replace", "A", "")
434 EQ("*************", "*************", "replace", "A", "")
435 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)
436
437 # substring deletion (from=="the", to=="")
438 EQ("", "the", "replace", "the", "")
439 EQ("ater", "theater", "replace", "the", "")
440 EQ("", "thethe", "replace", "the", "")
441 EQ("", "thethethethe", "replace", "the", "")
442 EQ("aaaa", "theatheatheathea", "replace", "the", "")
443 EQ("that", "that", "replace", "the", "")
444 EQ("thaet", "thaet", "replace", "the", "")
445 EQ("here and re", "here and there", "replace", "the", "")
446 EQ("here and re and re", "here and there and there",
447 "replace", "the", "", sys.maxint)
448 EQ("here and re and re", "here and there and there",
449 "replace", "the", "", -1)
450 EQ("here and re and re", "here and there and there",
451 "replace", "the", "", 3)
452 EQ("here and re and re", "here and there and there",
453 "replace", "the", "", 2)
454 EQ("here and re and there", "here and there and there",
455 "replace", "the", "", 1)
456 EQ("here and there and there", "here and there and there",
457 "replace", "the", "", 0)
458 EQ("here and re and re", "here and there and there", "replace", "the", "")
459
460 EQ("abc", "abc", "replace", "the", "")
461 EQ("abcdefg", "abcdefg", "replace", "the", "")
462
463 # substring deletion (from=="bob", to=="")
464 EQ("bob", "bbobob", "replace", "bob", "")
465 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")
466 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")
467 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")
468
469 # single character replace in place (len(from)==len(to)==1)
470 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")
471 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")
472 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)
473 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)
474 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)
475 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)
476 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)
477 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)
478
479 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")
480 EQ("who goes there?", "Who goes there?", "replace", "W", "w")
481 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")
482 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")
483 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")
484
485 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")
486
487 # substring replace in place (len(from)==len(to) > 1)
488 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")
489 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)
490 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)
491 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)
492 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)
493 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)
494 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)
495 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)
496 EQ("cobob", "bobob", "replace", "bob", "cob")
497 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")
498 EQ("bobob", "bobob", "replace", "bot", "bot")
499
500 # replace single character (len(from)==1, len(to)>1)
501 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")
502 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)
503 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)
504 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)
505 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)
506 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)
507 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")
508
509 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")
510
511 # replace substring (len(from)>1, len(to)!=len(from))
512 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
513 "replace", "spam", "ham")
514 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
515 "replace", "spam", "ham", sys.maxint)
516 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
517 "replace", "spam", "ham", -1)
518 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
519 "replace", "spam", "ham", 4)
520 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",
521 "replace", "spam", "ham", 3)
522 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",
523 "replace", "spam", "ham", 2)
524 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",
525 "replace", "spam", "ham", 1)
526 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",
527 "replace", "spam", "ham", 0)
528
529 EQ("bobob", "bobobob", "replace", "bobob", "bob")
530 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")
531 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")
532
Guido van Rossum39478e82007-08-27 17:23:59 +0000533 # XXX Commented out. Is there any reason to support buffer objects
534 # as arguments for str.replace()? GvR
Guido van Rossum254348e2007-11-21 19:29:53 +0000535## ba = bytearray('a')
536## bb = bytearray('b')
Guido van Rossum39478e82007-08-27 17:23:59 +0000537## EQ("bbc", "abc", "replace", ba, bb)
538## EQ("aac", "abc", "replace", bb, ba)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000539
Thomas Wouters477c8d52006-05-27 19:21:47 +0000540 #
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000541 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)
542 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')
543 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)
544 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)
545 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)
546 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)
547 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')
548 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')
549 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)
550 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')
551 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)
552 self.checkequal('abc', 'abc', 'replace', '', '-', 0)
553 self.checkequal('', '', 'replace', '', '')
554 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)
555 self.checkequal('abc', 'abc', 'replace', 'xy', '--')
556 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with
557 # MemoryError due to empty result (platform malloc issue when requesting
558 # 0 bytes).
559 self.checkequal('', '123', 'replace', '123', '')
560 self.checkequal('', '123123', 'replace', '123', '')
561 self.checkequal('x', '123x123', 'replace', '123', '')
562
563 self.checkraises(TypeError, 'hello', 'replace')
564 self.checkraises(TypeError, 'hello', 'replace', 42)
565 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
566 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
567
Thomas Wouters477c8d52006-05-27 19:21:47 +0000568 def test_replace_overflow(self):
569 # Check for overflow checking on 32 bit machines
Guido van Rossum360e4b82007-05-14 22:51:27 +0000570 if sys.maxint != 2147483647 or struct.calcsize("P") > 4:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000571 return
572 A2_16 = "A" * (2**16)
573 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)
574 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
575 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
576
Georg Brandlc7885542007-03-06 19:16:20 +0000577
578
579class CommonTest(BaseTest):
580 # This testcase contains test that can be used in all
581 # stringlike classes. Currently this is str, unicode
582 # UserString and the string module.
583
584 def test_hash(self):
585 # SF bug 1054139: += optimization was not invalidating cached hash value
586 a = self.type2test('DNSSEC')
587 b = self.type2test('')
588 for c in a:
Guido van Rossum98297ee2007-11-06 21:34:58 +0000589## # Special case for the str8, since indexing returns a integer
590## # XXX Maybe it would be a good idea to seperate str8's tests...
591## if self.type2test == str8:
592## c = chr(c)
Georg Brandlc7885542007-03-06 19:16:20 +0000593 b += c
594 hash(b)
595 self.assertEqual(hash(a), hash(b))
596
597 def test_capitalize(self):
598 self.checkequal(' hello ', ' hello ', 'capitalize')
599 self.checkequal('Hello ', 'Hello ','capitalize')
600 self.checkequal('Hello ', 'hello ','capitalize')
601 self.checkequal('Aaaa', 'aaaa', 'capitalize')
602 self.checkequal('Aaaa', 'AaAa', 'capitalize')
603
604 self.checkraises(TypeError, 'hello', 'capitalize', 42)
605
606 def test_lower(self):
607 self.checkequal('hello', 'HeLLo', 'lower')
608 self.checkequal('hello', 'hello', 'lower')
609 self.checkraises(TypeError, 'hello', 'lower', 42)
610
611 def test_upper(self):
612 self.checkequal('HELLO', 'HeLLo', 'upper')
613 self.checkequal('HELLO', 'HELLO', 'upper')
614 self.checkraises(TypeError, 'hello', 'upper', 42)
615
616 def test_expandtabs(self):
617 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
618 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
619 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)
620 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)
621 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')
622 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)
623 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)
624
625 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
626
627 def test_additional_split(self):
628 self.checkequal(['this', 'is', 'the', 'split', 'function'],
629 'this is the split function', 'split')
630
631 # by whitespace
632 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')
633 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)
634 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
635 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)
636 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)
637 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,
638 sys.maxint-1)
639 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)
640 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)
641 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)
642
643 self.checkequal([], ' ', 'split')
644 self.checkequal(['a'], ' a ', 'split')
645 self.checkequal(['a', 'b'], ' a b ', 'split')
646 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)
647 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)
648 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)
649 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')
650 aaa = ' a '*20
651 self.checkequal(['a']*20, aaa, 'split')
652 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)
653 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)
654
655 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000656 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000657
658 def test_additional_rsplit(self):
659 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],
660 'this is the rsplit function', 'rsplit')
661
662 # by whitespace
663 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')
664 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)
665 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
666 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)
667 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)
668 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,
669 sys.maxint-20)
670 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)
671 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)
672 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)
673
674 self.checkequal([], ' ', 'rsplit')
675 self.checkequal(['a'], ' a ', 'rsplit')
676 self.checkequal(['a', 'b'], ' a b ', 'rsplit')
677 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)
678 self.checkequal([' a b','c'], ' a b c ', 'rsplit',
679 None, 1)
680 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',
681 None, 2)
682 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)
683 aaa = ' a '*20
684 self.checkequal(['a']*20, aaa, 'rsplit')
685 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)
686 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)
687
688 # mixed use of str and unicode
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000689 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', ' ', 2)
Georg Brandlc7885542007-03-06 19:16:20 +0000690
691 def test_strip(self):
692 self.checkequal('hello', ' hello ', 'strip')
693 self.checkequal('hello ', ' hello ', 'lstrip')
694 self.checkequal(' hello', ' hello ', 'rstrip')
695 self.checkequal('hello', 'hello', 'strip')
696
697 # strip/lstrip/rstrip with None arg
698 self.checkequal('hello', ' hello ', 'strip', None)
699 self.checkequal('hello ', ' hello ', 'lstrip', None)
700 self.checkequal(' hello', ' hello ', 'rstrip', None)
701 self.checkequal('hello', 'hello', 'strip', None)
702
703 # strip/lstrip/rstrip with str arg
704 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
705 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
706 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
707 self.checkequal('hello', 'hello', 'strip', 'xyz')
708
Georg Brandlc7885542007-03-06 19:16:20 +0000709 self.checkraises(TypeError, 'hello', 'strip', 42, 42)
710 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
711 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
712
713 def test_ljust(self):
714 self.checkequal('abc ', 'abc', 'ljust', 10)
715 self.checkequal('abc ', 'abc', 'ljust', 6)
716 self.checkequal('abc', 'abc', 'ljust', 3)
717 self.checkequal('abc', 'abc', 'ljust', 2)
718 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')
719 self.checkraises(TypeError, 'abc', 'ljust')
720
721 def test_rjust(self):
722 self.checkequal(' abc', 'abc', 'rjust', 10)
723 self.checkequal(' abc', 'abc', 'rjust', 6)
724 self.checkequal('abc', 'abc', 'rjust', 3)
725 self.checkequal('abc', 'abc', 'rjust', 2)
726 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')
727 self.checkraises(TypeError, 'abc', 'rjust')
728
729 def test_center(self):
730 self.checkequal(' abc ', 'abc', 'center', 10)
731 self.checkequal(' abc ', 'abc', 'center', 6)
732 self.checkequal('abc', 'abc', 'center', 3)
733 self.checkequal('abc', 'abc', 'center', 2)
734 self.checkequal('***abc****', 'abc', 'center', 10, '*')
735 self.checkraises(TypeError, 'abc', 'center')
736
737 def test_swapcase(self):
738 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')
739
740 self.checkraises(TypeError, 'hello', 'swapcase', 42)
741
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000742 def test_zfill(self):
743 self.checkequal('123', '123', 'zfill', 2)
744 self.checkequal('123', '123', 'zfill', 3)
745 self.checkequal('0123', '123', 'zfill', 4)
746 self.checkequal('+123', '+123', 'zfill', 3)
747 self.checkequal('+123', '+123', 'zfill', 4)
748 self.checkequal('+0123', '+123', 'zfill', 5)
749 self.checkequal('-123', '-123', 'zfill', 3)
750 self.checkequal('-123', '-123', 'zfill', 4)
751 self.checkequal('-0123', '-123', 'zfill', 5)
752 self.checkequal('000', '', 'zfill', 3)
753 self.checkequal('34', '34', 'zfill', 1)
754 self.checkequal('0034', '34', 'zfill', 4)
755
756 self.checkraises(TypeError, '123', 'zfill')
757
758class MixinStrUnicodeUserStringTest:
759 # additional tests that only work for
760 # stringlike objects, i.e. str, unicode, UserString
761 # (but not the string module)
762
763 def test_islower(self):
764 self.checkequal(False, '', 'islower')
765 self.checkequal(True, 'a', 'islower')
766 self.checkequal(False, 'A', 'islower')
767 self.checkequal(False, '\n', 'islower')
768 self.checkequal(True, 'abc', 'islower')
769 self.checkequal(False, 'aBc', 'islower')
770 self.checkequal(True, 'abc\n', 'islower')
771 self.checkraises(TypeError, 'abc', 'islower', 42)
772
773 def test_isupper(self):
774 self.checkequal(False, '', 'isupper')
775 self.checkequal(False, 'a', 'isupper')
776 self.checkequal(True, 'A', 'isupper')
777 self.checkequal(False, '\n', 'isupper')
778 self.checkequal(True, 'ABC', 'isupper')
779 self.checkequal(False, 'AbC', 'isupper')
780 self.checkequal(True, 'ABC\n', 'isupper')
781 self.checkraises(TypeError, 'abc', 'isupper', 42)
782
783 def test_istitle(self):
784 self.checkequal(False, '', 'istitle')
785 self.checkequal(False, 'a', 'istitle')
786 self.checkequal(True, 'A', 'istitle')
787 self.checkequal(False, '\n', 'istitle')
788 self.checkequal(True, 'A Titlecased Line', 'istitle')
789 self.checkequal(True, 'A\nTitlecased Line', 'istitle')
790 self.checkequal(True, 'A Titlecased, Line', 'istitle')
791 self.checkequal(False, 'Not a capitalized String', 'istitle')
792 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')
793 self.checkequal(False, 'Not--a Titlecase String', 'istitle')
794 self.checkequal(False, 'NOT', 'istitle')
795 self.checkraises(TypeError, 'abc', 'istitle', 42)
796
797 def test_isspace(self):
798 self.checkequal(False, '', 'isspace')
799 self.checkequal(False, 'a', 'isspace')
800 self.checkequal(True, ' ', 'isspace')
801 self.checkequal(True, '\t', 'isspace')
802 self.checkequal(True, '\r', 'isspace')
803 self.checkequal(True, '\n', 'isspace')
804 self.checkequal(True, ' \t\r\n', 'isspace')
805 self.checkequal(False, ' \t\r\na', 'isspace')
806 self.checkraises(TypeError, 'abc', 'isspace', 42)
807
808 def test_isalpha(self):
809 self.checkequal(False, '', 'isalpha')
810 self.checkequal(True, 'a', 'isalpha')
811 self.checkequal(True, 'A', 'isalpha')
812 self.checkequal(False, '\n', 'isalpha')
813 self.checkequal(True, 'abc', 'isalpha')
814 self.checkequal(False, 'aBc123', 'isalpha')
815 self.checkequal(False, 'abc\n', 'isalpha')
816 self.checkraises(TypeError, 'abc', 'isalpha', 42)
817
818 def test_isalnum(self):
819 self.checkequal(False, '', 'isalnum')
820 self.checkequal(True, 'a', 'isalnum')
821 self.checkequal(True, 'A', 'isalnum')
822 self.checkequal(False, '\n', 'isalnum')
823 self.checkequal(True, '123abc456', 'isalnum')
824 self.checkequal(True, 'a1b3c', 'isalnum')
825 self.checkequal(False, 'aBc000 ', 'isalnum')
826 self.checkequal(False, 'abc\n', 'isalnum')
827 self.checkraises(TypeError, 'abc', 'isalnum', 42)
828
829 def test_isdigit(self):
830 self.checkequal(False, '', 'isdigit')
831 self.checkequal(False, 'a', 'isdigit')
832 self.checkequal(True, '0', 'isdigit')
833 self.checkequal(True, '0123456789', 'isdigit')
834 self.checkequal(False, '0123456789a', 'isdigit')
835
836 self.checkraises(TypeError, 'abc', 'isdigit', 42)
837
838 def test_title(self):
839 self.checkequal(' Hello ', ' hello ', 'title')
840 self.checkequal('Hello ', 'hello ', 'title')
841 self.checkequal('Hello ', 'Hello ', 'title')
842 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')
843 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )
844 self.checkequal('Getint', "getInt", 'title')
845 self.checkraises(TypeError, 'hello', 'title', 42)
846
847 def test_splitlines(self):
848 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')
849 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')
850 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')
851 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')
852 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')
853 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')
854 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)
855
856 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)
857
858 def test_startswith(self):
859 self.checkequal(True, 'hello', 'startswith', 'he')
860 self.checkequal(True, 'hello', 'startswith', 'hello')
861 self.checkequal(False, 'hello', 'startswith', 'hello world')
862 self.checkequal(True, 'hello', 'startswith', '')
863 self.checkequal(False, 'hello', 'startswith', 'ello')
864 self.checkequal(True, 'hello', 'startswith', 'ello', 1)
865 self.checkequal(True, 'hello', 'startswith', 'o', 4)
866 self.checkequal(False, 'hello', 'startswith', 'o', 5)
867 self.checkequal(True, 'hello', 'startswith', '', 5)
868 self.checkequal(False, 'hello', 'startswith', 'lo', 6)
869 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)
870 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)
871 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)
872
873 # test negative indices
874 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)
875 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)
876 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)
877 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)
878 self.checkequal(False, 'hello', 'startswith', 'ello', -5)
879 self.checkequal(True, 'hello', 'startswith', 'ello', -4)
880 self.checkequal(False, 'hello', 'startswith', 'o', -2)
881 self.checkequal(True, 'hello', 'startswith', 'o', -1)
882 self.checkequal(True, 'hello', 'startswith', '', -3, -3)
883 self.checkequal(False, 'hello', 'startswith', 'lo', -9)
884
885 self.checkraises(TypeError, 'hello', 'startswith')
886 self.checkraises(TypeError, 'hello', 'startswith', 42)
887
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000888 # test tuple arguments
889 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))
890 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))
891 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))
892 self.checkequal(False, 'hello', 'startswith', ())
893 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',
894 'rld', 'lowo'), 3)
895 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',
896 'rld'), 3)
897 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)
898 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)
899 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)
900
901 self.checkraises(TypeError, 'hello', 'startswith', (42,))
902
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000903 def test_endswith(self):
904 self.checkequal(True, 'hello', 'endswith', 'lo')
905 self.checkequal(False, 'hello', 'endswith', 'he')
906 self.checkequal(True, 'hello', 'endswith', '')
907 self.checkequal(False, 'hello', 'endswith', 'hello world')
908 self.checkequal(False, 'helloworld', 'endswith', 'worl')
909 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)
910 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)
911 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)
912 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)
913 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)
914 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)
915 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)
916 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)
917 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)
918
919 # test negative indices
920 self.checkequal(True, 'hello', 'endswith', 'lo', -2)
921 self.checkequal(False, 'hello', 'endswith', 'he', -2)
922 self.checkequal(True, 'hello', 'endswith', '', -3, -3)
923 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)
924 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)
925 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)
926 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)
927 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)
928 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)
929 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)
930 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)
931 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)
932 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)
933
934 self.checkraises(TypeError, 'hello', 'endswith')
935 self.checkraises(TypeError, 'hello', 'endswith', 42)
936
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000937 # test tuple arguments
938 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))
939 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))
940 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))
941 self.checkequal(False, 'hello', 'endswith', ())
942 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',
943 'rld', 'lowo'), 3)
944 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',
945 'rld'), 3, -1)
946 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)
947 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)
948 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)
949
950 self.checkraises(TypeError, 'hello', 'endswith', (42,))
951
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000952 def test___contains__(self):
953 self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
954 self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)
955 self.checkequal(False, 'abc', '__contains__', '\0') # vereq('\0' in 'abc', False)
956 self.checkequal(True, '\0abc', '__contains__', '\0') # vereq('\0' in '\0abc', True)
957 self.checkequal(True, 'abc\0', '__contains__', '\0') # vereq('\0' in 'abc\0', True)
958 self.checkequal(True, '\0abc', '__contains__', 'a') # vereq('a' in '\0abc', True)
959 self.checkequal(True, 'asdf', '__contains__', 'asdf') # vereq('asdf' in 'asdf', True)
960 self.checkequal(False, 'asd', '__contains__', 'asdf') # vereq('asdf' in 'asd', False)
961 self.checkequal(False, '', '__contains__', 'asdf') # vereq('asdf' in '', False)
962
963 def test_subscript(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000964 self.checkequal('a', 'abc', '__getitem__', 0)
965 self.checkequal('c', 'abc', '__getitem__', -1)
966 self.checkequal('a', 'abc', '__getitem__', 0)
967 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
968 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
969 self.checkequal('a', 'abc', '__getitem__', slice(0, 1))
970 self.checkequal('', 'abc', '__getitem__', slice(0, 0))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000971
972 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
973
974 def test_slice(self):
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000975 self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
976 self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
977 self.checkequal('ab', 'abc', '__getitem__', slice(0, 2))
978 self.checkequal('bc', 'abc', '__getitem__', slice(1, 3))
979 self.checkequal('b', 'abc', '__getitem__', slice(1, 2))
980 self.checkequal('', 'abc', '__getitem__', slice(2, 2))
981 self.checkequal('', 'abc', '__getitem__', slice(1000, 1000))
982 self.checkequal('', 'abc', '__getitem__', slice(2000, 1000))
983 self.checkequal('', 'abc', '__getitem__', slice(2, 1))
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000984
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000985 self.checkraises(TypeError, 'abc', '__getitem__', 'def')
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000986
Thomas Woutersed03b412007-08-28 21:37:11 +0000987 def test_extended_getslice(self):
988 # Test extended slicing by comparing with list slicing.
989 s = string.ascii_letters + string.digits
990 indices = (0, None, 1, 3, 41, -1, -2, -37)
991 for start in indices:
992 for stop in indices:
993 # Skip step 0 (invalid)
994 for step in indices[1:]:
995 L = list(s)[start:stop:step]
996 self.checkequal("".join(L), s, '__getitem__',
997 slice(start, stop, step))
998
Walter Dörwald0fd583c2003-02-21 12:53:50 +0000999 def test_mul(self):
1000 self.checkequal('', 'abc', '__mul__', -1)
1001 self.checkequal('', 'abc', '__mul__', 0)
1002 self.checkequal('abc', 'abc', '__mul__', 1)
1003 self.checkequal('abcabcabc', 'abc', '__mul__', 3)
1004 self.checkraises(TypeError, 'abc', '__mul__')
1005 self.checkraises(TypeError, 'abc', '__mul__', '')
Martin v. Löwis18e16552006-02-15 17:27:45 +00001006 # XXX: on a 64-bit system, this doesn't raise an overflow error,
1007 # but either raises a MemoryError, or succeeds (if you have 54TiB)
1008 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001009
1010 def test_join(self):
1011 # join now works with any sequence type
1012 # moved here, because the argument order is
1013 # different in string.join (see the test in
1014 # test.test_string.StringTest.test_join)
1015 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])
1016 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001017 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))
1018 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001019 self.checkequal('w x y z', ' ', 'join', Sequence())
1020 self.checkequal('abc', 'a', 'join', ('abc',))
1021 self.checkequal('z', 'a', 'join', UserList(['z']))
Walter Dörwald67e83882007-05-05 12:26:27 +00001022 self.checkequal('a.b.c', '.', 'join', ['a', 'b', 'c'])
Guido van Rossum98297ee2007-11-06 21:34:58 +00001023 self.assertRaises(TypeError, '.'.join, ['a', 'b', 3])
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001024 for i in [5, 25, 125]:
1025 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1026 ['a' * i] * i)
1027 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',
1028 ('a' * i,) * i)
1029
Guido van Rossum98297ee2007-11-06 21:34:58 +00001030 #self.checkequal(str(BadSeq1()), ' ', 'join', BadSeq1())
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001031 self.checkequal('a b c', ' ', 'join', BadSeq2())
1032
1033 self.checkraises(TypeError, ' ', 'join')
1034 self.checkraises(TypeError, ' ', 'join', 7)
Guido van Rossumf1044292007-09-27 18:01:22 +00001035 self.checkraises(TypeError, ' ', 'join', [1, 2, bytes()])
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001036 try:
1037 def f():
1038 yield 4 + ""
1039 self.fixtype(' ').join(f())
Guido van Rossumb940e112007-01-10 16:19:56 +00001040 except TypeError as e:
Michael W. Hudsonb2308bb2005-10-21 11:45:01 +00001041 if '+' not in str(e):
1042 self.fail('join() ate exception message')
1043 else:
1044 self.fail('exception not raised')
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001045
1046 def test_formatting(self):
1047 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')
1048 self.checkequal('+10+', '+%d+', '__mod__', 10)
1049 self.checkequal('a', "%c", '__mod__', "a")
1050 self.checkequal('a', "%c", '__mod__', "a")
1051 self.checkequal('"', "%c", '__mod__', 34)
1052 self.checkequal('$', "%c", '__mod__', 36)
1053 self.checkequal('10', "%d", '__mod__', 10)
Walter Dörwald43440a62003-03-31 18:07:50 +00001054 self.checkequal('\x7f', "%c", '__mod__', 0x7f)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001055
1056 for ordinal in (-100, 0x200000):
1057 # unicode raises ValueError, str raises OverflowError
1058 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)
1059
1060 self.checkequal(' 42', '%3ld', '__mod__', 42)
1061 self.checkequal('0042.00', '%07.2f', '__mod__', 42)
Raymond Hettinger9bfe5332003-08-27 04:55:52 +00001062 self.checkequal('0042.00', '%07.2F', '__mod__', 42)
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001063
1064 self.checkraises(TypeError, 'abc', '__mod__')
1065 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)
1066 self.checkraises(TypeError, '%s%s', '__mod__', (42,))
1067 self.checkraises(TypeError, '%c', '__mod__', (None,))
1068 self.checkraises(ValueError, '%(foo', '__mod__', {})
1069 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))
1070
1071 # argument names with properly nested brackets are supported
1072 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})
1073
1074 # 100 is a magic number in PyUnicode_Format, this forces a resize
1075 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')
1076
1077 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))
1078 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))
1079 self.checkraises(ValueError, '%10', '__mod__', (42,))
1080
1081 def test_floatformatting(self):
1082 # float formatting
Guido van Rossum805365e2007-05-07 22:24:25 +00001083 for prec in range(100):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001084 format = '%%.%if' % prec
1085 value = 0.01
Guido van Rossum805365e2007-05-07 22:24:25 +00001086 for x in range(60):
Walter Dörwald0fd583c2003-02-21 12:53:50 +00001087 value = value * 3.141592655 / 3.0 * 10.0
1088 # The formatfloat() code in stringobject.c and
1089 # unicodeobject.c uses a 120 byte buffer and switches from
1090 # 'f' formatting to 'g' at precision 50, so we expect
1091 # OverflowErrors for the ranges x < 50 and prec >= 67.
1092 if x < 50 and prec >= 67:
1093 self.checkraises(OverflowError, format, "__mod__", value)
1094 else:
1095 self.checkcall(format, "__mod__", value)
1096
Thomas Wouters477c8d52006-05-27 19:21:47 +00001097 def test_inplace_rewrites(self):
1098 # Check that strings don't copy and modify cached single-character strings
1099 self.checkequal('a', 'A', 'lower')
1100 self.checkequal(True, 'A', 'isupper')
1101 self.checkequal('A', 'a', 'upper')
1102 self.checkequal(True, 'a', 'islower')
1103
1104 self.checkequal('a', 'A', 'replace', 'A', 'a')
1105 self.checkequal(True, 'A', 'isupper')
1106
1107 self.checkequal('A', 'a', 'capitalize')
1108 self.checkequal(True, 'a', 'islower')
1109
1110 self.checkequal('A', 'a', 'swapcase')
1111 self.checkequal(True, 'a', 'islower')
1112
1113 self.checkequal('A', 'a', 'title')
1114 self.checkequal(True, 'a', 'islower')
1115
1116 def test_partition(self):
1117
1118 self.checkequal(('this is the par', 'ti', 'tion method'),
1119 'this is the partition method', 'partition', 'ti')
1120
1121 # from raymond's original specification
1122 S = 'http://www.python.org'
1123 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')
1124 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')
1125 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')
1126 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')
1127
1128 self.checkraises(ValueError, S, 'partition', '')
1129 self.checkraises(TypeError, S, 'partition', None)
1130
1131 def test_rpartition(self):
1132
1133 self.checkequal(('this is the rparti', 'ti', 'on method'),
1134 'this is the rpartition method', 'rpartition', 'ti')
1135
1136 # from raymond's original specification
1137 S = 'http://www.python.org'
1138 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')
Thomas Wouters477c8d52006-05-27 19:21:47 +00001140 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')
1141 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')
1142
1143 self.checkraises(ValueError, S, 'rpartition', '')
1144 self.checkraises(TypeError, S, 'rpartition', None)
1145
Walter Dörwald57d88e52004-08-26 16:53:04 +00001146
Walter Dörwald57d88e52004-08-26 16:53:04 +00001147class MixinStrUnicodeTest:
Tim Peters108f1372004-08-27 05:36:07 +00001148 # Additional tests that only work with str and unicode.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001149
1150 def test_bug1001011(self):
1151 # Make sure join returns a NEW object for single item sequences
Tim Peters108f1372004-08-27 05:36:07 +00001152 # involving a subclass.
1153 # Make sure that it is of the appropriate type.
1154 # Check the optimisation still occurs for standard objects.
Walter Dörwald57d88e52004-08-26 16:53:04 +00001155 t = self.type2test
1156 class subclass(t):
1157 pass
1158 s1 = subclass("abcd")
1159 s2 = t().join([s1])
1160 self.assert_(s1 is not s2)
1161 self.assert_(type(s2) is t)
Tim Peters108f1372004-08-27 05:36:07 +00001162
1163 s1 = t("abcd")
1164 s2 = t().join([s1])
1165 self.assert_(s1 is s2)
1166
1167 # Should also test mixed-type join.
Guido van Rossumef87d6e2007-05-02 19:09:54 +00001168 if t is str:
Tim Peters108f1372004-08-27 05:36:07 +00001169 s1 = subclass("abcd")
1170 s2 = "".join([s1])
1171 self.assert_(s1 is not s2)
1172 self.assert_(type(s2) is t)
1173
1174 s1 = t("abcd")
1175 s2 = "".join([s1])
1176 self.assert_(s1 is s2)
1177
Guido van Rossum98297ee2007-11-06 21:34:58 +00001178## elif t is str8:
1179## s1 = subclass("abcd")
1180## s2 = "".join([s1])
1181## self.assert_(s1 is not s2)
1182## self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001183
Guido van Rossum98297ee2007-11-06 21:34:58 +00001184## s1 = t("abcd")
1185## s2 = "".join([s1])
1186## self.assert_(s1 is not s2)
1187## self.assert_(type(s2) is str) # promotes!
Tim Peters108f1372004-08-27 05:36:07 +00001188
1189 else:
1190 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)