Пример #1
0
def test_deapth_first_empty_graph():

    graph = Graph()

    banana = Vertex("banana")
    with pytest.raises(ValueError):
        vertices_lst = graph.depth_first(banana)
def test_nodes_retrived():
    graph=Graph()
    graph.add_node('a')
    graph.add_node('b')
    actual = graph.get_nodes()
    expected = {'a': [],'b': []}
    assert expected == actual
Пример #3
0
def test_breadth_first_one_element():

    graph = Graph()

    banana = graph.add_node('banana')
    vertices_lst = graph.breadth_first(banana)
    assert vertices_lst == [banana]
def test_add_node():

    graph = Graph()

    actual = graph.add_node('hello').value

    assert actual == 'hello'
def test_size():
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    actual = graph.size()
    expected = 2
    assert expected == actual
def test_helper2_no_neighbors():

    graph = Graph()

    a = graph.add_node("a")

    neighbors_dict = helper2(graph.get_neighbors(a))

    assert neighbors_dict == {}
def test_size_empty():

    graph = Graph()

    expected = 0

    actual = graph.size()

    assert actual == expected
def test_add_edge_start():

    graph = Graph()

    start = Node('start')

    end = graph.add_node('end')

    actual = graph.add_edge(start, end)
    assert actual == 'Start node is not exist in Graph'
def test_add_edge_end():

    graph = Graph()

    end = Node('end')

    start = graph.add_node('start')

    actual = graph.add_edge(start, end)
    assert actual == 'End node is not exist in Graph'
Пример #10
0
def test_get_neighbors_no_neighbors():

    graph = Graph()

    banana = graph.add_node('banana')

    neighbors = graph.get_neighbors(banana)

    assert len(neighbors) == 0
    assert neighbors == []
Пример #11
0
def test_edge_end_node_not_in_graph():

    graph = Graph()

    end = Vertex('end')

    start = graph.add_node('start')

    with pytest.raises(KeyError):
        graph.add_edge(start, end)
def test_get_nodes():

    graph = Graph()

    hi = graph.add_node('hi')

    hello = graph.add_node('hello')

    actual = len(graph.get_nodes())

    assert actual == 2
def test_size():

    graph = Graph()

    graph.add_node('hello')

    expected = 1

    actual = graph.size()

    assert actual == expected
Пример #14
0
def test_breadth_first_regular2():
    graph = Graph()

    banana = graph.add_node('banana')

    apple = graph.add_node('apple')

    strawberry = graph.add_node('strawberry')
    graph.add_edge(banana, apple)

    graph.add_edge(strawberry, banana)

    vertices_lst = graph.breadth_first(strawberry)
    assert vertices_lst == [strawberry, banana, apple]
Пример #15
0
def test_add_edge():

    graph = Graph()

    end = graph.add_node('banana')

    start = graph.add_node('apple')

    graph.add_edge(start, end)

    assert graph._adjacency_list[start][0] == (end, 0)
    assert graph.get_neighbors(start) == [(end, 0)]
    graph.add_edge(end, start)
    assert graph.get_neighbors(end) == [(start, 0)]
Пример #16
0
def test_add_edge_with_weight():

    graph = Graph()

    end = graph.add_node('banana')

    start = graph.add_node('apple')

    graph.add_edge(start, end, 44)

    assert graph.get_neighbors(start) == [(end, 44)]

    graph.add_edge(end, start, 23)

    assert graph.get_neighbors(end) == [(start, 23)]
Пример #17
0
def test_add_edge_with_weight_letters():

    graph = Graph()

    end = graph.add_node('dog')

    start = graph.add_node('cat')

    graph.add_edge(start, end, "a")

    assert graph.get_neighbors(start) == [(end, "a")]

    graph.add_edge(end, start, "b")

    assert graph.get_neighbors(end) == [(start, "b")]
Пример #18
0
def test_breadth_first_different_start_node():

    graph = Graph()

    banana = graph.add_node('banana')

    apple = graph.add_node('apple')

    strawberry = graph.add_node('strawberry')

    graph.add_edge(banana, apple)

    graph.add_edge(strawberry, banana)

    vertices_lst = graph.breadth_first(banana)

    assert vertices_lst == [banana, apple]
Пример #19
0
def test_get_nodes():
    gr = Graph()
    gr.add_node('1')
    gr.add_node('2')
    gr.add_node('3')

    assert len(gr.get_nodes()) == 3
def test_one_node():
    graph = Graph()
    graph.add_node('node1')
    graph.add_edge('node1','node1')
    actual = graph.get_neighbors('node1')
    expected = 'node1 :edge--> node1 ... weight: 1'
    assert expected == actual
Пример #21
0
def test_size():

    graph = Graph()

    graph.add_node('apple')

    assert graph.size() == 1

    graph.add_node('banana')

    assert graph.size() == 2
def test_collection_of_nodes_properly_retrived():
    g = Graph()
    g.add_node('a')
    g.add_node('b')
    g.add_node('c')
    actual = g.get_nodes()
    expected = {'a': [], 'b': [], 'c': []}
    assert expected == actual
def test_one_node_one_edge():
    g = Graph()
    g.add_node('a')

    g.add_edge('a', 'a')
    actual = g.get_neighbors('a')
    expected = 'a --> a edge weight: 1'
    assert expected == actual
Пример #24
0
def test_get_neighbors():
    gr = Graph()
    start = gr.add_node('start')
    end = gr.add_node('end')
    gr.add_edge(start, end, 4)
    neighbors = gr.get_neighbors(start)

    assert neighbors[0][0].value == 'end'
    assert neighbors[0][1] == 4
def test_edge():
    graph=Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_edge('a', 'b', 8)

    actual = graph.get_neighbors('a')
    expected = 'a :edge--> b ... weight: 8'
    assert actual == expected
def test_edge_added_to_graph():
    g = Graph()
    g.add_node('a')
    g.add_node('b')
    g.add_edge('a', 'b', 1)

    actual = g.get_neighbors('a')
    expected = 'a --> b edge weight: 1'
    assert actual == expected
Пример #27
0
def test_breadth_first_regular():

    graph = Graph()

    banana = graph.add_node('banana')
    vertices_lst = graph.breadth_first(banana)

    apple = graph.add_node('apple')
    graph.add_edge(banana, apple)

    vertices_lst = graph.breadth_first(banana)

    assert vertices_lst == [banana, apple]
def test_proper_size_is_returned():
    g = Graph()
    g.add_node('a')
    g.add_node('b')
    g.add_node('c')
    g.add_node('d')

    actual = g.size()
    expected = 4
    assert expected == actual
Пример #29
0
def test_add_edge():
    gr = Graph()
    start = gr.add_node('start')
    end = gr.add_node('end')
    gr.add_edge(start, end)

    assert gr.adjacency_list[start][0] == (end, 1)
Пример #30
0
def test_breadth_firts_islands_nodes():
    graph = Graph()

    banana = graph.add_node('banana')

    apple = graph.add_node('apple')

    strawberry = graph.add_node('strawberry')

    graph.add_edge(banana, apple)

    vertices_lst = graph.breadth_first(banana)

    assert vertices_lst == [banana, apple]

    vertices_lst = graph.breadth_first(apple)

    assert vertices_lst == [apple]