test pybytearraycheck crashes checknull test pybytearraycheckexact crashes checknull test pybytearrayfromstringandsize test pybytearrayfromobject crashes fromobjectnull test pybytearraysize crashes sizeb abc crashes sizeobject crashes sizenull test pybytearrayasstring asstring testcapi bytearrayasstring self assertequalasstringbytearrayb abc 4 b abc0 self assertequalasstringbytearraysubclassb abc 4 b abc0 self assertequalasstringbytearrayb abc0def 8 b abc0def0 crashes asstringb abc 0 crashes asstringobject 0 crashes asstringnull 0 def testconcatself crashes concatnull bytearrayb def crashes concatbytearrayb abc null crashes concatnull object crashes concatobject null test pybytearrayresize resize testcapi bytearrayresize ba bytearrayb abcdef self assertequalresizeba 3 0 self assertequalba bytearrayb abc self assertequalresizeba 10 0 self assertequallenba 10 self assertequalba 3 bytearrayb abc self assertequalresizeba 220 0 self assertequallenba 220 self assertequalba 3 bytearrayb abc self assertequalresizeba 0 0 self assertequalba bytearray ba bytearraysubclassb abcdef self assertequalresizeba 3 0 self assertequalba bytearrayb abc self assertraisesmemoryerror resize bytearray pyssizetmax self assertraisesmemoryerror resize bytearray1000 pyssizetmax crashes resizebytearrayb abc 1 crashes resizeb abc 0 crashes resizeobject 0 crashes resizenull 0 if name main unittest main test pybytearray_check crashes check null test pybytearray_checkexact crashes check null test pybytearray_fromstringandsize test pybytearray_fromobject crashes fromobject null test pybytearray_size crashes size b abc crashes size object crashes size null test pybytearray_asstring crashes asstring b abc 0 crashes asstring object 0 crashes asstring null 0 test pybytearray_concat crashes concat null bytearray b def crashes concat bytearray b abc null crashes concat null object crashes concat object null test pybytearray_resize crashes resize bytearray b abc 1 crashes resize b abc 0 crashes resize object 0 crashes resize null 0
import unittest
from test.support import import_helper

_testcapi = import_helper.import_module('_testcapi')
from _testcapi import PY_SSIZE_T_MIN, PY_SSIZE_T_MAX

NULL = None

class ByteArraySubclass(bytearray):
    pass

class BytesLike:
    def __init__(self, value):
        self.value = value
    def __bytes__(self):
        return self.value


