def test_add_two_edges(self): g = Graph() g.add_node("new") g.add_node("node") g.add_node("test") g.add_edge("new", "node") g.add_edge("node", "test", 45) test_list = [(g.node_dict, { "new": [], "node": [], "test": [] }), (g.adjacency_matrix, { "new": { "new": 999999, "node": 999998, "test": 999999 }, "node": { "new": 999999, "node": 999999, "test": 45 }, "test": { "new": 999999, "node": 999999, "test": 999999 } }), (g.nb_nodes, 3), (g.nb_edges, 2)] for g_element, expected_value in test_list: with self.subTest(): self.assertEqual(g_element, expected_value)
def test_get_dist_matrix(self): g = Graph() g.add_node("new") g.add_node("node") g.add_node("test") g.add_edge("new", "node") g.add_edge("node", "test", 45) mat = g.get_dist_matrix() self.assertEqual( mat, { "new": { "new": 999999, "node": 999998, "test": 999999 }, "node": { "new": 999999, "node": 999999, "test": 45 }, "test": { "new": 999999, "node": 999999, "test": 999999 } })
def test_out_degree(self): g = Graph() g.add_node("new") g.add_node("node") g.add_node("test") g.add_edge("new", "node") g.add_edge("node", "test", 45) deg = g.out_degree("new") self.assertEqual(deg, 1)
def test_get_out_neighbors(self): g = Graph() g.add_node("new") g.add_node("node") g.add_node("test") g.add_edge("new", "node") g.add_edge("node", "test", 45) neighbors = g.get_out_neighbors("new") self.assertEqual(neighbors, ["node"])
def test_edge_list(self): g = Graph() g.add_node("new") g.add_node("node") g.add_node("test") g.add_edge("new", "node") g.add_edge("node", "test", 45) edges = g.edge_list() self.assertEqual(edges, [("new", "node", 999998), ("node", "test", 45)])
def test_out_degree_unknown_key(self): g = Graph() g.add_node("new") g.add_node("node") g.add_node("test") g.add_edge("new", "node") g.add_edge("node", "test", 45) with self.assertRaises(IndexError) as context: g.out_degree("blip") self.assertTrue(context.exception)
def test_get_in_neighbors_unknown_key(self): g = Graph() g.add_node("new") g.add_node("node") g.add_node("test") g.add_edge("new", "node") g.add_edge("node", "test", 45) with self.assertRaises(IndexError) as context: g.get_in_neighbors("blip") self.assertTrue(context.exception)
def test_str_full(self): g = Graph() g.add_node("new") g.add_node("node") g.add_node("test") g.add_edge("new", "node") g.add_edge("node", "test", 45) out = io.StringIO() sys.stdout = out print(g) self.assertEqual( out.getvalue(), "{'new': {'new': 999999, 'node': 999998, 'test': 999999}, " "'node': {'node': 999999, 'new': 999999, 'test': 45}, " "'test': {'test': 999999, 'new': 999999, 'node': 999999}}\n")
def test_add_the_same_edge_twice(self): g = Graph() g.add_node("new") g.add_node("node") g.add_edge("new", "node") g.add_edge("new", "node") test_list = [(g.node_dict, { "new": [], "node": [] }), (g.adjacency_matrix, { "new": { "new": 999999, "node": 999998 }, "node": { "new": 999999, "node": 999999 } }), (g.nb_nodes, 2), (g.nb_edges, 1)] for g_element, expected_value in test_list: with self.subTest(): self.assertEqual(g_element, expected_value)