Ejemplo n.º 1
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'
Ejemplo n.º 2
0
def test_offsetof_type() -> None:
    class SomeStruct(Struct):
        field1: UInt8
        field2: UInt16
        field3: UInt16

    assert offsetof(SomeStruct, 'field1') == 0
    assert offsetof(SomeStruct, 'field2') == 1
    assert offsetof(SomeStruct, 'field3') == 3
    with raises(KeyError):
        offsetof(SomeStruct, 'bad_field')
Ejemplo n.º 3
0
def test_offsetof_value() -> None:
    class SomeStruct(Struct):
        field1: UInt8
        field2: UInt16
        field3: UInt16

    data = SomeStruct(0)
    assert offsetof(data, 'field1') == 0
    assert offsetof(data, 'field2') == 1
    assert offsetof(data, 'field3') == 3
    with raises(KeyError):
        offsetof(data, 'bad_field')
Ejemplo n.º 4
0
def test_offsetof_type() -> None:
    some_t = create_struct('SomeStruct', {
        'field1': UInt8,
        'field2': UInt16,
        'field3': UInt16,
    })

    assert offsetof(some_t, 'field1') == 0
    assert offsetof(some_t, 'field2') == 1
    assert offsetof(some_t, 'field3') == 3
    with raises(KeyError):
        offsetof(some_t, 'bad_field')
Ejemplo n.º 5
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'
Ejemplo n.º 6
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'
Ejemplo n.º 7
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'
Ejemplo n.º 8
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'
Ejemplo n.º 9
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'
Ejemplo n.º 10
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'
Ejemplo n.º 11
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'
Ejemplo n.º 12
0
def test_padding_classvar() -> None:
    class SomeStruct(Struct):
        _pad1 = Padding(2)
        field1 = UInt8()
        field2 = UInt16()
        _pad2 = Padding(1)
        _pad3 = Padding(1)
        field3 = UInt16()
        _pad4 = Padding(2)

    assert sizeof(SomeStruct) == 11
    assert offsetof(SomeStruct, 'field1') == 2
    assert offsetof(SomeStruct, 'field2') == 3
    assert offsetof(SomeStruct, 'field3') == 7

    data = SomeStruct((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x00\x12\x56\x34\x00\x00\x9a\x78\x00\x00'
Ejemplo n.º 13
0
def test_padding_annotation() -> None:
    class SomeStruct(Struct):
        _pad1: Padding[Literal[2]]
        field1: UInt8
        field2: UInt16
        _pad2: Padding[Literal[1]]
        _pad3: Padding[Literal[1]]
        field3: UInt16
        _pad4: Padding[Literal[2]]

    assert sizeof(SomeStruct) == 11
    assert offsetof(SomeStruct, 'field1') == 2
    assert offsetof(SomeStruct, 'field2') == 3
    assert offsetof(SomeStruct, 'field3') == 7

    data = SomeStruct((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x00\x12\x56\x34\x00\x00\x9a\x78\x00\x00'
Ejemplo n.º 14
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'
Ejemplo n.º 15
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'
Ejemplo n.º 16
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'
Ejemplo n.º 17
0
def test_padding() -> None:
    struct_t = create_struct(
        'SomeStruct', {
            '_pad1': Padding(2),
            'field1': UInt8,
            'field2': UInt16,
            '_pad2': Padding(1),
            '_pad3': Padding(1),
            'field3': UInt16,
            '_pad4': Padding(2),
        })

    assert sizeof(struct_t) == 11
    assert offsetof(struct_t, 'field1') == 2
    assert offsetof(struct_t, 'field2') == 3
    assert offsetof(struct_t, 'field3') == 7

    data = struct_t((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x00\x12\x56\x34\x00\x00\x9a\x78\x00\x00'
Ejemplo n.º 18
0
def test_shifted() -> None:
    child1_t = create_struct('Child1', {
        'field1_1': UInt16,
        'field1_2': UInt8,
        'field1_3': UInt8,
    })
    child2_t = create_struct('Child2', {
        'field2_1': UInt8,
        'field2_2': UInt16,
        'field2_3': UInt8,
    })
    data_t = create_union(
        'Data', {
            'child1': child1_t,
            'child2': shifted(child2_t, 1),
            'child3': shifted(UInt16, 1),
        })
    assert sizeof(data_t) == 5
    assert offsetof(data_t, 'child1') == 0
    assert offsetof(data_t, 'child2') == 1
    assert offsetof(data_t, 'child3') == 1

    data: Any = data_t(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 data['child2']['field2_2'] == 0x34
    assert data['child3'] == 0x3412
    assert bytes(data) == b'\x00\x12\x34\x00\x00'

    child1['field1_1'] = 0x5678
    assert data['child2']['field2_1'] == 0x56
    assert data['child3'] == 0x3456
    assert bytes(data) == b'\x78\x56\x34\x00\x00'
Ejemplo n.º 19
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'
Ejemplo n.º 20
0
def test_shifted_classvar() -> 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 = shifted(Child2, 1)
        child3 = shifted(UInt16, 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 data.child2.field2_2 == 0x34
    assert data.child3 == 0x3412
    assert bytes(data) == b'\x00\x12\x34\x00\x00'

    child1.field1_1 = 0x5678
    assert data.child2.field2_1 == 0x56
    assert data.child3 == 0x3456
    assert bytes(data) == b'\x78\x56\x34\x00\x00'
Ejemplo n.º 21
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'
Ejemplo n.º 22
0
def test_offsetof_bad() -> None:
    with raises(TypeError):
        _ = offsetof('Invalid value', 'bad_field')  # type: ignore