Skip to content

Commit

Permalink
Adding new add_vertex function and corresponding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
toadkarter committed Oct 3, 2023
1 parent 0ebb28d commit 23370f6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/graaflib/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ class graph {
*/
[[nodiscard]] vertex_id_t add_vertex(auto&& vertex);

/**
* Add a vertex to the graph with a specific ID
*
* @param vertex The vertex to be added
* @param id The requested ID for the new vertex
* @return vertices_id_t - The ID of the new vertex
* @throws id_taken exception - If the relevant ID is already in use
*/
[[nodiscard]] vertex_id_t add_vertex(auto&& vertex, vertex_id_t id);

/**
* Remove a vertex from the graph and update all its neighbors
*
Expand Down
12 changes: 12 additions & 0 deletions include/graaflib/graph.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ vertex_id_t graph<VERTEX_T, EDGE_T, GRAPH_TYPE_V>::add_vertex(auto&& vertex) {
return vertex_id;
}

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,
vertex_id_t id) {
if (has_vertex(id)) {
throw std::invalid_argument{"Vertex already exists at ID [" +
std::to_string(id) + "]"};
}

vertices_.emplace(id, std::forward<VERTEX_T>(vertex));
return id;
}

template <typename VERTEX_T, typename EDGE_T, graph_type GRAPH_TYPE_V>
void graph<VERTEX_T, EDGE_T, GRAPH_TYPE_V>::remove_vertex(
vertex_id_t vertex_id) {
Expand Down
25 changes: 25 additions & 0 deletions test/graaflib/graph_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ TYPED_TEST(GraphTest, VertexCount) {
ASSERT_EQ(graph.vertex_count(), 2);
ASSERT_TRUE(graph.has_vertex(vertex_id_2));
ASSERT_EQ(graph.get_vertex(vertex_id_2), 20);

// WHEN - THEN
constexpr int specific_id = 4;
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);
}

TYPED_TEST(GraphTest, RemoveVertex) {
Expand Down Expand Up @@ -173,6 +180,24 @@ TYPED_TEST(GraphTest, VertexTests) {
EXPECT_FALSE(graph.has_vertex(nonExistingVertexId));
EXPECT_TRUE(graph.has_vertex(vertex_id_2));
EXPECT_EQ(graph.get_vertex(vertex_id_2), 20);

ASSERT_THROW(
{
try {
// Add vertex to ID that already exists
[[maybe_unused]] const auto duplicate_id{
graph.add_vertex(50, vertex_id_1)};
FAIL()
<< "Expected std::invalid_argument exception, but no exception "
"was thrown.";
} catch (const std::invalid_argument &ex) {
EXPECT_EQ(ex.what(), fmt::format("Vertex already exists at ID [{}]",
vertex_id_1));
throw;
}
},
std::invalid_argument);
EXPECT_EQ(graph.get_vertex(vertex_id_1), 1);
}

TYPED_TEST(GraphTest, GetEdgeNonExistingEdge) {
Expand Down

0 comments on commit 23370f6

Please sign in to comment.