class CAPITest(unittest.TestCase):
    def test_check(self):
        
        check = _testcapi.bytearray_check
        self.assertTrue(check(bytearray(b'abc')))
        self.assertFalse(check(b'abc'))
        self.assertTrue(check(ByteArraySubclass(b'abc')))
        self.assertFalse(check(BytesLike(b'abc')))
        self.assertFalse(check(3))
        self.assertFalse(check([]))
        self.assertFalse(check(object()))

        

    def test_checkexact(self):
        
        check = _testcapi.bytearray_checkexact
        self.assertTrue(check(bytearray(b'abc')))
        self.assertFalse(check(b'abc'))
        self.assertFalse(check(ByteArraySubclass(b'abc')))
        self.assertFalse(check(BytesLike(b'abc')))
        self.assertFalse(check(3))
        self.assertFalse(check([]))
        self.assertFalse(check(object()))

        

    def test_fromstringandsize(self):
        
        fromstringandsize = _testcapi.bytearray_fromstringandsize

        self.assertEqual(fromstringandsize(b'abc'), bytearray(b'abc'))
        self.assertEqual(fromstringandsize(b'abc', 2), bytearray(b'ab'))
        self.assertEqual(fromstringandsize(b'abc\0def'), bytearray(b'abc\0def'))
        self.assertEqual(fromstringandsize(b'', 0), bytearray())
        self.assertEqual(fromstringandsize(NULL, 0), bytearray())
        self.assertEqual(len(fromstringandsize(NULL, 3)), 3)
        self.assertRaises(MemoryError, fromstringandsize, NULL, PY_SSIZE_T_MAX)

        self.assertRaises(SystemError, fromstringandsize, b'abc', -1)
        self.assertRaises(SystemError, fromstringandsize, b'abc', PY_SSIZE_T_MIN)
        self.assertRaises(SystemError, fromstringandsize, NULL, -1)
        self.assertRaises(SystemError, fromstringandsize, NULL, PY_SSIZE_T_MIN)

    def test_fromobject(self):
        
        fromobject = _testcapi.bytearray_fromobject

        self.assertEqual(fromobject(b'abc'), bytearray(b'abc'))
        self.assertEqual(fromobject(bytearray(b'abc')), bytearray(b'abc'))
        self.assertEqual(fromobject(ByteArraySubclass(b'abc')), bytearray(b'abc'))
        self.assertEqual(fromobject([97, 98, 99]), bytearray(b'abc'))
        self.assertEqual(fromobject(3), bytearray(b'\0\0\0'))
        self.assertRaises(TypeError, fromobject, BytesLike(b'abc'))
        self.assertRaises(TypeError, fromobject, 'abc')
        self.assertRaises(TypeError, fromobject, object())

        

    def test_size(self):
        
        size = _testcapi.bytearray_size

        self.assertEqual(size(bytearray(b'abc')), 3)
        self.assertEqual(size(ByteArraySubclass(b'abc')), 3)

        
        
        

    def test_asstring(self):
        
        asstring = _testcapi.bytearray_asstring

        self.assertEqual(asstring(bytearray(b'abc'), 4), b'abc\0')
        self.assertEqual(asstring(ByteArraySubclass(b'abc'), 4), b'abc\0')
        self.assertEqual(asstring(bytearray(b'abc\0def'), 8), b'abc\0def\0')

        
        
        

    def test_concat(self):
        
        concat = _testcapi.bytearray_concat

        ba = bytearray(b'abc')
        self.assertEqual(concat(ba, b'def'), bytearray(b'abcdef'))
        self.assertEqual(ba, b'abc')

        self.assertEqual(concat(b'abc', b'def'), bytearray(b'abcdef'))
        self.assertEqual(concat(b'a\0b', b'c\0d'), bytearray(b'a\0bc\0d'))
        self.assertEqual(concat(bytearray(b'abc'), b'def'), bytearray(b'abcdef'))
        self.assertEqual(concat(b'abc', bytearray(b'def')), bytearray(b'abcdef'))
        self.assertEqual(concat(bytearray(b'abc'), b''), bytearray(b'abc'))
        self.assertEqual(concat(b'', bytearray(b'def')), bytearray(b'def'))
        self.assertEqual(concat(memoryview(b'xabcy')[1:4], b'def'),
                         bytearray(b'abcdef'))
        self.assertEqual(concat(b'abc', memoryview(b'xdefy')[1:4]),
                         bytearray(b'abcdef'))

        self.assertRaises(TypeError, concat, memoryview(b'axbycz')[::2], b'def')
        self.assertRaises(TypeError, concat, b'abc', memoryview(b'dxeyfz')[::2])
        self.assertRaises(TypeError, concat, b'abc', 'def')
        self.assertRaises(TypeError, concat, 'abc', b'def')
        self.assertRaises(TypeError, concat, 'abc', 'def')
        self.assertRaises(TypeError, concat, [], b'def')
        self.assertRaises(TypeError, concat, b'abc', [])
        self.assertRaises(TypeError, concat, [], [])

        
        
        
        

    def test_resize(self):
        
        resize = _testcapi.bytearray_resize

        ba = bytearray(b'abcdef')
        self.assertEqual(resize(ba, 3), 0)
        self.assertEqual(ba, bytearray(b'abc'))
        self.assertEqual(resize(ba, 10), 0)
        self.assertEqual(len(ba), 10)
        self.assertEqual(ba[:3], bytearray(b'abc'))
        self.assertEqual(resize(ba, 2**20), 0)
        self.assertEqual(len(ba), 2**20)
        self.assertEqual(ba[:3], bytearray(b'abc'))
        self.assertEqual(resize(ba, 0), 0)
        self.assertEqual(ba, bytearray())

        ba = ByteArraySubclass(b'abcdef')
        self.assertEqual(resize(ba, 3), 0)
        self.assertEqual(ba, bytearray(b'abc'))

        self.assertRaises(MemoryError, resize, bytearray(), PY_SSIZE_T_MAX)
        self.assertRaises(MemoryError, resize, bytearray(1000), PY_SSIZE_T_MAX)

        
        
        
        


if __name__ == "__main__":
    unittest.main()