Skip to content
Snippets Groups Projects
Commit 7313fa2d authored by Maxime Lalisse's avatar Maxime Lalisse
Browse files

Q6

parent e2e6db93
Branches main
No related tags found
No related merge requests found
......@@ -85,6 +85,26 @@ public:
}
}
Graph deep_copy() const {
Graph copy;
for (const auto& [id, node] : nodes) {
copy.nodes[id] = Node{id, std::make_shared<ND>(*node.data)};
}
for (const auto& [id, edge] : edges) {
copy.edges[id] = Edge{id, edge.source_id, edge.dest_id, std::make_shared<ED>(*edge.data)};
}
for (const auto& [source, dests] : dests) {
copy.dests[source] = std::vector<int>();
for (auto d : dests) {
copy.dests[source].push_back(d);
}
}
return copy;
}
inline int add_node(const ND &m) {
Node node;
node.node_id = id_counter;
......
No preview for this file type
......@@ -179,3 +179,71 @@ SCENARIO("Decorators", "[graph t]")
}
}
}
// Test deep copy
SCENARIO("Deep copy", "[graph t]")
{
GIVEN("A graph with some items") {
Graph<string, EdgeProps> g;
int a = g.add_node("A");
int b = g.add_node("B");
int c = g.add_node("C");
int d = g.add_node("D");
EdgeProps _ab, _ac, _bd, _cd;
_ab.set_string("msg-ab");
_ac.set_string("msg-ac");
_bd.set_string("msg-bd");
_cd.set_string("msg-cd");
_ab.set_length(1.0);
_ac.set_length(2.0);
_bd.set_length(3.0);
_cd.set_length(4.0);
int ab = g.add_edge(_ab, a,b);
int ac = g.add_edge(_ac, a,c);
int bd = g.add_edge(_bd, b,d);
int cd = g.add_edge(_cd, c,d);
REQUIRE(*g.get_node_data(a) == "A");
REQUIRE(*g.get_node_data(b) == "B");
REQUIRE(*g.get_node_data(c) == "C");
REQUIRE(*g.get_node_data(d) == "D");
REQUIRE(ab == 0);
REQUIRE(ac == 1);
EdgeProps __ab = *g.get_edge_data(ab);
EdgeProps __ac = *g.get_edge_data(ac);
EdgeProps __cd = *g.get_edge_data(cd);
EdgeProps __bd = *g.get_edge_data(bd);
REQUIRE(__ab.get_string() == "msg-ab");
REQUIRE(__ac.get_string() == "msg-ac");
REQUIRE(__cd.get_string() == "msg-cd");
REQUIRE(__bd.get_string() == "msg-bd");
vector<int> succ_a = {b, c};
REQUIRE(g.get_successors(a) == succ_a);
vector<int> succ_b = {d};
REQUIRE(g.get_successors(b) == succ_b);
vector<int> prec_d = {b, c};
REQUIRE(g.get_predecessors(d) == prec_d);
WHEN("Copying the graph") {
Graph g1(g);
THEN ("We obtain the same graph, with the same indexes") {
REQUIRE(g1.get_predecessors(d) == prec_d);
}
}
WHEN("Changing the copy") {
Graph g2(g);
EdgeProps _bc;
_bc.set_string("msg-bc");
_bc.set_length(5.0);
g2.add_edge(_bc, b,c);
THEN ("The original is not changed") {
REQUIRE(g.get_successors(b).size() == 1);
REQUIRE(g2.get_successors(b).size() == 2);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment