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