Exemple #1
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'
Exemple #2
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'
Exemple #3
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 #4
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 #5
0
 class Data(Union):
     child1 = padded(UInt8, 4)
     child2 = padded(UInt16, 1)
     child3 = Child()
Exemple #6
0
 class SomeStruct(Struct):
     field1 = padded('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 = padded(UInt8, 2)
     field2 = UInt16()
     field3 = padded(UInt16, 3)