def test_unpack_primitive(): s = struct.pack('q', 1234) assert unpack_primitive(ord('q'), s, 0) == 1234 # # left bound check with pytest.raises(IndexError): unpack_primitive(ord('q'), s, -8) # # right bound check with pytest.raises(IndexError): unpack_primitive(ord('q'), s, 1) # not enough bytes
def test_uint64(self): if sys.maxint != (1 << 63) - 1: py.test.skip('64 bit only') if IS_PYPY and sys.pypy_version_info < (5, 6): py.test.skip('Broken on PyPy<5.6') # buf = struct.pack('Q', sys.maxint) val = unpack_primitive(ord('Q'), buf, 0) assert val == sys.maxint assert type(val) is int # buf = struct.pack('Q', sys.maxint + 1) val = unpack_primitive(ord('Q'), buf, 0) assert val == sys.maxint + 1 assert type(val) is long
def sum_unpack(buf): mybufs = (buf, buf) res = 0 for i in range(self.N): buf = mybufs[i % 2] res += unpack_primitive(ord('q'), buf, 16) return res
def test_unpack_primitive_ints(self): buf = '\xff' * 8 assert unpack_primitive(ord('b'), buf, 0) == -1 assert unpack_primitive(ord('h'), buf, 0) == -1 assert unpack_primitive(ord('i'), buf, 0) == -1 assert unpack_primitive(ord('q'), buf, 0) == -1 # assert unpack_primitive(ord('B'), buf, 0) == (1 << 8) - 1 assert unpack_primitive(ord('H'), buf, 0) == (1 << 16) - 1 assert unpack_primitive(ord('I'), buf, 0) == (1 << 32) - 1 assert unpack_primitive(ord('Q'), buf, 0) == (1 << 64) - 1
def test_errors(self): buf = b'\xff' * 8 with pytest.raises(IndexError): unpack_primitive(ord('q'), buf, -1) with pytest.raises(IndexError): unpack_primitive(ord('q'), buf, 8) with pytest.raises(TypeError): unpack_primitive(ord('q'), 42, 0)
def test_bytearray(self): buf = bytearray(struct.pack('q', 42)) assert unpack_primitive(ord('q'), buf, 0) == 42
def test_unpack_primitive_floats(self): buf = struct.pack('f', math.pi) assert unpack_primitive(ord('f'), buf, 0) == struct.unpack('f', buf)[0] # buf = struct.pack('d', math.pi) assert unpack_primitive(ord('d'), buf, 0) == struct.unpack('d', buf)[0]
def setbool(self, byteoffset, bitoffset, value): ifmt = Types.uint8.ifmt current = unpack_primitive(ifmt, self._buf, byteoffset) current |= (value << bitoffset) self.set(ifmt, byteoffset, current)