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