From 6486f20ac899db939646f872b3850d0c7ddee209 Mon Sep 17 00:00:00 2001 From: lambda7xx Date: Mon, 17 Jul 2023 09:11:13 +0000 Subject: [PATCH] add test_adjacency_multidigraph.cc --- lib/utils/include/utils/containers.h | 1 - lib/utils/include/utils/graph/query_set.h | 7 +- lib/utils/src/graph/adjacency_multidigraph.cc | 23 --- .../test/src/test_adjacency_multidigraph.cc | 139 +++++++----------- 4 files changed, 55 insertions(+), 115 deletions(-) diff --git a/lib/utils/include/utils/containers.h b/lib/utils/include/utils/containers.h index ced5faf69d..2ff7902607 100644 --- a/lib/utils/include/utils/containers.h +++ b/lib/utils/include/utils/containers.h @@ -164,7 +164,6 @@ std::unordered_map filter_keys(std::unordered_map const &m, result.insert(kv); } } - std::cout<<"filter_keys,result.size():"< struct query_set { query_set() = delete; - query_set(T const &query) : query({query}) { - std::cout << "1" << std::endl; - } + query_set(T const &query) : query({query}) {} + query_set(std::unordered_set const &query) : query(query) {} query_set(optional> const &query) : query(query) {} @@ -71,10 +70,10 @@ template std::unordered_map query_keys(query_set const &q, C const &m) { - std::cout << "3" << std::endl; if(is_matchall(q)) { return m; } + std::unordered_set q_set = allowed_values(q); auto filter_lambda = [&q_set](K const &key) { return q_set.find(key) != q_set.end(); diff --git a/lib/utils/src/graph/adjacency_multidigraph.cc b/lib/utils/src/graph/adjacency_multidigraph.cc index af10592260..c70754f65c 100644 --- a/lib/utils/src/graph/adjacency_multidigraph.cc +++ b/lib/utils/src/graph/adjacency_multidigraph.cc @@ -6,7 +6,6 @@ namespace FlexFlow { Node AdjacencyMultiDiGraph::add_node() { Node node{this->next_node_idx}; adjacency[node]; - std::cout << "add node " << node.value() << std::endl; this->next_node_idx++; return node; } @@ -42,27 +41,6 @@ void AdjacencyMultiDiGraph::remove_edge(MultiDiEdge const &e) { std::unordered_set AdjacencyMultiDiGraph::query_edges(MultiDiEdgeQuery const &q) const { std::unordered_set result; - // //std::cout<<"q.srcs:"<adjacency) { - // std::cout<<"this->adjacency node1:"<adjacency node2:"<adjacency NodePort1:"<adjacency NodePort2:"<adjacency); - // std::cout<<"x.size:"<adjacency)) { for (auto const &dst_kv : query_keys(q.dsts, src_kv.second)) { for (auto const &srcIdx_kv : query_keys(q.srcIdxs, dst_kv.second)) { @@ -73,7 +51,6 @@ std::unordered_set } } - std::cout<<"query_edges, result.size():"<{n0, n1, n2}); - std::unordered_set nodes = {n0, n2}; - query_set q{nodes}; - CHECK(g.query_nodes(NodeQuery(q)) == std::unordered_set{n0, n2}); + CHECK(g.query_nodes(NodeQuery(query_set({n0, n2}))) == std::unordered_set{n0, n2}); - // CHECK(g.query_edges(MultiDiEdgeQuery::all()) == - // std::unordered_set{e1, e2, e3, e4}); + CHECK(g.query_edges(MultiDiEdgeQuery::all()) == + std::unordered_set{e1, e2, e3, e4}); - // CHECK(g.query_edges({}) == std::unordered_set{e1, e2, e3, - // e4}); CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_node(n1)) == std::unordered_set{}); CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_node(n1)) == @@ -41,84 +37,53 @@ TEST_CASE("AdjacencyMultiDiGraph:basic_test") { std::unordered_set{}); CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idx(p1)) == std::unordered_set{e1, e4}); -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_nodes({n1, n2})) == -// std::unordered_set{e3, e4}); -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_nodes({n0, n2})) == -// std::unordered_set{e2, e3}); -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_idxs({p1, p2})) == -// std::unordered_set{e3, e4}); -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idxs({p0, p2})) == -// std::unordered_set{e2, e3}); -// CHECK(g.query_edges(MultiDiEdgeQuery::all() -// .with_src_node(n1) -// .with_dst_node(n2) -// .with_src_idx(p1) -// .with_dst_idx(p2)) == -// std::unordered_set{}); -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idx(p2)) == -// std::unordered_set{e2}); + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_nodes(query_set({n1, n2}))) == + std::unordered_set{e3, e4}); + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_nodes(query_set({n0, n2}))) == + std::unordered_set{e2, e3}); + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_idxs(query_set({p1, p2}))) == + std::unordered_set{e3, e4}); + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idxs(query_set({p0, p2}))) == + std::unordered_set{e2, e3}); + CHECK(g.query_edges(MultiDiEdgeQuery::all() + .with_src_node(n1) + .with_dst_node(n2) + .with_src_idx(p1) + .with_dst_idx(p2)) == + std::unordered_set{}); + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idx(p2)) == + std::unordered_set{e2}); + + SUBCASE("remove node" ) { + g.remove_node_unsafe(n0); + + CHECK(g.query_nodes(NodeQuery::all()) == std::unordered_set{n1, n2}); + + CHECK(g.query_edges(MultiDiEdgeQuery::all()) == std::unordered_set{e3, e4}); + + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_node(n0)) == + std::unordered_set{}); + + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_node(n0)) == + std::unordered_set{e3}); + + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_idx(p2)) == + std::unordered_set{e3, e4}); + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idx(p0)) == + std::unordered_set{e3}); + } + + SUBCASE("remove_edge") { + g.remove_edge(e1); + + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_node(n0).with_dst_node( + n1)) == std::unordered_set{}); + + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_node(n2)) == + std::unordered_set{e2}); + + CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_idx(p2)) == + std::unordered_set{e3, e4}); + } + } - -// TEST_CASE("AdjacencyMultiDiGraph:remove_node") { -// MultiDiGraph g = MultiDiGraph::create(); -// Node n0 = g.add_node(); -// Node n1 = g.add_node(); -// Node n2 = g.add_node(); -// NodePort p0 = g.add_node_port(); -// NodePort p1 = g.add_node_port(); -// NodePort p2 = g.add_node_port(); -// MultiDiEdge e1{n0, n1, p0, p1}; -// MultiDiEdge e2{n0, n2, p0, p2}; -// MultiDiEdge e3{n2, n0, p2, p0}; -// MultiDiEdge e4{n2, n1, p2, p1}; -// g.add_edge(e1); -// g.add_edge(e2); -// g.add_edge(e3); -// g.add_edge(e4); - -// g.remove_node_unsafe(n0); - -// CHECK(g.query_nodes({}) == std::unordered_set{n1, n2}); - -// CHECK(g.query_edges({}) == std::unordered_set{e3, e4}); - -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_node(n0)) == -// std::unordered_set{}); - -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_node(n0)) == -// std::unordered_set{e3}); - -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_idxs({p2})) == -// std::unordered_set{e3, e4}); -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_idxs({p0})) == -// std::unordered_set{e3}); -// } - -// TEST_CASE("AdjacencyMultiDiGraph:remove_edge") { -// AdjacencyMultiDiGraph g; -// Node n0 = g.add_node(); -// Node n1 = g.add_node(); -// Node n2 = g.add_node(); -// NodePort p0 = g.add_node_port(); -// NodePort p1 = g.add_node_port(); -// NodePort p2 = g.add_node_port(); -// MultiDiEdge e1{n0, n1, p0, p1}; -// MultiDiEdge e2{n0, n2, p0, p2}; -// MultiDiEdge e3{n2, n0, p2, p0}; -// MultiDiEdge e4{n2, n1, p2, p1}; -// g.add_edge(e1); -// g.add_edge(e2); -// g.add_edge(e3); -// g.add_edge(e4); - -// g.remove_edge(e1); - -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_node(n0).with_dst_node( -// n1)) == std::unordered_set{}); - -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_dst_node(n2)) == -// std::unordered_set{e2}); - -// CHECK(g.query_edges(MultiDiEdgeQuery::all().with_src_idx(p2)) == -// std::unordered_set{e3, e4}); -// }