def test_add_neighbor(self): egon_vertex = Vertex('Egon') yurac_vertex = Vertex('Yurac') # Make to check the main values in both Vertex print("Egon Vertex: ", egon_vertex.id) print(egon_vertex.add_neighbor(yurac_vertex)) assert egon_vertex.add_neighbor(yurac_vertex) assert egon_vertex.neighbors == {yurac_vertex: 'yurac_vertex'}
def test_get_edge_weight(self): ramon = Vertex('Ramon Geronimo') jessie = Vertex('Jessie Pichardo') joel = Vertex('Joel Pichardo') ramon.add_neighbor(jessie, 10) ramon.add_neighbor(joel, 5) assert ramon.get_edge_weight(jessie) == 10 assert ramon.get_edge_weight(joel) == 5
def test_add_edge(self): graph = Graph() a = Vertex('A') b = Vertex('B') graph.add_vertex(a) graph.add_vertex(b) graph.add_edge(a, b) a.add_neighbor(b) assert b in a.get_neighbors()
def test_add_neighbor(self): label = "Sugar" vertex = Vertex(label) vertex.add_neighbor("Kevin", weight = 0) # Should tell us that Sugar has a neighbor assert any(vertex.neighbors) is True # Should tell us that Sugar's neighbor is Kevin assert vertex.neighbors == {"Kevin": 0}
def test_get_neighbors(self): ramon = Vertex('Ramon Geronimo') jessie = Vertex('Jessie Pichardo') joel = Vertex('Joel Pichardo') ramon.add_neighbor(jessie) ramon.add_neighbor(joel) self.assertCountEqual(ramon.get_neighbors(), [jessie, joel]) self.assertCountEqual(jessie.get_neighbors(), [])
def test_get_edge_weight(self): v1 = Vertex(1) v2 = Vertex(2) v3 = Vertex(3) # Test default weight variable v2.add_neighbor(v1) assert v2.get_edge_weight(v1) == 1 # Test passed in weight variable v2.add_neighbor(v3, 3) assert v2.get_edge_weight(v3) == 3
class VertexTests(unittest.TestCase): def setUp(self): self.vertex = Vertex("A") def test_init(self): assert self.vertex.id is 'A' assert not self.vertex.neighbors def test_add_neighbor(self): # Adding a neighbor neighbor_one = "B" self.vertex.add_neighbor(neighbor_one) assert len(self.vertex.get_neighbors()) is 1 assert neighbor_one in self.vertex.neighbors assert self.vertex.get_edge_weight(neighbor_one) is 0 # Adding another neighbor neighbor_two = 'C' self.vertex.add_neighbor(neighbor_two, 4) assert len(self.vertex.get_neighbors()) is 2 assert neighbor_two in self.vertex.neighbors assert self.vertex.get_edge_weight(neighbor_two) is 4 # Handling duplicate vertex self.vertex.add_neighbor(neighbor_one, 8) assert len(self.vertex.get_neighbors()) is 2 assert neighbor_one in self.vertex.neighbors assert self.vertex.get_edge_weight(neighbor_one) is 8 def test_negatives(self): # Requesting a weight from a non existing edge assert self.vertex.get_edge_weight("B") is None
def test_add_neighbor(self): jessie = Vertex('Jessie Pichardo') joel = Vertex('Joel Pichardo') jessie.add_neighbor(joel) self.assertDictEqual(jessie.neighbors, {joel: 1}) self.assertDictEqual(joel.neighbors, {}) ramon = Vertex('Ramon Geronimo') jessie.add_neighbor(ramon, 5) self.assertDictEqual(jessie.neighbors, {joel: 1, ramon: 5}) with self.assertRaises(KeyError): jessie.add_neighbor(joel) ramon.add_neighbor(jessie) self.assertDictEqual(ramon.neighbors, {jessie: 1})
def test_get_neighbors(self): label = "Sugar" vertex = Vertex(label) vertex.add_neighbor("Kevin", weight = 0) vertex.add_neighbor("Chewie", weight = 4) vertex.add_neighbor("Maggie", weight = 8) vertex.add_neighbor("Ducky", weight = 2) # Should tell us Sugar has 4 neighbors assert len(vertex.neighbors) == 4 # Should tell us Sugar's neighbors are Kevin, Chewie, Maggie, Ducky assert vertex.get_neighbors() == {"Kevin", "Chewie", "Maggie", "Ducky"}
def test_get_neighbors(self): v1 = Vertex(1) v2 = Vertex(2) v3 = Vertex(3) # Test getting neighbors v1.add_neighbor(v2) v1.add_neighbor(v3) self.assertCountEqual(v1.get_neighbors(), [v2, v3]) v2.add_neighbor(v1, 2) v2.add_neighbor(v3, 2) self.assertCountEqual(v2.get_neighbors(), [v1, v3]) v3.add_neighbor(v1, 3) v3.add_neighbor(v2, 3) self.assertCountEqual(v3.get_neighbors(), [v1, v2])
def test_get_edge_weight(self): label = "Sugar" vertex = Vertex(label) vertex.add_neighbor("Kevin", weight = 0) vertex.add_neighbor("Chewie", weight = 4) vertex.add_neighbor("Maggie", weight = 8) vertex.add_neighbor("Ducky", weight = 2) # Should raise ValueError telling us "Rainbow Unicorn" is not a neighbor self.assertRaises(ValueError, vertex.get_edge_weight, "Rainbow Unicorn") # Should tell us Sugar has 4 neighbors assert len(vertex.neighbors) == 4 # Should tell us Sugar's neighbors have a weight of 0, 4, 8, or 2 assert vertex.get_edge_weight("Kevin") == 0 assert vertex.get_edge_weight("Chewie") == 4 assert vertex.get_edge_weight("Maggie") == 8 assert vertex.get_edge_weight("Ducky") == 2
def test_add_neighbor(self): vertex_1 = Vertex('A') # add a neighbor without weight vertex_2 = 'B' vertex_1.add_neighbor(vertex_2) assert vertex_2 in vertex_1.neighbors assert len(vertex_1.get_neighbors()) is 1 assert vertex_1.get_edge_weight(vertex_2) is 0 vertex_3 = 'C' vertex_1.add_neighbor(vertex_3, 5) assert vertex_3 in vertex_1.neighbors assert len(vertex_1.get_neighbors()) is 2 assert vertex_1.get_edge_weight(vertex_3) is 5 # duplicate neighbor vertex_1.add_neighbor(vertex_2, 3) assert vertex_2 in vertex_1.neighbors assert len(vertex_1.get_neighbors()) is 2 assert vertex_1.get_edge_weight(vertex_2) is 3
def test_get_edge_weight(self): test_vertex = Vertex(1) test_vertex.add_neighbor(2, 1) assert test_vertex.get_edge_weight(2) == 1
def test_add_neighbors_weighted(self): test_vertex = Vertex(1) test_vertex.add_neighbor(2, 1) assert len(test_vertex.neighbors) == 1 assert 2 in test_vertex.neighbors.keys() assert 1 in test_vertex.neighbors.values()
def test_add_neighbor(self): v1 = Vertex(1) v2 = Vertex(2) # Test default weight variable v1.add_neighbor(v2) self.assertDictEqual(v1.neighbors, {v2: 1}) # Error should be raised if v2 is added again with self.assertRaises(KeyError): v1.add_neighbor(v2) # Test passed in weight variable v3 = Vertex(3) v1.add_neighbor(v3, 3) self.assertDictEqual(v1.neighbors, {v2: 1, v3: 3}) # Error should be raised if v3 is added again with self.assertRaises(KeyError): v1.add_neighbor(v3, 3) # Test recursive adding v2.add_neighbor(v1) self.assertDictEqual(v2.neighbors, {v1: 1}) v3.add_neighbor(v1) self.assertDictEqual(v3.neighbors, {v1: 1})
def test_get_neighbors(self): test_vertex = Vertex(1) test_vertex.add_neighbor(2, 1) assert 2 in test_vertex.get_neighbors()
import cPickle as pickle from graph import Vertex from graph import Graph v1 = Vertex({},{"name":"John"}) v2 = Vertex({v1:3},{"name":"Jane"}) v1.add_neighbor(v2,3) v3 = Vertex({v2:2},{"name":"Charles"}) v2.add_neighbor(v3,2) v4 = Vertex({v1:4,v2:2},{"name":"Catherine"}) v1.add_neighbor(v4,4) v2.add_neighbor(v4,2) v5 = Vertex({v1:9,v2:3,v3:8,v4:9},{"name":"Jessica"}) v1.add_neighbor(v5,9) v2.add_neighbor(v5,3) v3.add_neighbor(v5,8) v4.add_neighbor(v5,9) v6 = Vertex({v2:6,v3:10},{"name":"Hugo"}) v2.add_neighbor(v6,6) v3.add_neighbor(v6,10) v7 = Vertex({v5:2,v6:10},{"name":"Stanley"}) v5.add_neighbor(v7,2) v6.add_neighbor(v7,10) v8 = Vertex({v3:9,v7:3},{"name":"Emily"}) v3.add_neighbor(v7,9) v7.add_neighbor(v7,3) graph_var = Graph([v1,v2,v3,v4,v5,v6,v7,v8],[[0,1,0,1,1,0,0,0],[1,0,1,1,1,1,0,0], [0,1,0,0,1,1,0,1],[1,1,0,0,1,0,0,0],[1,1,1,1,0,0,1,0],[0,1,1,0,0,0,1,0], [0,1,1,0,0,0,1,0],[0,0,0,0,1,1,0,1],[0,0,1,0,0,0,1,0]]) pickle.dump(graph_var,open('test.p','wb'))