def test_invalid_source_index(self): graph = retworkx.PyGraph() graph.add_node(0) graph.add_node(1) graph.add_edge(0, 1, None) with self.assertRaises(IndexError): retworkx.num_shortest_paths_unweighted(graph, 4)
def test_node_with_no_path(self): graph = retworkx.generators.directed_path_graph(5) graph.extend_from_edge_list([(6, 7), (7, 8), (8, 9), (9, 10), (10, 11)]) expected = {1: 1, 2: 1, 3: 1, 4: 1} res = retworkx.num_shortest_paths_unweighted(graph, 0) self.assertEqual(expected, res) res = retworkx.num_shortest_paths_unweighted(graph, 6) expected = {7: 1, 8: 1, 9: 1, 10: 1, 11: 1} self.assertEqual(expected, res)
def test_grid_graph(self): """Test num shortest paths for a 5x5 grid graph 0 -> 1 -> 2 -> 3 -> 4 | | | | | v v v v v 5 -> 6 -> 7 -> 8 -> 9 | | | | | v v v v v 10-> 11-> 12-> 13-> 14 | | | | | v v v v v 15-> 16-> 17-> 18-> 19 | | | | | v v v v v 20-> 21-> 22-> 23-> 24 """ graph = retworkx.generators.directed_grid_graph(5, 5) res = retworkx.num_shortest_paths_unweighted(graph, 0) expected = { 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 2, 7: 3, 8: 4, 9: 5, 10: 1, 11: 3, 12: 6, 13: 10, 14: 15, 15: 1, 16: 4, 17: 10, 18: 20, 19: 35, 20: 1, 21: 5, 22: 15, 23: 35, 24: 70, } self.assertEqual(expected, res)
def test_parallel_paths(self): graph = retworkx.PyGraph() graph.extend_from_edge_list([ (0, 1), (1, 2), (2, 3), (0, 4), (4, 5), (5, 3), ]) res = retworkx.num_shortest_paths_unweighted(graph, 0) expected = { 1: 1, 2: 1, 3: 2, 4: 1, 5: 1, } self.assertEqual(expected, res)
def test_node_indices_with_holes(self): graph = retworkx.generators.directed_path_graph(5) graph.extend_from_edge_list([(6, 7), (7, 8), (8, 9), (9, 10), (10, 11)]) graph.add_edge(4, 6, None) graph.remove_node(5) expected = { 1: 1, 2: 1, 3: 1, 4: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, } res = retworkx.num_shortest_paths_unweighted(graph, 0) self.assertEqual(expected, res)
def test_grid_graph(self): """Test num shortest paths for a 5x5 grid graph 0 - 1 - 2 - 3 - 4 | | | | | 5 - 6 - 7 - 8 - 9 | | | | | 10- 11- 12- 13- 14 | | | | | 15- 16- 17- 18- 19 | | | | | 20- 21- 22- 23- 24 """ graph = retworkx.generators.grid_graph(5, 5) res = retworkx.num_shortest_paths_unweighted(graph, 0) expected = { 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 2, 7: 3, 8: 4, 9: 5, 10: 1, 11: 3, 12: 6, 13: 10, 14: 15, 15: 1, 16: 4, 17: 10, 18: 20, 19: 35, 20: 1, 21: 5, 22: 15, 23: 35, 24: 70, } self.assertEqual(expected, res)
def test__ne__different_length(self): self.assertTrue( retworkx.num_shortest_paths_unweighted(self.dag, 0) != { 1: 1, 2: 2 })
def test_iter(self): mapping_iter = iter(retworkx.num_shortest_paths_unweighted( self.dag, 0)) output = list(mapping_iter) self.assertEqual(output, [1])
def test_values(self): values = retworkx.num_shortest_paths_unweighted(self.dag, 0).values() self.assertEqual([1], list(values))
def test_index_error(self): res = retworkx.num_shortest_paths_unweighted(self.dag, 0) with self.assertRaises(IndexError): res[42]
def test_str(self): res = retworkx.num_shortest_paths_unweighted(self.dag, 0) self.assertEqual("NodesCountMapping{1: 1}", str(res))
def test_deepcopy(self): paths = retworkx.num_shortest_paths_unweighted(self.dag, 0) paths_copy = copy.deepcopy(paths) self.assertEqual(paths, paths_copy)
def test__eq__not_match_values(self): self.assertFalse( retworkx.num_shortest_paths_unweighted(self.dag, 0) == {1: 2})
def test__eq__match(self): self.assertTrue( retworkx.num_shortest_paths_unweighted(self.dag, 0) == {1: 1})
def test_no_edges(self): graph = retworkx.PyGraph() graph.add_node(0) graph.add_node(1) res = retworkx.num_shortest_paths_unweighted(graph, 0) self.assertEqual({}, res)
def test__ne__invalid_type(self): self.assertTrue( retworkx.num_shortest_paths_unweighted(self.dag, 0) != ["a", None])
def test__gt__not_implemented(self): with self.assertRaises(NotImplementedError): retworkx.num_shortest_paths_unweighted(self.dag, 0) > {1: 1}
def test__eq__different_length(self): self.assertFalse( retworkx.num_shortest_paths_unweighted(self.dag, 0) == { 1: 1, 2: 2 })
def test_pickle(self): paths = retworkx.num_shortest_paths_unweighted(self.dag, 0) paths_pickle = pickle.dumps(paths) paths_copy = pickle.loads(paths_pickle) self.assertEqual(paths, paths_copy)
def test_eq__same_type(self): self.assertEqual( retworkx.num_shortest_paths_unweighted(self.dag, 0), retworkx.num_shortest_paths_unweighted(self.dag, 0), )
def test_hash(self): res = retworkx.num_shortest_paths_unweighted(self.dag, 0) hash_res = hash(res) self.assertIsInstance(hash_res, int) # Assert hash is stable self.assertEqual(hash_res, hash(res))
def test__eq__invalid_type(self): self.assertFalse( retworkx.num_shortest_paths_unweighted(self.dag, 0) == ["a", None])
def test_keys(self): keys = retworkx.num_shortest_paths_unweighted(self.dag, 0).keys() self.assertEqual([1], list(keys))
def test__eq__invalid_inner_type(self): self.assertFalse( retworkx.num_shortest_paths_unweighted(self.dag, 0) == {0: "a"})
def test_items(self): items = retworkx.num_shortest_paths_unweighted(self.dag, 0).items() self.assertEqual([(1, 1)], list(items))
def test__ne__match(self): self.assertFalse( retworkx.num_shortest_paths_unweighted(self.dag, 0) != {1: 1})
def test_not_contains(self): res = retworkx.num_shortest_paths_unweighted(self.dag, 0) self.assertNotIn(0, res)
def test__ne__not_match_values(self): self.assertTrue( retworkx.num_shortest_paths_unweighted(self.dag, 0) != {1: 2})