Пример #1
0
    def test_copy_from_list_of_structs_with_pointers(self):
        class Person(Struct):
            __static_data_size__ = 1
            __static_ptrs_size__ = 1

        john =  b('\x20\x00\x00\x00\x00\x00\x00\x00'    # age=32
                  '\x01\x00\x00\x00\x2a\x00\x00\x00'    # name=ptr
                  'J' 'o' 'h' 'n' '\x00\x00\x00\x00')   # John

        # emily is a "split struct", with garbage between the body and the extra
        emily = b('garbage0'
                  '\x18\x00\x00\x00\x00\x00\x00\x00'    # age=24
                  '\x09\x00\x00\x00\x32\x00\x00\x00'    # name=ptr
                  'garbage1'
                  'garbage2'
                  '\x45\x6d\x69\x6c\x79\x00\x00\x00'    # Emily
                  'garbage3')

        john = Person.from_buffer(john, 0, 1, 1)
        emily = Person.from_buffer(emily, 8, 1, 1)
        #
        buf = SegmentBuilder()
        pos = buf.allocate(8)
        item_type = StructItemType(Person)
        buf.copy_from_list(pos, item_type, [john, emily])
        s = buf.as_string()
        expected_buf = b('\x01\x00\x00\x00\x27\x00\x00\x00'    # ptrlist
                         '\x08\x00\x00\x00\x01\x00\x01\x00'    # list tag
                         '\x20\x00\x00\x00\x00\x00\x00\x00'    # age=32
                         '\x09\x00\x00\x00\x2a\x00\x00\x00'    # name=ptr
                         '\x18\x00\x00\x00\x00\x00\x00\x00'    # age=24
                         '\x05\x00\x00\x00\x32\x00\x00\x00'    # name=ptr
                         'J' 'o' 'h' 'n' '\x00\x00\x00\x00'    # John
                         'E' 'm' 'i' 'l' 'y' '\x00\x00\x00')   # Emily
        assert s == expected_buf
Пример #2
0
    def test_copy_from_list_of_structs(self):
        class Point(Struct):
            __static_data_size__ = 2
            __static_ptrs_size__ = 0

        buf1 = b('\x0a\x00\x00\x00\x00\x00\x00\x00'    # 10
                 '\x64\x00\x00\x00\x00\x00\x00\x00')   # 100
        buf2 = b('\x14\x00\x00\x00\x00\x00\x00\x00'    # 20
                 '\xc8\x00\x00\x00\x00\x00\x00\x00')   # 200
        buf3 = b('\x1e\x00\x00\x00\x00\x00\x00\x00'    # 30
                 '\x2c\x01\x00\x00\x00\x00\x00\x00')   # 300
        buf4 = b('\x28\x00\x00\x00\x00\x00\x00\x00'    # 40
                 '\x90\x01\x00\x00\x00\x00\x00\x00')   # 400
        p1 = Point.from_buffer(buf1, 0, 2, 0)
        p2 = Point.from_buffer(buf2, 0, 2, 0)
        p3 = Point.from_buffer(buf3, 0, 2, 0)
        p4 = Point.from_buffer(buf4, 0, 2, 0)
        #
        buf = SegmentBuilder()
        pos = buf.allocate(8)
        item_type = StructItemType(Point)
        buf.copy_from_list(pos, item_type, [p1, p2, p3, p4])
        s = buf.as_string()
        expected_buf = b('\x01\x00\x00\x00\x47\x00\x00\x00'    # ptrlist
                         '\x10\x00\x00\x00\x02\x00\x00\x00'    # list tag
                         '\x0a\x00\x00\x00\x00\x00\x00\x00'    # 10
                         '\x64\x00\x00\x00\x00\x00\x00\x00'    # 100
                         '\x14\x00\x00\x00\x00\x00\x00\x00'    # 20
                         '\xc8\x00\x00\x00\x00\x00\x00\x00'    # 200
                         '\x1e\x00\x00\x00\x00\x00\x00\x00'    # 30
                         '\x2c\x01\x00\x00\x00\x00\x00\x00'    # 300
                         '\x28\x00\x00\x00\x00\x00\x00\x00'    # 40
                         '\x90\x01\x00\x00\x00\x00\x00\x00')   # 400
        assert s == expected_buf
Пример #3
0
 def test_null_pointers(self):
     NULL = b'\x00\x00\x00\x00\x00\x00\x00\x00' # NULL pointer
     buf = SegmentBuilder()
     pos = buf.allocate(24)
     buf.copy_from_struct(0, Struct, None)
     buf.alloc_text(8, None)
     buf.copy_from_list(16, PrimitiveItemType(Types.int64), None)
     s = buf.as_string()
     assert s == NULL*3
Пример #4
0
 def test_copy_from_list_int8(self):
     buf = SegmentBuilder()
     buf.allocate(8) # allocate some garbage at the beginning
     pos = buf.allocate(8)
     item_type = PrimitiveItemType(Types.int8)
     buf.copy_from_list(pos, item_type, [1, 2, 3, 4])
     s = buf.as_string()
     assert s == b(
         '\x00\x00\x00\x00\x00\x00\x00\x00'   # garbage
         '\x01\x00\x00\x00\x22\x00\x00\x00'   # ptrlist
         '\x01\x02\x03\x04\x00\x00\x00\x00')  # 1,2,3,4 + padding
Пример #5
0
 def test_copy_from_list_float64(self):
     buf = SegmentBuilder()
     buf.allocate(8) # allocate some garbage at the beginning
     pos = buf.allocate(8)
     item_type = PrimitiveItemType(Types.float64)
     buf.copy_from_list(pos, item_type, [1.234, 2.345, 3.456, 4.567])
     s = buf.as_string()
     assert s == b(
         '\x00\x00\x00\x00\x00\x00\x00\x00'   # garbage
         '\x01\x00\x00\x00\x25\x00\x00\x00'   # ptrlist
         '\x58\x39\xb4\xc8\x76\xbe\xf3\x3f'   # 1.234
         '\xc3\xf5\x28\x5c\x8f\xc2\x02\x40'   # 2.345
         '\xd9\xce\xf7\x53\xe3\xa5\x0b\x40'   # 3.456
         '\xf8\x53\xe3\xa5\x9b\x44\x12\x40')  # 4.567
Пример #6
0
 def test_copy_from_list_of_text(self):
     buf = SegmentBuilder()
     pos = buf.allocate(8)
     item_type = TextItemType(Types.text)
     buf.copy_from_list(pos, item_type, [b'A', b'BC', b'DEF', b'GHIJ'])
     s = buf.as_string()
     expected_buf = b('\x01\x00\x00\x00\x26\x00\x00\x00'   # ptrlist
                      '\x0d\x00\x00\x00\x12\x00\x00\x00'   # ptr item 1
                      '\x0d\x00\x00\x00\x1a\x00\x00\x00'   # ptr item 2
                      '\x0d\x00\x00\x00\x22\x00\x00\x00'   # ptr item 3
                      '\x0d\x00\x00\x00\x2a\x00\x00\x00'   # ptr item 4
                      'A' '\x00\x00\x00\x00\x00\x00\x00'   # A
                      'B' 'C' '\x00\x00\x00\x00\x00\x00'   # BC
                      'D' 'E' 'F' '\x00\x00\x00\x00\x00'   # DEF
                      'G' 'H' 'I' 'J' '\x00\x00\x00\x00')  # GHIJ
     assert s == expected_buf