Skip to content

Commit

Permalink
Коновалов Игорь - Лабораторная работа #3 (#383)
Browse files Browse the repository at this point in the history
* init

* style fix

* import fix
  • Loading branch information
IgorKonovalovAleks authored Jun 8, 2024
1 parent 77721d7 commit 0b18972
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/graph_comp_shem_a/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ get_filename_component(DIR_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
set(MODULE "${DIR_NAME}")
set(LIBRARY "lib_${MODULE}")
set(TESTS "test_${MODULE}")
set(APPLICATION "app_${MODULE}")

# Include directory with public headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# Add all submodules
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(application)

#############################################
##### Testing
#############################################

include("CTestTests.txt")
69 changes: 69 additions & 0 deletions modules/graph_comp_shem_a/CTestTests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#############################################
##### Testing
#############################################

set(prefix "${MODULE}")

add_test(
NAME ${prefix}_run
COMMAND ${APPLICATION}
)
set_tests_properties (${prefix}_run PROPERTIES
LABELS "${MODULE}")

add_test(
NAME ${prefix}_thelp
COMMAND ${APPLICATION} --help
)
set_tests_properties (${prefix}_thelp PROPERTIES
PASS_REGULAR_EXPRESSION "This program calculates number of graph components"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_test_one
COMMAND ${APPLICATION} 2 1 0
)
set_tests_properties (${prefix}_test_one PROPERTIES
PASS_REGULAR_EXPRESSION "1"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_test_two
COMMAND ${APPLICATION} 5 1 0,2 1 4 3
)
set_tests_properties (${prefix}_test_two PROPERTIES
PASS_REGULAR_EXPRESSION "2"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_test_three
COMMAND ${APPLICATION} 1
)
set_tests_properties (${prefix}_test_three PROPERTIES
PASS_REGULAR_EXPRESSION "1"
LABELS "${MODULE}")


add_test(
NAME ${prefix}_test_one_naive
COMMAND ${APPLICATION} 8 1,7 0,6 3 2,4 3 1,7 0,6
)
set_tests_properties (${prefix}_test_one_naive PROPERTIES
PASS_REGULAR_EXPRESSION "3"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_test_two_naive
COMMAND ${APPLICATION} 10 1 0,2 3,1 4,2 5,3 6,4 7,5 8,6 9,7 8
)
set_tests_properties (${prefix}_test_two_naive PROPERTIES
PASS_REGULAR_EXPRESSION "1"
LABELS "${MODULE}")

add_test(
NAME ${prefix}_test_three_naive
COMMAND ${APPLICATION} 0
)
set_tests_properties (${prefix}_test_three_naive PROPERTIES
PASS_REGULAR_EXPRESSION "0"
LABELS "${MODULE}")
15 changes: 15 additions & 0 deletions modules/graph_comp_shem_a/application/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set(target ${APPLICATION})

file(GLOB srcs "*.cpp")
set_source_files_properties(${srcs} PROPERTIES
LABELS "${MODULE};Application")

add_executable(${target} ${srcs})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${MODULE}
LABELS "${MODULE};Application")

target_link_libraries(${target} ${LIBRARY})
if (UNIX)
target_link_libraries(${target} ${CMAKE_THREAD_LIBS_INIT})
endif (UNIX)
12 changes: 12 additions & 0 deletions modules/graph_comp_shem_a/application/graph_components.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 Konovalov Igor

#include <iostream>

#include "include/graph_components_app.h"

int main(int argc, char* argv[]) {
auto output = GraphComponentsApplication()(argc, argv);

std::cout << output << '\n';
return 0;
}
20 changes: 20 additions & 0 deletions modules/graph_comp_shem_a/include/graph_components_app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 Konovalov Igor

#pragma once

#include <string>

#include "include/graph_components.h"

class GraphComponentsApplication {
public:
GraphComponentsApplication() = default;
std::string operator()(int argc, char* argv[]);

private:
int n{};
int result{};
std::string message_{};
bool validate(int argc, char* argv[]);
void help(const char* appName, const char* msg = nullptr);
};
1 change: 1 addition & 0 deletions modules/graph_comp_shem_a/src/graph_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "include/graph_components.h"

#include <vector>
#include <iostream>

void DFS(int x, std::vector<std::vector<int>>& v, std::vector<int>& was) {
was[x] = 1;
Expand Down
48 changes: 48 additions & 0 deletions modules/graph_comp_shem_a/src/graph_components_app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Konovalov Igor

#include "include/graph_components_app.h"

#include <cstring>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <vector>

bool GraphComponentsApplication::validate(int argc, char* argv[]) {
if (argc == 1) {
help(argv[0]);
return false;
} else if (std::strcmp(argv[1], "--help") == 0) {
help(argv[0]);
return false;
}
n = std::stoi(argv[1]);
return true;
}

void GraphComponentsApplication::help(const char* appName, const char* msg) {
std::stringstream message;

if (msg) message << "Error: " << msg << '\n';

message << "This program calculates number of graph components";
message_ = message.str();
}

std::string GraphComponentsApplication::operator()(int argc, char* argv[]) {
bool b = validate(argc, argv);
std::vector<std::vector<int>> v(n);
for (int i = 2; i < argc; i++) {
std::istringstream ss;
ss.str(argv[i]);
for (std::string s; std::getline(ss, s, ','); ) {
v[i - 2].push_back(std::stoi(s));
}
}
if (b) {
std::stringstream str;
str << graph_components_numbers(n, v);
message_ = str.str();
}
return message_;
}

0 comments on commit 0b18972

Please sign in to comment.