github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/third_party/stdlib/test/string_tests.py (about)

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