Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
0
def test_typechecks_type_another() -> None:
    ptr1 = Pointer32(SomeStruct, 0)
    ptr2 = Pointer32(UInt16, 0)
    ptr1_t = typeof(ptr1)
    ptr2_t = typeof(ptr2)

    assert not isinstance(ptr1_t, ptr2_t)
    assert not issubclass(ptr1_t, ptr2_t)

    assert not isinstance(ptr1, ptr2_t)
    with raises(TypeError):
        issubclass(ptr1, ptr2_t)  # type: ignore
Ejemplo n.º 4
0
def test_typechecks_type_another() -> None:
    arr1_v = Array(UInt16, 5)
    arr1_t = typeof(arr1_v)

    arr2_v = Array(UInt8, 3)
    arr2_t = typeof(arr2_v)

    assert not isinstance(arr2_t, arr1_t)
    assert not issubclass(arr2_t, arr1_t)

    assert not isinstance(arr2_v, arr1_t)
    with raises(TypeError):
        issubclass(arr2_v, arr1_t)  # type: ignore
Ejemplo n.º 5
0
def test_eq() -> None:
    arr1_t = typeof(Array(UInt16, 4))
    arr2_t = typeof(Array(UInt8, 4))
    arr3_t = typeof(Array(UInt8, 8))
    arr4_t = typeof(Array(UInt16, 4))
    assert arr1_t == arr1_t  # pylint: disable=comparison-with-itself
    assert arr1_t != arr2_t
    assert arr1_t != arr3_t
    assert arr1_t == arr4_t

    assert arr1_t != UInt16
    assert arr1_t != Array
    assert arr1_t != list
Ejemplo n.º 6
0
def test_create_getitem_value() -> None:
    bytes_t = Bytes[Literal[5]]
    data = bytes_t(b'\x11\x22\x33\x44\x55')
    assert sizeof(bytes_t) == 5
    assert typeof(data) is bytes_t
    assert sizeof(data) == 5
    assert len(data) == 5
Ejemplo n.º 7
0
def test_create_getitem_value() -> None:
    ptr_t = Pointer32[SomeStruct]
    ptr = ptr_t(0x1234)
    assert ptr.int_type() is UInt32
    assert ptr.ref_type() is SomeStruct
    assert sizeof(ptr) == 4
    assert typeof(ptr) is ptr_t
Ejemplo n.º 8
0
def test_create_new() -> None:
    ptr = Pointer32(SomeStruct, 0x1234)
    ptr_t = typeof(ptr)
    assert ptr_t.int_type() is UInt32
    assert ptr_t.ref_type() is SomeStruct
    assert sizeof(ptr_t) == 4
    assert sizeof(ptr) == 4
Ejemplo n.º 9
0
def test_typeof() -> None:
    class SomeStruct(Struct):
        field1: UInt8
        field2: UInt16

    data = SomeStruct(0)
    assert typeof(data) is SomeStruct
Ejemplo n.º 10
0
def test_typeof() -> None:
    class Data(Union):
        u16: UInt16
        u8: UInt8

    data = Data(0)
    assert typeof(data) is Data
Ejemplo n.º 11
0
def test_typeof_value() -> None:
    class SomeStruct(Struct):
        field1: UInt8
        field2: UInt16

    inst = SomeStruct(0)
    assert typeof(inst) is SomeStruct
Ejemplo n.º 12
0
def test_typeof_type() -> None:
    class SomeStruct(Struct):
        field1: UInt8
        field2: UInt16

    with raises(TypeError):
        _ = typeof(SomeStruct)  # type: ignore
Ejemplo n.º 13
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
Ejemplo n.º 14
0
def test_typeof() -> None:
    data_t = create_union('Data', {
        'u16': UInt16,
        'u8': UInt8,
    })

    data = data_t(0)
    assert typeof(data) is data_t
Ejemplo n.º 15
0
def test_typeof() -> None:
    some_t = create_struct('SomeStruct', {
        'field1': UInt8,
        'field2': UInt16,
    })

    data = some_t(0)
    assert typeof(data) is some_t
Ejemplo n.º 16
0
def test_void_2() -> None:
    ptr = Pointer32(Void, 0x12)
    assert ptr.int_type() is UInt32
    assert ptr.ref_type() is Void
    assert sizeof(ptr) == 4

    ptr_t = typeof(ptr)
    assert ptr_t.int_type() is UInt32
    assert ptr_t.ref_type() is Void
    assert sizeof(ptr_t) == 4
