Skip to content

Commit

Permalink
Accounting for colliding IDs in get_vertex() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
toadkarter committed Oct 3, 2023
1 parent b19bbf1 commit d9f2dc8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions include/graaflib/graph.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ graph<VERTEX_T, EDGE_T, GRAPH_TYPE_V>::get_neighbors(

template <typename VERTEX_T, typename EDGE_T, graph_type GRAPH_TYPE_V>
vertex_id_t graph<VERTEX_T, EDGE_T, GRAPH_TYPE_V>::add_vertex(auto&& vertex) {
// TODO: check overflow
const auto vertex_id{vertex_id_supplier_++};
while (has_vertex(vertex_id_supplier_)) {
vertex_id_supplier_++;
}
const auto vertex_id{vertex_id_supplier_};
vertices_.emplace(vertex_id, std::forward<VERTEX_T>(vertex));
return vertex_id;
}
Expand Down
10 changes: 9 additions & 1 deletion test/graaflib/graph_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ TYPED_TEST(GraphTest, VertexCount) {
ASSERT_EQ(graph.get_vertex(vertex_id_2), 20);

// WHEN - THEN
constexpr int specific_id = 4;
constexpr int specific_id = 2;
const auto vertex_specific_id{graph.add_vertex(30, specific_id)};
ASSERT_EQ(graph.vertex_count(), 3);
ASSERT_TRUE(graph.has_vertex(specific_id));
ASSERT_EQ(graph.get_vertex(specific_id), 30);

// WHEN - THEN
constexpr int colliding_id = 2;
const auto vertex_id_4{graph.add_vertex(40)};
ASSERT_EQ(graph.vertex_count(), 4);
ASSERT_TRUE(graph.has_vertex(colliding_id + 1));
ASSERT_EQ(graph.get_vertex(colliding_id), 30);
ASSERT_EQ(graph.get_vertex(colliding_id + 1), 40);
}

TYPED_TEST(GraphTest, RemoveVertex) {
Expand Down

0 comments on commit d9f2dc8

Please sign in to comment.