diff --git a/modules/graph_comp_shem_a/CMakeLists.txt b/modules/graph_comp_shem_a/CMakeLists.txt new file mode 100644 index 000000000..6bd24443b --- /dev/null +++ b/modules/graph_comp_shem_a/CMakeLists.txt @@ -0,0 +1,17 @@ +# Declare variables for binaries' names +get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +set(MODULE "${DIR_NAME}") +set(LIBRARY "lib_${MODULE}") +set(TESTS "test_${MODULE}") + +# Include directory with public headers +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +# Add all submodules +add_subdirectory(src) +add_subdirectory(test) + +############################################# +##### Testing +############################################# + diff --git a/modules/graph_comp_shem_a/include/graph_components.h b/modules/graph_comp_shem_a/include/graph_components.h new file mode 100644 index 000000000..3e2bcacfd --- /dev/null +++ b/modules/graph_comp_shem_a/include/graph_components.h @@ -0,0 +1,12 @@ +// Copyright 2024 Shemiakina Alesia + +#ifndef MODULES_GRAPH_COMP_SHEM_A_INCLUDE_GRAPH_COMPONENTS_H_ +#define MODULES_GRAPH_COMP_SHEM_A_INCLUDE_GRAPH_COMPONENTS_H_ + +#include + +void DFS(int x, std::vector>& v, std::vector& was); +int graph_components_numbers(int n, std::vector> v); + + +#endif // MODULES_GRAPH_COMP_SHEM_A_INCLUDE_GRAPH_COMPONENTS_H_ diff --git a/modules/graph_comp_shem_a/src/CMakeLists.txt b/modules/graph_comp_shem_a/src/CMakeLists.txt new file mode 100644 index 000000000..b1dc57b01 --- /dev/null +++ b/modules/graph_comp_shem_a/src/CMakeLists.txt @@ -0,0 +1,18 @@ +set(target ${LIBRARY}) + +file(GLOB srcs "*.cpp") +file(GLOB hdrs "../include/*.h") +set_source_files_properties(${srcs} ${hdrs} PROPERTIES + LABELS "${MODULE};Library") + +add_library(${target} STATIC ${srcs} ${hdrs}) +set_target_properties(${target} PROPERTIES + OUTPUT_NAME ${MODULE} + LABELS "${MODULE};Library") + +if (UNIX) + target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT}) +endif (UNIX) +target_link_libraries(${target} ${LIBRARY_DEPS}) + +set(LIBRARY_DEPS "${LIBRARY_DEPS};${target}" PARENT_SCOPE) diff --git a/modules/graph_comp_shem_a/src/graph_components.cpp b/modules/graph_comp_shem_a/src/graph_components.cpp new file mode 100644 index 000000000..14b8b3662 --- /dev/null +++ b/modules/graph_comp_shem_a/src/graph_components.cpp @@ -0,0 +1,27 @@ +// Copyright 2024 Shemiakina Alesia + +#include "include/graph_components.h" + +#include + +void DFS(int x, std::vector>& v, std::vector& was) { + was[x] = 1; + for (int y : v[x]) + if (was[y] == 0) + DFS(y, v, was); +} + +int graph_components_numbers(int n, std::vector> v) { + std::vector was(n); + for (int x = 0; x < n; x++) { + was[x] = 0; + } + int ans = 0; + for (int x = 0; x < n; x++) { + if (was[x] == 0) { + DFS(x, v, was); + ans++; + } + } + return ans; +} diff --git a/modules/graph_comp_shem_a/test/CMakeLists.txt b/modules/graph_comp_shem_a/test/CMakeLists.txt new file mode 100644 index 000000000..cd06c57bb --- /dev/null +++ b/modules/graph_comp_shem_a/test/CMakeLists.txt @@ -0,0 +1,27 @@ +set(target ${TESTS}) + +file(GLOB srcs "*.cpp") +set_source_files_properties(${srcs} PROPERTIES + LABELS "${MODULE};Test") + +add_executable(${target} ${srcs} ${hdrs}) +set_target_properties(${target} PROPERTIES + LABELS "${MODULE};Test") + +if (UNIX) + target_link_libraries(${target} gtest ${CMAKE_THREAD_LIBS_INIT} pthread) +endif (UNIX) +target_link_libraries(${target} gtest ${LIBRARY}) + +# VS2012 doesn't support correctly the tuples yet, +# see http://code.google.com/p/googletest/issues/detail?id=412 +if(MSVC) + target_compile_definitions(${target} PUBLIC _VARIADIC_MAX=10) +endif() + +add_test( + NAME ${MODULE}_gtest + COMMAND ${target} +) +set_tests_properties (${MODULE}_gtest PROPERTIES + LABELS "${MODULE}") diff --git a/modules/graph_comp_shem_a/test/test_graph_components.cpp b/modules/graph_comp_shem_a/test/test_graph_components.cpp new file mode 100644 index 000000000..7efc4a09b --- /dev/null +++ b/modules/graph_comp_shem_a/test/test_graph_components.cpp @@ -0,0 +1,57 @@ +// Copyright 2024 Shemiakina Alesia + +#include + +#include +#include +#include "include/graph_components.h" + +TEST(Shemiakina_Alesia_GraphNumbersTest, returns_1_when_given_2_super) { + std::vector> v{{1}, {0}}; + int count = graph_components_numbers(2, v); + EXPECT_EQ(1, count); +} + +TEST(Shemiakina_Alesia_GraphNumbersTest, returns_2_when_given_5) { + std::vector> v{{1}, {0, 2}, {1}, {4}, {3}}; + int count = graph_components_numbers(5, v); + + EXPECT_EQ(count, 2); +} + +TEST(Shemiakina_Alesia_GraphNumbersTest, returns_1_when_given_1) { + std::vector> v{{ }}; + int count = graph_components_numbers(1, v); + + EXPECT_EQ(count, 1); +} + +TEST(Shemiakina_Alesia_GraphNumbersTest, returns_3_when_given_8) { + std::vector> v{{1, 7}, {0, 6}, {3}, {2, 4}, + {3}, { }, {1, 7}, {0, 6}}; + int count = graph_components_numbers(8, v); + + EXPECT_EQ(count, 3); +} + +TEST(Shemiakina_Alesia_GraphNumbersTest, returns_1_when_given_10) { + std::vector> v{{1}, {0, 2}, {3, 1}, {4, 2}, {5, 3}, + {6, 4}, {7, 5}, {8, 6}, {9, 7}, {8}}; + int count = graph_components_numbers(10, v); + + EXPECT_EQ(count, 1); +} + +TEST(Shemiakina_Alesia_GraphNumbersTest, returns_0_when_given_0) { + std::vector> v{ }; + int count = graph_components_numbers(0, v); + + EXPECT_EQ(count, 0); +} + +TEST(Shemiakina_Alesia_GraphNumbersTest, one_sided_list) { + std::vector> v{{1}, {2}, {0}}; + int count = graph_components_numbers(3, v); + + EXPECT_EQ(count, 1); +} diff --git a/modules/graph_comp_shem_a/test/test_main.cpp b/modules/graph_comp_shem_a/test/test_main.cpp new file mode 100644 index 000000000..473a26d55 --- /dev/null +++ b/modules/graph_comp_shem_a/test/test_main.cpp @@ -0,0 +1,8 @@ +// Copyright 2024 Shemiakina Alesia + +#include + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}