-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Федотов Кирилл. Лабораторная работа №2 (#373)
* first commit * fix codestyle * fix codestyle2 * add tests * code review * code review2
- Loading branch information
1 parent
5eb3fc5
commit 671d01c
Showing
7 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |