예제 #1
0
def basicUnitTest():
    import io
    
    uint1, uint2 = UINT(), UINT()
    int1, int2 = INT(), INT()
    bool1, bool2 = BOOL(), BOOL()
    stream = io.BytesIO()
    encoder = PlaygroundStandardPacketEncoder()
    
    uint1.setData(10)
    encoder.encode(stream, uint1)
    stream.seek(0)
    encoder.decode(stream, uint2)
    assert uint2.data() == uint1.data()
    
    stream = io.BytesIO()
    int1.setData(-10)
    encoder.encode(stream, int1)
    stream.seek(0)
    encoder.decode(stream, int2)
    assert int1.data() == int2.data()
    
    stream = io.BytesIO()
    bool1.setData(False)
    encoder.encode(stream, bool1)
    stream.seek(0)
    encoder.decode(stream, bool2)
    assert bool1.data() == bool2.data()
    
    listfield1 = ListFieldType(UINT)
    listfield2 = ListFieldType(UINT)
    listfield1.append(10)
    listfield1.append(100)
    listfield1.append(1000)
    
    stream = io.BytesIO()
    encoder.encode(stream, listfield1)
    stream.seek(0)
    encoder.decode(stream, listfield2)
    
    assert len(listfield1) == len(listfield2)
    for i in range(len(listfield1)):
        assert listfield1[i] == listfield2[i]
    
    str1 = StringFieldType()
    str2 = StringFieldType()
    str1.setData("Test1 string")
    
    stream = io.BytesIO()
    encoder.encode(stream, str1)
    stream.seek(0)
    encoder.decode(stream, str2)
    
    assert str1.data() == str2.data()
    
    class SomeFields(PacketFields):
        FIELDS = [  ("field1", UINT({Bits:32})),
                    ("field2", UINT({Bits:32})),
                    ("list1",  ListFieldType(UINT({Bits:8})))
                    ]
    
    fields1Field = ComplexFieldType(SomeFields)
    fields2Field = ComplexFieldType(SomeFields)
    
    fields1 = SomeFields()
    fields1.field1 = 50
    fields1.field2 = 500
    fields1.list1 = []
    fields1.list1.append(0)
    fields1.list1.append(255)
    
    fields1Field.setData(fields1)
    fields2Field.setData(SomeFields())
    
    stream = io.BytesIO()
    encoder.encode(stream, fields1Field)
    stream.seek(0)
    encoder.decode(stream, fields2Field)
    
    fields2 = fields2Field.data()
    
    assert fields1.field1 == fields2.field1
    assert fields1.field2 == fields2.field2
    assert len(fields1.list1) == len(fields2.list1)
    assert fields1.list1[0] == fields2.list1[0]
    assert fields1.list1[-1] == fields2.list1[-1]
예제 #2
0
 class SomeFields(PacketFields):
     FIELDS = [  ("field1", UINT({Bits:32})),
                 ("field2", UINT({Bits:32})),
                 ("list1",  ListFieldType(UINT({Bits:8})))
                 ]
예제 #3
0
class DataPacket(PacketType):
    DEFINITION_IDENTIFIER = "lab1b.gzhang21.DataPacket"
    DEFINITION_VERSION = "1.0"

    FIELDS = [("id", UINT({Optional: True})), ("data", BUFFER)]