Ejemplo n.º 17
0
def test_typeof() -> None:
    class SomeStruct(Struct):
        field = Array(UInt16, 4)

    data = SomeStruct(0)
    arr_t = SomeStruct.field
    arr = data.field
    assert typeof(arr) is arr_t
    assert arr_t.item_type() is UInt16
    assert arr.item_type() is UInt16
Ejemplo n.º 18
0
def test_clone() -> None:
    arr_t = typeof(Array(UInt16, 4))
    clone = clone_type(arr_t)
    assert clone is not arr_t
    assert clone.item_type() is UInt16
    assert clone.length() == 4
    assert type_name(clone) == 'uint16_t[4]'

    clone2 = clone_type(clone, name='uint16x4')
    assert type_name(clone2) == 'uint16x4'

    clone3 = clone_type(clone2)
    assert type_name(clone3) == 'uint16x4'
Ejemplo n.º 19
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
Ejemplo n.º 20
0
def test_typechecks_type() -> None:
    ptr = Pointer32(SomeStruct, 0)
    ptr_t = typeof(ptr)

    assert not isinstance(Pointer32, ptr_t)
    assert not issubclass(Pointer32, ptr_t)

    assert not isinstance(ptr_t, ptr_t)
    assert issubclass(ptr_t, ptr_t)

    assert isinstance(ptr, ptr_t)
    with raises(TypeError):
        issubclass(ptr, ptr_t)  # type: ignore

    assert not isinstance(0, ptr_t)
    with raises(TypeError):
        issubclass(0, ptr_t)  # type: ignore
Ejemplo n.º 21
0
def test_typechecks_type() -> None:
    arr_v = Array(UInt16, 5)
    arr_t = typeof(arr_v)

    assert not isinstance(Array, arr_t)
    assert not issubclass(Array, arr_t)

    assert not isinstance(arr_t, arr_t)
    assert issubclass(arr_t, arr_t)

    assert isinstance(arr_v, arr_t)
    with raises(TypeError):
        issubclass(arr_v, arr_t)  # type: ignore

    assert not isinstance('string', arr_t)
    with raises(TypeError):
        issubclass('string', arr_t)  # type: ignore
Ejemplo n.º 22
0
def test_eq() -> None:
    uint16_clone = clone_type(UInt16, name='UInt')

    ptr_a = Pointer16[UInt16]
    ptr_b = Pointer16[UInt32]
    ptr_c = Pointer32[UInt16]
    ptr_d = clone_type(ptr_a, name='Ptr')
    ptr_e = typeof(Pointer16(uint16_clone))

    assert ptr_a == ptr_a  # pylint: disable=comparison-with-itself
    assert ptr_a != ptr_b
    assert ptr_a != ptr_c
    assert ptr_a == ptr_d
    assert ptr_a == ptr_e

    assert ptr_a != Pointer16
    assert ptr_a != UInt16
    assert ptr_a != type
Ejemplo n.º 23
0
def test_type_typeof() -> None:
    class SomeStruct(Struct):
        field: UInt16

    data = SomeStruct()
    assert typeof(data.field) == UInt16
Ejemplo n.º 24
0
def test_typeof_bad() -> None:
    with raises(TypeError):
        _ = typeof('Invalid value')  # type: ignore
Ejemplo n.º 25
0
def test_name() -> None:
    data = Array(UInt16, 4)
    arr_t = typeof(data)
    assert type_name(arr_t) == 'uint16_t[4]'
    assert type_name(data) == 'uint16_t[4]'
Ejemplo n.º 26
0
def test_name_fwdred() -> None:
    data = Pointer32(ForwardRef, 0)
    ptr_t = typeof(data)
    assert type_name(ptr_t) == 'void *'
    assert type_name(data) == 'void *'
Ejemplo n.º 27
0
def test_name_void() -> None:
    data = Pointer32(Void, 0)
    ptr_t = typeof(data)
    assert type_name(ptr_t) == 'void *'
    assert type_name(data) == 'void *'
Ejemplo n.º 28
0
def test_name_2() -> None:
    data = Pointer32(UInt16, 0)
    ptr_t = typeof(data)
    assert type_name(ptr_t) == 'uint16_t *'
    assert type_name(data) == 'uint16_t *'
Ejemplo n.º 29
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]'