-
Notifications
You must be signed in to change notification settings - Fork 123
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
Bug Fix for #418 - Graph<T>::removeNode has potential to throw due to optional being accessed early #430
Merged
Merged
Bug Fix for #418 - Graph<T>::removeNode has potential to throw due to optional being accessed early #430
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0be3518
optional should first check whether the node indeed exists
Ajay-26 5ced829
fixed typo in impl and also added test for removing node that was nev…
Ajay-26 d8d07c1
incorporated review into PR
Ajay-26 7f1efe6
resolved use after free for map iterator
Ajay-26 4489104
reformatted code as per PR feedback
Ajay-26 6f4e654
reformatted code as per PR feedback
Ajay-26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sbaldu marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1340,6 +1340,45 @@ TEST(TestRemoveNode, Test_connectedNode) { | |
ASSERT_EQ(graph.getEdgeSet().size(), 1); | ||
} | ||
|
||
TEST(TestRemoveNode, Test_removeInvalidNode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
/** Test to call the remove_node function on a node that was never added. In this case getNode will return an optional that is nullptr*/ | ||
// Create a graph with 3 nodes and 3 edges. | ||
CXXGraph::Node<int> node1("1", 1); | ||
CXXGraph::Node<int> node2("2", 2); | ||
CXXGraph::Node<int> node3("3", 3); | ||
CXXGraph::DirectedEdge<int> edge1(1, node1, node2); | ||
CXXGraph::DirectedEdge<int> edge2(2, node2, node1); | ||
CXXGraph::DirectedEdge<int> edge3(3, node1, node3); | ||
CXXGraph::T_EdgeSet<int> edgeSet; | ||
// Add the 3 edges into the graph. | ||
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge1)); | ||
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge2)); | ||
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge3)); | ||
// Initialise the graph | ||
CXXGraph::Graph<int> graph(edgeSet); | ||
|
||
// Check the initial number of edges and nodes. Everything should be okay so far | ||
ASSERT_EQ(graph.getNodeSet().size(), 3); | ||
ASSERT_EQ(graph.getEdgeSet().size(), 3); | ||
|
||
// Remove a node that was never in the graph | ||
graph.removeNode("4"); | ||
|
||
// Number of nodes and edges in the graph should remain the same | ||
ASSERT_EQ(graph.getNodeSet().size(), 3); | ||
ASSERT_EQ(graph.getEdgeSet().size(), 3); | ||
|
||
// Remove an existing node, the edge associated with that node should also be removed. Node "3" had just outgoing edge, so there should now be 2 nodes and 2 edges. | ||
graph.removeNode("3"); | ||
ASSERT_EQ(graph.getNodeSet().size(), 2); | ||
ASSERT_EQ(graph.getEdgeSet().size(), 2); | ||
|
||
// Remove the node that had already been removed. Should not change anything about the graph now, similar to when "4" was removed above | ||
graph.removeNode("3"); | ||
ASSERT_EQ(graph.getNodeSet().size(), 2); | ||
ASSERT_EQ(graph.getEdgeSet().size(), 2); | ||
} | ||
|
||
TEST(TestGetNode, Test_1) { | ||
CXXGraph::Node<int> node1("1", 1); | ||
CXXGraph::Node<int> node2("2", 2); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems ok, but it would be more concise if the entire downstream logic were gated by
nodeOpt
, i.e.:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your feedback, I made a commit with the suggested edits - let me know if it looks okay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Approved