Skip to content

Commit

Permalink
Add test of set data for isolated node
Browse files Browse the repository at this point in the history
  • Loading branch information
sbaldu committed Sep 26, 2023
1 parent 02dcf69 commit e24b8ea
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/GraphTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,3 +821,56 @@ TEST(TestGetNode, Test_1) {
auto node_notfound = graph.getNode("5");
ASSERT_FALSE(node_notfound.has_value());
}

TEST(GraphTest, set_data_isolated) {
// Create the graph
CXXGraph::Node<int> node1("1", 1);
CXXGraph::Node<int> node2("2", 2);
CXXGraph::Node<int> node3("3", 3);
std::pair<const CXXGraph::Node<int> *, const CXXGraph::Node<int> *> pairNode(
&node1, &node2);
CXXGraph::DirectedEdge<int> edge1(1, pairNode);
CXXGraph::DirectedEdge<int> edge2(2, node2, node3);
CXXGraph::UndirectedEdge<int> edge3(3, node1, node3);
CXXGraph::T_EdgeSet<int> edgeSet;
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge1));
edgeSet.insert(make_shared<CXXGraph::DirectedEdge<int>>(edge2));
edgeSet.insert(make_shared<CXXGraph::UndirectedEdge<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));

std::map<std::string, int> initial_values;
// Construct map with the initial values of the nodes data
for (const auto &nodeIt : graph.getNodeSet()) {
initial_values[nodeIt->getUserId()] = nodeIt->getData();
}
// Change the data contained in the nodes singularly
std::map<std::string, int> new_values;
for (const auto &nodeIt : graph.getNodeSet()) {
int r = std::rand();
graph.setNodeData(nodeIt->getUserId(), r);
new_values[nodeIt->getUserId()] = r;
}
// Check the final values of the node data
for (const auto &nodeIt : graph.getNodeSet()) {
ASSERT_EQ(nodeIt->getData(), new_values[nodeIt->getUserId()]);
}

// Now set the data of all the nodes at once
std::map<std::string, int> data_values;
for (const auto &nodeIt : graph.getNodeSet()) {
int r = std::rand();
data_values[nodeIt->getUserId()] = r;
}
graph.setNodeData(data_values);
for (const auto &nodeIt : graph.getNodeSet()) {
ASSERT_EQ(nodeIt->getData(), data_values[nodeIt->getUserId()]);
}
}

0 comments on commit e24b8ea

Please sign in to comment.