def test_topological_sort_DAG(): ordering = basic.topological_sort({ "A": [], "B": [], "C": ["A", "B"], "D": ["A", "B"], "E": ["A", "C"], "F": ["B", "D"], "G": ["A", "E"], "H": ["B", "F"] }) assert set(ordering[0:2]) == {"A", "B"} assert is_before("A", "C", ordering) assert is_before("B", "C", ordering) assert is_before("A", "D", ordering) assert is_before("B", "D", ordering) assert is_before("A", "E", ordering) assert is_before("C", "E", ordering) assert is_before("B", "F", ordering) assert is_before("D", "F", ordering) assert is_before("A", "G", ordering) assert is_before("E", "G", ordering) assert is_before("B", "H", ordering) assert is_before("F", "H", ordering)
def test_topological_sort_circular_3(): with pytest.raises(basic.GraphException) as e: ordering = basic.topological_sort({ "D": ["C", "B"], "C": ["A"], "B": ["A"], "A": ["D"] }) assert str(e.value) == "Circular dependency detected in graph"
def test_topological_sort_diamond_1(): ordering = basic.topological_sort({ "D": ["C", "B"], "C": ["A"], "B": ["A"], "A": [] }) assert ordering[0] == "A" assert set(ordering[1:3]) == {"B", "C"} assert ordering[-1] == "D"
def test_topological_sort_one_dependency(): ordering = basic.topological_sort({"B": ["A"]}) assert ordering == ["A", "B"]
def test_topological_sort_empty(): ordering = basic.topological_sort({}) assert len(ordering) == 0