def test_get_fieldsize(self): test_cases = [(np.zeros(8, dtype="int32"), 1), (np.array([1024] * 8, dtype="int32"), 8), (np.arange(0, 7, dtype="int32"), 4), (np.array([-1, -2, 2, 1] * 2, dtype="int32"), 3)] for array, result in test_cases: fs = agi_bitfield.get_fieldsize(array) self.assertEqual(result, fs)
def test_compress_decompress_field_small(self): data = np.array([1, 0] * 4, dtype="int32") fs = agi_bitfield.get_fieldsize(data) oft = io.BytesIO() cf = agi_bitfield.compress_field(data, fs, oft) self.assertEqual(b'', oft.getvalue()) conv_ = agi_bitfield.conv(fs) dec = agi_bitfield.decode_field(cf[:fs]) self.assertEqual([1 + conv_, 0 + conv_] * 4, dec)
def test_compress_decode_field_overflow(self): data = np.array([1, 2, 3, 4, 255, 40000, -4, 2], dtype="int32") fs = agi_bitfield.get_fieldsize(data) oft = io.BytesIO() cf = agi_bitfield.compress_field(data, fs, oft) self.assertEqual(pack("h", 255) + pack("i", 40000), oft.getvalue()) mask_ = agi_bitfield.mask(fs) dec = agi_bitfield.decode_field(cf) self.assertEqual([1 + 127, 2 + 127, 3 + 127, 4 + 127, 0xfe & mask_, 0xff & mask_, -4 + 127, 2 + 127], dec)
def test_full_compress_decompress_field(self): data = np.array([1, 2, 3, 4, 255, -40000, -4, 2], dtype="int32") fs = agi_bitfield.get_fieldsize(data) oft = io.BytesIO() cf = agi_bitfield.compress_field(data, fs, oft) oft = io.BytesIO(oft.getvalue()) # do this to simulate actual conditions. the data is passed between # encoding and decoding as bytes so the index would not be preserved field = agi_bitfield.decode_field(cf[:fs]) agi_bitfield.undo_escapes(field, fs, oft) self.assertEqual(list(data), field)