예제 #1
0
def test_create_new() -> None:
    data = Bytes(5, b'\x11\x22\x33\x44\x55')
    bytes_t = typeof(data)
    assert bytes_t.length() == 5
    assert sizeof(bytes_t) == 5
    assert data.length() == 5
    assert sizeof(data) == 5
    assert len(data) == 5
예제 #2
0
def test_typechecks_type_another() -> None:
    bytes4_t = typeof(Bytes(4))
    bytes8_t = typeof(Bytes(8))
    bytes4_v = Bytes(4, b'1234')

    assert not isinstance(bytes4_t, bytes8_t)
    assert not issubclass(bytes4_t, bytes8_t)

    assert not isinstance(bytes4_v, bytes8_t)
    with raises(TypeError):
        issubclass(bytes4_v, bytes8_t)  # type: ignore
예제 #3
0
def test_eq() -> None:
    bytes16 = typeof(Bytes(16))
    bytes8 = typeof(Bytes(8))
    bytes16_clone = clone_type(bytes16, name='bytes16')

    assert bytes16 == bytes16  # pylint: disable=comparison-with-itself
    assert bytes16 != bytes8
    assert bytes16 == bytes16_clone

    assert bytes16 != UInt16
    assert bytes16 != Bytes
    assert bytes16 != bytes
예제 #4
0
def test_typechecks_type() -> None:
    bytes_t = typeof(Bytes(4))
    bytes_v = Bytes(4, b'1234')

    assert not isinstance(Bytes, bytes_t)
    assert not issubclass(Bytes, bytes_t)

    assert not isinstance(bytes_t, bytes_t)
    assert issubclass(bytes_t, bytes_t)

    assert isinstance(bytes_v, bytes_t)
    with raises(TypeError):
        issubclass(bytes_v, bytes_t)  # type: ignore

    assert not isinstance(b'string', bytes_t)
    with raises(TypeError):
        issubclass(b'string', bytes_t)  # type: ignore
예제 #5
0
def test_member_change_another() -> None:
    class Container(Struct):
        field_b: Bytes[Literal[5]]
        field_u: UInt32

    inst = Container()
    another = Bytes(5, b'\x01\x23\x45\x67\x89')
    data = inst.field_b
    inst.field_b = another
    assert bytes(data) == b'\x01\x23\x45\x67\x89'
예제 #6
0
def test_init_bad_3() -> None:
    with raises(ValueError):
        _ = Bytes(5, b'too_many_bytes')
예제 #7
0
def test_init_bad_2() -> None:
    with raises(TypeError):
        _ = Bytes(5, 'Bad value')  # type: ignore
예제 #8
0
def test_init_another_2() -> None:
    data1 = Bytes(5, b'\x11\x22\x33\x44\x55')
    data2 = Bytes(5, data1)
    assert bytes(data2) == b'\x11\x22\x33\x44\x55'
예제 #9
0
def test_init_another_1() -> None:
    bytes_t = Bytes[Literal[5]]
    data1 = Bytes(5, b'\x11\x22\x33\x44\x55')
    data2 = bytes_t(data1)
    assert bytes(data2) == b'\x11\x22\x33\x44\x55'
예제 #10
0
def test_init_short_2() -> None:
    data = Bytes(5, b'\x11\x22\x33')
    assert bytes(data) == b'\x11\x22\x33\x00\x00'
예제 #11
0
def test_setitem() -> None:
    data = Bytes(6, b'abcdef')
    data[2] = b'X'
    assert bytes(data) == b'abXdef'
예제 #12
0
 class Container(Struct):
     field = Bytes(5)
예제 #13
0
def test_setitem_bad_len() -> None:
    data = Bytes(6, b'abcdef')
    with raises(ValueError):
        data[2] = b'XY'
예제 #14
0
def test_setitem_bad_index() -> None:
    data = Bytes(6, b'abcdef')
    with raises(IndexError):
        data[6] = b'X'
예제 #15
0
def test_init_bad_4() -> None:
    with raises(TypeError):
        _ = Bytes('5', b'\x11\x22\x33\x44\x55')  # type: ignore
예제 #16
0
def test_name_2() -> None:
    data = Bytes(5, b'abcde')
    bytes_t = typeof(data)
    assert type_name(bytes_t) == 'char[5]'
    assert type_name(data) == 'char[5]'
예제 #17
0
def test_getitem() -> None:
    data = Bytes(6, b'abcdef')
    assert data[2] == b'c'