Files
troupe-standup/wizard-work/beam/test_lcs.py
T

63 lines
1.4 KiB
Python

import os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from lcs import lcs_length, lcs, lcs_diff
def test_empty():
assert lcs_length([], []) == 0
assert lcs([], [1,2]) == []
assert lcs([1,2], []) == []
def test_identical():
s = [1,2,3,4]
assert lcs_length(s, s) == 4
assert lcs(s, s) == s
def test_classic():
a = "ABCBDAB"
b = "BDCAB"
assert lcs_length(a, b) == 4
seq = lcs(a, b)
assert len(seq) == 4
# verify it's a subsequence of both
def subseq(x, y):
it = iter(y)
return all(c in it for c in x)
assert subseq(seq, a) and subseq(seq, b)
def test_no_common():
assert lcs_length("abc", "xyz") == 0
assert lcs("abc", "xyz") == []
def test_diff_roundtrip():
a = [1, 2, 3, 4, 5]
b = [2, 4, 6]
ops = lcs_diff(a, b)
# reconstruct b from a applying ops
out = []
for op, item in ops:
if op in ("keep", "insert"):
out.append(item)
# delete: skip
assert out == b
# deletes+keeps should cover a
covered = [item for op, item in ops if op in ("keep", "delete")]
assert covered == a
def test_strings():
assert lcs("kitten", "sitting") == list("ittn") or len(lcs("kitten","sitting"))==4
assert lcs_length("kitten", "sitting") == 4
def test_type_error():
try:
lcs_length(5, [1])
assert False, "expected TypeError"
except TypeError:
pass