import unittest
import retworkx
import numpy
class TestDispatchPyGraph(unittest.TestCase):
class_type = "PyGraph"
def setUp(self):
super().setUp()
if self.class_type == "PyGraph":
self.graph = retworkx.undirected_gnp_random_graph(10, .5, seed=42)
else:
self.graph = retworkx.directed_gnp_random_graph(10, .5, seed=42)
def test_distance_matrix(self):
res = retworkx.distance_matrix(self.graph)
self.assertIsInstance(res, numpy.ndarray)
def test_distance_matrix_as_undirected(self):
if self.class_type == "PyGraph":
with self.assertRaises(TypeError):
retworkx.distance_matrix(self.graph, as_undirected=True)
else:
res = retworkx.distance_matrix(self.graph, as_undirected=True)
self.assertIsInstance(res, numpy.ndarray)
def test_adjacency_matrix(self):
res = retworkx.adjacency_matrix(self.graph)
self.assertIsInstance(res, numpy.ndarray)
def test_all_simple_paths(self):
res = retworkx.all_simple_paths(self.graph, 0, 1)
self.assertIsInstance(res, list)
def test_floyd_warshall_numpy(self):
res = retworkx.floyd_warshall_numpy(self.graph)
self.assertIsInstance(res, numpy.ndarray)
def test_astar_shortest_path(self):
res = retworkx.astar_shortest_path(self.graph, 0, lambda _: True,
lambda _: 1, lambda _: 1)
self.assertIsInstance(list(res), list)
def test_dijkstra_shortest_paths(self):
res = retworkx.dijkstra_shortest_paths(self.graph, 0)
self.assertIsInstance(res, dict)
def test_dijkstra_shortest_path_lengths(self):
res = retworkx.dijkstra_shortest_path_lengths(self.graph, 0,
lambda _: 1)
self.assertIsInstance(res, dict)
def test_k_shortest_path_lengths(self):
res = retworkx.k_shortest_path_lengths(self.graph, 0, 2, lambda _: 1)
self.assertIsInstance(res, dict)
def test_dfs_edges(self):
res = retworkx.dfs_edges(self.graph, 0)
self.assertIsInstance(list(res), list)
class TestDispatchPyDiGraph(TestDispatchPyGraph):
class_type = "PyDiGraph"