Example #1
0
def test_undirected_graph():
    g = UndirectedGraph()

    assert num_vertices(g) == 0
    u = add_vertex(g)
    assert num_vertices(g) == 1
    v = add_vertex(g)
    assert num_vertices(g) == 2
    w = add_vertex(g)
    assert num_vertices(g) == 3

    assert num_edges(g) == 0
    e_uv, _ = add_edge(u, v, g)
    assert num_edges(g) == 1
    e_uv1, _ = add_edge(v, u, g)
    assert num_edges(g) == 2
    e_uw, _ = add_edge(u, w, g)
    assert num_edges(g) == 3
    e_vw, _ = add_edge(v, w, g)
    assert num_edges(g) == 4

    print("Edges = %s" % {e for e in edges(g)})

    print("In-edges(%s) = %s" % (u, {e for e in in_edges(u, g)}))
    assert in_degree(u, g) == 3
    assert in_degree(v, g) == 3
    assert in_degree(
        w, g) == 2, "in_edges(%s) = %s" % (w, {u
                                               for u in in_edges(w, g)})

    print("Out-edges(%s) = %s" % (u, {e for e in out_edges(u, g)}))
    assert out_degree(u, g) == 3
    assert out_degree(v, g) == 3
    assert out_degree(w, g) == 2

    print("Removing %s" % e_uv)
    remove_edge(e_uv, g)
    assert num_edges(g) == 3

    print("Removing %s" % e_uv1)
    remove_edge(e_uv1, g)
    assert num_edges(g) == 2

    print("Removing %s" % v)
    remove_vertex(v, g)
    assert num_vertices(g) == 2

    print("Edges = %s" % {e for e in edges(g)})
    assert num_edges(g) == 1

    print("Out-edges(%s) = %s" % (u, {e for e in out_edges(u, g)}))
    assert out_degree(u, g) == 1
    assert out_degree(w, g) == 1
    assert in_degree(u, g) == 1
    assert in_degree(w, g) == 1
Example #2
0
def test_undirected_graph_add_vertex():
    g = make_g2()
    assert num_vertices(g) == 3
    assert num_edges(g) == 5

    # Add vertex x
    x = add_vertex(g)
    assert num_vertices(g) == 4

    # Add edge (v -> x)
    add_edge(v, x, g)
    assert num_edges(g) == 6
    assert out_degree(v, g) == 4
Example #3
0
def test_undirected_graph_remove_vertex():
    g = make_g2()
    assert num_vertices(g) == 3
    assert num_edges(g) == 5

    remove_vertex(v, g)
    assert num_vertices(g) == 2
    assert num_edges(g) == 2

    remove_vertex(w, g)
    assert num_vertices(g) == 1
    assert num_edges(g) == 0

    remove_vertex(u, g)
    assert num_vertices(g) == 0
    assert num_edges(g) == 0
Example #4
0
def test_all():
    for directed in [True, False]:
        for (i, g) in enumerate([make_g1(directed), make_g2(directed)]):
            print("Processing G%s (directed = %s)" % (i, directed))
            vis = MyDepthFirstSearchVisitor(verbose=False)
            map_color = defaultdict(int)
            depth_first_search(0, g, make_assoc_property_map(map_color), vis)

            n = num_vertices(g)
            m = num_edges(g)
            n_ = vis.num_vertices
            m_ = vis.num_edges

            # Our graph are connected, so these assertion should be verified
            assert n_ == n, "Visited %s/%s vertices" % (n_, n)
            if directed:
                assert m_ == m, "Visited %s/%s edges" % (m_, m)
            else:
                # Undirected edges are visited forward and backward, so they are visited "twice"
                # Not that (u -> u) arc would be only visited once.
                assert m_ == 2 * m, "Visited %s/%s edges" % (m_, m)

            # Finally, all vertices should all be BLACK
            for u in vertices(g):
                assert map_color[u] == BLACK
Example #5
0
def test_rpn_deque_ast():
    tokenized = tokenizer_re("(a?b)*?c+d")
    ast = Ast()
    output = RpnDequeAst(map_operators=MAP_OPERATORS_RE, ast=ast)
    ret = shunting_yard_postfix(tokenized, MAP_OPERATORS_RE, output=output)
    assert num_vertices(ast) == 11
    assert num_edges(ast) == 10
    [root] = ret
    assert root == 10
    from pybgl.graphviz import graph_to_html
    graph_to_html(ast)
Example #6
0
def check_graph_size(g: Graph, n_expected: int, m_expected: int):
    """
    Tests whether a Graph has the expected size.
    Args:
        n_expected: The expected number of vertices.
        m_expected: The expected number of edges.
    """
    n = num_vertices(g)
    m = num_edges(g)
    assert n == n_expected, "Expected %s vertices, got %s" % (n_expected, n)
    assert m == m_expected, "Expected %s edges, got %s" % (m_expected, m)
Example #7
0
def test_shunting_yard_ast():
    tokenized = tokenizer_re("(a?b)*?c+d")
    (ast, root) = shunting_yard_ast(tokenized, MAP_OPERATORS_RE)
    assert num_vertices(ast) == 11
    assert num_edges(ast) == 10
    assert root == 10
Example #8
0
def test_undirected_graph_num_vertices():
    g1 = make_g1()
    assert num_vertices(g1) == 3
Example #9
0
def test_compile_dfa_ipv6():
    dfa = make_dfa_ipv6()
    assert num_vertices(dfa) == 15 and num_edges(dfa) == 279
Example #10
0
def test_compile_nfa_ipv6():
    nfa = make_nfa_ipv6()
    assert num_vertices(nfa) >= 15 and num_edges(nfa) >= 279