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 construction of weighted Edge in addEdge overload #360

Merged
merged 2 commits into from
Oct 3, 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
6 changes: 4 additions & 2 deletions include/CXXGraph/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,8 @@ template <typename T>
void Graph<T>::addEdge(const Edge<T> *edge) {
if (edge->isDirected().has_value() && edge->isDirected().value()) {
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
auto edge_shared = make_shared<DirectedWeightedEdge<T>>(*edge);
auto edge_shared = make_shared<DirectedWeightedEdge<T>>(
*dynamic_cast<const DirectedWeightedEdge<T> *>(edge));
this->edgeSet.insert(edge_shared);

std::pair<shared<const Node<T>>, shared<const Edge<T>>> elem = {
Expand All @@ -879,7 +880,8 @@ void Graph<T>::addEdge(const Edge<T> *edge) {
}
} else {
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
auto edge_shared = make_shared<UndirectedWeightedEdge<T>>(*edge);
auto edge_shared = make_shared<UndirectedWeightedEdge<T>>(
*dynamic_cast<const UndirectedWeightedEdge<T> *>(edge));
this->edgeSet.insert(edge_shared);

std::pair<shared<const Node<T>>, shared<const Edge<T>>> elem = {
Expand Down
41 changes: 41 additions & 0 deletions test/GraphTest.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

#include <CXXGraph/Edge/DirectedWeightedEdge.hpp>
#include <CXXGraph/Edge/UndirectedWeightedEdge.hpp>
#include <CXXGraph/Edge/Weighted.hpp>
#include <memory>

#include "CXXGraph/CXXGraph.hpp"
Expand Down Expand Up @@ -207,6 +210,44 @@ TEST(GraphTest, RawAddEdge_3) {
ASSERT_FALSE(graph.isUndirectedGraph());
}

TEST(GraphTest, AddEdgeWeight_raw) {
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 1);
CXXGraph::Node<int> node3("3", 1);
CXXGraph::DirectedWeightedEdge<int> edge1(1, node1, node2, 3);
CXXGraph::UndirectedWeightedEdge<int> edge2(2, node1, node3, 5);
CXXGraph::Graph<int> graph;

graph.addEdge(&edge1);
graph.addEdge(&edge2);

// Check that the edges are weighted
ASSERT_TRUE((*graph.getEdge(1))->isWeighted());
ASSERT_TRUE((*graph.getEdge(2))->isWeighted());
// Check the value of the weights
ASSERT_EQ(std::dynamic_pointer_cast<const CXXGraph::Weighted>(*graph.getEdge(1))->getWeight(), 3);
ASSERT_EQ(std::dynamic_pointer_cast<const CXXGraph::Weighted>(*graph.getEdge(2))->getWeight(), 5);
}

TEST(GraphTest, AddEdgeWeight_shared) {
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 1);
CXXGraph::Node<int> node3("3", 1);
CXXGraph::DirectedWeightedEdge<int> edge1(1, node1, node2, 3);
CXXGraph::UndirectedWeightedEdge<int> edge2(2, node1, node3, 5);
CXXGraph::Graph<int> graph;

graph.addEdge(make_shared<const CXXGraph::DirectedWeightedEdge<int>>(edge1));
graph.addEdge(make_shared<const CXXGraph::UndirectedWeightedEdge<int>>(edge2));

// Check that the edges are weighted
ASSERT_TRUE((*graph.getEdge(1))->isWeighted());
ASSERT_TRUE((*graph.getEdge(2))->isWeighted());
// Check the value of the weights
ASSERT_EQ(std::dynamic_pointer_cast<const CXXGraph::Weighted>(*graph.getEdge(1))->getWeight(), 3);
ASSERT_EQ(std::dynamic_pointer_cast<const CXXGraph::Weighted>(*graph.getEdge(2))->getWeight(), 5);
}

TEST(GraphTest, DirectedEdgeCycle_1) {
CXXGraph::Node<int> node1("a", 1);
CXXGraph::Node<int> node2("b", 1);
Expand Down
Loading