def test_new_list_overflow(): def unpack(p): assert ptr.kind(p) == ptr.LIST return ptr.offset(p), ptr.list_size_tag(p), ptr.list_item_count(p) # p = ptr.new_list(-1, 0, 0) assert unpack(p) == (-1, 0, 0) # p = ptr.new_list(0, -1, 0) assert unpack(p) == (0, 7, 0) # p = ptr.new_list(0, 0, -1) assert unpack(p) == (0, 0, 2**29 - 1)
def _new_ptrlist(self, size_tag, ptr_offset, item_type, item_count): if size_tag != ptr.LIST_SIZE_COMPOSITE: # a plain ptr return ptr.new_list(ptr_offset, size_tag, item_count) # # if size is composite, ptr contains the total size in words, and # we also need to emit a "list tag" data_size = item_type.structcls.__static_data_size__ # in words ptrs_size = item_type.structcls.__static_ptrs_size__ # in words total_words = (data_size + ptrs_size) * item_count # # emit the tag tag = ptr.new_struct(item_count, data_size, ptrs_size) self._alloc(struct.pack('<q', tag)) return ptr.new_list(ptr_offset, ptr.LIST_SIZE_COMPOSITE, total_words)
def test_new_list(): p = ptr.new_list(64, 7, 200) assert ptr.kind(p) == ptr.LIST assert ptr.offset(p) == 64 assert ptr.list_size_tag(p) == 7 assert ptr.list_item_count(p) == 200 assert p == 0x0000064700000101
def test_hash_str(): buf = ('garbage0' 'hello capnproto\0') # string p = ptr.new_list(0, ptr.LIST_SIZE_8, 16) b = Segment(buf) h = b.hash_str(p, 0, 0, additional_size=-1) assert h == hash("hello capnproto") h = b.hash_str(p, 0, 0, additional_size=0) assert h == hash("hello capnproto\0")
def test_read_str(): buf = ('garbage0' 'hello capnproto\0') # string p = ptr.new_list(0, ptr.LIST_SIZE_8, 16) b = Segment(buf) s = b.read_str(p, 0, "", additional_size=-1) assert s == "hello capnproto" s = b.read_str(p, 0, "", additional_size=0) assert s == "hello capnproto\0"
def is_compact(self, buf, offset, kind, **kwds): buf = Segment(buf) if kind == ptr.STRUCT: p = ptr.new_struct(0, **kwds) elif kind == ptr.LIST: p = ptr.new_list(0, **kwds) else: assert False return is_compact(buf, p, offset - 8)
def alloc_data(self, offset, value, suffix=None): if value is None: return 0 # NULL if suffix: value += suffix ptr_offset = self._calc_relative_offset(offset) p = ptr.new_list(ptr_offset, ptr.LIST_SIZE_8, len(value)) self._alloc(value) self._record_allocation(offset, p) return p
def alloc_list(self, pos, size_tag, item_count, body_length): """ Allocate a new list of the given size, and write the resulting pointer at position i. Return the newly allocated position. """ body_length = ptr.round_up_to_word(body_length) result = self.allocate(body_length) offet = result - (pos + 8) p = ptr.new_list(offet // 8, size_tag, item_count) self.write_int64(pos, p) return result
def _get_end(self): p = ptr.new_list(0, self._size_tag, self._item_count) return endof(self._seg, p, self._offset - 8)