diff --git a/micall/tests/test_find_maximum_overlap.py b/micall/tests/test_find_maximum_overlap.py index 714c0eb70..e96ceae7e 100644 --- a/micall/tests/test_find_maximum_overlap.py +++ b/micall/tests/test_find_maximum_overlap.py @@ -1,5 +1,5 @@ import pytest -from micall.utils.find_maximum_overlap import find_maximum_overlap +from micall.utils.find_maximum_overlap import find_maximum_overlap, show_maximum_overlap @pytest.mark.parametrize( @@ -25,3 +25,57 @@ def test_maximum_overlap_cases(left, right, expected): else: with pytest.raises(expected): find_maximum_overlap(left, right) + + +@pytest.mark.parametrize( + "left, right, expected", + [ + ('tttttxxxxx', 'uuuuuxxx', '''\ +tttttxxxxx +uuuuuxxx-- +'''), + ('tttttxxx', 'uuuuuxxxxx', '''\ +--tttttxxx +uuuuuxxxxx +'''), + ('xxxttttt', 'xxxxxuuuuu', '''\ +--xxxttttt +xxxxxuuuuu +'''), + ('xxxxxttttt', 'xxxuuuuu', '''\ +xxxxxttttt +xxxuuuuu-- +'''), + ('xxxxttttt', 'uuuuuxxx', '''\ +-----xxxxttttt +uuuuuxxx------ +'''), + ('aaaaxxxxttttt', 'uxxxk', '''\ +aaaaxxxxttttt +---uxxxk----- +''' + ), + ('tttttxxxx', 'xxxuuuuu', '''\ +tttttxxxx----- +------xxxuuuuu +'''), + ('tttttxxxx', 'xxxuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu', '''\ +tttttxxxx------------------------------- +------xxxuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu +'''), + ('tttttxxxxpppppppppppppppppppp', 'xxxuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu', '''\ +tttttxxxxpppppppppppppppppppp----------- +------xxxuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu +'''), + ], +) +def test_print(left, right, expected): + shift = find_maximum_overlap(left, right) + ret = show_maximum_overlap(left, right, shift) + if ret != expected: + print() + print() + print(ret) + print() + print() + assert ret == expected diff --git a/micall/utils/find_maximum_overlap.py b/micall/utils/find_maximum_overlap.py index ba9574ef2..eeb271dbc 100644 --- a/micall/utils/find_maximum_overlap.py +++ b/micall/utils/find_maximum_overlap.py @@ -71,3 +71,17 @@ def find_maximum_overlap(arr1: Sequence[object], shift_value = max_offset - len(total) return int(shift_value) + + +def show_maximum_overlap(arr1: Sequence[object], + arr2: Sequence[object], + shift: int, + ) -> str: + + arr1_line = '-' * (abs(shift) - len(arr1)) + ''.join(map(str, arr1)) + arr2_line = '-' * (len(arr1) - abs(shift)) + ''.join(map(str, arr2)) + max_len = max(len(arr1_line), len(arr2_line)) + lines = (x.ljust(max_len, '-') + for x in [arr1_line, arr2_line]) + + return '\n'.join(chain(lines, ['']))