def test_run_length(self): matrix = np.array([[99, -59, 0, 7, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [12, -2, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) rl = codec.run_length_coding(transform.zigzag(matrix)) expected = [{ 'zeros': 0, 'value': 99, 'bits': 7 }, { 'zeros': 1, 'value': -59, 'bits': 6 }, { 'zeros': 6, 'value': 7, 'bits': 3 }, { 'zeros': 4, 'value': 12, 'bits': 4 }, { 'zeros': 1, 'value': -2, 'bits': 2 }, { 'zeros': 0, 'value': 0, 'bits': 0 }] self.assertEqual([codec.RunLength.from_dict(e) for e in expected], rl)
def test_run_length_trivial(self): matrix = np.array([[1, 2], [3, 4]]) rl = codec.run_length_coding(transform.zigzag(matrix)[1:]) self.assertEqual(rl, [ codec.RunLength(3, 0), codec.RunLength(2, 0), codec.RunLength(4, 0) ])
def test_accidental_combine(self): rle = [ codec.RunLength(value=0, length=14), codec.RunLength(value=31, length=0) ] invert = codec.decode_run_length(rle, 15) rle_2 = codec.run_length_coding(invert, max_len=0xF) self.assertEqual(rle, rle_2)
def _symbol_0_0(self, has, arr): out = codec.run_length_coding(np.array(arr)) last = out[-1] self.assertEqual((last.length == 0 and last.value == 0), has)
def test_rle_too_long(self): l = ([0] * 17) + [1] arr = np.array(l) out = codec.run_length_coding(arr, max_len=0xF) zeros = [s.length for s in out] self.assertEqual(zeros, [14, 2])
def test_rle_break_plus_1(self): arr = [0, 0, 0, 0, 0, 1] rle = codec.run_length_coding(np.array(arr), max_len=4) invert = codec.decode_run_length(rle, len(arr)) self.assertEqual(invert, arr)
def test_rle_max_double(self): arr = [-1, 0, 0, 0, 0, 1, 2] rle = codec.run_length_coding(np.array(arr), max_len=4) invert = codec.decode_run_length(rle, len(arr)) self.assertEqual(arr, invert)
def test_rle_random(self): arr = [np.random.randint(-5, 5) for _ in range(10000)] rle = codec.run_length_coding(np.array(arr)) invert = codec.decode_run_length(rle, 10000) self.assertEqual(invert, arr)
def test_rle_consecutives(self): arr = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0] rle = codec.run_length_coding(np.array(arr)) invert = codec.decode_run_length(rle, len(arr)) self.assertEqual(invert, arr)
def test_rle_max_len(self): arr = [0, 0, 0, 0, 0, 1] rle = codec.run_length_coding(np.array(arr)) invert = codec.decode_run_length(rle, len(arr)) self.assertEqual(arr, invert)
def test_zero_block_reconstruct(self): matrix = np.array([[0, 0, 11, 0], [0, 0, 0, 0]]) lin = transform.zigzag(matrix) out = codec.run_length_coding(lin) invert = codec.decode_run_length(out, 8) self.assertEqual(lin, invert)
def test_zero_block_rle(self): matrix = np.array([[0, 0, 11, 0], [0, 0, 0, 0]]) lin = transform.zigzag(matrix) out = codec.run_length_coding(lin) self.assertEqual(out, [codec.RunLength(11, 3), codec.RunLength(0, 0)])