Home | History | Annotate | Download | only in test
      1 """Unit tests for the bytes and bytearray types.
      2 
      3 XXX This is a mess.  Common tests should be moved to buffer_tests.py,
      4 which itself ought to be unified with string_tests.py (and the latter
      5 should be modernized).
      6 """
      7 
      8 import os
      9 import re
     10 import sys
     11 import copy
     12 import functools
     13 import pickle
     14 import tempfile
     15 import unittest
     16 import test.test_support
     17 import test.string_tests
     18 import test.buffer_tests
     19 
     20 
     21 if sys.flags.bytes_warning:
     22     def check_bytes_warnings(func):
     23         @functools.wraps(func)
     24         def wrapper(*args, **kw):
     25             with test.test_support.check_warnings(('', BytesWarning)):
     26                 return func(*args, **kw)
     27         return wrapper
     28 else:
     29     # no-op
     30     def check_bytes_warnings(func):
     31         return func
     32 
     33 
     34 class Indexable:
     35     def __init__(self, value=0):
     36         self.value = value
     37     def __index__(self):
     38         return self.value
     39 
     40 
     41 class BaseBytesTest(unittest.TestCase):
     42 
     43     def test_basics(self):
     44         b = self.type2test()
     45         self.assertEqual(type(b), self.type2test)
     46         self.assertEqual(b.__class__, self.type2test)
     47 
     48     def test_empty_sequence(self):
     49         b = self.type2test()
     50         self.assertEqual(len(b), 0)
     51         self.assertRaises(IndexError, lambda: b[0])
     52         self.assertRaises(IndexError, lambda: b[1])
     53         self.assertRaises(IndexError, lambda: b[sys.maxint])
     54         self.assertRaises(IndexError, lambda: b[sys.maxint+1])
     55         self.assertRaises(IndexError, lambda: b[10**100])
     56         self.assertRaises(IndexError, lambda: b[-1])
     57         self.assertRaises(IndexError, lambda: b[-2])
     58         self.assertRaises(IndexError, lambda: b[-sys.maxint])
     59         self.assertRaises(IndexError, lambda: b[-sys.maxint-1])
     60         self.assertRaises(IndexError, lambda: b[-sys.maxint-2])
     61         self.assertRaises(IndexError, lambda: b[-10**100])
     62 
     63     def test_from_list(self):
     64         ints = list(range(256))
     65         b = self.type2test(i for i in ints)
     66         self.assertEqual(len(b), 256)
     67         self.assertEqual(list(b), ints)
     68 
     69     def test_from_index(self):
     70         b = self.type2test([Indexable(), Indexable(1), Indexable(254),
     71                             Indexable(255)])
     72         self.assertEqual(list(b), [0, 1, 254, 255])
     73         self.assertRaises(ValueError, self.type2test, [Indexable(-1)])
     74         self.assertRaises(ValueError, self.type2test, [Indexable(256)])
     75 
     76     def test_from_ssize(self):
     77         self.assertEqual(self.type2test(0), b'')
     78         self.assertEqual(self.type2test(1), b'\x00')
     79         self.assertEqual(self.type2test(5), b'\x00\x00\x00\x00\x00')
     80         self.assertRaises(ValueError, self.type2test, -1)
     81 
     82         self.assertEqual(self.type2test('0', 'ascii'), b'0')
     83         self.assertEqual(self.type2test(b'0'), b'0')
     84         self.assertRaises(OverflowError, self.type2test, sys.maxsize + 1)
     85 
     86     def test_constructor_type_errors(self):
     87         self.assertRaises(TypeError, self.type2test, 0.0)
     88         class C:
     89             pass
     90         # allowed in 2.x
     91         #self.assertRaises(TypeError, self.type2test, ["0"])
     92         self.assertRaises(TypeError, self.type2test, [0.0])
     93         self.assertRaises(TypeError, self.type2test, [None])
     94         self.assertRaises(TypeError, self.type2test, [C()])
     95 
     96     def test_constructor_value_errors(self):
     97         self.assertRaises(ValueError, self.type2test, [-1])
     98         self.assertRaises(ValueError, self.type2test, [-sys.maxint])
     99         self.assertRaises(ValueError, self.type2test, [-sys.maxint-1])
    100         self.assertRaises(ValueError, self.type2test, [-sys.maxint-2])
    101         self.assertRaises(ValueError, self.type2test, [-10**100])
    102         self.assertRaises(ValueError, self.type2test, [256])
    103         self.assertRaises(ValueError, self.type2test, [257])
    104         self.assertRaises(ValueError, self.type2test, [sys.maxint])
    105         self.assertRaises(ValueError, self.type2test, [sys.maxint+1])
    106         self.assertRaises(ValueError, self.type2test, [10**100])
    107 
    108     def test_compare(self):
    109         b1 = self.type2test([1, 2, 3])
    110         b2 = self.type2test([1, 2, 3])
    111         b3 = self.type2test([1, 3])
    112 
    113         self.assertEqual(b1, b2)
    114         self.assertTrue(b2 != b3)
    115         self.assertTrue(b1 <= b2)
    116         self.assertTrue(b1 <= b3)
    117         self.assertTrue(b1 <  b3)
    118         self.assertTrue(b1 >= b2)
    119         self.assertTrue(b3 >= b2)
    120         self.assertTrue(b3 >  b2)
    121 
    122         self.assertFalse(b1 != b2)
    123         self.assertFalse(b2 == b3)
    124         self.assertFalse(b1 >  b2)
    125         self.assertFalse(b1 >  b3)
    126         self.assertFalse(b1 >= b3)
    127         self.assertFalse(b1 <  b2)
    128         self.assertFalse(b3 <  b2)
    129         self.assertFalse(b3 <= b2)
    130 
    131     @check_bytes_warnings
    132     def test_compare_to_str(self):
    133         # Byte comparisons with unicode should always fail!
    134         # Test this for all expected byte orders and Unicode character sizes
    135         self.assertEqual(self.type2test(b"\0a\0b\0c") == u"abc", False)
    136         self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == u"abc", False)
    137         self.assertEqual(self.type2test(b"a\0b\0c\0") == u"abc", False)
    138         self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == u"abc", False)
    139         self.assertEqual(self.type2test() == unicode(), False)
    140         self.assertEqual(self.type2test() != unicode(), True)
    141 
    142     def test_reversed(self):
    143         input = list(map(ord, "Hello"))
    144         b = self.type2test(input)
    145         output = list(reversed(b))
    146         input.reverse()
    147         self.assertEqual(output, input)
    148 
    149     def test_getslice(self):
    150         def by(s):
    151             return self.type2test(map(ord, s))
    152         b = by("Hello, world")
    153 
    154         self.assertEqual(b[:5], by("Hello"))
    155         self.assertEqual(b[1:5], by("ello"))
    156         self.assertEqual(b[5:7], by(", "))
    157         self.assertEqual(b[7:], by("world"))
    158         self.assertEqual(b[7:12], by("world"))
    159         self.assertEqual(b[7:100], by("world"))
    160 
    161         self.assertEqual(b[:-7], by("Hello"))
    162         self.assertEqual(b[-11:-7], by("ello"))
    163         self.assertEqual(b[-7:-5], by(", "))
    164         self.assertEqual(b[-5:], by("world"))
    165         self.assertEqual(b[-5:12], by("world"))
    166         self.assertEqual(b[-5:100], by("world"))
    167         self.assertEqual(b[-100:5], by("Hello"))
    168 
    169     def test_extended_getslice(self):
    170         # Test extended slicing by comparing with list slicing.
    171         L = list(range(255))
    172         b = self.type2test(L)
    173         indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
    174         for start in indices:
    175             for stop in indices:
    176                 # Skip step 0 (invalid)
    177                 for step in indices[1:]:
    178                     self.assertEqual(b[start:stop:step], self.type2test(L[start:stop:step]))
    179 
    180     def test_encoding(self):
    181         sample = u"Hello world\n\u1234\u5678\u9abc\udef0"
    182         for enc in ("utf8", "utf16"):
    183             b = self.type2test(sample, enc)
    184             self.assertEqual(b, self.type2test(sample.encode(enc)))
    185         self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin1")
    186         b = self.type2test(sample, "latin1", "ignore")
    187         self.assertEqual(b, self.type2test(sample[:-4], "utf-8"))
    188 
    189     def test_decode(self):
    190         sample = u"Hello world\n\u1234\u5678\u9abc\def0\def0"
    191         for enc in ("utf8", "utf16"):
    192             b = self.type2test(sample, enc)
    193             self.assertEqual(b.decode(enc), sample)
    194         sample = u"Hello world\n\x80\x81\xfe\xff"
    195         b = self.type2test(sample, "latin1")
    196         self.assertRaises(UnicodeDecodeError, b.decode, "utf8")
    197         self.assertEqual(b.decode("utf8", "ignore"), "Hello world\n")
    198         self.assertEqual(b.decode(errors="ignore", encoding="utf8"),
    199                          "Hello world\n")
    200 
    201     def test_from_int(self):
    202         b = self.type2test(0)
    203         self.assertEqual(b, self.type2test())
    204         b = self.type2test(10)
    205         self.assertEqual(b, self.type2test([0]*10))
    206         b = self.type2test(10000)
    207         self.assertEqual(b, self.type2test([0]*10000))
    208 
    209     def test_concat(self):
    210         b1 = self.type2test(b"abc")
    211         b2 = self.type2test(b"def")
    212         self.assertEqual(b1 + b2, b"abcdef")
    213         self.assertEqual(b1 + bytes(b"def"), b"abcdef")
    214         self.assertEqual(bytes(b"def") + b1, b"defabc")
    215         self.assertRaises(TypeError, lambda: b1 + u"def")
    216         self.assertRaises(TypeError, lambda: u"abc" + b2)
    217 
    218     def test_repeat(self):
    219         for b in b"abc", self.type2test(b"abc"):
    220             self.assertEqual(b * 3, b"abcabcabc")
    221             self.assertEqual(b * 0, b"")
    222             self.assertEqual(b * -1, b"")
    223             self.assertRaises(TypeError, lambda: b * 3.14)
    224             self.assertRaises(TypeError, lambda: 3.14 * b)
    225             # XXX Shouldn't bytes and bytearray agree on what to raise?
    226             self.assertRaises((OverflowError, MemoryError),
    227                               lambda: b * sys.maxsize)
    228 
    229     def test_repeat_1char(self):
    230         self.assertEqual(self.type2test(b'x')*100, self.type2test([ord('x')]*100))
    231 
    232     def test_contains(self):
    233         b = self.type2test(b"abc")
    234         self.assertIn(ord('a'), b)
    235         self.assertIn(int(ord('a')), b)
    236         self.assertNotIn(200, b)
    237         self.assertRaises(ValueError, lambda: 300 in b)
    238         self.assertRaises(ValueError, lambda: -1 in b)
    239         self.assertRaises(TypeError, lambda: None in b)
    240         self.assertRaises(TypeError, lambda: float(ord('a')) in b)
    241         self.assertRaises(TypeError, lambda: u"a" in b)
    242         for f in bytes, bytearray:
    243             self.assertIn(f(b""), b)
    244             self.assertIn(f(b"a"), b)
    245             self.assertIn(f(b"b"), b)
    246             self.assertIn(f(b"c"), b)
    247             self.assertIn(f(b"ab"), b)
    248             self.assertIn(f(b"bc"), b)
    249             self.assertIn(f(b"abc"), b)
    250             self.assertNotIn(f(b"ac"), b)
    251             self.assertNotIn(f(b"d"), b)
    252             self.assertNotIn(f(b"dab"), b)
    253             self.assertNotIn(f(b"abd"), b)
    254 
    255     def test_fromhex(self):
    256         self.assertRaises(TypeError, self.type2test.fromhex)
    257         self.assertRaises(TypeError, self.type2test.fromhex, 1)
    258         self.assertEqual(self.type2test.fromhex(u''), self.type2test())
    259         b = bytearray([0x1a, 0x2b, 0x30])
    260         self.assertEqual(self.type2test.fromhex(u'1a2B30'), b)
    261         self.assertEqual(self.type2test.fromhex(u'  1A 2B  30   '), b)
    262         self.assertEqual(self.type2test.fromhex(u'0000'), b'\0\0')
    263         self.assertRaises(ValueError, self.type2test.fromhex, u'a')
    264         self.assertRaises(ValueError, self.type2test.fromhex, u'rt')
    265         self.assertRaises(ValueError, self.type2test.fromhex, u'1a b cd')
    266         self.assertRaises(ValueError, self.type2test.fromhex, u'\x00')
    267         self.assertRaises(ValueError, self.type2test.fromhex, u'12   \x00   34')
    268 
    269     def test_join(self):
    270         self.assertEqual(self.type2test(b"").join([]), b"")
    271         self.assertEqual(self.type2test(b"").join([b""]), b"")
    272         for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]:
    273             lst = list(map(self.type2test, lst))
    274             self.assertEqual(self.type2test(b"").join(lst), b"abc")
    275             self.assertEqual(self.type2test(b"").join(tuple(lst)), b"abc")
    276             self.assertEqual(self.type2test(b"").join(iter(lst)), b"abc")
    277         self.assertEqual(self.type2test(b".").join([b"ab", b"cd"]), b"ab.cd")
    278         # XXX more...
    279 
    280     def test_count(self):
    281         b = self.type2test(b'mississippi')
    282         self.assertEqual(b.count(b'i'), 4)
    283         self.assertEqual(b.count(b'ss'), 2)
    284         self.assertEqual(b.count(b'w'), 0)
    285 
    286     def test_startswith(self):
    287         b = self.type2test(b'hello')
    288         self.assertFalse(self.type2test().startswith(b"anything"))
    289         self.assertTrue(b.startswith(b"hello"))
    290         self.assertTrue(b.startswith(b"hel"))
    291         self.assertTrue(b.startswith(b"h"))
    292         self.assertFalse(b.startswith(b"hellow"))
    293         self.assertFalse(b.startswith(b"ha"))
    294 
    295     def test_endswith(self):
    296         b = self.type2test(b'hello')
    297         self.assertFalse(bytearray().endswith(b"anything"))
    298         self.assertTrue(b.endswith(b"hello"))
    299         self.assertTrue(b.endswith(b"llo"))
    300         self.assertTrue(b.endswith(b"o"))
    301         self.assertFalse(b.endswith(b"whello"))
    302         self.assertFalse(b.endswith(b"no"))
    303 
    304     def test_find(self):
    305         b = self.type2test(b'mississippi')
    306         self.assertEqual(b.find(b'ss'), 2)
    307         self.assertEqual(b.find(b'ss', 3), 5)
    308         self.assertEqual(b.find(b'ss', 1, 7), 2)
    309         self.assertEqual(b.find(b'ss', 1, 3), -1)
    310         self.assertEqual(b.find(b'w'), -1)
    311         self.assertEqual(b.find(b'mississippian'), -1)
    312 
    313     def test_rfind(self):
    314         b = self.type2test(b'mississippi')
    315         self.assertEqual(b.rfind(b'ss'), 5)
    316         self.assertEqual(b.rfind(b'ss', 3), 5)
    317         self.assertEqual(b.rfind(b'ss', 0, 6), 2)
    318         self.assertEqual(b.rfind(b'w'), -1)
    319         self.assertEqual(b.rfind(b'mississippian'), -1)
    320 
    321     def test_index(self):
    322         b = self.type2test(b'world')
    323         self.assertEqual(b.index(b'w'), 0)
    324         self.assertEqual(b.index(b'orl'), 1)
    325         self.assertRaises(ValueError, b.index, b'worm')
    326         self.assertRaises(ValueError, b.index, b'ldo')
    327 
    328     def test_rindex(self):
    329         # XXX could be more rigorous
    330         b = self.type2test(b'world')
    331         self.assertEqual(b.rindex(b'w'), 0)
    332         self.assertEqual(b.rindex(b'orl'), 1)
    333         self.assertRaises(ValueError, b.rindex, b'worm')
    334         self.assertRaises(ValueError, b.rindex, b'ldo')
    335 
    336     def test_replace(self):
    337         b = self.type2test(b'mississippi')
    338         self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
    339         self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
    340 
    341     def test_split(self):
    342         b = self.type2test(b'mississippi')
    343         self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
    344         self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi'])
    345         self.assertEqual(b.split(b'w'), [b])
    346 
    347     def test_split_whitespace(self):
    348         for b in (b'  arf  barf  ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
    349                   b'arf\fbarf', b'arf\vbarf'):
    350             b = self.type2test(b)
    351             self.assertEqual(b.split(), [b'arf', b'barf'])
    352             self.assertEqual(b.split(None), [b'arf', b'barf'])
    353             self.assertEqual(b.split(None, 2), [b'arf', b'barf'])
    354         for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'):
    355             b = self.type2test(b)
    356             self.assertEqual(b.split(), [b])
    357         self.assertEqual(self.type2test(b'  a  bb  c  ').split(None, 0), [b'a  bb  c  '])
    358         self.assertEqual(self.type2test(b'  a  bb  c  ').split(None, 1), [b'a', b'bb  c  '])
    359         self.assertEqual(self.type2test(b'  a  bb  c  ').split(None, 2), [b'a', b'bb', b'c  '])
    360         self.assertEqual(self.type2test(b'  a  bb  c  ').split(None, 3), [b'a', b'bb', b'c'])
    361 
    362     def test_split_string_error(self):
    363         self.assertRaises(TypeError, self.type2test(b'a b').split, u' ')
    364 
    365     def test_split_unicodewhitespace(self):
    366         b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
    367         self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f'])
    368 
    369     def test_rsplit(self):
    370         b = self.type2test(b'mississippi')
    371         self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b''])
    372         self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi'])
    373         self.assertEqual(b.rsplit(b'w'), [b])
    374 
    375     def test_rsplit_whitespace(self):
    376         for b in (b'  arf  barf  ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf',
    377                   b'arf\fbarf', b'arf\vbarf'):
    378             b = self.type2test(b)
    379             self.assertEqual(b.rsplit(), [b'arf', b'barf'])
    380             self.assertEqual(b.rsplit(None), [b'arf', b'barf'])
    381             self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf'])
    382         self.assertEqual(self.type2test(b'  a  bb  c  ').rsplit(None, 0), [b'  a  bb  c'])
    383         self.assertEqual(self.type2test(b'  a  bb  c  ').rsplit(None, 1), [b'  a  bb', b'c'])
    384         self.assertEqual(self.type2test(b'  a  bb  c  ').rsplit(None, 2), [b'  a', b'bb', b'c'])
    385         self.assertEqual(self.type2test(b'  a  bb  c  ').rsplit(None, 3), [b'a', b'bb', b'c'])
    386 
    387     def test_rsplit_string_error(self):
    388         self.assertRaises(TypeError, self.type2test(b'a b').rsplit, u' ')
    389 
    390     def test_rsplit_unicodewhitespace(self):
    391         b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F")
    392         self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f'])
    393 
    394     def test_partition(self):
    395         b = self.type2test(b'mississippi')
    396         self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi'))
    397         self.assertEqual(b.partition(b'w'), (b'mississippi', b'', b''))
    398 
    399     def test_rpartition(self):
    400         b = self.type2test(b'mississippi')
    401         self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi'))
    402         self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
    403         self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi'))
    404 
    405     def test_pickling(self):
    406         for proto in range(pickle.HIGHEST_PROTOCOL + 1):
    407             for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
    408                 b = self.type2test(b)
    409                 ps = pickle.dumps(b, proto)
    410                 q = pickle.loads(ps)
    411                 self.assertEqual(b, q)
    412 
    413     def test_strip(self):
    414         b = self.type2test(b'mississippi')
    415         self.assertEqual(b.strip(b'i'), b'mississipp')
    416         self.assertEqual(b.strip(b'm'), b'ississippi')
    417         self.assertEqual(b.strip(b'pi'), b'mississ')
    418         self.assertEqual(b.strip(b'im'), b'ssissipp')
    419         self.assertEqual(b.strip(b'pim'), b'ssiss')
    420         self.assertEqual(b.strip(b), b'')
    421 
    422     def test_lstrip(self):
    423         b = self.type2test(b'mississippi')
    424         self.assertEqual(b.lstrip(b'i'), b'mississippi')
    425         self.assertEqual(b.lstrip(b'm'), b'ississippi')
    426         self.assertEqual(b.lstrip(b'pi'), b'mississippi')
    427         self.assertEqual(b.lstrip(b'im'), b'ssissippi')
    428         self.assertEqual(b.lstrip(b'pim'), b'ssissippi')
    429 
    430     def test_rstrip(self):
    431         b = self.type2test(b'mississippi')
    432         self.assertEqual(b.rstrip(b'i'), b'mississipp')
    433         self.assertEqual(b.rstrip(b'm'), b'mississippi')
    434         self.assertEqual(b.rstrip(b'pi'), b'mississ')
    435         self.assertEqual(b.rstrip(b'im'), b'mississipp')
    436         self.assertEqual(b.rstrip(b'pim'), b'mississ')
    437 
    438     def test_strip_whitespace(self):
    439         b = self.type2test(b' \t\n\r\f\vabc \t\n\r\f\v')
    440         self.assertEqual(b.strip(), b'abc')
    441         self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v')
    442         self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc')
    443 
    444     def test_strip_bytearray(self):
    445         self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b')
    446         self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc')
    447         self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab')
    448 
    449     def test_strip_string_error(self):
    450         self.assertRaises(TypeError, self.type2test(b'abc').strip, u'b')
    451         self.assertRaises(TypeError, self.type2test(b'abc').lstrip, u'b')
    452         self.assertRaises(TypeError, self.type2test(b'abc').rstrip, u'b')
    453 
    454     def test_ord(self):
    455         b = self.type2test(b'\0A\x7f\x80\xff')
    456         self.assertEqual([ord(b[i:i+1]) for i in range(len(b))],
    457                          [0, 65, 127, 128, 255])
    458 
    459     def test_none_arguments(self):
    460         # issue 11828
    461         b = self.type2test(b'hello')
    462         l = self.type2test(b'l')
    463         h = self.type2test(b'h')
    464         x = self.type2test(b'x')
    465         o = self.type2test(b'o')
    466 
    467         self.assertEqual(2, b.find(l, None))
    468         self.assertEqual(3, b.find(l, -2, None))
    469         self.assertEqual(2, b.find(l, None, -2))
    470         self.assertEqual(0, b.find(h, None, None))
    471 
    472         self.assertEqual(3, b.rfind(l, None))
    473         self.assertEqual(3, b.rfind(l, -2, None))
    474         self.assertEqual(2, b.rfind(l, None, -2))
    475         self.assertEqual(0, b.rfind(h, None, None))
    476 
    477         self.assertEqual(2, b.index(l, None))
    478         self.assertEqual(3, b.index(l, -2, None))
    479         self.assertEqual(2, b.index(l, None, -2))
    480         self.assertEqual(0, b.index(h, None, None))
    481 
    482         self.assertEqual(3, b.rindex(l, None))
    483         self.assertEqual(3, b.rindex(l, -2, None))
    484         self.assertEqual(2, b.rindex(l, None, -2))
    485         self.assertEqual(0, b.rindex(h, None, None))
    486 
    487         self.assertEqual(2, b.count(l, None))
    488         self.assertEqual(1, b.count(l, -2, None))
    489         self.assertEqual(1, b.count(l, None, -2))
    490         self.assertEqual(0, b.count(x, None, None))
    491 
    492         self.assertEqual(True, b.endswith(o, None))
    493         self.assertEqual(True, b.endswith(o, -2, None))
    494         self.assertEqual(True, b.endswith(l, None, -2))
    495         self.assertEqual(False, b.endswith(x, None, None))
    496 
    497         self.assertEqual(True, b.startswith(h, None))
    498         self.assertEqual(True, b.startswith(l, -2, None))
    499         self.assertEqual(True, b.startswith(h, None, -2))
    500         self.assertEqual(False, b.startswith(x, None, None))
    501 
    502     def test_find_etc_raise_correct_error_messages(self):
    503         # issue 11828
    504         b = self.type2test(b'hello')
    505         x = self.type2test(b'x')
    506         self.assertRaisesRegexp(TypeError, r'\bfind\b', b.find,
    507                                 x, None, None, None)
    508         self.assertRaisesRegexp(TypeError, r'\brfind\b', b.rfind,
    509                                 x, None, None, None)
    510         self.assertRaisesRegexp(TypeError, r'\bindex\b', b.index,
    511                                 x, None, None, None)
    512         self.assertRaisesRegexp(TypeError, r'\brindex\b', b.rindex,
    513                                 x, None, None, None)
    514         self.assertRaisesRegexp(TypeError, r'\bcount\b', b.count,
    515                                 x, None, None, None)
    516         self.assertRaisesRegexp(TypeError, r'\bstartswith\b', b.startswith,
    517                                 x, None, None, None)
    518         self.assertRaisesRegexp(TypeError, r'\bendswith\b', b.endswith,
    519                                 x, None, None, None)
    520 
    521 
    522 class ByteArrayTest(BaseBytesTest):
    523     type2test = bytearray
    524 
    525     def test_nohash(self):
    526         self.assertRaises(TypeError, hash, bytearray())
    527 
    528     def test_bytearray_api(self):
    529         short_sample = b"Hello world\n"
    530         sample = short_sample + b"\0"*(20 - len(short_sample))
    531         tfn = tempfile.mktemp()
    532         try:
    533             # Prepare
    534             with open(tfn, "wb") as f:
    535                 f.write(short_sample)
    536             # Test readinto
    537             with open(tfn, "rb") as f:
    538                 b = bytearray(20)
    539                 n = f.readinto(b)
    540             self.assertEqual(n, len(short_sample))
    541             # Python 2.x
    542             b_sample = (ord(s) for s in sample)
    543             self.assertEqual(list(b), list(b_sample))
    544             # Test writing in binary mode
    545             with open(tfn, "wb") as f:
    546                 f.write(b)
    547             with open(tfn, "rb") as f:
    548                 self.assertEqual(f.read(), sample)
    549             # Text mode is ambiguous; don't test
    550         finally:
    551             try:
    552                 os.remove(tfn)
    553             except os.error:
    554                 pass
    555 
    556     def test_reverse(self):
    557         b = bytearray(b'hello')
    558         self.assertEqual(b.reverse(), None)
    559         self.assertEqual(b, b'olleh')
    560         b = bytearray(b'hello1') # test even number of items
    561         b.reverse()
    562         self.assertEqual(b, b'1olleh')
    563         b = bytearray()
    564         b.reverse()
    565         self.assertFalse(b)
    566 
    567     def test_regexps(self):
    568         def by(s):
    569             return bytearray(map(ord, s))
    570         b = by("Hello, world")
    571         self.assertEqual(re.findall(r"\w+", b), [by("Hello"), by("world")])
    572 
    573     def test_setitem(self):
    574         b = bytearray([1, 2, 3])
    575         b[1] = 100
    576         self.assertEqual(b, bytearray([1, 100, 3]))
    577         b[-1] = 200
    578         self.assertEqual(b, bytearray([1, 100, 200]))
    579         b[0] = Indexable(10)
    580         self.assertEqual(b, bytearray([10, 100, 200]))
    581         try:
    582             b[3] = 0
    583             self.fail("Didn't raise IndexError")
    584         except IndexError:
    585             pass
    586         try:
    587             b[-10] = 0
    588             self.fail("Didn't raise IndexError")
    589         except IndexError:
    590             pass
    591         try:
    592             b[0] = 256
    593             self.fail("Didn't raise ValueError")
    594         except ValueError:
    595             pass
    596         try:
    597             b[0] = Indexable(-1)
    598             self.fail("Didn't raise ValueError")
    599         except ValueError:
    600             pass
    601         try:
    602             b[0] = None
    603             self.fail("Didn't raise TypeError")
    604         except TypeError:
    605             pass
    606 
    607     def test_delitem(self):
    608         b = bytearray(range(10))
    609         del b[0]
    610         self.assertEqual(b, bytearray(range(1, 10)))
    611         del b[-1]
    612         self.assertEqual(b, bytearray(range(1, 9)))
    613         del b[4]
    614         self.assertEqual(b, bytearray([1, 2, 3, 4, 6, 7, 8]))
    615 
    616     def test_setslice(self):
    617         b = bytearray(range(10))
    618         self.assertEqual(list(b), list(range(10)))
    619 
    620         b[0:5] = bytearray([1, 1, 1, 1, 1])
    621         self.assertEqual(b, bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9]))
    622 
    623         del b[0:-5]
    624         self.assertEqual(b, bytearray([5, 6, 7, 8, 9]))
    625 
    626         b[0:0] = bytearray([0, 1, 2, 3, 4])
    627         self.assertEqual(b, bytearray(range(10)))
    628 
    629         b[-7:-3] = bytearray([100, 101])
    630         self.assertEqual(b, bytearray([0, 1, 2, 100, 101, 7, 8, 9]))
    631 
    632         b[3:5] = [3, 4, 5, 6]
    633         self.assertEqual(b, bytearray(range(10)))
    634 
    635         b[3:0] = [42, 42, 42]
    636         self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9]))
    637 
    638         b[3:] = b'foo'
    639         self.assertEqual(b, bytearray([0, 1, 2, 102, 111, 111]))
    640 
    641         b[:3] = memoryview(b'foo')
    642         self.assertEqual(b, bytearray([102, 111, 111, 102, 111, 111]))
    643 
    644         b[3:4] = []
    645         self.assertEqual(b, bytearray([102, 111, 111, 111, 111]))
    646 
    647         b[1:] = list(b'uuuu')  # this works only on Python2
    648         self.assertEqual(b, bytearray([102, 117, 117, 117, 117]))
    649 
    650         for elem in [5, -5, 0, long(10e20), u'str', 2.3, [u'a', u'b'], [[]]]:
    651             with self.assertRaises(TypeError):
    652                 b[3:4] = elem
    653 
    654         for elem in [[254, 255, 256], [-256, 9000]]:
    655             with self.assertRaises(ValueError):
    656                 b[3:4] = elem
    657 
    658     def test_extended_set_del_slice(self):
    659         indices = (0, None, 1, 3, 19, 300, 1<<333, -1, -2, -31, -300)
    660         for start in indices:
    661             for stop in indices:
    662                 # Skip invalid step 0
    663                 for step in indices[1:]:
    664                     L = list(range(255))
    665                     b = bytearray(L)
    666                     # Make sure we have a slice of exactly the right length,
    667                     # but with different data.
    668                     data = L[start:stop:step]
    669                     data.reverse()
    670                     L[start:stop:step] = data
    671                     b[start:stop:step] = data
    672                     self.assertEqual(b, bytearray(L))
    673 
    674                     del L[start:stop:step]
    675                     del b[start:stop:step]
    676                     self.assertEqual(b, bytearray(L))
    677 
    678     def test_setslice_trap(self):
    679         # This test verifies that we correctly handle assigning self
    680         # to a slice of self (the old Lambert Meertens trap).
    681         b = bytearray(range(256))
    682         b[8:] = b
    683         self.assertEqual(b, bytearray(list(range(8)) + list(range(256))))
    684 
    685     def test_iconcat(self):
    686         b = bytearray(b"abc")
    687         b1 = b
    688         b += b"def"
    689         self.assertEqual(b, b"abcdef")
    690         self.assertEqual(b, b1)
    691         self.assertTrue(b is b1)
    692         b += b"xyz"
    693         self.assertEqual(b, b"abcdefxyz")
    694         try:
    695             b += u""
    696         except TypeError:
    697             pass
    698         else:
    699             self.fail("bytes += unicode didn't raise TypeError")
    700 
    701     def test_irepeat(self):
    702         b = bytearray(b"abc")
    703         b1 = b
    704         b *= 3
    705         self.assertEqual(b, b"abcabcabc")
    706         self.assertEqual(b, b1)
    707         self.assertTrue(b is b1)
    708 
    709     def test_irepeat_1char(self):
    710         b = bytearray(b"x")
    711         b1 = b
    712         b *= 100
    713         self.assertEqual(b, b"x"*100)
    714         self.assertEqual(b, b1)
    715         self.assertTrue(b is b1)
    716 
    717     def test_alloc(self):
    718         b = bytearray()
    719         alloc = b.__alloc__()
    720         self.assertTrue(alloc >= 0)
    721         seq = [alloc]
    722         for i in range(100):
    723             b += b"x"
    724             alloc = b.__alloc__()
    725             self.assertTrue(alloc >= len(b))
    726             if alloc not in seq:
    727                 seq.append(alloc)
    728 
    729     def test_extend(self):
    730         orig = b'hello'
    731         a = bytearray(orig)
    732         a.extend(a)
    733         self.assertEqual(a, orig + orig)
    734         self.assertEqual(a[5:], orig)
    735         a = bytearray(b'')
    736         # Test iterators that don't have a __length_hint__
    737         a.extend(map(ord, orig * 25))
    738         a.extend(ord(x) for x in orig * 25)
    739         self.assertEqual(a, orig * 50)
    740         self.assertEqual(a[-5:], orig)
    741         a = bytearray(b'')
    742         a.extend(iter(map(ord, orig * 50)))
    743         self.assertEqual(a, orig * 50)
    744         self.assertEqual(a[-5:], orig)
    745         a = bytearray(b'')
    746         a.extend(list(map(ord, orig * 50)))
    747         self.assertEqual(a, orig * 50)
    748         self.assertEqual(a[-5:], orig)
    749         a = bytearray(b'')
    750         self.assertRaises(ValueError, a.extend, [0, 1, 2, 256])
    751         self.assertRaises(ValueError, a.extend, [0, 1, 2, -1])
    752         self.assertEqual(len(a), 0)
    753         a = bytearray(b'')
    754         a.extend([Indexable(ord('a'))])
    755         self.assertEqual(a, b'a')
    756 
    757     def test_remove(self):
    758         b = bytearray(b'hello')
    759         b.remove(ord('l'))
    760         self.assertEqual(b, b'helo')
    761         b.remove(ord('l'))
    762         self.assertEqual(b, b'heo')
    763         self.assertRaises(ValueError, lambda: b.remove(ord('l')))
    764         self.assertRaises(ValueError, lambda: b.remove(400))
    765         self.assertRaises(TypeError, lambda: b.remove(u'e'))
    766         # remove first and last
    767         b.remove(ord('o'))
    768         b.remove(ord('h'))
    769         self.assertEqual(b, b'e')
    770         self.assertRaises(TypeError, lambda: b.remove(u'e'))
    771         b.remove(Indexable(ord('e')))
    772         self.assertEqual(b, b'')
    773 
    774     def test_pop(self):
    775         b = bytearray(b'world')
    776         self.assertEqual(b.pop(), ord('d'))
    777         self.assertEqual(b.pop(0), ord('w'))
    778         self.assertEqual(b.pop(-2), ord('r'))
    779         self.assertRaises(IndexError, lambda: b.pop(10))
    780         self.assertRaises(IndexError, lambda: bytearray().pop())
    781         # test for issue #6846
    782         self.assertEqual(bytearray(b'\xff').pop(), 0xff)
    783 
    784     def test_nosort(self):
    785         self.assertRaises(AttributeError, lambda: bytearray().sort())
    786 
    787     def test_append(self):
    788         b = bytearray(b'hell')
    789         b.append(ord('o'))
    790         self.assertEqual(b, b'hello')
    791         self.assertEqual(b.append(100), None)
    792         b = bytearray()
    793         b.append(ord('A'))
    794         self.assertEqual(len(b), 1)
    795         self.assertRaises(TypeError, lambda: b.append(u'o'))
    796         b = bytearray()
    797         b.append(Indexable(ord('A')))
    798         self.assertEqual(b, b'A')
    799 
    800     def test_insert(self):
    801         b = bytearray(b'msssspp')
    802         b.insert(1, ord('i'))
    803         b.insert(4, ord('i'))
    804         b.insert(-2, ord('i'))
    805         b.insert(1000, ord('i'))
    806         self.assertEqual(b, b'mississippi')
    807         # allowed in 2.x
    808         #self.assertRaises(TypeError, lambda: b.insert(0, b'1'))
    809         b = bytearray()
    810         b.insert(0, Indexable(ord('A')))
    811         self.assertEqual(b, b'A')
    812 
    813     def test_copied(self):
    814         # Issue 4348.  Make sure that operations that don't mutate the array
    815         # copy the bytes.
    816         b = bytearray(b'abc')
    817         self.assertFalse(b is b.replace(b'abc', b'cde', 0))
    818 
    819         t = bytearray([i for i in range(256)])
    820         x = bytearray(b'')
    821         self.assertFalse(x is x.translate(t))
    822 
    823     def test_partition_bytearray_doesnt_share_nullstring(self):
    824         a, b, c = bytearray(b"x").partition(b"y")
    825         self.assertEqual(b, b"")
    826         self.assertEqual(c, b"")
    827         self.assertTrue(b is not c)
    828         b += b"!"
    829         self.assertEqual(c, b"")
    830         a, b, c = bytearray(b"x").partition(b"y")
    831         self.assertEqual(b, b"")
    832         self.assertEqual(c, b"")
    833         # Same for rpartition
    834         b, c, a = bytearray(b"x").rpartition(b"y")
    835         self.assertEqual(b, b"")
    836         self.assertEqual(c, b"")
    837         self.assertTrue(b is not c)
    838         b += b"!"
    839         self.assertEqual(c, b"")
    840         c, b, a = bytearray(b"x").rpartition(b"y")
    841         self.assertEqual(b, b"")
    842         self.assertEqual(c, b"")
    843 
    844     def test_resize_forbidden(self):
    845         # #4509: can't resize a bytearray when there are buffer exports, even
    846         # if it wouldn't reallocate the underlying buffer.
    847         # Furthermore, no destructive changes to the buffer may be applied
    848         # before raising the error.
    849         b = bytearray(range(10))
    850         v = memoryview(b)
    851         def resize(n):
    852             b[1:-1] = range(n + 1, 2*n - 1)
    853         resize(10)
    854         orig = b[:]
    855         self.assertRaises(BufferError, resize, 11)
    856         self.assertEqual(b, orig)
    857         self.assertRaises(BufferError, resize, 9)
    858         self.assertEqual(b, orig)
    859         self.assertRaises(BufferError, resize, 0)
    860         self.assertEqual(b, orig)
    861         # Other operations implying resize
    862         self.assertRaises(BufferError, b.pop, 0)
    863         self.assertEqual(b, orig)
    864         self.assertRaises(BufferError, b.remove, b[1])
    865         self.assertEqual(b, orig)
    866         def delitem():
    867             del b[1]
    868         self.assertRaises(BufferError, delitem)
    869         self.assertEqual(b, orig)
    870         # deleting a non-contiguous slice
    871         def delslice():
    872             b[1:-1:2] = b""
    873         self.assertRaises(BufferError, delslice)
    874         self.assertEqual(b, orig)
    875 
    876     def test_empty_bytearray(self):
    877         # Issue #7561: operations on empty bytearrays could crash in many
    878         # situations, due to a fragile implementation of the
    879         # PyByteArray_AS_STRING() C macro.
    880         self.assertRaises(ValueError, int, bytearray(b''))
    881 
    882 
    883 class AssortedBytesTest(unittest.TestCase):
    884     #
    885     # Test various combinations of bytes and bytearray
    886     #
    887 
    888     @check_bytes_warnings
    889     def test_repr_str(self):
    890         for f in str, repr:
    891             self.assertEqual(f(bytearray()), "bytearray(b'')")
    892             self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')")
    893             self.assertEqual(f(bytearray([0, 1, 254, 255])),
    894                              "bytearray(b'\\x00\\x01\\xfe\\xff')")
    895             self.assertEqual(f(b"abc"), "b'abc'")
    896             self.assertEqual(f(b"'"), '''b"'"''') # '''
    897             self.assertEqual(f(b"'\""), r"""b'\'"'""") # '
    898 
    899     def test_compare_bytes_to_bytearray(self):
    900         self.assertEqual(b"abc" == bytes(b"abc"), True)
    901         self.assertEqual(b"ab" != bytes(b"abc"), True)
    902         self.assertEqual(b"ab" <= bytes(b"abc"), True)
    903         self.assertEqual(b"ab" < bytes(b"abc"), True)
    904         self.assertEqual(b"abc" >= bytes(b"ab"), True)
    905         self.assertEqual(b"abc" > bytes(b"ab"), True)
    906 
    907         self.assertEqual(b"abc" != bytes(b"abc"), False)
    908         self.assertEqual(b"ab" == bytes(b"abc"), False)
    909         self.assertEqual(b"ab" > bytes(b"abc"), False)
    910         self.assertEqual(b"ab" >= bytes(b"abc"), False)
    911         self.assertEqual(b"abc" < bytes(b"ab"), False)
    912         self.assertEqual(b"abc" <= bytes(b"ab"), False)
    913 
    914         self.assertEqual(bytes(b"abc") == b"abc", True)
    915         self.assertEqual(bytes(b"ab") != b"abc", True)
    916         self.assertEqual(bytes(b"ab") <= b"abc", True)
    917         self.assertEqual(bytes(b"ab") < b"abc", True)
    918         self.assertEqual(bytes(b"abc") >= b"ab", True)
    919         self.assertEqual(bytes(b"abc") > b"ab", True)
    920 
    921         self.assertEqual(bytes(b"abc") != b"abc", False)
    922         self.assertEqual(bytes(b"ab") == b"abc", False)
    923         self.assertEqual(bytes(b"ab") > b"abc", False)
    924         self.assertEqual(bytes(b"ab") >= b"abc", False)
    925         self.assertEqual(bytes(b"abc") < b"ab", False)
    926         self.assertEqual(bytes(b"abc") <= b"ab", False)
    927 
    928     @test.test_support.requires_docstrings
    929     def test_doc(self):
    930         self.assertIsNotNone(bytearray.__doc__)
    931         self.assertTrue(bytearray.__doc__.startswith("bytearray("), bytearray.__doc__)
    932         self.assertIsNotNone(bytes.__doc__)
    933         self.assertTrue(bytes.__doc__.startswith("bytes("), bytes.__doc__)
    934 
    935     def test_from_bytearray(self):
    936         sample = bytes(b"Hello world\n\x80\x81\xfe\xff")
    937         buf = memoryview(sample)
    938         b = bytearray(buf)
    939         self.assertEqual(b, bytearray(sample))
    940 
    941     @check_bytes_warnings
    942     def test_to_str(self):
    943         self.assertEqual(str(b''), "b''")
    944         self.assertEqual(str(b'x'), "b'x'")
    945         self.assertEqual(str(b'\x80'), "b'\\x80'")
    946         self.assertEqual(str(bytearray(b'')), "bytearray(b'')")
    947         self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')")
    948         self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')")
    949 
    950     def test_literal(self):
    951         tests =  [
    952             (b"Wonderful spam", "Wonderful spam"),
    953             (br"Wonderful spam too", "Wonderful spam too"),
    954             (b"\xaa\x00\000\200", "\xaa\x00\000\200"),
    955             (br"\xaa\x00\000\200", r"\xaa\x00\000\200"),
    956         ]
    957         for b, s in tests:
    958             self.assertEqual(b, bytearray(s, 'latin-1'))
    959         for c in range(128, 256):
    960             self.assertRaises(SyntaxError, eval,
    961                               'b"%s"' % chr(c))
    962 
    963     def test_translate(self):
    964         b = b'hello'
    965         ba = bytearray(b)
    966         rosetta = bytearray(range(0, 256))
    967         rosetta[ord('o')] = ord('e')
    968         c = b.translate(rosetta, b'l')
    969         self.assertEqual(b, b'hello')
    970         self.assertEqual(c, b'hee')
    971         c = ba.translate(rosetta, b'l')
    972         self.assertEqual(ba, b'hello')
    973         self.assertEqual(c, b'hee')
    974         c = b.translate(None, b'e')
    975         self.assertEqual(c, b'hllo')
    976         c = ba.translate(None, b'e')
    977         self.assertEqual(c, b'hllo')
    978         self.assertRaises(TypeError, b.translate, None, None)
    979         self.assertRaises(TypeError, ba.translate, None, None)
    980 
    981     def test_split_bytearray(self):
    982         self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b'])
    983 
    984     def test_rsplit_bytearray(self):
    985         self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b'])
    986 
    987     # Optimizations:
    988     # __iter__? (optimization)
    989     # __reversed__? (optimization)
    990 
    991     # XXX More string methods?  (Those that don't use character properties)
    992 
    993     # There are tests in string_tests.py that are more
    994     # comprehensive for things like split, partition, etc.
    995     # Unfortunately they are all bundled with tests that
    996     # are not appropriate for bytes
    997 
    998     # I've started porting some of those into bytearray_tests.py, we should port
    999     # the rest that make sense (the code can be cleaned up to use modern
   1000     # unittest methods at the same time).
   1001 
   1002 class BytearrayPEP3137Test(unittest.TestCase,
   1003                        test.buffer_tests.MixinBytesBufferCommonTests):
   1004     def marshal(self, x):
   1005         return bytearray(x)
   1006 
   1007     def test_returns_new_copy(self):
   1008         val = self.marshal(b'1234')
   1009         # On immutable types these MAY return a reference to themselves
   1010         # but on mutable types like bytearray they MUST return a new copy.
   1011         for methname in ('zfill', 'rjust', 'ljust', 'center'):
   1012             method = getattr(val, methname)
   1013             newval = method(3)
   1014             self.assertEqual(val, newval)
   1015             self.assertTrue(val is not newval,
   1016                             methname+' returned self on a mutable object')
   1017         for expr in ('val.split()[0]', 'val.rsplit()[0]',
   1018                      'val.partition(".")[0]', 'val.rpartition(".")[2]',
   1019                      'val.splitlines()[0]', 'val.replace("", "")'):
   1020             newval = eval(expr)
   1021             self.assertEqual(val, newval)
   1022             self.assertTrue(val is not newval,
   1023                             expr+' returned val on a mutable object')
   1024 
   1025 class FixedStringTest(test.string_tests.BaseTest):
   1026 
   1027     def fixtype(self, obj):
   1028         if isinstance(obj, str):
   1029             return obj.encode("utf-8")
   1030         return super(FixedStringTest, self).fixtype(obj)
   1031 
   1032     # Currently the bytes containment testing uses a single integer
   1033     # value. This may not be the final design, but until then the
   1034     # bytes section with in a bytes containment not valid
   1035     def test_contains(self):
   1036         pass
   1037     def test_expandtabs(self):
   1038         pass
   1039     def test_upper(self):
   1040         pass
   1041     def test_lower(self):
   1042         pass
   1043     def test_hash(self):
   1044         # XXX check this out
   1045         pass
   1046 
   1047 
   1048 class ByteArrayAsStringTest(FixedStringTest):
   1049     type2test = bytearray
   1050 
   1051 
   1052 class ByteArraySubclass(bytearray):
   1053     pass
   1054 
   1055 class ByteArraySubclassTest(unittest.TestCase):
   1056 
   1057     def test_basic(self):
   1058         self.assertTrue(issubclass(ByteArraySubclass, bytearray))
   1059         self.assertIsInstance(ByteArraySubclass(), bytearray)
   1060 
   1061         a, b = b"abcd", b"efgh"
   1062         _a, _b = ByteArraySubclass(a), ByteArraySubclass(b)
   1063 
   1064         # test comparison operators with subclass instances
   1065         self.assertTrue(_a == _a)
   1066         self.assertTrue(_a != _b)
   1067         self.assertTrue(_a < _b)
   1068         self.assertTrue(_a <= _b)
   1069         self.assertTrue(_b >= _a)
   1070         self.assertTrue(_b > _a)
   1071         self.assertTrue(_a is not a)
   1072 
   1073         # test concat of subclass instances
   1074         self.assertEqual(a + b, _a + _b)
   1075         self.assertEqual(a + b, a + _b)
   1076         self.assertEqual(a + b, _a + b)
   1077 
   1078         # test repeat
   1079         self.assertTrue(a*5 == _a*5)
   1080 
   1081     def test_join(self):
   1082         # Make sure join returns a NEW object for single item sequences
   1083         # involving a subclass.
   1084         # Make sure that it is of the appropriate type.
   1085         s1 = ByteArraySubclass(b"abcd")
   1086         s2 = bytearray().join([s1])
   1087         self.assertTrue(s1 is not s2)
   1088         self.assertTrue(type(s2) is bytearray, type(s2))
   1089 
   1090         # Test reverse, calling join on subclass
   1091         s3 = s1.join([b"abcd"])
   1092         self.assertTrue(type(s3) is bytearray)
   1093 
   1094     def test_pickle(self):
   1095         a = ByteArraySubclass(b"abcd")
   1096         a.x = 10
   1097         a.y = ByteArraySubclass(b"efgh")
   1098         for proto in range(pickle.HIGHEST_PROTOCOL + 1):
   1099             b = pickle.loads(pickle.dumps(a, proto))
   1100             self.assertNotEqual(id(a), id(b))
   1101             self.assertEqual(a, b)
   1102             self.assertEqual(a.x, b.x)
   1103             self.assertEqual(a.y, b.y)
   1104             self.assertEqual(type(a), type(b))
   1105             self.assertEqual(type(a.y), type(b.y))
   1106 
   1107     def test_copy(self):
   1108         a = ByteArraySubclass(b"abcd")
   1109         a.x = 10
   1110         a.y = ByteArraySubclass(b"efgh")
   1111         for copy_method in (copy.copy, copy.deepcopy):
   1112             b = copy_method(a)
   1113             self.assertNotEqual(id(a), id(b))
   1114             self.assertEqual(a, b)
   1115             self.assertEqual(a.x, b.x)
   1116             self.assertEqual(a.y, b.y)
   1117             self.assertEqual(type(a), type(b))
   1118             self.assertEqual(type(a.y), type(b.y))
   1119 
   1120     def test_init_override(self):
   1121         class subclass(bytearray):
   1122             def __init__(self, newarg=1, *args, **kwargs):
   1123                 bytearray.__init__(self, *args, **kwargs)
   1124         x = subclass(4, source=b"abcd")
   1125         self.assertEqual(x, b"abcd")
   1126         x = subclass(newarg=4, source=b"abcd")
   1127         self.assertEqual(x, b"abcd")
   1128 
   1129 def test_main():
   1130     #test.test_support.run_unittest(BytesTest)
   1131     #test.test_support.run_unittest(AssortedBytesTest)
   1132     #test.test_support.run_unittest(BytesAsStringTest)
   1133     test.test_support.run_unittest(
   1134         ByteArrayTest,
   1135         ByteArrayAsStringTest,
   1136         ByteArraySubclassTest,
   1137         BytearrayPEP3137Test)
   1138 
   1139 if __name__ == "__main__":
   1140     test_main()
   1141