diff --git a/modules/fedotov_k_set/CMakeLists.txt b/modules/fedotov_k_set/CMakeLists.txt new file mode 100644 index 000000000..c54b3d08b --- /dev/null +++ b/modules/fedotov_k_set/CMakeLists.txt @@ -0,0 +1,12 @@ +# 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) diff --git a/modules/fedotov_k_set/include/set.h b/modules/fedotov_k_set/include/set.h new file mode 100644 index 000000000..384191c0d --- /dev/null +++ b/modules/fedotov_k_set/include/set.h @@ -0,0 +1,27 @@ +// Copyright 2024 Fedotov Kirill +#ifndef MODULES_FEDOTOV_K_SET_INCLUDE_SET_H_ +#define MODULES_FEDOTOV_K_SET_INCLUDE_SET_H_ + +#include +#include +#include +#include +#include +#include + +class Set { + private: + std::vector elements; + + public: + Set(); + + void add(int element); + void remove(int element); + bool contains(int element) const; + Set unionWith(const Set& other) const; + Set intersectWith(const Set& other) const; + void print() const; +}; + +#endif // MODULES_FEDOTOV_K_SET_INCLUDE_SET_H_ diff --git a/modules/fedotov_k_set/src/CMakeLists.txt b/modules/fedotov_k_set/src/CMakeLists.txt new file mode 100644 index 000000000..b1dc57b01 --- /dev/null +++ b/modules/fedotov_k_set/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/fedotov_k_set/src/set.cpp b/modules/fedotov_k_set/src/set.cpp new file mode 100644 index 000000000..22a15ceb4 --- /dev/null +++ b/modules/fedotov_k_set/src/set.cpp @@ -0,0 +1,48 @@ +// Copyright 2024 Fedotov Kirill + +#include "include/set.h" + +Set::Set() {} + +void Set::add(int element) { + if (!contains(element)) { + elements.push_back(element); + } +} + +void Set::remove(int element) { + auto it = std::find(elements.begin(), elements.end(), element); + if (it != elements.end()) { + elements.erase(it); + } +} + +bool Set::contains(int element) const { + return std::find(elements.begin(), elements.end(), element) != elements.end(); +} + +Set Set::unionWith(const Set& other) const { + Set result = *this; + for (int elem : other.elements) { + result.add(elem); + } + return result; +} + +Set Set::intersectWith(const Set& other) const { + Set result; + for (int elem : elements) { + if (other.contains(elem)) { + result.add(elem); + } + } + return result; +} + +void Set::print() const { + std::cout << "{ "; + for (int elem : elements) { + std::cout << elem << " "; + } + std::cout << "}" << std::endl; +} diff --git a/modules/fedotov_k_set/test/CMakeLists.txt b/modules/fedotov_k_set/test/CMakeLists.txt new file mode 100644 index 000000000..cd06c57bb --- /dev/null +++ b/modules/fedotov_k_set/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/fedotov_k_set/test/test_main.cpp b/modules/fedotov_k_set/test/test_main.cpp new file mode 100644 index 000000000..0e63463fc --- /dev/null +++ b/modules/fedotov_k_set/test/test_main.cpp @@ -0,0 +1,8 @@ +// Copyright 2024 Fedotov Kirill + +#include + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/modules/fedotov_k_set/test/test_set.cpp b/modules/fedotov_k_set/test/test_set.cpp new file mode 100644 index 000000000..49ddbff69 --- /dev/null +++ b/modules/fedotov_k_set/test/test_set.cpp @@ -0,0 +1,139 @@ +// Copyright 2024 Fedotov Kirill + +#include +#include "include/set.h" + +TEST(SetTest, AddElements) { + Set set; + set.add(1); + set.add(2); + set.add(3); + + EXPECT_TRUE(set.contains(1)); + EXPECT_TRUE(set.contains(2)); + EXPECT_TRUE(set.contains(3)); + EXPECT_FALSE(set.contains(4)); +} + +TEST(SetTest, RemoveElements) { + Set set; + set.add(1); + set.add(2); + set.add(3); + + set.remove(2); + + EXPECT_TRUE(set.contains(1)); + EXPECT_FALSE(set.contains(2)); + EXPECT_TRUE(set.contains(3)); +} + +TEST(SetTest, UnionWith) { + Set set1; + set1.add(1); + set1.add(2); + set1.add(3); + + Set set2; + set2.add(3); + set2.add(4); + set2.add(5); + + Set unionSet = set1.unionWith(set2); + + EXPECT_TRUE(unionSet.contains(1)); + EXPECT_TRUE(unionSet.contains(2)); + EXPECT_TRUE(unionSet.contains(3)); + EXPECT_TRUE(unionSet.contains(4)); + EXPECT_TRUE(unionSet.contains(5)); +} + +TEST(SetTest, IntersectWith) { + Set set1; + set1.add(1); + set1.add(2); + set1.add(3); + + Set set2; + set2.add(3); + set2.add(4); + set2.add(5); + + Set intersectSet = set1.intersectWith(set2); + + EXPECT_FALSE(intersectSet.contains(1)); + EXPECT_FALSE(intersectSet.contains(2)); + EXPECT_TRUE(intersectSet.contains(3)); + EXPECT_FALSE(intersectSet.contains(4)); + EXPECT_FALSE(intersectSet.contains(5)); +} + +TEST(SetTest, Print) { + Set set; + set.add(1); + set.add(2); + set.add(3); + + testing::internal::CaptureStdout(); + set.print(); + std::string output = testing::internal::GetCapturedStdout(); + + EXPECT_EQ(output, "{ 1 2 3 }\n"); +} + +TEST(SetTest, AddDuplicateElements) { + Set set; + set.add(1); + set.add(1); + set.add(2); + set.add(2); + + EXPECT_TRUE(set.contains(1)); + EXPECT_TRUE(set.contains(2)); + + testing::internal::CaptureStdout(); + set.print(); + std::string output = testing::internal::GetCapturedStdout(); + + EXPECT_EQ(output, "{ 1 2 }\n"); +} + +TEST(SetTest, RemoveFromEmptySet) { + Set set; + set.remove(1); + + EXPECT_FALSE(set.contains(1)); +} + +TEST(SetTest, EmptySet) { + Set set; + EXPECT_FALSE(set.contains(1)); +} + +TEST(SetTest, UnionWithEmptySet) { + Set set1; + set1.add(1); + set1.add(2); + + Set set2; + + Set unionSet = set1.unionWith(set2); + + EXPECT_TRUE(unionSet.contains(1)); + EXPECT_TRUE(unionSet.contains(2)); + EXPECT_FALSE(unionSet.contains(3)); +} + +TEST(SetTest, AddElements2) { + Set set; + set.add(1); + set.add(2); + set.add(3); + set.add(5); + + EXPECT_TRUE(set.contains(1)); + EXPECT_TRUE(set.contains(2)); + EXPECT_TRUE(set.contains(3)); + EXPECT_FALSE(set.contains(4)); + EXPECT_TRUE(set.contains(5)); +}