Exemple #1
0
def test_shifted() -> None:
    struct_t = create_struct(
        'SomeStruct', {
            'field1': shifted(UInt8, 1),
            'field2': UInt16,
            'field3': shifted(UInt16, 3),
        })

    assert sizeof(struct_t) == 9
    assert sizeof(struct_t['field1']) == 1
    assert sizeof(struct_t['field2']) == 2
    assert sizeof(struct_t['field3']) == 2
    assert offsetof(struct_t, 'field1') == 1
    assert offsetof(struct_t, 'field2') == 2
    assert offsetof(struct_t, 'field3') == 7

    data = struct_t((0x12, 0x3456, 0x789a))
    assert bytes(data) == b'\x00\x12\x56\x34\x00\x00\x00\x9a\x78'
Exemple #2
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'
Exemple #3
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'
Exemple #4
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'
Exemple #5
0
 class Data(Union):
     child1 = Child1()
     child2 = shifted(Child2, 1)
     child3 = shifted(UInt16, 1)
Exemple #6
0
 class SomeStruct(Struct):
     field1 = shifted('bad value', 2)  # type: ignore
     field2 = UInt16()
Exemple #7
0
 class SomeStruct(Struct):
     field1 = shifted(Array(UInt16, 3), 1)
     field2 = padded(Array(UInt8, 3), 2)
Exemple #8
0
 class SomeStruct(Struct):
     field1 = padded(shifted(UInt8, 2), 2)
     _pad = Padding(1)
     field2 = shifted(padded(UInt16, 1), 1)
     field3 = UInt16()
Exemple #9
0
 class SomeStruct(Struct):
     field1 = shifted(UInt8, 1)
     field2 = UInt16()
     field3 = shifted(UInt16, 3)