Skip to content

Commit

Permalink
Федотов Кирилл. Лабораторная работа №2 (#373)
Browse files Browse the repository at this point in the history
* first commit

* fix codestyle

* fix codestyle2

* add tests

* code review

* code review2
  • Loading branch information
KirFedotov authored May 27, 2024
1 parent 5eb3fc5 commit 671d01c
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 0 deletions.
12 changes: 12 additions & 0 deletions modules/fedotov_k_set/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions modules/fedotov_k_set/include/set.h
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
#include <algorithm>

class Set {
private:
std::vector<int> 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_
18 changes: 18 additions & 0 deletions modules/fedotov_k_set/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)
48 changes: 48 additions & 0 deletions modules/fedotov_k_set/src/set.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
27 changes: 27 additions & 0 deletions modules/fedotov_k_set/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}")
8 changes: 8 additions & 0 deletions modules/fedotov_k_set/test/test_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024 Fedotov Kirill

#include <gtest/gtest.h>

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
139 changes: 139 additions & 0 deletions modules/fedotov_k_set/test/test_set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright 2024 Fedotov Kirill

#include <gtest/gtest.h>
#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));
}

0 comments on commit 671d01c

Please sign in to comment.