Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in overload of addEdge #321

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 35 additions & 23 deletions include/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,33 +243,31 @@ class Graph {
/**
* @brief This function generate a list of adjacency matrix with every element
* of the matrix contain the node where is directed the link and the Edge
* corrispondent to the link Note: No Thread Safe
* corrispondent to the link
* Note: No Thread Safe
*/
virtual const std::shared_ptr<AdjacencyMatrix<T>> getAdjMatrix() const;
/**
* \brief This function generates a set of nodes linked to the provided node in a
* directed graph
* Note: No Thread Safe
* \brief This function generates a set of nodes linked to the provided node
* in a directed graph
*
* @param Pointer to the node
*
*/
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>> outNeighbors(
const Node<T> *node) const;
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule

MISRA 12.3 rule
outNeighbors(const Node<T> *node) const;
/**
* \brief This function generates a set of nodes linked to the provided node in a
* directed graph
* Note: No Thread Safe
* \brief This function generates a set of nodes linked to the provided node
* in a directed graph
*
* @param Pointer to the node
*
*/
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>> outNeighbors(
shared<const Node<T>> node) const;
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule

MISRA 12.3 rule
outNeighbors(shared<const Node<T>> node) const;
/**
* \brief This function generates a set of nodes linked to the provided node in
* any graph
* Note: No Thread Safe
* \brief This function generates a set of nodes linked to the provided node
* in any graph
*
* @param Pointer to the node
*
Expand All @@ -278,9 +276,8 @@ class Graph {
inOutNeighbors(const Node<T> *node) const;
/**
* \brief
* \brief This function generates a set of nodes linked to the provided node in
* any graph
* Note: No Thread Safe
* \brief This function generates a set of nodes linked to the provided node
* in any graph
*
* @param Pointer to the node
*
Expand Down Expand Up @@ -741,8 +738,23 @@ void Graph<T>::setEdgeSet(const T_EdgeSet<T> &edgeSet) {

template <typename T>
void Graph<T>::addEdge(const Edge<T> *edge) {
auto edge_shared = make_shared<const Edge<T>>(*edge);
this->edgeSet.insert(edge_shared);
if (edge->isDirected().has_value() && edge->isDirected().value()) {
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
auto edge_shared = make_shared<DirectedWeightedEdge<T>>(*edge);
this->edgeSet.insert(edge_shared);
} else {
auto edge_shared = make_shared<DirectedEdge<T>>(*edge);
this->edgeSet.insert(edge_shared);
}
} else {
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
auto edge_shared = make_shared<UndirectedWeightedEdge<T>>(*edge);
this->edgeSet.insert(edge_shared);
} else {
auto edge_shared = make_shared<UndirectedEdge<T>>(*edge);
this->edgeSet.insert(edge_shared);
}
}
}

template <typename T>
Expand Down Expand Up @@ -1385,16 +1397,16 @@ const std::shared_ptr<AdjacencyMatrix<T>> Graph<T>::getAdjMatrix() const {
}

template <typename T>
const std::unordered_set<shared<const Node<T>>, nodeHash<T>> Graph<T>::outNeighbors(
const Node<T> *node) const {
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule

MISRA 12.3 rule
Graph<T>::outNeighbors(const Node<T> *node) const {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 806 with no text in the supplied rule-texts-file

misra violation 806 with no text in the supplied rule-texts-file
auto node_shared = make_shared<const Node<T>>(*node);

return outNeighbors(node_shared);
}

template <typename T>
const std::unordered_set<shared<const Node<T>>, nodeHash<T>> Graph<T>::outNeighbors(
shared<const Node<T>> node) const {
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.3 rule

MISRA 12.3 rule
Graph<T>::outNeighbors(shared<const Node<T>> node) const {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 806 with no text in the supplied rule-texts-file

misra violation 806 with no text in the supplied rule-texts-file

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 508 with no text in the supplied rule-texts-file

misra violation 508 with no text in the supplied rule-texts-file
auto adj = getAdjMatrix();
if (adj->find(node) == adj->end()) {
return std::unordered_set<shared<const Node<T>>, nodeHash<T>>();
Expand Down
60 changes: 58 additions & 2 deletions test/GraphTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,60 @@ TEST(GraphTest, GetNodeSet_2) {
}) != nodeSet.end());
}

TEST(GraphTest, RawAddEdge_1) {
CXXGraph::Node<int> n1("a", 1);
CXXGraph::Node<int> n2("b", 1);
CXXGraph::Node<int> n3("c", 1);

CXXGraph::DirectedEdge<int> e1(0, n1, n2);
CXXGraph::DirectedEdge<int> e2(1, n2, n3);
CXXGraph::DirectedEdge<int> e3(2, n3, n1);

CXXGraph::Graph<int> graph;
graph.addEdge(&e1);
graph.addEdge(&e2);
graph.addEdge(&e3);

ASSERT_TRUE(graph.isDirectedGraph());
ASSERT_FALSE(graph.isUndirectedGraph());
}

TEST(GraphTest, RawAddEdge_2) {
CXXGraph::Node<int> n1("a", 1);
CXXGraph::Node<int> n2("b", 1);
CXXGraph::Node<int> n3("c", 1);

CXXGraph::UndirectedEdge<int> e1(0, n1, n2);
CXXGraph::UndirectedEdge<int> e2(1, n2, n3);
CXXGraph::UndirectedEdge<int> e3(2, n3, n1);

CXXGraph::Graph<int> graph;
graph.addEdge(&e1);
graph.addEdge(&e2);
graph.addEdge(&e3);

ASSERT_FALSE(graph.isDirectedGraph());
ASSERT_TRUE(graph.isUndirectedGraph());
}

TEST(GraphTest, RawAddEdge_3) {
CXXGraph::Node<int> n1("a", 1);
CXXGraph::Node<int> n2("b", 1);
CXXGraph::Node<int> n3("c", 1);

CXXGraph::UndirectedEdge<int> e1(0, n1, n2);
CXXGraph::DirectedEdge<int> e2(1, n2, n3);
CXXGraph::UndirectedEdge<int> e3(2, n3, n1);

CXXGraph::Graph<int> graph;
graph.addEdge(&e1);
graph.addEdge(&e2);
graph.addEdge(&e3);

ASSERT_FALSE(graph.isDirectedGraph());
ASSERT_FALSE(graph.isUndirectedGraph());
}

TEST(GraphTest, adj_print_1) {
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
Expand Down Expand Up @@ -338,7 +392,8 @@ TEST(GraphTest, test_outEdges_shared) {
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge12));
CXXGraph::Graph<int> graph(edgeSet);

for (auto x : graph.outNeighbors(make_shared<const CXXGraph::Node<int>>(node1))) {
for (auto x :
graph.outNeighbors(make_shared<const CXXGraph::Node<int>>(node1))) {
ASSERT_TRUE(x == make_shared<const CXXGraph::Node<int>>(node2) ||
x == make_shared<const CXXGraph::Node<int>>(node3));
}
Expand Down Expand Up @@ -426,7 +481,8 @@ TEST(GraphTest, test_inOutEdges_shared) {
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<int>>(edge8));
CXXGraph::Graph<int> graph(edgeSet);

for (auto x : graph.inOutNeighbors(make_shared<const CXXGraph::Node<int>>(node1))) {
for (auto x :
graph.inOutNeighbors(make_shared<const CXXGraph::Node<int>>(node1))) {
ASSERT_TRUE(x == make_shared<const CXXGraph::Node<int>>(node2) ||
x == make_shared<const CXXGraph::Node<int>>(node3));
}
Expand Down