Esempio n. 1
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
Esempio n. 2
0
def test_type_sizeof() -> None:
    class SomeStruct(Struct):
        field: UInt16

    data = SomeStruct()
    assert sizeof(SomeStruct.field) == 2
    assert sizeof(data.field) == 2
Esempio n. 3
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
Esempio n. 4
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
Esempio n. 5
0
def test_packed_size() -> None:
    class SomeStruct(Struct):
        field1: UInt8
        field2: UInt16

    data = SomeStruct(0)
    assert sizeof(SomeStruct) == 3
    assert sizeof(data) == 3
    assert bytes(data) == b'\x00\x00\x00'
Esempio n. 6
0
def test_packed_size() -> None:
    some_t = create_struct('SomeStruct', {
        'field1': UInt8,
        'field2': UInt16,
    })
    some = some_t(0)
    assert sizeof(some_t) == 3
    assert sizeof(some) == 3
    assert bytes(some) == b'\x00\x00\x00'
Esempio n. 7
0
def test_void_1() -> None:
    ptr_t = Pointer32[Void]
    assert ptr_t.int_type() is UInt32
    assert ptr_t.ref_type() is Void
    assert sizeof(ptr_t) == 4

    ptr = ptr_t(0x12)
    assert ptr.int_type() is UInt32
    assert ptr.ref_type() is Void
    assert sizeof(ptr) == 4
Esempio n. 8
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
Esempio n. 9
0
def test_sizeof() -> None:
    class SomeStruct(Struct):
        field = Array(UInt16, 4)

    data = SomeStruct(0)
    arr_t = SomeStruct.field
    arr = data.field
    assert sizeof(arr_t) == 8
    assert sizeof(arr) == 8
    assert arr_t.length() == 4
    assert arr.length() == 4
    assert len(arr) == 4
Esempio n. 10
0
def test_sizeof() -> None:
    class Pos(Struct):
        x: UInt16
        y: UInt16

    class Data(Union):
        u16: UInt16
        pos: Pos
        u8: UInt8

    data = Data(0)
    assert sizeof(Data) == 4
    assert sizeof(data) == 4
Esempio n. 11
0
def test_struct_modified_member_classvar() -> None:
    class SomeStruct(Struct):
        field1 = shifted(Array(UInt16, 3), 1)
        field2 = padded(Array(UInt8, 3), 2)

    assert sizeof(SomeStruct) == 12
    assert sizeof(SomeStruct.field1) == 6
    assert sizeof(SomeStruct.field2) == 3
    assert offsetof(SomeStruct, 'field1') == 1
    assert offsetof(SomeStruct, 'field2') == 7

    data = SomeStruct(((0x1122, 0x3344, 0x5566), (0x77, 0x88, 0x99)))
    assert bytes(data) == b'\x00\x22\x11\x44\x33\x66\x55\x77\x88\x99\x00\x00'
Esempio n. 12
0
def test_struct_modified_member_dynamic() -> None:
    struct_t = create_struct(
        'SomeStruct', {
            'field1': shifted(Array(UInt16, 3), 1),
            'field2': padded(Array(UInt8, 3), 2),
        })
    assert sizeof(struct_t) == 12
    assert sizeof(struct_t['field1']) == 6
    assert sizeof(struct_t['field2']) == 3
    assert offsetof(struct_t, 'field1') == 1
    assert offsetof(struct_t, 'field2') == 7

    data = struct_t(((0x1122, 0x3344, 0x5566), (0x77, 0x88, 0x99)))
    assert bytes(data) == b'\x00\x22\x11\x44\x33\x66\x55\x77\x88\x99\x00\x00'
Esempio n. 13
0
def test_sizeof() -> None:
    class Pos(Struct):
        x: UInt16
        y: UInt16

    data_t = create_union('Data', {
        'u16': UInt16,
        'pos': Pos,
        'u8': UInt8,
    })

    data = data_t(0)
    assert sizeof(data_t) == 4
    assert sizeof(data) == 4
