Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import Graph
import unittest
import GraphExceptions
class Tests(unittest.TestCase):
def testGraphNotEmpty(self):
g = Graph.addNode(None, "a")
self.assertNotEqual(g, None)
def testAddNode(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
Graph.addNode(g, "c")
shouldBe = {"a": [], "b": [], "c": []}
self.assertEqual(g, shouldBe)
def testAddEdge(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
Graph.addNode(g, "c")
Graph.addEdge(g, "a", "b")
shouldBe = {"a": ["b"], "b": ["a"], "c": []}
self.assertEqual(g, shouldBe)
def testAddExistingNode(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
Graph.addNode(g, "a")
shouldBe = {"a": [], "b": []}
self.assertEqual(g, shouldBe)
def testAddExistingEdge(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
Graph.addEdge(g, "a", "b")
Graph.addEdge(g, "b", "a")
shouldBe = {"a": ["b"], "b": ["a"]}
self.assertEqual(g, shouldBe)
def testAddEdgeThatIsNotInGraph(self):
g = Graph.addNode(None, "a")
Graph.addNode(g, "b")
with self.assertRaises(GraphExceptions.NotInGraph):
Graph.addEdge(g, "a", "c")
def testAddWeightedEdgeThatIsNotInGraph(self):
g = Graph.addNode(None, "a")
Graph.addNode(g, "b")
with self.assertRaises(GraphExceptions.NotInGraph):
Graph.addWeightedEdge(g, "a", "c", 1)
def testAddEdgeToNoneGraph(self):
with self.assertRaises(GraphExceptions.NoGraph):
Graph.addEdge(None, "a", "b")
def testAddWeightedEdgeToNoneGraph(self):
with self.assertRaises(GraphExceptions.NoGraph):
Graph.addWeightedEdge(None, "a", "b", 1)
def testBfsOnNoneGraph(self):
with self.assertRaises(GraphExceptions.NoGraph):
Graph.BFS(None, "a")
def testDfsOnNoneGraph(self):
with self.assertRaises(GraphExceptions.NoGraph):
Graph.DFS(None, "a")
def testDijkstraOnNoneGraph(self):
with self.assertRaises(GraphExceptions.NoGraph):
Graph.dijkstra(None, "a", "b")
def testIsPathOnNoneGraph(self):
with self.assertRaises(GraphExceptions.NoGraph):
Graph.isPath(None, "a", "b")
def testIsPath(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
Graph.addNode(g, "c")
Graph.addEdge(g, "a", "b")
Graph.addEdge(g, "b", "c")
path = Graph.isPath(g, "a", "c")
self.assertEqual(path, ["a", "b", "c"])
def testIsNoPath(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
Graph.addNode(g, "c")
Graph.addEdge(g, "a", "b")
path = Graph.isPath(g, "a", "c")
self.assertIsNone(path)
def testIsConnected(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
Graph.addNode(g, "c")
Graph.addEdge(g, "a", "b")
Graph.addEdge(g, "b", "c")
self.assertTrue(Graph.isConnected(g))
def testIsNotConnected(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
Graph.addNode(g, "c")
Graph.addEdge(g, "a", "b")
self.assertFalse(Graph.isConnected(g))
def testNotInGraphBFS(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
with self.assertRaises(GraphExceptions.NotInGraph):
Graph.BFS(g, "c")
def testNotInGraphDFS(self):
g = Graph.addNode(None,"a")
Graph.addNode(g, "b")
with self.assertRaises(GraphExceptions.NotInGraph):
Graph.DFS(g, "c")
def testDijkstra(self):
expectedPath = ['a', 's', 'c', 'e', 'h']
expectedWeight = 40
g = Graph.addNode(None, "a")
Graph.addNode(g,"b")
Graph.addNode(g,"c")
Graph.addNode(g,"d")
Graph.addNode(g,"e")
Graph.addNode(g,"f")
Graph.addNode(g,"g")
Graph.addNode(g,"h")
Graph.addNode(g,"s")
Graph.addWeightedEdge(g,"a","b",1)
Graph.addWeightedEdge(g,"a","s",5)
Graph.addWeightedEdge(g,"s","c",20)
Graph.addWeightedEdge(g,"c","d",1)
Graph.addWeightedEdge(g,"c","e",10)
Graph.addWeightedEdge(g,"c","f",30)
Graph.addWeightedEdge(g,"f","g",3)
Graph.addWeightedEdge(g,"s","g",2)
Graph.addWeightedEdge(g,"e","h",5)
Graph.addWeightedEdge(g,"g","h",40)
path, totalWeight = Graph.dijkstra(g, "a", "h")
self.assertEqual(path, expectedPath)
self.assertEqual(totalWeight, expectedWeight)
def testDijkstraWithNoPath(self):
g = Graph.addNode(None, "a")
Graph.addNode(g,"b")
Graph.addNode(g,"c")
Graph.addWeightedEdge(g,"a","b",1)
with self.assertRaises(GraphExceptions.NoPath):
Graph.dijkstra(g, "a", "c")
if __name__ == "__main__":
unittest.main()