Skip to content

Commit

Permalink
Шемякина - Лабораторная работа #2 (#381)
Browse files Browse the repository at this point in the history
* first commit

* second commit

* third commit

* fourth commit

* fifth commit

* sixth commit

* seventh commit

* eighth commit

* nineth commit

* tenth commit

* eleventh commit
  • Loading branch information
alesyacurtis authored Jun 3, 2024
1 parent cd73e54 commit d94ef15
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 0 deletions.
17 changes: 17 additions & 0 deletions modules/graph_comp_shem_a/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
#############################################

12 changes: 12 additions & 0 deletions modules/graph_comp_shem_a/include/graph_components.h
Original file line number Diff line number Diff line change
@@ -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 <vector>

void DFS(int x, std::vector<std::vector<int>>& v, std::vector<int>& was);
int graph_components_numbers(int n, std::vector<std::vector<int>> v);


#endif // MODULES_GRAPH_COMP_SHEM_A_INCLUDE_GRAPH_COMPONENTS_H_
18 changes: 18 additions & 0 deletions modules/graph_comp_shem_a/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions modules/graph_comp_shem_a/src/graph_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Shemiakina Alesia

#include "include/graph_components.h"

#include <vector>

void DFS(int x, std::vector<std::vector<int>>& v, std::vector<int>& 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<std::vector<int>> v) {
std::vector<int> 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;
}
27 changes: 27 additions & 0 deletions modules/graph_comp_shem_a/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")
57 changes: 57 additions & 0 deletions modules/graph_comp_shem_a/test/test_graph_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2024 Shemiakina Alesia

#include <gtest/gtest.h>

#include <tuple>
#include <vector>
#include "include/graph_components.h"

TEST(Shemiakina_Alesia_GraphNumbersTest, returns_1_when_given_2_super) {
std::vector<std::vector<int>> 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<std::vector<int>> 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<std::vector<int>> v{{ }};
int count = graph_components_numbers(1, v);

EXPECT_EQ(count, 1);
}

TEST(Shemiakina_Alesia_GraphNumbersTest, returns_3_when_given_8) {
std::vector<std::vector<int>> 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<std::vector<int>> 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<std::vector<int>> v{ };
int count = graph_components_numbers(0, v);

EXPECT_EQ(count, 0);
}

TEST(Shemiakina_Alesia_GraphNumbersTest, one_sided_list) {
std::vector<std::vector<int>> v{{1}, {2}, {0}};
int count = graph_components_numbers(3, v);

EXPECT_EQ(count, 1);
}
8 changes: 8 additions & 0 deletions modules/graph_comp_shem_a/test/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024 Shemiakina Alesia

#include <gtest/gtest.h>

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit d94ef15

Please sign in to comment.