Esempio n. 14
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
Esempio n. 15
0
def test_padded_annotation() -> None:
    class SomeStruct(Struct):
        field1: Annotated[UInt8, Padding(2)]
        field2: UInt16
        field3: Annotated[UInt16, Padding(3)]

    assert sizeof(SomeStruct) == 10
    assert sizeof(SomeStruct.field1) == 1
    assert sizeof(SomeStruct.field2) == 2
    assert sizeof(SomeStruct.field3) == 2
    assert offsetof(SomeStruct, 'field1') == 0
    assert offsetof(SomeStruct, 'field2') == 3
    assert offsetof(SomeStruct, 'field3') == 5

    data = SomeStruct((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x12\x00\x00\x56\x34\x9a\x78\x00\x00\x00'
Esempio n. 16
0
def test_padded_classvar() -> None:
    class SomeStruct(Struct):
        field1 = padded(UInt8, 2)
        field2 = UInt16()
        field3 = padded(UInt16, 3)

    assert sizeof(SomeStruct) == 10
    assert sizeof(SomeStruct.field1) == 1
    assert sizeof(SomeStruct.field2) == 2
    assert sizeof(SomeStruct.field3) == 2
    assert offsetof(SomeStruct, 'field1') == 0
    assert offsetof(SomeStruct, 'field2') == 3
    assert offsetof(SomeStruct, 'field3') == 5

    data = SomeStruct((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x12\x00\x00\x56\x34\x9a\x78\x00\x00\x00'
Esempio n. 17
0
def test_shifted_classvar() -> None:
    class SomeStruct(Struct):
        field1 = shifted(UInt8, 1)
        field2 = UInt16()
        field3 = shifted(UInt16, 3)

    assert sizeof(SomeStruct) == 9
    assert sizeof(SomeStruct.field1) == 1
    assert sizeof(SomeStruct.field2) == 2
    assert sizeof(SomeStruct.field3) == 2
    assert offsetof(SomeStruct, 'field1') == 1
    assert offsetof(SomeStruct, 'field2') == 2
    assert offsetof(SomeStruct, 'field3') == 7

    data = SomeStruct((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x12\x56\x34\x00\x00\x00\x9a\x78'
Esempio n. 18
0
def test_sizeof_value() -> None:
    class SomeStruct(Struct):
        field1: UInt8
        field2: UInt16

    inst = SomeStruct(0)
    assert sizeof(inst) == 3
Esempio n. 19
0
def test_shifted_annotation() -> None:
    class SomeStruct(Struct):
        field1: Annotated[UInt8, Shift(1)]
        field2: UInt16
        field3: Annotated[UInt16, Shift(3)]

    assert sizeof(SomeStruct) == 9
    assert sizeof(SomeStruct.field1) == 1
    assert sizeof(SomeStruct.field2) == 2
    assert sizeof(SomeStruct.field3) == 2
    assert offsetof(SomeStruct, 'field1') == 1
    assert offsetof(SomeStruct, 'field2') == 2
    assert offsetof(SomeStruct, 'field3') == 7

    data = SomeStruct((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x12\x56\x34\x00\x00\x00\x9a\x78'
Esempio n. 20
0
def test_shifted_annotation() -> None:
    class Child1(Struct):
        field1_1: UInt16
        field1_2: UInt8
        field1_3: UInt8

    class Child2(Struct):
        field2_1: UInt8
        field2_2: UInt16
        field2_3: UInt8

    class Data(Union):
        child1: Child1
        child2: Annotated[Child2, Shift(1)]
        child3: Annotated[UInt16, Shift(1)]

    assert sizeof(Data) == 5
    assert offsetof(Data, 'child1') == 0
    assert offsetof(Data, 'child2') == 1
    assert offsetof(Data, 'child3') == 1

    data = Data(0)
    child1 = data.child1
    data.child2.field2_1 = 0x12
    assert child1.field1_1 == 0x1200
    assert data.child3 == 0x12
    assert bytes(data) == b'\x00\x12\x00\x00\x00'

    child1.field1_2 = 0x34
    assert child1.field1_2 == 0x34
    assert data.child3 == 0x3412
    assert bytes(data) == b'\x00\x12\x34\x00\x00'
Esempio n. 21
0
def test_modifiers_classvar() -> None:
    class SomeStruct(Struct):
        field1 = padded(shifted(UInt8, 2), 2)
        _pad = Padding(1)
        field2 = shifted(padded(UInt16, 1), 1)
        field3 = UInt16()

    assert sizeof(SomeStruct) == 12
    assert sizeof(SomeStruct.field1) == 1
    assert sizeof(SomeStruct.field2) == 2
    assert sizeof(SomeStruct.field3) == 2
    assert offsetof(SomeStruct, 'field1') == 2
    assert offsetof(SomeStruct, 'field2') == 7
    assert offsetof(SomeStruct, 'field3') == 10

    data = SomeStruct((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x00\x12\x00\x00\x00\x00\x56\x34\x00\x9a\x78'
Esempio n. 22
0
def test_padded() -> None:
    struct_t = create_struct(
        'SomeStruct', {
            'field1': padded(UInt8, 2),
            'field2': UInt16,
            'field3': padded(UInt16, 3),
        })
    assert sizeof(struct_t) == 10
    assert sizeof(struct_t['field1']) == 1
    assert sizeof(struct_t['field2']) == 2
    assert sizeof(struct_t['field3']) == 2
    assert offsetof(struct_t, 'field1') == 0
    assert offsetof(struct_t, 'field2') == 3
    assert offsetof(struct_t, 'field3') == 5

    data = struct_t((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x12\x00\x00\x56\x34\x9a\x78\x00\x00\x00'
Esempio n. 23
0
def test_modifiers_annotation() -> None:
    class SomeStruct(Struct):
        field1: Annotated[UInt8, Padding(2), Shift(2)]
        _pad: Padding[Literal[1]]
        field2: Annotated[UInt16, Shift(1), Padding(1)]
        field3: UInt16

    assert sizeof(SomeStruct) == 12
    assert sizeof(SomeStruct.field1) == 1
    assert sizeof(SomeStruct.field2) == 2
    assert sizeof(SomeStruct.field3) == 2
    assert offsetof(SomeStruct, 'field1') == 2
    assert offsetof(SomeStruct, 'field2') == 7
    assert offsetof(SomeStruct, 'field3') == 10

    data = SomeStruct((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x00\x12\x00\x00\x00\x00\x56\x34\x00\x9a\x78'
Esempio n. 24
0
def test_modifiers_classvar() -> None:
    struct_t = create_struct(
        'SomeStruct', {
            'field1': padded(shifted(UInt8, 2), 2),
            '_pad': Padding(1),
            'field2': shifted(UInt16, 2),
            'field3': UInt16(),
        })
    assert sizeof(struct_t) == 12
    assert sizeof(struct_t['field1']) == 1
    assert sizeof(struct_t['field2']) == 2
    assert sizeof(struct_t['field3']) == 2
    assert offsetof(struct_t, 'field1') == 2
    assert offsetof(struct_t, 'field2') == 8
    assert offsetof(struct_t, 'field3') == 10

    data = struct_t((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x00\x12\x00\x00\x00\x00\x00\x56\x34\x9a\x78'
Esempio n. 25
0
def test_padded_classvar() -> None:
    class Child(Struct):
        field1_1 = UInt16()
        field1_2 = UInt16()

    class Data(Union):
        child1 = padded(UInt8, 4)
        child2 = padded(UInt16, 1)
        child3 = Child()

    assert sizeof(Data) == 5
    assert sizeof(Data.child1) == 1
    assert sizeof(Data.child2) == 2
    assert sizeof(Data.child3) == 4

    data = Data(0)
    data.child2 = 0x1234
    assert data.child1 == 0x34
    assert bytes(data.child3) == b'\x34\x12\x00\x00'
Esempio n. 26
0
def test_padded_annotation() -> None:
    class Child(Struct):
        field1_1: UInt16
        field1_2: UInt16

    class Data(Union):
        child1: Annotated[UInt8, Padding(4)]
        child2: Annotated[UInt16, Padding(1)]
        child3: Child

    assert sizeof(Data) == 5
    assert sizeof(Data.child1) == 1
    assert sizeof(Data.child2) == 2
    assert sizeof(Data.child3) == 4

    data = Data(0)
    data.child2 = 0x1234
    assert data.child1 == 0x34
    assert bytes(data.child3) == b'\x34\x12\x00\x00'
Esempio n. 27
0
def test_padded() -> None:
    child_t = create_struct('Child', {
        'field1_1': UInt16,
        'field1_2': UInt16,
    })
    data_t = create_union(
        'Data', {
            'child1': padded(UInt8, 4),
            'child2': padded(UInt16, 1),
            'child3': child_t,
        })

    assert sizeof(data_t) == 5
    assert sizeof(data_t['child1']) == 1
    assert sizeof(data_t['child2']) == 2
    assert sizeof(data_t['child3']) == 4

    data = data_t(0)
    data['child2'] = 0x1234
    assert data['child1'] == 0x34
    assert bytes(data['child3']) == b'\x34\x12\x00\x00'
Esempio n. 28
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'
Esempio n. 29
0
def test_padding_classvar() -> None:
    class Child(Struct):
        field1 = UInt16()
        field2 = UInt8()
        field3 = UInt8()

    class Data(Union):
        child = Child()
        _pad = Padding(5)

    assert sizeof(Data) == 5
    data = Data(b'\x11\x22\x33\x44\x55')
    assert data.child.field3 == 0x44
    assert bytes(data) == b'\x11\x22\x33\x44\x55'
Esempio n. 30
0
def test_padding_annotation() -> None:
    class Child(Struct):
        field1: UInt16
        field2: UInt8
        field3: UInt8

    class Data(Union):
        child: Child
        _pad: Padding[Literal[5]]

    assert sizeof(Data) == 5
    data = Data(b'\x11\x22\x33\x44\x55')
    assert data.child.field3 == 0x44
    assert bytes(data) == b'\x11\x22\x33\x44\x55'