From 40d254a53bd56a5104205cae2316a5f024f12c55 Mon Sep 17 00:00:00 2001 From: anton Date: Mon, 18 Mar 2024 17:40:02 +0300 Subject: [PATCH 1/7] TimedDoor.h edited --- README.md | 4 ++-- include/TimedDoor.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f19f864..19caea0 100644 --- a/README.md +++ b/README.md @@ -44,13 +44,13 @@ class TimedDoor : public Door { private: DoorTimerAdapter * adapter; int iTimeout; - bool opened; + bool isOpened; public: explicit TimedDoor(int); bool isDoorOpened(); void unlock(); void lock(); - void DoorTimeOut(); + int getTimeOut(); void throwState(); }; diff --git a/include/TimedDoor.h b/include/TimedDoor.h index e1eade2..04429ad 100644 --- a/include/TimedDoor.h +++ b/include/TimedDoor.h @@ -32,13 +32,13 @@ class TimedDoor : public Door { private: DoorTimerAdapter * adapter; int iTimeout; - bool opened; + bool isOpened; public: explicit TimedDoor(int); bool isDoorOpened(); void unlock(); void lock(); - void DoorTimeOut(); + int getTimeOut() const; void throwState(); }; From 84cb7ff2ff2e4c4c393587cea506c9d019b288de Mon Sep 17 00:00:00 2001 From: ThreeToads Date: Thu, 30 May 2024 18:44:03 +0300 Subject: [PATCH 2/7] feat(labs3): add 4 files change and realisation --- include/TimedDoor.h | 2 +- src/TimedDoor.cpp | 60 +++++++++++++++++++++++++++++++++++ src/main.cpp | 2 +- test/tests.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 src/TimedDoor.cpp diff --git a/include/TimedDoor.h b/include/TimedDoor.h index 04429ad..2e4bd8b 100644 --- a/include/TimedDoor.h +++ b/include/TimedDoor.h @@ -1,4 +1,4 @@ -// Copyright 2021 GHA Test Team +// Copyright 2024 Kokin Ivan #ifndef INCLUDE_TIMEDDOOR_H_ #define INCLUDE_TIMEDDOOR_H_ diff --git a/src/TimedDoor.cpp b/src/TimedDoor.cpp new file mode 100644 index 0000000..379fa81 --- /dev/null +++ b/src/TimedDoor.cpp @@ -0,0 +1,60 @@ +// Copyright 2024 Kokin Ivan + +#include +#include +#include + +#include "TimedDoor.h" + +void Timer::sleep(int sec) { + std::this_thread::sleep_for(std::chrono::seconds(sec)); +} + +void Timer::tregister(int sec, TimerClient* timer) { + client = timer; + sleep(sec); + client->Timeout(); + client = nullptr; +} + +DoorTimerAdapter::DoorTimerAdapter(TimedDoor& door) : door(door) {} + +void DoorTimerAdapter::Timeout() { + if (door.isDoorOpened()) { + throw std::runtime_error("Door open so looooooong"); + } +} + +TimedDoor::TimedDoor(int timeout) : iTimeout(timeout), isOpened(false) { + adapter = new DoorTimerAdapter(*this); +} + +bool TimedDoor::isDoorOpened() { + return isOpened; +} + +void TimedDoor::lock() { + if (!isOpened) { + throw std::logic_error("Error! Door locked"); + } + isOpened = false; +} + +void TimedDoor::unlock() { + if (isOpened) { + throw std::logic_error("Error! Door unlocked"); + } + isOpened = true; +} + +int TimedDoor::getTimeOut() const { + return iTimeout; +} + +void TimedDoor::throwState() { + if (isOpened) { + throw std::runtime_error("Error! Opened"); + } else { + throw std::runtime_error("Error! Closed"); + } +} diff --git a/src/main.cpp b/src/main.cpp index 9b83162..0adb4ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -// Copyright 2021 GHA Test Team +// Copyright 2024 Kokin Ivan #include "TimedDoor.h" #include diff --git a/test/tests.cpp b/test/tests.cpp index 605e570..47fc177 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,6 +1,81 @@ -// Copyright 2021 GHA Test Team +// Copyright 2024 Kokin Ivan #include #include #include +#include +#include #include "TimedDoor.h" + +class MockTimerClient : public TimerClient { +public: + MOCK_METHOD(void, Timeout, (), (override)); +}; + +class TimedDoorTest : public ::testing::Test { +protected: + TimedDoor door; + MockTimerClient *timerC; + Timer timer; +public: + TimedDoorTest() : door(1), timer() {} +protected: + void SetUp() override { + timerC = new MockTimerClient(); + } + + void TearDown() override { + delete timerC; + } +}; + +TEST_F(TimedDoorTest, TimedDoorLockedDef) { + EXPECT_FALSE(door.isDoorOpened()); +} + +TEST_F(TimedDoorTest, DoorTimerAdapter) { + DoorTimerAdapter adapter(door); + EXPECT_NO_THROW(adapter.Timeout()); +} + +TEST_F(TimedDoorTest, TimedDoorLock) { + EXPECT_THROW(door.lock(), std::logic_error); +} + +TEST_F(TimedDoorTest, Lock) { + door.unlock(); + door.lock(); + EXPECT_FALSE(door.isDoorOpened()); +} + +TEST_F(TimedDoorTest, opened_exception_throw) { + door.unlock(); + EXPECT_THROW(door.throwState(), std::runtime_error); +} + +TEST_F(TimedDoorTest, throw_lock) { + door.unlock(); + door.lock(); + EXPECT_THROW(door.throwState(), std::runtime_error); +} + +TEST_F(TimedDoorTest, Unclock_test) { + door.unlock(); + EXPECT_THROW(door.unlock(), std::logic_error); +} + +TEST_F(TimedDoorTest, TimerTimeoutCall) { + EXPECT_CALL(*timerC, Timeout()).Times(1); + timer.tregister(100, timerC); +} + +TEST_F(TimedDoorTest, TimeoutThrowsOnUnlocked) { + DoorTimerAdapter adapter(door); + door.unlock(); + EXPECT_THROW(adapter.Timeout(), std::runtime_error); +} + +TEST_F(TimedDoorTest, unlock_door_test) { + door.unlock(); + EXPECT_TRUE(door.isDoorOpened()); +} From ef466d659567938b275497466923d613e3022c66 Mon Sep 17 00:00:00 2001 From: ThreeToads Date: Thu, 30 May 2024 18:46:09 +0300 Subject: [PATCH 3/7] fix(labs3): space before cpplint --- test/tests.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/tests.cpp b/test/tests.cpp index 47fc177..168b045 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -8,18 +8,18 @@ #include "TimedDoor.h" class MockTimerClient : public TimerClient { -public: + public: MOCK_METHOD(void, Timeout, (), (override)); }; class TimedDoorTest : public ::testing::Test { -protected: + protected: TimedDoor door; MockTimerClient *timerC; Timer timer; -public: + public: TimedDoorTest() : door(1), timer() {} -protected: + protected: void SetUp() override { timerC = new MockTimerClient(); } From d2db673fa6499417007c22187533c5acbf460395 Mon Sep 17 00:00:00 2001 From: ThreeToads Date: Thu, 30 May 2024 18:47:02 +0300 Subject: [PATCH 4/7] fix(labs3): add CMakeLists.txt --- CMakeLists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36758e8..db22b17 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,15 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -g") set(tool_dest "bin") set(lib_dest "lib") set(include_dest "include/") +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.10.0 +) +FetchContent_MakeAvailable(googletest) + add_subdirectory(src) add_subdirectory(test) From 695ad956d961f1b94e7846b99af537303ad0f3a6 Mon Sep 17 00:00:00 2001 From: ThreeToads Date: Thu, 30 May 2024 18:51:21 +0300 Subject: [PATCH 5/7] fix(labs3): fix files comments --- src/TimedDoor.cpp | 4 ++-- test/tests.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TimedDoor.cpp b/src/TimedDoor.cpp index 379fa81..b9da692 100644 --- a/src/TimedDoor.cpp +++ b/src/TimedDoor.cpp @@ -1,8 +1,8 @@ // Copyright 2024 Kokin Ivan #include -#include -#include +#include // NOLINT [build/c++11] +#include // NOLINT [build/c++11] #include "TimedDoor.h" diff --git a/test/tests.cpp b/test/tests.cpp index 168b045..4e0fe29 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,7 +1,7 @@ // Copyright 2024 Kokin Ivan -#include -#include +#include // [build/c++11] +#include // [build/c++11] #include #include #include From 68566c4fb926898c4d673142ed2b3e0205df316d Mon Sep 17 00:00:00 2001 From: ThreeToads Date: Thu, 30 May 2024 18:53:16 +0300 Subject: [PATCH 6/7] fix(labs3): fix files comments --- test/tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/tests.cpp b/test/tests.cpp index 4e0fe29..57dc184 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,7 +1,7 @@ // Copyright 2024 Kokin Ivan -#include // [build/c++11] -#include // [build/c++11] +#include // NOLINT [build/c++11] +#include // NOLINT [build/c++11] #include #include #include From ffcee4dae50fb80a7d1e040f29ffec72258f077d Mon Sep 17 00:00:00 2001 From: ThreeToads Date: Thu, 30 May 2024 18:54:12 +0300 Subject: [PATCH 7/7] fix(labs3): fix files comments --- test/tests.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/tests.cpp b/test/tests.cpp index 57dc184..5fff3b3 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,10 +1,10 @@ // Copyright 2024 Kokin Ivan -#include // NOLINT [build/c++11] -#include // NOLINT [build/c++11] +#include +#include #include -#include -#include +#include // NOLINT [build/c++11] +#include // NOLINT [build/c++11] #include "TimedDoor.h" class MockTimerClient : public TimerClient {