PageRenderTime 94ms CodeModel.GetById 10ms app.highlight 74ms RepoModel.GetById 1ms app.codeStats 1ms

/Lib/test/test_bytes.py

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