Exemplo n.º 1
0
 def test_remove_neighbor(self):
     vertex = Vertex(data='a')
     self.assertEqual(set(list(vertex.adjacent.keys())), set([]))
     vertex.add_neighbor(data='b')
     self.assertEqual(set(list(vertex.adjacent.keys())), set(['b']))
     self.assertEqual(vertex.adjacent.get('b'), 1)
     self.assertEqual(vertex.remove_neighbor(data='b'), True)
Exemplo n.º 2
0
 def test_add_empty_tuple(self):
     vertex = Vertex(data='a')
     self.assertEqual(vertex.add(neighbors=None), None)
     self.assertEqual(set(list(vertex.get_neighbors())), set([]))
     data = [
         tuple()
     ]
     self.assertEqual(vertex.add(neighbors=data), None)
Exemplo n.º 3
0
 def test_remove_neighbor_key_error(self):
     vertex = Vertex(data='a')
     self.assertEqual(set(list(vertex.adjacent.keys())), set([]))
     vertex.add_neighbor(data='b')
     self.assertEqual(set(list(vertex.adjacent.keys())), set(['b']))
     self.assertEqual(vertex.adjacent.get('b'), 1)
     with self.assertRaises(KeyError) as error:
         self.assertEqual(vertex.remove_neighbor(data='invalid_key'), False)
Exemplo n.º 4
0
def test_size_returns_correct_size():
    graph = Graph()
    vertex1 = Vertex(1)
    vertex2 = Vertex(2)
    vertex3 = Vertex(3)
    graph.add_node(vertex1)
    graph.add_node(vertex2)
    graph.add_node(vertex3)
    actual = graph.size()
    assert actual == 3
Exemplo n.º 5
0
def test_an_edge_can_be_added_to_graph():
    graph = Graph()
    vertex1 = Vertex(1)
    vertex2 = Vertex(2)
    graph.add_node(vertex1)
    graph.add_node(vertex2)
    graph.add_edge(vertex1, vertex2, 20)
    vertex1_list = graph._adjacency_list[vertex1]
    first_edge = vertex1_list[0]
    assert first_edge.vertex == vertex2
    assert first_edge.weight == 20
Exemplo n.º 6
0
def test_a_collection_of_all_nodes_can_be_retrieved_from_graph():
    graph = Graph()
    vertex1 = Vertex(1)
    vertex2 = Vertex(2)
    vertex3 = Vertex(3)
    graph.add_node(vertex1)
    graph.add_node(vertex2)
    graph.add_node(vertex3)
    list = graph.get_nodes()
    expected = [1, 2, 3]
    for i, obj in enumerate(list):
        assert obj.value == expected[i]
Exemplo n.º 7
0
 def test_get_weight(self):
     vertex = Vertex(data='a')
     self.assertEqual(set(list(vertex.adjacent.keys())), set([]))
     vertex.add_neighbor(data='b')
     self.assertEqual(vertex.get_weight('b'), 1)
     vertex.add_neighbor(data='b', weight=5)
     self.assertEqual(vertex.get_weight('b'), 5)
Exemplo n.º 8
0
def test_neighbors_are_returned_with_weights_between_nodes_included():
    graph = Graph()
    vertex1 = Vertex(1)
    vertex2 = Vertex(2)
    vertex3 = Vertex(3)
    graph.add_node(vertex1)
    graph.add_node(vertex2)
    graph.add_node(vertex3)
    graph.add_edge(vertex1, vertex2, 20)
    graph.add_edge(vertex1, vertex3, 30)
    actual = graph.get_neighbors(vertex1)
    edge1 = actual[0]
    edge2 = actual[1]
    assert edge1.vertex == vertex2
    assert edge1.weight == 20
    assert edge2.vertex == vertex3
    assert edge2.weight == 30
def test_add_edge_interloper_end():

    graph = Graph()

    end = Vertex('end')

    start = graph.add_node('start')

    with pytest.raises(KeyError):
        graph.add_edge(start, end)
Exemplo n.º 10
0
 def test_add(self):
     vertex = Vertex(data='a')
     self.assertEqual(vertex.add(neighbors=None), None)
     self.assertEqual(set(list(vertex.get_neighbors())), set([]))
     data = [
         ('b', 3),
         ('c', 7),
         ('d'),
         tuple(['e'])
     ]
     vertex.add(neighbors=data)
     self.assertEqual(set(list(vertex.get_neighbors())),
                      set(['b', 'c', 'd', 'e']))
     self.assertEqual(vertex.get_weight('b'), 3)
     self.assertEqual(vertex.get_weight('c'), 7)
     self.assertEqual(vertex.get_weight('d'), 1)
     self.assertEqual(vertex.get_weight('e'), 1)
