Skip to content

Commit

Permalink
Merge pull request #17 from stasinosntaveas/main
Browse files Browse the repository at this point in the history
added copy constructor for graph
  • Loading branch information
spirosmaggioros authored Feb 4, 2024
2 parents 062a70c + 1f97e34 commit efcf278
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/classes/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ template <typename T> class graph {
}
}

/**
* @brief Construct a new graph object
*
* @param g the graph we want to copy
*/
graph(const graph &g) {
adj = g.adj;
__elements = g.__elements;
__type = g.__type;
}

/**
* @brief operator = for the graph class
*
* @param g the graph we want to copy
* @return graph&
*/
graph &operator=(const graph &g) {
adj = g.adj;
__elements = g.__elements;
__type = g.__type;
return *this;
}

/**
* @brief Destroy the graph object
*/
~graph() { adj.clear(); }

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/graph/graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,29 @@ TEST_CASE("testing eulerian check") {
g3.add_edge(3, 4);
REQUIRE(g3.eulerian() == 2);
}

TEST_CASE("testing copy constructor") {
graph<int> g("undirected");
g.add_edge(1, 3);
g.add_edge(1, 0);
g.add_edge(1, 2);
g.add_edge(2, 0);
g.add_edge(0, 3);
g.add_edge(3, 4);
graph<int> g1(g);
graph<int> g2 = g;
REQUIRE(g.dfs(1) == g1.dfs(1));
REQUIRE(g.dfs(1) == g2.dfs(1));

graph<char> g3("directed");
g3.add_edge('a', 'b');
g3.add_edge('b', 'd');
g3.add_edge('c', 'd');
g3.add_edge('w', 'd');
g3.add_edge('d', 'a');

graph<char> g4 = g3;
graph<char> g5(g3);
REQUIRE(g4.dfs('a') == g3.dfs('a'));
REQUIRE(g5.dfs('a') == g3.dfs('a'));
}

0 comments on commit efcf278

Please sign in to comment.