def test_fork(self): "Test that a forked graph works" graph = {1: [], 2: [1], 3: [1], 4: [2, 3]} # type: Dict[int, List[int]] # Valid orderings are `[1, 3, 2, 4]` or `[1, 2, 3, 4]`, but we should # always get the same one. self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
def test_disconnected(self): "Test that a graph with no edges work" graph = {1: [], 2: []} # type: Dict[int, List[int]] # For disconnected nodes the output is simply sorted. self.assertEqual(list(sorted_topologically([1, 2], graph)), [1, 2])
def test_handle_empty_graph(self): "Test that a graph where a node doesn't have an entry is treated as empty" graph = {} # type: Dict[int, List[int]] # For disconnected nodes the output is simply sorted. self.assertEqual(list(sorted_topologically([1, 2], graph)), [1, 2])
def test_linear(self): "Test that a simple `4 -> 3 -> 2 -> 1` graph works" graph = {1: [], 2: [1], 3: [2], 4: [3]} # type: Dict[int, List[int]] self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
def test_multiple_paths(self): "Test that a graph with multiple paths between two nodes work" graph = { 1: [], 2: [1], 3: [2], 4: [3, 2, 1] } # type: Dict[int, List[int]] self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
def test_duplicates(self): "Test that a graph with duplicate edges work" graph = { 1: [], 2: [1, 1], 3: [2, 2], 4: [3] } # type: Dict[int, List[int]] self.assertEqual(list(sorted_topologically([4, 3, 2, 1], graph)), [1, 2, 3, 4])
def test_subset(self): "Test that only sorting a subset of the graph works" graph = {1: [], 2: [1], 3: [2], 4: [3]} # type: Dict[int, List[int]] self.assertEqual(list(sorted_topologically([4, 3], graph)), [3, 4])
def test_empty(self): "Test that an empty graph works correctly" graph = {} # type: Dict[int, List[int]] self.assertEqual(list(sorted_topologically([], graph)), [])