| 86 | } |
| 87 | |
| 88 | void CheckGraph(const MutableGraphView& mutable_graph) { |
| 89 | GraphView immutable_graph(mutable_graph.graph()); |
| 90 | EXPECT_EQ(mutable_graph.graph()->node_size(), |
| 91 | immutable_graph.graph()->node_size()); |
| 92 | EXPECT_EQ(mutable_graph.graph(), immutable_graph.graph()); |
| 93 | |
| 94 | auto check_edges = |
| 95 | [](const absl::flat_hash_set<MutableGraphView::Edge>& mutable_edges, |
| 96 | const absl::flat_hash_set<GraphView::Edge>& immutable_edges) { |
| 97 | EXPECT_EQ(mutable_edges.size(), immutable_edges.size()); |
| 98 | for (const auto& fanin_edge : mutable_edges) { |
| 99 | GraphView::Edge immutable_edge( |
| 100 | {fanin_edge.src.node, fanin_edge.src.port_id}, |
| 101 | {fanin_edge.dst.node, fanin_edge.dst.port_id}); |
| 102 | EXPECT_TRUE(immutable_edges.contains(immutable_edge)); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | // Check graph connectivity. |
| 107 | for (auto& node : *mutable_graph.graph()->mutable_node()) { |
| 108 | EXPECT_EQ(&node, immutable_graph.GetNode(node.name())); |
| 109 | |
| 110 | auto mutable_fanins = |
| 111 | mutable_graph.GetFanins(node, /*include_controlling_nodes=*/true); |
| 112 | auto immutable_fanins = |
| 113 | immutable_graph.GetFanins(node, /*include_controlling_nodes=*/true); |
| 114 | EXPECT_EQ(mutable_fanins.size(), immutable_fanins.size()); |
| 115 | for (const auto& fanin : mutable_fanins) { |
| 116 | GraphView::OutputPort immutable_fanin(fanin.node, fanin.port_id); |
| 117 | EXPECT_TRUE(immutable_fanins.contains(immutable_fanin)); |
| 118 | } |
| 119 | |
| 120 | auto mutable_fanouts = |
| 121 | mutable_graph.GetFanouts(node, /*include_controlled_nodes=*/true); |
| 122 | auto immutable_fanouts = |
| 123 | immutable_graph.GetFanouts(node, /*include_controlled_nodes=*/true); |
| 124 | EXPECT_EQ(mutable_fanouts.size(), immutable_fanouts.size()); |
| 125 | for (const auto& fanout : mutable_fanouts) { |
| 126 | GraphView::InputPort immutable_fanout(fanout.node, fanout.port_id); |
| 127 | EXPECT_TRUE(immutable_fanouts.contains(immutable_fanout)); |
| 128 | } |
| 129 | |
| 130 | auto mutable_fanin_edges = |
| 131 | mutable_graph.GetFaninEdges(node, /*include_controlling_edges=*/true); |
| 132 | auto immutable_fanin_edges = |
| 133 | immutable_graph.GetFaninEdges(node, /*include_controlling_edges=*/true); |
| 134 | check_edges(mutable_fanin_edges, immutable_fanin_edges); |
| 135 | |
| 136 | auto mutable_fanout_edges = |
| 137 | mutable_graph.GetFanoutEdges(node, /*include_controlled_edges=*/true); |
| 138 | auto immutable_fanout_edges = |
| 139 | immutable_graph.GetFanoutEdges(node, /*include_controlled_edges=*/true); |
| 140 | check_edges(mutable_fanout_edges, immutable_fanout_edges); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | TEST(MutableGraphViewTest, AddSubgraph) { |
| 145 | GraphDef graph_def = test::function::GDef( |
no test coverage detected