From ac67f77201b26c23191bc3fd9bab59cc8bca07c5 Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 18 Dec 2024 17:53:10 +0300 Subject: [PATCH 01/17] added a graph class --- CMakeLists.txt | 22 +++-- include/CMakeLists.txt | 0 include/graph/graph.h | 47 +++++++++++ src/CMakeLists.txt | 0 src/graph/CMakeLists.txt | 7 ++ src/graph/graph.cpp | 146 +++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 14 ++-- test/main.cpp | 2 +- test/test_graph.cpp | 173 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 396 insertions(+), 15 deletions(-) delete mode 100644 include/CMakeLists.txt create mode 100644 include/graph/graph.h delete mode 100644 src/CMakeLists.txt create mode 100644 src/graph/CMakeLists.txt create mode 100644 src/graph/graph.cpp create mode 100644 test/test_graph.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a8e8360..be0cd36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,19 @@ -cmake_minimum_required(VERSION 3.20) +cmake_minimum_required(VERSION 3.15) -project(cpp_template) +set(ProjectName "itlab") -include(cmake/configure.cmake) +project(${ProjectName}) include_directories(include) -enable_testing() - -add_subdirectory(3rdparty) -add_subdirectory(app) -add_subdirectory(include) -add_subdirectory(src) +add_subdirectory(3rdparty/googletest) +add_subdirectory(src/graph) add_subdirectory(test) + +# REPORT +message( STATUS "") +message( STATUS "General configuration for ${PROJECT_NAME}") +message( STATUS "======================================") +message( STATUS "") +message( STATUS " Configuration: ${CMAKE_BUILD_TYPE}") +message( STATUS "") \ No newline at end of file diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt deleted file mode 100644 index e69de29..0000000 diff --git a/include/graph/graph.h b/include/graph/graph.h new file mode 100644 index 0000000..0c68d68 --- /dev/null +++ b/include/graph/graph.h @@ -0,0 +1,47 @@ +#ifndef GRAPH_H +#define GRAPH_H + +#include +#include +#include +#include + +class Vertex { +private: + int id; + std::list neighbors; + +public: + Vertex(int id); + void addNeighbor(int neighbor); + void removeNeighbor(int neighbor); + void print() const; + int getId() const; + const std::list& getNeighbors() const; +}; + + + +class Graph { +private: + std::unordered_map vertices; + +public: + + Graph(); + + void addVertex(int id); + void getVertex() const; + void addEdge(int u, int v); + void removeEdge(int u, int v); + void removeVertex(int id); + int vertexCount() const; + int edgeCount() const; + bool empty() const; + void printGraph() const; + bool hasPath(int u, int v); + std::vector BFS(int start); + ~Graph(); +}; + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/graph/CMakeLists.txt b/src/graph/CMakeLists.txt new file mode 100644 index 0000000..af77d7d --- /dev/null +++ b/src/graph/CMakeLists.txt @@ -0,0 +1,7 @@ +file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h") +file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp") + +add_library(${ProjectName} STATIC ${SOURCE_FILES} ${HEADER_FILES}) +target_sources(${ProjectName} PRIVATE ${HEADER_FILES}) + +target_include_directories(${ProjectName} PUBLIC ${CMAKE_SOURCE_DIR}/src) \ No newline at end of file diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp new file mode 100644 index 0000000..c3cc8b0 --- /dev/null +++ b/src/graph/graph.cpp @@ -0,0 +1,146 @@ +#include "./graph/graph.h" + +Vertex::Vertex(int id) : id(id) {} + +void Vertex::addNeighbor(int neighbor) { + if (neighbor != id) { + neighbors.push_back(neighbor); + } +} + +void Vertex::removeNeighbor(int neighbor) { + neighbors.remove(neighbor); +} + +void Vertex::print() const { + std::cout << id << ": "; + for (const int& neighbor : neighbors) { + std::cout << neighbor << " "; + } + std::cout << std::endl; +} + +int Vertex::getId() const { + return id; +} + +const std::list& Vertex::getNeighbors() const { + return neighbors; +} + +Graph::Graph() {} + +void Graph::addVertex(int id) { + if (vertices.find(id) == vertices.end()) { + vertices[id] = new Vertex(id); + } +} + +void Graph::getVertex() const { + if (!this->empty()) { + for (auto it = vertices.begin(); it != vertices.end(); it++) + std::cout << it->first << " "; + std::cout << std::endl; + } +} + +void Graph::addEdge(int u, int v) { + if (vertices.find(u) == vertices.end()) { addVertex(u); } + if (vertices.find(v) == vertices.end()) { addVertex(v); } + vertices[u]->addNeighbor(v); +} + +void Graph::removeEdge(int u, int v) { + if (vertices.find(u) != vertices.end()) { + vertices[u]->removeNeighbor(v); + } +} + +void Graph::removeVertex(int id) { + for (auto& pair : vertices) { pair.second->removeNeighbor(id); } + auto it = vertices.find(id); + if (it != vertices.end()) { + delete it->second; + vertices.erase(it); + } +} + +int Graph::vertexCount() const { + int count = 0; + for (auto it = vertices.begin(); it != vertices.end(); it++) + count++; + return count; +} + +int Graph::edgeCount() const { + int count = 0; + for (auto it = vertices.begin(); it != vertices.end(); it++) { + count += (it->second->getNeighbors()).size(); + } + return count; +} + + +bool Graph::empty() const { + return vertices.empty(); +} + +void Graph::printGraph() const { + for (const auto& pair : vertices) { + pair.second->print(); + } +} + +bool Graph::hasPath(int u, int v) { + if (vertices.find(u) != vertices.end() && vertices.find(v) != vertices.end()) { + std::unordered_map visited; + std::queue queue; + queue.push(u); + + while (!queue.empty()) { + int current = queue.front(); + queue.pop(); + if (current == v) { return true; } + visited[current] = true; + + for (const int& neighbor : vertices[current]->getNeighbors()) + if (!visited[neighbor]) + queue.push(neighbor); + + } + return false; + } +} + +std::vector Graph::BFS(int start) { + std::vector traversal_order; + std::unordered_map visited; + std::queue queue; + + visited[start] = true; + queue.push(start); + + while (!queue.empty()) { + int current = queue.front(); + queue.pop(); + traversal_order.push_back(current); + + if (vertices.find(current) != vertices.end()) { + for (const int& neighbor : vertices[current]->getNeighbors()) { + if (!visited[neighbor]) { + visited[neighbor] = true; + queue.push(neighbor); + } + } + } + } + //for (int i : traversal_order) + // std::cout << i << " "; + return traversal_order; +} + +Graph::~Graph() { + for (auto& pair : vertices) { + delete pair.second; + } +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9dada0c..14bf041 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,10 @@ -file(GLOB_RECURSE TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) +file(GLOB_RECURSE TEST_FILES ./*.cpp) -add_executable(run_tests ${TEST_SRC_FILES}) -target_link_libraries(run_tests PUBLIC - gtest_main -) +set(TestsName "Tests") + +add_executable(${TestsName} ${TEST_FILES}) + +target_link_libraries(${TestsName} PRIVATE ${ProjectName} gtest) + +enable_testing() +add_test(NAME ${TestsName} COMMAND ${TestsName}) \ No newline at end of file diff --git a/test/main.cpp b/test/main.cpp index 4d820af..a0bda37 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,4 +1,4 @@ -#include +#include int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/test/test_graph.cpp b/test/test_graph.cpp new file mode 100644 index 0000000..d0fe2c0 --- /dev/null +++ b/test/test_graph.cpp @@ -0,0 +1,173 @@ +#include "./graph/graph.h" +#include "./graph/graph.cpp" +#include + + +TEST(Graph, can_create_graph) +{ + ASSERT_NO_THROW(Graph g); +} + +TEST(Graph, can_add_vertex_to_graph) +{ + Graph g; + + ASSERT_NO_THROW(g.addVertex(1)); +} + +TEST(Graph, can_add_edge_to_graph) +{ + Graph g; + + ASSERT_NO_THROW(g.addEdge(1,0)); +} + +TEST(Graph, can_add_edge_with_same_id_to_graph) +{ + Graph g; + + ASSERT_NO_THROW(g.addEdge(1, 1)); +} + +TEST(Graph, can_add_vertex_and_edge_to_graph) +{ + Graph g; + + ASSERT_NO_THROW(g.addEdge(1, 0)); + ASSERT_NO_THROW(g.addVertex(1)); +} + +TEST(Graph, can_get_vertex) +{ + Graph g; + g.addEdge(1, 0); + + ASSERT_NO_THROW(g.getVertex()); +} + +TEST(Graph, can_remove_vertex) +{ + Graph g; + g.addEdge(1, 0); + + g.removeVertex(1); + + ASSERT_EQ(g.vertexCount(),1); +} + +TEST(Graph, can_get_vertex_count) +{ + Graph g; + + g.addEdge(1, 0); + g.addVertex(2); + g.addVertex(3); + g.removeVertex(1); + g.addVertex(1); + g.removeVertex(3); + + ASSERT_EQ(g.vertexCount(), 3); +} + +TEST(Graph, can_get_edge_count) +{ + Graph g; + + g.addEdge(1, 0); + g.addEdge(2, 0); + g.addEdge(0, 2); + g.addEdge(2, 2); + + ASSERT_EQ(g.edgeCount(), 3); +} + +TEST(Graph, check_graph_is_empty) +{ + Graph g; + + ASSERT_TRUE(g.empty()); +} + +TEST(Graph, check_graph_is_not_empty) +{ + Graph g; + + g.addEdge(1, 0); + + ASSERT_FALSE(g.empty()); +} + +TEST(Graph, check_graph_no_path_between_vertexes) +{ + Graph g; + + g.addEdge(0,1); + g.addEdge(0, 3); + g.addEdge(1, 2); + g.addEdge(3, 1); + + ASSERT_FALSE(g.hasPath(1,0)); +} + +TEST(Graph, check_graph_has_path_between_vertexes) +{ + Graph g; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + g.addEdge(3, 1); + + ASSERT_TRUE(g.hasPath(3,1)); +} + +TEST(Graph, check_graph_can_find_path_between_vertexes_after_delete) +{ + Graph g; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + g.addEdge(3, 1); + g.removeVertex(3); + + ASSERT_FALSE(g.hasPath(3, 1)); + ASSERT_FALSE(g.hasPath(0, 3)); +} + +TEST(Graph, can_create_bfs_path) +{ + Graph g; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + + ASSERT_NO_THROW(g.BFS(0)); +} + +TEST(Graph, can_create_bfs_path_in_empty_graph) +{ + Graph g; + + ASSERT_NO_THROW(g.BFS(0)); +} + +TEST(Graph, check_bfs_path) +{ + Graph g; + std::vector v1 = { 0,1,3,2,4,6,7,5,8,9 }; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + g.addEdge(3, 4); + g.addEdge(4, 5); + g.addEdge(3,6); + g.addEdge(3, 7); + g.addEdge(7,8); + g.addEdge(8, 9); + std::vector v = g.BFS(0); + + ASSERT_EQ(v, v1); +} \ No newline at end of file From 5f36230bfedf2d85a8178acdbea96ff1e4448591 Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 20 Dec 2024 02:29:18 +0300 Subject: [PATCH 02/17] gtest dir fix --- CMakeLists.txt | 2 +- test/main.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index be0cd36..75658c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ project(${ProjectName}) include_directories(include) -add_subdirectory(3rdparty/googletest) +add_subdirectory(3rdparty/googletest/gtest) add_subdirectory(src/graph) add_subdirectory(test) diff --git a/test/main.cpp b/test/main.cpp index a0bda37..47f4482 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -3,4 +3,4 @@ int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} +} \ No newline at end of file From 48679371dd0dbce14b7094e026f0a6e8ddb740ea Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 25 Feb 2025 17:47:16 +0300 Subject: [PATCH 03/17] CMake fix 1.0 --- CMakeLists.txt | 4 +-- include/graph/graph.h | 2 +- src/graph/graph.cpp | 72 ++++++++++++++++++++----------------------- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 75658c8..76e8ec1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.20) set(ProjectName "itlab") @@ -6,7 +6,7 @@ project(${ProjectName}) include_directories(include) -add_subdirectory(3rdparty/googletest/gtest) +add_subdirectory(3rdparty) add_subdirectory(src/graph) add_subdirectory(test) diff --git a/include/graph/graph.h b/include/graph/graph.h index 0c68d68..b0d0249 100644 --- a/include/graph/graph.h +++ b/include/graph/graph.h @@ -29,7 +29,6 @@ class Graph { public: Graph(); - void addVertex(int id); void getVertex() const; void addEdge(int u, int v); @@ -39,6 +38,7 @@ class Graph { int edgeCount() const; bool empty() const; void printGraph() const; + bool bfs_helper(int start, int vert, bool flag, std::vector* v_ord); bool hasPath(int u, int v); std::vector BFS(int start); ~Graph(); diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp index c3cc8b0..b650d86 100644 --- a/src/graph/graph.cpp +++ b/src/graph/graph.cpp @@ -92,51 +92,47 @@ void Graph::printGraph() const { } bool Graph::hasPath(int u, int v) { - if (vertices.find(u) != vertices.end() && vertices.find(v) != vertices.end()) { - std::unordered_map visited; - std::queue queue; - queue.push(u); + if (vertices.find(u) == vertices.end() || + vertices.find(v) == vertices.end()) { + return false; + } + return bfs_helper(u, v, true, nullptr); +} - while (!queue.empty()) { - int current = queue.front(); - queue.pop(); - if (current == v) { return true; } - visited[current] = true; +std::vector Graph::BFS(int start) { + std::vector v_ord; + bfs_helper(start, -1, false, &v_ord); + return v_ord; +} - for (const int& neighbor : vertices[current]->getNeighbors()) - if (!visited[neighbor]) - queue.push(neighbor); +bool Graph::bfs_helper(int start, int vert, bool flag, std::vector* v_ord) { + std::unordered_map visited; + std::queue queue; - } - return false; + queue.push(start); + visited[start] = true; + + while (!queue.empty()) { + int current = queue.front(); + queue.pop(); + + if (flag && current == vert) { + return true; + } + if (v_ord != nullptr) { + v_ord->push_back(current); } -} -std::vector Graph::BFS(int start) { - std::vector traversal_order; - std::unordered_map visited; - std::queue queue; - - visited[start] = true; - queue.push(start); - - while (!queue.empty()) { - int current = queue.front(); - queue.pop(); - traversal_order.push_back(current); - - if (vertices.find(current) != vertices.end()) { - for (const int& neighbor : vertices[current]->getNeighbors()) { - if (!visited[neighbor]) { - visited[neighbor] = true; - queue.push(neighbor); - } - } + if (vertices.find(current) != vertices.end()) { + for (const int& neighbor : vertices[current]->getNeighbors()) { + if (!visited[neighbor]) { + visited[neighbor] = true; + queue.push(neighbor); } + } } - //for (int i : traversal_order) - // std::cout << i << " "; - return traversal_order; + } + return false; } Graph::~Graph() { From fb575540f48ef7ca8a6c426321aa15d930522baa Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 25 Feb 2025 18:05:19 +0300 Subject: [PATCH 04/17] CMake fix 2.0 --- test/main.cpp | 2 +- test/test_graph.cpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/test/main.cpp b/test/main.cpp index 47f4482..1659be8 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,4 +1,4 @@ -#include +#include "gtest/gtest.h" int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/test/test_graph.cpp b/test/test_graph.cpp index d0fe2c0..45cf3dc 100644 --- a/test/test_graph.cpp +++ b/test/test_graph.cpp @@ -1,6 +1,5 @@ -#include "./graph/graph.h" -#include "./graph/graph.cpp" -#include +#include "gtest/gtest.h" +#include "graph/graph.cpp" TEST(Graph, can_create_graph) From f2afeea15f5ec92b491b0fa2e2be258ab0317296 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 25 Feb 2025 18:19:02 +0300 Subject: [PATCH 05/17] CMake fix 2.0 --- include/graph/graph.h | 65 +++++++++++++++++++++---------------------- test/main.cpp | 1 + test/test_graph.cpp | 1 - 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/include/graph/graph.h b/include/graph/graph.h index b0d0249..1dcdd53 100644 --- a/include/graph/graph.h +++ b/include/graph/graph.h @@ -1,47 +1,44 @@ -#ifndef GRAPH_H +#ifndef GRAPH_H #define GRAPH_H #include -#include #include #include +#include class Vertex { -private: - int id; - std::list neighbors; - -public: - Vertex(int id); - void addNeighbor(int neighbor); - void removeNeighbor(int neighbor); - void print() const; - int getId() const; - const std::list& getNeighbors() const; + private: + int id; + std::list neighbors; + + public: + Vertex(int id); + void addNeighbor(int neighbor); + void removeNeighbor(int neighbor); + void print() const; + int getId() const; + const std::list& getNeighbors() const; }; - - class Graph { -private: - std::unordered_map vertices; - -public: - - Graph(); - void addVertex(int id); - void getVertex() const; - void addEdge(int u, int v); - void removeEdge(int u, int v); - void removeVertex(int id); - int vertexCount() const; - int edgeCount() const; - bool empty() const; - void printGraph() const; - bool bfs_helper(int start, int vert, bool flag, std::vector* v_ord); - bool hasPath(int u, int v); - std::vector BFS(int start); - ~Graph(); + private: + std::unordered_map vertices; + + public: + Graph(); + void addVertex(int id); + void getVertex() const; + void addEdge(int u, int v); + void removeEdge(int u, int v); + void removeVertex(int id); + int vertexCount() const; + int edgeCount() const; + bool empty() const; + void printGraph() const; + bool bfs_helper(int start, int vert, bool flag, std::vector* v_ord); + bool hasPath(int u, int v); + std::vector BFS(int start); + ~Graph(); }; #endif diff --git a/test/main.cpp b/test/main.cpp index 1659be8..d6efe14 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,5 +1,6 @@ #include "gtest/gtest.h" + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff --git a/test/test_graph.cpp b/test/test_graph.cpp index 45cf3dc..e900440 100644 --- a/test/test_graph.cpp +++ b/test/test_graph.cpp @@ -1,7 +1,6 @@ #include "gtest/gtest.h" #include "graph/graph.cpp" - TEST(Graph, can_create_graph) { ASSERT_NO_THROW(Graph g); From 82f0dc44ed03575591e4fa988a308a89491e8157 Mon Sep 17 00:00:00 2001 From: Vlad Date: Thu, 27 Feb 2025 18:06:24 +0300 Subject: [PATCH 06/17] clang-tidy, clang-format and ubuntu-build fix 1.0 --- include/graph/graph.h | 15 ++--- src/graph/graph.cpp | 146 ++++++++++++++++++------------------------ test/CMakeLists.txt | 2 +- test/test_graph.cpp | 3 +- 4 files changed, 74 insertions(+), 92 deletions(-) diff --git a/include/graph/graph.h b/include/graph/graph.h index 1dcdd53..bf2b478 100644 --- a/include/graph/graph.h +++ b/include/graph/graph.h @@ -1,18 +1,17 @@ #ifndef GRAPH_H #define GRAPH_H -#include #include -#include #include +#include class Vertex { private: - int id; - std::list neighbors; + int id_; + std::list neighbors_; public: - Vertex(int id); + Vertex(int id_); void addNeighbor(int neighbor); void removeNeighbor(int neighbor); void print() const; @@ -22,15 +21,15 @@ class Vertex { class Graph { private: - std::unordered_map vertices; + std::unordered_map vertices_; public: Graph(); - void addVertex(int id); + void addVertex(int id_); void getVertex() const; void addEdge(int u, int v); void removeEdge(int u, int v); - void removeVertex(int id); + void removeVertex(int id_); int vertexCount() const; int edgeCount() const; bool empty() const; diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp index b650d86..679fcc5 100644 --- a/src/graph/graph.cpp +++ b/src/graph/graph.cpp @@ -1,114 +1,89 @@ #include "./graph/graph.h" -Vertex::Vertex(int id) : id(id) {} +#include +#include +#include +#include + +Vertex::Vertex(int id_) : id_(id_) {} void Vertex::addNeighbor(int neighbor) { - if (neighbor != id) { - neighbors.push_back(neighbor); - } + if (neighbor != id_) { neighbors_.push_back(neighbor); } } -void Vertex::removeNeighbor(int neighbor) { - neighbors.remove(neighbor); -} +void Vertex::removeNeighbor(int neighbor) { neighbors_.remove(neighbor); } void Vertex::print() const { - std::cout << id << ": "; - for (const int& neighbor : neighbors) { - std::cout << neighbor << " "; - } - std::cout << std::endl; + std::cout << id_ << ": "; + for (const int& neighbor : neighbors_) { + std::cout << neighbor << " "; + } + std::cout << '\n'; } -int Vertex::getId() const { - return id; -} +int Vertex::getId() const { return id_; } -const std::list& Vertex::getNeighbors() const { - return neighbors; -} +const std::list& Vertex::getNeighbors() const { return neighbors_; } -Graph::Graph() {} +Graph::Graph() = default; -void Graph::addVertex(int id) { - if (vertices.find(id) == vertices.end()) { - vertices[id] = new Vertex(id); - } +void Graph::addVertex(int id_) { + if (vertices_.find(id_) == vertices_.end()) { vertices_[id_] = new Vertex(id_); } } void Graph::getVertex() const { - if (!this->empty()) { - for (auto it = vertices.begin(); it != vertices.end(); it++) - std::cout << it->first << " "; - std::cout << std::endl; + if (!this->empty()) { + for (auto& vertice : vertices_) { + std::cout << vertice.first << " "; } + std::cout << '\n'; + } } void Graph::addEdge(int u, int v) { - if (vertices.find(u) == vertices.end()) { addVertex(u); } - if (vertices.find(v) == vertices.end()) { addVertex(v); } - vertices[u]->addNeighbor(v); + if (vertices_.find(u) == vertices_.end()) { addVertex(u); } + if (vertices_.find(v) == vertices_.end()) { addVertex(v); } + vertices_[u]->addNeighbor(v); } void Graph::removeEdge(int u, int v) { - if (vertices.find(u) != vertices.end()) { - vertices[u]->removeNeighbor(v); - } + if (vertices_.find(u) != vertices_.end()) { vertices_[u]->removeNeighbor(v); } } -void Graph::removeVertex(int id) { - for (auto& pair : vertices) { pair.second->removeNeighbor(id); } - auto it = vertices.find(id); - if (it != vertices.end()) { - delete it->second; - vertices.erase(it); - } +void Graph::removeVertex(int id_) { + for (auto& pair : vertices_) { + pair.second->removeNeighbor(id_); + } + auto it = vertices_.find(id_); + if (it != vertices_.end()) { + delete it->second; + vertices_.erase(it); + } } int Graph::vertexCount() const { - int count = 0; - for (auto it = vertices.begin(); it != vertices.end(); it++) - count++; - return count; + int count = 0; + for (auto& vertice : vertices_) { + count++; + } + return count; } int Graph::edgeCount() const { - int count = 0; - for (auto it = vertices.begin(); it != vertices.end(); it++) { - count += (it->second->getNeighbors()).size(); - } - return count; + int count = 0; + for (auto it = vertices_.begin(); it != vertices_.end(); it++) count += (it->second->getNeighbors()).size(); + return count; } - -bool Graph::empty() const { - return vertices.empty(); -} +bool Graph::empty() const { return vertices_.empty(); } void Graph::printGraph() const { - for (const auto& pair : vertices) { - pair.second->print(); - } -} - -bool Graph::hasPath(int u, int v) { - if (vertices.find(u) == vertices.end() || - vertices.find(v) == vertices.end()) { - return false; - } - return bfs_helper(u, v, true, nullptr); -} - -std::vector Graph::BFS(int start) { - std::vector v_ord; - bfs_helper(start, -1, false, &v_ord); - return v_ord; + for (const auto& pair : vertices_) { pair.second->print(); } } bool Graph::bfs_helper(int start, int vert, bool flag, std::vector* v_ord) { std::unordered_map visited; std::queue queue; - queue.push(start); visited[start] = true; @@ -116,15 +91,11 @@ bool Graph::bfs_helper(int start, int vert, bool flag, std::vector* v_ord) int current = queue.front(); queue.pop(); - if (flag && current == vert) { - return true; - } - if (v_ord != nullptr) { - v_ord->push_back(current); - } + if (flag && current == vert) return true; + if (v_ord != nullptr) v_ord->push_back(current); - if (vertices.find(current) != vertices.end()) { - for (const int& neighbor : vertices[current]->getNeighbors()) { + if (vertices_.find(current) != vertices_.end()) { + for (const int& neighbor : vertices_[current]->getNeighbors()) { if (!visited[neighbor]) { visited[neighbor] = true; queue.push(neighbor); @@ -135,8 +106,19 @@ bool Graph::bfs_helper(int start, int vert, bool flag, std::vector* v_ord) return false; } +bool Graph::hasPath(int u, int v) { + if (vertices_.find(u) == vertices_.end() || vertices_.find(v) == vertices_.end()) return false; + return bfs_helper(u, v, true, nullptr); +} + +std::vector Graph::BFS(int start) { + std::vector v_ord; + bfs_helper(start, -1, false, &v_ord); + return v_ord; +} + Graph::~Graph() { - for (auto& pair : vertices) { - delete pair.second; - } + for (auto& pair : vertices_) { + delete pair.second; + } } \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 14bf041..0ab479f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ file(GLOB_RECURSE TEST_FILES ./*.cpp) -set(TestsName "Tests") +set(TestsName "run_tests") add_executable(${TestsName} ${TEST_FILES}) diff --git a/test/test_graph.cpp b/test/test_graph.cpp index e900440..48942ad 100644 --- a/test/test_graph.cpp +++ b/test/test_graph.cpp @@ -1,5 +1,6 @@ #include "gtest/gtest.h" -#include "graph/graph.cpp" +#include "graph/graph.h" +#include TEST(Graph, can_create_graph) { From 2f1141a56c469d55b7984d4af7f5cffaa6998555 Mon Sep 17 00:00:00 2001 From: Vlad Date: Thu, 27 Feb 2025 18:57:27 +0300 Subject: [PATCH 07/17] clang-tidy, clang-format, ubuntu-build and cmake fix 2.0 --- CMakeLists.txt | 11 ++- include/CMakeLists.txt | 2 + src/CMakeLists.txt | 1 + src/graph/CMakeLists.txt | 9 +- src/graph/graph.cpp | 59 +++++++++---- test/CMakeLists.txt | 15 ++-- test/{ => graph}/main.cpp | 0 test/graph/test_graph.cpp | 154 ++++++++++++++++++++++++++++++++++ test/test_graph.cpp | 172 -------------------------------------- 9 files changed, 213 insertions(+), 210 deletions(-) create mode 100644 include/CMakeLists.txt create mode 100644 src/CMakeLists.txt rename test/{ => graph}/main.cpp (100%) create mode 100644 test/graph/test_graph.cpp delete mode 100644 test/test_graph.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 76e8ec1..9a522c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,18 @@ cmake_minimum_required(VERSION 3.20) -set(ProjectName "itlab") +project(itlab_2024) -project(${ProjectName}) +include(cmake/configure.cmake) include_directories(include) +enable_testing() + + add_subdirectory(3rdparty) -add_subdirectory(src/graph) +add_subdirectory(app) +add_subdirectory(include) +add_subdirectory(src) add_subdirectory(test) # REPORT diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 0000000..168bbf0 --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,2 @@ +file(GLOB_RECURSE graph_headers graph/*.h) +set(GRAPH_HEADERS "${graph_headers}" PARENT_SCOPE) \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..eceaa59 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(graph) \ No newline at end of file diff --git a/src/graph/CMakeLists.txt b/src/graph/CMakeLists.txt index af77d7d..fe4c58a 100644 --- a/src/graph/CMakeLists.txt +++ b/src/graph/CMakeLists.txt @@ -1,7 +1,2 @@ -file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h") -file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp") - -add_library(${ProjectName} STATIC ${SOURCE_FILES} ${HEADER_FILES}) -target_sources(${ProjectName} PRIVATE ${HEADER_FILES}) - -target_include_directories(${ProjectName} PUBLIC ${CMAKE_SOURCE_DIR}/src) \ No newline at end of file +file(GLOB_RECURSE graph_src *.cpp) +add_library(graph_lib STATIC "${GRAPH_HEADERS}" "${graph_src}") \ No newline at end of file diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp index 679fcc5..3cd9d99 100644 --- a/src/graph/graph.cpp +++ b/src/graph/graph.cpp @@ -1,17 +1,20 @@ #include "./graph/graph.h" #include -#include #include +#include +#include #include Vertex::Vertex(int id_) : id_(id_) {} void Vertex::addNeighbor(int neighbor) { - if (neighbor != id_) { neighbors_.push_back(neighbor); } + if (neighbor != id_) { + neighbors_.push_back(neighbor); + } } -void Vertex::removeNeighbor(int neighbor) { neighbors_.remove(neighbor); } +void Vertex::removeNeighbor(int neighbor) { neighbors_.remove(neighbor); } void Vertex::print() const { std::cout << id_ << ": "; @@ -21,19 +24,21 @@ void Vertex::print() const { std::cout << '\n'; } -int Vertex::getId() const { return id_; } +int Vertex::getId() const { return id_; } -const std::list& Vertex::getNeighbors() const { return neighbors_; } +const std::list& Vertex::getNeighbors() const { return neighbors_; } Graph::Graph() = default; void Graph::addVertex(int id_) { - if (vertices_.find(id_) == vertices_.end()) { vertices_[id_] = new Vertex(id_); } + if (vertices_.find(id_) == vertices_.end()) { + vertices_[id_] = new Vertex(id_); + } } void Graph::getVertex() const { if (!this->empty()) { - for (auto& vertice : vertices_) { + for (const auto& vertice : vertices_) { std::cout << vertice.first << " "; } std::cout << '\n'; @@ -41,13 +46,19 @@ void Graph::getVertex() const { } void Graph::addEdge(int u, int v) { - if (vertices_.find(u) == vertices_.end()) { addVertex(u); } - if (vertices_.find(v) == vertices_.end()) { addVertex(v); } + if (vertices_.find(u) == vertices_.end()) { + addVertex(u); + } + if (vertices_.find(v) == vertices_.end()) { + addVertex(v); + } vertices_[u]->addNeighbor(v); } void Graph::removeEdge(int u, int v) { - if (vertices_.find(u) != vertices_.end()) { vertices_[u]->removeNeighbor(v); } + if (vertices_.find(u) != vertices_.end()) { + vertices_[u]->removeNeighbor(v); + } } void Graph::removeVertex(int id_) { @@ -63,7 +74,7 @@ void Graph::removeVertex(int id_) { int Graph::vertexCount() const { int count = 0; - for (auto& vertice : vertices_) { + for (const auto& vertice : vertices_) { count++; } return count; @@ -71,17 +82,22 @@ int Graph::vertexCount() const { int Graph::edgeCount() const { int count = 0; - for (auto it = vertices_.begin(); it != vertices_.end(); it++) count += (it->second->getNeighbors()).size(); + for (const auto& vertice : vertices_) { + count += (vertice.second->getNeighbors()).size(); + } return count; } -bool Graph::empty() const { return vertices_.empty(); } +bool Graph::empty() const { return vertices_.empty(); } void Graph::printGraph() const { - for (const auto& pair : vertices_) { pair.second->print(); } + for (const auto& pair : vertices_) { + pair.second->print(); + } } -bool Graph::bfs_helper(int start, int vert, bool flag, std::vector* v_ord) { +bool Graph::bfs_helper(int start, int vert, bool flag, + std::vector* v_ord) { std::unordered_map visited; std::queue queue; queue.push(start); @@ -91,8 +107,12 @@ bool Graph::bfs_helper(int start, int vert, bool flag, std::vector* v_ord) int current = queue.front(); queue.pop(); - if (flag && current == vert) return true; - if (v_ord != nullptr) v_ord->push_back(current); + if (flag && current == vert) { + return true; + } + if (v_ord != nullptr) { + v_ord->push_back(current); + } if (vertices_.find(current) != vertices_.end()) { for (const int& neighbor : vertices_[current]->getNeighbors()) { @@ -107,7 +127,10 @@ bool Graph::bfs_helper(int start, int vert, bool flag, std::vector* v_ord) } bool Graph::hasPath(int u, int v) { - if (vertices_.find(u) == vertices_.end() || vertices_.find(v) == vertices_.end()) return false; + if (vertices_.find(u) == vertices_.end() || + vertices_.find(v) == vertices_.end()) { + return false; + } return bfs_helper(u, v, true, nullptr); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0ab479f..2048a6b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,10 +1,5 @@ -file(GLOB_RECURSE TEST_FILES ./*.cpp) - -set(TestsName "run_tests") - -add_executable(${TestsName} ${TEST_FILES}) - -target_link_libraries(${TestsName} PRIVATE ${ProjectName} gtest) - -enable_testing() -add_test(NAME ${TestsName} COMMAND ${TestsName}) \ No newline at end of file +file(GLOB_RECURSE TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) +add_executable(run_tests ${TEST_SRC_FILES}) +target_link_libraries(run_tests PUBLIC + gtest_main +) \ No newline at end of file diff --git a/test/main.cpp b/test/graph/main.cpp similarity index 100% rename from test/main.cpp rename to test/graph/main.cpp diff --git a/test/graph/test_graph.cpp b/test/graph/test_graph.cpp new file mode 100644 index 0000000..daeecf0 --- /dev/null +++ b/test/graph/test_graph.cpp @@ -0,0 +1,154 @@ +#include + +#include "graph/graph.h" +#include "gtest/gtest.h" + +TEST(Graph, can_create_graph) { ASSERT_NO_THROW(Graph g); } + +TEST(Graph, can_add_vertex_to_graph) { + Graph g; + + ASSERT_NO_THROW(g.addVertex(1)); +} + +TEST(Graph, can_add_edge_to_graph) { + Graph g; + + ASSERT_NO_THROW(g.addEdge(1, 0)); +} + +TEST(Graph, can_add_edge_with_same_id_to_graph) { + Graph g; + + ASSERT_NO_THROW(g.addEdge(1, 1)); +} + +TEST(Graph, can_add_vertex_and_edge_to_graph) { + Graph g; + + ASSERT_NO_THROW(g.addEdge(1, 0)); + ASSERT_NO_THROW(g.addVertex(1)); +} + +TEST(Graph, can_get_vertex) { + Graph g; + g.addEdge(1, 0); + + ASSERT_NO_THROW(g.getVertex()); +} + +TEST(Graph, can_remove_vertex) { + Graph g; + g.addEdge(1, 0); + + g.removeVertex(1); + + ASSERT_EQ(g.vertexCount(), 1); +} + +TEST(Graph, can_get_vertex_count) { + Graph g; + + g.addEdge(1, 0); + g.addVertex(2); + g.addVertex(3); + g.removeVertex(1); + g.addVertex(1); + g.removeVertex(3); + + ASSERT_EQ(g.vertexCount(), 3); +} + +TEST(Graph, can_get_edge_count) { + Graph g; + + g.addEdge(1, 0); + g.addEdge(2, 0); + g.addEdge(0, 2); + g.addEdge(2, 2); + + ASSERT_EQ(g.edgeCount(), 3); +} + +TEST(Graph, check_graph_is_empty) { + Graph g; + + ASSERT_TRUE(g.empty()); +} + +TEST(Graph, check_graph_is_not_empty) { + Graph g; + + g.addEdge(1, 0); + + ASSERT_FALSE(g.empty()); +} + +TEST(Graph, check_graph_no_path_between_vertexes) { + Graph g; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + g.addEdge(3, 1); + + ASSERT_FALSE(g.hasPath(1, 0)); +} + +TEST(Graph, check_graph_has_path_between_vertexes) { + Graph g; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + g.addEdge(3, 1); + + ASSERT_TRUE(g.hasPath(3, 1)); +} + +TEST(Graph, check_graph_can_find_path_between_vertexes_after_delete) { + Graph g; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + g.addEdge(3, 1); + g.removeVertex(3); + + ASSERT_FALSE(g.hasPath(3, 1)); + ASSERT_FALSE(g.hasPath(0, 3)); +} + +TEST(Graph, can_create_bfs_path) { + Graph g; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + + ASSERT_NO_THROW(g.BFS(0)); +} + +TEST(Graph, can_create_bfs_path_in_empty_graph) { + Graph g; + + ASSERT_NO_THROW(g.BFS(0)); +} + +TEST(Graph, check_bfs_path) { + Graph g; + std::vector v1 = {0, 1, 3, 2, 4, 6, 7, 5, 8, 9}; + + g.addEdge(0, 1); + g.addEdge(0, 3); + g.addEdge(1, 2); + g.addEdge(3, 4); + g.addEdge(4, 5); + g.addEdge(3, 6); + g.addEdge(3, 7); + g.addEdge(7, 8); + g.addEdge(8, 9); + std::vector v = g.BFS(0); + + ASSERT_EQ(v, v1); +} \ No newline at end of file diff --git a/test/test_graph.cpp b/test/test_graph.cpp deleted file mode 100644 index 48942ad..0000000 --- a/test/test_graph.cpp +++ /dev/null @@ -1,172 +0,0 @@ -#include "gtest/gtest.h" -#include "graph/graph.h" -#include - -TEST(Graph, can_create_graph) -{ - ASSERT_NO_THROW(Graph g); -} - -TEST(Graph, can_add_vertex_to_graph) -{ - Graph g; - - ASSERT_NO_THROW(g.addVertex(1)); -} - -TEST(Graph, can_add_edge_to_graph) -{ - Graph g; - - ASSERT_NO_THROW(g.addEdge(1,0)); -} - -TEST(Graph, can_add_edge_with_same_id_to_graph) -{ - Graph g; - - ASSERT_NO_THROW(g.addEdge(1, 1)); -} - -TEST(Graph, can_add_vertex_and_edge_to_graph) -{ - Graph g; - - ASSERT_NO_THROW(g.addEdge(1, 0)); - ASSERT_NO_THROW(g.addVertex(1)); -} - -TEST(Graph, can_get_vertex) -{ - Graph g; - g.addEdge(1, 0); - - ASSERT_NO_THROW(g.getVertex()); -} - -TEST(Graph, can_remove_vertex) -{ - Graph g; - g.addEdge(1, 0); - - g.removeVertex(1); - - ASSERT_EQ(g.vertexCount(),1); -} - -TEST(Graph, can_get_vertex_count) -{ - Graph g; - - g.addEdge(1, 0); - g.addVertex(2); - g.addVertex(3); - g.removeVertex(1); - g.addVertex(1); - g.removeVertex(3); - - ASSERT_EQ(g.vertexCount(), 3); -} - -TEST(Graph, can_get_edge_count) -{ - Graph g; - - g.addEdge(1, 0); - g.addEdge(2, 0); - g.addEdge(0, 2); - g.addEdge(2, 2); - - ASSERT_EQ(g.edgeCount(), 3); -} - -TEST(Graph, check_graph_is_empty) -{ - Graph g; - - ASSERT_TRUE(g.empty()); -} - -TEST(Graph, check_graph_is_not_empty) -{ - Graph g; - - g.addEdge(1, 0); - - ASSERT_FALSE(g.empty()); -} - -TEST(Graph, check_graph_no_path_between_vertexes) -{ - Graph g; - - g.addEdge(0,1); - g.addEdge(0, 3); - g.addEdge(1, 2); - g.addEdge(3, 1); - - ASSERT_FALSE(g.hasPath(1,0)); -} - -TEST(Graph, check_graph_has_path_between_vertexes) -{ - Graph g; - - g.addEdge(0, 1); - g.addEdge(0, 3); - g.addEdge(1, 2); - g.addEdge(3, 1); - - ASSERT_TRUE(g.hasPath(3,1)); -} - -TEST(Graph, check_graph_can_find_path_between_vertexes_after_delete) -{ - Graph g; - - g.addEdge(0, 1); - g.addEdge(0, 3); - g.addEdge(1, 2); - g.addEdge(3, 1); - g.removeVertex(3); - - ASSERT_FALSE(g.hasPath(3, 1)); - ASSERT_FALSE(g.hasPath(0, 3)); -} - -TEST(Graph, can_create_bfs_path) -{ - Graph g; - - g.addEdge(0, 1); - g.addEdge(0, 3); - g.addEdge(1, 2); - - ASSERT_NO_THROW(g.BFS(0)); -} - -TEST(Graph, can_create_bfs_path_in_empty_graph) -{ - Graph g; - - ASSERT_NO_THROW(g.BFS(0)); -} - -TEST(Graph, check_bfs_path) -{ - Graph g; - std::vector v1 = { 0,1,3,2,4,6,7,5,8,9 }; - - g.addEdge(0, 1); - g.addEdge(0, 3); - g.addEdge(1, 2); - g.addEdge(3, 4); - g.addEdge(4, 5); - g.addEdge(3,6); - g.addEdge(3, 7); - g.addEdge(7,8); - g.addEdge(8, 9); - std::vector v = g.BFS(0); - - ASSERT_EQ(v, v1); -} \ No newline at end of file From fdfe528d6172f701e8e6122b220ce7f1e4095112 Mon Sep 17 00:00:00 2001 From: Vlad Date: Thu, 27 Feb 2025 19:36:51 +0300 Subject: [PATCH 08/17] CMake, builts, clang format/tidy fix 3.0 --- CMakeLists.txt | 13 +++++-------- include/CMakeLists.txt | 2 -- src/CMakeLists.txt | 1 - src/graph/CMakeLists.txt | 9 +++++++-- src/graph/graph.cpp | 8 +------- test/CMakeLists.txt | 15 ++++++++++----- test/graph/main.cpp | 3 +-- 7 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 include/CMakeLists.txt delete mode 100644 src/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a522c9..cf34635 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,15 @@ cmake_minimum_required(VERSION 3.20) -project(itlab_2024) - -include(cmake/configure.cmake) +set(ProjectName "itlab") +project(${ProjectName}) include_directories(include) -enable_testing() +include_directories(include) -add_subdirectory(3rdparty) -add_subdirectory(app) -add_subdirectory(include) -add_subdirectory(src) +add_subdirectory(3rdparty/googletest) +add_subdirectory(src/graph) add_subdirectory(test) # REPORT diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt deleted file mode 100644 index 168bbf0..0000000 --- a/include/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -file(GLOB_RECURSE graph_headers graph/*.h) -set(GRAPH_HEADERS "${graph_headers}" PARENT_SCOPE) \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index eceaa59..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(graph) \ No newline at end of file diff --git a/src/graph/CMakeLists.txt b/src/graph/CMakeLists.txt index fe4c58a..af77d7d 100644 --- a/src/graph/CMakeLists.txt +++ b/src/graph/CMakeLists.txt @@ -1,2 +1,7 @@ -file(GLOB_RECURSE graph_src *.cpp) -add_library(graph_lib STATIC "${GRAPH_HEADERS}" "${graph_src}") \ No newline at end of file +file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h") +file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp") + +add_library(${ProjectName} STATIC ${SOURCE_FILES} ${HEADER_FILES}) +target_sources(${ProjectName} PRIVATE ${HEADER_FILES}) + +target_include_directories(${ProjectName} PUBLIC ${CMAKE_SOURCE_DIR}/src) \ No newline at end of file diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp index 3cd9d99..d29061b 100644 --- a/src/graph/graph.cpp +++ b/src/graph/graph.cpp @@ -72,13 +72,7 @@ void Graph::removeVertex(int id_) { } } -int Graph::vertexCount() const { - int count = 0; - for (const auto& vertice : vertices_) { - count++; - } - return count; -} +int Graph::vertexCount() const { return static_cast(vertices_.size()); } int Graph::edgeCount() const { int count = 0; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2048a6b..48c524f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,10 @@ -file(GLOB_RECURSE TEST_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) -add_executable(run_tests ${TEST_SRC_FILES}) -target_link_libraries(run_tests PUBLIC - gtest_main -) \ No newline at end of file +file(GLOB_RECURSE TEST_FILES ./*.cpp) + +set(TestsName "run_tests") + +add_executable(${TestsName} ${TEST_FILES}) + +target_link_libraries(${TestsName} PRIVATE ${ProjectName} gtest) + +enable_testing() +add_test(NAME ${TestsName} COMMAND ${TestsName}) \ No newline at end of file diff --git a/test/graph/main.cpp b/test/graph/main.cpp index d6efe14..9a17845 100644 --- a/test/graph/main.cpp +++ b/test/graph/main.cpp @@ -1,7 +1,6 @@ #include "gtest/gtest.h" - -int main(int argc, char **argv) { +int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } \ No newline at end of file From 116cf4a3505b7fc15a3690c197d0da19d5301d81 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 4 Mar 2025 18:08:12 +0300 Subject: [PATCH 09/17] tensor add 1.0 --- CMakeLists.txt | 2 +- include/tensor/tensor.h | 42 ++++++++++++++++++ src/tensor/CMakeLists.txt | 7 +++ src/tensor/tensor.cpp | 74 +++++++++++++++++++++++++++++++ test/CMakeLists.txt | 3 +- test/tensor/test_tensor.cpp | 88 +++++++++++++++++++++++++++++++++++++ 6 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 include/tensor/tensor.h create mode 100644 src/tensor/CMakeLists.txt create mode 100644 src/tensor/tensor.cpp create mode 100644 test/tensor/test_tensor.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cf34635..62c28f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(${ProjectName}) include_directories(include) -include_directories(include) +enable_testing() add_subdirectory(3rdparty/googletest) diff --git a/include/tensor/tensor.h b/include/tensor/tensor.h new file mode 100644 index 0000000..f58414b --- /dev/null +++ b/include/tensor/tensor.h @@ -0,0 +1,42 @@ +#ifndef TENSOR_H +#define TENSOR_H + +#include +#include +#include +#include +#include +#include +#include +#include + +struct Shape { + std::vector dimensions; + size_t total_elements; + + Shape(std::vector dims); + + size_t get_rank() const; +}; + +enum class Layout { NCHW, NHWC, ND }; + +template +class Tensor { + public: + Shape shape; + Layout layout; + std::vector data; + Tensor(const Shape &sh, Layout l = Layout::ND); + Tensor(std::vector dims, Layout l = Layout::ND); + + size_t get_linear_index(const std::vector &indices) const; + + T &at(const std::vector &indices); + const T &at(const std::vector &indices) const; + +}; + +#endif + + \ No newline at end of file diff --git a/src/tensor/CMakeLists.txt b/src/tensor/CMakeLists.txt new file mode 100644 index 0000000..af77d7d --- /dev/null +++ b/src/tensor/CMakeLists.txt @@ -0,0 +1,7 @@ +file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h") +file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp") + +add_library(${ProjectName} STATIC ${SOURCE_FILES} ${HEADER_FILES}) +target_sources(${ProjectName} PRIVATE ${HEADER_FILES}) + +target_include_directories(${ProjectName} PUBLIC ${CMAKE_SOURCE_DIR}/src) \ No newline at end of file diff --git a/src/tensor/tensor.cpp b/src/tensor/tensor.cpp new file mode 100644 index 0000000..07cab2b --- /dev/null +++ b/src/tensor/tensor.cpp @@ -0,0 +1,74 @@ +#include "./tensor/tensor.h" + +Shape::Shape(std::vector dims) : dimensions(std::move(dims)) { + total_elements = std::accumulate(dimensions.begin(), dimensions.end(), 1ULL, + std::multiplies<>()); +} + +size_t Shape::get_rank() const { return dimensions.size(); } + +template +Tensor::Tensor(const Shape &sh, Layout l) + : shape(sh), layout(l), data(sh.total_elements) { + if (sh.get_rank() == 4 && l == Layout::ND) { + std::cerr << "4D Tensor created with ND layout." << std::endl; + } +} + +template +Tensor::Tensor(std::vector dims, Layout l) + : Tensor(Shape(std::move(dims)), l) {} + + +template +size_t Tensor::get_linear_index(const std::vector &indices) const { + if (indices.size() != shape.get_rank()) { + throw std::runtime_error("Incorrect number of indices provided."); + } + for (size_t i = 0; i < indices.size(); ++i) { + if (indices[i] >= shape.dimensions[i]) { + throw std::out_of_range("Index out of range for dimension"); + } + } + + size_t linear_index = 0; + size_t stride = 1; + + if (shape.get_rank() == 4) { + if (layout == Layout::NCHW) { + linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] * + shape.dimensions[3]) + + indices[1] * (shape.dimensions[2] * shape.dimensions[3]) + + indices[2] * shape.dimensions[3] + indices[3]; + } else if (layout == Layout::NHWC) { + linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] * + shape.dimensions[3]) + + indices[1] * (shape.dimensions[2] * shape.dimensions[3]) + + indices[2] * shape.dimensions[3] + indices[3]; + } else { + linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] * + shape.dimensions[3]) + + indices[1] * (shape.dimensions[2] * shape.dimensions[3]) + + indices[2] * shape.dimensions[3] + indices[3]; + } + } else { + std::vector reversed_dims = shape.dimensions; + std::reverse(reversed_dims.begin(), reversed_dims.end()); + for (int i = static_cast(reversed_dims.size()) - 1; i >= 0; --i) { + linear_index += indices[i] * stride; + stride *= reversed_dims[i]; + } + } + + return linear_index; +} + +template +T &Tensor::at(const std::vector &indices) { + return data[get_linear_index(indices)]; +} + +template +const T &Tensor::at(const std::vector &indices) const { + return data[get_linear_index(indices)]; +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 48c524f..049ad49 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,7 @@ + file(GLOB_RECURSE TEST_FILES ./*.cpp) -set(TestsName "run_tests") +set(TestsName "run_test") add_executable(${TestsName} ${TEST_FILES}) diff --git a/test/tensor/test_tensor.cpp b/test/tensor/test_tensor.cpp new file mode 100644 index 0000000..3e11630 --- /dev/null +++ b/test/tensor/test_tensor.cpp @@ -0,0 +1,88 @@ +#include "gtest/gtest.h" +#include "./tensor/tensor.h" +#include "./tensor/tensor.cpp" + +TEST(ShapeTest, ConstructorAndGetRank) { + + Shape s({2, 3, 4}); + + ASSERT_EQ(s.get_rank(), 3); + ASSERT_EQ(s.total_elements, 24); +} + +TEST(TensorTestDouble, ConstructorAndAccess) { + + Tensor t({2, 3}, Layout::ND); + t.at({0, 0}) = 1.0; + t.at({0, 1}) = 2.0; + t.at({0, 2}) = 3.0; + t.at({1, 0}) = 4.0; + t.at({1, 1}) = 5.0; + t.at({1, 2}) = 6.0; + + ASSERT_DOUBLE_EQ(t.at({0, 0}), 1.0); + ASSERT_DOUBLE_EQ(t.at({0, 1}), 2.0); + ASSERT_DOUBLE_EQ(t.at({0, 2}), 4.0); + ASSERT_DOUBLE_EQ(t.at({1, 0}), 4.0); + ASSERT_DOUBLE_EQ(t.at({1, 1}), 5.0); + ASSERT_DOUBLE_EQ(t.at({1, 2}), 6.0); + + const Tensor &ct = t; + + ASSERT_DOUBLE_EQ(ct.at({0, 1}), 2.0); +} + +TEST(TensorTestDouble, GetLinearIndex2D_ND) { + + Tensor t({2, 3}, Layout::ND); + + ASSERT_EQ(t.get_linear_index({0, 0}), 0); + ASSERT_EQ(t.get_linear_index({0, 2}), 2); + ASSERT_EQ(t.get_linear_index({1, 0}), 2); + ASSERT_EQ(t.get_linear_index({1, 2}), 4); +} + +TEST(TensorTestDouble, GetLinearIndex4D_NCHW) { + + Tensor t({2, 3, 4, 5}, Layout::NCHW); + + ASSERT_EQ(t.get_linear_index({0, 0, 0, 0}), 0); + ASSERT_EQ(t.get_linear_index({1, 2, 3, 4}), 119); +} + +TEST(TensorTestDouble, GetLinearIndex4D_NHWC) { + + Tensor t({2, 3, 4, 5}, Layout::NHWC); + + ASSERT_EQ(t.get_linear_index({0, 0, 0, 0}), 0); + ASSERT_EQ(t.get_linear_index({1, 2, 3, 4}), 119); +} +TEST(TensorTestDouble, GetLinearIndex4D_ND) { + + Tensor t4d_nd({2, 3, 4, 5}, Layout::ND); + + ASSERT_EQ(t4d_nd.get_linear_index({0, 0, 0, 0}), 0); + ASSERT_EQ(t4d_nd.get_linear_index({1, 2, 3, 4}), 119); +} + +TEST(TensorTestDouble, GetLinearIndex_OutOfBounds) { + + Tensor t2d({2, 3}, Layout::ND); + Tensor t4d_nchw({2, 3, 4, 5}, Layout::NCHW); + Tensor t4d_nhwc({2, 3, 4, 5}, Layout::NHWC); + Tensor t4d_nd({2, 3, 4, 5}, Layout::ND); + + EXPECT_THROW(t2d.get_linear_index({2, 0}), std::out_of_range); + EXPECT_THROW(t2d.get_linear_index({0, 3}), std::out_of_range); + EXPECT_THROW(t4d_nchw.get_linear_index({2, 0, 0, 0}), std::out_of_range); + EXPECT_THROW(t4d_nhwc.get_linear_index({0, 3, 0, 0}), std::out_of_range); + EXPECT_THROW(t4d_nd.get_linear_index({0, 0, 4, 0}), std::out_of_range); +} + +TEST(TensorTestDouble, GetLinearIndex_WrongNumberOfIndices) { + Tensor t2d({2, 3}, Layout::ND); + Tensor t4d_nchw({2, 3, 4, 5}, Layout::NCHW); + + EXPECT_THROW(t2d.get_linear_index({0}), std::runtime_error); + EXPECT_THROW(t4d_nchw.get_linear_index({0, 0, 0}), std::runtime_error); +} \ No newline at end of file From 55a9ee81433669d51cd48d0d5f23aa5439578b9b Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 23 Mar 2025 20:52:10 +0300 Subject: [PATCH 10/17] graph pr fix --- include/tensor/tensor.h | 42 ------------------ src/tensor/CMakeLists.txt | 7 --- src/tensor/tensor.cpp | 74 ------------------------------- test/tensor/test_tensor.cpp | 88 ------------------------------------- 4 files changed, 211 deletions(-) delete mode 100644 include/tensor/tensor.h delete mode 100644 src/tensor/CMakeLists.txt delete mode 100644 src/tensor/tensor.cpp delete mode 100644 test/tensor/test_tensor.cpp diff --git a/include/tensor/tensor.h b/include/tensor/tensor.h deleted file mode 100644 index f58414b..0000000 --- a/include/tensor/tensor.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef TENSOR_H -#define TENSOR_H - -#include -#include -#include -#include -#include -#include -#include -#include - -struct Shape { - std::vector dimensions; - size_t total_elements; - - Shape(std::vector dims); - - size_t get_rank() const; -}; - -enum class Layout { NCHW, NHWC, ND }; - -template -class Tensor { - public: - Shape shape; - Layout layout; - std::vector data; - Tensor(const Shape &sh, Layout l = Layout::ND); - Tensor(std::vector dims, Layout l = Layout::ND); - - size_t get_linear_index(const std::vector &indices) const; - - T &at(const std::vector &indices); - const T &at(const std::vector &indices) const; - -}; - -#endif - - \ No newline at end of file diff --git a/src/tensor/CMakeLists.txt b/src/tensor/CMakeLists.txt deleted file mode 100644 index af77d7d..0000000 --- a/src/tensor/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -file(GLOB_RECURSE HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h") -file(GLOB_RECURSE SOURCE_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp") - -add_library(${ProjectName} STATIC ${SOURCE_FILES} ${HEADER_FILES}) -target_sources(${ProjectName} PRIVATE ${HEADER_FILES}) - -target_include_directories(${ProjectName} PUBLIC ${CMAKE_SOURCE_DIR}/src) \ No newline at end of file diff --git a/src/tensor/tensor.cpp b/src/tensor/tensor.cpp deleted file mode 100644 index 07cab2b..0000000 --- a/src/tensor/tensor.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "./tensor/tensor.h" - -Shape::Shape(std::vector dims) : dimensions(std::move(dims)) { - total_elements = std::accumulate(dimensions.begin(), dimensions.end(), 1ULL, - std::multiplies<>()); -} - -size_t Shape::get_rank() const { return dimensions.size(); } - -template -Tensor::Tensor(const Shape &sh, Layout l) - : shape(sh), layout(l), data(sh.total_elements) { - if (sh.get_rank() == 4 && l == Layout::ND) { - std::cerr << "4D Tensor created with ND layout." << std::endl; - } -} - -template -Tensor::Tensor(std::vector dims, Layout l) - : Tensor(Shape(std::move(dims)), l) {} - - -template -size_t Tensor::get_linear_index(const std::vector &indices) const { - if (indices.size() != shape.get_rank()) { - throw std::runtime_error("Incorrect number of indices provided."); - } - for (size_t i = 0; i < indices.size(); ++i) { - if (indices[i] >= shape.dimensions[i]) { - throw std::out_of_range("Index out of range for dimension"); - } - } - - size_t linear_index = 0; - size_t stride = 1; - - if (shape.get_rank() == 4) { - if (layout == Layout::NCHW) { - linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] * - shape.dimensions[3]) + - indices[1] * (shape.dimensions[2] * shape.dimensions[3]) + - indices[2] * shape.dimensions[3] + indices[3]; - } else if (layout == Layout::NHWC) { - linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] * - shape.dimensions[3]) + - indices[1] * (shape.dimensions[2] * shape.dimensions[3]) + - indices[2] * shape.dimensions[3] + indices[3]; - } else { - linear_index = indices[0] * (shape.dimensions[1] * shape.dimensions[2] * - shape.dimensions[3]) + - indices[1] * (shape.dimensions[2] * shape.dimensions[3]) + - indices[2] * shape.dimensions[3] + indices[3]; - } - } else { - std::vector reversed_dims = shape.dimensions; - std::reverse(reversed_dims.begin(), reversed_dims.end()); - for (int i = static_cast(reversed_dims.size()) - 1; i >= 0; --i) { - linear_index += indices[i] * stride; - stride *= reversed_dims[i]; - } - } - - return linear_index; -} - -template -T &Tensor::at(const std::vector &indices) { - return data[get_linear_index(indices)]; -} - -template -const T &Tensor::at(const std::vector &indices) const { - return data[get_linear_index(indices)]; -} \ No newline at end of file diff --git a/test/tensor/test_tensor.cpp b/test/tensor/test_tensor.cpp deleted file mode 100644 index 3e11630..0000000 --- a/test/tensor/test_tensor.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "gtest/gtest.h" -#include "./tensor/tensor.h" -#include "./tensor/tensor.cpp" - -TEST(ShapeTest, ConstructorAndGetRank) { - - Shape s({2, 3, 4}); - - ASSERT_EQ(s.get_rank(), 3); - ASSERT_EQ(s.total_elements, 24); -} - -TEST(TensorTestDouble, ConstructorAndAccess) { - - Tensor t({2, 3}, Layout::ND); - t.at({0, 0}) = 1.0; - t.at({0, 1}) = 2.0; - t.at({0, 2}) = 3.0; - t.at({1, 0}) = 4.0; - t.at({1, 1}) = 5.0; - t.at({1, 2}) = 6.0; - - ASSERT_DOUBLE_EQ(t.at({0, 0}), 1.0); - ASSERT_DOUBLE_EQ(t.at({0, 1}), 2.0); - ASSERT_DOUBLE_EQ(t.at({0, 2}), 4.0); - ASSERT_DOUBLE_EQ(t.at({1, 0}), 4.0); - ASSERT_DOUBLE_EQ(t.at({1, 1}), 5.0); - ASSERT_DOUBLE_EQ(t.at({1, 2}), 6.0); - - const Tensor &ct = t; - - ASSERT_DOUBLE_EQ(ct.at({0, 1}), 2.0); -} - -TEST(TensorTestDouble, GetLinearIndex2D_ND) { - - Tensor t({2, 3}, Layout::ND); - - ASSERT_EQ(t.get_linear_index({0, 0}), 0); - ASSERT_EQ(t.get_linear_index({0, 2}), 2); - ASSERT_EQ(t.get_linear_index({1, 0}), 2); - ASSERT_EQ(t.get_linear_index({1, 2}), 4); -} - -TEST(TensorTestDouble, GetLinearIndex4D_NCHW) { - - Tensor t({2, 3, 4, 5}, Layout::NCHW); - - ASSERT_EQ(t.get_linear_index({0, 0, 0, 0}), 0); - ASSERT_EQ(t.get_linear_index({1, 2, 3, 4}), 119); -} - -TEST(TensorTestDouble, GetLinearIndex4D_NHWC) { - - Tensor t({2, 3, 4, 5}, Layout::NHWC); - - ASSERT_EQ(t.get_linear_index({0, 0, 0, 0}), 0); - ASSERT_EQ(t.get_linear_index({1, 2, 3, 4}), 119); -} -TEST(TensorTestDouble, GetLinearIndex4D_ND) { - - Tensor t4d_nd({2, 3, 4, 5}, Layout::ND); - - ASSERT_EQ(t4d_nd.get_linear_index({0, 0, 0, 0}), 0); - ASSERT_EQ(t4d_nd.get_linear_index({1, 2, 3, 4}), 119); -} - -TEST(TensorTestDouble, GetLinearIndex_OutOfBounds) { - - Tensor t2d({2, 3}, Layout::ND); - Tensor t4d_nchw({2, 3, 4, 5}, Layout::NCHW); - Tensor t4d_nhwc({2, 3, 4, 5}, Layout::NHWC); - Tensor t4d_nd({2, 3, 4, 5}, Layout::ND); - - EXPECT_THROW(t2d.get_linear_index({2, 0}), std::out_of_range); - EXPECT_THROW(t2d.get_linear_index({0, 3}), std::out_of_range); - EXPECT_THROW(t4d_nchw.get_linear_index({2, 0, 0, 0}), std::out_of_range); - EXPECT_THROW(t4d_nhwc.get_linear_index({0, 3, 0, 0}), std::out_of_range); - EXPECT_THROW(t4d_nd.get_linear_index({0, 0, 4, 0}), std::out_of_range); -} - -TEST(TensorTestDouble, GetLinearIndex_WrongNumberOfIndices) { - Tensor t2d({2, 3}, Layout::ND); - Tensor t4d_nchw({2, 3, 4, 5}, Layout::NCHW); - - EXPECT_THROW(t2d.get_linear_index({0}), std::runtime_error); - EXPECT_THROW(t4d_nchw.get_linear_index({0, 0, 0}), std::runtime_error); -} \ No newline at end of file From dfb2fd9150c76032b05ba70ec96812dc5c230e4a Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 25 Mar 2025 22:19:11 +0300 Subject: [PATCH 11/17] codecov fix --- .github/workflows/main.yml | 2 +- test/CMakeLists.txt | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 31adbd5..31129f1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -102,7 +102,7 @@ jobs: cmake --build build --parallel - name: Test run: | - build/bin/run_tests + build/bin/run_test env: CTEST_OUTPUT_ON_FAILURE: 1 - name: Generate lcov Coverage Data diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 049ad49..431473b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,6 +6,3 @@ set(TestsName "run_test") add_executable(${TestsName} ${TEST_FILES}) target_link_libraries(${TestsName} PRIVATE ${ProjectName} gtest) - -enable_testing() -add_test(NAME ${TestsName} COMMAND ${TestsName}) \ No newline at end of file From 74aa3beb87b5eacd0c486b5e655e73f628f28539 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 25 Mar 2025 22:27:09 +0300 Subject: [PATCH 12/17] codecov fix 2 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 31129f1..7eea43f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -102,7 +102,7 @@ jobs: cmake --build build --parallel - name: Test run: | - build/bin/run_test + build/test/run_test env: CTEST_OUTPUT_ON_FAILURE: 1 - name: Generate lcov Coverage Data From e9c92bfa630f992bdffc23e01660066b4e78e454 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 8 Apr 2025 00:10:39 +0300 Subject: [PATCH 13/17] tests fix --- include/graph/graph.h | 5 ++-- src/graph/graph.cpp | 13 ++--------- test/graph/test_graph.cpp | 49 ++++++++++++++++----------------------- 3 files changed, 24 insertions(+), 43 deletions(-) diff --git a/include/graph/graph.h b/include/graph/graph.h index bf2b478..734edc6 100644 --- a/include/graph/graph.h +++ b/include/graph/graph.h @@ -26,12 +26,11 @@ class Graph { public: Graph(); void addVertex(int id_); - void getVertex() const; void addEdge(int u, int v); void removeEdge(int u, int v); void removeVertex(int id_); - int vertexCount() const; - int edgeCount() const; + int getVertices() const; + int getEdges() const; bool empty() const; void printGraph() const; bool bfs_helper(int start, int vert, bool flag, std::vector* v_ord); diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp index d29061b..54d7065 100644 --- a/src/graph/graph.cpp +++ b/src/graph/graph.cpp @@ -36,15 +36,6 @@ void Graph::addVertex(int id_) { } } -void Graph::getVertex() const { - if (!this->empty()) { - for (const auto& vertice : vertices_) { - std::cout << vertice.first << " "; - } - std::cout << '\n'; - } -} - void Graph::addEdge(int u, int v) { if (vertices_.find(u) == vertices_.end()) { addVertex(u); @@ -72,9 +63,9 @@ void Graph::removeVertex(int id_) { } } -int Graph::vertexCount() const { return static_cast(vertices_.size()); } +int Graph::getVertices() const { return static_cast(vertices_.size()); } -int Graph::edgeCount() const { +int Graph::getEdges() const { int count = 0; for (const auto& vertice : vertices_) { count += (vertice.second->getNeighbors()).size(); diff --git a/test/graph/test_graph.cpp b/test/graph/test_graph.cpp index daeecf0..36fd926 100644 --- a/test/graph/test_graph.cpp +++ b/test/graph/test_graph.cpp @@ -3,38 +3,38 @@ #include "graph/graph.h" #include "gtest/gtest.h" -TEST(Graph, can_create_graph) { ASSERT_NO_THROW(Graph g); } - TEST(Graph, can_add_vertex_to_graph) { Graph g; - ASSERT_NO_THROW(g.addVertex(1)); + g.addVertex(1); + + ASSERT_EQ(1, g.getVertices()); } TEST(Graph, can_add_edge_to_graph) { Graph g; - ASSERT_NO_THROW(g.addEdge(1, 0)); + g.addEdge(1, 0); + + ASSERT_EQ(1, g.getEdges()); } -TEST(Graph, can_add_edge_with_same_id_to_graph) { +TEST(Graph, cant_add_edge_with_same_id_to_graph) { Graph g; - ASSERT_NO_THROW(g.addEdge(1, 1)); + g.addEdge(1, 1); + + ASSERT_EQ(0, g.getEdges()); } TEST(Graph, can_add_vertex_and_edge_to_graph) { Graph g; - ASSERT_NO_THROW(g.addEdge(1, 0)); - ASSERT_NO_THROW(g.addVertex(1)); -} - -TEST(Graph, can_get_vertex) { - Graph g; g.addEdge(1, 0); + g.addVertex(1); - ASSERT_NO_THROW(g.getVertex()); + ASSERT_EQ(1, g.getEdges()); + ASSERT_EQ(1, g.getEdges()); } TEST(Graph, can_remove_vertex) { @@ -43,10 +43,10 @@ TEST(Graph, can_remove_vertex) { g.removeVertex(1); - ASSERT_EQ(g.vertexCount(), 1); + ASSERT_EQ(g.getVertices(), 1); } -TEST(Graph, can_get_vertex_count) { +TEST(Graph, can_get_vertices_count) { Graph g; g.addEdge(1, 0); @@ -56,10 +56,10 @@ TEST(Graph, can_get_vertex_count) { g.addVertex(1); g.removeVertex(3); - ASSERT_EQ(g.vertexCount(), 3); + ASSERT_EQ(g.getVertices(), 3); } -TEST(Graph, can_get_edge_count) { +TEST(Graph, can_get_edges_count) { Graph g; g.addEdge(1, 0); @@ -67,7 +67,7 @@ TEST(Graph, can_get_edge_count) { g.addEdge(0, 2); g.addEdge(2, 2); - ASSERT_EQ(g.edgeCount(), 3); + ASSERT_EQ(g.getEdges(), 3); } TEST(Graph, check_graph_is_empty) { @@ -119,20 +119,11 @@ TEST(Graph, check_graph_can_find_path_between_vertexes_after_delete) { ASSERT_FALSE(g.hasPath(0, 3)); } -TEST(Graph, can_create_bfs_path) { - Graph g; - - g.addEdge(0, 1); - g.addEdge(0, 3); - g.addEdge(1, 2); - - ASSERT_NO_THROW(g.BFS(0)); -} - TEST(Graph, can_create_bfs_path_in_empty_graph) { Graph g; + std::vector v1 = {0}; - ASSERT_NO_THROW(g.BFS(0)); + ASSERT_EQ(g.BFS(0),v1); } TEST(Graph, check_bfs_path) { From 375119fd9084c052c820f29debf86cccee9b2253 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 8 Apr 2025 00:19:08 +0300 Subject: [PATCH 14/17] clang-format fix --- test/graph/test_graph.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/graph/test_graph.cpp b/test/graph/test_graph.cpp index 36fd926..d7a399c 100644 --- a/test/graph/test_graph.cpp +++ b/test/graph/test_graph.cpp @@ -122,8 +122,8 @@ TEST(Graph, check_graph_can_find_path_between_vertexes_after_delete) { TEST(Graph, can_create_bfs_path_in_empty_graph) { Graph g; std::vector v1 = {0}; - - ASSERT_EQ(g.BFS(0),v1); + + ASSERT_EQ(g.BFS(0), v1); } TEST(Graph, check_bfs_path) { From 7ae4dce3980e80952e7a6e94b83ce881f0ad289d Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 8 Apr 2025 00:21:23 +0300 Subject: [PATCH 15/17] clang-format fix --- test/graph/test_graph.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/graph/test_graph.cpp b/test/graph/test_graph.cpp index d7a399c..ae5684c 100644 --- a/test/graph/test_graph.cpp +++ b/test/graph/test_graph.cpp @@ -122,7 +122,7 @@ TEST(Graph, check_graph_can_find_path_between_vertexes_after_delete) { TEST(Graph, can_create_bfs_path_in_empty_graph) { Graph g; std::vector v1 = {0}; - + ASSERT_EQ(g.BFS(0), v1); } From 9b79f08e173e83b41d77082460be9a5684557ba0 Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 8 Apr 2025 00:23:03 +0300 Subject: [PATCH 16/17] clang-format fix --- test/graph/test_graph.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/graph/test_graph.cpp b/test/graph/test_graph.cpp index ae5684c..a382832 100644 --- a/test/graph/test_graph.cpp +++ b/test/graph/test_graph.cpp @@ -23,7 +23,7 @@ TEST(Graph, cant_add_edge_with_same_id_to_graph) { Graph g; g.addEdge(1, 1); - + ASSERT_EQ(0, g.getEdges()); } From 38a2cbf9bffdc739ff5dcb65695f643c4c35855a Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 8 Apr 2025 00:31:35 +0300 Subject: [PATCH 17/17] build fix --- .github/workflows/main.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7eea43f..eaf6a59 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,6 +15,7 @@ jobs: submodules: recursive - name: Install dependencies run: | + sudo apt-get update sudo apt-get install -y cmake ninja-build ccache scons - name: ccache uses: hendrikmuhs/ccache-action@v1.2 @@ -37,6 +38,7 @@ jobs: submodules: recursive - name: Install dependencies run: | + sudo apt-get update sudo apt-get install -y cmake ninja-build ccache scons - name: ccache uses: hendrikmuhs/ccache-action@v1.2 @@ -81,6 +83,7 @@ jobs: submodules: true - name: Install dependencies run: | + sudo apt-get update sudo apt-get install -y cmake ninja-build ccache gcovr lcov scons - uses: actions/checkout@v4 with: