]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/string_tests.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / string_tests.py
CommitLineData
4710c53d 1"""\r
2Common tests shared by test_str, test_unicode, test_userstring and test_string.\r
3"""\r
4\r
5import unittest, string, sys, struct\r
6from test import test_support\r
7from UserList import UserList\r
8\r
9class Sequence:\r
10 def __init__(self, seq='wxyz'): self.seq = seq\r
11 def __len__(self): return len(self.seq)\r
12 def __getitem__(self, i): return self.seq[i]\r
13\r
14class BadSeq1(Sequence):\r
15 def __init__(self): self.seq = [7, 'hello', 123L]\r
16\r
17class BadSeq2(Sequence):\r
18 def __init__(self): self.seq = ['a', 'b', 'c']\r
19 def __len__(self): return 8\r
20\r
21class CommonTest(unittest.TestCase):\r
22 # This testcase contains test that can be used in all\r
23 # stringlike classes. Currently this is str, unicode\r
24 # UserString and the string module.\r
25\r
26 # The type to be tested\r
27 # Change in subclasses to change the behaviour of fixtesttype()\r
28 type2test = None\r
29\r
30 # All tests pass their arguments to the testing methods\r
31 # as str objects. fixtesttype() can be used to propagate\r
32 # these arguments to the appropriate type\r
33 def fixtype(self, obj):\r
34 if isinstance(obj, str):\r
35 return self.__class__.type2test(obj)\r
36 elif isinstance(obj, list):\r
37 return [self.fixtype(x) for x in obj]\r
38 elif isinstance(obj, tuple):\r
39 return tuple([self.fixtype(x) for x in obj])\r
40 elif isinstance(obj, dict):\r
41 return dict([\r
42 (self.fixtype(key), self.fixtype(value))\r
43 for (key, value) in obj.iteritems()\r
44 ])\r
45 else:\r
46 return obj\r
47\r
48 # check that object.method(*args) returns result\r
49 def checkequal(self, result, object, methodname, *args):\r
50 result = self.fixtype(result)\r
51 object = self.fixtype(object)\r
52 args = self.fixtype(args)\r
53 realresult = getattr(object, methodname)(*args)\r
54 self.assertEqual(\r
55 result,\r
56 realresult\r
57 )\r
58 # if the original is returned make sure that\r
59 # this doesn't happen with subclasses\r
60 if object == realresult:\r
61 class subtype(self.__class__.type2test):\r
62 pass\r
63 object = subtype(object)\r
64 realresult = getattr(object, methodname)(*args)\r
65 self.assertTrue(object is not realresult)\r
66\r
67 # check that object.method(*args) raises exc\r
68 def checkraises(self, exc, object, methodname, *args):\r
69 object = self.fixtype(object)\r
70 args = self.fixtype(args)\r
71 self.assertRaises(\r
72 exc,\r
73 getattr(object, methodname),\r
74 *args\r
75 )\r
76\r
77 # call object.method(*args) without any checks\r
78 def checkcall(self, object, methodname, *args):\r
79 object = self.fixtype(object)\r
80 args = self.fixtype(args)\r
81 getattr(object, methodname)(*args)\r
82\r
83 def test_hash(self):\r
84 # SF bug 1054139: += optimization was not invalidating cached hash value\r
85 a = self.type2test('DNSSEC')\r
86 b = self.type2test('')\r
87 for c in a:\r
88 b += c\r
89 hash(b)\r
90 self.assertEqual(hash(a), hash(b))\r
91\r
92 def test_capitalize(self):\r
93 self.checkequal(' hello ', ' hello ', 'capitalize')\r
94 self.checkequal('Hello ', 'Hello ','capitalize')\r
95 self.checkequal('Hello ', 'hello ','capitalize')\r
96 self.checkequal('Aaaa', 'aaaa', 'capitalize')\r
97 self.checkequal('Aaaa', 'AaAa', 'capitalize')\r
98\r
99 self.checkraises(TypeError, 'hello', 'capitalize', 42)\r
100\r
101 def test_count(self):\r
102 self.checkequal(3, 'aaa', 'count', 'a')\r
103 self.checkequal(0, 'aaa', 'count', 'b')\r
104 self.checkequal(3, 'aaa', 'count', 'a')\r
105 self.checkequal(0, 'aaa', 'count', 'b')\r
106 self.checkequal(3, 'aaa', 'count', 'a')\r
107 self.checkequal(0, 'aaa', 'count', 'b')\r
108 self.checkequal(0, 'aaa', 'count', 'b')\r
109 self.checkequal(2, 'aaa', 'count', 'a', 1)\r
110 self.checkequal(0, 'aaa', 'count', 'a', 10)\r
111 self.checkequal(1, 'aaa', 'count', 'a', -1)\r
112 self.checkequal(3, 'aaa', 'count', 'a', -10)\r
113 self.checkequal(1, 'aaa', 'count', 'a', 0, 1)\r
114 self.checkequal(3, 'aaa', 'count', 'a', 0, 10)\r
115 self.checkequal(2, 'aaa', 'count', 'a', 0, -1)\r
116 self.checkequal(0, 'aaa', 'count', 'a', 0, -10)\r
117 self.checkequal(3, 'aaa', 'count', '', 1)\r
118 self.checkequal(1, 'aaa', 'count', '', 3)\r
119 self.checkequal(0, 'aaa', 'count', '', 10)\r
120 self.checkequal(2, 'aaa', 'count', '', -1)\r
121 self.checkequal(4, 'aaa', 'count', '', -10)\r
122\r
123 self.checkequal(1, '', 'count', '')\r
124 self.checkequal(0, '', 'count', '', 1, 1)\r
125 self.checkequal(0, '', 'count', '', sys.maxint, 0)\r
126\r
127 self.checkequal(0, '', 'count', 'xx')\r
128 self.checkequal(0, '', 'count', 'xx', 1, 1)\r
129 self.checkequal(0, '', 'count', 'xx', sys.maxint, 0)\r
130\r
131 self.checkraises(TypeError, 'hello', 'count')\r
132 self.checkraises(TypeError, 'hello', 'count', 42)\r
133\r
134 # For a variety of combinations,\r
135 # verify that str.count() matches an equivalent function\r
136 # replacing all occurrences and then differencing the string lengths\r
137 charset = ['', 'a', 'b']\r
138 digits = 7\r
139 base = len(charset)\r
140 teststrings = set()\r
141 for i in xrange(base ** digits):\r
142 entry = []\r
143 for j in xrange(digits):\r
144 i, m = divmod(i, base)\r
145 entry.append(charset[m])\r
146 teststrings.add(''.join(entry))\r
147 teststrings = list(teststrings)\r
148 for i in teststrings:\r
149 i = self.fixtype(i)\r
150 n = len(i)\r
151 for j in teststrings:\r
152 r1 = i.count(j)\r
153 if j:\r
154 r2, rem = divmod(n - len(i.replace(j, '')), len(j))\r
155 else:\r
156 r2, rem = len(i)+1, 0\r
157 if rem or r1 != r2:\r
158 self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))\r
159 self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))\r
160\r
161 def test_find(self):\r
162 self.checkequal(0, 'abcdefghiabc', 'find', 'abc')\r
163 self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)\r
164 self.checkequal(-1, 'abcdefghiabc', 'find', 'def', 4)\r
165\r
166 self.checkequal(0, 'abc', 'find', '', 0)\r
167 self.checkequal(3, 'abc', 'find', '', 3)\r
168 self.checkequal(-1, 'abc', 'find', '', 4)\r
169\r
170 # to check the ability to pass None as defaults\r
171 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a')\r
172 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4)\r
173 self.checkequal(-1, 'rrarrrrrrrrra', 'find', 'a', 4, 6)\r
174 self.checkequal(12, 'rrarrrrrrrrra', 'find', 'a', 4, None)\r
175 self.checkequal( 2, 'rrarrrrrrrrra', 'find', 'a', None, 6)\r
176\r
177 self.checkraises(TypeError, 'hello', 'find')\r
178 self.checkraises(TypeError, 'hello', 'find', 42)\r
179\r
180 self.checkequal(0, '', 'find', '')\r
181 self.checkequal(-1, '', 'find', '', 1, 1)\r
182 self.checkequal(-1, '', 'find', '', sys.maxint, 0)\r
183\r
184 self.checkequal(-1, '', 'find', 'xx')\r
185 self.checkequal(-1, '', 'find', 'xx', 1, 1)\r
186 self.checkequal(-1, '', 'find', 'xx', sys.maxint, 0)\r
187\r
188 # issue 7458\r
189 self.checkequal(-1, 'ab', 'find', 'xxx', sys.maxsize + 1, 0)\r
190\r
191 # For a variety of combinations,\r
192 # verify that str.find() matches __contains__\r
193 # and that the found substring is really at that location\r
194 charset = ['', 'a', 'b', 'c']\r
195 digits = 5\r
196 base = len(charset)\r
197 teststrings = set()\r
198 for i in xrange(base ** digits):\r
199 entry = []\r
200 for j in xrange(digits):\r
201 i, m = divmod(i, base)\r
202 entry.append(charset[m])\r
203 teststrings.add(''.join(entry))\r
204 teststrings = list(teststrings)\r
205 for i in teststrings:\r
206 i = self.fixtype(i)\r
207 for j in teststrings:\r
208 loc = i.find(j)\r
209 r1 = (loc != -1)\r
210 r2 = j in i\r
211 self.assertEqual(r1, r2)\r
212 if loc != -1:\r
213 self.assertEqual(i[loc:loc+len(j)], j)\r
214\r
215 def test_rfind(self):\r
216 self.checkequal(9, 'abcdefghiabc', 'rfind', 'abc')\r
217 self.checkequal(12, 'abcdefghiabc', 'rfind', '')\r
218 self.checkequal(0, 'abcdefghiabc', 'rfind', 'abcd')\r
219 self.checkequal(-1, 'abcdefghiabc', 'rfind', 'abcz')\r
220\r
221 self.checkequal(3, 'abc', 'rfind', '', 0)\r
222 self.checkequal(3, 'abc', 'rfind', '', 3)\r
223 self.checkequal(-1, 'abc', 'rfind', '', 4)\r
224\r
225 # to check the ability to pass None as defaults\r
226 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a')\r
227 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4)\r
228 self.checkequal(-1, 'rrarrrrrrrrra', 'rfind', 'a', 4, 6)\r
229 self.checkequal(12, 'rrarrrrrrrrra', 'rfind', 'a', 4, None)\r
230 self.checkequal( 2, 'rrarrrrrrrrra', 'rfind', 'a', None, 6)\r
231\r
232 self.checkraises(TypeError, 'hello', 'rfind')\r
233 self.checkraises(TypeError, 'hello', 'rfind', 42)\r
234\r
235 # For a variety of combinations,\r
236 # verify that str.rfind() matches __contains__\r
237 # and that the found substring is really at that location\r
238 charset = ['', 'a', 'b', 'c']\r
239 digits = 5\r
240 base = len(charset)\r
241 teststrings = set()\r
242 for i in xrange(base ** digits):\r
243 entry = []\r
244 for j in xrange(digits):\r
245 i, m = divmod(i, base)\r
246 entry.append(charset[m])\r
247 teststrings.add(''.join(entry))\r
248 teststrings = list(teststrings)\r
249 for i in teststrings:\r
250 i = self.fixtype(i)\r
251 for j in teststrings:\r
252 loc = i.rfind(j)\r
253 r1 = (loc != -1)\r
254 r2 = j in i\r
255 self.assertEqual(r1, r2)\r
256 if loc != -1:\r
257 self.assertEqual(i[loc:loc+len(j)], self.fixtype(j))\r
258\r
259 # issue 7458\r
260 self.checkequal(-1, 'ab', 'rfind', 'xxx', sys.maxsize + 1, 0)\r
261\r
262 def test_index(self):\r
263 self.checkequal(0, 'abcdefghiabc', 'index', '')\r
264 self.checkequal(3, 'abcdefghiabc', 'index', 'def')\r
265 self.checkequal(0, 'abcdefghiabc', 'index', 'abc')\r
266 self.checkequal(9, 'abcdefghiabc', 'index', 'abc', 1)\r
267\r
268 self.checkraises(ValueError, 'abcdefghiabc', 'index', 'hib')\r
269 self.checkraises(ValueError, 'abcdefghiab', 'index', 'abc', 1)\r
270 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', 8)\r
271 self.checkraises(ValueError, 'abcdefghi', 'index', 'ghi', -1)\r
272\r
273 # to check the ability to pass None as defaults\r
274 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a')\r
275 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4)\r
276 self.checkraises(ValueError, 'rrarrrrrrrrra', 'index', 'a', 4, 6)\r
277 self.checkequal(12, 'rrarrrrrrrrra', 'index', 'a', 4, None)\r
278 self.checkequal( 2, 'rrarrrrrrrrra', 'index', 'a', None, 6)\r
279\r
280 self.checkraises(TypeError, 'hello', 'index')\r
281 self.checkraises(TypeError, 'hello', 'index', 42)\r
282\r
283 def test_rindex(self):\r
284 self.checkequal(12, 'abcdefghiabc', 'rindex', '')\r
285 self.checkequal(3, 'abcdefghiabc', 'rindex', 'def')\r
286 self.checkequal(9, 'abcdefghiabc', 'rindex', 'abc')\r
287 self.checkequal(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)\r
288\r
289 self.checkraises(ValueError, 'abcdefghiabc', 'rindex', 'hib')\r
290 self.checkraises(ValueError, 'defghiabc', 'rindex', 'def', 1)\r
291 self.checkraises(ValueError, 'defghiabc', 'rindex', 'abc', 0, -1)\r
292 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, 8)\r
293 self.checkraises(ValueError, 'abcdefghi', 'rindex', 'ghi', 0, -1)\r
294\r
295 # to check the ability to pass None as defaults\r
296 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a')\r
297 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4)\r
298 self.checkraises(ValueError, 'rrarrrrrrrrra', 'rindex', 'a', 4, 6)\r
299 self.checkequal(12, 'rrarrrrrrrrra', 'rindex', 'a', 4, None)\r
300 self.checkequal( 2, 'rrarrrrrrrrra', 'rindex', 'a', None, 6)\r
301\r
302 self.checkraises(TypeError, 'hello', 'rindex')\r
303 self.checkraises(TypeError, 'hello', 'rindex', 42)\r
304\r
305 def test_lower(self):\r
306 self.checkequal('hello', 'HeLLo', 'lower')\r
307 self.checkequal('hello', 'hello', 'lower')\r
308 self.checkraises(TypeError, 'hello', 'lower', 42)\r
309\r
310 def test_upper(self):\r
311 self.checkequal('HELLO', 'HeLLo', 'upper')\r
312 self.checkequal('HELLO', 'HELLO', 'upper')\r
313 self.checkraises(TypeError, 'hello', 'upper', 42)\r
314\r
315 def test_expandtabs(self):\r
316 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')\r
317 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)\r
318 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 4)\r
319 self.checkequal('abc\r\nab def\ng hi', 'abc\r\nab\tdef\ng\thi', 'expandtabs', 4)\r
320 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs')\r
321 self.checkequal('abc\rab def\ng hi', 'abc\rab\tdef\ng\thi', 'expandtabs', 8)\r
322 self.checkequal('abc\r\nab\r\ndef\ng\r\nhi', 'abc\r\nab\r\ndef\ng\r\nhi', 'expandtabs', 4)\r
323 self.checkequal(' a\n b', ' \ta\n\tb', 'expandtabs', 1)\r
324\r
325 self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)\r
326 # This test is only valid when sizeof(int) == sizeof(void*) == 4.\r
327 if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:\r
328 self.checkraises(OverflowError,\r
329 '\ta\n\tb', 'expandtabs', sys.maxint)\r
330\r
331 def test_split(self):\r
332 self.checkequal(['this', 'is', 'the', 'split', 'function'],\r
333 'this is the split function', 'split')\r
334\r
335 # by whitespace\r
336 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'split')\r
337 self.checkequal(['a', 'b c d'], 'a b c d', 'split', None, 1)\r
338 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)\r
339 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 3)\r
340 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None, 4)\r
341 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'split', None,\r
342 sys.maxint-1)\r
343 self.checkequal(['a b c d'], 'a b c d', 'split', None, 0)\r
344 self.checkequal(['a b c d'], ' a b c d', 'split', None, 0)\r
345 self.checkequal(['a', 'b', 'c d'], 'a b c d', 'split', None, 2)\r
346\r
347 self.checkequal([], ' ', 'split')\r
348 self.checkequal(['a'], ' a ', 'split')\r
349 self.checkequal(['a', 'b'], ' a b ', 'split')\r
350 self.checkequal(['a', 'b '], ' a b ', 'split', None, 1)\r
351 self.checkequal(['a', 'b c '], ' a b c ', 'split', None, 1)\r
352 self.checkequal(['a', 'b', 'c '], ' a b c ', 'split', None, 2)\r
353 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'split')\r
354 aaa = ' a '*20\r
355 self.checkequal(['a']*20, aaa, 'split')\r
356 self.checkequal(['a'] + [aaa[4:]], aaa, 'split', None, 1)\r
357 self.checkequal(['a']*19 + ['a '], aaa, 'split', None, 19)\r
358\r
359 # by a char\r
360 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|')\r
361 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)\r
362 self.checkequal(['a', 'b|c|d'], 'a|b|c|d', 'split', '|', 1)\r
363 self.checkequal(['a', 'b', 'c|d'], 'a|b|c|d', 'split', '|', 2)\r
364 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 3)\r
365 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|', 4)\r
366 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'split', '|',\r
367 sys.maxint-2)\r
368 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'split', '|', 0)\r
369 self.checkequal(['a', '', 'b||c||d'], 'a||b||c||d', 'split', '|', 2)\r
370 self.checkequal(['endcase ', ''], 'endcase |', 'split', '|')\r
371 self.checkequal(['', ' startcase'], '| startcase', 'split', '|')\r
372 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'split', '|')\r
373 self.checkequal(['a', '', 'b\x00c\x00d'], 'a\x00\x00b\x00c\x00d', 'split', '\x00', 2)\r
374\r
375 self.checkequal(['a']*20, ('a|'*20)[:-1], 'split', '|')\r
376 self.checkequal(['a']*15 +['a|a|a|a|a'],\r
377 ('a|'*20)[:-1], 'split', '|', 15)\r
378\r
379 # by string\r
380 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//')\r
381 self.checkequal(['a', 'b//c//d'], 'a//b//c//d', 'split', '//', 1)\r
382 self.checkequal(['a', 'b', 'c//d'], 'a//b//c//d', 'split', '//', 2)\r
383 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 3)\r
384 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//', 4)\r
385 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'split', '//',\r
386 sys.maxint-10)\r
387 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'split', '//', 0)\r
388 self.checkequal(['a', '', 'b////c////d'], 'a////b////c////d', 'split', '//', 2)\r
389 self.checkequal(['endcase ', ''], 'endcase test', 'split', 'test')\r
390 self.checkequal(['', ' begincase'], 'test begincase', 'split', 'test')\r
391 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',\r
392 'split', 'test')\r
393 self.checkequal(['a', 'bc'], 'abbbc', 'split', 'bb')\r
394 self.checkequal(['', ''], 'aaa', 'split', 'aaa')\r
395 self.checkequal(['aaa'], 'aaa', 'split', 'aaa', 0)\r
396 self.checkequal(['ab', 'ab'], 'abbaab', 'split', 'ba')\r
397 self.checkequal(['aaaa'], 'aaaa', 'split', 'aab')\r
398 self.checkequal([''], '', 'split', 'aaa')\r
399 self.checkequal(['aa'], 'aa', 'split', 'aaa')\r
400 self.checkequal(['A', 'bobb'], 'Abbobbbobb', 'split', 'bbobb')\r
401 self.checkequal(['A', 'B', ''], 'AbbobbBbbobb', 'split', 'bbobb')\r
402\r
403 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH')\r
404 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'split', 'BLAH', 19)\r
405 self.checkequal(['a']*18 + ['aBLAHa'], ('aBLAH'*20)[:-4],\r
406 'split', 'BLAH', 18)\r
407\r
408 # mixed use of str and unicode\r
409 self.checkequal([u'a', u'b', u'c d'], 'a b c d', 'split', u' ', 2)\r
410\r
411 # argument type\r
412 self.checkraises(TypeError, 'hello', 'split', 42, 42, 42)\r
413\r
414 # null case\r
415 self.checkraises(ValueError, 'hello', 'split', '')\r
416 self.checkraises(ValueError, 'hello', 'split', '', 0)\r
417\r
418 def test_rsplit(self):\r
419 self.checkequal(['this', 'is', 'the', 'rsplit', 'function'],\r
420 'this is the rsplit function', 'rsplit')\r
421\r
422 # by whitespace\r
423 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d ', 'rsplit')\r
424 self.checkequal(['a b c', 'd'], 'a b c d', 'rsplit', None, 1)\r
425 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)\r
426 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 3)\r
427 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None, 4)\r
428 self.checkequal(['a', 'b', 'c', 'd'], 'a b c d', 'rsplit', None,\r
429 sys.maxint-20)\r
430 self.checkequal(['a b c d'], 'a b c d', 'rsplit', None, 0)\r
431 self.checkequal(['a b c d'], 'a b c d ', 'rsplit', None, 0)\r
432 self.checkequal(['a b', 'c', 'd'], 'a b c d', 'rsplit', None, 2)\r
433\r
434 self.checkequal([], ' ', 'rsplit')\r
435 self.checkequal(['a'], ' a ', 'rsplit')\r
436 self.checkequal(['a', 'b'], ' a b ', 'rsplit')\r
437 self.checkequal([' a', 'b'], ' a b ', 'rsplit', None, 1)\r
438 self.checkequal([' a b','c'], ' a b c ', 'rsplit',\r
439 None, 1)\r
440 self.checkequal([' a', 'b', 'c'], ' a b c ', 'rsplit',\r
441 None, 2)\r
442 self.checkequal(['a', 'b'], '\n\ta \t\r b \v ', 'rsplit', None, 88)\r
443 aaa = ' a '*20\r
444 self.checkequal(['a']*20, aaa, 'rsplit')\r
445 self.checkequal([aaa[:-4]] + ['a'], aaa, 'rsplit', None, 1)\r
446 self.checkequal([' a a'] + ['a']*18, aaa, 'rsplit', None, 18)\r
447\r
448\r
449 # by a char\r
450 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|')\r
451 self.checkequal(['a|b|c', 'd'], 'a|b|c|d', 'rsplit', '|', 1)\r
452 self.checkequal(['a|b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 2)\r
453 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 3)\r
454 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|', 4)\r
455 self.checkequal(['a', 'b', 'c', 'd'], 'a|b|c|d', 'rsplit', '|',\r
456 sys.maxint-100)\r
457 self.checkequal(['a|b|c|d'], 'a|b|c|d', 'rsplit', '|', 0)\r
458 self.checkequal(['a||b||c', '', 'd'], 'a||b||c||d', 'rsplit', '|', 2)\r
459 self.checkequal(['', ' begincase'], '| begincase', 'rsplit', '|')\r
460 self.checkequal(['endcase ', ''], 'endcase |', 'rsplit', '|')\r
461 self.checkequal(['', 'bothcase', ''], '|bothcase|', 'rsplit', '|')\r
462\r
463 self.checkequal(['a\x00\x00b', 'c', 'd'], 'a\x00\x00b\x00c\x00d', 'rsplit', '\x00', 2)\r
464\r
465 self.checkequal(['a']*20, ('a|'*20)[:-1], 'rsplit', '|')\r
466 self.checkequal(['a|a|a|a|a']+['a']*15,\r
467 ('a|'*20)[:-1], 'rsplit', '|', 15)\r
468\r
469 # by string\r
470 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//')\r
471 self.checkequal(['a//b//c', 'd'], 'a//b//c//d', 'rsplit', '//', 1)\r
472 self.checkequal(['a//b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 2)\r
473 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 3)\r
474 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//', 4)\r
475 self.checkequal(['a', 'b', 'c', 'd'], 'a//b//c//d', 'rsplit', '//',\r
476 sys.maxint-5)\r
477 self.checkequal(['a//b//c//d'], 'a//b//c//d', 'rsplit', '//', 0)\r
478 self.checkequal(['a////b////c', '', 'd'], 'a////b////c////d', 'rsplit', '//', 2)\r
479 self.checkequal(['', ' begincase'], 'test begincase', 'rsplit', 'test')\r
480 self.checkequal(['endcase ', ''], 'endcase test', 'rsplit', 'test')\r
481 self.checkequal(['', ' bothcase ', ''], 'test bothcase test',\r
482 'rsplit', 'test')\r
483 self.checkequal(['ab', 'c'], 'abbbc', 'rsplit', 'bb')\r
484 self.checkequal(['', ''], 'aaa', 'rsplit', 'aaa')\r
485 self.checkequal(['aaa'], 'aaa', 'rsplit', 'aaa', 0)\r
486 self.checkequal(['ab', 'ab'], 'abbaab', 'rsplit', 'ba')\r
487 self.checkequal(['aaaa'], 'aaaa', 'rsplit', 'aab')\r
488 self.checkequal([''], '', 'rsplit', 'aaa')\r
489 self.checkequal(['aa'], 'aa', 'rsplit', 'aaa')\r
490 self.checkequal(['bbob', 'A'], 'bbobbbobbA', 'rsplit', 'bbobb')\r
491 self.checkequal(['', 'B', 'A'], 'bbobbBbbobbA', 'rsplit', 'bbobb')\r
492\r
493 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH')\r
494 self.checkequal(['a']*20, ('aBLAH'*20)[:-4], 'rsplit', 'BLAH', 19)\r
495 self.checkequal(['aBLAHa'] + ['a']*18, ('aBLAH'*20)[:-4],\r
496 'rsplit', 'BLAH', 18)\r
497\r
498 # mixed use of str and unicode\r
499 self.checkequal([u'a b', u'c', u'd'], 'a b c d', 'rsplit', u' ', 2)\r
500\r
501 # argument type\r
502 self.checkraises(TypeError, 'hello', 'rsplit', 42, 42, 42)\r
503\r
504 # null case\r
505 self.checkraises(ValueError, 'hello', 'rsplit', '')\r
506 self.checkraises(ValueError, 'hello', 'rsplit', '', 0)\r
507\r
508 def test_strip(self):\r
509 self.checkequal('hello', ' hello ', 'strip')\r
510 self.checkequal('hello ', ' hello ', 'lstrip')\r
511 self.checkequal(' hello', ' hello ', 'rstrip')\r
512 self.checkequal('hello', 'hello', 'strip')\r
513\r
514 # strip/lstrip/rstrip with None arg\r
515 self.checkequal('hello', ' hello ', 'strip', None)\r
516 self.checkequal('hello ', ' hello ', 'lstrip', None)\r
517 self.checkequal(' hello', ' hello ', 'rstrip', None)\r
518 self.checkequal('hello', 'hello', 'strip', None)\r
519\r
520 # strip/lstrip/rstrip with str arg\r
521 self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')\r
522 self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')\r
523 self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')\r
524 self.checkequal('hello', 'hello', 'strip', 'xyz')\r
525\r
526 # strip/lstrip/rstrip with unicode arg\r
527 if test_support.have_unicode:\r
528 self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',\r
529 'strip', unicode('xyz', 'ascii'))\r
530 self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',\r
531 'lstrip', unicode('xyz', 'ascii'))\r
532 self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',\r
533 'rstrip', unicode('xyz', 'ascii'))\r
534 # XXX\r
535 #self.checkequal(unicode('hello', 'ascii'), 'hello',\r
536 # 'strip', unicode('xyz', 'ascii'))\r
537\r
538 self.checkraises(TypeError, 'hello', 'strip', 42, 42)\r
539 self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)\r
540 self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)\r
541\r
542 def test_ljust(self):\r
543 self.checkequal('abc ', 'abc', 'ljust', 10)\r
544 self.checkequal('abc ', 'abc', 'ljust', 6)\r
545 self.checkequal('abc', 'abc', 'ljust', 3)\r
546 self.checkequal('abc', 'abc', 'ljust', 2)\r
547 self.checkequal('abc*******', 'abc', 'ljust', 10, '*')\r
548 self.checkraises(TypeError, 'abc', 'ljust')\r
549\r
550 def test_rjust(self):\r
551 self.checkequal(' abc', 'abc', 'rjust', 10)\r
552 self.checkequal(' abc', 'abc', 'rjust', 6)\r
553 self.checkequal('abc', 'abc', 'rjust', 3)\r
554 self.checkequal('abc', 'abc', 'rjust', 2)\r
555 self.checkequal('*******abc', 'abc', 'rjust', 10, '*')\r
556 self.checkraises(TypeError, 'abc', 'rjust')\r
557\r
558 def test_center(self):\r
559 self.checkequal(' abc ', 'abc', 'center', 10)\r
560 self.checkequal(' abc ', 'abc', 'center', 6)\r
561 self.checkequal('abc', 'abc', 'center', 3)\r
562 self.checkequal('abc', 'abc', 'center', 2)\r
563 self.checkequal('***abc****', 'abc', 'center', 10, '*')\r
564 self.checkraises(TypeError, 'abc', 'center')\r
565\r
566 def test_swapcase(self):\r
567 self.checkequal('hEllO CoMPuTErS', 'HeLLo cOmpUteRs', 'swapcase')\r
568\r
569 self.checkraises(TypeError, 'hello', 'swapcase', 42)\r
570\r
571 def test_replace(self):\r
572 EQ = self.checkequal\r
573\r
574 # Operations on the empty string\r
575 EQ("", "", "replace", "", "")\r
576 EQ("A", "", "replace", "", "A")\r
577 EQ("", "", "replace", "A", "")\r
578 EQ("", "", "replace", "A", "A")\r
579 EQ("", "", "replace", "", "", 100)\r
580 EQ("", "", "replace", "", "", sys.maxint)\r
581\r
582 # interleave (from=="", 'to' gets inserted everywhere)\r
583 EQ("A", "A", "replace", "", "")\r
584 EQ("*A*", "A", "replace", "", "*")\r
585 EQ("*1A*1", "A", "replace", "", "*1")\r
586 EQ("*-#A*-#", "A", "replace", "", "*-#")\r
587 EQ("*-A*-A*-", "AA", "replace", "", "*-")\r
588 EQ("*-A*-A*-", "AA", "replace", "", "*-", -1)\r
589 EQ("*-A*-A*-", "AA", "replace", "", "*-", sys.maxint)\r
590 EQ("*-A*-A*-", "AA", "replace", "", "*-", 4)\r
591 EQ("*-A*-A*-", "AA", "replace", "", "*-", 3)\r
592 EQ("*-A*-A", "AA", "replace", "", "*-", 2)\r
593 EQ("*-AA", "AA", "replace", "", "*-", 1)\r
594 EQ("AA", "AA", "replace", "", "*-", 0)\r
595\r
596 # single character deletion (from=="A", to=="")\r
597 EQ("", "A", "replace", "A", "")\r
598 EQ("", "AAA", "replace", "A", "")\r
599 EQ("", "AAA", "replace", "A", "", -1)\r
600 EQ("", "AAA", "replace", "A", "", sys.maxint)\r
601 EQ("", "AAA", "replace", "A", "", 4)\r
602 EQ("", "AAA", "replace", "A", "", 3)\r
603 EQ("A", "AAA", "replace", "A", "", 2)\r
604 EQ("AA", "AAA", "replace", "A", "", 1)\r
605 EQ("AAA", "AAA", "replace", "A", "", 0)\r
606 EQ("", "AAAAAAAAAA", "replace", "A", "")\r
607 EQ("BCD", "ABACADA", "replace", "A", "")\r
608 EQ("BCD", "ABACADA", "replace", "A", "", -1)\r
609 EQ("BCD", "ABACADA", "replace", "A", "", sys.maxint)\r
610 EQ("BCD", "ABACADA", "replace", "A", "", 5)\r
611 EQ("BCD", "ABACADA", "replace", "A", "", 4)\r
612 EQ("BCDA", "ABACADA", "replace", "A", "", 3)\r
613 EQ("BCADA", "ABACADA", "replace", "A", "", 2)\r
614 EQ("BACADA", "ABACADA", "replace", "A", "", 1)\r
615 EQ("ABACADA", "ABACADA", "replace", "A", "", 0)\r
616 EQ("BCD", "ABCAD", "replace", "A", "")\r
617 EQ("BCD", "ABCADAA", "replace", "A", "")\r
618 EQ("BCD", "BCD", "replace", "A", "")\r
619 EQ("*************", "*************", "replace", "A", "")\r
620 EQ("^A^", "^"+"A"*1000+"^", "replace", "A", "", 999)\r
621\r
622 # substring deletion (from=="the", to=="")\r
623 EQ("", "the", "replace", "the", "")\r
624 EQ("ater", "theater", "replace", "the", "")\r
625 EQ("", "thethe", "replace", "the", "")\r
626 EQ("", "thethethethe", "replace", "the", "")\r
627 EQ("aaaa", "theatheatheathea", "replace", "the", "")\r
628 EQ("that", "that", "replace", "the", "")\r
629 EQ("thaet", "thaet", "replace", "the", "")\r
630 EQ("here and re", "here and there", "replace", "the", "")\r
631 EQ("here and re and re", "here and there and there",\r
632 "replace", "the", "", sys.maxint)\r
633 EQ("here and re and re", "here and there and there",\r
634 "replace", "the", "", -1)\r
635 EQ("here and re and re", "here and there and there",\r
636 "replace", "the", "", 3)\r
637 EQ("here and re and re", "here and there and there",\r
638 "replace", "the", "", 2)\r
639 EQ("here and re and there", "here and there and there",\r
640 "replace", "the", "", 1)\r
641 EQ("here and there and there", "here and there and there",\r
642 "replace", "the", "", 0)\r
643 EQ("here and re and re", "here and there and there", "replace", "the", "")\r
644\r
645 EQ("abc", "abc", "replace", "the", "")\r
646 EQ("abcdefg", "abcdefg", "replace", "the", "")\r
647\r
648 # substring deletion (from=="bob", to=="")\r
649 EQ("bob", "bbobob", "replace", "bob", "")\r
650 EQ("bobXbob", "bbobobXbbobob", "replace", "bob", "")\r
651 EQ("aaaaaaa", "aaaaaaabob", "replace", "bob", "")\r
652 EQ("aaaaaaa", "aaaaaaa", "replace", "bob", "")\r
653\r
654 # single character replace in place (len(from)==len(to)==1)\r
655 EQ("Who goes there?", "Who goes there?", "replace", "o", "o")\r
656 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O")\r
657 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", sys.maxint)\r
658 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", -1)\r
659 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 3)\r
660 EQ("WhO gOes there?", "Who goes there?", "replace", "o", "O", 2)\r
661 EQ("WhO goes there?", "Who goes there?", "replace", "o", "O", 1)\r
662 EQ("Who goes there?", "Who goes there?", "replace", "o", "O", 0)\r
663\r
664 EQ("Who goes there?", "Who goes there?", "replace", "a", "q")\r
665 EQ("who goes there?", "Who goes there?", "replace", "W", "w")\r
666 EQ("wwho goes there?ww", "WWho goes there?WW", "replace", "W", "w")\r
667 EQ("Who goes there!", "Who goes there?", "replace", "?", "!")\r
668 EQ("Who goes there!!", "Who goes there??", "replace", "?", "!")\r
669\r
670 EQ("Who goes there?", "Who goes there?", "replace", ".", "!")\r
671\r
672 # substring replace in place (len(from)==len(to) > 1)\r
673 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**")\r
674 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", sys.maxint)\r
675 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", -1)\r
676 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 4)\r
677 EQ("Th** ** a t**sue", "This is a tissue", "replace", "is", "**", 3)\r
678 EQ("Th** ** a tissue", "This is a tissue", "replace", "is", "**", 2)\r
679 EQ("Th** is a tissue", "This is a tissue", "replace", "is", "**", 1)\r
680 EQ("This is a tissue", "This is a tissue", "replace", "is", "**", 0)\r
681 EQ("cobob", "bobob", "replace", "bob", "cob")\r
682 EQ("cobobXcobocob", "bobobXbobobob", "replace", "bob", "cob")\r
683 EQ("bobob", "bobob", "replace", "bot", "bot")\r
684\r
685 # replace single character (len(from)==1, len(to)>1)\r
686 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK")\r
687 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", -1)\r
688 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", sys.maxint)\r
689 EQ("ReyKKjaviKK", "Reykjavik", "replace", "k", "KK", 2)\r
690 EQ("ReyKKjavik", "Reykjavik", "replace", "k", "KK", 1)\r
691 EQ("Reykjavik", "Reykjavik", "replace", "k", "KK", 0)\r
692 EQ("A----B----C----", "A.B.C.", "replace", ".", "----")\r
693\r
694 EQ("Reykjavik", "Reykjavik", "replace", "q", "KK")\r
695\r
696 # replace substring (len(from)>1, len(to)!=len(from))\r
697 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",\r
698 "replace", "spam", "ham")\r
699 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",\r
700 "replace", "spam", "ham", sys.maxint)\r
701 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",\r
702 "replace", "spam", "ham", -1)\r
703 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",\r
704 "replace", "spam", "ham", 4)\r
705 EQ("ham, ham, eggs and ham", "spam, spam, eggs and spam",\r
706 "replace", "spam", "ham", 3)\r
707 EQ("ham, ham, eggs and spam", "spam, spam, eggs and spam",\r
708 "replace", "spam", "ham", 2)\r
709 EQ("ham, spam, eggs and spam", "spam, spam, eggs and spam",\r
710 "replace", "spam", "ham", 1)\r
711 EQ("spam, spam, eggs and spam", "spam, spam, eggs and spam",\r
712 "replace", "spam", "ham", 0)\r
713\r
714 EQ("bobob", "bobobob", "replace", "bobob", "bob")\r
715 EQ("bobobXbobob", "bobobobXbobobob", "replace", "bobob", "bob")\r
716 EQ("BOBOBOB", "BOBOBOB", "replace", "bob", "bobby")\r
717\r
718 with test_support.check_py3k_warnings():\r
719 ba = buffer('a')\r
720 bb = buffer('b')\r
721 EQ("bbc", "abc", "replace", ba, bb)\r
722 EQ("aac", "abc", "replace", bb, ba)\r
723\r
724 #\r
725 self.checkequal('one@two!three!', 'one!two!three!', 'replace', '!', '@', 1)\r
726 self.checkequal('onetwothree', 'one!two!three!', 'replace', '!', '')\r
727 self.checkequal('one@two@three!', 'one!two!three!', 'replace', '!', '@', 2)\r
728 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 3)\r
729 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@', 4)\r
730 self.checkequal('one!two!three!', 'one!two!three!', 'replace', '!', '@', 0)\r
731 self.checkequal('one@two@three@', 'one!two!three!', 'replace', '!', '@')\r
732 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@')\r
733 self.checkequal('one!two!three!', 'one!two!three!', 'replace', 'x', '@', 2)\r
734 self.checkequal('-a-b-c-', 'abc', 'replace', '', '-')\r
735 self.checkequal('-a-b-c', 'abc', 'replace', '', '-', 3)\r
736 self.checkequal('abc', 'abc', 'replace', '', '-', 0)\r
737 self.checkequal('', '', 'replace', '', '')\r
738 self.checkequal('abc', 'abc', 'replace', 'ab', '--', 0)\r
739 self.checkequal('abc', 'abc', 'replace', 'xy', '--')\r
740 # Next three for SF bug 422088: [OSF1 alpha] string.replace(); died with\r
741 # MemoryError due to empty result (platform malloc issue when requesting\r
742 # 0 bytes).\r
743 self.checkequal('', '123', 'replace', '123', '')\r
744 self.checkequal('', '123123', 'replace', '123', '')\r
745 self.checkequal('x', '123x123', 'replace', '123', '')\r
746\r
747 self.checkraises(TypeError, 'hello', 'replace')\r
748 self.checkraises(TypeError, 'hello', 'replace', 42)\r
749 self.checkraises(TypeError, 'hello', 'replace', 42, 'h')\r
750 self.checkraises(TypeError, 'hello', 'replace', 'h', 42)\r
751\r
752 def test_replace_overflow(self):\r
753 # Check for overflow checking on 32 bit machines\r
754 if sys.maxint != 2147483647 or struct.calcsize("P") > 4:\r
755 return\r
756 A2_16 = "A" * (2**16)\r
757 self.checkraises(OverflowError, A2_16, "replace", "", A2_16)\r
758 self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)\r
759 self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)\r
760\r
761 def test_zfill(self):\r
762 self.checkequal('123', '123', 'zfill', 2)\r
763 self.checkequal('123', '123', 'zfill', 3)\r
764 self.checkequal('0123', '123', 'zfill', 4)\r
765 self.checkequal('+123', '+123', 'zfill', 3)\r
766 self.checkequal('+123', '+123', 'zfill', 4)\r
767 self.checkequal('+0123', '+123', 'zfill', 5)\r
768 self.checkequal('-123', '-123', 'zfill', 3)\r
769 self.checkequal('-123', '-123', 'zfill', 4)\r
770 self.checkequal('-0123', '-123', 'zfill', 5)\r
771 self.checkequal('000', '', 'zfill', 3)\r
772 self.checkequal('34', '34', 'zfill', 1)\r
773 self.checkequal('0034', '34', 'zfill', 4)\r
774\r
775 self.checkraises(TypeError, '123', 'zfill')\r
776\r
777# XXX alias for py3k forward compatibility\r
778BaseTest = CommonTest\r
779\r
780class MixinStrUnicodeUserStringTest:\r
781 # additional tests that only work for\r
782 # stringlike objects, i.e. str, unicode, UserString\r
783 # (but not the string module)\r
784\r
785 def test_islower(self):\r
786 self.checkequal(False, '', 'islower')\r
787 self.checkequal(True, 'a', 'islower')\r
788 self.checkequal(False, 'A', 'islower')\r
789 self.checkequal(False, '\n', 'islower')\r
790 self.checkequal(True, 'abc', 'islower')\r
791 self.checkequal(False, 'aBc', 'islower')\r
792 self.checkequal(True, 'abc\n', 'islower')\r
793 self.checkraises(TypeError, 'abc', 'islower', 42)\r
794\r
795 def test_isupper(self):\r
796 self.checkequal(False, '', 'isupper')\r
797 self.checkequal(False, 'a', 'isupper')\r
798 self.checkequal(True, 'A', 'isupper')\r
799 self.checkequal(False, '\n', 'isupper')\r
800 self.checkequal(True, 'ABC', 'isupper')\r
801 self.checkequal(False, 'AbC', 'isupper')\r
802 self.checkequal(True, 'ABC\n', 'isupper')\r
803 self.checkraises(TypeError, 'abc', 'isupper', 42)\r
804\r
805 def test_istitle(self):\r
806 self.checkequal(False, '', 'istitle')\r
807 self.checkequal(False, 'a', 'istitle')\r
808 self.checkequal(True, 'A', 'istitle')\r
809 self.checkequal(False, '\n', 'istitle')\r
810 self.checkequal(True, 'A Titlecased Line', 'istitle')\r
811 self.checkequal(True, 'A\nTitlecased Line', 'istitle')\r
812 self.checkequal(True, 'A Titlecased, Line', 'istitle')\r
813 self.checkequal(False, 'Not a capitalized String', 'istitle')\r
814 self.checkequal(False, 'Not\ta Titlecase String', 'istitle')\r
815 self.checkequal(False, 'Not--a Titlecase String', 'istitle')\r
816 self.checkequal(False, 'NOT', 'istitle')\r
817 self.checkraises(TypeError, 'abc', 'istitle', 42)\r
818\r
819 def test_isspace(self):\r
820 self.checkequal(False, '', 'isspace')\r
821 self.checkequal(False, 'a', 'isspace')\r
822 self.checkequal(True, ' ', 'isspace')\r
823 self.checkequal(True, '\t', 'isspace')\r
824 self.checkequal(True, '\r', 'isspace')\r
825 self.checkequal(True, '\n', 'isspace')\r
826 self.checkequal(True, ' \t\r\n', 'isspace')\r
827 self.checkequal(False, ' \t\r\na', 'isspace')\r
828 self.checkraises(TypeError, 'abc', 'isspace', 42)\r
829\r
830 def test_isalpha(self):\r
831 self.checkequal(False, '', 'isalpha')\r
832 self.checkequal(True, 'a', 'isalpha')\r
833 self.checkequal(True, 'A', 'isalpha')\r
834 self.checkequal(False, '\n', 'isalpha')\r
835 self.checkequal(True, 'abc', 'isalpha')\r
836 self.checkequal(False, 'aBc123', 'isalpha')\r
837 self.checkequal(False, 'abc\n', 'isalpha')\r
838 self.checkraises(TypeError, 'abc', 'isalpha', 42)\r
839\r
840 def test_isalnum(self):\r
841 self.checkequal(False, '', 'isalnum')\r
842 self.checkequal(True, 'a', 'isalnum')\r
843 self.checkequal(True, 'A', 'isalnum')\r
844 self.checkequal(False, '\n', 'isalnum')\r
845 self.checkequal(True, '123abc456', 'isalnum')\r
846 self.checkequal(True, 'a1b3c', 'isalnum')\r
847 self.checkequal(False, 'aBc000 ', 'isalnum')\r
848 self.checkequal(False, 'abc\n', 'isalnum')\r
849 self.checkraises(TypeError, 'abc', 'isalnum', 42)\r
850\r
851 def test_isdigit(self):\r
852 self.checkequal(False, '', 'isdigit')\r
853 self.checkequal(False, 'a', 'isdigit')\r
854 self.checkequal(True, '0', 'isdigit')\r
855 self.checkequal(True, '0123456789', 'isdigit')\r
856 self.checkequal(False, '0123456789a', 'isdigit')\r
857\r
858 self.checkraises(TypeError, 'abc', 'isdigit', 42)\r
859\r
860 def test_title(self):\r
861 self.checkequal(' Hello ', ' hello ', 'title')\r
862 self.checkequal('Hello ', 'hello ', 'title')\r
863 self.checkequal('Hello ', 'Hello ', 'title')\r
864 self.checkequal('Format This As Title String', "fOrMaT thIs aS titLe String", 'title')\r
865 self.checkequal('Format,This-As*Title;String', "fOrMaT,thIs-aS*titLe;String", 'title', )\r
866 self.checkequal('Getint', "getInt", 'title')\r
867 self.checkraises(TypeError, 'hello', 'title', 42)\r
868\r
869 def test_splitlines(self):\r
870 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\rghi", 'splitlines')\r
871 self.checkequal(['abc', 'def', '', 'ghi'], "abc\ndef\n\r\nghi", 'splitlines')\r
872 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi", 'splitlines')\r
873 self.checkequal(['abc', 'def', 'ghi'], "abc\ndef\r\nghi\n", 'splitlines')\r
874 self.checkequal(['abc', 'def', 'ghi', ''], "abc\ndef\r\nghi\n\r", 'splitlines')\r
875 self.checkequal(['', 'abc', 'def', 'ghi', ''], "\nabc\ndef\r\nghi\n\r", 'splitlines')\r
876 self.checkequal(['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], "\nabc\ndef\r\nghi\n\r", 'splitlines', 1)\r
877\r
878 self.checkraises(TypeError, 'abc', 'splitlines', 42, 42)\r
879\r
880 def test_startswith(self):\r
881 self.checkequal(True, 'hello', 'startswith', 'he')\r
882 self.checkequal(True, 'hello', 'startswith', 'hello')\r
883 self.checkequal(False, 'hello', 'startswith', 'hello world')\r
884 self.checkequal(True, 'hello', 'startswith', '')\r
885 self.checkequal(False, 'hello', 'startswith', 'ello')\r
886 self.checkequal(True, 'hello', 'startswith', 'ello', 1)\r
887 self.checkequal(True, 'hello', 'startswith', 'o', 4)\r
888 self.checkequal(False, 'hello', 'startswith', 'o', 5)\r
889 self.checkequal(True, 'hello', 'startswith', '', 5)\r
890 self.checkequal(False, 'hello', 'startswith', 'lo', 6)\r
891 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3)\r
892 self.checkequal(True, 'helloworld', 'startswith', 'lowo', 3, 7)\r
893 self.checkequal(False, 'helloworld', 'startswith', 'lowo', 3, 6)\r
894\r
895 # test negative indices\r
896 self.checkequal(True, 'hello', 'startswith', 'he', 0, -1)\r
897 self.checkequal(True, 'hello', 'startswith', 'he', -53, -1)\r
898 self.checkequal(False, 'hello', 'startswith', 'hello', 0, -1)\r
899 self.checkequal(False, 'hello', 'startswith', 'hello world', -1, -10)\r
900 self.checkequal(False, 'hello', 'startswith', 'ello', -5)\r
901 self.checkequal(True, 'hello', 'startswith', 'ello', -4)\r
902 self.checkequal(False, 'hello', 'startswith', 'o', -2)\r
903 self.checkequal(True, 'hello', 'startswith', 'o', -1)\r
904 self.checkequal(True, 'hello', 'startswith', '', -3, -3)\r
905 self.checkequal(False, 'hello', 'startswith', 'lo', -9)\r
906\r
907 self.checkraises(TypeError, 'hello', 'startswith')\r
908 self.checkraises(TypeError, 'hello', 'startswith', 42)\r
909\r
910 # test tuple arguments\r
911 self.checkequal(True, 'hello', 'startswith', ('he', 'ha'))\r
912 self.checkequal(False, 'hello', 'startswith', ('lo', 'llo'))\r
913 self.checkequal(True, 'hello', 'startswith', ('hellox', 'hello'))\r
914 self.checkequal(False, 'hello', 'startswith', ())\r
915 self.checkequal(True, 'helloworld', 'startswith', ('hellowo',\r
916 'rld', 'lowo'), 3)\r
917 self.checkequal(False, 'helloworld', 'startswith', ('hellowo', 'ello',\r
918 'rld'), 3)\r
919 self.checkequal(True, 'hello', 'startswith', ('lo', 'he'), 0, -1)\r
920 self.checkequal(False, 'hello', 'startswith', ('he', 'hel'), 0, 1)\r
921 self.checkequal(True, 'hello', 'startswith', ('he', 'hel'), 0, 2)\r
922\r
923 self.checkraises(TypeError, 'hello', 'startswith', (42,))\r
924\r
925 def test_endswith(self):\r
926 self.checkequal(True, 'hello', 'endswith', 'lo')\r
927 self.checkequal(False, 'hello', 'endswith', 'he')\r
928 self.checkequal(True, 'hello', 'endswith', '')\r
929 self.checkequal(False, 'hello', 'endswith', 'hello world')\r
930 self.checkequal(False, 'helloworld', 'endswith', 'worl')\r
931 self.checkequal(True, 'helloworld', 'endswith', 'worl', 3, 9)\r
932 self.checkequal(True, 'helloworld', 'endswith', 'world', 3, 12)\r
933 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 1, 7)\r
934 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 2, 7)\r
935 self.checkequal(True, 'helloworld', 'endswith', 'lowo', 3, 7)\r
936 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 4, 7)\r
937 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, 8)\r
938 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 1)\r
939 self.checkequal(False, 'ab', 'endswith', 'ab', 0, 0)\r
940\r
941 # test negative indices\r
942 self.checkequal(True, 'hello', 'endswith', 'lo', -2)\r
943 self.checkequal(False, 'hello', 'endswith', 'he', -2)\r
944 self.checkequal(True, 'hello', 'endswith', '', -3, -3)\r
945 self.checkequal(False, 'hello', 'endswith', 'hello world', -10, -2)\r
946 self.checkequal(False, 'helloworld', 'endswith', 'worl', -6)\r
947 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, -1)\r
948 self.checkequal(True, 'helloworld', 'endswith', 'worl', -5, 9)\r
949 self.checkequal(True, 'helloworld', 'endswith', 'world', -7, 12)\r
950 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -99, -3)\r
951 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -8, -3)\r
952 self.checkequal(True, 'helloworld', 'endswith', 'lowo', -7, -3)\r
953 self.checkequal(False, 'helloworld', 'endswith', 'lowo', 3, -4)\r
954 self.checkequal(False, 'helloworld', 'endswith', 'lowo', -8, -2)\r
955\r
956 self.checkraises(TypeError, 'hello', 'endswith')\r
957 self.checkraises(TypeError, 'hello', 'endswith', 42)\r
958\r
959 # test tuple arguments\r
960 self.checkequal(False, 'hello', 'endswith', ('he', 'ha'))\r
961 self.checkequal(True, 'hello', 'endswith', ('lo', 'llo'))\r
962 self.checkequal(True, 'hello', 'endswith', ('hellox', 'hello'))\r
963 self.checkequal(False, 'hello', 'endswith', ())\r
964 self.checkequal(True, 'helloworld', 'endswith', ('hellowo',\r
965 'rld', 'lowo'), 3)\r
966 self.checkequal(False, 'helloworld', 'endswith', ('hellowo', 'ello',\r
967 'rld'), 3, -1)\r
968 self.checkequal(True, 'hello', 'endswith', ('hell', 'ell'), 0, -1)\r
969 self.checkequal(False, 'hello', 'endswith', ('he', 'hel'), 0, 1)\r
970 self.checkequal(True, 'hello', 'endswith', ('he', 'hell'), 0, 4)\r
971\r
972 self.checkraises(TypeError, 'hello', 'endswith', (42,))\r
973\r
974 def test___contains__(self):\r
975 self.checkequal(True, '', '__contains__', '')\r
976 self.checkequal(True, 'abc', '__contains__', '')\r
977 self.checkequal(False, 'abc', '__contains__', '\0')\r
978 self.checkequal(True, '\0abc', '__contains__', '\0')\r
979 self.checkequal(True, 'abc\0', '__contains__', '\0')\r
980 self.checkequal(True, '\0abc', '__contains__', 'a')\r
981 self.checkequal(True, 'asdf', '__contains__', 'asdf')\r
982 self.checkequal(False, 'asd', '__contains__', 'asdf')\r
983 self.checkequal(False, '', '__contains__', 'asdf')\r
984\r
985 def test_subscript(self):\r
986 self.checkequal(u'a', 'abc', '__getitem__', 0)\r
987 self.checkequal(u'c', 'abc', '__getitem__', -1)\r
988 self.checkequal(u'a', 'abc', '__getitem__', 0L)\r
989 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 3))\r
990 self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))\r
991 self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))\r
992 self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))\r
993\r
994 self.checkraises(TypeError, 'abc', '__getitem__', 'def')\r
995\r
996 def test_slice(self):\r
997 self.checkequal('abc', 'abc', '__getslice__', 0, 1000)\r
998 self.checkequal('abc', 'abc', '__getslice__', 0, 3)\r
999 self.checkequal('ab', 'abc', '__getslice__', 0, 2)\r
1000 self.checkequal('bc', 'abc', '__getslice__', 1, 3)\r
1001 self.checkequal('b', 'abc', '__getslice__', 1, 2)\r
1002 self.checkequal('', 'abc', '__getslice__', 2, 2)\r
1003 self.checkequal('', 'abc', '__getslice__', 1000, 1000)\r
1004 self.checkequal('', 'abc', '__getslice__', 2000, 1000)\r
1005 self.checkequal('', 'abc', '__getslice__', 2, 1)\r
1006\r
1007 self.checkraises(TypeError, 'abc', '__getslice__', 'def')\r
1008\r
1009 def test_extended_getslice(self):\r
1010 # Test extended slicing by comparing with list slicing.\r
1011 s = string.ascii_letters + string.digits\r
1012 indices = (0, None, 1, 3, 41, -1, -2, -37)\r
1013 for start in indices:\r
1014 for stop in indices:\r
1015 # Skip step 0 (invalid)\r
1016 for step in indices[1:]:\r
1017 L = list(s)[start:stop:step]\r
1018 self.checkequal(u"".join(L), s, '__getitem__',\r
1019 slice(start, stop, step))\r
1020\r
1021 def test_mul(self):\r
1022 self.checkequal('', 'abc', '__mul__', -1)\r
1023 self.checkequal('', 'abc', '__mul__', 0)\r
1024 self.checkequal('abc', 'abc', '__mul__', 1)\r
1025 self.checkequal('abcabcabc', 'abc', '__mul__', 3)\r
1026 self.checkraises(TypeError, 'abc', '__mul__')\r
1027 self.checkraises(TypeError, 'abc', '__mul__', '')\r
1028 # XXX: on a 64-bit system, this doesn't raise an overflow error,\r
1029 # but either raises a MemoryError, or succeeds (if you have 54TiB)\r
1030 #self.checkraises(OverflowError, 10000*'abc', '__mul__', 2000000000)\r
1031\r
1032 def test_join(self):\r
1033 # join now works with any sequence type\r
1034 # moved here, because the argument order is\r
1035 # different in string.join (see the test in\r
1036 # test.test_string.StringTest.test_join)\r
1037 self.checkequal('a b c d', ' ', 'join', ['a', 'b', 'c', 'd'])\r
1038 self.checkequal('abcd', '', 'join', ('a', 'b', 'c', 'd'))\r
1039 self.checkequal('bd', '', 'join', ('', 'b', '', 'd'))\r
1040 self.checkequal('ac', '', 'join', ('a', '', 'c', ''))\r
1041 self.checkequal('w x y z', ' ', 'join', Sequence())\r
1042 self.checkequal('abc', 'a', 'join', ('abc',))\r
1043 self.checkequal('z', 'a', 'join', UserList(['z']))\r
1044 if test_support.have_unicode:\r
1045 self.checkequal(unicode('a.b.c'), unicode('.'), 'join', ['a', 'b', 'c'])\r
1046 self.checkequal(unicode('a.b.c'), '.', 'join', [unicode('a'), 'b', 'c'])\r
1047 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', unicode('b'), 'c'])\r
1048 self.checkequal(unicode('a.b.c'), '.', 'join', ['a', 'b', unicode('c')])\r
1049 self.checkraises(TypeError, '.', 'join', ['a', unicode('b'), 3])\r
1050 for i in [5, 25, 125]:\r
1051 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',\r
1052 ['a' * i] * i)\r
1053 self.checkequal(((('a' * i) + '-') * i)[:-1], '-', 'join',\r
1054 ('a' * i,) * i)\r
1055\r
1056 self.checkraises(TypeError, ' ', 'join', BadSeq1())\r
1057 self.checkequal('a b c', ' ', 'join', BadSeq2())\r
1058\r
1059 self.checkraises(TypeError, ' ', 'join')\r
1060 self.checkraises(TypeError, ' ', 'join', 7)\r
1061 self.checkraises(TypeError, ' ', 'join', Sequence([7, 'hello', 123L]))\r
1062 try:\r
1063 def f():\r
1064 yield 4 + ""\r
1065 self.fixtype(' ').join(f())\r
1066 except TypeError, e:\r
1067 if '+' not in str(e):\r
1068 self.fail('join() ate exception message')\r
1069 else:\r
1070 self.fail('exception not raised')\r
1071\r
1072 def test_formatting(self):\r
1073 self.checkequal('+hello+', '+%s+', '__mod__', 'hello')\r
1074 self.checkequal('+10+', '+%d+', '__mod__', 10)\r
1075 self.checkequal('a', "%c", '__mod__', "a")\r
1076 self.checkequal('a', "%c", '__mod__', "a")\r
1077 self.checkequal('"', "%c", '__mod__', 34)\r
1078 self.checkequal('$', "%c", '__mod__', 36)\r
1079 self.checkequal('10', "%d", '__mod__', 10)\r
1080 self.checkequal('\x7f', "%c", '__mod__', 0x7f)\r
1081\r
1082 for ordinal in (-100, 0x200000):\r
1083 # unicode raises ValueError, str raises OverflowError\r
1084 self.checkraises((ValueError, OverflowError), '%c', '__mod__', ordinal)\r
1085\r
1086 longvalue = sys.maxint + 10L\r
1087 slongvalue = str(longvalue)\r
1088 if slongvalue[-1] in ("L","l"): slongvalue = slongvalue[:-1]\r
1089 self.checkequal(' 42', '%3ld', '__mod__', 42)\r
1090 self.checkequal('42', '%d', '__mod__', 42L)\r
1091 self.checkequal('42', '%d', '__mod__', 42.0)\r
1092 self.checkequal(slongvalue, '%d', '__mod__', longvalue)\r
1093 self.checkcall('%d', '__mod__', float(longvalue))\r
1094 self.checkequal('0042.00', '%07.2f', '__mod__', 42)\r
1095 self.checkequal('0042.00', '%07.2F', '__mod__', 42)\r
1096\r
1097 self.checkraises(TypeError, 'abc', '__mod__')\r
1098 self.checkraises(TypeError, '%(foo)s', '__mod__', 42)\r
1099 self.checkraises(TypeError, '%s%s', '__mod__', (42,))\r
1100 self.checkraises(TypeError, '%c', '__mod__', (None,))\r
1101 self.checkraises(ValueError, '%(foo', '__mod__', {})\r
1102 self.checkraises(TypeError, '%(foo)s %(bar)s', '__mod__', ('foo', 42))\r
1103 self.checkraises(TypeError, '%d', '__mod__', "42") # not numeric\r
1104 self.checkraises(TypeError, '%d', '__mod__', (42+0j)) # no int/long conversion provided\r
1105\r
1106 # argument names with properly nested brackets are supported\r
1107 self.checkequal('bar', '%((foo))s', '__mod__', {'(foo)': 'bar'})\r
1108\r
1109 # 100 is a magic number in PyUnicode_Format, this forces a resize\r
1110 self.checkequal(103*'a'+'x', '%sx', '__mod__', 103*'a')\r
1111\r
1112 self.checkraises(TypeError, '%*s', '__mod__', ('foo', 'bar'))\r
1113 self.checkraises(TypeError, '%10.*f', '__mod__', ('foo', 42.))\r
1114 self.checkraises(ValueError, '%10', '__mod__', (42,))\r
1115\r
1116 def test_floatformatting(self):\r
1117 # float formatting\r
1118 for prec in xrange(100):\r
1119 format = '%%.%if' % prec\r
1120 value = 0.01\r
1121 for x in xrange(60):\r
1122 value = value * 3.14159265359 / 3.0 * 10.0\r
1123 self.checkcall(format, "__mod__", value)\r
1124\r
1125 def test_inplace_rewrites(self):\r
1126 # Check that strings don't copy and modify cached single-character strings\r
1127 self.checkequal('a', 'A', 'lower')\r
1128 self.checkequal(True, 'A', 'isupper')\r
1129 self.checkequal('A', 'a', 'upper')\r
1130 self.checkequal(True, 'a', 'islower')\r
1131\r
1132 self.checkequal('a', 'A', 'replace', 'A', 'a')\r
1133 self.checkequal(True, 'A', 'isupper')\r
1134\r
1135 self.checkequal('A', 'a', 'capitalize')\r
1136 self.checkequal(True, 'a', 'islower')\r
1137\r
1138 self.checkequal('A', 'a', 'swapcase')\r
1139 self.checkequal(True, 'a', 'islower')\r
1140\r
1141 self.checkequal('A', 'a', 'title')\r
1142 self.checkequal(True, 'a', 'islower')\r
1143\r
1144 def test_partition(self):\r
1145\r
1146 self.checkequal(('this is the par', 'ti', 'tion method'),\r
1147 'this is the partition method', 'partition', 'ti')\r
1148\r
1149 # from raymond's original specification\r
1150 S = 'http://www.python.org'\r
1151 self.checkequal(('http', '://', 'www.python.org'), S, 'partition', '://')\r
1152 self.checkequal(('http://www.python.org', '', ''), S, 'partition', '?')\r
1153 self.checkequal(('', 'http://', 'www.python.org'), S, 'partition', 'http://')\r
1154 self.checkequal(('http://www.python.', 'org', ''), S, 'partition', 'org')\r
1155\r
1156 self.checkraises(ValueError, S, 'partition', '')\r
1157 self.checkraises(TypeError, S, 'partition', None)\r
1158\r
1159 # mixed use of str and unicode\r
1160 self.assertEqual('a/b/c'.partition(u'/'), ('a', '/', 'b/c'))\r
1161\r
1162 def test_rpartition(self):\r
1163\r
1164 self.checkequal(('this is the rparti', 'ti', 'on method'),\r
1165 'this is the rpartition method', 'rpartition', 'ti')\r
1166\r
1167 # from raymond's original specification\r
1168 S = 'http://www.python.org'\r
1169 self.checkequal(('http', '://', 'www.python.org'), S, 'rpartition', '://')\r
1170 self.checkequal(('', '', 'http://www.python.org'), S, 'rpartition', '?')\r
1171 self.checkequal(('', 'http://', 'www.python.org'), S, 'rpartition', 'http://')\r
1172 self.checkequal(('http://www.python.', 'org', ''), S, 'rpartition', 'org')\r
1173\r
1174 self.checkraises(ValueError, S, 'rpartition', '')\r
1175 self.checkraises(TypeError, S, 'rpartition', None)\r
1176\r
1177 # mixed use of str and unicode\r
1178 self.assertEqual('a/b/c'.rpartition(u'/'), ('a/b', '/', 'c'))\r
1179\r
1180 def test_none_arguments(self):\r
1181 # issue 11828\r
1182 s = 'hello'\r
1183 self.checkequal(2, s, 'find', 'l', None)\r
1184 self.checkequal(3, s, 'find', 'l', -2, None)\r
1185 self.checkequal(2, s, 'find', 'l', None, -2)\r
1186 self.checkequal(0, s, 'find', 'h', None, None)\r
1187\r
1188 self.checkequal(3, s, 'rfind', 'l', None)\r
1189 self.checkequal(3, s, 'rfind', 'l', -2, None)\r
1190 self.checkequal(2, s, 'rfind', 'l', None, -2)\r
1191 self.checkequal(0, s, 'rfind', 'h', None, None)\r
1192\r
1193 self.checkequal(2, s, 'index', 'l', None)\r
1194 self.checkequal(3, s, 'index', 'l', -2, None)\r
1195 self.checkequal(2, s, 'index', 'l', None, -2)\r
1196 self.checkequal(0, s, 'index', 'h', None, None)\r
1197\r
1198 self.checkequal(3, s, 'rindex', 'l', None)\r
1199 self.checkequal(3, s, 'rindex', 'l', -2, None)\r
1200 self.checkequal(2, s, 'rindex', 'l', None, -2)\r
1201 self.checkequal(0, s, 'rindex', 'h', None, None)\r
1202\r
1203 self.checkequal(2, s, 'count', 'l', None)\r
1204 self.checkequal(1, s, 'count', 'l', -2, None)\r
1205 self.checkequal(1, s, 'count', 'l', None, -2)\r
1206 self.checkequal(0, s, 'count', 'x', None, None)\r
1207\r
1208 self.checkequal(True, s, 'endswith', 'o', None)\r
1209 self.checkequal(True, s, 'endswith', 'lo', -2, None)\r
1210 self.checkequal(True, s, 'endswith', 'l', None, -2)\r
1211 self.checkequal(False, s, 'endswith', 'x', None, None)\r
1212\r
1213 self.checkequal(True, s, 'startswith', 'h', None)\r
1214 self.checkequal(True, s, 'startswith', 'l', -2, None)\r
1215 self.checkequal(True, s, 'startswith', 'h', None, -2)\r
1216 self.checkequal(False, s, 'startswith', 'x', None, None)\r
1217\r
1218 def test_find_etc_raise_correct_error_messages(self):\r
1219 # issue 11828\r
1220 s = 'hello'\r
1221 x = 'x'\r
1222 self.assertRaisesRegexp(TypeError, r'\bfind\b', s.find,\r
1223 x, None, None, None)\r
1224 self.assertRaisesRegexp(TypeError, r'\brfind\b', s.rfind,\r
1225 x, None, None, None)\r
1226 self.assertRaisesRegexp(TypeError, r'\bindex\b', s.index,\r
1227 x, None, None, None)\r
1228 self.assertRaisesRegexp(TypeError, r'\brindex\b', s.rindex,\r
1229 x, None, None, None)\r
1230 self.assertRaisesRegexp(TypeError, r'^count\(', s.count,\r
1231 x, None, None, None)\r
1232 self.assertRaisesRegexp(TypeError, r'^startswith\(', s.startswith,\r
1233 x, None, None, None)\r
1234 self.assertRaisesRegexp(TypeError, r'^endswith\(', s.endswith,\r
1235 x, None, None, None)\r
1236\r
1237class MixinStrStringUserStringTest:\r
1238 # Additional tests for 8bit strings, i.e. str, UserString and\r
1239 # the string module\r
1240\r
1241 def test_maketrans(self):\r
1242 self.assertEqual(\r
1243 ''.join(map(chr, xrange(256))).replace('abc', 'xyz'),\r
1244 string.maketrans('abc', 'xyz')\r
1245 )\r
1246 self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw')\r
1247\r
1248 def test_translate(self):\r
1249 table = string.maketrans('abc', 'xyz')\r
1250 self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def')\r
1251\r
1252 table = string.maketrans('a', 'A')\r
1253 self.checkequal('Abc', 'abc', 'translate', table)\r
1254 self.checkequal('xyz', 'xyz', 'translate', table)\r
1255 self.checkequal('yz', 'xyz', 'translate', table, 'x')\r
1256 self.checkequal('yx', 'zyzzx', 'translate', None, 'z')\r
1257 self.checkequal('zyzzx', 'zyzzx', 'translate', None, '')\r
1258 self.checkequal('zyzzx', 'zyzzx', 'translate', None)\r
1259 self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip')\r
1260 self.checkraises(ValueError, 'xyz', 'translate', 'too short')\r
1261\r
1262\r
1263class MixinStrUserStringTest:\r
1264 # Additional tests that only work with\r
1265 # 8bit compatible object, i.e. str and UserString\r
1266\r
1267 if test_support.have_unicode:\r
1268 def test_encoding_decoding(self):\r
1269 codecs = [('rot13', 'uryyb jbeyq'),\r
1270 ('base64', 'aGVsbG8gd29ybGQ=\n'),\r
1271 ('hex', '68656c6c6f20776f726c64'),\r
1272 ('uu', 'begin 666 <data>\n+:&5L;&\\@=V]R;&0 \n \nend\n')]\r
1273 for encoding, data in codecs:\r
1274 self.checkequal(data, 'hello world', 'encode', encoding)\r
1275 self.checkequal('hello world', data, 'decode', encoding)\r
1276 # zlib is optional, so we make the test optional too...\r
1277 try:\r
1278 import zlib\r
1279 except ImportError:\r
1280 pass\r
1281 else:\r
1282 data = 'x\x9c\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\x01\x00\x1a\x0b\x04]'\r
1283 self.checkequal(data, 'hello world', 'encode', 'zlib')\r
1284 self.checkequal('hello world', data, 'decode', 'zlib')\r
1285\r
1286 self.checkraises(TypeError, 'xyz', 'decode', 42)\r
1287 self.checkraises(TypeError, 'xyz', 'encode', 42)\r
1288\r
1289\r
1290class MixinStrUnicodeTest:\r
1291 # Additional tests that only work with str and unicode.\r
1292\r
1293 def test_bug1001011(self):\r
1294 # Make sure join returns a NEW object for single item sequences\r
1295 # involving a subclass.\r
1296 # Make sure that it is of the appropriate type.\r
1297 # Check the optimisation still occurs for standard objects.\r
1298 t = self.type2test\r
1299 class subclass(t):\r
1300 pass\r
1301 s1 = subclass("abcd")\r
1302 s2 = t().join([s1])\r
1303 self.assertTrue(s1 is not s2)\r
1304 self.assertTrue(type(s2) is t)\r
1305\r
1306 s1 = t("abcd")\r
1307 s2 = t().join([s1])\r
1308 self.assertTrue(s1 is s2)\r
1309\r
1310 # Should also test mixed-type join.\r
1311 if t is unicode:\r
1312 s1 = subclass("abcd")\r
1313 s2 = "".join([s1])\r
1314 self.assertTrue(s1 is not s2)\r
1315 self.assertTrue(type(s2) is t)\r
1316\r
1317 s1 = t("abcd")\r
1318 s2 = "".join([s1])\r
1319 self.assertTrue(s1 is s2)\r
1320\r
1321 elif t is str:\r
1322 s1 = subclass("abcd")\r
1323 s2 = u"".join([s1])\r
1324 self.assertTrue(s1 is not s2)\r
1325 self.assertTrue(type(s2) is unicode) # promotes!\r
1326\r
1327 s1 = t("abcd")\r
1328 s2 = u"".join([s1])\r
1329 self.assertTrue(s1 is not s2)\r
1330 self.assertTrue(type(s2) is unicode) # promotes!\r
1331\r
1332 else:\r
1333 self.fail("unexpected type for MixinStrUnicodeTest %r" % t)\r