Skip to content

Commit

Permalink
Add more tests for addNode and removeNode methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sbaldu committed Sep 26, 2023
1 parent 8e8fb2e commit cc1e11f
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion test/GraphTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ TEST(ReverseDirectedGraphTest, test_exception) {
ASSERT_THROW(mixedGraph.reverseDirectedGraph(), std::runtime_error);
}

TEST(IsolatedNodeGraphTest, Test_1) {
TEST(IsolatedNodeGraphTest, Test_AddNode1) {
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);
Expand All @@ -706,3 +706,91 @@ TEST(IsolatedNodeGraphTest, Test_1) {
ASSERT_EQ(graph.getNodeSet().size(), 4);
ASSERT_EQ(graph.getEdgeSet().size(), 3);
}

TEST(IsolatedNodeGraphTest, Test_AddNode2) {
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;
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge3));
CXXGraph::Graph<int> graph(edgeSet);

// Create an isolated node and add it to the graph
CXXGraph::Node<int> node4("4", 4);
graph.addNode(&node4);

// Check that the number of nodes in the graph is 4
ASSERT_EQ(graph.getNodeSet().size(), 4);
ASSERT_EQ(graph.getEdgeSet().size(), 3);
}

TEST(TestRemoveNode, Test_isolatedNode) {
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;
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge3));
CXXGraph::Graph<int> graph(edgeSet);

// Create isolated nodes and add them to the graph
CXXGraph::Node<int> node4("4", 4);
CXXGraph::Node<int> node5("5", 5);
CXXGraph::Node<int> node6("6", 6);
graph.addNode(make_shared<CXXGraph::Node<int>>(node4));
graph.addNode(make_shared<CXXGraph::Node<int>>(node5));
graph.addNode(make_shared<CXXGraph::Node<int>>(node6));

// Check the initial number of edges and nodes
ASSERT_EQ(graph.getNodeSet().size(), 6);
ASSERT_EQ(graph.getEdgeSet().size(), 3);

// We now remove node 5
graph.removeNode("5");

// Check the final number of edges and nodes
ASSERT_EQ(graph.getNodeSet().size(), 5);
ASSERT_EQ(graph.getEdgeSet().size(), 3);
}

TEST(TestRemoveNode, Test_connectedNode) {
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;
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::Edge<int>>(edge3));
CXXGraph::Graph<int> graph(edgeSet);

// Create isolated nodes and add them to the graph
CXXGraph::Node<int> node4("4", 4);
CXXGraph::Node<int> node5("5", 5);
CXXGraph::Node<int> node6("6", 6);
graph.addNode(make_shared<CXXGraph::Node<int>>(node4));
graph.addNode(make_shared<CXXGraph::Node<int>>(node5));
graph.addNode(make_shared<CXXGraph::Node<int>>(node6));

// Check the initial number of edges and nodes
ASSERT_EQ(graph.getNodeSet().size(), 6);
ASSERT_EQ(graph.getEdgeSet().size(), 3);

// We now remove node 2
graph.removeNode("2");

// Check the final number of edges and nodes
ASSERT_EQ(graph.getNodeSet().size(), 5);
ASSERT_EQ(graph.getEdgeSet().size(), 1);
}

0 comments on commit cc1e11f

Please sign in to comment.