Exemplo n.º 1
0
def test_typechecks_type_another() -> None:
    type_w = clone_type(UInt16, name='Word')
    type_f = clone_type(Float, name='Float')
    value_w = type_w(0x1234)

    assert not isinstance(type_w, type_f)
    assert not issubclass(type_w, type_f)

    assert not isinstance(value_w, type_f)
    with raises(TypeError):
        issubclass(value_w, type_f)  # type: ignore
Exemplo n.º 2
0
def test_eq() -> None:
    assert UInt16 == UInt16  # pylint: disable=comparison-with-itself
    assert UInt16 != UInt8
    assert UInt16 != Int16
    assert UInt16 != Float
    assert UInt16 != int

    clone16 = clone_type(UInt16, name='UInt')
    assert UInt16 == clone16

    clone8 = clone_type(UInt8)
    assert UInt16 != clone8
Exemplo n.º 3
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'
Exemplo n.º 4
0
def test_clone() -> None:
    clone = clone_type(Float)
    data = clone(0)
    assert clone is not Float
    assert sizeof(clone) == 4
    assert type_name(clone) == 'float'
    assert data == 0.0

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

    clone3 = clone_type(clone2)
    assert type_name(clone3) == 'Float'
Exemplo n.º 5
0
def test_clone() -> None:
    bytes_t = Bytes[Literal[5]]
    clone = clone_type(bytes_t)
    data = clone(0)
    assert clone is not bytes_t
    assert sizeof(clone) == 5
    assert type_name(clone) == 'char[5]'
    assert bytes(data) == b'\x00\x00\x00\x00\x00'

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

    clone3 = clone_type(clone2)
    assert type_name(clone3) == 'str_5'
Exemplo n.º 6
0
def test_clone() -> None:
    ptr_t = Pointer16[UInt32]
    clone = clone_type(ptr_t)
    data = clone(0)
    assert clone is not ptr_t
    assert sizeof(clone) == 2
    assert clone.int_type() is UInt16
    assert clone.ref_type() is UInt32
    assert type_name(clone) == 'uint32_t *'
    assert bytes(data) == b'\x00\x00'

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

    clone3 = clone_type(clone2)
    assert type_name(clone3) == 'PUINT32'
Exemplo n.º 7
0
def test_eq() -> None:
    class StructA(Struct):
        field1: UInt8
        field2: UInt16

    class StructB(Struct):
        field1: UInt16
        field2: UInt8

    class StructC(Struct):
        field2: UInt16
        field1: UInt8

    class StructD(Struct):
        field1: UInt8
        field2: UInt16
        field3: UInt16

    uint16_clone = clone_type(UInt16, name='UInt')

    class StructE(Struct):
        field1 = UInt8()
        field2 = uint16_clone()

    assert StructA == StructA  # pylint: disable=comparison-with-itself
    assert StructA != StructB
    assert StructA != StructC
    assert StructA != StructD
    assert StructA == StructE
    assert StructB != StructC

    assert StructA != UInt8
    assert StructA != Struct
    assert StructA != type
Exemplo n.º 8
0
def test_eq() -> None:
    class UnionA(Union):
        field1: UInt8
        field2: UInt16

    class UnionB(Union):
        field1: UInt16
        field2: UInt8

    class UnionC(Union):
        field2: UInt16
        field1: UInt8

    class UnionD(Union):
        field1: UInt8
        field2: UInt16
        field3: UInt16

    uint16_clone = clone_type(UInt16, name='UInt')

    class UnionE(Union):
        field1 = UInt8()
        field2 = uint16_clone()

    assert UnionA == UnionA  # pylint: disable=comparison-with-itself
    assert UnionA != UnionB
    assert UnionA == UnionC
    assert UnionA != UnionD
    assert UnionA == UnionE
    assert UnionB != UnionC

    assert UnionA != UInt8
    assert UnionA != Struct
    assert UnionA != type
Exemplo n.º 9
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
Exemplo n.º 10
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
Exemplo n.º 11
0
def test_typechecks_meta_another() -> None:
    clone16 = clone_type(UInt16, name='Word')
    value = clone16(0x1234)

    assert not isinstance(Integer, Real)
    assert not issubclass(Integer, Real)

    assert not isinstance(clone16, Real)
    assert not issubclass(clone16, Real)

    assert not isinstance(value, Real)
    with raises(TypeError):
        issubclass(value, Real)  # type: ignore
Exemplo n.º 12
0
def test_typechecks_type() -> None:
    clone16 = clone_type(UInt16, name='Word')
    value = clone16(0x1234)

    assert not isinstance(clone16, UInt16)
    assert issubclass(clone16, UInt16)

    assert isinstance(value, UInt16)
    with raises(TypeError):
        issubclass(value, UInt16)  # type: ignore

    assert not isinstance(b'string', UInt16)
    with raises(TypeError):
        issubclass(b'string', UInt16)  # type: ignore
Exemplo n.º 13
0
def test_clone() -> None:
    class Data(Union):
        u16: UInt16
        u8: UInt8

    clone = clone_type(Data)
    data = clone(0)
    assert clone is not Data
    assert sizeof(clone) == 2
    assert type_name(clone) == 'Data'
    assert tuple(i for i in clone) == ('u16', 'u8')
    assert offsetof(clone, 'u16') == 0
    assert 'u32' not in clone
    assert bytes(data) == b'\x00\x00'
Exemplo n.º 14
0
def test_clone() -> None:
    class SomeStruct(Struct):
        field1: UInt8
        field2: UInt16
        field3: UInt16

    clone = clone_type(SomeStruct)
    data = clone(0)
    assert clone is not SomeStruct
    assert sizeof(clone) == 5
    assert type_name(clone) == 'SomeStruct'
    assert tuple(i for i in clone) == ('field1', 'field2', 'field3')
    assert offsetof(clone, 'field3') == 3
    assert 'field4' not in clone
    assert bytes(data) == b'\x00\x00\x00\x00\x00'
Exemplo n.º 15
0
def test_typechecks_meta() -> None:
    clone16 = clone_type(UInt16, name='Word')
    value = clone16(0x1234)

    assert not isinstance(Integer, Integer)
    assert issubclass(Integer, Integer)

    assert not isinstance(clone16, Integer)
    assert issubclass(clone16, Integer)

    assert isinstance(value, Integer)
    with raises(TypeError):
        issubclass(value, Integer)  # type: ignore

    assert not isinstance('string', Integer)
    with raises(TypeError):
        issubclass('string', Integer)  # type: ignore
Exemplo n.º 16
0
def test_clone_bad() -> None:
    with raises(TypeError):
        _ = clone_type('Invalid value')  # type: ignore
Exemplo n.º 17
0
def test_clone_bad_name() -> None:
    with raises(TypeError):
        _ = clone_type(UInt16, name=123)  # type: ignore