40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
from topo import topo_sort, has_cycle, CycleError
|
|
|
|
def test_empty():
|
|
assert topo_sort([]) == []
|
|
|
|
def test_single():
|
|
assert topo_sort([("a", [])]) == ["a"]
|
|
|
|
def test_linear():
|
|
assert topo_sort([("b", ["a"]), ("c", ["b"])]) == ["a", "b", "c"]
|
|
|
|
def test_diamond():
|
|
r = topo_sort([("d", ["b", "c"]), ("b", ["a"]), ("c", ["a"])])
|
|
assert r[0] == "a" and r[-1] == "d"
|
|
assert r.index("b") < r.index("c")
|
|
|
|
def test_stability():
|
|
assert topo_sort([("x", []), ("y", []), ("z", [])]) == ["x", "y", "z"]
|
|
|
|
def test_cycle():
|
|
try:
|
|
topo_sort([("a", ["b"]), ("b", ["a"])])
|
|
assert False
|
|
except CycleError as e:
|
|
assert set(e.remaining) == {"a", "b"}
|
|
|
|
def test_self_loop():
|
|
assert has_cycle([("a", ["a"])])
|
|
|
|
def test_no_cycle():
|
|
assert not has_cycle([("a", []), ("b", ["a"])])
|
|
|
|
def test_dup_preds():
|
|
assert topo_sort([("b", ["a", "a"])]) == ["a", "b"]
|
|
|
|
if __name__ == "__main__":
|
|
for name in [n for n in globals() if n.startswith("test_")]:
|
|
globals()[name]()
|
|
print("ALL TESTS PASSED")
|