Esempio n. 1
0
def test_GetExpressionSets_commutative_graph_labels(
    wiki: programl_pb2.ProgramGraph,
    wiki_with_commutativity: programl_pb2.ProgramGraph,
):
    """Test that commutative ops produce the same labels."""
    a = subexpressions.CommonSubexpressionAnnotator(wiki)
    graphs_a = a.MakeAnnotated().graphs
    b = subexpressions.CommonSubexpressionAnnotator(wiki_with_commutativity)
    graphs_b = b.MakeAnnotated().graphs
    assert len(graphs_a) == len(graphs_b) == 2
    a, b = graphs_a[0], graphs_b[0]
    for node in a.nodes():
        # Note we can't test for equality of 'x' because the root node is chosen
        # randomly.
        assert a.nodes[node]["y"] == b.nodes[node]["y"]
Esempio n. 2
0
def test_MakeSubexpressionsGraphs_wiki_without_subexpressions(
    wiki_without_subexpressions: programl_pb2.ProgramGraph, ):
    """Test that graph without common subexpressions yields no outputs."""
    annotator = subexpressions.CommonSubexpressionAnnotator(
        wiki_without_subexpressions)
    graphs = annotator.MakeAnnotated().graphs
    assert len(graphs) == 0
Esempio n. 3
0
def test_MakeSubexpressionsGraphs_wiki(wiki: programl_pb2.ProgramGraph):
    """Test the labels generated by a graph with a common subexpression."""
    annotator = subexpressions.CommonSubexpressionAnnotator(wiki)
    annotated = annotator.MakeAnnotated()
    assert len(annotated.graphs) == 2

    g = annotated.graphs[0]

    # Features
    assert g.nodes[0]["x"] == [-1, 0]  # a1
    assert g.nodes[1]["x"] == [-1, 0]  # a2
    assert g.nodes[2]["x"] == [-1, 0]  # b
    assert g.nodes[3]["x"] == [-1, 0]  # c
    assert g.nodes[4]["x"] == [-1, 0]  # d1
    assert g.nodes[5]["x"] == [-1, 0]  # d2
    assert g.nodes[6]["x"] == [-1, 0]  # e
    assert g.nodes[7]["x"] == [-1, 0]  # g

    assert g.nodes[8]["x"] != g.nodes[9]["x"]  # s0
    assert g.nodes[10]["x"] == [-1, 0]  # s1
    assert g.nodes[11]["x"] == [-1, 0]  # s2

    # Labels
    assert g.nodes[0]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
    assert g.nodes[1]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
    assert g.nodes[2]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
    assert g.nodes[3]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
    assert g.nodes[4]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
    assert g.nodes[5]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
    assert g.nodes[6]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
    assert g.nodes[7]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION

    assert g.nodes[8]["y"] == subexpressions.COMMON_SUBEXPRESSION
    assert g.nodes[9]["y"] == subexpressions.COMMON_SUBEXPRESSION
    assert g.nodes[10]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
    assert g.nodes[11]["y"] == subexpressions.NOT_COMMON_SUBEXPRESSION
Esempio n. 4
0
def test_MakeAnnotated_real_protos(real_proto: programl_pb2.ProgramGraph, ):
    """Opaque black-box test of reachability annotator."""
    annotator = subexpressions.CommonSubexpressionAnnotator(real_proto)
    annotated = annotator.MakeAnnotated(10)
    assert len(annotated.graphs) <= 10