def test_len(self): K = graph.Graph() K.vertices = {1: [3], 2: [3, 4], 3: [1, 2, 4], 4: [2, 3]} K.edges = {(1, 3): [], (2, 3): [], (2, 4): [], (3, 4): []} K.nodes = {} self.assertEqual(len(K), 4) self.assertNotEqual(len(K), 5)
def test_degree(self): K = graph.Graph() K.vertices = {1: [3], 2: [3, 4], 3: [1, 2, 4], 4: [2, 3]} K.edges = {(1, 3): [], (2, 3): [], (2, 4): [], (3, 4): []} K.nodes = {} self.assertEqual(K.degree(3), 3) self.assertRaises(graph.GraphInsertError, G.degree, 5)
def test_remove_node(self): graph_remove = graph.Graph({ 'A': ['B', 'C'], 'B': ['A', 'E', 'D'], 'C': ['B', 'D'] }) node = "B" self.assertEqual(graph_remove.remove_vertex(node), "node B has been removed") graph_remove._Graph__graph.clear()
def test_bfs(self): graph_traverse = graph.Graph({ 'A': ['B', 'C'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F', 'G'], 'D': ['B'], 'E': ['B'], 'F': ['C'], 'G': ['C'] }) path = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] self.assertEqual(graph_traverse.bfs('A'), path) graph_traverse._Graph__graph.clear()
import unittest from graphlibrary import graph G = graph.Graph() G.vertices = {1: [3], 2: [3, 4], 3: [1, 2, 4], 4: [2, 3]} G.edges = {(1, 3): [], (2, 3): [], (2, 4): [], (3, 4): []} G.nodes = {} class TestGraph(unittest.TestCase): def test_contains(self): self.assertTrue(1 in G) self.assertFalse(5 in G) def test_len(self): K = graph.Graph() K.vertices = {1: [3], 2: [3, 4], 3: [1, 2, 4], 4: [2, 3]} K.edges = {(1, 3): [], (2, 3): [], (2, 4): [], (3, 4): []} K.nodes = {} self.assertEqual(len(K), 4) self.assertNotEqual(len(K), 5) def test_get_item(self): self.assertIs(G[2], G.vertices[2]) self.assertIsNot(G[3], G.vertices[4]) def test_add_vertex(self): G.add_vertex(6) self.assertTrue(6 in G) G.add_vertex(7, weight='50')
import unittest from graphlibrary import prim from graphlibrary import digraph from graphlibrary import graph G = graph.Graph() G.vertices = { 1: [2, 3, 4], 2: [7], 3: [2, 5, 6], 4: [], 5: [], 6: [], 7: [], 8: [] } G.edges = { (1, 2): { 'weight': 500 }, (1, 3): { 'weight': 100 }, (1, 4): { 'weight': 300 }, (3, 2): { 'weight': 200 }, (2, 7): {
def test_bad_initialization_graph_object(self): graph_values = ['A', 'B', 'C'] wrong_init_graph = graph.Graph(graph_values) self.assertEqual(wrong_init_graph._Graph__graph, None)
def test_uninitialized_graph_object(self): empty_graph = graph.Graph() self.assertEquals(empty_graph._Graph__graph, {})
def test_add_edge_bad_spec(self): graph_edge = graph.Graph({'A': [], 'B': [], 'C': []}) edge = ['A'] self.assertEqual(graph_edge.add_edge(edge), 'Wrong input') graph_edge._Graph__graph.clear()
def test_add_edge_correct_spec(self): graph_edge = graph.Graph({'A': [], 'B': [], 'C': []}) edge = ['A', 'B'] self.assertTrue(graph_edge.add_edge(edge)) graph_edge._Graph__graph.clear()
def test_add_vertices(self): graph_add = graph.Graph() node = "A" graph_add.add_vertices(node) self.assertTrue(graph_add._Graph__graph, ) graph_add._Graph__graph.clear()
def test_initialized_graph_object(self): graph_values = {'A': ['B', 'C'], 'B': ['A', 'E', 'D'], 'C': ['B', 'C']} initialized_graph = graph.Graph(graph_values) self.assertEquals(initialized_graph._Graph__graph, graph_values) initialized_graph._Graph__graph.clear()
import unittest from graphlibrary import first_search from graphlibrary import digraph from graphlibrary import graph EmptyG = graph.Graph() G = graph.Graph() G.vertices = { 1: [2, 3, 4], 2: [1, 3, 7], 3: [1, 2, 5, 6], 4: [1], 5: [3], 6: [3], 7: [2] } G.edges = { (1, 2): [], (1, 3): [], (1, 4): [], (2, 3): [], (2, 7): [], (3, 5): [], (3, 6): [] } G.nodes = {} class TestFirst_Search(unittest.TestCase): def test_BFS(self):