Exemple #1
0
def test_binary_operator() -> None:
    obj1 = UInt16(10)
    obj2 = UInt16(20)
    obj3 = Float(30.0)

    res1 = obj1 + 1
    res2 = obj1 + 1.0
    res3 = 1 + obj1
    res4 = 1.0 + obj1
    res5 = obj1 + obj2
    res6 = obj1 + obj3

    assert isinstance(res1, int)
    assert res1 == 11

    assert isinstance(res2, float)
    assert res2 == 11.0

    assert isinstance(res3, int)
    assert res3 == 11

    assert isinstance(res4, float)
    assert res4 == 11.0

    assert isinstance(res5, int)
    assert res5 == 30

    assert isinstance(res6, float)
    assert res6 == 40.0
Exemple #2
0
def test_init_another() -> None:
    class SomeStruct(Struct):
        field: UInt16

    data = SomeStruct()
    data.field = UInt16(UInt16(0x1234))
    assert data.field == 0x1234
Exemple #3
0
 class SomeStruct(Struct):
     _pad1 = Padding(2)
     field1 = UInt8()
     field2 = UInt16()
     _pad2 = Padding(1)
     _pad3 = Padding(1)
     field3 = UInt16()
     _pad4 = Padding(2)
Exemple #4
0
def test_init_native_value() -> None:
    class SomeStruct(Struct):
        field: UInt16

    data = SomeStruct()
    data.field = UInt16(0x1234)
    assert data.field == 0x1234
Exemple #5
0
def test_init_bad_type() -> None:
    class SomeStruct(Struct):
        field: UInt16

    data = SomeStruct()
    with raises(TypeError):
        data.field = UInt16('bad value')  # type: ignore
Exemple #6
0
def test_init_bad_size() -> None:
    class SomeStruct(Struct):
        field: UInt16

    data = SomeStruct()
    with raises(ValueError):
        data.field = UInt16(b'\x11\x22\x33')
Exemple #7
0
def test_init_bytes() -> None:
    class SomeStruct(Struct):
        field: UInt16

    data = SomeStruct()
    data.field = UInt16(b'\x12\x34')
    assert data.field == 0x3412
Exemple #8
0
def test_unary_operator() -> None:
    obj = UInt16(10)

    res1 = -obj
    res2 = index(obj)

    assert isinstance(res1, int)
    assert res1 == -10

    assert isinstance(res2, int)
    assert res2 == 10
Exemple #9
0
def test_inplace_operator() -> None:
    obj_1 = obj_1_bak = UInt16(10)
    obj_2 = obj_2_bak = UInt16(20)
    obj_3 = obj_3_bak = UInt16(30)
    obj_4 = obj_4_bak = UInt16(40)
    obj_5 = UInt16(50)
    obj_6 = Float(60.9)

    obj_1 += 1
    assert obj_1 is obj_1_bak
    assert obj_1 == 11

    obj_2 += 2.6
    assert obj_2 is obj_2_bak
    assert obj_2 == 22

    obj_3 += obj_5
    assert obj_3 is obj_3_bak
    assert obj_3 == 80

    obj_4 += obj_6
    assert obj_4 is obj_4_bak
    assert obj_4 == 100
Exemple #10
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 #11
0
 class SomeStruct(Struct):
     field1 = padded(UInt8, 2)
     field2 = UInt16()
     field3 = padded(UInt16, 3)
Exemple #12
0
 class Child(Struct):
     field1_1 = UInt16()
     field1_2 = UInt16()
Exemple #13
0
 class SomeStruct(Struct):
     field1 = shifted('bad value', 2)  # type: ignore
     field2 = UInt16()
Exemple #14
0
 class SomeStruct(Struct):
     field1 = padded(shifted(UInt8, 2), 2)
     _pad = Padding(1)
     field2 = shifted(padded(UInt16, 1), 1)
     field3 = UInt16()
Exemple #15
0
 class Data(Union):
     u16 = UInt16()
     pos = Pos()
     u8 = UInt8()
Exemple #16
0
 class Pos(Struct):
     x = UInt16()
     y = UInt16()
Exemple #17
0
 class Child(Struct):
     field1 = UInt16()
     field2 = UInt8()
     field3 = UInt8()
Exemple #18
0
 class Child1(Struct):
     field1_1 = UInt16()
     field1_2 = UInt8()
     field1_3 = UInt8()
Exemple #19
0
 class Child2(Struct):
     field2_1 = UInt8()
     field2_2 = UInt16()
     field2_3 = UInt8()
 class Rect(Struct):
     x = UInt16()
     y: UInt16
     width: UInt8
     height = UInt8()
Exemple #21
0
 class SomeStruct(Struct):
     field1 = shifted(UInt8, 1)
     field2 = UInt16()
     field3 = shifted(UInt16, 3)