def test_python_unpack_used_as_fallback(self, python_packer, mgr): # Don't let the manager report that it contains 'cpp' mgr.__contains__ = MagicMock(return_value=False) data = byte_arrays_of_floats().example() with test.util.force_python_ibm_float(False): ibm_float_packer.unpack_ibm_floats(*data) mgr.__getitem__.assert_not_called() python_packer.return_value.unpack.assert_called_with(*data)
def test_cpp_unpack_used_when_available(self, python_packer, mgr): # Let the manager report that it contains 'cpp' mgr.__contains__ = MagicMock(return_value=True) data = byte_arrays_of_floats().example() with test.util.force_python_ibm_float(False): ibm_float_packer.unpack_ibm_floats(*data) mgr.__getitem__.assert_called_once_with('cpp') mgr.__getitem__.return_value.obj.unpack.assert_called_with(*data) python_packer.return_value.unpack.assert_not_called()
def test_roundtrip(self, data): packed = ibm_float_packer.pack_ibm_floats(data) unpacked = ibm_float_packer.unpack_ibm_floats(packed, len(data)) for f, u in zip(data, unpacked): assert almost_equal( f, float(u), epsilon=EPSILON_IBM_FLOAT)
def read_binary_values(fh, pos=None, seg_y_type='int32', num_items=1, endian='>'): """Read a series of values from a binary file. Args: fh: A file-like-object open in binary mode. pos: An optional file offset in bytes from the beginning from which the data is to be read. If None, the current file position will be used. seg_y_type: The SEG Y data type. num_items: The number of items to be read. endian: '>' for big-endian data (the standard and default), '<' for little-endian (non-standard) Returns: A sequence containing count items. """ ctype = SEG_Y_TYPE_TO_CTYPE[seg_y_type] item_size = size_in_bytes(ctype) block_size = item_size * num_items pos = fh.tell() if pos is None else pos fh.seek(pos, os.SEEK_SET) buf = fh.read(block_size) if len(buf) < block_size: raise EOFError("{} bytes requested but only {} available when reading " "from position {}".format(block_size, len(buf), pos)) values = (unpack_ibm_floats(buf, num_items) if ctype == 'ibm' else unpack_values(buf, ctype, endian)) assert len(values) == num_items return values
def test_roundtrip(self, floats): byte_data, num_items = floats unpacked = ibm_float_packer.unpack_ibm_floats(byte_data, num_items) packed = ibm_float_packer.pack_ibm_floats(unpacked) assert bytes(byte_data) == bytes(packed)
def test_roundtrip(self, data): packed = ibm_float_packer.pack_ibm_floats(data) unpacked = ibm_float_packer.unpack_ibm_floats(packed, len(data)) for f, u in zip(data, unpacked): assert almost_equal(f, float(u), epsilon=EPSILON_IBM_FLOAT)