Exemplo n.º 11
0
 def test_add_tuple_not_int(self):
     vertex = Vertex(data='a')
     self.assertEqual(vertex.add(neighbors=None), None)
     self.assertEqual(set(list(vertex.get_neighbors())), set([]))
     data = [
         ('b', 'invalid weight'),
     ]
     with self.assertRaises(TypeError) as error:
         vertex.add(neighbors=data)
def test_get_nodes():

    graph = Graph()

    banana = graph.add_node('banana')

    apple = graph.add_node('apple')

    loner = Vertex('loner')

    expected = 2

    actual = len(graph.get_nodes())

    assert actual == expected
Exemplo n.º 13
0
def read_data(file_path): 
    # Vertices
    vertices = []
    with open(file_path, "r") as f:
        # coordinates start from the 7th line, end with EOF
        for _ in range(6):
            f.readline()
        for line in f:
            splitted_line = line.split()
            if len(splitted_line) == 3:
                idx, loc_x, loc_y = splitted_line
                v = Vertex(int(idx), float(loc_x), float(loc_y))
                vertices.append(v)

    return vertices
Exemplo n.º 14
0
def test_breadth_first_empty_graph():
    graph = Graph()
    spam = Vertex("spam")
    with pytest.raises(ValueError):
        vertices_lst = graph.breadth_first(spam)
Exemplo n.º 15
0
def test_vertex_can_be_added_to_graph():
    graph = Graph()
    vertex1 = Vertex("hello")
    graph.add_node(vertex1)
    result = graph.size()
    assert result == 1
Exemplo n.º 16
0
def test_create_vertex():
    actual = Vertex('a').value
    expected = 'a'
    assert actual == expected
Exemplo n.º 17
0
def test_can_instantiate_Vertex():
    vertex = Vertex()
    assert vertex
    assert vertex.value == None
Exemplo n.º 18
0
def test_can_instantiate_Edge():
    vertex1 = Vertex("hello")
    edge = Edge(vertex1, weight=50)
    assert edge.vertex == vertex1
    assert edge.weight == 50
Exemplo n.º 19
0
 def test_add_neighbors_not_tuple(self):
     vertex = Vertex(data='a')
     with self.assertRaises(TypeError) as error:
         self.assertIsNone(vertex.add(neighbors=['invalid_data']))
Exemplo n.º 20
0
 def test_vertex_initialization_invalid_data(self):
     with self.assertRaises(TypeError) as error:
         vertex = Vertex(data=None)
Exemplo n.º 21
0
 def test_vertex_initialization(self):
     vertex = Vertex(data='a')
     self.assertEqual(set(list(vertex.adjacent.keys())), set([]))
Exemplo n.º 22
0
 def test_get_weight_keyerror(self):
     vertex = Vertex(data='a')
     self.assertEqual(set(list(vertex.adjacent.keys())), set([]))
     vertex.add_neighbor(data='b')
     with self.assertRaises(KeyError) as error:
         self.assertEqual(vertex.get_weight('c'), None)
Exemplo n.º 23
0
 def test_add_neighbor_negative_weight(self):
     vertex = Vertex(data='a')
     self.assertEqual(set(list(vertex.adjacent.keys())), set([]))
     with self.assertRaises(TypeError) as error:
         vertex.add_neighbor(data='b', weight=-5)
Exemplo n.º 24
0
def test_add_edge_interloper_start():
    graph = Graph()
    start = Vertex("start")
    end = graph.add_node("end")
    with pytest.raises(KeyError):
        graph.add_edge(start, end)
Exemplo n.º 25
0
def test_add_vertex_fail():
    vertex = Vertex('a')
    actual = vertex.value
    expected = 'b'
    assert actual != expected
Exemplo n.º 26
0
 def test_get_neighbor(self):
     vertex = Vertex(data='a')
     self.assertEqual(set(list(vertex.adjacent.keys())), set([]))
     vertex.add_neighbor(data='b')
     self.assertEqual(vertex.get_neighbors(), ['b'])
Exemplo n.º 27
0
 def test_get_neighbor_empty_adjacent_list(self):
     vertex = Vertex(data='a')
     self.assertEqual(vertex.get_neighbors(), [])
Exemplo n.º 28
0
def test_add_vertex_pass():
    vertex = Vertex('a')
    actual = vertex.value
    expected = 'a'
    assert actual == expected
Exemplo n.º 29
0
def test_edge_end_node_not_in_graph():
    graph = Graph()
    end = Vertex('end')
    start = graph.add_vertex('start')
    with pytest.raises(KeyError):
        graph.add_edge(start, end)